HLSL's, Cg and the RenderMonkey

But HLSLs Can Make Life Easier

I've discussed the combination/ permutation problem in great depth in my last article, Vertex Shaders and Pixel Shaders. This is still a big problem when using an HLSL to target the DX8 platform. However, there are a number of ways that HLSLs can actually make this problem easier to solve.

The most obvious example of the problem is where different lighting needs to be carried out on geometry. In the past, people have used fragment processors to enable them to recompile code on the fly. What this means is that vertex shaders are written in small blocks. Each block has to take its inputs from certain temporary registers, and place its outputs on either the output register or another temporary register (depending on how general the fragment system is). This system is a lot more powerful using an HLSL, because each fragment can just operate on local variable names. Once the fragments are stitched together, the compiler should take care of register allocation and mov elimination, the two biggest problems with fragment systems.

The alternative to a fragment system is a set of general shaders, which cater to common lighting environments. Different lighting environments aren't catered to with different shaders, they're just shoehorned into the general shader that has already been written. This system is also helped by HLSLs where the time taken to write each general shader drops dramatically, allowing the programmer to create more general shaders to cater to a variety of different lighting environments. Because HLSL is so much quicker to develop, these variants are very quick to write.

To be honest, I'm not entirely sure that HLSLs have much to give in the case of pixel shaders. As I've already discussed, the programmer is going to have to be on his toes at all times when playing with DX8 pixel shaders. However, there is something to be said for increased code clarity. And in the future, the pixel shaders are going to get more complex, and more general, making HLSLs imperative if we're to take full advantage of the hardware. For now, it doesn't hurt to use HLSLs for pixel shaders, but don't expect beginners to understand what's going on without teaching them the assembly level first.