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.
Purpose and Functionality
The primary goals of vertex processing include:
- Coordinate Transformation: Transforming vertices from object-space to world-space, then to view-space, and finally to clip-space.
- Lighting Calculations: Applying lighting models to determine the color and intensity of vertices based on vertex attributes like normals and material properties.
- Vertex Attribute Manipulation: Modifying or generating vertex attributes such as texture coordinates, color, and normals.
- Geometry Manipulation: Implementing effects like skinning (for character animation), tessellation, and displacement mapping.
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:
- Position: The 3D coordinates of the vertex in object space.
- Normal: A vector indicating the surface orientation at the vertex, used for lighting.
- Texture Coordinates (UV): Values used to sample textures.
- Color: Vertex color information.
- Tangent/Binormal: Vectors used in normal mapping.
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:
- Object Space: The local coordinate system of the model.
- World Space: The global coordinate system where all objects are placed.
- View (Camera) Space: The coordinate system relative to the camera's position and orientation.
- Clip Space: A homogeneous coordinate space used for clipping primitives against the viewing frustum. The final output of the vertex shader is in clip space.
Transformations
The most common transformations are matrix multiplications:
- Model Matrix: Transforms from object space to world space.
- View Matrix: Transforms from world space to view space.
- Projection Matrix: Transforms from view space to clip space.
These are often combined into a single World-View-Projection (WVP) matrix.
Common Techniques in Vertex Processing
- Skinning: Deforming a mesh based on the movement of an underlying skeleton for character animation.
- Hardware Tessellation: Dynamically adding geometric detail to surfaces using specialized shader stages (Hull and Domain Shaders) that are invoked after vertex processing.
- Vertex Displacement: Modifying vertex positions based on texture lookups or procedural algorithms to create complex surface details.
- Procedural Vertex Generation: Generating vertex data on the fly, such as for particles or procedural geometry.
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 |