I’m experimenting a bit with deferred shading and I would like to save a fragments’s world position in a texture map. One possibility is to save the x, y and z components as floats but that is pretty memory-expensive.
I have seen in some papers that it is also possible to save only the fragment’s z-buffer value. But how can you recover the original world position using fragment programs?
Packing 4 unsigned bytes in a float would be easy using bit operations but these aren’t available in GLSL at the moment. Is there no other way to achieve this?
So these don’t really work all the time. The packing of a float into four bytes assumes that the value is in the range [-1, 1]. Instead, it should probably try to encode/decode the exponent bits.
Packing the bytes into a float using multiplies are going to only use the 23 mantissa bits. And then, you have to worry about the implicit significant bit. To losslessly pack 4 bytes into a float is considerably more involved – you need to bring the sign bit and exponent bits into play.
In other words: it probably isn’t worth it unless you can use the inbuilt functions since they almost certainly do it better than you can with arithmetic. Doesn’t the NV glslang implementation expose the entire Cg standard library? I don’t think you need to do Cg_Shader unless the shader code itself is written in Cg.
Originally posted by Won: So these don’t really work all the time. The packing of a float into four bytes assumes that the value is in the range [-1, 1]. Instead, it should probably try to encode/decode the exponent bits.
Yes, it seems to work only in the range [0.0, 1.0) when I tested.
With 1.0, you will get 0.0 with those numbers.
Not enough precision maybe?