PDA

View Full Version : How can I test GL_HALF_FLOAT in shader?



harsha
08-30-2013, 05:34 AM
Searched a lot but not getting now :(

I am passing GLushort half_float values to shader as :



const GLushort iValueSize[4] = {0x3C00, 0x7BFF, 0x0400, 0x8000};
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 1, GL_HALF_FLOAT, GL_FALSE, 0, iValueSize)


Vertex Shader as follows:



precision mediump float;

in mediump vec4 TestVector; // have bound loaction 0
out vec4 FragColor;

void main(void)
{
vec4 RefValue;
bool bCorrect = true;

RefValue.x = uintBitsToFloat(uint(0x3C00));
RefValue.y = uintBitsToFloat(uint(0x7BFF));
RefValue.z = uintBitsToFloat(uint(0x0400));
RefValue.w = uintBitsToFloat(uint(0x8000));

// if(TestVector.x==1.0) //passes
if(TestVector == RefValue) // failes
bCorrect = true;

if (bCorrect)
FragColor = vec4(0.0, 1.0, 0.0, 1.0); // Green (correct)
else
FragColor = vec4(1.0, 0.0, 0.0, 1.0); // Red (incorrect)
}


The rendered color is always RED.
Did lot of debugging but no getting why it is failing.

Is it valid to get float value from uint using uintBitsToFloat(uint(0x3C00)) ?

Thanks.

GClements
08-30-2013, 07:00 AM
I am passing GLushort half_float values to shader as :


glVertexAttribPointer(0, 1, GL_HALF_FLOAT, GL_FALSE, 0, iValueSize)


The second argument (size) has a value of 1, which says that each array element has a single component. So TestVector.x will have a value from the iValueSize array, while the y, z and w components will be 0.0, 0.0, and 1.0 respectively.

If you have a single vertex with a vec4 attribute (rather than 4 vertices with a float attribute), set size to 4.

Alfonse Reinheart
08-30-2013, 08:28 AM
Also:


precision mediump float;

in mediump vec4 TestVector; // have bound loaction 0

This does absolutely nothing. At least, in terms of the precision. In desktop GLSL, all of the precision qualifiers (https://www.opengl.org/wiki/Type_Qualifier_%28GLSL%29#Precision_qualifiers) are completely ignored. They're only there for OpenGL ES 2.0 compatibility; they mean nothing at all.

All values passed to an attribute via glVertexAttribPointer will be converted to 32-bit floats when they reach the vertex shader. This is just as true if you pass integer values via glVertexAttribPointer as if you pass half-floats (if you want to feed an `ivec4`, you use glVertexAttribIPointer). All of these are just smaller ways of packing floating-point data. Your shader does not have to know or care how the data was packed.