pouët.net

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!
added on the 2009-02-16 11:50:09 by krabob krabob
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.
added on the 2009-02-16 11:58:27 by Preacher Preacher
Jup.
added on the 2009-02-16 11:59:50 by raer raer
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.
added on the 2009-02-16 12:02:01 by shadez shadez
ok thx all.
added on the 2009-02-16 13:34:14 by krabob krabob
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.
added on the 2009-02-16 14:58:27 by krabob krabob
If I have
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?
added on the 2009-03-15 20:55:11 by xernobyl xernobyl
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.
added on the 2009-03-16 10:20:07 by nystep nystep

login