• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Shader Unlimited Lights

30 posts in this topic

I don't know have many lights will be used in the scene, so I'm looking for a way to be able to pass any number of lights.

 

I thought that I could create array with high number of lights in the shader and then tell the shader how many lights was passed for light calculation, for example:

// Shader
#define MAX_LIGHTS 8 // *** I don't know how many lights, so I will assume 1000 is the maximum possible ***
PointLight pointLights[MAX_LIGHTS];
DirectionalLight directionalLights[MAX_LIGHTS];
SpotLight spotLights[MAX_LIGHTS];

float numOfLights;

// Then loop in the shader
(for int i = 0; i < numOfLights; i++)
{
     // Code to calculate light here...
}

Is this way a good idea?

Edited by Medo3337
0

Share this post


Link to post
Share on other sites

@belfegor: That wouldn't be efficient, some important lights MIGHT not affect the mesh, since I wouldn't be able to figure out 100% which is important, what if more than 4 light sources affecting the mesh?

0

Share this post


Link to post
Share on other sites

@Migi0027: Don't you think that I can just calculate the distance between the light source and the mesh? I guess that's easier and faster...

 

What If more than 4 lights affecting the mesh while the shader only allow 4 lights per pass?

0

Share this post


Link to post
Share on other sites
D3D9 pixel shaders have a major limitation, which is that they can't dynamically index into shader constants. This means that it can't use an actual loop construct in assembly to implement your for loop, instead it has to unroll it and do something like this:

 

 

It's possible to use a loop with D3D9, take a look at this sample:

http://www.dhpoware.com/demos/d3d9NormalMappingWithManyLights.html

0

Share this post


Link to post
Share on other sites
Okay, how about I use 8 lights for each type of light?
 
Is that good?
#define MAX_LIGHTS 8
PointLight pointLights[MAX_LIGHTS];
DirectionalLight directionalLights[MAX_LIGHTS];
SpotLight spotLights[MAX_LIGHTS];

Since 99% of the cases the lights would never exceed 8 lights on a single mesh.

Edited by Medo3337
0

Share this post


Link to post
Share on other sites

Do you really need that many directional lights? I suspect you can probably get away with just one for sunlight.

 

Does 8 lights per object look any better than 4 lights per object in the scenes you'll be rendering?

 

What's the performance like with 8 lights on your target hardware spec?

 

Essentially what I'm saying is that whatever number you come up with may have to change later. Don't worry too much about that yet, other than to make sure you can easily adjust it later when you know what your actual requirements are.

1

Share this post


Link to post
Share on other sites

Unless I'm mistaken, you've never actually stated you are working exclusively with D3D9, so if you are so concerned with the number of lights you can support, then why not use deferred shading or some type of tiled forward renderer? Even if you are limited to D3D9, you could still use deferred shading.

0

Share this post


Link to post
Share on other sites

@Chris_F: Not planning to get into deferred shading now, and I would run into some problems for example (transparent mesh)

 

@Adam_42: Hmm.. I could make directional light max: 2, other lights max: 8, do you think that's okay?

 

I think even 10 should be fine, because I will only calculate the lights that I passed to the shader.

0

Share this post


Link to post
Share on other sites

One can break the constant limit - and force to use a proper loop - in SM 3.0 by encoding stuff in textures, using dynamic textures and uploading your data with LockRectangle. 

 

*digging up my D3D9 stuff*

 

This is a quick check with 100 point lights. The texture is 4-channel float, 3 texels are needed per point light. 

 

83d765266027189.jpg

 

Is this a good idea ? I doubt it. Though I'm surprised it still runs smooth I wonder how that scales (this was just one model). Rather go with a deferred approach and cull the lights by distance/visibility as others suggested. 

 

It may not be a good idea, but one thing it is, it's interesting! huh.png

0

Share this post


Link to post
Share on other sites

Off topic:

 

It has it's use, e.g. here it's used for skinning a massive amount of characters (vertex texture fetch, not pixel shader this time). This approach is also useful if you have a model with a hell of a lot of bones.

 

I'm posting since you are using D3D11: The constant limit is considerably higher and even if you're hitting the limit, you don't need to use this approach - directly. Have a look into structured buffers. The usage is quite convenient: you really just define the struct in both shader and C++ and use with an array syntax. Under the hood something similar is happening as above: a "texture buffer" is used (HLSL register t#).

0

Share this post


Link to post
Share on other sites

One can break the constant limit - and force to use a proper loop - in SM 3.0 by encoding stuff in textures

I think there's an echo in here tongue.png

2

Share this post


Link to post
Share on other sites

Okay guys, I want a direct answer...

 

Is it OK to do the following?

PointLight pointLights[6];
SpotLight spotLights[6];
DirectionalLight directionalLights[2];
0

Share this post


Link to post
Share on other sites

 

Okay guys, I want a direct answer...

 

Is it OK to do the following?

PointLight pointLights[6];
SpotLight spotLights[6];
DirectionalLight directionalLights[2];

Sure why not?

0

Share this post


Link to post
Share on other sites

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

Edited by Medo3337
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

-1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0