There's solutions to convert float to RGBA but I would like to encode a float depth value in RGB format and save the three components in three separated unused alpha channels and recompose when needed. I prefer 24bit float because the decoded value is to be compared to regular 24+8 bit depthstencil buffer.
there's this code for float to RGBA (https://gamedev.net/forums/topic/684158-rgba-to-float-percision/5321338/)
vec4 pack(float depth)
{
const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const vec4 bitMsk = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
vec4 comp = fract(depth * bitSh);
comp -= comp.xxyz * bitMsk;
return comp;
}
float unpack(vec2 pos)
{
vec4 packedZValue = texture2D(shadow_map, pos);
const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1);
float shadow = dot(packedZValue , bitShifts);
return shadow;
}
I tried to change to vec3 by removing the first component as below but get weird results (see picture below).
Note: left panels are showing the alpha chanel were the depth is stored as 3 component (1rt = x, 3rd = y and 4th = z, second is something else) by default the alpha channel is 1 (so it appears white) as it is for depth buffer and I perform a alpha OP_MIN test to write new value as if it was comparison_less. So we can see something in panel 1/3 but in 4 it is still white suggesting that the z value is 1 or more.
vec3 pack(float depth)
{
const vec3 bitSh = vec3(256.0 * 256.0, 256.0, 1.0);
const vec3 bitMsk = vec3(0, 1.0 / 256.0, 1.0 / 256.0);
vec3 comp = fract(depth * bitSh);
comp -= comp.xyz * bitMsk;
return comp;
}
float unpack()
{
vec3 packedZValue = float3(textureA.a, textureB.a, textureC.a);
const vec3 bitShifts = vec4(1.0 / (256.0 * 256.0), 1.0 / 256.0, 1);
float shadow = dot(packedZValue , bitShifts);
return shadow;
}

