Jump to content

  • Log In with Google      Sign In   
  • Create Account


GLSL Float Packing


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 RPTD   Members   -  Reputation: 311

Like
0Likes
Like

Posted 20 April 2012 - 01:25 PM

If you have for example a conventional 8-bit RGB or RGBA texture you can pack a float (for example depth) in there using some GLSL based encoding/decoding trick. Now I'd like to examine some special case. Let's say we have a 16-bit float RGB or RGBA texture as render target. I have 2 float values which are in the range 0-1 and for which it would be okay to loose precision down to 8-bit or 7-bit. Is there a way to pack 2 float values into 1 16-bit float texture color component? Since that way a single GBuffer texture could hold 8 float values of limited accuracy. Somebody said one to use "float output = floor( floatValue1 * 255.0 ) + floatValue2;". Somehow I don't think that would do the trick as 16-bit float is not stored in a way for this to have much of an effect. I presume the sign bit, exponent bits and a couple of value bits would have to be used to store one value and the rest for the second.
Life's like a Hydra... cut off one problem just to have two more popping out.
Leader and Coder: Project Epsylon | Drag[en]gine Game Engine

Sponsor:

#2 Tsus   Members   -  Reputation: 978

Like
0Likes
Like

Posted 20 April 2012 - 02:53 PM

Hi!

What about packing 8 floats into an GL_RG32UI? (two unsigned ints with each 4 byte, which means 8 bit per float.)
This could save you some instructions, since you could pack with the little helper below 4 floats simultaneously (instead of just 2 if you would use 16-bit floats).

Would that help you?

// vec4 to rgba8Unorm
uint pack(vec4 value)
{
	// Ensure values are in [0..1] and make NaNs become zeros.
	value = min(max(value,0.0f), 1.0f);

	// Each component gets 8 bit.
	value = value * 255 + 0.5f;	
	value = floor(value);

	// Pack into one 32 bit uint.
	return	 (((uint)value.x)	  |
			(((uint)value.y)<< 8) |
			(((uint)value.z)<<16) |
			(((uint)value.w)<<24) );
}

// rgba8Unorm to vec4
vec4 unpack(uint value)
{
	return vec4((float)  (value	  & 0x000000ff) / 255,
				(float) ((value>> 8) & 0x000000ff) / 255,
				(float) ((value>>16) & 0x000000ff) / 255,
				(float) ((value>>24) & 0x000000ff) / 255);
}
(Haven't checked the code... Just ported from HLSL to GLSL.)

Cheers!

Acagamics e.V. – IGDA Student Game Development Club (University of Magdeburg, Germany)


#3 RPTD   Members   -  Reputation: 311

Like
0Likes
Like

Posted 20 April 2012 - 04:17 PM

Not an option unfortunately since you can't use multiple render targets bound to an FBO which are of different texture format. I need RGBA_F16 so my only option is packing 2 floats into each F16 component there.
Life's like a Hydra... cut off one problem just to have two more popping out.
Leader and Coder: Project Epsylon | Drag[en]gine Game Engine

#4 tanzanite7   Members   -  Reputation: 1124

Like
0Likes
Like

Posted 20 April 2012 - 11:11 PM

Not an option unfortunately since you can't use multiple render targets bound to an FBO which are of different texture format.

Since when?

There was (Not sure whether it still is (*). I think the limitation was lifted, but not sure) a requirement of all targets having the same total bits per texel - but no requirements on how the bits are used (ie. R32F is the same size as RGBA8).

*) After searching around a bit: not sure when, but seems the last restriction was lifted at some point - so, any and all of the required formats ( http://www.opengl.org/wiki/Image_Formats#Texture_and_Renderbuffer ) are OK. have not yet used mismatching (ex: RGBA8 + RGBA32F) ones myself.

#5 pcmaster   Members   -  Reputation: 644

Like
0Likes
Like

Posted 23 April 2012 - 01:48 AM

In DX10+ it's perfectly legal and usual to bind several render targets of completely different formats and bit depths and numbers of channels (such as RGBA8 + RGBA32F + RG16F + whatever) to output merger simultaneously and get perfect results. The only restriction is the same resolution (width * height * array slices). I don't see why this wouldn't work with OpenGL 3+ as SM4.0+ HW apparently supports this.

#6 Hodgman   Moderators   -  Reputation: 27080

Like
0Likes
Like

Posted 23 April 2012 - 04:15 AM

Is there a way to pack 2 float values into 1 16-bit float texture color component? Since that way a single GBuffer texture could hold 8 float values of limited accuracy. Somebody said one to use "float output = floor( floatValue1 * 255.0 ) + floatValue2;". Somehow I don't think that would do the trick as 16-bit float is not stored in a way for this to have much of an effect. I presume the sign bit, exponent bits and a couple of value bits would have to be used to store one value and the rest for the second.

I've never done this, but it should be possible. All the required details should be on the wikipedia page on floating point (and on logarithms). I imagine a packing function would look something like:
half PackHalf( float A/*one bit*/, float B/*5 bits*/, float C/*10 bits*/ )//all inputs in 0-1 range

{

  float s = A*2-1;// -1/+1

  float e = lerp( -13, 14, B );

  float f = lerp( 1024, 2047, C );

  return s*f*pow(2,e);

}//N.B. most likely contains mistakes


#7 RPTD   Members   -  Reputation: 311

Like
0Likes
Like

Posted 23 April 2012 - 03:41 PM

In DX10+ it's perfectly legal and usual to bind several render targets of completely different formats and bit depths and numbers of channels (such as RGBA8 + RGBA32F + RG16F + whatever) to output merger simultaneously and get perfect results. The only restriction is the same resolution (width * height * array slices). I don't see why this wouldn't work with OpenGL 3+ as SM4.0+ HW apparently supports this.

Because in the original specs FBOs didn't allow for such combinations and the five an FBO Incomplete error. There's some extension I think that is supposed to remove this restriction but I had no luck so far getting a mix to work reliably across different hardware so I'm hesitant to rely on such format combination although it would be of an advantage.

Is there a way to pack 2 float values into 1 16-bit float texture color component? Since that way a single GBuffer texture could hold 8 float values of limited accuracy. Somebody said one to use "float output = floor( floatValue1 * 255.0 ) + floatValue2;". Somehow I don't think that would do the trick as 16-bit float is not stored in a way for this to have much of an effect. I presume the sign bit, exponent bits and a couple of value bits would have to be used to store one value and the rest for the second.

I've never done this, but it should be possible. All the required details should be on the wikipedia page on floating point (and on logarithms). I imagine a packing function would look something like:
half PackHalf( float A/*one bit*/, float B/*5 bits*/, float C/*10 bits*/ )//all inputs in 0-1 range
{
  float s = A*2-1;// -1/+1
  float e = lerp( -13, 14, B );
  float f = lerp( 1024, 2047, C );
  return s*f*pow(2,e);
}//N.B. most likely contains mistakes

I see where you come from with this one. So an 8+8 bit (or 7+7 bit) encoding doesn't seem possible, only a 5+10 encoding.
Life's like a Hydra... cut off one problem just to have two more popping out.
Leader and Coder: Project Epsylon | Drag[en]gine Game Engine

#8 tanzanite7   Members   -  Reputation: 1124

Like
0Likes
Like

Posted 25 April 2012 - 06:24 PM

Because in the original specs FBOs didn't allow for such combinations and the five an FBO Incomplete error. There's some extension I think that is supposed to remove this restriction but I had no luck so far getting a mix to work reliably across different hardware so I'm hesitant to rely on such format combination although it would be of an advantage.

The original specs did allow mixing, for example: RGBA8 + RG16F + R32F (ie. all 32bit formats). The restriction was lifted in one of the OpenGl releases (ie. no extension of any sort is needed. it is core.) - however, i was not able to track down exactly which OGL version it was :/. Probably starting with 3.0?

#9 RPTD   Members   -  Reputation: 311

Like
0Likes
Like

Posted 26 April 2012 - 12:56 PM

Would be a possibility. I'm currently using 3.0 as the smallest required OGL version for the graphic module. I'm certainly gonna test it again. Maybe nVidia doesn't mess up with newer drivers anymore like it did so far. That said how much "working" do you expect from a graphic card that can't do linear filtering on a cube map without crashing U_U .
Life's like a Hydra... cut off one problem just to have two more popping out.
Leader and Coder: Project Epsylon | Drag[en]gine Game Engine




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