---
title: "Recipe: a fragment shader in WGSL"
description: "In app.contract, import the package and give the canvas the shader's asset path."
modificationDate: "2026-11-22"
---

> ## 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 fragment shader in WGSL

<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-shader: <what was wrong or missing>"`

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

</AgentInstructions>

Run `bun exact.mjs add @exact/shader`. In `app.contract`, import the package and give the
canvas the shader's asset path. A surface reports its first draw and any error through
`exactSurface("shader")`. If the browser rejects a shader while you run `bun exact.mjs web`,
the development page shows the file and the error. Click the message to dismiss it.
With a literal `src`, the build also checks that any WGSL `override` used by the fragment
has a default value.

```prompt Draw a WGSL shader in my app
Add a canvas that a WGSL fragment shader draws to the Exact app in this folder. Use https://github.com/expo/exact/blob/main/docs/recipes/gpu-shader.md as the source for each step. The Exact checkout has the same page at docs/recipes/gpu-shader.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say what the shader draws (or give a Shadertoy shader to port) and which screen holds it, ask me before you write code. If it needs texture channels or multiple passes, stop and tell me: the gpu-module recipe's Rust surface does those.
3. Install the package: `bun exact.mjs add @exact/shader`. Write the shader as a file in `assets/`, with one fragment entry and the recipe's `Uniforms` struct.
4. To port a Shadertoy `mainImage`, follow the recipe's example. `u.resolution` is `iResolution.xy` in device pixels, and `u.time` is `iTime` in seconds. WGSL's pixel origin is the top left: subtract `pixel.y` from the height to get Shadertoy's `fragCoord`. Return the colour that the shader assigns to `fragColor`.
5. In app.contract, import the package and give the canvas the shader's asset path, as the recipe's `canvas surface=shader(src="assets/gradient.wgsl") width=240 height=160 aria-hidden=true` does. To add a parameter, declare a member in `Uniforms` and supply it beside `src` (`speed=0.5`). Give every WGSL `override` that the fragment uses a default value.
6. Read `exactSurface("shader")` as a resource, and show its `error` with `role="alert"` when it is not empty. A shader that reads time animates, and Reduce Motion holds it still. To cap its frame rate, write `maxFps=30`.
7. The canvas is opaque, and the alpha that the shader returns is ignored. Only for a soft or irregular edge over the page, write `alphaMode="premultiplied"` beside `src` and return colour already multiplied by alpha, with no channel above `a`. On Android, keep a full-window shader opaque if it can be.
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 the browser rejects the shader while `bun exact.mjs web` runs, the development page shows the file and the error. 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 the shader's file and its parameters, what the tests and checks reported, and the launch command for each target platform.
```

```text
use ShaderState from "@exact/shader"

component App
  resource fx = exactSurface("shader") as shape ShaderState
  view
    column
      canvas surface=shader(src="assets/gradient.wgsl") width=240 height=160 aria-hidden=true
      if fx.error != ""
        text fx.error role="alert"
```

Write `assets/gradient.wgsl` with one fragment entry. This ports a Shadertoy
`mainImage(out vec4 fragColor, in vec2 fragCoord)` that colours pixels by position and time.

```wgsl
struct Uniforms {
    time: f32,
    resolution: vec2f,
}
@group(0) @binding(0) var<uniform> u: Uniforms;

@fragment
fn main(@builtin(position) pixel: vec4f) -> @location(0) vec4f {
    let fragCoord = vec2f(pixel.x, u.resolution.y - pixel.y);
    let uv = fragCoord / u.resolution;
    return vec4f(uv, 0.5 + 0.5 * cos(u.time), 1.0);
}
```

`u.resolution` is Shadertoy's `iResolution.xy` in device pixels. WGSL's pixel origin is the
top left, so subtracting `pixel.y` from the height gives Shadertoy's bottom-left `fragCoord`.
`u.time` is `iTime` in seconds. Return the colour you would assign to `fragColor`.
To add a parameter, declare a member such as `speed: f32` in `Uniforms`, then supply
`speed=0.5` beside `src`. A shader that reads time animates. `maxFps=30` caps its frame
rate, and Reduce Motion holds it still. For texture channels or multiple passes, use the
[Rust GPU surface](https://exact.expo.dev/recipes/gpu-module.md).

A shader's canvas is opaque: the alpha it returns is ignored. For a soft or irregular edge
over the page, write `alphaMode="premultiplied"` beside `src` (WebGPU's
[canvas alpha mode](https://www.w3.org/TR/webgpu/#enumdef-gpucanvasalphamode)) and return
colour already multiplied by alpha, as in `vec4f(color * a, a)`, with no channel above `a`.
The canvas then starts each frame transparent, and its `background-color` and the page show
where alpha is below one. Every host that draws shaders composes it. On Android a
premultiplied canvas is drawn into the window, so a full-window shader that can be opaque
should stay opaque.
