Compute pipelines are great for, well, computations, but they are not the best choice for drawing shapes.
The other type of pipelines supported by WebGPU are render pipelines.
Render pipelines are used for drawing points, lines and triangles onto textures.
Whereas compute pipelines execute threads on a grid you define, render pipelines work in stages.
You configure the pipeline to draw a kind of shape (points, lines, or triangles), and during the draw call:
The vertex function runs once per vertex and outputs a screen position for it.
The GPU calculates which pixels each shape covers in a process called rasterization.
The fragment function runs once per covered pixel and outputs a color.
The following example draws three red points.
Each point lands on a single pixel, since 'point-list' topology rasterizes each vertex to one pixel regardless of canvas size.
The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of elements. The HTMLCanvasElement interface also inherits the properties and methods of the HTMLElement interface.
It is a little more complicated than creating and dispatching a compute pipeline.
Let us go through the code step by step.
Initialize the root, just like we did in a previous guide.
import { tgpu, d } from'typegpu';
const root = await tgpu.init();
Create the render pipeline with root.createRenderPipeline, setting its topology to 'point-list'.
This will make the pipeline draw a point for each position returned from the vertex function.
const pipeline = root.createRenderPipeline({
primitive: { topology: 'point-list' },
// ...
});
Define a vertex function, which tells the GPU where each vertex is located.
When dispatching the pipeline later, we’ll specify how many vertices to draw.
The GPU runs this function once per vertex, with $vertexIndex going from 0 up to count - 1.
The returned position is a 4-dimensional vector in clip space: X goes from -1 on the left to 1 on the right, and Y goes from -1 on the bottom to 1 on the top.
Z is used for depth testing (irrelevant here),
and W can stay 1 for now, see homogeneous coordinates if you’re curious.
Define a fragment function, which the GPU calls for each pixel covered by a shape.
For 'point-list' topology, that’s exactly once per returned vertex.
This particular fragment function always returns red.
fragment: ()=> {
'use gpu';
return d.vec4f(1, 0, 0, 1);
},
Query the canvas. This example assumes your page has a <canvas> element.
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
Call root.configureContext. This will create and configure a context for the provided canvas.
This context can be then used to render onto the canvas.
Dispatch the pipeline by providing a color attachment and calling .draw(count), where count is the number of vertices to draw.
The color attachment specifies the draw target, plus optional props like clearValue and loadOp.
In the example above, the $position is the only value returned from the vertex function.
We can actually return more values.
In the example below, we pass in an additional color prop.
Let’s change the topology from 'point-list' to 'line-strip'.
The vertex and fragment functions stay exactly the same.
This changes how the GPU connects the vertices together.
primitive: { topology: 'point-list' },
primitive: { topology: 'line-strip' },
The fragment now runs for every pixel along the line between the first and second vertices, and again between the second and third.
Notice that the line’s color transitions smoothly from red to green to blue.
Each fragment receives values that are interpolated across the shape based on its position.
If the first vertex returns color d.vec3f(1, 0, 0) and the second returns color d.vec3f(0, 1, 0), then a pixel exactly halfway between them sees d.vec3f(0.5, 0.5, 0).
This applies to every extra prop returned by vertex, and this is a core principle of render pipelines.
Let’s change topology once more, this time to 'triangle-list' - the most widely used option.
primitive: { topology: 'line-strip' },
primitive: { topology: 'triangle-list' },
Here’s how it looks on a high-resolution canvas.
A triangle is drawn for each 3 vertices passed, and the fragment runs for every pixel covered by the triangle they define.
To draw more triangles, return more positions from the vertex function and pass a matching count to draw().
Two triangles require 6 vertices, three triangles require 9 and so on.
In all examples above, we draw once and stop.
To animate, two things change: we call pipeline.draw() every frame, and we pass a value from JS into the shader that each frame can vary.
const
const timeUniform:TgpuUniform<d.F32>
timeUniform =
const root:TgpuRoot
root.
TgpuRoot.createUniform<d.F32>(typeSchema: d.F32, initial?: number | ((buffer:TgpuBuffer<NoInfer<d.F32>>)=>void) |undefined): TgpuUniform<d.F32> (+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Read-only on the GPU, optimized for small data. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
@param ― typeSchema The type of data that this buffer will hold.
@param ― initial Either initial value of the buffer, or an initializer to execute on the mapped buffer. (optional)
createUniform(
import d
d.
const f32: d.F32
export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
requestAnimationFrame calls frame once per repaint and passes a timestamp in milliseconds.
We divide by 1000 to get seconds and write that into timeUniform, which the vertex function reads as timeUniform.$.
Before, we drew points and lines in a very low resolution.
On high resolution canvases, they are barely visible.
Unfortunately, there is no way to change the thickness of the drawn points and lines.
To draw bigger points and lines, people usually use many triangles to approximate the shape.
There is however a different approach, that skips the geometry altogether.
The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of elements. The HTMLCanvasElement interface also inherits the properties and methods of the HTMLElement interface.
The fullScreenTriangle vertex function (from typegpu/common) returns positions that lie outside clip space, so a single triangle ends up covering the entire visible canvas.
Alongside $position it outputs uv, a 2D coordinate that runs from (0, 0) to (1, 1) across the canvas.
The fragment function receives the interpolated uv in each pixel.
Each pixel then colors itself based on the distance from its uv to the center (0.5, 0.5): closer than 0.2 is red, otherwise black.
You now know how compute shaders, vertex shaders and fragment shaders fit together.
From here you can keep building everything yourself, or reach for TypeGPU Kit, which builds on these same pieces.
It has ready-made modules for cameras, lighting, post-processing and particles that record into your own passes.
To start a project with it, run npm create @typegpu/kit.