• Create Account

## packing two floats to one and back in HLSL/CG

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.

2 replies to this topic

### #1ayanami0  Members

139
Like
1Likes
Like

Posted 01 April 2013 - 04:57 AM

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

### #2Hodgman  Moderators

49387
Like
4Likes
Like

Posted 01 April 2013 - 05:24 AM

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

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;

### #3ayanami0  Members

139
Like
1Likes
Like

Posted 01 April 2013 - 06:39 AM

It works!

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

Bless you,
James

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.