• Advertisement
Sign in to follow this  

packing two floats to one and back in HLSL/CG

This topic is 1758 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this issue similar to this has been dealt with, but most of them are only one way. what I need being able to pack two 8-bit floats: A,B range[0~1] to a single 8-bit float C, and expanding C back to a less accurate A_ and B_. the resulting A_, and B_ can have accuracy of only 4-bit or 3-bit. I've tried many methods from this forum, incuding the ones used for z-depth. but most of them only return an usable data, but the other one is destroyed. Thanks for help

Share this post


Link to post
Share on other sites
Advertisement

I haven't tried this code (just typed it into the forum), but it might be worth a try wink.png

This should pack two 0-1 range floats into the hi/low 4 bits of an 8 bit fraction.

//quantize from 0-1 floats, to 0-15 integers, which can be represented in 4 bits
a = round(a*15);
b = round(b*15);
//bit shift a into the upper 4 bits of the fraction, and b into the lower 4 bits
float c = dot( float2(a,b), float2(1.0/(255.0/16.0), 1.0/255.0 ) );
return c to an 8-bit render target

float c = tex2d(...) //point sampling and/or tex-coord at exact texel centres.
//shift so that a is in the integer part and b in the fractional part
float temp = c * 255.0/16.0;
//reconstruct the original (but quantized) a&b
float a = floor(temp) / 15.0;
float b = frac(temp) * 16.0/15.0;

Share this post


Link to post
Share on other sites

It works! 

I can't believe you just write it out so casually and worked!  I envy you, Hodgman!

Bless you,
James

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement