- OpenGL 4 Shading Language Cookbook(Second Edition)
- David Wolff
- 541字
- 2021-07-21 17:50:58
Getting a list of active uniform variables
While it is a simple process to query for the location of an individual uniform variable, there may be instances where it can be useful to generate a list of all active uniform variables. For example, one might choose to create a set of variables to store the location of each uniform and assign their values after the program is linked. This would avoid the need to query for uniform locations when setting the value of the uniform variables, creating slightly more efficient code.
The process for listing uniform variables is very similar to the process for listing attributes (see the Getting a list of active vertex input attributes and locations recipe), so this recipe will refer the reader back to the previous recipe for detailed explanation.
Getting ready
Start with a basic OpenGL program that compiles and links a shader program. In the following, we'll assume that the handle to the program is in a variable named programHandle
.
How to do it…
After linking and enabling the shader program, use the following code to display the list of active uniforms:
- Start by querying for the number of active uniform variables:
GLint numUniforms = 0; glGetProgramInterfaceiv( handle, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
- Loop through each uniform index and query for the length of the name, the type, the location and the block index:
GLenum properties[] = {GL_NAME_LENGTH, GL_TYPE, GL_LOCATION, GL_BLOCK_INDEX}; printf("Active uniforms:\n"); for( int i = 0; i < numUniforms; ++i ) { GLint results[4]; glGetProgramResourceiv(handle, GL_UNIFORM, i, 4, properties, 4, NULL, results); if( results[3] != -1 ) continue; // Skip uniforms in blocks GLint nameBufSize = results[0] + 1; char * name = new char[nameBufSize]; glGetProgramResourceName(handle, GL_UNIFORM, i, nameBufSize, NULL, name); printf("%-5d %s (%s)\n", results[2], name, getTypeString(results[1])); delete [] name; }
How it works...
The process is very similar to the process shown in the recipe Getting a list of active vertex input attributes and locations. I will focus on the main differences.
First and most obvious is that we use GL_UNIFORM
instead of GL_PROGRAM_INPUT
as the interface that we are querying in glGetProgramResourceiv
and glGetProgramInterfaceiv
. Second, we query for the block index (using GL_BLOCK_INDEX
in the properties
array). The reason for this is that some uniform variables are contained within a uniform block (see the recipe Using uniform blocks and uniform buffer objects). For this example, we only want information about uniforms that are not within blocks. The block index will be -1
if the uniform variable is not within a block, so we skip any uniform variables that do not have a block index of -1
.
Again, we use the getTypeString
function to convert the type value into a human-readable string (see example code).
When this is run on the shader program from the previous recipe, we see the following output:
Active uniforms: 0 RotationMatrix (mat4)
There's more...
As with vertex attributes, a uniform variable is not considered active unless it is determined by the GLSL linker that it will be used within the shader.
The previous code is only valid for OpenGL 4.3 and later. Alternatively, you can achieve similar results using the functions glGetProgramiv
, glGetActiveUniform
, glGetUniformLocation
, and glGetActiveUniformName
.
See also
- The Sending data to a shader using uniform variables recipe
- 精通MATLAB圖像處理
- Getting Started with MariaDB
- HBase Design Patterns
- 大數據時代
- 高維聚類知識發現關鍵技術研究及應用
- Mastering ServiceNow Scripting
- Excel 2007常見技法與行業應用實例精講
- Word 2007,Excel 2007辦公應用融會貫通
- 精通數據科學:從線性回歸到深度學習
- Salesforce Advanced Administrator Certification Guide
- Linux Shell編程從初學到精通
- Creating ELearning Games with Unity
- 深度學習原理與 TensorFlow實踐
- Python語言從入門到精通
- Cisco UCS Cookbook