home/ atoms/ wgsl-inter-stage-variables

Data passes from vertex to fragment shaders as inter-stage variables declared with @location attributes, automatically interpolated across triangles

Values computed in a vertex shader can be passed to the fragment shader as inter-stage (interpolated) variables. The vertex shader returns a struct with both @builtin(position) and additional @location(n) fields. The GPU interpolates these values across the triangle’s surface — values at corners are smoothly blended at interior pixels. The fragment shader receives them as @location(n) inputs (or by reusing the vertex output struct as its input type). This is how cell coordinates, normals, UVs, and colors travel through the pipeline.

Examples

struct VertexOutput {
  @builtin(position) pos: vec4f,
  @location(0) cell: vec2f,
};
@fragment
fn fragmentMain(input: VertexOutput) -> @location(0) vec4f {
  return vec4f(input.cell / grid, 0, 1);
}

Assessment

Three triangle vertices have @location(0) values 0.0, 0.5, 1.0. What value does the fragment shader receive at the pixel exactly halfway between the 0.5 and 1.0 corners?

“To pass any data between the vertex and fragment stages, you need to include it”
corpus · your-first-webgpu-app-google-codelab · chunk 12