1
\$\begingroup\$

I have the following shader, which whose fragment shader contains a sampler array of 16 elements.

Fragment

#version 330 core
in vec2 _uv;
flat in int _instanceID;

out vec4 color;

uniform sampler2D sampler[16];

void main() {
    color = texture(sampler[_instanceID], _uv);
}

Now when I try to set the second array element using the following method

void Shader::setUniform(const std::string& uniform, GLuint* values, size_t amount) const {
    bindProgram();

    GLuint loc = getUniformLocation(uniform + "[0]");
    for(int i = 0; i < amount; i++){
        glUniform1ui(loc+i, values[i]);
        std::cout << values[i] << " "; //For debug
    }
    std::cout << std::endl; //For debug
}

It just sets the first element of the shader. Even though I'm passing 2 values.

This is the output of the cout's:

5 4
5 4
5 4
5 4
... 

Looking up the shader variables with GDebugger, only the first element is set. enter image description here It also only draws the first (index 0) image.

\$\endgroup\$
  • 1
    \$\begingroup\$ Any reason why you're not using uniform sampler2DArray in your GLSL and GL_TEXTURE_2D_ARRAY in your C++? You have #version 330 core so these are supported, and they're probably what you actually want to use instead of an array of samplers. \$\endgroup\$ – Maximus Minimus Dec 11 '18 at 12:11
1
\$\begingroup\$

getUniformLocation isn't required to be continuous for array uniforms, so instead of just fetching the location of "sampler[0]", you also need to find the location for "sampler[1]" and so on.

\$\endgroup\$
  • \$\begingroup\$ That is the problem, if I try to get the uniform location of "sampler[1]", it does not find it. Only for "sampler[0]"! \$\endgroup\$ – Kiryu144 Dec 11 '18 at 17:14
  • \$\begingroup\$ That was not apparent from the code you posted, please update the code sample if that is not the code you are using. \$\endgroup\$ – Tyyppi_77 Dec 11 '18 at 18:34

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.