diff --git a/gl_engine/Window.cpp b/gl_engine/Window.cpp index 78df9628..ae811b0f 100644 --- a/gl_engine/Window.cpp +++ b/gl_engine/Window.cpp @@ -57,6 +57,7 @@ #include "nucleus/timing/TimerManager.h" #include "nucleus/timing/TimerInterface.h" #include "nucleus/timing/CpuTimer.h" +#include "nucleus/utils/bit_coding.h" #if (defined(__linux) && !defined(__ANDROID__)) || defined(_WIN32) || defined(_WIN64) #include "GpuAsyncQueryTimer.h" #endif @@ -120,7 +121,7 @@ void Window::initialise_gpu() TextureDefinition{ Framebuffer::ColourFormat::RGB8 }, // Albedo TextureDefinition{ Framebuffer::ColourFormat::RGBA32F }, // Position WCS and distance (distance is optional, but i use it directly for a little speed improvement) TextureDefinition{ Framebuffer::ColourFormat::RG16UI }, // Octahedron Normals - TextureDefinition{ Framebuffer::ColourFormat::R32UI } // Discretized Encoded Depth for readback IMPORTANT: IF YOU MOVE THIS YOU HAVE TO ADAPT THE GET DEPTH FUNCTION + TextureDefinition{ Framebuffer::ColourFormat::RGBA8 } // Discretized Encoded Depth for readback IMPORTANT: IF YOU MOVE THIS YOU HAVE TO ADAPT THE GET DEPTH FUNCTION }); m_atmospherebuffer = std::make_unique(Framebuffer::DepthFormat::None, std::vector{ TextureDefinition{Framebuffer::ColourFormat::RGBA8} }); @@ -440,9 +441,8 @@ void Window::update_gpu_quads(const std::vectorread_colour_attachment_pixel(3, normalised_device_coordinates, &fakeNormalizedDepth); - const auto depth = float(std::exp(double(fakeNormalizedDepth) / 4294967295.0 * 13.0)); + const auto read_float = nucleus::utils::bit_coding::to_f16f16(m_gbuffer->read_colour_attachment_pixel(3, normalised_device_coordinates))[0]; + const auto depth = std::exp(read_float * 13.f); return depth; } diff --git a/gl_engine/shaders/encoder.glsl b/gl_engine/shaders/encoder.glsl index 55531a25..a86122a1 100644 --- a/gl_engine/shaders/encoder.glsl +++ b/gl_engine/shaders/encoder.glsl @@ -25,14 +25,12 @@ highp float fakeNormalizeWSDepth(highp float depth) { return log(depth) / 13.0; } -highp float fakeUnNormalizeWSDepth(highp float ndepth) { - return exp(13.0 * ndepth); -} -highp uint depthWSEncode1u32(highp float depth) { - return uint(fakeNormalizeWSDepth(depth) * 4294967295.0); -} -highp float depthWSDecode1u32(highp uint depth32) { - return fakeUnNormalizeWSDepth(float(depth32) / 4294967295.0); +lowp vec2 depthWSEncode2n8(highp float depth) { + highp float value = fakeNormalizeWSDepth(depth); + mediump uint scaled = uint(value * 65535.f + 0.5f); + mediump uint r = scaled >> 8u; + mediump uint b = scaled & 255u; + return vec2(float(r) / 255.f, float(b) / 255.f); } // ===== OCTAHEDRON MAPPING FOR NORMALS ===== diff --git a/gl_engine/shaders/snow.glsl b/gl_engine/shaders/snow.glsl index 11216ea6..9eed417d 100644 --- a/gl_engine/shaders/snow.glsl +++ b/gl_engine/shaders/snow.glsl @@ -22,24 +22,24 @@ #define ORIGIN_SHIFT 20037508.3427892430765884 #define PI 3.1415926535897932384626433 -mediump float mod289(mediump float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } -mediump vec4 mod289(mediump vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } -mediump vec4 perm(mediump vec4 x) { return mod289(((x * 34.0) + 1.0) * x); } +highp float mod289(highp float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } +highp vec4 mod289(highp vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } +highp vec4 perm(highp vec4 x) { return mod289(((x * 34.0) + 1.0) * x); } -mediump float noise(mediump vec3 p){ - mediump vec3 a = floor(p); - mediump vec3 d = p - a; +highp float noise(highp vec3 p){ + highp vec3 a = floor(p); + highp vec3 d = p - a; d = d * d * (3.0 - 2.0 * d); - mediump vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); - mediump vec4 k1 = perm(b.xyxy); - mediump vec4 k2 = perm(k1.xyxy + b.zzww); - mediump vec4 c = k2 + a.zzzz; - mediump vec4 k3 = perm(c); - mediump vec4 k4 = perm(c + 1.0); - mediump vec4 o1 = fract(k3 * (1.0 / 41.0)); - mediump vec4 o2 = fract(k4 * (1.0 / 41.0)); - mediump vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); - mediump vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); + highp vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); + highp vec4 k1 = perm(b.xyxy); + highp vec4 k2 = perm(k1.xyxy + b.zzww); + highp vec4 c = k2 + a.zzzz; + highp vec4 k3 = perm(c); + highp vec4 k4 = perm(c + 1.0); + highp vec4 o1 = fract(k3 * (1.0 / 41.0)); + highp vec4 o2 = fract(k4 * (1.0 / 41.0)); + highp vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); + highp vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); return o4.y * d.y + o4.x * (1.0 - d.y); } diff --git a/gl_engine/shaders/tile.frag b/gl_engine/shaders/tile.frag index 4d2dbaa0..5acbe6d1 100644 --- a/gl_engine/shaders/tile.frag +++ b/gl_engine/shaders/tile.frag @@ -26,7 +26,7 @@ uniform sampler2D texture_sampler; layout (location = 0) out lowp vec3 texout_albedo; layout (location = 1) out highp vec4 texout_position; layout (location = 2) out highp uvec2 texout_normal; -layout (location = 3) out highp uint texout_depth; +layout (location = 3) out lowp vec4 texout_depth; in highp vec2 uv; in highp vec3 var_pos_cws; @@ -69,7 +69,7 @@ void main() { texout_normal = octNormalEncode2u16(normal); // Write and encode distance for readback - texout_depth = depthWSEncode1u32(dist); + texout_depth = vec4(depthWSEncode2n8(dist), 0.0, 0.0); // HANDLE OVERLAYS (and mix it with the albedo color) THAT CAN JUST BE DONE IN THIS STAGE // (because of DATA thats not forwarded)