I'm learning OpenGL and noticed that I have the option to use either glGetAttribLocation()
or glBindAttribLocation()
when dealing with input attributes but for uniforms, I have to use glGetUniformLocation()
. Is there any technical reason for not including glBindUniformLocation()
in the API?
A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.
glLinkProgram links the program object specified by program . A shader object of type GL_VERTEX_SHADER attached to program is used to create an executable that will run on the programmable vertex processor.
layout(location = output index) out vec4 outColor; As with vertex shader inputs, this allows the user to forgo the use of glBindFragDataLocation. Similarly, the values in the shader override the values provided by this function.
gl_FragColor is the principal variable that your fragment shader is designed to change. If your code does not assign a value to it then it is left undefined for the rest of the pipeline. gl_FragData is an array of data that can be used by the rest of the pipeline.
For starters, aside from calling a function like glUniform* (...)
to set the value of a uniform, there is no other thing you can do with them. Uniforms did not exist before GLSL.
Attribute locations in OpenGL date back to the GL_ARB_vertex_program
assembly language (where attribs. had numbers instead of names). In fact, when this was first added to GL, those indices aliased to traditional fixed-function pointers (e.g. 0 == glVertexPointer (...)
, 2 == glNormalPointer (...)
, 3 == glColorPointer (...)
) so that vertex programs could easily be dropped into existing applications. GLSL re-used parts of the API that the assembly language extensions pioneered (this happens a lot).
It is worth mentioning that in GLSL, only 1 alias is guaranteed/allowed in a compliant implementation and that is 0 (many drivers, NV in particular, alias more than this). When mixing and matching fixed-function and programmable vertex attributes, it is important to avoid indices that alias to other used pointers and this requires the ability to set the locations manually.
More importantly though, the ability to explicitly set generic vertex attribute locations makes adapting GLSL programs to rigidly defined data more convenient. Instead of setting up a different set of vertex pointers just to match a vertex shader's expected interface, it is often easier to compile/link a GLSL program to match the layout of your vertex data.
This behavior is not so important for uniforms, but nevertheless, they do have similar functionality in the form of Uniform Buffers. You can bind uniform buffers to a shareable location (of your choosing) and re-use all of the values in their Uniform Block across all of your GLSL programs. In fact, this functionality was originally introduced in the form of an extension with the name: GL_EXT_bindable_uniform
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With