Vertex Processing

The vertex processing stage is the first programmable stage in the graphics pipeline responsible for transforming and processing individual vertices. It takes vertex data as input, manipulates it, and outputs transformed vertices ready for subsequent stages.

Diagram illustrating the Vertex Processing stage in the DirectX Graphics Pipeline
Figure 1: Conceptual flow of vertex data through the Vertex Processing stage.

Purpose and Functionality

The primary goals of vertex processing include:

Key Components and Concepts

Input and Output

The vertex processing stage receives a stream of vertices, each containing various attributes. The output is a stream of transformed vertices, with clip-space coordinates being the most critical for the next stages.

Common vertex attributes include:

Vertex Shaders

Vertex processing is typically handled by a vertex shader, a small program that runs on the GPU for each vertex. Developers write vertex shaders using high-level shading languages like HLSL (High-Level Shading Language).

Example Vertex Shader (HLSL)


struct VS_INPUT
{
    float4 Position : POSITION;
    float3 Normal   : NORMAL;
    float2 TexCoord : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 Position : SV_POSITION; // Clip-space position
    float2 TexCoord : TEXCOORD0;
    float3 WorldNormal : NORMAL; // Transformed normal
};

// Uniforms (e.g., transformation matrices) would be declared here

VS_OUTPUT Main(VS_INPUT input)
{
    VS_OUTPUT output;

    // Transform position from object space to clip space
    // output.Position = mul(input.Position, WorldViewProjectionMatrix);
    output.Position = input.Position; // Placeholder for transformation

    // Transform normal to world space (if needed for lighting)
    // output.WorldNormal = mul(input.Normal, WorldMatrix);
    output.WorldNormal = input.Normal; // Placeholder

    // Pass through texture coordinates
    output.TexCoord = input.TexCoord;

    return output;
}
        

Coordinate Spaces

Vertex processing involves transforming coordinates through several spaces:

Transformations

The most common transformations are matrix multiplications:

These are often combined into a single World-View-Projection (WVP) matrix.

Important Note: The final output of the vertex shader must be in clip space, represented by homogeneous coordinates (x, y, z, w). These coordinates are then used by the clipping and perspective division stages.

Common Techniques in Vertex Processing

Interfacing with Other Stages

After vertex processing, the transformed vertices are passed to the input assembler (which groups vertices into primitives like triangles), then to the clipping stage, and finally to the rasterizer. The attributes output by the vertex shader (e.g., texture coordinates, colors) are then interpolated across the primitives during rasterization and passed to the pixel shader.

Stage Input Output Primary Purpose
Vertex Processing Vertex Attributes (Position, Normal, TexCoord, etc.) Transformed Vertices (Clip-space Position), Interpolated Attributes Coordinate transformation, lighting, per-vertex effects
Clipping Primitives (Triangles, Lines, Points) Clipped Primitives Removes geometry outside the view frustum
Rasterization Clipped Primitives Pixel Fragments Converts primitives into fragments for shading