3D programming is great, but when something goes wrong, it can be quite hard to debug!

I was trying to launch two different OpenGL programs one after another, the first for the normal rendering, the second for a post-processing effect (motion blur, etc.). It seemed the first program's output was cleared as soon as the second one was activated.

I eventually found a behavior that is well summed up at http://www.gamedev.net/topic/541571-glsl-changing-between-shaders/page__view__findpost__p__4495411.

You don't write:

glUseProgram( shader-A );
modelA->Draw();
glUseProgram( shader-B );
modelB->Draw();
glutSwapBuffers();

You write:

modelA->Draw();
glUseProgram( shader-B );
modelB->Draw();
glUseProgram( shader-A );
glutSwapBuffers();

and since the code is called multiple times (at each frame), this will cycle OK. (There seem to be an GL_INVALID_OPERATION error the very first time I call glUseProgram, too.)

Anybody has an idea on why we have this behavior?

This “fix” sounds like cargo-culting (ie., your fix works by accident); probably something in Draw() sets some state that you did not expect. You're certainly not supposed to draw the first time using no shader; that doesn't make sense.

/ Steinar /

Comment by Anonymous Sun Oct 2 01:36:51 2011

lol - I definitely agree with you! The very first draw actually never works, with or without glUseProgram. I'm going to check the Mesa source code for inspiration.

Comment by beuc Sun Oct 2 08:04:44 2011

I eventually tracked the issue: using (Free)GLUT, I use a "display" and "idle" callback, and the idle callback was calling glUniform on the wrong program.

In addition, glGetError did not reset to OK after a successful call to glUseProgram, carrying the previous error from glUniform, which made me believe the problem lied in glUseProgram, while it was earlier in the program flow.

Tip: MESA_DEBUG=1 helps :)

Comment by beuc Sun Oct 2 09:02:26 2011