Management of multiple shaders

Started by
1 comment, last by bluntman 12 years, 10 months ago
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.
Advertisement
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.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
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?

This topic is closed to new replies.

Advertisement