.nvdb files
.nvdb is NanoVDB's file format: a header, then each grid's metadata and image. It is the exact format here
(D14): sparse, full resolution, read and written byte for byte.
Read
NvdbFile parses a file with any of NanoVDB's codecs: NONE, ZIP and BLOSC. BLOSC covers what NanoVDB writes
(nanovdb_convert -b/--blosc: LZ4 frames, split at 1 GiB) and what NVIDIA Warp writes (Volume.save_to_nvdb(codec="blosc"):
BloscLZ), as NanoVDB 14 reads both. A grid image decompressed from a ZIP or BLOSC file is a copy; from an uncompressed
one it may be a view of the source buffer (isGridImageZeroCopy(i)). Files from NanoVDB 32.x (OpenVDB 10 to 14) load;
another major version throws VDBUnsupportedError.
import { NvdbFile } from '@pmndrs/vdb/tools';
const file = await NvdbFile.fromURL(url); // or NvdbFile.fromArrayBuffer(bytes)
for (const meta of file.grids) console.log(meta.name, meta.gridType, meta.gridClass, meta.voxelSize);
// The grid image is what VDBGridPool.add() takes, byte for byte. Float, Fp4, Fp8, Fp16, FpN and Half grids load.
const image = file.gridImage(0); // decompressed when the file is zipped
Names in the metadata
file.grids is the file's own metadata (NvdbGridMetadata), and two of its fields are spelled differently from the
rest of the library:
vdbInspect(image).gridType uses the metadata spelling too. Nothing is renamed in 0.2.
import { vdbModuleWgsl, type VDBGridTypeName } from '@pmndrs/vdb';
import { nvdbPoolType } from '@pmndrs/vdb/tools';
// The file says 'Float', 'Fp8', 'FpN'; pools and modules say 'float', 'fp8', 'fpn'.
const types: VDBGridTypeName[] = [];
for (const meta of file.grids) {
const type = nvdbPoolType(meta); // null for a grid the pool does not hold (Vec3f, ...)
if (type && !types.includes(type)) types.push(type);
}
const wgsl = vdbModuleWgsl({ slot: { group: 0, firstBinding: 0 }, types, profile: 'sample' });
NvdbFile loads Float, Fp4, Fp8, Fp16, FpN and Half grids. It is the one reader: there is no separate readNvdb, so use
NvdbFile for bytes you already hold (fromArrayBuffer, or fromURL for a fetch) and readNvdbStream for a stream.
Native nanovdb_convert writes Float, Fp8 and FpN; vdbQuantize(image, 'fp4' | 'fp16' | 'half') makes the others (native
NanoVDB 13.2+ writes Half grids only from C++, createNanoGrid<…, nanovdb::Half>).
Write
writeNvdb(images, { codec, zipLevel }) writes any number of grid images to one file. codec is 'none', 'zip' or
'blosc'. 'blosc' writes what nanovdb_convert -b writes: LZ4 Blosc frames (byte shuffle of 4-byte words, 1 MiB
blocks of four 256 KiB streams, a new frame every 1 GiB), byte for byte the same as native NanoVDB for the same image, so
native tools built with Blosc and NVIDIA Warp read it. ZIP (level 9) is smaller (on the sample grids BLOSC is 1.1 to 2.5
times the ZIP size); BLOSC decodes faster in native readers. All three codecs stream (readNvdbStream).
build-dense on building grids shows it in use.
Segment layout
A file with several grids can be laid out two ways (segments, on writeNvdb and vdbBakeCoarseFirst). With one grid
both are the same bytes.
The index misread is a defect in native NanoVDB v14, in its own multi-grid files too
(#33 tracks the upstream report). So: if a native tool will open "grid n" of
your file, write 'per-grid'; if it reads by name, or the file is for the web, keep the default. What 'per-grid' costs
a web stream is under coarse-first.
'one' stays the default in 0.2 (D40). It may be revisited when a native consumer is found that reads our files by
index, or when the upstream fix has shipped.
Stream
readNvdbStream(source) takes a Response, a ReadableStream or any async iterable of byte chunks, and yields each
grid as soon as its bytes are complete, whatever the codec (a BLOSC grid decodes chunk by chunk into its own buffer).
import { readNvdbStream } from '@pmndrs/vdb/tools';
const response = await fetch(url);
// Each grid is emitted as soon as its bytes (raw or zipped) are complete, so the first grid can be
// on screen while the rest downloads.
for await (const grid of readNvdbStream(response, { onProgress: (p) => console.log(p.bytesReceived, p.bytesTotal) })) {
console.log(grid.index, grid.baseName, grid.lod, grid.image.byteLength);
}
Under it, NvdbStreamReader is the push parser (push(chunk) returns the grids a chunk completed). Hostile input is
bounded: maxGridBytes and maxNameBytes cap what a header may claim.
Coarse-first
vdbBakeCoarseFirst(source, options) writes one .nvdb holding [<name>.lod48, <name>]: a coarse preview first, the
full grid after it. A reader that streams (readNvdbStream, or vdbStreamCoarseFirst in three.js)
shows the preview as soon as its bytes arrive, then swaps in the full grid. The preview filter defaults to
'trilinear' (D26); keep 'box' for thin or high-frequency data.
It takes segments too (segment layout). With the default 'one' the stream knows the full grid's
size, type and voxel size before the preview's data. With 'per-grid' native index reads work and the preview arrives
184 bytes sooner (one preview; 374 with two), but a web stream learns of the full grid only when its segment starts, so
in vdbStreamCoarseFirst:
- the preview is added at its own size, with no room reserved for the full grid: the swap moves the grid to a larger
range (a pool growth and a new bind group, where
'one'swaps in place); voxelRatiois 1 while the preview shows, so aVDBVolumeMaterialwith an explicitdensityScaledraws the preview k times thinner (k: preview voxel / full voxel) until the swap;- the first event's
typeslacks the full grid's type, so aVDBGridNodemade from it needs that type passed by hand (an FpN preview before an Fp8 full grid would otherwise read 0 after the swap).
import { vdbBakeCoarseFirst, nvdbLodName, parseNvdbLodName } from '@pmndrs/vdb/tools';
// One .nvdb holding [<name>.lod48, <name>]: a coarse preview first, the full grid after it.
const file = vdbBakeCoarseFirst(image, { previewResolution: 48, quantize: 'fp8', fullQuantize: 'fpn', codec: 'zip' });
console.log(nvdbLodName('cloud', 48)); // the name of the preview grid
console.log(parseNvdbLodName(nvdbLodName('cloud', 48))); // { baseName: 'cloud', lod: 48 }
CPU reads
vdbReadValueCpu and the sampling functions are the WGSL module's tree descent in TypeScript, so you can check GPU results
or read voxels with no GPU. They take a grid image and index-space coordinates. vdbReadValueCpu is the exact f64 decode;
vdbReadValueF32Cpu matches the shader's f32 arithmetic and is the oracle for GPU parity. They read Float, Fp4, Fp8, Fp16, FpN and Half
grids (and vdbReadValueVec3Cpu / vdbSampleTrilinearVec3Cpu read Vec3f).
import { vdbReadValueCpu, vdbSampleTrilinearCpu } from '@pmndrs/vdb/tools';
// The same tree descent as the WGSL module, in TypeScript: an oracle for GPU results, no GPU needed.
// Index space: the integer coordinates are voxels. (Float, Fp4, Fp8, Fp16, FpN and Half grids.)
console.log(vdbReadValueCpu(image, [10, 20, 30])); // { value, active }
console.log(vdbSampleTrilinearCpu(image, [10.5, 20.25, 30])); // continuous index-space sample
Walking the tree
vdbTreeNodes(image, options) is a generator over what a scalar grid image stores (Float, Half, Fp4, Fp8, Fp16, FpN):
each leaf with its origin and active voxel count, and each tile (a constant block stored in a node's table in place of
a child) with the level of the node that holds it, its origin, size, value and active state. internalNodes: true adds
the upper and lower nodes themselves, each before what it holds. Use it to draw node boxes, to count what a grid
stores, or to tell whether a voxel's value is its own (a leaf) or shared by a block (a tile). Space that no entry covers
reads the background. It does not decode voxel values: read those with vdbReadValueCpu.
import { vdbTreeNodes } from '@pmndrs/vdb/tools';
// Leaves and active tiles, in the image's own order. Each entry covers origin .. origin + size - 1 (index space).
let activeVoxels = 0;
for (const node of vdbTreeNodes(image)) {
if (node.kind === 'leaf') activeVoxels += node.activeVoxelCount; // an 8-voxel block with one value per voxel
else if (node.kind === 'tile') activeVoxels += node.size ** 3; // one value for a block of 8, 128 or 4096 voxels
}
// Boxes to draw: the lower nodes (128 voxels) of a large grid, without visiting its leaves' entries.
const boxes = [];
for (const node of vdbTreeNodes(image, { leaves: false, tiles: 'none', internalNodes: true })) {
if (node.kind === 'lower') boxes.push({ min: node.origin, size: node.size });
}
// A generator: stop whenever you have enough.
// tiles: 'all' also yields inactive tiles (node.active is false): a level set's interior, and every empty slot.
The default yields leaves and active tiles; tiles: 'none' drops the tiles and leaves: false the leaves.
tiles: 'all' yields every table slot without a child. That adds the inactive tiles that mean something (a level set's
interior) and also the empty slots, inactive tiles that hold the background: up to 32,768 per upper node and 4,096 per
lower node, so filter on value. Entries come in the image's order (root entries by signed origin, then each node's
table by slot). The image is checked when the first entry is asked for.
vdbGridStatsCpu and vdbDecodeToAtlasCpu are the CPU versions of the core's compute tools. Every WGSL entry point
also has a CPU twin used as its test oracle (hddaWalk, rayClip, nodeMax, the frame renderer's passes); those live
in the repository's tools internals (src/tools/internal.ts), not in the published API (D32).
Errors
Every error @pmndrs/vdb/tools throws on bad input is a VDBToolsError, so one instanceof covers every format.
Its code says what went wrong:
A failed NvdbFile.fromURL fetch is a plain Error: it is the network, not the input.