Sign in to follow this  
Butabah

Program not finding my uniforms.

Recommended Posts

Butabah    100
I've been modelling a framework for my own learning engine after superbible 5's code. One thing is however is that it will not find my uniforms, always turns up -1 as the result

I'd post all my code, but here is the relevant portions:

[code]

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(program);
GLint location = glGetUniformLocation(program, "offset");
if (location > -1)
{
glUniform2f(location, 1.0f, 1.0f);
}

oglo->render();


glUseProgram(0);

assert(glGetError() == GL_NO_ERROR);

glutSwapBuffers();
glutPostRedisplay();
}
[/code]

Thats the main render code, heres the shader codes

[code]
// shader.v

#version 330

layout(location = 0) in vec4 position;
uniform vec2 offset;

void main()
{
vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);
gl_Position = position + totalOffset;
}

// different file shader.f

#version 330

out vec4 outputColor;

void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}[/code]

Edit: Just in case, here is some more code

[code]
// My loading shaders function

GLuint createShaderFromSource(const GLchar *source, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);

GLint shaderStatus;
glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderStatus);
if (shaderStatus == GL_FALSE)
return -1;

assert(glGetError() == GL_NO_ERROR);

return shader;
}
// My creating program with multiple shader source

GLuint createProgramWithShaders(GLuint shaders, ...)
{
GLuint program = glCreateProgram();

va_list shaderArgs;
va_start(shaderArgs, shaders);
for (unsigned int i = 0; i < shaders; ++i)
{
GLuint currentShader = va_arg(shaderArgs, GLuint);
glAttachShader(program, currentShader);
}
va_end(shaderArgs);

glLinkProgram(program);

GLint progStatus;
glGetProgramiv (program, GL_LINK_STATUS, &progStatus);
if (progStatus == GL_FALSE)
return -1;

assert(glGetError() == GL_NO_ERROR);

return program;
}

// my stock shaders code

void initStockShaders()
{
GLuint vertShader = createShaderFromFile("shader.v", GL_VERTEX_SHADER);
GLuint fragShader = createShaderFromFile("shader.f", GL_FRAGMENT_SHADER);

program = createProgramWithShaders(vertShader, fragShader);

assert(glGetError() == GL_NO_ERROR);
}
[/code]

Share this post


Link to post
Share on other sites
TriKri    124
According to the reference page for glGetUniformLocation, it will return -1 if the variable name does not correspond to an active uniform variable in the shader program or if the name starts with the reserved prefix "gl_". Except from that, I do not know. It seems like it should find your variable offset. In some cases, the compiler can decide to optimize some variables away, since they are not used, hence you will get -1 when you try to find their location, but that doesn't seem to be the case here either.

A thing I noticed though is that you have written [font="Courier New"]if (location > -1)[/font]. Are you sure that location can't be negative even if the variable was found, i.e. location < -1 also? I haven't checked this myself though, but just in case I think that [font="Courier New"]if (location != -1)[/font] would probably be better to use.

I guess you have also made sure that there isn't anything wrong with your shader program before you try to find the variable (it's linked and compiled properly). Maybe you should check the [font="Courier New"]glGetError[/font] directly after calling [font="Courier New"]glGetUniformLocation[/font], before trying to do anything else with OpenGL, like rendering.

Share this post


Link to post
Share on other sites
Butabah    100
[quote name='TriKri' timestamp='1310208835' post='4833041']
According to the reference page for glGetUniformLocation, it will return -1 if the variable name does not correspond to an active uniform variable in the shader program or if the name starts with the reserved prefix "gl_". Except from that, I do not know. It seems like it should find your variable offset. In some cases, the compiler can decide to optimize some variables away, since they are not used, hence you will get -1 when you try to find their location, but that doesn't seem to be the case here either.

A thing I noticed though is that you have written [font="Courier New"]if (location > -1)[/font]. Are you sure that location can't be negative even if the variable was found, i.e. location < -1 also? I haven't checked this myself though, but just in case I think that [font="Courier New"]if (location != -1)[/font] would probably be better to use.

I guess you have also made sure that there isn't anything wrong with your shader program before you try to find the variable (it's linked and compiled properly). Maybe you should check the [font="Courier New"]glGetError[/font] directly after calling [font="Courier New"]glGetUniformLocation[/font], before trying to do anything else with OpenGL, like rendering.
[/quote]

I fixed it, after a while I got around to reading the reference for glGetUniformLocation

It appears GLSL doesn't *have* to give you the location *if* it isn't used in the actual program, a sort of efficiency work.

EDIT: It appears that I already did this in my original code, I was using it before. For some reason when I just fiddled around with the shader (moving from file to code), it fixed itself? uhhh......... x.x

Share this post


Link to post
Share on other sites
Butabah    100
So I looked more into it, and as it turns out, it was my compiling\linking function.

For some reason the variable parameters function wasn't pointing me to the right shaders, so I just made a "load shaders pair" function

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this