Skip to content

ModulePost-processing / AntialiasingConcept: Scenes and passes

FXAA

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

import
import { fxaa } from '@typegpu/kit'
call
fxaa.create(root, { format, source, region?, … }) → Fxaa
facts
recordsrenderencoderallocateson resizelifecycleinitAsync()
On this page

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

raymarchFive balls melting into a cube, ray-marched in one pass.
Hold the pointer over an edge against the floor and toggle FXAA. The loupe shows stepped edges on the raymarched balls and cube, then the smoothed pixels.

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

import {
import fxaa
fxaa
,
import finish
finish
} from '@typegpu/kit';
const
const screen: targets.Targets<Specs<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>>
screen
=
import targets
targets
.
function create<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>(root: TgpuRoot, source: targets.SizeSource, specs: {
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}, options?: targets.Options): targets.Targets<Specs<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>>

Creates render targets that follow a size. Replaces the whole set when the size changes. Put targets needed only sometimes in their own set and create that set when needed. Throws for transient targets with sampling, storage or mip levels, for multisampled targets with storage or mip levels, and for layered targets that are multisampled, mipped or have more layers than the device allows.

@param ― source the size it follows, such as a surface

@param ― specs a format and options per target, by name

@param ― options.resolution the fraction of each side drawn, which changes without allocating

@returns ― a set of targets you fit every frame and destroy

create
(
const root: TgpuRoot
root
,
const surface: web.Surface
surface
, {
hdr: {
readonly format: "rgba16float";
readonly sampled: true;
}
hdr
: {
format: "rgba16float"
format
: 'rgba16float',
sampled: true
sampled
: true },
ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
}
ldr
: {
format: "rgba8unorm"
format
: 'rgba8unorm',
sampled: true
sampled
: true },
});
const
const final: finish.Finish
final
=
import finish
finish
.
function create(root: TgpuRoot, options: finish.Options): finish.Finish

Creates the final pass of a frame. It reads the source, adds glow, tone maps, grades, applies the vignette, encodes and dithers. It draws one triangle into the caller pass.

@allocates ― on resize: a bind group for a new source or glow

@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

@records ― 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.

@param ― options.source the picture, rendered or a texture, read at every draw

@param ― options.region the part of the source that holds the picture, read at every draw

@param ― options.glow a bloom, or a glow of your own, added before tone mapping

@param ― options.exposure an adapted exposure, read on the GPU

@returns ― a final pass you draw every frame

create
(
const root: TgpuRoot
root
, {
Options.format: GPUTextureFormat

Sets the output format, such as the canvas format.

format
: 'rgba8unorm',
Options.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. draw reads it on every call, so the texture can be rebuilt.

@cpu

source
: () =>
const screen: targets.Targets<Specs<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>>
screen
.
Targets<Specs<{ readonly hdr: { readonly format: "rgba16float"; readonly sampled: true; }; readonly ldr: { readonly format: "rgba8unorm"; readonly sampled: true; }; }>>.textures: {
readonly hdr: targets.Target<{
readonly format: "rgba16float";
readonly sampled: true;
}>;
readonly ldr: targets.Target<{
readonly format: "rgba8unorm";
readonly sampled: true;
}>;
}

Provides the current textures. A size change replaces them.

textures
.
hdr: targets.Target<{
readonly format: "rgba16float";
readonly sampled: true;
}>
hdr
});
const
const smooth: fxaa.Fxaa
smooth
=
import fxaa
fxaa
.
function create(root: TgpuRoot, options: fxaa.Options): fxaa.Fxaa

Creates an FXAA 3.11 pass at the default quality. It smooths edges after the source is in display range. Each pixel follows its edge in both directions to estimate its length, then samples across the edge. It draws one triangle into the caller pass.

@allocates ― on resize: a bind group for a new source

@owns ― 2 buffers, a sampler, a pipeline, a bind group: the settings, the region it reads and the source's group

@records ― 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.

@param ― options.source the picture as the screen shows it, read at every draw

@param ― options.region the part of the source that holds the picture, read at every draw

@returns ― a pass you draw every frame

create
(
const root: TgpuRoot
root
, {
Options.format: GPUTextureFormat

Sets the output format, such as the canvas format.

format
:
const surface: web.Surface
surface
.
Surface.format: GPUTextureFormat

Reports the format of its textures, which pipelines that draw into it target.

format
,
Options.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. draw reads it on every call and throws for -srgb formats, which decode on sampling.

@cpu

source
: () =>
const screen: targets.Targets<Specs<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>>
screen
.
Targets<Specs<{ readonly hdr: { readonly format: "rgba16float"; readonly sampled: true; }; readonly ldr: { readonly format: "rgba8unorm"; readonly sampled: true; }; }>>.textures: {
readonly hdr: targets.Target<{
readonly format: "rgba16float";
readonly sampled: true;
}>;
readonly ldr: targets.Target<{
readonly format: "rgba8unorm";
readonly sampled: true;
}>;
}

Provides the current textures. A size change replaces them.

textures
.
ldr: targets.Target<{
readonly format: "rgba8unorm";
readonly sampled: true;
}>
ldr
});
// After the scene each frame
const final: finish.Finish
final
.
Finish.record(target: Frame | TgpuCommandEncoder, view: GPUTextureView, options?: finish.RecordOptions): void

Opens a pass on view, such as surface.view(frame), draws into it and ends it. A viewport draws the picture into that rectangle and clears the rest, transparent with alpha: 'premultiplied' and black otherwise. Optional timer writes measure the pass.

record
(
const frame: Frame
frame
,
const screen: targets.Targets<Specs<{
readonly hdr: {
readonly format: "rgba16float";
readonly sampled: true;
};
readonly ldr: {
readonly format: "rgba8unorm";
readonly sampled: true;
};
}>>
screen
.
Targets<Specs<{ readonly hdr: { readonly format: "rgba16float"; readonly sampled: true; }; readonly ldr: { readonly format: "rgba8unorm"; readonly sampled: true; }; }>>.views: {
readonly hdr: GPUTextureView;
readonly ldr: GPUTextureView;
}

Provides attachment views of mip 0, created once per rebuild.

views
.
ldr: GPUTextureView
ldr
);
const smooth: fxaa.Fxaa
smooth
.
Fxaa.record(target: Frame | TgpuCommandEncoder, view: GPUTextureView, options?: Timed): void

Opens a pass on view, such as surface.view(frame), draws into it and ends it. Optional timer writes measure the pass.

record
(
const frame: Frame
frame
,
const surface: web.Surface
surface
.
Surface.view(frame: Frame): GPUTextureView

Returns a view of the current canvas texture, created once per texture. Applies pending size changes first, which a pass using context would skip.

view
(
const frame: Frame
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, 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.

smooth.settings is a uniform. Use TypeGPU’s patch when settings change, as the final pass 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.

  • FXAA uses the default FXAA 3.11 edge-search quality. It is not configurable.
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →

Reference

kit.fxaa

moduleHow to read this card

Callsborrows, never destroystakesreturns

fxaa.create(root, { format, source, region?, … }) → Fxaa

root
the root
source
the picture as the screen shows it, read at every draw
region?
the part of the source that holds the picture, read at every draw
Fxaa
a pass you draw every frame
takes

Options

4 fields
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<Settings>

Sets initial values over defaults.Default defaults, with edge 0.166, floor 0.0833 and subpixel 0.75.

returns

Fxaa

4 members
settings

TgpuUniform<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.

In a frame

records

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.

Holds

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()destroy()(none)[Symbol.dispose](none)

Exports

schemas
Settings
constants
defaults
schema

Settings

3 fields
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.

used byRaymarchRobot armSPH sculpt