Chapter 8. The Zen of Shader Programming
Zen of Pixel Programming
Now that you have the HLSL language concepts firmly in hand, let’s consider how to use them in common scenarios. This final chapter explores the essential subjects that every shader developer needs to know.
Sampling a Pixel Color
Examine the main function signature in any shader code (Example 8-1).
Example 8-1. Sampling pixel color in shader
sampler2Dinput:register(S0);float4main(float2uv:TEXCOORD):COLOR{float4color;color=tex2D(input,uv.xy);returncolor;}
The parameter passed into the function is typed as float2. It represents the x and y coordinates
of the current pixel. As each pixel is processed by the shader, the
float2 parameter provides access to
the current location. So what do you do with that information?
A common task is to use the coordinate to determine the current
pixel color. To accomplish this task requires the talents of the
tex2D function. I looked up the
definition of the text2D function in the DirectX documentation and it
states:
“Samples a 2D texture.”
If the documentation writer was looking to accolades for the shortest answer, this little nugget is a prizewinner. Short, maybe, but not very useful. Let me try.
The tex2D function performs a
texture lookup. In other words, it examines a given texture (a pointer
to an image) and returns the color value at the specified location.
There are other texture functions (tex1D and tex3D) available, but for now we’ll stay with the 2D version. The ...