Issue: rgba channels as a float32

Greetings,
I am trying to use the rgba values of a texture (32 bits) in a fragment shader program, as a single 32-bit floating point, without any success.
Every hint is well accepted!
Thanks

uniform sampler2D textureY;
uniform sampler2D textureX;
void main(void) {
float x = float(texture2D(textureY,gl_TexCoord[0].st).rgba);
float y = float(texture2D(textureX,gl_TexCoord[0].st).rgba);
gl_FragColor = x + y;
}

You are trying to stuff vec4 into a single float, no go. Try

float x = texture2D(textureY, gl_TexCoord[0].st).r;

and later on you are doing the opposite, sticking float to a vec4, instead try

gl_FragColor = vec4(x, x, x, 1.0);

/N

What he’s trying to do is store a single float (4-bytes/texel) in a RGBA8 texture (4-bytes/texel), and come up with the fragment shader magic to get that float back from the texture2D’s return value.

Vittoria, problem with this is it won’t linearly interpolate and MIPmap correctly. Even if you write up the appropriate decode magic in the frag shader, it won’t work unless you’re using GL_NEAREST min and mag filters. Are you?

A perhaps better solution is to use half-float or full-float textures. If you need linear interp or MIPmapping, make sure the GPUs you’ll be running on support it. If you have questions, just ask.

What he’s trying to do is store a single float (4-bytes/texel) in a RGBA8 texture (4-bytes/texel), and come up with the fragment shader magic to get that float back from the texture2D’s return value.

Oh, that’s right. Hope this will help (search for ‘pack’)…

/N

Even if you write up the appropriate decode magic in the frag shader, it won’t work unless you’re using GL_NEAREST min and mag filters. Are you?

Yes, i’m…and i also setted the clamp option for edges.
Pls,can you explain better the use of half-float and full-float texture?How can i see if it’s supported?

It might be more expedient if you explain what you need and what card(s) you’re targetting?

You want to store a floating point value in a texture and access it in your fragment shader, yes? Do you need linear interpolation? Do you need MIPmapping? Do you need full-float precision, or will half-float do? Which GPU(s)?

Half-float (fp16) textures have been supported with MIPmapping and linear interpolation for a while now. Full-float (fp32) textures with MIPmapping and interpolation only in the latest gen cards (IIRC).

Some OpenGL extensions you can check your drivers for (make sure you’ve got the latest drivers installed):

  • ARB_half_float_pixel
  • ARB_texture_float

or if you must:

  • ATI_texture_float
  • NV_half_float
  • NV_float_buffer

My target would be the more platform-indipendent as possible, and what i need is a full precision floating point. It may come from fp32 texture or from whatever other trick can be made (using 4 8-bit wise rgba channels as a single float?).
I don’t know if i need interpolation or mipmapping, because i’m using the fragment shader for GP programming…
Now i’m testing on a mobility radeon X1600 on my laptop…
thanks for your reply

You need to establish if you need interpolation or MIPmapping. If this is just GPGPU stuff, you probably don’t need MIPmapping. However, do you expect to be able to sample between texel centers in this texture and have the GPU smartly compute the weighted average, or are you happy doing this yourself in a shader if you need to.

If you don’t need MIPmapping or interpolation, and representing values in the 0…1 range is sufficient, use an RGBA8 texture and the unpack suggestion in the link nico posted. That’ll be supported pretty much anywhere. If you need the exponent, you can use an adaptation of this where you siphon off 8 bits for the exponent and 24 for mantissa.

If you do need MIPmapping or interpolation however (and don’t want to simulate in the shader), well, you’re probably going to need to pony up and use half-float textures, or insist on the latest cards and use full-float textures.

You said half-float was insufficient for you. Are you sure? How many mantissa bits and how many exponent bits of precision do you need? Half-float (aka fp16) is s10e5, and full-float (aka fp32) is s23e8.

From Delphi3D, it looks like your X1600 only supports ATI_texture_float, or at least did 2 years ago (Mar '06). Check with the latest drivers This exposes fp16 and fp32 texture formats, but does not guarantee you can get these with MIPmapping or interpolation. Not being an ATI guy I don’t know where to look for that. For reference, this extension is supported on NVidia GeForce 6+ cards in the latest drivers and has been for years. It uses the same values for the texture symbols as the newer ARB_texture_float.

I don’t need MIPmapping because i’m rendering textures on quads of the same size, with both linear MIN and MAG_FILTER on (every texture element on a single output pixel).
I need interpolation and representing values out of the [0,1] range, so the unpack suggestion is not useful.
What i need is the more representing precision as possible because i’m working with physical varyings, so approximation is the main concern…
Now i’ll check ati driver release, looking for new extention support.
Thank for your help

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.