iDevGames Forums
Sharing uniforms across shader programs - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Sharing uniforms across shader programs (/thread-8066.html)



Sharing uniforms across shader programs - iamflimflam1 - Sep 3, 2010 08:39 AM

Hi All,

I've been porting my openGL ES 1.1 game over to 2.0. I've got a number of vertex shader programs now that all need to take a uniform for the model view projection matrix.

Can I give this uniform the same name in each shader program?

If I do give the uniform the same name in the vertex shader programs, when I call glGetUniformLocation will I get the same uniform location for each program or will I get a different location for each one?

Assuming I get the same location, when I set the value for the mvp uniform do I need to set it for each program I use or can I set it at the start of my rendering and will all the programs get the same value?

Sorry if these are all obvious questions - but I've looked through the gold book and it didn't really seem to say.


RE: Sharing uniforms across shader programs - arekkusu - Sep 3, 2010 09:22 AM

(Sep 3, 2010 08:39 AM)iamflimflam1 Wrote:  Can I give this uniform the same name in each shader program
Yes.

Quote:If I do give the uniform the same name in the vertex shader programs, when I call glGetUniformLocation will I get the same uniform location for each program or will I get a different location for each one?
Undefined. The compiler can assign any location it wants. Don't depend on it being the same.

Quote:Assuming I get the same location, when I set the value for the mvp uniform do I need to set it for each program I use or can I set it at the start of my rendering and will all the programs get the same value?
You need to set it for each program.


RE: Sharing uniforms across shader programs - iamflimflam1 - Sep 3, 2010 10:20 AM

Thanks arekkusu,, so uniforms are pretty much tied to the program.

I seem to remember reading somewhere that if you set a uniform on a program, change programs, then switch back to the original program the uniform's value is still valid - is that definitely the case?

Thanks
Chris.


RE: Sharing uniforms across shader programs - arekkusu - Sep 3, 2010 10:39 AM

Yes. Uniforms are program state.

Uniforms are initialized to zero when you (re)link.


RE: Sharing uniforms across shader programs - Frogblast - Sep 3, 2010 12:25 PM

(Sep 3, 2010 10:39 AM)arekkusu Wrote:  Yes. Uniforms are program state.

Uniforms are initialized to zero when you (re)link.

Yes, but they retain their values at other times (such as in the UseProgram(A); UseProgram(B); UseProgram(A)) example.