miris
miris

Reputation: 221

glDrawElements is successfull however glDrawElementsInstance fails with GL_INVALID_OPERATION

I am in the process of migrating the 3D graphic engine of our desktop application from compatibility profile to core profile. Since there is much code to cover, I am changing renderers one by one on compatibility profile and when all were converted I am planing to switch profile to core.

In order to do so I am writing a small rendering tests to make sure that I understand how to use advance features of OpenGL 4.3 on top of the compatibility profile.

I wrote the following initialization code:

glGenVertexArrays (1, &_vertexArrayObjectID);
glBindVertexArray (_vertexArrayObjectID);

glGenBuffers (1, &_vertexBufferObjectID);
glGenBuffers (1, &_indexBufferObjectID);
glGenBuffers (1, &_instanceBufferID);

glBindBuffer (GL_ARRAY_BUFFER, _vertexBufferObjectID);
glBufferData (GL_ARRAY_BUFFER, sizeof (Verts), Verts, GL_STATIC_DRAW);

glVertexAttribPointer ((GLuint) 0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray (0);

glBindBuffer (GL_ARRAY_BUFFER, _instanceBufferID);
glBufferData (GL_ARRAY_BUFFER, sizeof (Centers), Centers, GL_STATIC_DRAW);

glVertexAttribPointer ((GLuint) 1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray (1);
glVertexAttribDivisor (1, 1);

glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (Faces), Faces, GL_STATIC_DRAW);

// creates the shader objects and program and links it in the current VAO scope
generateShaderProgram (); 

glBindVertexArray (0);

My rendering function is:

glBindVertexArray (_vertexArrayObjectID);

// Load the shader into the rendering pipeline 
glUseProgram (_program);

// Render the scene:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawElements (GL_LINE_LOOP, _indexCount, GL_UNSIGNED_INT, 0);

// Cleanup all the things we bound 
glUseProgram (0);
glBindVertexArray (0);

This code works correctly without any errors. However when I change the drawing line (without changing anything else) to:

glDrawElementsInstanced (GL_LINE_LOOP, _indexCount, GL_UNSIGNED_INT, 0, 4);

I get GL_INVALID_OPERATION (1282) error right after the above command.

My shader program is:

// vertex shader
#version 400                        
in layout(location=0) vec3 Position; 
in layout(location=1) vec3 Center; 
out vec3 theColor;                  
void main () {                      
    vec4 v = vec4(Position, 1.0);   
    vec4 offset = vec4(Center, 1.0);    
    mat4 transform = mat4(vec4(0.1,0.0,0.0,0.0),vec4(0.0,0.1,0.0,0.0),vec4(0.0,0.0,0.1,0.0),offset); "
    gl_Position = transform * v;   
    theColor = Position;            
}

//fragment shader
#version 400                            
in vec3 theColor;                       
out vec4 fragColor;                     
void main () {                          
    fragColor = vec4(theColor, 1.0);    
}

Am I using the instance rendering incorrectly?

Upvotes: 1

Views: 248

Answers (1)

miris
miris

Reputation: 221

Problem fixed!

This code runs in compatibility mode so draw instance is accessed through extension. It seems that in my scene tree (on top which I was running the test) there is some support for caching via display list. it seems that draw elements work correctly with display list however draw instanced does not.

quoting from: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_draw_instanced.txt

Additions to Chapter 5 of the OpenGL 2.1 Specification (Special Functions)

The error INVALID_OPERATION is generated if DrawArraysInstancedARB or DrawElementsInstancedARB is called during display list compilation.

The error was removed when I tracked down the creation of the display list and removed it.

Upvotes: 1

Related Questions