DirectX Documentation

Geometry Shading

Geometry shaders are a programmable stage in the DirectX rendering pipeline that can take a primitive (a point, line, or triangle) as input and output one or more primitives. This allows for dynamic generation and manipulation of geometry on the GPU, opening up a wide range of visual effects and optimization possibilities.

Overview

Unlike vertex shaders, which process one vertex at a time, geometry shaders operate on entire primitives. They are invoked after the vertex shader and before the rasterizer. A single invocation of a geometry shader receives a primitive (and its associated vertices) and can emit:

Geometry Shading Flow Diagram

Simplified flow of a primitive through the rendering pipeline with Geometry Shading.

Key Capabilities

Input and Output

Geometry shaders can be configured to accept specific input primitive types, such as:

The output primitive type is specified when writing the shader and can be:

A crucial aspect is the ability to emit vertices. Each emitted vertex can have its own set of attributes, including position, color, texture coordinates, and other custom data.

Example Snippet (HLSL)

Here's a conceptual example of a geometry shader that takes a triangle as input and outputs two triangles (effectively duplicating the original).


// Input primitive type: Triangle
// Output primitive type: Triangle List
[maxvertexcount(6)] // Max output vertices: 2 triangles * 3 vertices/triangle
void GS(triangleinput primitive[3] : SV_POSITION)
{
    // Emit the first triangle (original)
    for (int i = 0; i < 3; ++i)
    {
        AppendVertex(primitive[i]);
    }
    EndPrimitive();

    // Emit a second triangle (e.g., slightly offset or modified)
    for (int i = 0; i < 3; ++i)
    {
        // Example modification: offset in Y
        VertexOutput v;
        v.position = primitive[i].position;
        v.position.y += 0.1f; // Simple offset
        v.color = primitive[i].color;
        AppendVertex(v);
    }
    EndPrimitive();
}

Considerations

Use Cases

Geometry shaders provide a powerful tool for developers to extend the capabilities of the DirectX rendering pipeline, enabling advanced graphical techniques and optimizations.