A WGSL fragment function can reuse the vertex stage's output struct as its input type, ensuring @location consistency automatically
When the vertex and fragment shaders are in the same shader module, the fragment function can declare its input as the same struct type returned by the vertex function. Since the struct already carries the correct @location(n) annotations, field names and binding indices are guaranteed consistent. This is more maintainable than declaring a separate input struct for the fragment stage: adding a new inter-stage variable only requires changing the VertexOutput struct definition in one place. All three patterns (individual @location args, separate FragInput struct, reuse VertexOutput) produce the same GPU behavior.
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);
}
// Equivalent to declaring: fn fragmentMain(@location(0) cell: vec2f) -> ...
Assessment
Name the three WGSL patterns for receiving inter-stage variables in a fragment function. What is the practical advantage of reusing the VertexOutput struct over a separate FragInput struct?