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

Managing shaders

Started by AbsolutDew Sep 28, 2006 at 6:10 PM 0 replies 600+ views
Original Post
AbsolutDew
AbsolutDew
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?
deathkrush
deathkrush
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
engineConst.Set();

foreach userConst in userConstants
userConst.Set();

Then you can add the callback functions using a similar mechanism (in case you want to update user constants every frame).
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)

Topic Locked

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

Sign in to reply to this topic.