Values are being reinterpreted strangely in my shader

Since I can only post one image per post, I combined all the images into one large one and attached it at the bottom.

I have an input variable in my shader defined as such:

in uvec3 bone_ids;

And I have this little bit of code to test the first value in it:

float val = 0;
if (bone_ids[0] > 805306366)
{
    val = 1;
}

Color = vec3(1.0, 0.0, val);

Looking at the color of my object, val is definitely set to 1. Why is the first element some comically large number? I ran the thing through renderdoc and it showed no interesting values like that in my bone_ids vector.

If I just write the id to the position like:

gl_Position = vec4(bone_ids[0], 0.0f, 0.0f, 0.0f);

I see the crazy value come back…

This is that value in binary, which is nothing like any of my other numbers so I’m not sure if I’m just arbitrarily reinterpreting garbage data somehow…

I’m at a total loss, any help would be appreciated, thanks!

image

Are you using glVertexAttribPointer or glVertexAttribIPointer to specify the attribute array? Integer attributes must use the latter; the former will always cause the values to be converted to floats if they aren’t already floats. The part of the driver which sets up attribute arrays has no knowledge of the program which will be reading from those arrays; in particular, it doesn’t know whether the input variable will be floating-point or integer.

That was it! Thank you soooooo much! I feel quite silly now, I knew that function was a thing at some point but it totally slipped my mind. I wonder why it came up with that floating point value though. I went through the binary buffer data by hand trying to figure out how that specific float value could be accidentally reinterpreted from the buffer data but didn’t find anything.

Double conversion. Interpreting the bits of the float 1.0 as an integer gives 0x3f800000 = 1065353216. Interpreting the bits of the float 1065353216.0 as an integer gives 0x4e7e0000 = 1316880384.

So if the client-side array actually contained floats but you used glVertexAttribPointer with a type parameter of GL_INT or GL_UNSIGNED_INT, a value of 1.0 would be read as the integer 0x3f800000 = 1065353216 and converted to the float 1065353216.0 (bit pattern 0x4e7e0000), resulting in bone_ids[0] having the value 0x4e7e0000 = 1316880384.

1 Like