Archive for February, 2008

Loops and Shaders

Friday, February 1st, 2008

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.
(more…)