/** * One CPU-sorted draw over several splat sources that share a {@link SplatPool}. * * {@link UnifiedSplatMesh} needs compute passes (gather + GPU sort), so it is * WebGPU-only. This is the WebGL2 answer to the same artifact: two meshes * drawn separately are two transparent draws, each sorted only within itself, * so whichever draws last paints wholesale over the other. Here every source * is a tenant of **one** pool, so a single draw can index all of their rows; * the sources' exact active lists are concatenated into one worker sort keyed * by each source's own world matrix, and the result is drawn as one instanced * mesh whose `splatIndex` / `sourceId` attributes come straight from the * worker. No data is gathered or copied: the draw reads the pool textures the * sources already filled. * * Publication keeps the worker-path boundary of `SplatMesh`: every source's * pending texture rows are captured together with the combined index list, * and the rows upload only when the matching order comes back. Until then the * previous complete frame stays visible. Sources keep streaming meanwhile; * their later changes go into the next capture. * * Works on either backend (a WebGPU host would normally prefer the compute * path). Sources must be `editable` meshes drawing from the same pool. * Effects: each source's own stack ({@link setSourceModifiers}) runs in that * source's local frame, as the compute path's gather does; the mesh's own * {@link modifiers} then run on every splat in the drawn (world) frame; then * per-source opacity. Placement, LOD alpha and packed-SH range are per source. * * Several views that share one camera (split panes) draw different subsets of * the same sorted set through {@link setSourceDrawMask}: membership stays the * union, and the mask is a uniform write that leaves the order alone. */ import * as THREE from 'three/webgpu'; import type { WebGLRenderer } from 'three'; import { SplatMesh, type SplatPickOptions } from '../../core/splat-mesh'; import type { SplatSortMetric } from '../../core/splat-mesh-types'; import { SplatPool } from '../../core/splat-mesh-pool'; import { type DisplayColorModifier } from '../../core/splat-mesh-material'; import { type DepthOfFieldSettings } from '../../core/depth-of-field'; import type { SplatModifier } from '../../core/splat-modifier'; import type { UnifiedDrawPublicationSnapshot, UnifiedSplatPickResult, UnifiedSplatSourceOptions } from '../unified-splat-mesh'; /** Construction options for a {@link PooledUnifiedSplatMesh}. */ export interface PooledUnifiedSplatMeshOptions { /** Camera-space ordering key for the combined sort. Default `'depth'`. */ sortMetric?: SplatSortMetric; /** * Maximum registered sources. Default 64; at most 256 (one-byte source * ids). Shader tables are sized to the live source count, not to this. */ maxSources?: number; /** Whether sources composite in display (sRGB) space; every source must agree. Default `false`. */ srgbOutput?: boolean; /** Minimum interval between worker sorts under camera motion, ms. Default adaptive. */ sortIntervalMs?: number; } /** Registration settings for one source; the compute path's, plus {@link PooledUnifiedSourceOptions.liveContent}. */ export type PooledUnifiedSourceOptions = Pick & { /** * For a source that rewrites the same splats in fixed slots every frame * (a `SplatClipMesh` on its frame path): its rows upload in the frame they * are written, drawn through the last installed order, instead of waiting * for a combined sort that includes them. The sort keeps running in the * background and reads the source's centers from the pool, so its depth * order against the other sources trails its content by one sort (a * fraction of a second on a large scene) instead of holding its motion. * * Only content may change this way: a change to the source's active list * still waits for a sort. Default `false`. * * @experimental */ liveContent?: boolean; }; /** * WebGL2 resource use of the pooled draw's vertex program, against the * device's limits. Uniform arrays become std140 blocks there, and a vertex * stage may bind as few as 12 of them; a program over a limit fails to link * and draws nothing, so hosts check this and fall back. */ export interface PooledShaderBudget { vertexUniformBlocks: number; maxVertexUniformBlocks: number; vertexSamplers: number; maxVertexSamplers: number; /** Material build this report describes. */ materialRevision: number; ok: boolean; } /** CPU timings of the last {@link PooledUnifiedSplatMesh.update}. */ export interface PooledUnifiedPerformanceTimings { totalMs: number; sortSubmitMs: number; sortSubmitted: boolean; sortSubmissions: number; sourceCount: number; /** Splats in the last captured sort request. */ activeCount: number; /** Splats currently drawn (last installed order). */ instanceCount: number; /** Worker round trip of the last completed sort, ms. */ lastSortLatencyMs: number; } /** * Whether `renderer` can drive a {@link PooledUnifiedSplatMesh}: any * initialized three `WebGPURenderer` backend, WebGL2 included. * * @experimental May change in a minor release. */ export declare function supportsPooledUnifiedSplatMesh(renderer: object): boolean; /** * One pool, one worker sort, one draw, dynamic tenants - see the module * overview. Register sources that already draw from `pool`; the pooled mesh * hides them, owns their publication, and draws their rows itself. * * @experimental May change in a minor release. */ export declare class PooledUnifiedSplatMesh extends THREE.Mesh { /** * Brand shared by every VLAM splat draw (`SplatMesh` and its subclasses, and * both unified meshes), for code that must find them in a scene without * importing each class. */ readonly isSplatDraw: true; /** Type marker for hosts that cannot import this module eagerly. */ readonly isPooledUnifiedSplatMesh: true; readonly pool: SplatPool; private readonly renderer; private readonly sortMetric; private readonly maxSources; private readonly srgbOutput; /** Packed per-source tables; replaced (and the material rebuilt) when they grow. */ private tables; private readonly splatIndexAttribute; private readonly sourceIdAttribute; private readonly focal; private readonly viewport; private readonly frustumMargin; private readonly localCameraPosition; private readonly pixelScaleLimit; private readonly revealMultiplier; private readonly dofFocusDistance; private readonly dofAperture; private readonly screenBandMin; private readonly screenBandMax; private readonly sortScheduler; private readonly bounds; private readonly lastSortedState; private readonly sources; private readonly freeIds; private nextId; /** Ids freed since the last installed order; reusable once that order is replaced. */ private quarantinedIds; private sorter; private inFlight; private pending; private installed; private membership; private publicationRevision; private renderedPublicationRevision; private lastCaptureCount; /** * What the worker's retained index list describes: the drawn records, in * order, with the versions they were captured at. A camera-only re-sort of * the same list sends no indices at all. */ private retainedParts; private pendingMirrorRows; /** Undrawn sources published outside the worker cycle, awaiting a rendered-frame acknowledgement. */ private renderAcks; private modifierList; private displayColorModifierValue; private settings; private materialBuilt; /** A structural change (stacks, membership, table size) awaits the next update. */ private materialDirty; private materialRevision; private shaderBudgetValue; /** Pool SH bands the material was built for; a late allocation rebuilds it. */ private builtShBands; private disposed; private readonly performanceTimingsValue; constructor(renderer: THREE.WebGPURenderer, pool: SplatPool, options?: PooledUnifiedSplatMeshOptions); /** Splats the shared pool can hold; the combined draw never exceeds it. */ get capacity(): number; /** Number of registered sources. */ get sourceCount(): number; /** Parity with `UnifiedSplatMesh`: a pooled draw never drops a source. */ get droppedSourceCount(): number; /** Parity with `UnifiedSplatMesh`: a pooled draw never drops splats. */ get droppedSplatCount(): number; /** CPU timings of the last update. */ get performanceTimings(): Readonly; /** The last {@link checkShaderBudget} report, or `null` before one ran. */ get shaderBudget(): Readonly | null; /** Increments on every material build; compare with {@link shaderBudget}. */ get shaderRevision(): number; /** * Sets the stack `source` draws through, evaluated in that source's local * frame (pass the source mesh's own `modifiers`). Element-wise identical * lists are a no-op; a change rebuilds the material on the next update. * Uniform-only changes inside the modifiers never rebuild. */ setSourceModifiers(source: SplatMesh, modifiers: readonly SplatModifier[]): void; /** * Counts the uniform blocks and samplers the pooled vertex program binds on * WebGL2 and compares them with the device's limits. Resolves `null` on a * WebGPU backend (bind-group limits apply there instead). Compiles the * current material if needed, so call it after a structural change rather * than every frame; {@link shaderBudget} keeps the last report. */ checkShaderBudget(camera: THREE.Camera): Promise; /** * The one modifier stack applied to every drawn splat. Sources' own stacks * are not evaluated by this draw; `ctx.sourceCenter` is the pool-local * center and `ctx.localCenter` the placed world center. Modifiers reading * per-splat channels are not supported here (the pooled mesh defines none). */ get modifiers(): readonly SplatModifier[]; set modifiers(value: readonly SplatModifier[]); /** Optional RGB transform in the display fragment; replacing it rebuilds the material. */ get displayColorModifier(): DisplayColorModifier | null; set displayColorModifier(modifier: DisplayColorModifier | null); /** Core projected-2D depth of field on the pooled draw. Live uniforms. */ setDepthOfField(settings: Partial): void; /** Current core DoF uniforms (`aperture === 0` means off). */ getDepthOfField(): DepthOfFieldSettings; /** * Registers a source drawing from this mesh's pool. The source is hidden * from the regular scene draw and hands its worker publication to this mesh. * * @throws when the source draws from another pool, is render-only or a * `MergedSplatMesh`, disagrees with the first source's draw settings, or * the source table is full. */ addSource(source: SplatMesh, options?: PooledUnifiedSourceOptions): void; /** Unregisters a source and restores its standalone draw. */ removeSource(source: SplatMesh): boolean; /** * Whole-source display opacity. Values `<= 0` skip the source's splats in * the draw at once (uniform), without waiting for a new order. */ setSourceOpacity(source: SplatMesh, opacity: number): void; /** Shows or hides a source without changing its residency. */ setSourceVisible(source: SplatMesh, visible: boolean): void; /** * Draws or skips a source in the next draw **without** changing membership. * Unlike {@link setSourceVisible} and {@link setSourceOpacity}, the installed * order and any in-flight sort survive: the mask is one uniform write and * takes effect on the next render. Use it to draw different subsets of one * sorted set, for example two split panes that share the camera (keep the * union visible, flip the mask before each pane). Masked sources are not * picked. Defaults to drawn; the mask is dropped with the source. */ setSourceDrawMask(source: SplatMesh, drawn: boolean): void; /** Updates, captures, publishes and requests a combined sort. Call before rendering. */ update(camera: THREE.PerspectiveCamera): void; /** * Draws the current combined order into `target` from another camera. The * worker order is asynchronous, so a secondary view draws through the * primary order rather than re-sorting (the documented WebGL2 behaviour of * standalone meshes). Returns `false` when nothing is published yet. */ renderView(camera: THREE.PerspectiveCamera, renderer: THREE.WebGPURenderer, target?: THREE.RenderTarget | null): boolean; /** * Picks the frontmost visible splat under an NDC coordinate across every * drawn source, through each source's own depth-tested pick pass. */ pick(ndc: THREE.Vector2, camera: THREE.Camera, options?: SplatPickOptions): Promise; /** Whether a primary draw has rendered the latest installed publication. */ getDrawPublicationSnapshot(): UnifiedDrawPublicationSnapshot; onAfterRender(_renderer: WebGLRenderer, _scene: THREE.Scene, _camera: THREE.Camera): void; /** Releases every source back to its standalone draw and frees the draw resources. */ dispose(): void; private assertNotDisposed; private recordOf; private allocateId; /** Camera position in every source's local frame, for source stacks. */ private writeSourceCameras; private detachRecord; /** * Membership (which sources' splats belong in the combined list) changed: * an in-flight order would draw a stale set, and a pending one too. */ private noteMembershipChange; /** Captures and publishes a source that contributes nothing to the draw list. */ private publishUndrawn; /** * Uploads a live source's pending rows outside the worker cycle (see * {@link PooledUnifiedSourceOptions.liveContent}). The caller still marks * content changed, so the next sort re-sorts with these centers; a paused * source then stops re-sorting. */ private publishLive; private writeShRange; private writeViewUniforms; private captureView; private requestSortIfNeeded; /** * One combined capture across every drawn source: each source's pending rows * plus, unless the worker already holds the identical list, the concatenated * active indices and their source ids. */ private capture; private nextRequestId; private onOrderReady; private onSortFailure; /** Uploads every captured source row, then installs the matching order. */ private publishPending; private installOrder; private rebuildMaterial; }