Home Articles Tutorials Resources About

Simple Pixel Shader


By Pieter Germishuys, January 9 2006

Today we are going to be looking at the start of the pixel shader beginner series.

What is a pixel shader?

"Pixel processing is performed by pixel shaders on individual pixels. Pixel shaders work in concert with vertex shaders; the output of a vertex shader provides the inputs for a pixel shader. Other pixel operations (fog blending, stencil operations, and render-target blending) occur after execution of the shader."

The pixel shader completely replaces the the pixel processing pipeline This includes modifying vertex and texture data and yielding a pixel color value. A pixel shader is a function that computes effects based on a per-pixel basis.

Taking a look at the pixel processing pipeline

We have to see what the pixel processing pipeline consists of before we can write pixel shaders.
A simplified version of the rendering pipeline works in the following way.
1) Vertex and Primitive data gets sent to Tesselator ("The tesselator unit converts higher-order primitives, displacement maps, and mesh patches to vertex locations and stores those locations in vertex buffers.")
2) These vertices get sent to the vertex pipeline ("As a minimum, a vertex shader must output vertex position in homogeneous clip space. Optionally, the vertex shader can output texture coordinates, vertex color, vertex lighting, fog factors, and so on.")
3) Geometry processing takes place such as Clipping, rasterization is applied on transformed vertices.
4) Texturing takes place such as texture coordinates for 3d surfaces are applied to Direct3D

5) The pixel processing line jumps in here and uses the geometry data to modify the input vertex and texture data resulting in a pixel color.

For now that's where we are going to stop. So take a look at that and let it sink in for a good while maybe read up in the SDK docs at shaders too.. The SDK docs are a wealth of information.
Let's take a look at a simple pixel shader.

A simple pixel shader.

float4x4 worldViewProj : WORLDVIEWPROJ; //our world view projection matrix

//application to vertex structure
struct a2v
{
????float4 position : POSITION0;
????float4 color : COLOR0;
};

//vertex to pixel shader structure
struct v2p
{
????float4 position : POSITION0;
????float4 color : COLOR0;
};

//pixel shader to screen
struct p2f
{
????float4 color : COLOR0;
};

//VERTEX SHADER
void vs( in a2v IN, out v2p OUT )
{
????//getting to position to object space
????OUT.position = mul(IN.position, worldViewProj);
????OUT.color = IN.color;
}

void ps( in v2p IN, out p2f OUT )
{
????//output some color
????OUT.color = IN.color;
}

technique simple
{
????pass p0
????{
????????vertexshader = compile vs_1_1 vs();
????????pixelshader = compile ps_1_1 ps();
????}
}

Running through the shader
We link the color and position from the vertex stream using semantics. This should look familiar if you have read the previous tutorials on vertex shaders. Note the structure we added was a p2f struct. This structures basically specifies a color value that will be sent from the pixel shader to the screen.
Let's look at the vertex shader part. We transform the vertex from object space to screen space. The input color that the vertex has been set to is just transfered to the pixel shader.

Looking at the pixel shader. We just do the same. This means that the color has been passed through the 2 shaders without being modified. Thus resulting in the color that we specified for each vertex when we defined it.

Next we will look at how to extract color information from textures in pixel shaders.




Files for this tutorial

Filename Size
? hlsl5_vs2003.rar 198.1 KB
? hlsl5_vs2005.rar 222.2 KB
?
MDX info is an initiative by NetForge. All content is copyright ? 2005-2006 by its respective authors | About MDX info | Terms of Use |