Should I bind programs once or each time during render to my pipeline? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Should I bind programs once or each time during render to my pipeline? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I program an addon/addin for another program. This also allows me render stuff for the other program which uses OpenGL. So the problem I have now is that I don’t know what program the caller to my render function has already bound if any. I tried to solve the problem with a pipeline. The code I use is this (remember I program in java but your answers don’t have to be, I can look up java-equivalents to C/C++-OpenGL functions myself).

int currProgram = GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM); // Get old program
if (currProgram != 0) { // If there was any
    GL41.glUseProgramStages(pipeline, GL41.GL_ALL_SHADER_BITS,
            currProgram); // Bind it to my pipeline
    GL41.glUseProgramStages(pipeline, GL41.GL_VERTEX_SHADER_BIT,
            geometryProgram); // Bind my vertexShader to the pipeline
    GL41.glBindProgramPipeline(pipeline); // Bind the pipeline
} else { // If there wasn't
    GL20.glUseProgram(geometryProgram); // Use my program
GL41.glBindProgramPipeline(0); // Unbind pipeline
GL20.glUseProgram(currProgram); // rebind old program

I got two question which bother me right now. First of all like I implicitly said I don’t know if the prebound program changes from one call to another. This leads me to the question:

How do I unbind stages in the pipeline? do I just call

GL41.glUseProgramStages(pipeline, GL41.GL_ALL_SHADER_BITS,


Secondly it seems more or less unefficient to bind my own vertex shader everytime I render. I introduced this because I thought there could be more than one vertexShaders bound at a time. If this is not the case I could use GL41.GL_ALL_SHADER_BITS^GL41.GL_VERTEX_SHADER_BIT to bind and unbind the other shaders and keep my shader bound to the pipeline. So question number two:

Can there be more than one vertexShaders bound to a pipeline at once? And how do I rework my code to be efficient?


No just bind the one you need for the specific object. Then bind another for the next object. To “unbind” just bind zero like you did.

We are here to answer your question about Should I bind programs once or each time during render to my pipeline? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji