Shader stages
On this page

Shader stages: explicit GPU entry points

TypeShade entry points are top-level exported functions, each with a stage attribute. The attribute names the shader stage and any stage-specific metadata.

1. Start from TypeScript functions and modules

In TypeScript, export function makes a function part of a module’s public surface. In TypeShade, a stage attribute before it, @fragment export function fs(…) { … }, gives that function the GPU meaning of a pipeline entry point. A function with no stage attribute remains a reusable helper.

const WG_SIZE: override<u32> = 64;
function addBias(value: f32): f32 {
return value + 0.1;
}
@compute([WG_SIZE])
export function paint(gid: builtin<"global_invocation_id">) {
pixels[gid.x] = addBias(camera.pos.x);
}

2. Compute

A compute entry can name an override<u32> in its compute dimensions. Numeric dimensions remain valid. Builtins such as global_invocation_id are explicit parameters.

Choose a size in ordinary TypeScript, then pass it as a named constant to program.compute(). Read pipeline.workgroupShape to calculate dispatch counts. An imported entry accepts entry(bindings, groups, { constants: { WG_SIZE: selected } }), including inside device.fuse(). Calculate groups for the selected size and guard the last partial group in the shader. WebGL2 requires the default workgroup shape. The host-compute example has the complete host and shader.

const selected = input.length >= 256 ? 256 : 64;
const pipeline = await program.compute('paint', { constants: { WG_SIZE: selected } });
const [workgroupSize] = pipeline.workgroupShape;
pipeline.dispatch(encoder, bindings, [Math.ceil(input.length / workgroupSize), 1, 1]);

3. Vertex and fragment

@vertex and @fragment describe graphics-pipeline entry points. Inputs, outputs and builtins are expressed explicitly in the function signature. The shader pipeline says what each stage is handed and what it returns.

@vertex
export function vs(
vid: builtin<"vertex_index">,
vin: VsIn
): vec4 {
return camera.view * vec4(vin.position, 1)
}
@fragment
export function fs(): vec4 {
return vec4(1, 0, 0, 1)
}

4. Helper functions

An undecorated function is a helper, not an entry point. Do not put entry points on classes or treat this as a pipeline object.

function saturate(x: f32): f32 {
return clamp(x, 0.0, 1.0)
}

Next: String keys and Record

Edit this page Report a problem