Search code examples
c#openglshadergame-engineopentk

Why does this call of glEnableVertexPointer and glVertexPointer cause InvalidValue?


I am creating a game engine that includes basic game needs. Using glslDevil, it turns out my bind VBO method throws an InvalidValue error. A call of glVertexPointer and a call of glEnableVertexPointer cause the issue. The vertex attribute index is causing the issue. The index is 4294967295 which is well over 15. Everything else works perfectly fine. I am using OpenTK. Here is the bind to attribute method.

public void BindToAttribute(ShaderProgram prog, string attribute)
    {
        int location = GL.GetAttribLocation(prog.ProgramID, attribute);
        GL.EnableVertexAttribArray(location);
        Bind();
        GL.VertexAttribPointer(location, Size, PointerType, true, TSize, 0);
    }
public void Bind()
    {
        GL.BindBuffer(Target, ID);
    }

Here are my shaders if required.

Vertex Shader:

uniform mat4 transform;
uniform mat4 projection;
uniform mat4 camera;
in vec3 vertex;
in vec3 normal;
in vec4 color;
in vec2 uv;
out vec3 rnormal;
out vec4 rcolor;
out vec2 ruv;

void main(void)
{
    rcolor = color;
    rnormal = normal;
    ruv = uv;
    gl_Position = camera * projection * transform * vec4(vertex, 1);
}

Fragment Shader:

in vec3 rnormal;
in vec4 rcolor;
in vec2 ruv;
uniform sampler2D texture;

void main(void)
{
    gl_FragColor = texture2D(texture, ruv) * rcolor;
}

Am I not obtaining the index correctly or is there another issue?


Solution

  • the index that you are getting seems to be the problem: that is the index that you get when opengl doesn't find a valid attribute/uniform with that name.

    There are a few things that might be going on:

    • you are passing a string that doesn't exist in the shader program (check case sensitive and whatnot)
    • the uniform exists and you are passing the correct string, but you are not using that attribute in the shader so the driver has removed all the occurrences of that attribute in the final code due to optimization (therefore it doesn't exists anymore)

    In general though, that number shows that OpenGL can't find the uniform or attribute you were looking for

    EDIT:

    One trick is the following: let's assume you have some pixel shader code that returns a value that is the sum of many values:

    out vec4 color;
    
    void main()
    {
        // this shader does many calculations when you are 
        // using many values, but let's assume you want to debug 
        // just the diffuse color... how do you do it?
        // if you change the output to return just the diffuse color, 
        // the optimizer might remove code and you might have problems
        // 
        // if you have this
        color = various_calculation_1 + various_calculation_2 + ....;
        // what you can do is the following
        color *= 0.0000001f;  // so basically it's still calculated 
                              // but it almost won't show up
        color += value_to_debug; // example, the diffuse color
    }