Unknownloner
Unknownloner

Reputation: 107

GLSL struct uniform locations return -1

I'm trying to create some sort of basic lighting system. I have this struct set up:

struct Light
{
  vec3 position;
  vec4 diffuse;
  vec4 ambient;
  bool enabled;
};

And then i define 4 disabled lights:

uniform Light light0 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light1 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light2 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light3 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);

In my program I'm trying to get the uniform locations so I can change the lights, so I use this code.

int[][] lightLocs = new int[MAX_LIGHTS][4]; //Position, Diffuse, Ambient, Enabled
for(int i = 0; i < MAX_LIGHTS; i++) { //MAX_LIGHTS is set to 4
    lightLocs[i][0] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".position");
    lightLocs[i][1] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".diffuse");
    lightLocs[i][2] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".ambient");
    lightLocs[i][3] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".enabled");
    //Print locations just for bug testing
    for(int j = 0; j < 4; j++) {
        System.out.print(lightLocs[i][j] + ", ");
    }
    System.out.println();
}

But I'm getting some strange results. All 4 locations for the first 3 lights (0-2) return -1. the position location on light3 also returns -1, but the last 3 values return 4, 5, and 6. The 4, 5, and 6 make sense because I have 4 uniforms defined before the lights in the vertex shader, but why am I getting -1 for the other locations? I have also tried this:

uniform Light light0;
uniform Light light1;
uniform Light light2;
uniform Light light3;

to see if it would work, but then the rest of the uniform locations for the lights return as -1.

Upvotes: 1

Views: 488

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474116

Uniforms that are not used in your shader code will often be optimized out. These uniforms are not considered "active" and therefore have a location of -1.

Upvotes: 2

Related Questions