Jump to content
  • Advertisement
Sign in to follow this  
ogracian

Handling multiple lights like Fixed-Pipe with shaders...

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am currently thinking in rewrite my shader implementation (a shader script much like in quake III), to support pixel / vertex shaders. The main reason is that my current implementation need to update buffers using CPU (vertex anim, color src, etc.), so I would like to use vertex shaders to avoid locks. The point wich is stoping me to start this port is about lighting, in my current implementation when I need hardware lights, I just activate those lights needed for my mesh using SetLight method (up to 4 per mesh in my engine), so the Fixed Function Pipeline handle all the vertex lighting for me. So my question is how can this be handled using vertex/pixel shaders, I mean, need I to code my diffuse lighing shader to handle different light numbers like: diffuseLightVS_1PointLight diffuseLightVS_2PointLight .... .... .... diffuseLightVS_NPointLight Im asking this because as far as I know the Fixed pipeline use shaders internally, so how this "Fixed-Function pipeline", handle to render N lights (only those actived by SetLight method) PS: Im refering as lighting to simple per vertex diffuse lighting like F-FP support. Thanks in advance, Oscar

Share this post


Link to post
Share on other sites
Advertisement
You can use a for loop inside your shader to accomodate a different number of lights.

If you're using HLSL for your shaders you can either use this dynamically or you can use it as static branching so you write it once and then compile it several different times for different numbers of lights.

This is similar, in principle, to the SDK samples which perform skeletal animation and use a for loop depending on the number of bones.

-Mezz

Share this post


Link to post
Share on other sites
cool, thanks for your reply. I will take a look at the sample you suggest me. But on the other hand are the loops inside shaders supported by version 1.1? , because currently I am using a geForce 3 wich the maximum version of shaders it support is 1.1

PS: Could you develop a little more about the static branching method, I mean is it some kind of templates with shaders?

Regards,
Oscar

Share this post


Link to post
Share on other sites
Yes, sure thing :)
if you're using vs_1_1 you will be limited to static branching - i.e. using a for loop based on a constant (I think they are called uniform) value. This, I guess, is something like having templates in a shader because you write it once and compile it many different ways. It is almost definately the easiest and most robust way of supporting multiple lights because you are only writing the code once, and as you compile it out to different targets you are not incurring any performance hit.

If I just make up a little example (excuse the fact that I don't define any structures, it should be obvious from their names...) things should make more sense:



VS_LightOutput LightingFunction(VS_LightInput lightInput, int numLights)
{
for(int i=0; i < numLights; ++i)
{
// do lighting...
}
}

VS_Output MainShader(VS_Input input, uniform int numLights)
{
// call the lighting func
// having made up the input structure, and using the
// uniform parameter for this function!
LightingFunction(someLightInput, numLights);
}

// and to compile....

VertexShader vsArray[4] =
{
compile vs_1_1 MainShader( 1 ),
compile vs_1_1 MainShader( 2 ),
compile vs_1_1 MainShader( 3 ),
compile vs_1_1 MainShader( 4 )
};




Incidently, I think you need to use .fx files to do this properly.

-Mezz

Share this post


Link to post
Share on other sites
Thanks a lot for your help, I got it clear the way to go now.

PS: Your example clarify the things a lot.

Best regards,
Oscar

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!