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?
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
© 2022 - 2024 — McMap. All rights reserved.