CPU and GPU
On this page

CPU and GPU

The CPU runs host code that prepares data and submits GPU commands. Each GPU invocation executes a shader entry point with local values and access to declared resources.

When it helps

Independent element updates are a useful starting point. Give each invocation an index and an output location that it owns.

Worked example

Round the element count up to whole workgroups. The last workgroup includes extra invocations, so guard array access with the actual element count.

Example reasoning
length = 130
workgroupSize = 64
workgroups = ceil(length / workgroupSize) = 3
invocations = 3 * 64 = 192
0 <= i && i < length

Practical limits

Invocation order is not a coordination mechanism. Shared writes need a race-free algorithm, suitable atomics, or the synchronization allowed by the API. A workgroup barrier does not join separate workgroups.

CPU code prepares work and resources for the GPU. Shader languages define how invocations use those resources. Their restrictions come from language rules, API contracts, and compiler choices as well as hardware.

Invocations

Each invocation processes a vertex, fragment, or compute work item. Invocations can communicate through shared resources under the language memory and synchronization rules. Their execution order is not implied.

@compute([64, 1, 1])
export function paint(gid: builtin<"global_invocation_id">) {
pixels[gid.x] = camera.pos.x
}

What follows: a stage attribute says which of the three is calling, and a builtin<N> parameter is how an invocation learns its own place in the work. Shader stages states the stage attributes and Functions states the parameters.

Memory

A shader uses typed local values and resources bound by the host. WGSL and GLSL ES do not expose JavaScript's general object heap. TypeShade classes describe GPU structs whose bytes the host supplies.

class Camera {
view: mat4
pos: vec3
}
declare const camera: uniform<Camera>

What follows: new builds a value and not an object with identity, the order of a class's fields is the layout of the bytes the host writes, and resources use explicit bindings, including declare declarations. Types states the struct surface and Resources states the declarations.

Calls

WGSL and GLSL ES disallow recursive call graphs. A compiler may inline helpers or emit supported calls. Hardware stack details do not define this language restriction.

What follows: recursion is rejected, and a helper is an ordinary function the compiler can follow to its leaves. Functions states what a call may be.

Loops

Invocations can follow different paths and iteration counts. Divergence may affect throughput. A loop does not synchronize an entire stage; communication needs the synchronization rules of its execution and memory scope.

for (let i = 0; i < 4; i++) {
pixels[i] = pixels[i] + camera.pos.x
}

What follows: a for and a while end where their condition, a break or a return says, and the compiler refuses a loop it proves never ends. Control flow states which conditions and loops compile.

Value types

Shader variables have statically defined value types and representations. The compiler decides whether a value occupies registers or memory. A source type does not specify the hardware register allocation.

let value: f32 = 1
let index: u32 = 0
let direction: vec3 = vec3(0, 1, 0)

What follows: each shader value has a statically known GPU type such as f32, u32 or vec4. Types states the type surface and GPU types states the values.

Execution and language rules

These execution properties and language restrictions inform the TypeShade rules linked below.

Execution or language constraintWhat TypeShade asks forStated in
Calls an entry point once per vertex, fragment or work itemA stage attribute on the entry point and a parameter for every builtin inputShader stages
Uses typed values and bound resources without a JavaScript object heapNo growing array or string; new builds a value, structs describe layout, and resources use explicit bindingsResources
Disallows recursive call graphs in WGSL and GLSL ESA nonrecursive call graphFunctions
Permits different invocation paths and iteration countsA loop bound the compiler can readControl flow
Defines static value types independently of register allocationA statically known value type per variableGPU types

Further reading

  • WGSL specification writes down the execution model these facts come from, including what an invocation is and what it may hold.
  • MDN WebGPU API is the browser side of the same model, written for a JavaScript developer.

Next in this path: The shader pipeline, which says what each stage is handed and what it produces.

Edit this page Report a problem