dinsdag 16 augustus 2011

Introduction to HLSL - Part 2

Our first shader
In this part we take the two critical lines of the last part (marked bold) and turn it into a complete HLSL shader. In this introduction I target Shader Model 3.0 which is compatible with DirectX 9.0c.
// These are comments
// 1: Application inputs
float4x4 object_to_clip: WORLDVIEWPROJECTION;

// 2: Structures
struct vs_in {
   float4 pos_object: POSITION;
};

struct ps_in {
   float4 pos_clip: POSITION;
};

// 3: Vertex Shaders
ps_in vs_main(vs_in input) {
   ps_in output;
   output.pos_clip = mul(input.pos_object, object_to_clip);
   return output;
}

// 4: Pixel Shaders
float4 ps_main(ps_in input) : COLOR {
   return float4(1.f, 1.f, 1.f, 1.f);
}

// 5: Techniques
technique main {
   pass p0 {
      VertexShader = compile vs_3_0 vs_main();
      PixelShader  = compile ps_3_0 ps_main();
   }
}
The shader code consists of 5 parts. The application inputs, the structures used (for input and output), the vertex shader(s), the pixel shader(s) and the technique(s). The vertex shader is executed for each vertex in the model and does the minimum it has to do, transform the model into clip space. The pixel shader is executed for each pixel and simply returns a fully opaque white pixel in this case. In the next part we'll add some diffuse lighting.

Geen opmerkingen:

Een reactie posten