设置了多个统一后,glGetAttribLocation返回-1

时间:2017-10-18 07:02:03

标签: c++ c++11 opengl glsl opengl-4

我想创建将RGB转换为YCbCr的程序,因此我需要将变换矩阵和矢量设置为均匀。当我多次致电glUniform时,glGetAttribLocation会返回-1。这是我的代码。

片段着色器

#version 410
precision highp float;

uniform sampler2D u_texture;
uniform mat3 u_ycbcr_matrix;
uniform mat3 u_ycbcr_vector;

out vec4 color;

in vec2 v_coord;

void main() {
    color = texture(u_texture, v_coord) + vec4(u_ycbcr_matrix[0], 1);
}

C ++代码

void ScreenQuad::createProgram(ShaderProgram * program, string vsPath, string fsPath){
shaderProgram = *program;
shaderProgram.loadVertexShader(vsPath);
shaderProgram.loadFragmentShader(fsPath);
shaderProgram.link();

GLint attr = glGetAttribLocation(program -> getShaderProgram(), "position");
if (attr < 0) {
    fprintf(stdout, "Nieprawidlowy parametr: position");
}

glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(attr);

shaderProgram.use();
program -> setUniforms();
glUniform1i(glGetUniformLocation(shaderProgram.getShaderProgram(), "u_texture"), 0);

int total = -1;
glGetProgramiv( shaderProgram.getShaderProgram(), GL_ACTIVE_UNIFORMS, &total );
for(int i=0; i<total; ++i)  {
    int name_len=-1, num=-1;
    GLenum type = GL_ZERO;
    char name[100];
    glGetActiveUniform( shaderProgram.getShaderProgram(), GLuint(i), sizeof(name)-1,
                        &name_len, &num, &type, name );
    name[name_len] = 0;
    GLuint location = glGetUniformLocation(shaderProgram.getShaderProgram(), name);
    fprintf(stdout, "\n --- %s : %d---", name, location);
}

shaderPrograms.push_back(shaderProgram);}


ShaderProgram RGB2YCbCr::setUniforms() {
    GLuint program = getShaderProgram();
    fprintf(stdout, "\n+++ %d +++ \n", glGetUniformLocation(program, "u_ycbcr_matrix"));
    glUniformMatrix3fv(glGetUniformLocation(program, "u_ycbcr_matrix"), 1, GL_FALSE, ycbcr_matrix);

    fprintf(stdout, "\n+++ %d +++ \n", glGetUniformLocation(program, "u_ycbcr_vector"));
    glUniform3fv(glGetUniformLocation(program, "u_ycbcr_vector"), 1, ycbcr_vector);
};

1 个答案:

答案 0 :(得分:1)

您的着色器中有u_ycbcr_vector个制服并且您没有使用它。这是由GLSL编译器和链接器完成的某种优化,它将为声明但未使用的制服抛出无效位置。因此,如果您不使用它,请从着色器中删除该制服,然后再试一次。