Sign in to follow this  
YellowMaple

Simple shader questions

Recommended Posts

YellowMaple    174
Hello, I'm still in the process of learning shaders and was hoping someone could give some advice and answer some questions: When using shaders it seems that you are responsible for your own lighting calculations. So to do this, I've created a couple of arrays of float3's of size MAX_LIGHTS in my standard hlsl file to represent direction and rgb values for each light and I set and update these the value of these lights from code. In the future though, if I decide to have some meshes use a different vertex/pixel shader in another file, I'd have to declare these float3 arrays again and update them separately in my code and so on for any additional shaders I would add. So, I specified the registers these variables belong to so if I update the values once, they'd apply to all shaders.
float3 g_LightDirection[ MAX_LIGHTS ] : register(c4);

Is this the 'proper' way of doing things? Which leads me to my next question; how many registers can you assume there are in a shader? Is there a standard that specifies some max? Or is there a way to check? i.e. if in the above declaration for g_LightDirection I used register(c16) instead of c4, am I guaranteed that c16 exists? What would the highest register be? Finally, is it common for pixel shaders and vertex shaders to be grouped into the same shader/hlsl file? Is there any benefit to splitting up pixel and vertex shaders into separate hlsl files or to keeping them in the same file? Any advice would be much appreciated. Thanks!

Share this post


Link to post
Share on other sites
MJP    19788
Setting the value of constant register once for something like light properties is fine, as long as you can make sure that you reserve those registers so that other constants don't use them. A simple way to do this is declare shared parameters in a separate file, and then #include them in the rest of your main shader files (even if they don't need them). This if you have a collision the compiler will complain, and if you need to make a change you only do it in one place.

The number of registers depends on the register type and the shader profile. The docs have a page for each shader profile with a table telling you the max number...for instance the docs for vs_2_0 are here.


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