From dd73873a7b585b95c81e4d171a5e1bcc15b26449 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 16 Jul 2019 14:05:29 +0200 Subject: [PATCH] Fix packing inline --- compiled/Shaders/Material2_mesh.frag.glsl | 2 +- compiled/Shaders/Material_mesh.frag.glsl | 2 +- compiled/Shaders/armdefault_mesh.frag.glsl | 2 +- compiled/Shaders/deferred_light.frag.glsl | 2 +- .../deferred_light_VoxelAOvar.frag.glsl | 2 +- compiled/Shaders/std/gbuffer.glsl | 51 +++++++++++++------ 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/compiled/Shaders/Material2_mesh.frag.glsl b/compiled/Shaders/Material2_mesh.frag.glsl index 8dab972b..440f9c7a 100644 --- a/compiled/Shaders/Material2_mesh.frag.glsl +++ b/compiled/Shaders/Material2_mesh.frag.glsl @@ -23,7 +23,7 @@ vec3 n = normalize(wnormal); n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy); uint matid = 0; if (emission > 0) { basecol *= emission; matid = 1; } - fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4)); + fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid)); fragColor[1] = vec4(basecol, packFloat2(occlusion, specular)); vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5; vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5; diff --git a/compiled/Shaders/Material_mesh.frag.glsl b/compiled/Shaders/Material_mesh.frag.glsl index c74f4c84..2fc60a9f 100644 --- a/compiled/Shaders/Material_mesh.frag.glsl +++ b/compiled/Shaders/Material_mesh.frag.glsl @@ -30,7 +30,7 @@ vec3 n = normalize(wnormal); n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy); uint matid = 0; if (emission > 0) { basecol *= emission; matid = 1; } - fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4)); + fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid)); fragColor[1] = vec4(basecol, packFloat2(occlusion, specular)); vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5; vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5; diff --git a/compiled/Shaders/armdefault_mesh.frag.glsl b/compiled/Shaders/armdefault_mesh.frag.glsl index cea00eef..4485c201 100644 --- a/compiled/Shaders/armdefault_mesh.frag.glsl +++ b/compiled/Shaders/armdefault_mesh.frag.glsl @@ -23,7 +23,7 @@ vec3 n = normalize(wnormal); n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy); uint matid = 0; if (emission > 0) { basecol *= emission; matid = 1; } - fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4)); + fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid)); fragColor[1] = vec4(basecol, packFloat2(occlusion, specular)); vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5; vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5; diff --git a/compiled/Shaders/deferred_light.frag.glsl b/compiled/Shaders/deferred_light.frag.glsl index ecca78d9..82e58bb9 100644 --- a/compiled/Shaders/deferred_light.frag.glsl +++ b/compiled/Shaders/deferred_light.frag.glsl @@ -174,7 +174,7 @@ void main() { float roughness = g0.b; float metallic; uint matid; - unpackFloatInt16(g0.a, 4, metallic, matid); + unpackFloatInt16(g0.a, metallic, matid); vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ vec2 occspec = unpackFloat2(g1.a); vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor diff --git a/compiled/Shaders/deferred_light_VoxelAOvar.frag.glsl b/compiled/Shaders/deferred_light_VoxelAOvar.frag.glsl index 2dbb2259..9b9b0aea 100644 --- a/compiled/Shaders/deferred_light_VoxelAOvar.frag.glsl +++ b/compiled/Shaders/deferred_light_VoxelAOvar.frag.glsl @@ -175,7 +175,7 @@ void main() { float roughness = g0.b; float metallic; uint matid; - unpackFloatInt16(g0.a, 4, metallic, matid); + unpackFloatInt16(g0.a, metallic, matid); vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ vec2 occspec = unpackFloat2(g1.a); vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor diff --git a/compiled/Shaders/std/gbuffer.glsl b/compiled/Shaders/std/gbuffer.glsl index d7d2f6fc..a4bb2d3e 100644 --- a/compiled/Shaders/std/gbuffer.glsl +++ b/compiled/Shaders/std/gbuffer.glsl @@ -121,8 +121,10 @@ vec3 decNor(uint val) { // GBuffer helper - Sebastien Lagarde // https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/ -float packFloatInt(const float f, const uint i, const uint numBitI, const uint numBitTarget) { +float packFloatInt8(const float f, const uint i) { // Constant optimize by compiler + const int numBitTarget = 8; + const int numBitI = 4; const float prec = float(1 << numBitTarget); const float maxi = float(1 << numBitI); const float precMinusOne = prec - 1.0; @@ -132,8 +134,23 @@ float packFloatInt(const float f, const uint i, const uint numBitI, const uint n return t1 * f + t2 * float(i); } -void unpackFloatInt(const float val, const uint numBitI, const uint numBitTarget, out float f, out uint i) { +float packFloatInt16(const float f, const uint i) { // Constant optimize by compiler + const int numBitTarget = 16; + const int numBitI = 4; + const float prec = float(1 << numBitTarget); + const float maxi = float(1 << numBitI); + const float precMinusOne = prec - 1.0; + const float t1 = ((prec / maxi) - 1.0) / precMinusOne; + const float t2 = (prec / maxi) / precMinusOne; + // Code + return t1 * f + t2 * float(i); +} + +void unpackFloatInt8(const float val, out float f, out uint i) { + // Constant optimize by compiler + const int numBitTarget = 8; + const int numBitI = 4; const float prec = float(1 << numBitTarget); const float maxi = float(1 << numBitI); const float precMinusOne = prec - 1.0; @@ -148,20 +165,22 @@ void unpackFloatInt(const float val, const uint numBitI, const uint numBitTarget f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue } -float packFloatInt8(const float f, const uint i, const uint numBitI) { - return packFloatInt(f, i, numBitI, 8); -} - -void unpackFloatInt8(const float val, const uint numBitI, out float f, out uint i) { - unpackFloatInt(val, numBitI, 8, f, i); -} - -float packFloatInt16(const float f, const uint i, const uint numBitI) { - return packFloatInt(f, i, numBitI, 16); -} - -void unpackFloatInt16(const float val, const uint numBitI, out float f, out uint i) { - unpackFloatInt(val, numBitI, 16, f, i); +void unpackFloatInt16(const float val, out float f, out uint i) { + // Constant optimize by compiler + const int numBitTarget = 16; + const int numBitI = 4; + const float prec = float(1 << numBitTarget); + const float maxi = float(1 << numBitI); + const float precMinusOne = prec - 1.0; + const float t1 = ((prec / maxi) - 1.0) / precMinusOne; + const float t2 = (prec / maxi) / precMinusOne; + // Code + // extract integer part + // + rcp(precMinusOne) to deal with precision issue + i = int((val / t2) + (1.0 / precMinusOne)); + // Now that we have i, solve formula in packFloatInt for f + //f = (val - t2 * float(i)) / t1 => convert in mads form + f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue } #endif