How do I get/restore the current shader

I am writing a utility module (for drawing text) and to be polite to the host program I want to save and restore the current state.

I can get the current texture, and some enabled flags, but how can I get the currently active shaders? I have searched the red and orange books, searched the forum and googled. I can get a lot of information about a shader, but I can’t see if it is active and I can’t get the active one. I hope I just missed something obvious.


GLint id;
glGetIntegerv(GL_CURRENT_PROGRAM,&id);

Make sure that you are not in a section that builds a display list in compile mode only because this state is not part of it. To test that you are not in a section that builds a display list in compile mode, write a function or method that looks like this:


bool inDisplayList()
{
  bool result;
  GLint v;
  glGetIntegerv(GL_LIST_INDEX,&v);
  if(v!=0) { // we are building a display list
    glGetIntegerv(GL_LIST_MODE,&v);
    result=v==GL_COMPILE;
  } else {
    result=false;
  }
  return result;
}

I would not recommend using any of the Get* commands in a production environment as often gets require synchronization, especially in case of a multithreaded GL implementation.

Cannot your rather track the current program yourself?

Thanks! I figured it must be somewhere, but it is not in the reference card and rarely mentioned.

Make sure that you are not in a section that builds a display list in compile mode only because this state is not part of it

Since display lists are deprecated (and removed in 3.2), I hardly consider them to be a problem any more.

How can I track what current program the host program programmer uses? I am not writing that program. The only way I can do that is to make some glue for glUseProgram (interceptor or renamed call) which catches the argument, but that gives even more possible problems as far as I can tell. Anyway, this is for student projects, and they are not likely to make many multithreaded programs.

Oops, I didn’t notice that you have no access to modify the host program.

Yes, agree that creating a glue could be troublesome on its own.

Actually I didn’t mean that the host application is multithreaded, but the OpenGL driver itself and using Get* functions in case of a multithreaded OpenGL driver implementation can hurt performance.

Anyway, if it’s about some student projects, problably the overhead of using Get* stuff won’t be a problem.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.