Soapy
Soapy

Reputation: 567

Storing FP16 values in a RGBA8 texture

Is it possible to store FP16 values in a RGBA8 texture, stored across RG and BA, like in this image? How would it be achieved in OpenGL 3.3+ with render textures and FBOs?

enter image description here

Upvotes: 4

Views: 927

Answers (1)

user1118321
user1118321

Reputation: 26375

This is just off the top of my head, but couldn't you multiply by 255 and take both the integer part and the fractional part? Something like this:

vec2 normal = vec2(normalizedX, normalizedY);
normal = normal / 2.0 + 0.5; // <- This gets all values to be between 0 and 1
vec4 intNormal;
intNormal.r = (int)(normal.x * 255.0);
intNormal.g = (int)(fract(normal.x * 255.0) * 255.0);
intNormal.b = (int)(normal.y * 255.0);
intNormal.a = (int)(fract(normal.y * 255.0) * 255.0);

Then you could reconstruct them like this, I think:

vec4 intNormal = texture2D (gBuffer, coord); // Or wherever you get it from
vec2 normal;
normal.x = (float)intNormal.r / 255.0 + ((float)intNormal.g / 255.0) / 255.0;
normal.y = (float)intNormal.b / 255.0 + ((float)intNormal.a / 255.0) / 255.0;
normal = normal * 2.0 - 1.0; // <- this gets us back to -1.0 to 1.0 for the normal

Upvotes: 5

Related Questions