a question about shaders

in my application I have several instances of a class that draws to screen using two shaders.

for the first shader I have the code that loads and instantiates it in testApp, and then a pointer to the shader instance is passed to the class objects when they are created.

for the second shader I used a different approach: each object loads it independently when it is created (and so the shader is compiled several times, once for each class instance)

what is the best approach in this case? are there possible problems involved with one of them? does it makes sense to have more than one instance of the same shader or is it just a waste of GPU memory?

thanks in advance for your tips and suggestions

i recommend having a single instance of the shader rather than multiple instances. when you’re just dealing with one vs two instances it doesn’t make a big difference, but in general it’s best to get in a practice of coding in a way that scales well.

I agree with Kyle, from an architectural standpoint, the shader object is just a container for the GPU instructions, so it should be reused. Functionality is changed by the variables you send to to code via uniforms. The second way would be akin to making a new class for every object in your program, regardless of whether or not the objects’ functionality is the same, (two image classes that do exactly the same thing for example).

thanks for the advice, I’ve switched to the single-instance architecture.

that’s a great analogy tim – i think that’s exactly the right way to understand this.