# Mipmaps

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

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

> `import { mipmaps } from '@typegpu/kit'`: `mipmaps.create(root, texture, { combine, … }) → Mipmaps`. Fills a texture's mip levels in your encoder, each texel combining a 2×2 block of the level above with a `'use gpu'` function: a mean for sampling, the farthest depth for occlusion. **records** `record` compute, `of().record` compute: `record` after whatever writes level 0 or `source`, before what reads the levels. **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()`. **GPU functions** `mean`, `largest`, `smallest`. **types** `Combine`. `src/mipmaps.ts`; ocean.
>
> `Options`, taken by `create`:
> - `combine: Combine` A `'use gpu'` function. 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.
>
> `Mipmaps`, from `create`:
> - `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.

A texture you draw into, such as an atlas 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 texel 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 frame.

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

const depth = root
  .createTexture({ size: [1280, 720], format: 'depth32float' })
  .$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 farthest = root
  .createTexture({ size: [1024, 512], format: 'r32float', mipLevelCount: 11 })
  .$usage('storage', 'sampled');
const pyramid = mipmaps.create(root, farthest, { combine: mipmaps.largest, source: depth });

frames.create(root, (frame) => {
  // ... the depth prepass
  const compute = frame.encoder.beginComputePass();
  pyramid.record(compute);
  // ... the kernel that culls what the depth hides
  compute.end();
});
```

## Combining

`combine` takes the block's four texels as `vec4f`, left to right and then top to bottom, and returns the texel below them. The module 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.

## Levels and passes

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](baker.md)'s texture does: one dispatch 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](impostors.md). Both fill every array layer of a level.

`create` makes a bind group 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](targets.md) that a resize replaces, call `of` through [`memo`](targets.md#following-new-textures) with the current texture, so a resize makes bind groups and no pipeline.

## Limits

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