PDA

View Full Version : Why is glGetActiveUniform() misbehaving?



nikoniko
06-29-2017, 10:36 PM
I just learned about glGetActiveUniform(), so naturally I introduced a uniform in my vertex shader

uniform int sup;
and in my code, I wrote


GLuint supUniformLocation = glGetUniformLocation(<program_ID>, "sup");
glUniform1i(supUniformLocation, 1); // just in case being uninitialized messes things up
GLint supSize;
GLchar supName[100];
glGetActiveUniform(programID, supUniformLocation, sizeof(supName) - 1, NULL, &supSize, NULL, supName);
std::cout << supName << " " << supSize << std::endl;

All of this compiles fine, and spits out the following garbage:

supName: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠****╠╠╠╠╠╠╠╠, supSize: -858993460

I cannot figure out what is wrong. :/

Silence
06-30-2017, 01:07 AM
Any errors returned by OpenGL ?
Is your uniform used in your shader or just declared ?

GClements
06-30-2017, 05:22 AM
GLuint supUniformLocation = glGetUniformLocation(<program_ID>, "sup");


The return value of glGetUniformLocation() is GLint, not GLuint; the return value will be -1 if the name doesn't identify an active uniform. But the index parameter to glGetActiveUniform() is GLuint, and


GL_INVALID_VALUE is generated if index is greater than or equal to the number of active uniform variables in program.

So you need to check that the location isn't negative prior to casting.

The uniform variable will be eliminated by the compiler if its value isn't required to compute the program's outputs (this is why there's a concept of "active" uniforms).

nikoniko
06-30-2017, 03:31 PM
Silence and GClements: I used the uniform in the shader, and now it outputs the uniform name and size fine. Thanks!