/** * Texture-pool primitives behind `SplatMesh`. * * Splat attributes live in pool data textures, a fixed number of texels wide, * and every range the mesh hands out is a whole number of rows in them. This * module holds the parts of that scheme with no dependency on the mesh's own * state: creating the textures, measuring the backend's limits, and the row-span * bookkeeping the allocator runs on. * * {@link SplatPool} is public - a host builds one to let several meshes draw * from a shared memory envelope. A mesh whose packed-SH request is incompatible * with that pool is downgraded to `shBands === 0` rather than escaping the * envelope into a private pool. The rest is internal. */ import * as THREE from 'three/webgpu'; /** * Texels per row in every pool data texture. * * A range is row-aligned, so this also sets the allocation granularity: a * 10-splat chunk still occupies a whole 2048-texel row. Wider rows waste more * on small chunks; narrower rows need more rows than a backend may allow. */ export declare const SPLAT_DATA_TEXTURE_WIDTH = 2048; /** A free span of rows in the pool, as a row index and a row count. */ export interface RowSpan { start: number; count: number; } /** * Takes `rowCount` contiguous rows out of `spans`, best-fit. * * Best-fit rather than first-fit: a chunk pool churns ranges of very different * sizes, and spending the smallest span that fits keeps the large ones intact * for the large chunks that have nowhere else to go. * * Mutates `spans` in place. Throws when nothing contiguous is left, which is * the caller's cue to compact. * * @param poolRows - Total rows in the pool; named in the failure only. * @returns The starting row of the allocated span. */ export declare function allocateRowSpan(spans: RowSpan[], rowCount: number, poolRows: number): number; /** * Returns a span to the free list, coalescing it with any neighbours. * * Merging on release is what keeps {@link allocateRowSpan} able to satisfy a * large request after many small ranges have come and gone; without it the * free list fragments into unusable slivers. * * @returns The new free list, sorted by start row. */ export declare function releaseRowSpan(spans: RowSpan[], start: number, count: number): RowSpan[]; /** CPU-side mirror of the pool textures, kept for partial uploads. */ export interface SplatPoolBacking { centers: Float32Array; colors: Uint8Array; covarianceA: Float32Array; covarianceB: Float32Array; /** Per-splat packed SH; texture t holds coefficients 4t..4t+3. */ shPacked: Uint32Array[]; } /** A tenant's allocation in the pool, as a row index and a row count. */ export interface SplatPoolRange { startRow: number; rowCount: number; } /** * A mesh that draws from a {@link SplatPool}. * * Only compaction needs this: packing the pool relocates rows belonging to * every tenant, so each must be able to enumerate its ranges, follow them to * their new rows, and rebuild whatever it keyed by pool index. */ export interface SplatPoolTenant { /** The tenant's current allocations. */ poolRanges(): Iterable; /** * Adopts a new start row for one of this tenant's ranges. The pool has * already moved the splat data; the tenant moves anything else keyed by pool * row (per-splat channels) and records that those rows need re-uploading. */ relocatePoolRange(range: SplatPoolRange, targetRow: number, movedOnGpu?: boolean): void; /** * True when the GPU textures already hold this tenant's current data for * these rows (nothing pending upload or held for a worker publication), so * compaction may move them on the GPU. Omitted means never. */ poolRowsResidentOnGpu?(startRow: number, rowCount: number): boolean; /** Called once after a compaction, so the tenant can rebuild draw state. */ onPoolCompacted(): void; /** * Optionally frees up to `rows` rows this tenant holds beyond its own share * (cached detail it is not drawing), so a sibling can page in. Returns the * rows released. See {@link SplatPool.reclaimRows}. */ shedPoolRows?(rows: number): number; } /** Options for {@link SplatPool}. */ export interface SplatPoolOptions { /** Splats the pool must hold; rounded up to whole rows. */ capacity: number; /** * Texture precision for centers and covarianceA. `'float16'` halves their * VRAM; covarianceB stays float32 either way because it packs integer IDs * (SOG palette label, `.rad` frontier parent) that halves cannot represent * exactly above 2048. */ floatTextures?: 'float32' | 'float16'; /** Bands of per-splat (non-palette) SH to allocate for; 0 disables. */ packedShBands?: 0 | 1 | 2 | 3; /** * With `packedShBands` 0, the first tenant that requests packed SH * allocates the SH textures then: for a scene pool created before its main * capture's bands are known. */ allowLatePackedSh?: boolean; /** Packed-SH coefficient count for `packedShBands`, supplied by the caller * so this module stays free of the SH packing tables. */ packedShTextureCount?: number; /** * The device's `maxTextureDimension2D`, so a pool too tall for it fails here * rather than at first draw. Pass {@link deviceMaxTextureSize}. * * `SplatMesh.update` also checks this, but only once and only if it is ever * reached: a pool created past the limit and drawn through a path that * swallows that first throw renders forever against invalid textures, which * surfaces as an unreadable cascade of WebGPU "invalid due to a previous * error" validation failures and a black canvas. Checking at construction is * both earlier (before the allocation) and unskippable. * * Omitted or 0 skips the check, so a caller that cannot read the limit is not * falsely rejected. */ maxTextureSize?: number; } /** * The splat storage a mesh draws from: the data textures, their CPU backing, * and the row allocator that hands out space in them. * * Split out of `SplatMesh` because storage and drawing have different * lifetimes and, ultimately, different owners. A mesh's *draw identity* is * already independent of where its splats sit - `splatIndex` maps each * instance to a pool index and the sorter rewrites it freely - so the pool * behind those indices is a separate concern, and one pool can in principle * back several meshes (each keeping its own active list of pool indices). * * Allocation is row-aligned and best-fit; see {@link allocateRowSpan}. */ export declare class SplatPool { readonly width = 2048; readonly rows: number; readonly floatTextures: 'float32' | 'float16'; readonly packedShBands: 0 | 1 | 2 | 3; readonly backing: SplatPoolBacking; readonly centersTexture: THREE.DataTexture; readonly colorsTexture: THREE.DataTexture; readonly covarianceATexture: THREE.DataTexture; readonly covarianceBTexture: THREE.DataTexture; readonly shPackedTextures: readonly THREE.DataTexture[]; /** Free row spans, sorted by start row. Mutated in place by the allocator. */ freeRowSpans: RowSpan[]; private readonly tenants; /** * Pool index → its owner's packed active-list slot, valid only while that * pool index is active. * * Pool-owned rather than per-mesh because it is keyed by *pool* index: a * per-mesh copy would have to span the whole pool, so N meshes sharing one * pool would each pay 4 B per pool splat (a 4 M-splat pool shared by 13 * meshes = ~200 MB of reverse maps). One array is safe because a row belongs * to exactly one tenant at a time, so no two tenants ever write the same * entry - the slot numbers in it are simply read back by whoever owns * the row. */ private activeSlotByPoolIndexValue; private poolIndexTemplate; private cpuMirrorsReleasedValue; private lateShAllowed; constructor(options: SplatPoolOptions); /** Pool index → packed active-list slot. */ get activeSlotByPoolIndex(): Uint32Array; /** * A reusable `0, 1, 2, …` ramp over the pool, used to fill runs of pool * indices and active-list slots without a per-element loop. Pool-owned so * tenants sharing a pool share the one copy. */ indexTemplate(): Uint32Array; /** Splats the pool holds, always a whole number of rows. */ get capacity(): number; /** Whether `texture` belongs to this pool (and so outlives any one tenant). */ isPoolTexture(texture: THREE.Texture): boolean; /** The four core textures, in the order the upload path indexes them. */ get coreTextures(): readonly THREE.DataTexture[]; /** Rows still free, whether or not they are contiguous. */ get freeRows(): number; /** Takes `rowCount` contiguous rows; throws when none are left (compact cue). */ allocateRows(rowCount: number): number; /** Returns rows to the free list, coalescing neighbours. */ releaseRows(start: number, count: number): void; /** Resets the free list to one span covering the whole pool. */ resetFreeRows(fromRow?: number): void; /** * Adds a mesh that draws from this pool. Registration exists for * {@link compact}: packing the pool moves rows that belong to *every* * tenant, so each has to be told where its ranges went. */ register(tenant: SplatPoolTenant): void; /** * Drops a tenant. Release its ranges first: an unregistered tenant is * invisible to {@link compact}, which would then treat rows it still holds * as free and hand them to someone else. `compact` throws rather than let * that happen silently. */ unregister(tenant: SplatPoolTenant): void; /** * Asks the other tenants to shed rows they hold beyond their share until * `rows` rows are free. Returns the free rows afterwards. */ reclaimRows(requester: SplatPoolTenant, rows: number): number; /** Number of meshes drawing from this pool. */ get tenantCount(): number; /** * Packs every tenant's ranges toward row 0, removing the gaps that add / * remove churn leaves behind and restoring a single contiguous free span. * Callers reach this when {@link allocateRows} throws despite enough total * free rows - row-alignment fragmentation. * * The CPU backing arrays are authoritative, so nothing is re-fetched; moved * rows are re-uploaded on the tenant's next update. * * Note this is a whole-pool stall: with several tenants, one mesh's * fragmentation relocates the others' rows too, and every tenant rebuilds * its draw state. (Spark sidesteps the equivalent by allocating fixed-size, * interchangeable pages that never need packing; vlam's ranges are variable * runs, so it packs instead.) */ compact(renderer?: THREE.WebGPURenderer | null): void; dispose(): void; } export declare function createDataTexture(data: Float32Array | Uint8Array | Uint16Array, width: number, height: number, type: THREE.TextureDataType): THREE.DataTexture; /** * An RGBA32UI data texture: four raw `uint32` per texel, delivered to the * shader as a `uvec4` with no filtering or normalization. Packed SH words go * through this rather than a float texture so they survive bit-exact. */ export declare function createIntegerDataTexture(data: Uint32Array, width: number, height: number): THREE.DataTexture; /** * The renderer backend's maximum 2D texture dimension, or 0 when it can't be * read (in which case callers skip any limit check rather than falsely reject). * WebGPU exposes `device.limits.maxTextureDimension2D`; WebGL2 exposes * `gl.MAX_TEXTURE_SIZE`. */ export declare function deviceMaxTextureSize(renderer: THREE.WebGPURenderer): number; /** * Throws when a pool's data textures would be taller than the device allows. * * `maxTextureSize <= 0` means "limit unknown" and skips the check rather than * risk a false rejection; see {@link deviceMaxTextureSize}. * * @param rows - Rows the pool needs. * @param maxTextureSize - The device's `maxTextureDimension2D`. * @param width - Texels per row, for the splat count in the message. */ export declare function assertPoolRowsFitDevice(rows: number, maxTextureSize: number, width?: number): void; /** * Three's WebGPU backend submits one `queue.writeBuffer` per update range. * Merge touching mutations here so a region-atomic commit does not turn a * handful of contiguous backfills into several main-thread submissions. */ export declare function addMergedUpdateRange(attribute: THREE.BufferAttribute, start: number, count: number): void;