-1

I want to create program which converts RGB to YCbCr so I need set transform matrix and vector as uniform. When i call glUniform more than once glGetAttribLocation returns -1. Here is my code.

Fragment Shader

#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++ code

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);
};
  • 4
    Are you sure that `shaderProgram = *program;` works correctly? This seems to be a copy assignment invocation on object that should probably be non-copyable. And you haven't called `glGetError` even once to check error code. – user7860670 Oct 18 '17 at 07:07
  • `GLenum a = glGetError(); fprintf(stdout, "GL_GET_ERROR %d", a);` prints code 1282 – Konrad Marzec Oct 18 '17 at 07:39

1 Answers1

1

You have u_ycbcr_vector uniform in your shader and you are not using it. This is some kind of optimization done by GLSL compiler and linker that will throw invalid locations for uniforms declared but not used. So remove that uniform from shader if you are not using it and then try again.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174