# FXAA

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

Smooth stepped edges in the finished picture with one full-screen pass.

> `import { fxaa } from '@typegpu/kit'`: `fxaa.create(root, { format, source, region?, … }) → Fxaa`. Creates an FXAA 3.11 pass at the default quality. **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 pass that drew the source. **owns** 2 buffers, a sampler, a pipeline, a bind group: the settings, the region it reads and the source's group. **allocates** on resize: a bind group for a new source. **lifecycle** `initAsync()`. **schemas** `Settings`. **constants** `defaults`. `src/post/fxaa.ts`; raymarch, robot-arm, sph-sculpt.
>
> `Options`, taken by `create`:
> - `format: GPUTextureFormat` Sets the output format, such as the canvas format.
> - `source: Source` Provides an encoded picture in display range, such as an `rgba8unorm` target drawn by the final pass, as a function returning the texture or a rendered picture.
> - `region?: SizeSource` Selects the source region from its top-left corner, such as `regions.ldr` 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 edge 0.166, floor 0.0833 and subpixel 0.75.
>
> `Settings`, a schema:
> - `edge: d.f32` Sets the minimum contrast to smooth, as a fraction of the brightest local luma.
> - `floor: d.f32` Sets the minimum contrast to smooth in dark areas where the `edge` fraction is too small.
> - `subpixel: d.f32` Sets how much to soften detail smaller than a pixel, from 0 to 1.
>
> `Fxaa`, 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.

With one sample per pixel, angled edges form steps. [MSAA](targets.md) takes multiple samples in each drawing pass, and [TAA](taa.md) gathers samples over several frames. FXAA 3.11 by Timothy Lottes smooths the finished image in one full-screen pass, using nearby colours to detect edges.

Draw the final pass into an `rgba8unorm` target, then draw it to the screen with FXAA:

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

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

// After the scene each frame
final.record(frame, screen.views.ldr);
smooth.record(frame, surface.view(frame));
```

- `record(frame, view)` opens a pass on `view`, draws and ends it, and also takes an encoder in place of the frame. `smooth.draw(pass)` draws into a pass you opened.
- FXAA detects brightness edges in display-range colour. An `-srgb` source would decode while sampling, so it throws. An `-srgb` output is supported.
- A rendered picture brings its `region`. Give `region` yourself only with a function source drawn below full [resolution](targets.md#resolution-scale), such as `screen.regions.ldr`. FXAA follows edges in source pixels and stretches the region over the output.
- FXAA also smooths texture edges such as checkerboards and can blur text. Draw the HUD after `smooth.draw` in the same pass.

## Settings

`smooth.settings` is a uniform. Use TypeGPU's `patch` when settings change, as the [final pass](finish.md#settings) does. Raise `edge` and `floor` to preserve fine texture edges: they set the least contrast FXAA smooths. Set `subpixel` to 0 to keep details smaller than a pixel sharp.

## Limits

- FXAA uses the default FXAA 3.11 edge-search quality. It is not configurable.
