1

in my shaders I like to use a syntax like this:

layout (location = 0) in vec3 aPos;

So that I can just use the index 0 in glVertexAttribPointer and the such, saving the effort for the glGetAttribLocation call. I'd like to do the same with uniform values, but if I do

layout (location = 2) uniform float offset;

My vertex shader fails to compile. Is there any way to achieve the same behavior and not use glGetUniformLocation?

memememe
  • 663
  • 6
  • 21
  • Have you considered using UBOs instead of plain uniforms? – derhass Mar 13 '20 at 14:40
  • I'm not entirely familiar with them yet, what benefits would they have here? – memememe Mar 13 '20 at 15:12
  • 1
    you could use `std140` layout where you would not to have to query a thing. You also might be able to more efficiently group your uniforms by update frequency, and you can also use the same UBO for different shaders without having to keep update the uniforms separately for each shader – derhass Mar 13 '20 at 15:18

1 Answers1

7

OpenGL 4.3 or the ARB_explicit_uniform_location extension allows you to specify uniform locations within the shader using that syntax. So your shader #version needs to be 430 or you need to activate the extension in your shader to be able to use this syntax.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Oh I see! If I wanted to target older hardware instead, is it mandatory to use `glGetUniformLocation` for uniforms? And generally how old or bad do GPUs (even integrated ones) to not support 4.3? – memememe Mar 13 '20 at 14:34
  • 2
    @memememe: This is not really a hardware feature; it's purely an API change. So the availability of the extension depends entirely on how recent the driver is. Do you want to support hardware that itself hasn't [seen a driver update since 2013](http://opengl.gpuinfo.org/listreports.php?extension=GL_ARB_explicit_attrib_location&option=not)? – Nicol Bolas Mar 13 '20 at 14:37