Sharing uniforms across shader programs

Member
Posts: 40
Joined: 2009.05
Post: #1
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.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #2
(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.
Quote this message in a reply
Member
Posts: 40
Joined: 2009.05
Post: #3
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.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #4
Yes. Uniforms are program state.

Uniforms are initialized to zero when you (re)link.
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #5
(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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  (just sharing) C++ to Objective-C callbacks godexsoft 0 3,375 Apr 11, 2012 06:37 PM
Last Post: godexsoft
  sharing files between apps captainfreedom 3 2,738 Apr 19, 2010 01:11 PM
Last Post: captainfreedom
  Long shader compile times on iPhone 3G S conal 9 6,528 Nov 20, 2009 03:54 AM
Last Post: OneSadCookie
  Hi everyOne ,3D and other knowledge sharing priyankranka 2 2,641 Feb 1, 2009 10:41 PM
Last Post: priyankranka