PDA

View Full Version : Can a shader be attached to more than 1 program?



PrestoChung
03-21-2011, 06:18 PM
If you have say, 2 programs, can I attach the same vertex shader to both and have a different fragment shader?

Then whenever I need to change fragment shaders I just call glUseProgram( ); and that's it?

Do I need to call glAttachShader( ); after every switch?

Do I need to re-bind attribute/fragdata locations?

How about querying uniform locations?

When setting a VAO, the enabled attributes and pointers set with glEnableVertexAttribArray( ); glVertexAttribPointer( ); will stay the same no matter which Shader Program I load?
Example:
When I create my VAO I enable attributes 0 and 1, and I set the pointers. I then go on and bind other VAOs with other enabled attributes, say 0, 1, 2, or bind 0.
Now wanting to use my original VAO with the 0 and 1 attributes enabled, I call glBindVertexArray( );. Am I ready to call draw functions now? Or do I need to enable/disable the attributes I want?

Alfonse Reinheart
03-21-2011, 08:02 PM
If you have say, 2 programs, can I attach the same vertex shader to both and have a different fragment shader?

Yes.


Then whenever I need to change fragment shaders I just call glUseProgram( ); and that's it?

Yes, but this would be true regardless. Changing the program changes the program. Shader objects are only used to build programs.


Do I need to call glAttachShader( ); after every switch?

No. Programs are objects that contain their own state. The fact that two programs were linked from the same shader object is irrelevant.


Do I need to re-bind attribute/fragdata locations?

See above.


How about querying uniform locations?

Again, they are two separate program objects. Just as with any program objects, each program has its own uniform locations which you must query independently from the rest.


When setting a VAO, the enabled attributes and pointers set with glEnableVertexAttribArray( ); glVertexAttribPointer( ); will stay the same no matter which Shader Program I load?

VAOs are independent of program objects.