Sign in to follow this  

Managing shaders

Recommended Posts

I started recently making use of shaders in my project (DirectX, HLSL) and I'm having trouble figuring out how best (when and where) to set the various constants that the shaders depend on. I've only got like, two of them that I've made so far but I have stuff like effect->SetSomeConstant("SomeConstant", SOME_BIT_OF_INFORMATION_THAT_IS_NEVER_AVAILABLE_WHEN_I_WANT_IT) scattered absolutely everwhere. I'm vaguely envisioning some kind of callback system where I say MagicalMysteryBoxOfShaders->SetShaderConstant("SomeConstant", SOME_BIT_OF_INFORMATION_THAT_HOPEFULLY_IS_HANDY_NOW) and it loops through a list of pointers and updates them all (probably each frame). Is this a good idea? How do you do it? If I go ahead with this is there anything else that I could use this kind of system for (so I can design it somewhat generically) or am I going to have all this functionality (callbacks?) sitting around just to update shaders?

Share this post

Link to post
Share on other sites
I recommend splitting up your shader constants into engine constants and user constants. For engine constants write down a list of constants that every shader should have, for example projection transform, global ambient, light position, light direction, etc. These engine constants will be calculated and set by the engine.

The other set, user constants should be completely shader dependent. Each shader has a metadata file that describes these constants. Artists who design models can set engine constants and user constants for the meshes and these constants will be kept in the mesh file.

Finally, when it comes to setting everything up, you just do something like:

foreach engineConst in engineConstants

foreach userConst in userConstants

Then you can add the callback functions using a similar mechanism (in case you want to update user constants every frame).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this