ColumnLoops and ShadersColumn

Okay, so you want to write a shader to handle the creation of tons of simple per-pixel shadows.

Theory #1: Pack all the data about the light sources and shadows into a texture, and use these packed values inside the shader to draw the shadows in large values. You just have to loop through all the data, and viola!.. you have the ability to cast tons of shadows onto your scene.

In Practice: It turns out that packing values into a texture isn’t all that complicated. I already worked out what information I need to make the shadows in my demo program. I already has classes set up to generate points for the shadows. But when I started writing the shader with loops, I ran into problems. The first thing that happens is that the shader compiler tell you that it WILL NOT compile a loop if it can’t determine how many times it will execute. It tells you that the will execute 1024 times and it stops.

During my research, I found two ways around this issue. The formal way to use compiler options specifying that you intend to use flow control in the shader, and it will be nicer about letting you do it. The second method is to enforce a maximum value with the modulus operator.


for(i=0; i<numberOfLoops % maxValue; i++)
{

}

After reading about other people’s struggles with the same issue, it turns out that either of these methods will likely decrease performance. Some cards may not even support the flow control statements inherently, and so it will write your loop out into regular steps, many of which may get ignored during execution if you aren’t looping to that max value.

Theory #2: The simplest method then becomes writing the shader to handle ONE shadow at a time, and render once for every shadow in the scene. However, if the intent is to support LOTS of shadow on average, it may be good to support two or four shadows per pass, maxing out the register space to make each pass as effective as possible. This will cut the number of renders in half or better, depending on how shadow data slots I can cram in there.

I think this will be the best solution overall. Stay tuned for screen shots.

For those of you who read the previous post, here’s a zip file with the test application.

Click here > ShadowExample.zip

Leave a Reply

You must be logged in to post a comment.