---
title: "Recipe: a GPU module (a canvas drawn in Rust)"
description: "A canvas that the app's GPU module draws in Rust: a gpu/ crate beside app.json."
modificationDate: "2026-11-30"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: a GPU module (a canvas drawn in Rust)

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/gpu-module: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

For WGSL alone, see [fragment shaders](https://exact.expo.dev/recipes/gpu-shader.md). See also [device requirements](https://exact.expo.dev/reference/developer/rust-data-sources.md#gpu-device-requirements).

A `canvas surface=name(…)` that no data module draws uses `gpu/` beside `app.json`,
loaded after first pixel. Implement `exact_gpu::Surface`; see `apps/caltrain/gpu`.
`exact_gpu::wgpu` is wgpu 30; `gpu/README.md` covers its use.

Name the crate after the app plus `-gpu`. Use the workspace versions for all
three web dependencies that `module!` exports through.

```prompt Draw a canvas in Rust on the GPU
Give the Exact app in this folder a GPU module: a canvas that Rust code draws with wgpu. Use https://github.com/expo/exact/blob/main/docs/recipes/gpu-module.md as the source for each step. The Exact checkout has the same page at docs/recipes/gpu-module.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. Find the Exact checkout (the folder with scripts/exact.mjs); if you cannot find it, ask me where it is.
2. If I did not say what the canvas draws and which screen holds it, ask me before you write code. If one WGSL fragment shader can draw it, with no texture channels and no multiple passes, use the gpu-shader recipe instead and write no Rust.
3. Create `gpu/Cargo.toml` beside app.json, as the recipe shows. Name the crate after the app plus `-gpu`. Use the workspace versions for `exact-gpu` and for all three web dependencies. Add no root `Cargo.toml`: Exact generates the workspace at `.exact/gpu/`.
4. In `gpu/src/lib.rs`, implement `exact_gpu::Surface` for each surface, name each in `REGISTRY`, and end with `exact_gpu::module!(REGISTRY);`. `exact_gpu::wgpu` is wgpu 30: read `gpu/README.md` in the Exact checkout for its use, and `apps/caltrain/gpu` there for a whole module.
5. In app.contract, place the canvas by its surface name with a size and a `testId`, as the recipe's `canvas surface=clear() width=200 height=120 testId="sky"` does. Add a test in app.test.contract that expects the canvas in the tree.
6. For raw events, return `true` from `Surface::wants_input` and read them in `Surface::input`. On macOS, Contract's `pointerdown` runs before the surface receives the press: update a camera in one handler, so each gesture moves it once. For files, native surfaces get paths through `Surface::storage`; on the web, use browser storage APIs.
7. Run `bun exact.mjs web-build` or `bun exact.mjs mac` to build the module. If a dependency needs another version or crate, run `bun exact.mjs lock`. It writes `Cargo.gpu.lock` beside app.json: tell me to commit it.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me each surface and what it draws, what the builds, tests and checks reported, whether I must commit a new `Cargo.gpu.lock`, and the launch command for each target platform.
```

1. Create `gpu/Cargo.toml` as below. Exact generates its workspace at
   `.exact/gpu/` (LLP 1153.003 D5.1). The app needs no root `Cargo.toml`.
2. Add the canvas and run `bun exact.mjs web-build` or `bun exact.mjs mac`.
3. The workspace uses Exact's lockfile. If a dependency needs another version
   or crate, run `bun exact.mjs lock` and commit the resulting `Cargo.gpu.lock` beside `app.json`.

```toml
[package]
name = "field-log-gpu"
version.workspace = true
edition.workspace = true
license.workspace = true
publish = false
workspace = "../.exact/gpu"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
exact-gpu.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
web-sys.workspace = true
```

`gpu/src/lib.rs`, one surface that clears its canvas:

```rust
use exact_gpu::{wgpu, Frame, Registry, Surface, SurfaceError, Value};

struct Clear;

impl Surface for Clear {
    fn bind(&mut self, _inputs: &[Value], _at_ms: Option<f64>) -> Result<(), SurfaceError> {
        Ok(())
    }
    fn render(&mut self, _frame: &Frame, _device: &wgpu::Device, _queue: &wgpu::Queue,
        encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, _format: wgpu::TextureFormat) -> bool {
        encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: target, depth_slice: None, resolve_target: None,
                ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::BLUE), store: wgpu::StoreOp::Store },
            })],
            ..Default::default()
        });
        false
    }
}

fn clear() -> Box<dyn Surface> {
    Box::new(Clear)
}

pub static REGISTRY: Registry = Registry { surfaces: &[("clear", 0, clear)], shaders: &[] };

exact_gpu::module!(REGISTRY);
```

```text
canvas surface=clear() width=200 height=120 testId="sky"
```

Register the positional limit. It must match any fields declared by
`Surface::arguments()`. A short positional call fills trailing fields with
their defaults.

Return `true` from `Surface::wants_input` for raw events in `Surface::input`.
On macOS, Contract's `pointerdown` runs before the surface receives the press.
`pointermove`, `pointerup`, `wheel` and `contextmenu` handlers also run.
Update a camera in one handler so each gesture moves it once.

Native surfaces get file paths through [`Surface::storage`](https://exact.expo.dev/reference/developer/rust-data-sources.md#gpu-storage).
On the web, use browser storage APIs.
