VSGLInfoLib – Very Simple OpenGL Information Lib
OpenGL has a rich set of functions to query its state. It is possible to get information on just about anything, from which shaders are attached to a program, to the values of uniforms, or the properties of the textures currently bound.
However, it is also true, that to get some of this information a lot of code is required. When debugging, we end up writing code to access this and that information over and over again.
This lib attempts to provide that information with a minimal effort to the developer. All the above questions are answered, plus a few more.
Information is provided for the following objects:
- Buffers
- Textures
- Programs and Shaders
- Uniforms
- Vertex Array Objects
In Action
VSGLInfoLib provides a set of functions to display information that can be useful for debugging purposes. Each function is usually composed of a sequence of OpenGL state queries for some object in particular, or for types of objects.
Since there is no need for instances, all methods are static. Some methods return values, but most of them just send a human-readable string to a stream. To specify the output the stream the following function is provided
static void setOutputStream(std::ostream *outStream)
Passing NULL as an argument will set the stream to cout
, which is also the default setting.
Example
std::ostream *f; f = new std::ofstream("info.txt"); VSGLInfoLib::setOutputStream(f);
One piece of information we always like to have is the OpenGL and GLSL versions we are working with. The following function provides this and some more information. An example of the output provided is:
General Information Vendor: ATI Technologies Inc. Renderer: AMD Radeon HD 6900M Series Version: 4.1.11251 Core Profile Forward-Compatible/Debug Context GLSL: 4.10 Num. Extensions: 201
Another useful piece of info is extension availability. To check if an extension is supported the following function can be used:
static bool isExtensionSupported(std::string extName)
The library provides detailed information about OpenGL objects. For instance, we can get information on a given program as follows:
VSGLInfoLib::getProgramInfo(1);
Or, if using VSShaderLib:
VSShaderLib shader; ... // init program and shaders ... VSGLInfoLib::getProgramInfo(shader.getProgramIndex());
An example of the output provided is as follows:
Program Information for name 1 Shaders { GL_VERTEX_SHADER: 2 GL_FRAGMENT_SHADER: 3 } Program Separable: 0 Program Binary Retrievable Hint: 0 Link Status: 1 Validate_Status: 1 Delete_Status: 0 Active_Attributes: 3 Active_Uniforms: 9 Active_Uniform_Blocks: 2 Transform Feedback Buffer Mode: GL_INTERLEAVED_ATTRIBS
The numbers reported for the shaders are the names of each shader object.
For the same program we can query its uniforms and attributes with
static void getUniformsInfo(unsigned int program) static void getAttributesInfo(unsigned int program)
Assuming the following uniforms in the shaders
// in the vertex shader layout (std140) uniform Matrices { mat4 projModelViewMatrix; mat3 normalMatrix; }; // in the fragment shader layout (std140) uniform Material { vec4 diffuse; vec4 ambient; vec4 specular; vec4 emissive; float shininess; int texCount; }; uniform sampler2D texUnit;
An example of output for uniform information is as follows:
Uniforms Info for program: 1 texUnit GL_SAMPLER_2D location: 8 size: 4 Material Size 72 Block binding point: 1 Buffer bound to binding point: 1 { ambient GL_FLOAT_VEC4 offset: 16 size: 16 diffuse GL_FLOAT_VEC4 offset: 0 size: 16 emissive GL_FLOAT_VEC4 offset: 48 size: 16 shininess GL_FLOAT offset: 64 size: 4 specular GL_FLOAT_VEC4 offset: 32 size: 16 texCount GL_INT offset: 68 size: 4 } Matrices Size 112 Block binding point: 2 Buffer bound to binding point: 2 { normalMatrix GL_FLOAT_MAT3 offset: 64 size: 48 mat stride: 16 projModelViewMatrix GL_FLOAT_MAT4 offset: 0 size: 64 mat stride: 16 }
It is also possible to query uniform values, both for uniforms in the default block, as well as for uniforms in named blocks:
static void getUniformInfo(unsigned int program, std::string uniName) static void getUniformInBlockInfo(unsigned int program, std::string blockName, std::string uniName)
For instance we can query the uniform “diffuse”, inside block “Material”, as follows:
VSGLInfoLib::getUniformInBlockInfo( shader.getProgramIndex(), "Material", "diffuse");
Below is an example of the information we may obtain:
Block binding point: 1 Buffer bound to binding point: 1 Index of Uniform: 1 0.600000 0.600000 0.600000 0.000000
diky je to ok