2
votes

I've defined a struct in my fragment shader, as below:

struct light_source{
    vec4 Ld, location;
    int type;
    float radii, specular_exponent;
};  

And I'm using this uniform to access the struct members:
uniform light_source light_sources[5];

Now, from my C++ code, I'm getting the uniform locations like this:

Ld = glGetUniformLocation(Globals::ProgramId, "light_sources[0].Ld");
Ls = glGetUniformLocation(Globals::ProgramId, "light_sources[0].Ls");
location = glGetUniformLocation(Globals::ProgramId, "light_sources[0].location");
type = glGetUniformLocation(Globals::ProgramId, "light_sources[0].type");
radii = glGetUniformLocation(Globals::ProgramId, "light_sources[0].radii");
specular_exponent = glGetUniformLocation(Globals::ProgramId, "light_sources[0].specular_exponent");

When I print the above values, I find that the values are -1,1,2,-1,3,4 . So, I couldn't get locations for Ld and type . Similarly, if I add a new variable to the shader now , some times I get the location, and sometimes I get -1 .

I don't get the problem here. All the variable definitions are correct and there are no typos. Can this behavior be remedied?

1
are you using it? if the values aren't being used then they may be optimized outratchet freak
@ratchetfreak Thanks! Earlier, I added some dummy usages too for that. Like just assigning the light_sources[0].Ld to some new variable. But that new variable was also not being used anywhere (since it was just a dummy assignment). Now I added a proper usage for the variables, and I'm getting the locations. Thanks so much!sanjeev mk

1 Answers

4
votes

When a uniform/attribute is not being used or has no effect on the output then the compiler/linker is free to remove it entirely which results in the getUniformLocation and glGetAttributeLocation returning invalid values.