saving floating point value to int render target

shuipi

Newcomer
Hi
I need to store a float depth value computed in shader into the RGB channels of an A8R8G8B8 render target, and later I need to read this render target as a texture in another shader then extract the float depth value. Are these functions correct?

float3 pack(float z_norm) //z_norm is cam space z normalized to 0 ~ 1
{
float z_normLS24 = z_norm * 16777216.f;
float quotient16 = floor(z_normLS24 / 65536.f);
float remainder16 = z_normLS24 - quotient16 * 65536.f;
float quotient8 = floor(remainder16 / 256.f);
float remainder8 = remainder16 - quotient8 * 256.f;
return float3(quotient16 / 255.f, quotient8 / 255.f, remainder8 / 255.f);
}
float unpack(float3 packed_z_norm)
{
return (packed_z_norm.x * 255.f * 65536.f + packed_z_norm.y * 255.f * 256.f + packed_z_norm.z * 255) / 16777216.f;
}
 
Back
Top