# Final pass

> `@typegpu/kit` 0.1.0. If it is installed, read `node_modules/@typegpu/kit/docs/` instead.

The last pass of a frame, mapping linear light to the screen through a tone curve, bloom and your own grade.

> `import { finish } from '@typegpu/kit'`: `finish.create(root, { format, source, region?, glow?, exposure?, … }) → Finish`. Creates the final pass of a frame. **records** `draw` render, `record` encoder: `record` into a pass of its own on the view you give, usually the canvas's, or `draw` into the caller's pass, after the bloom's `record`. **owns** 2 buffers, a sampler, a pipeline, 2 bind groups: the settings, the regions it reads, a group for the glow and one for each texture the source returns. **allocates** on resize: a bind group for a new source or glow. **lifecycle** `initAsync()`. **needs** `exposure`: as `options.exposure`; fills accessors in its pipelines. **GPU functions** `glow`, `picture`. **schemas** `Settings`. **constants** `defaults`. **types** `Filter`, `Viewport`. `src/post/finish.ts`; afterimage, benthos, blockfall, censer, classroom, cloister, dominoes, ember-mirror, falling-sand, fireworks, fishtank, forest, hex-isle, jellies, jelly-world, kinetic, lattice, night-market, ocean, platformer, quiver-trees, raymarch, robot-arm, skeletons, slime-mould, solar-system, sph-sculpt, spider, stampede, star-map, survey-model.
>
> `Options`, taken by `create`:
> - `format: GPUTextureFormat` Sets the output format, such as the canvas format.
> - `source: Source` Provides the picture at any size: a rendered picture, such as `aa` or `blur`, or a function returning a texture, such as `() => screen.textures.hdr`.
> - `region?: SizeSource` Selects the source region from its top-left corner, such as `regions.hdr` when drawing below full resolution. Default the region of a rendered `source`, else the whole texture.
> - `glow?: Rendered` Adds a bloom, or a glow of your own such as an emission target, before tone mapping. Default none.
> - `exposure?: Pick<Exposure, 'inject'>` Multiplies `settings.exposure` by an exposure adapted to the picture on the GPU, such as one from `exposure.create` recorded before this pass. Default none.
> - `tone?: Tone | null` A `'use gpu'` function. Maps linear source colours to display range with `aces`, `neutral`, `reinhard` or a custom curve. Default `neutral`.
> - `filter?: Filter` Selects `linear` filtering or `sharp` filtering for fractionally scaled pixel art. Default `linear`.
> - `encode?: boolean` Applies the sRGB transfer function in the shader. Default true unless `format` ends in `-srgb`.
> - `grade?: (color: d.v3f, uv: d.v2f) => d.v3f` A `'use gpu'` function. Grades or tints the colour after tone mapping and before the vignette, encoding and dither. Default none.
> - `alpha?: 'opaque' | 'premultiplied'` Selects output alpha handling. Default `opaque`, which writes alpha 1.
> - `settings?: Partial<d.InferInput<typeof Settings>>` Sets initial values over `defaults`. Default `defaults`, with exposure 1, vignette 0.35, dither 1 and glowIntensity 0.12.
>
> `RecordOptions`, taken by `Finish.record`:
> - `viewport?: Viewport` Draws into this rectangle of the view, such as a letterbox. Default the whole view.
> - `timestampWrites?: TimestampWrites | undefined` Supplies a timer's writes for the passes. Default none.
>
> `Settings`, a schema:
> - `exposure: d.f32` Multiplies the source before tone mapping.
> - `vignette: d.f32` Sets corner darkening from 0 for none to 1 for full darkening.
> - `dither: d.f32` Sets dither amplitude in 8-bit steps of the stored output.
> - `glowIntensity: d.f32` Scales the glow from `options.glow` added to the source.
>
> `Finish`, from `create`:
> - `settings: TgpuUniform<typeof Settings>` Holds `defaults` with overrides from `settings`.
> - `initAsync()` Compiles the pipeline.
> - `draw(pass)` Draws into the caller's pass, which usually targets the canvas.
> - `record(target, view, options?)` Opens a pass on `view`, such as `surface.view(frame)`, draws into it and ends it.

Linear light can exceed 1. Blockfall's cleared blocks flash brighter than white. Clipping to the screen range of 0 to 1 turns bright colours yellow, bands dark gradients and loses glow. Use `finish.create` for a **final pass** that maps the image to the screen with one full-screen triangle. Add [bloom](#bloom), [FXAA](fxaa.md), [TAA](taa.md) or [depth of field](dof.md) around it.

```ts
import { bloom, finish } from '@typegpu/kit';

const screen = targets.create(root, surface, { hdr: { format: 'rgba16float', sampled: true } });
const source = () => screen.textures.hdr;

const glow = bloom.create(root, { source });
const final = finish.create(root, { format: surface.format, source, glow });

frames.animate(
  frames.create(root, (frame) => {
    screen.fit();

    const scene = frame.encoder.beginRenderPass({ colorAttachments: { view: screen.views.hdr } });
    // the scene, in linear colour
    scene.end();

    glow.record(frame);
    final.record(frame, surface.view(frame));
  }),
);
```

- **Drawing** Use `final.draw(pass)` for a pass you opened, such as one a HUD draws into afterwards. `record(frame, view, { timestampWrites })` times the pass under one [profiler](profiler.md) timer.
- **Viewport** `record(frame, view, { viewport })` draws the picture into a rectangle of the view, such as a letterbox, and clears the rest to black, or to transparent with `alpha: 'premultiplied'`.
- **Source** The source is a function returning a single-sampled float [target](targets.md) with `sampled: true`, or a picture another module rendered, such as `aa`, `blur` or `glow`. Other sources fail to type-check. A 32-bit float format requires `float32-filterable`.
- **Resizing** Each `draw` or `record` reads the source's current texture and region, so targets can resize through `fit()`. The final pass creates a bind group the first time it draws each source texture and reuses it on later draws.
- **Region** A rendered picture can carry its own `region`. Give `region` yourself only with a function source drawn below full [resolution](targets.md#resolution-scale), such as `screen.regions.hdr`.
- **Glow** The final pass adds glow to its own `source`. Bloom can therefore read a separate image, as in [What glows](#what-glows). Any object with `output`, and optionally `region`, can supply glow.
- **Stage order** Each pixel applies these stages in order: glow, exposure, `tone`, `grade`, vignette, sRGB encoding and dither.
- **Skipping stages** Uniform values of zero do not remove shader work. Vignette, dither and `glowIntensity` still run their calculations, and glow still samples. Omit stages with no `glow`, `tone: null`, no `grade` or `encode: false`. Dither always runs.
- **Picked colours** Convert screen-picked sRGB colours once with `srgbToLinear` from `@typegpu/color`. It runs on the CPU too.

[Falling sand](../examples/falling-sand.md) renders to a small target and upscales it with `filter: 'sharp'`.

## Tone curves

> `import { tone } from '@typegpu/kit'`: These `'use gpu'` functions apply tone curves and dither. **kind** shader. **GPU functions** `luminance`, `aces`, `neutral`, `reinhard`, `agx`, `dither`. **types** `Tone`. `src/post/tone.ts`; afterimage, benthos, censer, classroom, cloister, dominoes, ember-mirror, falling-sand, fireworks, jellies, lattice, night-market, ocean, raymarch, solar-system, sph-sculpt, stampede, star-map, survey-model.

| `tone`                   |                                                      |
| ------------------------ | ---------------------------------------------------- |
| `tone.neutral` (default) | Khronos PBR Neutral. Keeps hue until the highlights. |
| `tone.aces`              | Narkowicz's fit of ACES. Bright colours go white.    |
| `tone.reinhard`          | On luminance. Soft, and never reaches white.         |
| `tone.agx`               | Blender's AgX. Bright colours fade to white evenly.  |
| `null`                   | No curve, for sources already in display range.      |

`tone.neutral` and `tone.aces` are `neutral` and `aces` from `@typegpu/color`, and any other curve from it, such as `hable`, fits the `tone` option too.

## Bloom

> `import { bloom } from '@typegpu/kit'`: `bloom.create(root, { source, region?, … }) → Bloom`. Creates bloom from a chain of half-size textures. **records** `record` encoder: `record` after whatever wrote the source; then `output` holds the glow. **owns** targets, a sampler, 3 pipelines, 2 bind groups and a buffer per level: the chain of glow levels, the settings, a group for each pass, and the region each pass reads. **allocates** on resize: a new chain and the bind groups of its passes; so does a new source. **lifecycle** `initAsync()`, `destroy()`, `[Symbol.dispose]`. **GPU functions** `threshold`. **schemas** `Settings`. **constants** `defaults`. `src/post/bloom.ts`; afterimage, benthos, blockfall, classroom, cloister, dominoes, ember-mirror, falling-sand, fireworks, fishtank, forest, hex-isle, jellies, kinetic, lattice, night-market, ocean, quiver-trees, robot-arm, skeletons, slime-mould, solar-system, spider, stampede, star-map, survey-model.
>
> `Options`, taken by `create`:
> - `source: Source` Provides the HDR picture: a rendered picture, such as `aa`, or a function returning a texture, such as `() => screen.textures.hdr`.
> - `region?: SizeSource` Selects the source region from its top-left corner, such as `regions.hdr` when drawing below full resolution. Default the region of a rendered `source`, else the whole texture.
> - `levels?: number` Sets the number of levels, each half the size of the preceding level. Default 6.
> - `bright?: (color: d.v3f) => d.v3f` A `'use gpu'` function. Selects the linear source colour that contributes glow. Default `threshold`.
> - `settings?: Partial<d.InferInput<typeof Settings>>` Sets initial values over `defaults`. Default `defaults`, with threshold 1, knee 0.5 and spread 1.
>
> `Settings`, a schema:
> - `threshold: d.f32` Sets the brightness threshold for glow.
> - `knee: d.f32` Sets how far below the threshold glow begins, as a fraction of the threshold.
> - `spread: d.f32` Sets the distance between upsample taps, in texels of the smaller level.
>
> `Bloom`, from `create`:
> - `settings: TgpuUniform<typeof Settings>` Holds `defaults` with overrides from `settings`.
> - `output: FloatTexture` Provides the glow at half the source size in mip 0 of an `rgba16float` texture.
> - `region: SizeSource` Gives the glow region from the top-left corner of `output`, set by the last `record` call.
> - `initAsync()` Compiles the bloom pipelines.
> - `record(target, options?)` Records bloom passes after the source is written.
> - `destroy()` Destroys the glow texture.

A wide full-resolution blur needs hundreds of samples per pixel. Bloom reduces that work by blurring progressively smaller levels.

- **Downsampling** On the way down, each level halves the dimensions and samples 13 taps. Jimenez's filter combines five overlapping four-tap boxes, weighted one half for the centre and one eighth per corner.
- **First level** The first downsample keeps bright values through `bright`. It uses a **Karis average**, weighting boxes by `1 / (1 + luminance)`, to prevent flicker from isolated bright pixels.
- **Threshold** The default `bright` is `bloom.threshold`. It keeps the excess of the brightest channel above `threshold`. A quadratic softens the transition starting `knee × threshold` below it.
- **Upsampling** On the way up, each level samples the smaller level through a 3 × 3 tent, with weights 1, 2 and 4 divided by 16. Taps are `spread` texels apart. Add the filtered result to the next larger level.
- **Intensity** The final pass multiplies the glow by `glowIntensity` before exposure and tone mapping.
- **Region** A `region` restricts bloom to the source area drawn at [lower resolution](targets.md#resolution-scale). Each level reads and draws its corresponding region. The final pass reads `glow.region` through `glow`.
- **Timing** `record(frame, { timestampWrites })` times all its passes under one [profiler](profiler.md) timer.
- **Levels** The default 6 levels require 11 render passes, one downsample per level and one upsample for all but the smallest. Level 0 does about three quarters of the pixel work with 13 taps down and 9 up. Each following level has one quarter as many pixels. Removing a level saves little GPU work but removes two CPU-recorded passes, each requiring begin, viewport, draw and end calls.

### What glows

By default, everything above the brightness threshold glows, including white sky. To bloom only emitters, draw them into a separate target and bloom that target. Keep the full scene as the final pass's source. Use `bright` to select or modify emission:

```ts
import { bloom, finish } from '@typegpu/kit';

const screen = targets.create(root, surface, {
  hdr: { format: 'rgba16float', sampled: true },
  emission: { format: 'rgba16float', sampled: true },
});

// Bloom the full emission target with a warmer tint
const warm = (color: d.v3f) => {
  'use gpu';
  return color * d.vec3f(1, 0.7, 0.4);
};
const glow = bloom.create(root, { source: () => screen.textures.emission, bright: warm });
const final = finish.create(root, {
  format: surface.format,
  source: () => screen.textures.hdr,
  glow,
});
```

- Call `bloom.threshold(color)` within `bright` to tint values above the threshold or weight them by `tone.luminance`. It reads bloom settings and is valid only inside `bright`.
- A `bright` that reads another module [carries its fill](../concepts/connecting-modules.md), as `bright: tgpu.fn(masked).pipe(mask.inject())` does.

## Adapting exposure

> `import { exposure } from '@typegpu/kit'`: `exposure.create(root, { source, region?, … }) → Exposure`. Creates an exposure that adapts to the picture on the GPU. **records** `record` encoder: `record` after whatever wrote the source, before the final pass that reads it. **owns** 3 buffers, a sampler, a pipeline and a bind group: the settings, the frame's region and time, the exposure, and a group for the source. **allocates** on resize: a bind group for a new source. **lifecycle** `initAsync()`, `destroy()`, `[Symbol.dispose]`. **injects** member: `exposure.current`, for the final pass or shaders of your own. **accessors** `current`. **schemas** `Settings`. **constants** `defaults`. `src/post/exposure.ts`; fireworks, kinetic, ocean.
>
> `Options`, taken by `create`:
> - `source: Source` Provides the linear picture to measure, usually the final pass's source: a rendered picture, such as `aa`, or a function returning a texture.
> - `region?: SizeSource` Selects the source region from its top-left corner, such as `regions.hdr` when drawing below full resolution. Default the region of a rendered `source`, else the whole texture.
> - `settings?: Partial<d.InferInput<typeof Settings>>` Sets initial values over `defaults`. Default `defaults`, with key 0.18, lowest 1/16, highest 16, rise 1 and fall 3.
>
> `RecordOptions`, taken by `Exposure.record`:
> - `dt?: number` Sets the seconds of adaptation. Default the frame's `dt`, or the target at once.
> - `timestampWrites?: TimestampWrites | undefined` Supplies a timer's writes for the passes. Default none.
>
> `Settings`, a schema:
> - `key: d.f32` Sets the average luminance the exposed picture aims for, as a geometric mean over the picture.
> - `lowest: d.f32` Sets the lowest exposure, for the brightest pictures.
> - `highest: d.f32` Sets the highest exposure, for the darkest pictures.
> - `rise: d.f32` Sets how fast exposure rises after the picture darkens, per second.
> - `fall: d.f32` Sets how fast exposure falls after the picture brightens, per second, as `rise` does.
>
> `Exposure`, from `create`:
> - `settings: TgpuUniform<typeof Settings>` Holds `defaults` with overrides from `settings`.
> - `inject()` Fills `exposure.current` with the adapted exposure.
> - `initAsync()` Compiles the measuring pipeline.
> - `record(target, options?)` Measures the source and moves the exposure towards its target by `dt` seconds of adaptation, the frame's `dt` by default.
> - `reset()` Takes the target at once at the next `record`, for example after a camera cut.
> - `destroy()` Destroys the exposure buffer.

`exposure.create` measures the picture each frame and eases the exposure towards a target on the GPU, as an eye adapts to light and dark:

```ts
import { exposure, finish } from '@typegpu/kit';

const source = () => screen.textures.hdr;
const eye = exposure.create(root, { source, settings: { highest: 8 } });
const final = finish.create(root, { format: surface.format, source, exposure: eye });

frames.animate(
  frames.create(root, (frame) => {
    screen.fit();
    // the scene, into screen.views.hdr
    eye.record(frame);
    final.record(frame, surface.view(frame));
  }),
);
```

- **Metering** `record(frame)` averages the log luminance of 4096 samples over the source and moves the exposure towards `key` divided by that average, within `lowest` and `highest`. The exposure stays on the GPU, so the final pass of the same frame reads it.
- **Speed** Once the meter has a reading, exposure eases towards its target over the frame's `dt`, as [`clock.ease`](clock.md#easing-towards-a-goal) does. It uses `rise` as exposure rises and `fall` as it falls. Each second closes `1 - e^-rise` or `1 - e^-fall` of the remaining gap in stops. By default it falls three times as fast as it rises, so a flash dims the picture quickly and a dark scene brightens slowly. The first `record` takes the target at once.
- **With an encoder** With an encoder in place of the frame, give the seconds yourself, `eye.record(encoder, { dt })`. Without `dt`, it takes the target at once.
- **Trim** `final.settings.exposure` multiplies the adapted exposure. Use it to brighten or darken the result.
- **Second view** A second view, such as a picture-in-picture camera, takes its own `exposure.create` and final pass.
- **In shaders** Shaders of your own read the value through `exposure.current` with `root.pipe(eye.inject())`.
- **Settings** `eye.settings` patches like the other settings. Its fields don't overlap the final pass's or the bloom's.

| Field     | Default |                                                                  |
| --------- | ------- | ---------------------------------------------------------------- |
| `key`     | `0.18`  | Average luminance the exposed picture aims for, mid grey.        |
| `lowest`  | `1/16`  | Lowest exposure, for the brightest pictures.                     |
| `highest` | `16`    | Highest exposure, for the darkest pictures.                      |
| `rise`    | `1`     | How fast exposure rises after the picture darkens, per second.   |
| `fall`    | `3`     | How fast exposure falls after the picture brightens, per second. |

## Settings

`final.settings` and `glow.settings` are uniforms. Their initial values combine `defaults` with the `settings` option. Use TypeGPU's `patch` when settings change. It uploads supplied fields and skips fields absent from that uniform. Since the fields do not overlap, one object can patch both:

```ts
import { bloom, finish } from '@typegpu/kit';

const source = () => screen.textures.hdr;
const glow = bloom.create(root, { source, settings: { threshold: 0.6 } });
const final = finish.create(root, {
  format: surface.format,
  source,
  settings: { exposure: 1.4 },
  glow,
});

// When settings change
const look = { exposure: 1.1, threshold: 0.8 };
final.settings.patch(look);
glow.settings.patch(look);
```

| Field           | Default |                                                                            |
| --------------- | ------- | -------------------------------------------------------------------------- |
| `exposure`      | `1`     | Multiplies the source before the tone curve.                               |
| `vignette`      | `0.35`  | Darkens the corners, or fades them out over a page: 0 not at all, 1 fully. |
| `dither`        | `1`     | Noise against banding, in 8-bit steps of what the output stores. 0 is off. |
| `glowIntensity` | `0.12`  | How much glow the final pass adds.                                         |
| `threshold`     | `1`     | Brightness where the glow starts.                                          |
| `knee`          | `0.5`   | How gradually it starts, as a fraction of the threshold.                   |
| `spread`        | `1`     | Distance between taps on each step up, in texels of the smaller level.     |

The first four fields belong to the final pass and the last three to bloom. [FXAA](fxaa.md#settings)'s settings patch the same way.

## Grading

`grade(color, uv)` runs after the tone curve, on colours in display range, and before the vignette. The grade below cools the shadows:

```ts
import { finish, tone } from '@typegpu/kit';

const coolShadows = (color: d.v3f, _uv: d.v2f) => {
  'use gpu';
  const dark = 1 - std.saturate(tone.luminance(color) * 3);
  return std.mix(color, color * d.vec3f(0.85, 0.95, 1.15), dark);
};

const final = finish.create(root, {
  format: 'bgra8unorm',
  source: () => screen.textures.hdr,
  grade: coolShadows,
});
```

### Around the pixel

Use `finish.picture(uv)` inside `grade` to read a neighbouring source pixel with glow, exposure and tone mapping applied. `finish.glow(uv)` reads glow before `glowIntensity` scales it and returns zero if absent. The grade below adds red and blue fringes, like a lens's chromatic aberration:

```ts
import { finish } from '@typegpu/kit';

const fringe = (color: d.v3f, uv: d.v2f) => {
  'use gpu';
  const shift = (uv - 0.5) * 0.008;
  return d.vec3f(finish.picture(uv + shift).x, color.y, finish.picture(uv - shift).z);
};

const final = finish.create(root, {
  format: 'bgra8unorm',
  source: () => screen.textures.hdr,
  grade: fringe,
});
```

- Each `finish.picture` call repeats source and glow reads and tone mapping. The fringe example triples that work. For sharpening, sample neighbours `std.fwidth(uv)` apart, one texel.
- These functions work only inside the final pass's `grade`. Other uses fail shader compilation.
- For a lookup table, capture a 3D texture view and sampler in the grade. If the table can change, read it through a [filled accessor](../concepts/connecting-modules.md), for example `grade: tgpu.fn(looked).pipe((cfg) => cfg.with(table, () => view.$))`.

### Another target

A grade can show any sampled target instead of the picture, such as depth while you tune fog. Read a target that follows the canvas through a [`bindings.layout`](bindings.md) part, and set the shared group on the pass before `draw`:

```ts
import { d, std } from 'typegpu';
import { bindings, finish, targets } from '@typegpu/kit';

const screen = targets.create(root, surface, {
  hdr: { format: 'rgba16float', sampled: true },
  depth: { format: 'depth24plus', sampled: true },
});
const shown = bindings.layout({ depth: { texture: d.textureDepth2d() } });
const part = shown.bind({ depth: screen.textures.depth });
const shared = bindings.share(root, { shown: part }); // before `finish.create`

// Depth as grey, white at the eye
const depthOf = (_color: d.v3f, uv: d.v2f) => {
  'use gpu';
  const texel = d.vec2i(uv * d.vec2f(std.textureDimensions(shown.$.depth)));
  return d.vec3f(1 - std.textureLoad(shown.$.depth, texel, 0));
};
const final = finish.create(root, {
  format: surface.format,
  source: () => screen.textures.hdr,
  grade: tgpu.fn(depthOf).pipe(part.inject()),
});

frames.animate(
  frames.create(root, (frame) => {
    if (screen.fit()) part.replace({ depth: screen.textures.depth });
    // the scene, into hdr and depth

    const output = frame.encoder.beginRenderPass({
      colorAttachments: { view: surface.view(frame) },
    });
    output.setBindGroup(shared.layout, shared.group());
    final.draw(output);
    output.end();
  }),
);
```

- `fit()` returns true after it rebuilds the targets. Replacing the part's texture then keeps the pipeline.
- Without the share, `finish.create`'s pipeline binds the part's resources as fixed bindings, and `replace` throws at the first resize. Give `bind` an [`unshared`](bindings.md#resources-that-change) message to make the pipeline throw as it builds instead.
- With `glow`, the final pass uses 3 bind groups and the shared group is the fourth, WebGPU's limit.
- The final pass still encodes, vignettes and dithers what the grade returns. Pass `encode: false` and set `vignette` and `dither` to 0 to see the stored values unchanged.

### Intermediate targets

The same grade shows what a module computes on the way to the picture. [Ambient occlusion](ao.md) stores visibility in red, so a final pass can take its output as the source and show red as grey:

```ts
const grey = (color: d.v3f) => {
  'use gpu';
  return d.vec3f(color.x);
};
const occlusionView = finish.create(root, {
  format: 'bgra8unorm',
  source: occlusion,
  tone: null,
  encode: false,
  grade: grey,
  settings: { vignette: 0, dither: 0 },
});
```

## Over a page

A surface with `alphaMode: 'premultiplied'` shows what is behind it, such as a web page. Draw the scene in premultiplied colour into a target cleared to transparent, and give the final pass `alpha: 'premultiplied'`:

```ts
const surface = web.surface(root, document.querySelector('canvas')!, {
  alphaMode: 'premultiplied',
});
const screen = targets.create(root, surface, { hdr: { format: 'rgba16float', sampled: true } });

const glow = bloom.create(root, { source: () => screen.textures.hdr });
const final = finish.create(root, {
  format: surface.format,
  source: () => screen.textures.hdr,
  alpha: 'premultiplied',
  glow,
});
```

- The final pass divides the colour by alpha before the curve and grade, and multiplies it by alpha again after dither. Clear pixels stay clear, and the noise doesn't show over the page.
- The glow raises the alpha by its brightest channel, so it shows over the page around what casts it.
- The vignette fades the corners out, colour and alpha together. Darkening them would tint the page.

## In a shader of your own

Custom screen shaders can finish with a tone curve, `linearToSrgb` from `@typegpu/color` and `tone.dither(encoded, pixel, steps)` to reduce banding. Import curves, `luminance` and `dither` from `@typegpu/kit/tone` without creating bloom or a final pass.

```ts
import { tone } from '@typegpu/kit';
import { linearToSrgb } from '@typegpu/color';

const shown = (color: d.v3f, position: d.v4f) => {
  'use gpu';
  return d.vec4f(tone.dither(linearToSrgb(tone.neutral(color)), position.xy, 1), 1);
};
```

## An effect of your own

> `import { post } from '@typegpu/kit'`: `post.textureOf(source)`, `post.regionOf(source, region)`, `post.fitRegion(size, texture, region)`. These types and functions read a picture another pass rendered, as every post-processing `source` does. **kind** cpu. **GPU functions** `within`. **types** `FloatTexture`, `Rendered`, `Source`. `src/post/post.ts`.

An effect you write can take its picture the way the kit's effects do, so it fits anywhere in the chain. Every `source` option accepts a `post.Source`. An object with a float texture as `output`, and optionally a `region`, is a `post.Rendered`, which the next effect can take as its source:

```ts
import { post, type Frame, type SizeSource } from '@typegpu/kit';

interface GhostOptions {
  readonly source: post.Source;
  readonly region?: SizeSource;
}

function createGhost(root: TgpuRoot, options: GhostOptions) {
  const source = post.textureOf(options.source);
  let region: SizeSource | undefined;

  return {
    get output() {
      return trail(); // the texture this effect draws into
    },
    get region() {
      return region; // the region the last recording drew
    },
    record(frame: Frame) {
      // At every recording, since a resize replaces the texture and can move the region
      const picture = source();
      region = post.regionOf(options.source, options.region);
      // draw from `picture` within `region` into `output`
    },
  };
}
```

- `post.textureOf(source)` returns a function for either form of source. Call it at every recording.
- `post.regionOf(source, region)` picks the `region` option, else a rendered source's own region. `undefined` means the whole texture. Call it at every recording too, after taking the texture.
- `post.fitRegion(size, texture, region)` writes the region's width and height in texels into `size`, clamped to the texture. It returns whether the measured size changed. Use that value to skip redundant uniform writes.
- `post.within(at, region, size)` clamps texture coordinates to the region in a shader, so filtering reads nothing beyond its edge.

## What it's made of

| Part                         |                                                                                                         |
| ---------------------------- | ------------------------------------------------------------------------------------------------------- |
| `final.settings`             | Exposure, vignette, dither and `glowIntensity`, a uniform.                                              |
| `glow.settings`              | Threshold, knee and spread, a uniform.                                                                  |
| `glow.output`, `glow.region` | Bloom chain with glow at level 0 and its region. Read each frame because resizing replaces the texture. |

## Limits

- `tone` and `grade` compile into pipelines. Create another final pass to switch them at runtime.
- Built-in curves do not use Oklab. Per-channel curves shift orange toward yellow and blue toward cyan. Supply a custom `tone` using conversions from `@typegpu/color`.
- `linearToSrgb` passes negative values through; a unorm target clamps them as it stores.
- `exposure` meters with one compute pass of one 256-thread workgroup, the same at any resolution. Pixels darker than 0.0001 count as 0.0001, and every sample weighs the same, with no weighting towards the centre.
- Final-pass stage order is fixed. For a custom vignette or encoding, implement it in `grade` and set `vignette: 0` and `encode: false`.
