• Advertisement
Sign in to follow this  

Shader Unlimited Lights

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

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

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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.

Share this post


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

  • Advertisement