Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shaders, one per object

Started by Decrius Mar 25, 2009 at 3:28 AM 4 replies 1.2k views
Original Post
Decrius
Decrius
So, I've been reading up on shaders, and even ran a simple test program with shaders. I know how to write one (the basics), how to load the shaders and use them. However, the shaders apply to everything you draw. That is my problem. I want in my library people to be able to set a shader per object. I would probably make a shader class, holding the pointers to the shader in VRAM. However, how would I switch a shader on and off? It would only be used for one object at a time, and the next object could be an entirely different shader. So I should put a shader on, draw the object, and switch it off. How would I do this? What functions are used for this? And how bad is the overhead of switching (like with switching textures?)? Thanks!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Lord_Evil
Lord_Evil
You switch shaders off by calling glUseProgramObject(0).

The cost of switching shaders is very high. I'm not sure but it might be even higher that switching textures. So you should group your objects by shader and only call glUseProgramObject if the shader changes ('no shader' might be considered a special shader object).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Decrius
Decrius
Quote:
Original post by Lord_Evil
You switch shaders off by calling glUseProgramObject(0).

The cost of switching shaders is very high. I'm not sure but it might be even higher that switching textures. So you should group your objects by shader and only call glUseProgramObject if the shader changes ('no shader' might be considered a special shader object).


Thanks!

I guessed it would be expensive, but grouping by shader is near impossible in my situation...hmm. Thanks for the tip though :)

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
swiftcoder
swiftcoder
Quote:
Original post by Decrius
I guessed it would be expensive, but grouping by shader is near impossible in my situation...hmm. Thanks for the tip though :)
If you explain your situation, we may be able to suggest ways to improve it. This is a problem that has been solved many, many times.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Decrius
Decrius
Okay. I am writing a GUI library and want to be able to attach shaders to GUI objects.

Since the GUI elements are ordered back to front and grouped by window, it's rather difficult to add another groupings mechanism. I expect only a few GUI elements to be using Shaders though, but this is entirely up to the user (developer) using this library.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
swiftcoder
swiftcoder
For GUI objects, the cost of switching shaders should not be a problem - I assume there shouldn't be more than say 10-20 on screen at once.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.