The shader pipeline
On this page

The shader pipeline

A render pipeline transforms vertex data into candidate fragments and applies tests and output operations. A compute pipeline dispatches shader invocations directly to process resources.

When it helps

Choose the stage from the job: place geometry in the vertex stage, shade covered locations in the fragment stage, or update indexed data in compute.

Worked example

Assume equal clip-space w at the triangle vertices. Interpolating their colours with the displayed weights gives a weighted colour at the fragment.

Example reasoning
a = (1, 0, 0)
b = (0, 1, 0)
c = (0, 0, 1)
weights = (0.5, 0.25, 0.25)
color = 0.5 * a + 0.25 * b + 0.25 * c
color = (0.5, 0.25, 0.25)

Practical limits

Perspective interpolation generally depends on clip-space w. A fragment can also be discarded or fail depth and stencil tests, so running a fragment shader does not guarantee a stored pixel.

A shader pipeline is the chain of steps a GPU runs for one draw. The vertex stage and the fragment stage are the shaders in it. Fixed steps sit between them: primitive assembly, clipping and rasterization. A compute pipeline holds one stage, the compute stage. This page says what each stage is handed and what it gives back, and leaves the syntax to the language guide.

Steps of a draw

The WebGPU specification defines a draw as these steps, in this order. A step that names an entry point runs your shader code. Fixed-function hardware runs the other steps.

StepWhat runsWhat it does
Index resolutionFixed functionBuilds the list of vertices to process, from the index buffer or from the vertex count of the draw.
Vertex processingYour vertex entryRuns once per vertex and returns a clip-space position and the values the fragment stage reads.
Primitive assemblyFixed functionGroups the vertices into points, lines or triangles, as the pipeline topology says.
Primitive clippingFixed functionCuts each primitive to the clip volume.
RasterizationFixed functionFinds the fragments each primitive covers and the weight of each vertex at each fragment.
Fragment processingYour fragment entryRuns once per fragment on the vertex outputs weighed for it, and returns a value for each colour attachment.
Output mergingFixed functionUpdates depth and stencil from the depth and stencil tests, then blends and writes the colours of each fragment that passed both.

Stages

Each row is one stage of the pipeline the host set up.

StageWhat it is handedWhat it produces
VertexOne vertex of the draw: its index in the draw, and the fields the host laid out in the vertex buffers.A clip-space position, and the values the fragment stage will read.
FragmentThe values the vertex stage produced, weighed for this fragment, and the fragment’s own position.A value for each colour attachment the pipeline declares.
ComputeIts own place in the dispatch grid, and the resources the host bound.Nothing given back; a compute entry writes through the resources it holds.

Entry points

The signature of an entry point is that stage interface written out. A builtin<N> parameter is a value the stage hands the invocation, such as the vertex index or the fragment position. A struct parameter is per-vertex input in a vertex stage and a weighed value in a fragment stage. The return type is what the stage gives back to the pipeline, so a vertex entry returns a position and a fragment entry returns colour. Shader stages has the stage attributes and the spellings; this page stays with the meaning.

"use typeshade";
class Camera {
view: mat4
pos: vec3
}
declare const camera: uniform<Camera>
interface VsIn {
position: location<0, vec3>
uv: location<1, vec2>
}
@vertex
export function vs(vid: builtin<"vertex_index">, vin: VsIn): vec4 {
return camera.view * vec4(vin.position, 1)
}
@fragment
export function fs(pid: builtin<"position">): vec4 {
return vec4(pid.x, 0, 0, 1)
}

Reading a signature therefore tells you which stage the function belongs to, what the pipeline has to supply, and what the pipeline receives.

Interpolation

Between the vertex stage and the fragment stage the rasterizer works out which fragments a primitive covers. For each of them it weighs the values the vertices produced by how near the fragment lies to each vertex, and hands the fragment stage the result. A vertex entry writes a value per vertex and a fragment entry reads a value per fragment, so the two are different values with the same name.

class VsOut {
pos: builtin<"position">
uv: location<0, vec2>
id: location<1, u32>
tint: location<2, vec3, { interpolate: "flat" }>
}

A float location<n, T> field is weighed this way unless its interpolate option asks for another mode. An integer cannot be weighed, so the compiler gives id the flat qualifier on both targets without being asked, and the fragment reads the value one vertex of the primitive produced. { interpolate: "flat" } asks the same for tint. Types states the attribute types.

Compute

A compute stage has no rasterizer in front of it and no attachment behind it. The host dispatches a grid of work items, the entry point reads its own coordinates in that grid from a builtin parameter, and everything it produces goes through a storage resource. Resources states how a writable resource is declared.

"use typeshade";
class Camera {
view: mat4
pos: vec3
}
declare const camera: uniform<Camera>
declare const pixels: storage<array<f32>, "read_write">
@compute([64, 1, 1])
export function paint(gid: builtin<"global_invocation_id">) {
pixels[gid.x] = camera.pos.x
}

Further reading

Next in this path: WebGPU and WebGL2, which divides the work between the host application and the compiler.

Edit this page Report a problem