GLSL macOS VBO attribute location indices are not guaranteed to be in order?

Started by Lightbuffer, November 03, 2022, 07:58:05

Previous topic - Next topic

Lightbuffer

Hello!

Yesterday I found the hard way that if attributes are not guaranteed to be in order of their definition in a GLSL shader. I was trying to run my LWJGL on macOS and all stuff that required texture wasn't rendering, after a couple of hours of banging my head on the wall, I happened to find one model that was rendering at least something. Upon further investigation I noticed is that if I tweak model's UV it affects the vertices, and vice versa, but changing the colors still works as expected. I couldn't find anything on Google about it, but at one point I remembered about layout(location = ...). Adding explicit layout location indices fixed the textured shaders.

Apparently, for some of my shaders, macOS' (Catalina) OpenGL gives attribute indices in a random order.

So the question: is it a bad practice that I don't use glGetAttributeLocation? Would it be a better idea to rewrite the VAO code to find attribute locations from shaders, or is it fine to just specify for every vertex shader attribute a layout(location = ...)?

Thank you for attention!

KaiHH

QuoteSo the question: is it a bad practice that I don't use glGetAttributeLocation?

It's bad practice that you don't assign explicit location layout qualifiers for your vertex inputs/attributes in your GLSL shaders.
For example: If you at some point want to migrate to Vulkan GLSL, then it becomes even _mandatory_ to explicitly specify the location layout qualifier for every vertex attribute.

See section "4.4.1. Input Layout Qualifiers" of the GLSL 4.60 specification about that.

QuoteWould it be a better idea to rewrite the VAO code to find attribute locations from shaders, or is it fine to just specify for every vertex shader attribute a layout(location = ...)?

Usually, shaders and vertex specification (VAO) go hand in hand. You can't author one without knowing about the other, at least if you don't employ some name-based conventions with GLSL reflection.

So, the best thing would be to explicitly assign location layout qualifiers within your GLSL shaders.
If you don't want to do that, you can, like you said, use https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetAttribLocation.xhtml to find out the locations by name.

Last but not least there is also https://registry.khronos.org/OpenGL-Refpages/gl4/html/glBindAttribLocation.xhtml which you can use to explicitly set the attribute locations by name via the OpenGL API / your host program before linking the shader program.