Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Slagh   Members   -  Reputation: 148

Like
0Likes
Like

Posted 02 March 2008 - 07:01 PM

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]

Sponsor:

#2 jaafit   Members   -  Reputation: 116

Like
0Likes
Like

Posted 04 March 2008 - 09:58 AM

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);
}



#3 Slagh   Members   -  Reputation: 148

Like
0Likes
Like

Posted 11 March 2008 - 04:40 PM

Quote:
Original post by jaafit
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.

*** 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.

#4 corysama   Members   -  Reputation: 316

Like
0Likes
Like

Posted 14 March 2008 - 09:19 PM

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"




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS