Shader Unlimited Lights

Started by
29 comments, last by DwarvesH 10 years, 8 months ago

@Steve_Segreto: So, If I don't want to run into performance issues for most graphic cards out there and If I don't want to run into constant limits problems.

What is the maximum number that I should use for point, spot, directional lights in the shader array?

I believe for most graphic cards out there you should try to stay under 256 float constant registers (shader model 2.0).

Advertisement

@Steve_Segreto: Even If there are many variables?

Example:


LightDirectional directionalLight[8];
PointLight pointLight[100];
SpotLight spotLight[100];

Basically, I'm trying to set the maximum number possible.

How complex is your lighting model? How much math are you planning to do per light? How many grains of sand are there on a beach? These questions can't be answered in a vacuum, which is why people are trying to steer you to figure out the right answer (or create scalable solutions) for yourself. If I say: "You can support exactly 8 point lights in a SM3 shader", while assuming my particular lighting equation, that doesn't mean anything. Plus, you need to budget the rest of the frame (shadows, AO, defocus, particles, whatever...). How much GPU time do you want to spend rendering lights? What's your target hardware spec? More importantly - how many lights do you actually need to make your target content look good? Everything else is irrelevant noise.

@osmanb: I'm creating a Game Engine, I'm NOT sure how many light each game or scene will include.

So basically, I'm trying to set the maximum possible, I believe 8 lights per mesh should be enough for MOST games, but still, If I could allow more then it would be better.

The thing that I'm concerned about is that I could have a huge building (one mesh) which could include many lights (even more than the maximum in shader)

@Steve_Segreto: Even If there are many variables?

Example:


LightDirectional directionalLight[8];
PointLight pointLight[100];
SpotLight spotLight[100];

Basically, I'm trying to set the maximum number possible.

Hi Medo,

First convert the max number of floating point registers available for your shader model from a count of float4s to a count of bytes (16 per register). Then you need to take the sizeof( LightDirectional ) in bytes (multiplied by 100, since you want that many), plus the sizeof( PointLight ) in bytes (multiplied by 100) plus sizeof( SpotLight ) multiplied by 100. Now subtract that from the number of bytes available for variables/constants and that should tell you if you could compile that declaration within that shader model. Or you could just run fxc from the command line and try it out :)

Also if you were confused because I said 256 const float registers, I was just using some terminology I've seen in DX9 docs, I believe those same registers are used for uniform externs that you can set from a C++ program (i.e. variables)

Hmm, I believe setting 10 for spot light and 10 for point light and 3 for directional light should be enough, I think it won't affect the performance though.

I don't know why HLSL doesn't support dynamic arrays!

Wow, I didn't know about the limitation with dynamic indexing! There is so much I still don't know.

I am implementing my own multiple point light shader and I was wondering If I write permutations for every single number of point lights (up to a reasonable limit) and then use only constant indexing, can I overcome the generation of the hidden "if" statements? Basically figure out how many point lights are affecting the object, and render it using the appropriate shader.

Also, if instead of creating a new output variable in the vertex shader like this:


void VS_MainNM(in  float3 inPos       : POSITION,
			 in  float2 inTexCoord  : TEXCOORD,
			 in  float3 inNormal    : NORMAL,
			 in  float3 inTangent   : TANGENT,
			 
....

			 out float3 outPoint1Dir : TEXCOORD5,
			 out float3 outPoint2Dir : TEXCOORD6,
			 out float3 outPoint3Dir : TEXCOORD7,
			 out float3 outPoint4Dir : TEXCOORD8

I was thinking I could use and array:


void VS_MainNM(in  float3 inPos       : POSITION,
			 in  float2 inTexCoord  : TEXCOORD,
			 in  float3 inNormal    : NORMAL,
			 in  float3 inTangent   : TANGENT,

...

			 out float3 outPointDir[4] : TEXCOORD5

I tried out the shader and it works. But my question is will I be able to avoid the hidden "if"s if in the pixel shader I am always using constant indexes, like inPointDir[2] and never int i = 2; inPoint1Dir?

Arrayed var-out? Is that even possible? I'd be super extra special careful. Shader permutations are serious trouble, don't take them lightly (no pun intented). Proper shader authoring is your fast ticket out of this.

Previously "Krohm"

Yes, it is possible, and seems to work just fine. Here is an early prototype of terrain lighting with multiple point lights with forward rendering:

Basically an infinite number of lights, but you can only have limited number of lights in close proximity to each other. Only the lights that effect terrain (and a little bit extra to prevent light pop-in) are rendered with a constant cost picking algorithm, based only on view distance and the number of lights that are available in that view distance. Ignore the normal bleeding and the fact that every 5th light I add changes the position of every 4th. I had a bug there in the array assignment.

I'm just wary of not doing things in a particularly slow way. The arrays makes the code more manageable and I'm avoiding loops, unrolling all of them manually and using only constant indexes. I'm hoping that the compiler does not use an array and I just get the benefits of the syntactic sugar of arrays.

The pixel shaders takes a lot of uniform boolean input parameters and I use them in if statements to avoid parts of the shaders to have a smaller and more manageable number of written permutations needed. I suspect the compiler is smart enough to remove the unused code on a per technique basis. I am also trying to write the arithmetics as efficiently as possible to make the job of the optimizer easier.

My advice would be like nike says, "just do it".
Make a shader with for example 2 directional and 4 point lights, make a test scene and see how it looks. Next step could be adding specular. See how it looks, optimize abit. And so on and so on.

No offense, but reading your initial question I'm afraid there are no shortcuts
(speaking from own experience)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement