Skip to content

About

Tooling for Mips, Pyramids, Hybrid Z and more!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

downsampler

Fast mipmaps and depth pyramids for WebGPU, built with compute shaders.

const downsampler = new Downsampler( device );
downsampler.generate( texture ); // every mip level from level 0

The usual way to make mipmaps in WebGPU is one render pass per level, and another set per layer of a cube map or array. downsampler writes up to six levels in one compute dispatch and handles every layer in the same dispatch, so a full chain takes two dispatches at power-of-two sizes and three to six at typical screen sizes.

It also does the jobs render passes can't:

  • Hi-Z depth pyramids (min, max or both) from a depth buffer, guaranteed conservative for occlusion culling.
  • Partial updates: repaint a corner of a texture and refresh only the mip texels that depend on it.
  • Statistics: per-block mean, variance or max (Reducer), and a whole image down to one value, like average luminance for auto-exposure (ScalarReduction).

By default the results match render-pass mipmaps, so you can swap it in without a visual change. Every kernel is tested against a CPU reference on real GPUs. Zero dependencies; a three.js adapter is included.

Status: 0.1. The API can still change before 1.0 (CHANGELOG).

Install

npm install @pmndrs/downsampler

The types use the standard WebGPU types, so add @webgpu/types to your project. three is optional and only needed for @pmndrs/downsampler/three.

To run the examples, build from a checkout:

git clone https://github.com/pmndrs/downsampler.git
cd downsampler
npm ci
npm run build
npm run examples
# Open http://localhost:8080/examples/

The examples import ../dist/webgpu/index.js directly from the built checkout.

Quick start

import { Downsampler, requiredFeatures, requiredLimits } from '@pmndrs/downsampler/webgpu';

const adapter = await navigator.gpu.requestAdapter();
// requiredLimits asks for six storage textures, so one dispatch can write six levels;
// requiredFeatures for the optional features that make some formats faster or available.
const device = await adapter.requestDevice( { requiredFeatures: requiredFeatures( adapter ), requiredLimits: requiredLimits( adapter ) } );

const texture = device.createTexture( {
	format: 'rgba8unorm',
	size: [ 1920, 1080 ],
	mipLevelCount: 11,
	usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST,
} );
// … fill level 0 …

const downsampler = new Downsampler( device ); // make one per device and reuse it
downsampler.generate( texture );               // records and submits the work

Already have a command encoder? Use downsampler.encode( encoder, texture ) instead, and it records into yours.

Pipelines compile on first use. To keep that off the frame (a synchronous compile stalls rendering on Windows), await downsampler.warmup( texture ), scalarReduction.warmup() or Reducer.create( device, options ) first (compiling without hitches).

Choose the operation

Task Setting Runnable example
Ordinary color mipmaps Defaults (oddSizes: 'exact') Mip chain
Exact sRGB rounding sRGB texture with RENDER_ATTACHMENT, default srgbRounding: 'exact' three.js
Conservative Hi-Z Separate rg32float destination, { source: depth, reduction: 'minmax' } Hi-Z decisions
Dirty regions { rect: { x, y, width, height } } Brush replay and timings

See getting started for complete feature walkthroughs.

three.js

import { generateMipmaps, requiredLimits } from '@pmndrs/downsampler/three';

const renderer = new THREE.WebGPURenderer( { requiredLimits: requiredLimits( await navigator.gpu.requestAdapter() ) } );
await renderer.init();
generateMipmaps( renderer, texture ); // false if the texture isn't supported; three's own mipmaps still work then

This works today for StorageTextures and sRGB color textures. A built-in mipmapGeneration: 'compute' option for three.js itself is in progress.

How fast is it?

GPU time for a full chain, against one render pass per level:

Apple M5 Pro rgba8 rgba16float
512², six layers (a cube map) 27× faster
1024² 7× faster 4.5× faster
1920×1080 3× faster

On an NVIDIA RTX 3060 Laptop, from 960×540 to 2560×1440, it takes 5–20 % less time than render passes in rgba8 and up to 20 % less in rgba16float; a 512² cube map is 3–4× faster. The exception is sRGB color textures, which compute shaders can't write directly. The library draws those with render passes too, but rounds every level exactly, which plain render passes don't: they stay 1.05–1.6× faster on the RTX and about 5× on Intel. srgbRounding: 'device' skips the exact rounding and matches them.

The big wins are small textures and cube maps, where the cost of starting many render passes dominates. Very large textures (4096²) are limited by memory bandwidth, so every method takes about the same time there. If you can accept SPD's handling of odd sizes, oddSizes: 'spd' takes about 25 % less time on the RTX. Methods and full numbers: SPEC.md §8 and the November 15 Windows measurements. The earlier Windows report is historical; its open NVIDIA sampled-sRGB tolerance question was resolved in the November 16 owner decision (f7248a3). Exact sRGB rounding remains the default (current reference).

Browser support

Any browser with WebGPU. Tested in Chrome on macOS (Apple M5 Pro) and Windows (NVIDIA RTX 3060, Intel UHD), and on WebGPU compatibility-mode devices. Not yet tested: mobile, AMD, Safari, Firefox. If you try it there, npm run lab opens a page that runs the test suite on your device.

Documentation

Development

npm install
npm test                                            # build, unit tests, GPU suite on your GPU
npm run test:gpu -- --quick --adapter=swiftshader   # software WebGPU, no GPU needed
npm run test:examples                               # every example page, checked
npm run bench                                       # benchmarks

License

MIT

About

Tooling for Mips, Pyramids, Hybrid Z and more!

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages