• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

30 replies to this topic

### #21Steve_Segreto  Members

Posted 19 July 2013 - 01:55 AM

@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).

### #22Medo3337  Members

Posted 19 July 2013 - 12:39 PM

@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.

### #23osmanb  Members

Posted 19 July 2013 - 01:23 PM

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.

### #24Medo3337  Members

Posted 19 July 2013 - 02:24 PM

@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)

Edited by Medo3337, 19 July 2013 - 02:29 PM.

### #25Steve_Segreto  Members

Posted 21 July 2013 - 10:41 AM

@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)

### #26Medo3337  Members

Posted 21 July 2013 - 12:58 PM

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!

Edited by Medo3337, 21 July 2013 - 12:58 PM.

### #27DwarvesH  Members

Posted 28 July 2013 - 07:19 AM

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[i]?

### #28MaxDZ8  Members

Posted 29 July 2013 - 02:36 AM

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"

### #29DwarvesH  Members

Posted 29 July 2013 - 04:02 AM

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: http://www.youtube.com/watch?v=ov3RKuOAt70

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.

### #30cozzie  Members

Posted 08 August 2013 - 12:07 PM

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

### #31DwarvesH  Members

Posted 14 August 2013 - 04:49 AM

Yup, just do it. My version might be hopefully very optimized, but it works. Here is one directional light and multiple point lights:

http://dl.dropboxusercontent.com/u/45638513/02_01.png

I managed to cut down on shader body implementations with parametrized uniform inputs for the pixel shader. You can have theoretically an unlimited number of lights, but after a fair number of pixels affected by the lights and overall objects illuminated, performance drops down quickly. But it is enough for most realistic lighting schemes.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.