glDrawElements causes GL_INVALID_OPERATION

Any call to glDrawElements or glDrawArrays in OpenGL 3.2 in OSX Lion will produce the GL_INVALID_OPERATION error.

Here are the docs for this command:
http://www.opengl.org/sdk/docs/man3/xhtml/glDrawElements.xml

There are two explanations given for this error:

GL_INVALID_OPERATION is generated if a geometry shader is active and mode is incompatible with the input primitive type of the geometry shader in the currently installed program object.
GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to an enabled array or the element array and the buffer object’s data store is currently mapped.

I’m not sure what either of those mean. This same code is running on Windows with ATI and NVidia cards, and nearly identical code is running on iOS, with no problems.

Any ideas what could be wrong?

There are two explanations given for this error:

Those are far from the only reasons why you might get GL_INVALID_OPERATION.

OSX’s OpenlGL 3.2 implementation is core only, while the AMD/Nvidia drivers on Windows and Linux have the compatibility extension. Perhaps the error relates to that difference, such as not having a VAO bound when drawing.

I create and bind a single vertex array object when the program starts. That seems to be enough to make ATI and NVidia drivers happy on Windows.

The error occurs directly after the call to glDrawElements. A check right before this command does not detect any error.

I’ve been using VBOs for maybe five years. Just wondering if there was anything commonly known about GL 3.2 I could be missing.

I reduced my code down to this:

        float vertex[9];
        GLuint buffer;
        GLuint target = 0;
        
        //Create vertex buffer and set data
        glGenBuffers(1,&buffer);
	glBindBuffer(GL_ARRAY_BUFFER,buffer);
	glBufferData(GL_ARRAY_BUFFER,9*4,vertex,GL_STATIC_DRAW);
	glVertexAttribPointer(target,3,GL_FLOAT,GL_FALSE,0,0);        
        
        //Enable vertex buffer
        glEnableVertexAttribArray(target);
        glVertexAttribPointer(target,3,GL_FLOAT,GL_FALSE,0,0);
        
        //Draw
        glCheckError();
        glDrawArrays(GL_TRIANGLES,0,3);
        glCheckError();//GL_INVALID_OPERATION detected here

It runs fine on Windows, causes a GL_INVALID_OPERATION error on OSX Lion.

FYI, this comes before the code above:

		//Create default vertex array
		GLuint vao;
		glGenVertexArrays(1,&vao);
		glBindVertexArray(vao);

I think there are definite problems with OpenGL under OSX Lion. I’ve had problems with glDrawElements causing EXC_BAD_ACCESS since upgrading to Lion and Xcode 4.2. I tracked one cause down to glGenBuffers returning id’s starting at 1 again after displaying an open file panel. So I had several different buffers with the same id. I think the panel may have also messed with the binding of my VBO’s, but I can’t be certain.

If you’re generating and binding your VAO at application startup, perhaps the binding is getting reset somehow before the rest of your drawing code executes?

JoshKlint’s problem turned out to be that he wasn’t compiling any shaders. Attempting to draw without a valid program object will throw INVALID_OPERATION.

If you see any other problems, you should file bugs at bugreport.apple.com. (It’s not clear what a File Open panel has to do with your GL context…)

Yeah, what happened was the Mac file paths weren’t correct, and in OpenGL 3 there is no error emitted when a shader is compiled with no objects attached, so it never even occurred to me that could be a problem. The spec doesn’t say there should be an error (although there is for OpenGLES) so there’s actually nothing wrong here. Just something to look out for if you are new to Mac.

You also need to have a valid shader enabled when calling glDrawArrays or glDrawElements, or an invalid operation error will occur. I don’t believe the spec specifies this should happen, but since shaders are required in GL3 you should do it anyways.

I think there are definite problems with OpenGL under OSX Lion. I’ve had problems with glDrawElements causing EXC_BAD_ACCESS since upgrading to Lion and Xcode 4.2. I tracked one cause down to glGenBuffers returning id’s starting at 1 again after displaying an open file panel. So I had several different buffers with the same id. I think the panel may have also messed with the binding of my VBO’s, but I can’t be certain.

Sounds like your context somehow got switched.

Regarding VAOs, VBOs are shared between shared contexts. VAOs are not (although I think they should be). I just create one default VAO each time I create a context and bind it, then use my vertex buffer code like I normally would with OpenGL 2.

I’m pretty sure there’s a problem with FBOs on Lion, and I submitted a report, but I should point out that so far none of the problems I have had on Mac were actually a driver bug. Personally I am thrilled Apple seems to be taking high-end graphics seriously. They have the potential to take over PC gaming, which MS and Intel have treated like an unwanted stepchild for the last few years.