Sign in to follow this  
EvilNando

Management of multiple shaders

Recommended Posts

EvilNando    96
I would like to know some tips on how to manage multiple shader effects for my game

currently I have a basic shader going which only transforms vertices and applies texture data...

so what if I want to have some nice shader effects also? I have not tried to implement any of this yet but I can already see that changing shader programs will not allow me to continue rendering my quad system unless I just bring over the new shader all of what I have

so yeah I just want to hear tips on how to design this.

Share this post


Link to post
Share on other sites
lauris71    841
I implemented 3-level object structure:


Shader - container of single OpenGL shader object
Program - collection of Shaders - container for OpenGL program object
Material - specific rendering implementation - parameters + Program

Each mesh fragment refers to one material. In rendering phase, this material virtual ::bind() method will be called:
- it detects, whether that material is already bound to render context, if yes do nothing
- if it is not bound, a ::bind() method of Program will be called
- after that, uniforms will be set

In ::bind() method of program:
- If shaders are not yet compiled, ask Shaders to compile their code (creating OpenGL shader objects)
- If program is not yet linked, a new OpenGL program is created and OpenGL shader objects linked
- OpenGL program object is bound (GLUseProgram)

So basically whenever a object with new material is used, the OpenGL state is updated by relevant container objects.
You still should sort your objects by Material/Program while rendering because switching programs is expensive - but this can be optimized later.

Share this post


Link to post
Share on other sites
bluntman    255
Its difficult to determine what you mean from your question. Do you mean you are worried about duplicating functionality in each of your shaders, or about how to manage assigning different shaders to different meshes?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this