Sign in to follow this  
ayanami0

packing two floats to one and back in HLSL/CG

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this