Skip to content

ModuleCore / Passes and targetsConcept: Scenes and passes

Mipmaps

Fill a texture's mip levels in your encoder, each texel combining a 2×2 block of the level above.

import
import { mipmaps } from '@typegpu/kit'
call
mipmaps.create(root, texture, { combine, … }) → Mipmaps
facts
recordscomputelifecycleinitAsync()
On this page

A texture you draw into, such as an or a depth buffer, has only its first level. Sampling it small needs the smaller levels averaged from it, and an occlusion test needs each level to keep the farthest depth under it, so one answers for a whole block of the screen. mipmaps.create fills the levels from your 'use gpu' function of each 2×2 block, in the encoder or compute pass of your .

A depth texture's mip levels, each texel combining a 2×2 block of the level before

Try Tap a texel, or use the arrow keys, to light the 2×2 block it combines and what that block covers in the levels before. Then switch to mean, which blurs the box and the pole into what is behind them. largest keeps each block's farthest depth, so the pole is gone by level 1 and the box by level 2.
import {
import mipmaps
mipmaps
} from '@typegpu/kit';
const
const depth: TgpuTexture<{
size: [1280, 720];
format: "depth32float";
}> & RenderFlag & SampledFlag
depth
=
const root: TgpuRoot
root
.
TgpuRoot.createTexture<number, number, number, [1280, 720], "depth32float", number, number, GPUTextureFormat[], GPUTextureDimension>(props: CreateTextureOptions<[1280, 720], "depth32float", number, number, GPUTextureFormat[], GPUTextureDimension>): TgpuTexture<{
size: [1280, 720];
format: "depth32float";
}>
createTexture
({
size: [1280, 720]

The width, height, and depth or layer count of the texture.

size
: [1280, 720],
format: "depth32float"

The format of the texture.

format
: 'depth32float' })
.
TgpuTexture<{ size: [1280, 720]; format: "depth32float"; }>.$usage<["render", "sampled"]>(usages_0: "render", usages_1: "sampled"): TgpuTexture<{
size: [1280, 720];
format: "depth32float";
}> & RenderFlag & SampledFlag
$usage
('render', 'sampled');
// Half the depth's size, rounded up to powers of two, so every level halves evenly. A screen
// coordinate `uv` reads it at `uv * [640, 360] / [1024, 512]`
const
const farthest: TgpuTexture<{
size: [1024, 512];
format: "r32float";
mipLevelCount: 11;
}> & SampledFlag & StorageFlag
farthest
=
const root: TgpuRoot
root
.
TgpuRoot.createTexture<number, number, number, [1024, 512], "r32float", 11, number, GPUTextureFormat[], GPUTextureDimension>(props: CreateTextureOptions<[1024, 512], "r32float", 11, number, GPUTextureFormat[], GPUTextureDimension>): TgpuTexture<{
size: [1024, 512];
format: "r32float";
mipLevelCount: 11;
}>
createTexture
({
size: [1024, 512]

The width, height, and depth or layer count of the texture.

size
: [1024, 512],
format: "r32float"

The format of the texture.

format
: 'r32float',
mipLevelCount?: 11 | undefined

The number of mip levels the texture will contain.

@default ― 1

mipLevelCount
: 11 })
.
TgpuTexture<{ size: [1024, 512]; format: "r32float"; mipLevelCount: 11; }>.$usage<["storage", "sampled"]>(usages_0: "storage", usages_1: "sampled"): TgpuTexture<{
size: [1024, 512];
format: "r32float";
mipLevelCount: 11;
}> & SampledFlag & StorageFlag
$usage
('storage', 'sampled');
const
const pyramid: mipmaps.Mipmaps
pyramid
=
import mipmaps
mipmaps
.
function create(root: TgpuRoot, texture: TgpuTexture & SampledFlag, options: mipmaps.Options): mipmaps.Mipmaps

Creates what fills a texture's mip levels, every array layer of each, from the level above. A level of odd size leaves the last texels of the one above out.

@owns ― a pipeline, a bind group per level: the kernel or the draw that fills a level, and the views each level reads and writes

@records ― record after whatever writes level 0 or source, before what reads the levels

@param ― texture a 2D float colour texture with storage or render usage, and sampled usage

@returns ― mipmaps that record into the texture

create
(
const root: TgpuRoot
root
,
const farthest: TgpuTexture<{
size: [1024, 512];
format: "r32float";
mipLevelCount: 11;
}> & SampledFlag & StorageFlag
farthest
, {
Options.combine: mipmaps.Combine

Combines a 2×2 block of the level above into one texel.

@gpu

combine
:
import mipmaps
mipmaps
.
const largest: mipmaps.Combine

Keeps the largest of the four per channel, such as the farthest depth over a block.

largest
,
Options.source?: (TgpuTexture<any> & SampledFlag) | undefined

Fills level 0 from this texture's 2×2 blocks too, such as the depth a pyramid of farthest depths starts from. It has as many array layers, and blocks past its edge repeat its last texels. Default leaving level 0 to you.

source
:
const depth: TgpuTexture<{
size: [1280, 720];
format: "depth32float";
}> & RenderFlag & SampledFlag
depth
});
import frames
frames
.
function create(root: TgpuRoot, record: Recorder): frames.Frames

Records a frame into one encoder and submits once, with a clock. Borrows the root. The synchronous recorder borrows each frame's encoder.

@submits ― always once a frame after your recorder returns, and again at each frame.submit()

@param ― record your frame, recorded into each frame's encoder

@returns ― a loop you run with each frame's time and destroy

create
(
const root: TgpuRoot
root
, (
frame: frames.Frame
frame
) => {
// ... the depth prepass
const
const compute: TgpuComputePass
compute
=
frame: frames.Frame
frame
.
Frame.encoder: TgpuCommandEncoder

Records commands for submission after the recorder returns. submit() replaces this encoder. Read it at each call instead of keeping it, and let the frame submit it.

encoder
.
TgpuCommandEncoder.beginComputePass(descriptor?: TgpuComputePassDescriptor): TgpuComputePass

Begins recording a compute pass

beginComputePass
();
const pyramid: mipmaps.Mipmaps
pyramid
.
Mipmaps.record(target: TgpuCommandEncoder | TgpuComputePass): void

Records the levels in order. A texture with storage usage fills in a compute pass, the pass given or one of its own on the encoder. Another fills in render passes on the encoder.

record
(
const compute: TgpuComputePass
compute
);
// ... the kernel that culls what the depth hides
const compute: TgpuComputePass
compute
.
TgpuComputePass.end(): void

Completes the recording of this compute pass

end
();
});

combine takes the block’s four texels as vec4f, left to right and then top to bottom, and returns the texel below them. The exports three:

  • mipmaps.mean averages them, the filter for textures you sample.
  • mipmaps.largest and mipmaps.smallest keep the largest or smallest per channel, such as the farthest depth with depth that grows away from the eye.

A function of your own can weigh the texels by their alpha, so empty texels around a cutout leave its colour alone.

Level 0 is yours to draw or write, and record fills level 1 and on from it. With source, record fills level 0 too, from that texture’s blocks. A source larger than two texels per texel of level 0 leaves its far part out; one smaller repeats its last row and column.

Levels fill as a baker’s texture does: one per level for a texture with storage usage, and otherwise a render pass per level and array layer, such as for the rgba8unorm-srgb atlas of impostors. Both fill every array layer of a level.

create makes a per level of its texture. of(texture, source) fills another texture of the same format and usage through the same pipeline, with bind groups of its own, such as the other texture of a pair. For a target that a resize replaces, call of through memo with the current texture, so a resize makes bind groups and no pipeline.

  • The texture and source are single-sampled 2D textures with sampled usage that hold floats, and source has the texture’s array layers. A depth source reads its depth. create and of throw on another, naming the property.
  • A level of odd size leaves the last row or column of the level above out. Size the texture in powers of two for blocks that cover every texel.
atlasAn atlas is one texture that holds many small pictures side by side, such as every glyph of a font or every view of a model.Words →
texelA texel is one cell of a texture, as a pixel is one cell of the screen.Words →
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 →
moduleA module is one piece of GPU work, made by a factory that borrows your root and returns resources, shader functions and methods that record into your frame.A module borrows →
dispatchA dispatch runs a kernel once over a number of threads, inside a compute pass.Words →
bind groupA bind group is a set of buffers, textures and samplers that a pipeline reads, and a pipeline has room for four.Words →

Reference

kit.mipmaps

moduleHow to read this card

Callsborrows, never destroystakesreturns

mipmaps.create(root, texture, { combine, … }) → Mipmaps

root
the root
texture
a 2D float colour texture with storage or render usage, and sampled usage
Mipmaps
mipmaps that record into the texture
takes

Options

2 fields
combine

Combine'use gpu'

Combines a 2×2 block of the level above into one texel.

source?

TgpuTexture & SampledFlag

Fills level 0 from this texture's 2×2 blocks too, such as the depth a pyramid of farthest depths starts from.Default leaving level 0 to you.

returns

Mipmaps

3 members
record(target)

Records the levels in order.

initAsync()

Compiles the pipeline.

of(texture, source?)

Returns mipmaps of another texture of this one's format and usage, filled through the same pipeline from its own source or level 0, with their bind groups made now: the other texture of a pair, or the one a resize puts in this one's place.

In a frame

records

record after whatever writes level 0 or source, before what reads the levels.

Holds

owns

a pipeline ·a bind group per level

The kernel or the draw that fills a level, and the views each level reads and writes.

lifecycle

initAsync()destroy()(none)[Symbol.dispose](none)

Exports

GPU functions
mean largest smallest
types
Combine

used byOcean