# Depth of field

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

Blur by distance from a focus, with near objects spreading over what is sharp behind them.

> `import { dof } from '@typegpu/kit'`: `dof.create(root, view, { source, depth, pull?, region?, … }) → Dof`. Creates depth of field. **records** `record` encoder: `record` after the TAA resolve or whatever wrote the source, before bloom and the final pass; then `output` holds the picture. **owns** targets, 2 buffers, a sampler, 5 pipelines, 6 bind groups: the half-resolution picture, the tiles, their reach, the blur and the output; the settings and the regions; a group for each pass and one for the depth. With `pull` or after `aim`, also the focus and its step, the kernel moving it and its groups. **allocates** on resize: targets for the new size and the bind groups of its passes; so does a new source or depth. **lifecycle** `initAsync()`, `destroy()`, `[Symbol.dispose]`. **injects** hook: the half-resolution picture and regions `dof.circle` reads, from the part. **needs** `cameras`: as `view`; fills a hook in its pipelines. **GPU functions** `distanceAt`, `thinLens`, `focus`, `circle`. **schemas** `Settings`. **constants** `defaults`. **types** `Radius`. `src/post/dof.ts`; hex-isle, night-market.
>
> `Options`, taken by `create`:
> - `source: Source` Provides the linear picture: a rendered picture, such as `aa`, or a function returning a texture.
> - `depth: () => Depth` Returns a sampled depth texture of the same picture, of any size, such as a half-resolution prepass.
> - `radius?: Radius` A `'use gpu'` function. Returns the signed blur radius at an eye distance and a point of the picture, from 0 to 1 over its region, as a fraction of the picture height: negative in front of the focus, positive behind it. Default `thinLens` at `focus()`.
> - `pull?: number` Sets how fast the focus follows `aim`, per second: each second closes `1 - e^-pull` of the way, evenly in blur rather than in distance. Default `Infinity`, which focuses at once from the `record` after `aim`.
> - `rings?: 2 | 3` Sets the rings of taps in the gather, 3 for 49 taps or 2 for 25. Default 3.
> - `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 focus 10, aperture 0.05 and limit 0.02.
>
> `RecordOptions`, taken by `Dof.record`:
> - `dt?: number` Sets the seconds the focus moves by. Default the frame's `dt`, or all the way.
> - `timestampWrites?: TimestampWrites | undefined` Supplies a timer's writes for the passes. Default none.
>
> `Settings`, a schema:
> - `focus: d.f32` Sets the distance in focus, in world units along the view direction.
> - `aperture: d.f32` Sets the diameter of the lens opening, in world units.
> - `limit: d.f32` Sets the largest blur radius, as a fraction of the picture height.
>
> `Dof`, from `create`:
> - `settings: TgpuUniform<typeof Settings>` Holds `defaults` with overrides from `settings`.
> - `output: FloatTexture` Provides the full-size blurred `rgba16float` picture.
> - `region: SizeSource` Gives the region of `output` that `record` writes, matching the source region.
> - `part: bindings.Part` Provides the half-resolution picture and its regions for `dof.circle`.
> - `inject()` Fills `dof.circle` for pipelines created from the configuration it is piped into.
> - `aim(x, y)` Focuses on what the depth holds at a point of the picture, from 0 to 1 over its region, such as under a tap.
> - `initAsync()` Compiles the depth of field pipelines.
> - `record(target, options?)` Records the prepare, tiles, reach, gather and composite passes after the source is written.
> - `destroy()` Destroys the textures and the focus buffer.

A lens is sharp at one distance. In front of it and behind it, each point spreads into a disc, the circle of confusion, that grows with the distance from the focus. Blurring by distance needs a radius at each pixel, and a blur that spreads a near object over what is sharp behind it while a far one never bleeds onto what is in front.

`dof.create` reads the picture and a depth of it and records 5 passes:

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

const screen = targets.create(root, surface, {
  hdr: { format: 'rgba16float', sampled: true },
  depth: { format: 'depth32float', sampled: true },
});
const blur = dof.create(root, view, {
  source: () => screen.textures.hdr,
  depth: () => screen.textures.depth,
  settings: { focus: 4, aperture: 0.03 },
});
const glow = bloom.create(root, { source: blur });
const final = finish.create(root, { format: surface.format, source: blur, glow });

frames.animate(
  frames.create(root, (frame) => {
    screen.fit();
    // the scene, into hdr and depth

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

- **Order** Record it after [TAA](taa.md), with `source: aa`, and before bloom. Bloom reading `blur.output` turns out-of-focus highlights into glowing discs.
- **Radius** The radius is a fraction of the picture height, negative in front of the focus and positive behind it. By default it is the thin-lens circle, `0.5 × aperture × k × (1 / focus − 1 / distance)`, where `k` is half the camera's vertical [projection scale](cameras.md), its focal length over the sensor height. `limit` caps it.
- **Units** `aperture` and the distances share world units. A 50 mm lens at f/2 in a scene in metres opens 0.025. Larger openings make a scene look smaller, as a macro lens does.
- **Depth** `depth` can be any size and is read by UV. A multisampled depth can't be read, so draw a single-sampled depth prepass, as hex isle does at half resolution under its MSAA scene. Radius edges then step at the prepass's resolution.
- **Turning it off** To turn it off, skip `record` and give bloom and the final pass the picture it read. Point their `source` at a function that picks one.
- **Timing** `record` takes the frame or an encoder. `record(frame, { timestampWrites })` times its 5 render passes under one [profiler](profiler.md) timer. The compute pass that pulls the focus runs before the timer starts.

| Field      | Default |                                                             |
| ---------- | ------- | ----------------------------------------------------------- |
| `focus`    | `10`    | Distance in focus, in world units along the view direction. |
| `aperture` | `0.05`  | Diameter of the lens opening, in world units.               |
| `limit`    | `0.02`  | Largest blur radius, as a fraction of the picture height.   |

## Pulling focus

`blur.aim(x, y)` focuses on what the depth holds at a point of the picture, from 0 to 1 over it, such as under a tap. `pull` sets how fast the focus gets there, per second:

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

const blur = dof.create(root, view, {
  source: () => screen.textures.hdr,
  depth: () => screen.textures.depth,
  pull: 5,
});

// On a tap, at a point of the picture
blur.aim(0.5, 0.5);

// Each frame
blur.record(frame);
```

- The next `record` reads the distance at the aimed point, and each `record` from then on moves `settings.focus` towards it on the GPU by the frame's `dt`, in one more compute pass. Nothing is read back.
- Each second closes `1 - e^-pull` of the way, as [`clock.ease`](clock.md#easing-towards-a-goal) does, evenly in blur rather than in distance, so a pull from far to near looks as smooth as one from near to far. Without `pull`, the focus jumps.
- `settings.focus` sets the focus until the first `aim`. A `radius` of your own reads the moving focus with `dof.focus()`.
- With an encoder in place of the frame, give the seconds yourself, `blur.record(encoder, { dt })`.

## Your own radius

`radius(distance, uv)` replaces the thin lens. It is a `'use gpu'` function that returns the signed radius, and it carries its fills with `pipe`. Three functions work only inside it:

- `dof.thinLens(distance, focus)` returns the thin-lens radius with the settings' aperture.
- `dof.focus()` returns the distance in focus, which follows `aim`.
- `dof.distanceAt(uv)` returns the distance along the view direction to what the depth holds at a point of the picture, from [`cameras.distanceOf`](cameras.md#what-shaders-read).

[Hex isle](../examples/hex-isle.md) uses both, through a uniform that picks one. By default it blurs above and below a band, so the island looks like a miniature:

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

const cursor = root.createUniform(d.vec2f, d.vec2f(0.5));

// Focus on whatever is under the pointer
const underCursor = (distance: number, _uv: d.v2f) => {
  'use gpu';
  return dof.thinLens(distance, dof.distanceAt(cursor.$));
};

// A miniature: sharp in a band, blurred above it and in front below it
const tiltShift = (_distance: number, uv: d.v2f) => {
  'use gpu';
  const away = uv.y - 0.54;
  return std.sign(-away) * std.max(std.abs(away) - 0.07, 0) * 0.06;
};

const blur = dof.create(root, view, {
  source: () => screen.textures.hdr,
  depth: () => screen.textures.depth,
  radius: underCursor,
});
```

- Write the pointer into `cursor` as it moves, in picture coordinates from 0 to 1. The focus jumps to each new surface. To ease it, [pull the focus](#pulling-focus) with `aim` instead.
- The radius runs once per half-resolution texel and once per pixel, so keep it cheap.

## Seeing the radius

Depth of field keeps its blur radius where a final pass's [grade](finish.md#grading) can't read it. `dof.circle(uv)` reads it through `blur.part`, shared as in [Another target](finish.md#another-target). This grade tints the near field orange and the far field blue:

```ts
const shared = bindings.share(root, { blur: blur.part }); // before `finish.create`

const focusBand = (color: d.v3f, uv: d.v2f) => {
  'use gpu';
  const radius = dof.circle(uv);
  const tint = std.select(d.vec3f(1, 0.45, 0.1), d.vec3f(0.1, 0.45, 1), radius > 0);
  return std.mix(color, tint, std.saturate(std.abs(radius) * 100));
};
const focusView = finish.create(root, {
  format: 'bgra8unorm',
  source: blur,
  grade: tgpu.fn(focusBand).pipe(blur.inject()),
});
// Set `shared.group()` on the pass before `focusView.draw`
```

## What the passes do

1. **Prepare** At half resolution. Averages 2 × 2 pixels and stores the radius at the nearest of their depths in alpha.
2. **Tiles** At a sixteenth of each side. Each texel holds the largest radius in its 16 × 16 pixels.
3. **Reach** At the tiles' size. Each texel holds the largest radius in the tiles that a circle of `limit` can reach from it.
4. **Gather** At half resolution. Scales rings of taps, 3 rings for 49 taps or 2 for 25, to its tile's reach. A tap behind the focus counts where the smaller of its circle and the centre's reaches the centre, so what is sharp keeps its edge. A tap in front counts where its own circle reaches, and lays over the rest by how much of the disc such taps cover, so a blurred foreground spreads over a sharp background. Where the largest radius is under half a texel, it copies the centre.
5. **Composite** At full resolution. Adds the source's detail above the half-resolution picture back onto the blur where the radius is under a pixel, fading out by 3 pixels, and where no near field covers it.

| Pass      | Textures                                     | Samplers | Uniforms                  |
| --------- | -------------------------------------------- | -------- | ------------------------- |
| Prepare   | source, depth                                | 0        | settings, regions, camera |
| Tiles     | half-resolution picture                      | 0        | regions                   |
| Reach     | tiles                                        | 0        | settings, regions         |
| Gather    | half-resolution picture, reach               | 0        | settings, regions         |
| Composite | source, half-resolution picture, blur, depth | 1        | settings, regions, camera |

No pass binds storage. `radius` adds what it reads, such as the cursor's uniform, to the prepare and the composite. After an `aim`, a one-thread compute pass before the prepare reads the depth under the aimed point, moves the focus in a storage buffer and copies it into the settings.

## What it's made of

| Part                         |                                                                                                                   |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `blur.settings`              | Focus, aperture and limit, a uniform.                                                                             |
| `blur.output`, `blur.region` | Full-size `rgba16float` picture with depth of field and its region. Read each frame because resizing replaces it. |
| `blur.part`                  | The half-resolution picture and its regions, for `dof.circle`.                                                    |
| `blur.aim`                   | Pulls the focus to what is at a point of the picture.                                                             |

## Limits

- Depth of field reads one depth per pixel. A translucent drawing, which writes no depth, and anything the depth leaves out take the radius of what is behind them.
- Out-of-focus highlights are always discs.
- Under `cameras.flat`, `dof.thinLens` has no meaning. Give `radius` a function of your own.
