About Me
Programming. Realtime computer graphics. Demoscene. Game development. Stuff. LinksArchives
Misc |
2007-03-03A day well spent (encoding floats to RGBA)![]() So it was yesterday - almost whole day spent fighting rounding/precision errors when encoding floating point numbers into regular 8 bit RGBA textures. You know, the trivial stuff where you start with and everything is fine until sometimes, somewhere there's "something wrong". Must be rounding or quantizations errors; or maybe I should use 255 instead of 256; plus optionally add or subtract 0.5/256.0 (or would that be 0.5/255.0?). Or maybe the error is entirely somewhere else, and I'm just chasing ghosts here!inline float4 EncodeFloatRGBA( float v ) { ![]() ![]() Comments
Have you tried exactly the same code on CPU? My pure non-educated guess would be that such operations screw up precision anyhow...
... give a bit of a thought for a previous (offline) discussion. Corrected code:
// rgba_encoded_b = tex2d(s) // x = (a >= rgba_encoded_b) float4 t = sign(EncodeFloatRGBA(a) - rgba_encoded_b); x = (dot(float4(8,4,2,1), t) >= 0);
Does not quite work. I think this code depends on sign() returning zero in case arguments are equal; otherwise only the first component (dotted with 8) affects the result.
Now, one argument comes from a 8-bit texture, while the other is encoded on the fly. So basically they are never equal (at least I couldn't make them equal). What would be needed here is some sort of QuantizeFloatAsYouWouldDoWhenWritingIntoATexture() function :)
I suppose you can simulate quantinization by scaling and clamping. For example something along the lines:
// t = sign(EncodeFloatRGBA(a)-rgba_encoded_b); float INV_EPSILON = 127;// or 255? d = EncodeFloatRGBA(a)-rgba_encoded_b; t = (clamp(d*INV_EPSILON+0.5)-0.5)*2;
...but it still won't be exactly like the quantized version. For example, it looks like Radeon X1600 quantizes like this: x*255.0-0.55781; the last number is approximate. No idea why it's a minus, because I would think it should be a plus, but experiments tell otherwise.
So my thinking is that the chances of getting quantized-float-myself and float-coming-from-8bit-texture equal are pretty slim.
Hmmm... I wonder if you would get the same quantinization by doing 'dummy' texture reads from the 256x256 texture filled with f(u,v,0,1)... but even if that would work, it's of course 2 additional texture reads per pixel, which is basically a crap :(
Post a Comment
<< Home |