# Packing a float value to and from RGB in a shader.

## Recommended Posts

Slagh    148
I've edited this post and the thread name a bit, now that I've worked out what people search for. I've worked out the following HLSL code to turn a float value between 0 and 1 into 3 components ready to write to an 8-bit RGB texture:
   const float max24int = 256*256*256-1;
float3 decomp = floor( value * float3( max24int/(256*256), max24int/256, max24int ) ) / 255.0;
decomp.z -= decomp.y * 256.0;
decomp.y -= decomp.x * 256.0;

The recomposition on the other end is the usual dot product:
   float value = dot( decomp.xyz, float3( 255.0/256, 255.0/(256*256), 255.0/(256*256*256) ) );

Obviously the recomposition is about as good as it gets but I feel that there might be a better way to decompose than what I have above. [Edited by - Slagh on March 3, 2008 6:33:31 PM]

##### Share on other sites
jaafit    116
Not sure what the difference between this and the above is, but if you want to use RGBA, here's another method that I got from searching these same forums. If someone wants to explain the difference and pros/cons, I'd love to hear about them and I assume Slagh would as well.

float4 packFactors = float4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);float4 unpackFactors = float4( 1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0 );float4 bitMask = float4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);float4 Pack( float rawValue ){		float4 packedValue = float4(frac(packFactors*rawValue));	packedValue -= packedValue.xxyz * bitMask;	return packedValue;}float Unpack(float4 encodedValue){	return dot(encodedValue,unpackFactors);}

##### Share on other sites
Slagh    148
Quote:
 Original post by jaafitNot sure what the difference between this and the above is, but if you want to use RGBA, here's another method that I got from searching these same forums. If someone wants to explain the difference and pros/cons, I'd love to hear about them and I assume Slagh would as well.*** Source Snippet Removed ***

I tried this code out in my test bed and found that after the packed color was written to an RT and then read in another pass, there was a 'discontinuity' around 0.5.

The use of frac is actually quite similar to my earlier attempts, but I found that I had a lot of trouble guessing how the hardware would rescale my float values to [0 255] before writing to the color target. That led to my rework in the OP.

##### Share on other sites
corysama    342
I just posted a response to a later, but almost identical question under the header

"Encoding 16 and 32 bit floating point value into RGBA byte texture"