Understanding Limits on Older Shader Models


Because you manipulate a pixel shader in this code, one thing you might notice is that the number of available instructions you can use in a 1.1 pixel-shader pass is extremely limited. For pixel shaders under version 1.4, you are limited to 12 instructions for the entire shader program, which obviously isn't much. For version 1.4, the limit is raised to 28 (in two phases), but that is still not much. You are also limited to 8 constants in these shader programs. Pixel shader versions 2.0 and higher have the ability to perform more complex operations and have higher instruction and constant counts. You can check the capabilities for your card to find out how many of each are supported.

For the first part of the lighting code here, you use the same pixel shader you had in the beginning, but once you move on to the per-pixel lighting, you will quickly realize that you've run out of instruction space. Although you could probably tweak the shader to fit within the limited space, it's probably easier to just force the 2.0 shader model (which is what you'll be doing).

Two things you also might not be aware of are flow control and looping, both of which are available in shaders. Older shader models do not natively support flow control, or branching. To facilitate this, when generating the actual shader code, the HLSL compiler might actually unwind a loop or execute all branches of if statements. This point is something you need to know, particularly if you are already writing a complex shader that is stretching the limits of the instruction count. Take a simple loop such as the following:

 for(int i = 0; i<10; i++) {     pos.y += (float)i; } 

Even though there is only a single statement in the line, this shader code once unwound takes up a whopping 20 instructions (at least). When your instruction count limit is 12 (as for pixel shaders 1.1), this simple loop wouldn't compile.

With newer hardware coming out that supports the 3.0 shader models (and even current hardware that supports the 2.x shader models), these issues are less of a problem.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net