Switching GLSL Shaders...
category: general [glöplog]
Hello world.
I currently have to deal with multiple shaders during one frame drawing pass, using openGL 2.0. I don't find function documentations very clear, and I had problems when trying to switch shaders long ago around 2005/2006 when GLSL was new.
So the question is: What is the correct way to switch shaders ?:
Method 1:
- Having one "Program" with glCreateProgram(), one glUseProgram() call, then use glAttachShader() and glLinkProgram() to change the shader.
Method 2:
- Having as much "Program" as needed with a shader couple each, then use glUseProgram() for switching shader when needed. It seems more wise, but I remember of some problems back then in 2006 (on some configurations maybe).
I would be really glad to have your advice on it, and also maybe some note about speed issue for these functions...
Thanks and überbass!
I currently have to deal with multiple shaders during one frame drawing pass, using openGL 2.0. I don't find function documentations very clear, and I had problems when trying to switch shaders long ago around 2005/2006 when GLSL was new.
So the question is: What is the correct way to switch shaders ?:
Method 1:
- Having one "Program" with glCreateProgram(), one glUseProgram() call, then use glAttachShader() and glLinkProgram() to change the shader.
Method 2:
- Having as much "Program" as needed with a shader couple each, then use glUseProgram() for switching shader when needed. It seems more wise, but I remember of some problems back then in 2006 (on some configurations maybe).
I would be really glad to have your advice on it, and also maybe some note about speed issue for these functions...
Thanks and überbass!
Method 2! You don't want to be relinking your shader every frame.
I haven't had any issues with multiple shaders per frame with glUseProgram(). It's definitely the correct way.
Jup.
Yes, method 2. However unnecessary switching between shaders should be avoided due to performance, so sorting by material (shader) would be good idea whenever possible.
ok thx all.
are you kidding xernobyl? aren't you the guy that can't even look up the SDL code to see how it does opengl init under linux?
Quote:
http://www.opengl.org/sdk/docs/man/
Hehe,... the fact is I read the documentation before, but: in the glUseProgram() definition you can read:
Quote:
While a program object is in use, applications are free to
modify attached shader objects, compile attached shader objects,
attach additional shader objects, and detach or delete shader
objects. None of these operations will affect the executables
that are part of the current state. However, relinking the
program object that is currently in use will install the program
object as part of the current rendering state if the link
operation was successful[...]
One of the reason I open this thread for, is that the sentence "attach additional shader objects" is extremely suspect and can be unserstood like if you had more than one vertex shader or or more than one frag shader. at a time on a "program" object.
If I have
in one file, and
in another, if I compile them, attach, and link, does it work?
Code:
vec4 b(){ return vec4(1,1,1,1); }
in one file, and
Code:
main(){ gl_FragColor = b(); }
in another, if I compile them, attach, and link, does it work?
I'll just add that glUseProgram( 0 ); is used to come back to the fixed function pipeline. you can mix shaders and classic rendering in the same frame.