Does glsl implicitly convert variables to the in glVertexAttribPointer specified format?

when I looked at my vertex shader I noticed something strange:
I have written layout(location = 0) in vec4 position; although I specified in glVertexAttribPointer that the first attribute consists of 3 floats, not 4. The strange thing: It still works just fine.

What I would expect to happen is that the program crashes or at least renders some garbage because it reads in 4 floats per vertex which means the x component of the second vertex should be read as if it was the w component of the first vertex and should at some point read memory that does not belong to it.

Now my question: Why does the program still work? Does glsl internally know that I specified the false type and just fills in the missing component?

Thanks for your answers :slight_smile:

All attributes are effectively vec4. If you specify fewer than 4 components, the missing components will be set to zero for the second and third components and to one for the fourth component. The shader variables will ignore the additional components.

Sending attribute data to the shader program isn’t affected by the shader program itself.

One consequence of this is that if you have signed/unsigned integer attributes in the shader but use glVertexAttribPointer (rather than glVertexAttribIPointer), the data will be sent as floating-point and the variables will get the value of the floating-point bit pattern (e.g. 1.0 will be read as 0x3f800000 = 1065353216).

Correction: this may be what happens on a specific piece of hardware, but as far as the OpenGL specification is concerned, an integer type mismatch results in unspecified values.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.