How to structure a multi-pass shader system

How would I structure an rendering system with OpenGL? ( Specifically for a game engine, really )

How I’ve been doing it lately:

void render(){
      for(Program shader : shaders); shader.use();
            for(WorldEntity e : worldEntites); e.render(shader);
}

But the problem here is that it doesn’t allow for per-entity shaders… Which I may really need for some specific game things.
Adding a shader list to the world entities is out of the equation completely because it would interfere with the parent world’s current shader.

So my question is, how do you guys handle this kinda stuff?

hard to say really. depends on the type of your engine.

for me glsl-subroutines made the biggest difference. whenever you use lots of if-else-chains, this will help big time. https://www.opengl.org/wiki/Shader_Subroutine

have you played with UBO/TBO/SSBO’s yet ? these can help with the “per entity parameter” issue.

https://www.opengl.org/wiki/Uniform_Buffer_Object
https://www.opengl.org/wiki/Buffer_Texture
https://www.opengl.org/wiki/Shader_Storage_Buffer_Object

program-pipeline-objects can help out when you feel like having too many shaders in general. separate shader stages and recombine. https://www.opengl.org/wiki/Program_Pipeline_Object#Program_pipelines

personally, looking at your codesnip i would just use one shader with multiple subroutines.

shader.begin();

shader.assignSubroutine("pass1");
for ( WorldEntity e : worldEntities ) e.render();

shader.assignSubroutine("pass2");
for ( WorldEntity e : worldEntities ) e.render();

shader.end();

Thanks! I’ll look into subroutines. +1

So far they seem like pretty much everything I was asking for, I can’t believe I’ve never heard of them before :o