Jump to content
  • Advertisement
Sign in to follow this  
Atrix256

How to handle multiple lights in a single shader?

This topic is 3254 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

Hey Guys, Im converting over a project im working on from fixed function pipeline to shaders. It's going fairly well but one thing im wondering about is this... I have volumes set up in my world that have light definitions which is basically 0 to 8 lights, each being directional or positional lights that effect objects within that volume. I was just wondering, what is a good way to handle this in shaders? Should i make a different shader per volume? ie a volume has 3 positional lights and 1 directional light, should i make a shader that takes 4 sets of light values as a parameter (type, direction or position, color etc)? Or should i make one shader that takes 8 sets of light values and the lights that are off get color values of 0,0,0? Or is there a better way than those? BTW i am using HLSL and shader model 2 (not married to shader model 2 but it seemed reasonable as not too old, not too new - for compatibility vs features) Thanks!!

Share this post


Link to post
Share on other sites
Advertisement
If it's not too much work (how many different combinations do you have, and can you generate the different shaders automatically perhaps?), then performance will probably be better with different shaders. For older graphics-cards that only support shader model 2 this can be important for complicated shaders.
I don't remember how shader model 2 works with dynamic branching.. but you can pass a variable to the shader saying how many lights are on, and loop only that many times. But again, shader model 2 might not support that.. as it unrolls loops and has a maximum instruction count.

Share this post


Link to post
Share on other sites
Ok, that makes sense.

Thanks (:

Does anyone have any info about what % of computers out there support what shader models?

I was making a (perhaps un)educated guess and figured 2 was middle of the road but i don't know if that is actually true.

Share this post


Link to post
Share on other sites
Shader model 2 does not support dynamic branching, so you can't use that. DB can have severe performance implications on certain GPU's anyway, so generally you don't want to use it if you have alternatives.

vs_2_0 does support static branching, which is where you use special boolean registers to toggle sections of code on or off (they have to be set by your application prior to drawing with the shader). It also supports integer looping in a similar manner. ps_2_0 does not support this, but ps_2_x does (ps_2_a is supported by Nvidia FX-series and higher, ps_2_b is supported by ATI X-series and higher)

Another alternative that doesn't depend on your shader model is to use the effects framework to auto-generate variants of a shader for you. See this.

If you're looking to figure out what hardware is out there, the Steam Hardware Survey is a great resource.

Share this post


Link to post
Share on other sites
I have recently implemented a shader which does spot , point and directional branchlessly using some trickery.

so a directional light has constant attenuation and the light direction is always the same and point light has varying direction per pixel and has attenuation.

I pass in a point light as a 4 component vector direction.xyz and the 4th component is there to indicate if its directional or not. when working out direction of a light I always calculate the direction to each pixel.



//branchless selection of lights setup directions

//if light is directional (w = 1.0) then this will be multiplied by 0
vec3 light_direction =
normalize(light_pos.xyz - position) * (1.0 - light_pos.w);

//if light is directional then the position is used as the light direction
light_direction += normalize(light_pos.xyz * light_pos.w);

//calculate attenuation as normal
//this ensures that if the light is directional attenuation is 1
attenuation = min(attenuation + light_pos.w,1.0);




I also do this for spot lights and can deal with them branchlessly. of course you end up doing redundant calculations in the shader, I dont know if this is a good way or not but I just don't like branching lol.

Share this post


Link to post
Share on other sites
that's really neat you guys.

thats a smart way of using branchless calculations... i got to see michael abrash's introduction to the intel larabee chip and he did similar techniques for a few different things.

the effects framework looks bad ass though, i really love the idea that my artists can use the fx composer to build shaders that work in game without me needing to intervene or build them an editor of any kind!

I think im going to go that route, but not sure yet how to integrate the fx files they make with the lighting defined in the map editor ::ponders::

anyhow thanks you guys for the help, you have deffinately illuminated the path (:

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!