Shader - static variable?

Hi guys,

Can I have a static variable in a shader ? (ie shared across all instances of the shader/material)

My problem is I have a several instances of my modified Lighting shader that is used by several geometries, within that shader I need t frequently update a variable, that needs to be the same across all instances. At the moment I’m setting this variable on each instance, each time it changes, which clearly feels wrong. Is there a way I setup a static variable that I can set on one material instance, that will be used by all ?


Pretty sure not. Pretty sure it wouldn’t matter even so.

Your code might be simplified, though… what kind of value are you setting?

For example, I keep one ambient color instance that I give to all of my shaders so that I only have to update it in one place. They automatically see the new values because they share the instance.

Several pairs of floats and vectors, at this stage, but it may expand.

when you say you “keep one ambient color instance”, I’m not sure what you mean, would you mind elaborating a bit, or even a small example ?

do you mean you doing it like this (sudo code) …


public ColorRGBA color_ref = ColorRGBA.Blue;



color_ref = ColorRGBA.Red;


No, I mean:


public ColorRGBA color_ref = new ColorRGBA(somevalues);

mat.setColor( "m_Ambient", color_ref );

some_other_mat.setColor( "m_Ambient", color_ref );

color_ref.set( some new color values );


You can do the same thing with Vector3f/Vector4f, etc… which may help in your case.

spot on, thanks man!

You didn’t answer my original question, but far more importantly solved my problem… I need to start asking the correct questions :slight_smile:

You know … thats kinda evil. What if there was an optimization that only changed the variable in opengl if you called one of the methods to force it to refresh? Then it wouldn’t work :o

And Mythruna would break in a hundred different ways. :slight_smile:

Optimization is overrated, just buy a faster card :wink:

It would be a hard optimization to implement I think anyway… at least in a way that affects this… since every different shader has to send its new variables and it would be a rare case to only have one shader in an app.