Multiple lights - Directx10

Started by
7 comments, last by MJP 14 years, 10 months ago
Hi all, Just wanted some response about how to implement multiple lights in Dx10. I have looked about the forums and seen things about deffered shading etc and not sure really what route is to take. As present I have only had one active light in each scene. I assume deffered shading is the alternative to just looping through the passes in the effect again with a different light as this would be hideous. So what is deffered shading, how does it work? Hopefully you guys can give me a good insight / some interesting tutorials as always. Thanks guys.
Advertisement
You can easily do an array of light data structures and use them in one pass in fx_4_0.

Just add the contribution of each in a loop in your pixel shader.
Is it possible to have dynamically sized data types in the .fx file?

For example have a vector of lights like vector<Light> Lights then update this through code allowing for lights to be added and removed?
Quote:Original post by Maddius
Is it possible to have dynamically sized data types in the .fx file?

For example have a vector of lights like vector<Light> Lights then update this through code allowing for lights to be added and removed?


Nope, only static array.
Okay I have a problem / question, in my effect file I have my cb defined as:

cbuffer cbPerFrame
{
Light gLight[3];
float3 gEyePosW;
};

So thats an array of three lights, the light structure is defined in another file, but I know it works as I have tested each light individually.

Now in the C++ file I have an equivilent array of lights:

Light mLights[3];

These all get initalised with their data etc etc, then my question is this, when I get the variable by name from the effect file, does it pass it back as an array? Because the problem im having is that when I pass the array in from the C++ file only the first entry seems to work in the effect file. If I change each light to be the first entry they all work fine.

So I get the data like this:

mfxLightVar = mFX->GetVariableByName("gLight");

mfxLightVar is a instance of ID3D10EffectVariable*, does this need to be an array? I thought it would just get the data either way?

Then I set the data like this:

mfxLightVar->SetRawValue(&mLights, 0, sizeof(Light) * 3);

If anyone could point out the problem, I would be happy. I basically assume the whole array of data isnt getting passed into the array the otherside, but I dont know why. Never done HLSL before so its all new.

Thanks guys.
Okay I got it working, two directional lights point the opposite direction of each other.

I did this by using an adapted version of the implementation from the forum thread found at http://www.gamedev.net/community/forums/topic.asp?topic_id=534310 .

I wanted to know is this a worthwhile method to employ? How many lights are common to have active in a modern game, because while rendering a simple scene if I use 1 light the FPS idles at roughly 500FPS.

Now I introduce the new light by looping through the light array then the FPS drops to around 350FPS obviously this is not a problem in the test scence but do you think this kind of drop is acceptable and is the method worth expanding upon?

Thanks for all your help so far.
It really depends on the game you're making and what sort of scenes you're going to set up. Many games just have one or two primary directional lights, and very very few local light sources. Some games have tons of local light sources in small confined spaces. For the former a forward renderer where your shader loops through light sources is ideal. For the latter, a deferred approach usually works much better.

BTW with regards to your framerate drop...keep in mind that FPS is not linear. Therefore you can't measure the performance impact of something by measuring the difference in framerate. You need to convert back to milliseconds, and compare those values. In your case 500fps is 2ms per frame, and 350fps is 2.8ms per frame. A difference of 0.8ms is pretty small.
Okay im looking to create a small FPS demo in a small world so loop through is probably just fine. On a side note, what is deferred shading all about and how would you implement it?

From what I can see its rendering each light source to a differnt buffer and then combining them at draw time, is that so?
Here...this presentation should explain a few things. Might want to take a look at this too.

This topic is closed to new replies.

Advertisement