r/GraphicsProgramming • • Oct 23 '17

How about using shaders as singletons ?

I want to have a getActiveShader() method so it will return the active shader(custom class) and use it with my playerComponent or anything that needs to be rendered. I was thinking to use singletons to do that so that the shader can be accessed on the fly from everywhere. I am a bit confused though and i am not really sure if i am on the right track here. Any thoughts on the subject? Using Opengl 3.3 .

0 Upvotes

10 comments sorted by

4

u/Rusky Oct 23 '17

OpenGL already has the concept of an "active shader"- glUseProgram persists across draw calls. What would you be using your custom shader class for?

Further, it's generally not a great idea to run a lot of custom rendering code per entity anyway. You want everything in the form of a "drawable" type, as pure data that you can sort and filter. This lets you more easily minimize state changes, run multiple rendering passes, etc.

1

u/nutellis Oct 23 '17

First of all, the shader class is initializing a glsl program using vertex and fragment files. Then i can set it as active etc etc. Now lets say i have a mesh that i want to render using shader1 and another mesh that needs shader2. When i call the draw method from each mesh i want to change the active shader and use it without having to pass it as an argument.I want my shader objects to be known and all i have to do is something like:

tempshader = getInstanceof("shader1"); 
tempshader.use(); // call to glUseProgram
//do translations etc and draw  

3

u/Rusky Oct 23 '17

It sounds like you don't actually want a getActiveShader function, but just some way to access your shader library without storing references to the shaders everywhere?

The ideal solution would, again, be just to stop writing code like that. You shouldn't be calling a bunch of "drawMe" functions that "getInstanceOf("shader1").use(); drawMesh();" - you should have all the code that deals with shaders in one place where it just iterates through a data structure describing your scene. This way not only lets you avoid both singletons and passing shaders around, it also makes it much easier to implement and optimize various rendering techniques.

If you don't want to do that (y tho), I might have some kind of rendering context object that owns all the shaders and pass that around. You'll probably wind up with a lot of other renderer data beyond just the shader library, so this will help keep things under control.

But again, go the data driven route. Way easier all around.

1

u/nutellis Oct 23 '17

I already have a scene structure and everything is being drawn from there. Meshes, lights etc. Right now i use a single shader for meshes and a single shader for lights. I can easily change between them but i am still not sure how to manage multiple shaders for the same "type" of object. I am convinced that my approach of singletons was wrong, but the problem still persists. Maybe organize my objects per shader as /u/nicolasgz suggested?

3

u/Rusky Oct 23 '17

Yep- that's precisely the sort of thing I'm talking about. The meshes and lights in your scene shouldn't be doing any shader setting- they should just have a field that tells the renderer what shader (or rather material) they use. It could be a pointer, but it would probably be better to an index or perhaps even a fancier sort of handle.

Then the renderer can traverse your scene graph and build up a list of things to draw, sort it by which shaders each item uses, and blast through it setting each shader only once.

1

u/nutellis Oct 23 '17

It now makes sense. Thank you for your time.

5

u/[deleted] Oct 23 '17

I would recommend to re-read what Rusky said. You probably want to sort your objects based on what shader they need, to avoid redundant state changes. That means it's not the mesh's job to set the shader. Instead, there's a layer of code above that traverses the scene in an efficient order.

0

u/nutellis Oct 23 '17

I think i found it. I will create a "static" class that will "manage" the shaders. So everytime i need to change the shader i'll just do:

   Manager::activate("shadername");
   //draw and stuff

1

u/[deleted] Oct 25 '17

FWIW you can have more than one OpenGL context in a single program, which may not be able to share shaders, so it may become logically unsound to treat shaders (or any OpenGL GPU commands) as a global operation.