Jump to content

  • Log In with Google      Sign In   
  • Create Account

Packing a generic float into a rgba 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.

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

#1 Ivo Leitao   Members   -  Reputation: 168

Like
0Likes
Like

Posted 19 October 2010 - 05:12 AM

Hi,

I've searched the forums and the web for an answer to the topic above but I was able to find only code to do it for numbers between 0 and 1. I wonder if it's possible to encode a generic float (for example 2.3, 12.33454 or -0.4) in a rgba texture (A8R8G8B8 32 bits). I know that I can use float textures and I'm using them, however for old graphic cards like the integrated intel cards that are still in a lot of notebooks using the float format is not possible. I've compiled a couple of links bellow concerning this topic, but none of them answer my problem

http://www.gamedev.net/community/forums/topic.asp?topic_id=442138
http://www.gamedev.net/community/forums/topic.asp?topic_id=573743
http://www.gamedev.net/community/forums/topic.asp?topic_id=486847
http://www.gamedev.net/community/forums/topic.asp?topic_id=573743
http://www.gamedev.net/community/forums/topic.asp?topic_id=322318
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=265044
http://www.gamerendering.com/2008/09/25/packing-a-float-value-in-rgba/
http://theinstructionlimit.com/?p=33
http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
http://www.gamedev.net/community/forums/topic.asp?topic_id=498533
http://gamedusa.blogspot.com/2010/03/pack-normal-and-height-data-in-one.html

I wanto to pack in HLSL or GLSL and unpack in C#

[Edited by - Ivo Leitao on October 19, 2010 12:41:24 PM]

Sponsor:

#2 smasherprog   Members   -  Reputation: 432

Like
0Likes
Like

Posted 19 October 2010 - 06:06 AM

What do you mean an rgba texture? What format? You can create several types of float rgba formats http://msdn.microsoft.com/en-us/library/bb173059%28VS.85%29.aspx

pick which one you want and create it.


#3 Ivo Leitao   Members   -  Reputation: 168

Like
0Likes
Like

Posted 19 October 2010 - 06:12 AM

Quote:
Original post by smasherprog
What do you mean an rgba texture? What format? You can create several types of float rgba formats http://msdn.microsoft.com/en-us/library/bb173059%28VS.85%29.aspx

pick which one you want and create it.


I'm sorry I forgot to mention I was referring to A8R8G8B8 32 bit format.

tnks for your answer



#4 JorenJoestar   Members   -  Reputation: 173

Like
0Likes
Like

Posted 19 October 2010 - 06:26 AM

Hi Ivo,
there are a bunch of links around that can be useful:

http://diaryofagraphicsprogrammer.blogspot.com/2009/10/bitmasks-packing-data-into-fp-render.html

http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/

http://theinstructionlimit.com/?p=33

Hope those helps!


#5 Ivo Leitao   Members   -  Reputation: 168

Like
0Likes
Like

Posted 19 October 2010 - 06:39 AM

Quote:
Original post by JorenJoestar
Hi Ivo,
there are a bunch of links around that can be useful:

http://diaryofagraphicsprogrammer.blogspot.com/2009/10/bitmasks-packing-data-into-fp-render.html

http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/

http://theinstructionlimit.com/?p=33

Hope those helps!


Tnks for your answer but I've already reviewed all those links (two of them are on my list) but I was hoping for a way to do that with 32 bit ARGB textures where we have 8 bits per channel. I would like to split a float into 4 parts of 8 bits each and unpack it from those channels into a float.

Those links (specially the last two) store numbers in the [0..1) range I would like to store arbitrary floats. I don't know even if that is possible.

#6 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
0Likes
Like

Posted 19 October 2010 - 06:57 AM

Quote:
Original post by Ivo Leitao
Those links (specially the last two) store numbers in the [0..1) range I would like to store arbitrary floats. I don't know even if that is possible.

Just divide the float by the largest possible number you know that can appear. This is often a good practice even for FP formats, since best precision is in the [0; 1] range.

For example if you're doing shadow mapping, divide all values by the far clipping plane of your shadow camera.

When packing to an integer texture you will always lose some precision, so the division won't actually hurt.

Cheers
Dark Sylinc

#7 Ivo Leitao   Members   -  Reputation: 168

Like
0Likes
Like

Posted 19 October 2010 - 07:42 AM

Quote:
Original post by Matias Goldberg
Quote:
Original post by Ivo Leitao
Those links (specially the last two) store numbers in the [0..1) range I would like to store arbitrary floats. I don't know even if that is possible.

Just divide the float by the largest possible number you know that can appear. This is often a good practice even for FP formats, since best precision is in the [0; 1] range.

For example if you're doing shadow mapping, divide all values by the far clipping plane of your shadow camera.

When packing to an integer texture you will always lose some precision, so the division won't actually hurt.

Cheers
Dark Sylinc


Hum, I'm going to test it tonight Tkns. One question, what you have mentioned can be applied also to negative values for example -2.3 ?

Tnks for your answer

#8 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
0Likes
Like

Posted 19 October 2010 - 07:50 AM

Yes.
Suppose you know the minimum negative value is -50, and the maximum positive value is 112.
Then you do:
x = (x + 50) / (50 + 112)

That's it! Simple math to get the value in the range [0; 1]

Cheers
Dark Sylinc

#9 Ivo Leitao   Members   -  Reputation: 168

Like
0Likes
Like

Posted 19 October 2010 - 12:48 PM

Quote:
Original post by Matias Goldberg
Yes.
Suppose you know the minimum negative value is -50, and the maximum positive value is 112.
Then you do:
x = (x + 50) / (50 + 112)

That's it! Simple math to get the value in the range [0; 1]

Cheers
Dark Sylinc


Tnks a lot!

I've not made many tests yet (too sleepy :-)) but it seems to work. I've incorporated Ysaneya code from http://www.gamedev.net/community/forums/topic.asp?topic_id=463075&whichpage=1�. Bellow in HLSL :



float4 PackToFloat4(float value)
{
const float4 bitSh = float4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const float4 bitMsk = float4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
float4 res = frac(value * bitSh);
res -= res.xxyz * bitMsk;

return res;
}

float4 PackToFloat4(float value, float min, float max)
{
return PackToFloat4((value - min) / (max - min));
}

float UnpackToFloat(float4 value)
{
const float4 bitSh = float4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);

return dot(value, bitSh);
}

float UnpackToFloat(float4 value, float min, float max)
{
return UnpackToFloat(value) * (max - min) + min;
}






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