/** * Frees the JS-heap mirror of storage buffers only the GPU ever reads. * * ## The gap * * Every `THREE.StorageBufferAttribute` is constructed around a JS typed array, * and three's WebGPU backend never releases it. `WebGPUAttributeUtils`'s * `createAttribute` copies the array into a `mappedAtCreation` GPU buffer, * unmaps it, stores the buffer - and never calls `bufferAttribute * .onUploadCallback()`, the hook that exists for exactly this and that the * legacy WebGL renderer does call (`WebGLAttributes.js`). So a buffer of N * slots costs N bytes on the GPU *and the same again on the JS heap, * permanently*, even when nothing ever reads the CPU copy back. * * For VLAM that is not a rounding error. The unified work buffer alone is * 72 B/slot, gathered entirely on the GPU and never read back - at a 10M-slot * scene that is ~720 MB of JS heap holding zeroes, next to the ~720 MB of GPU * memory doing the actual work. Add the sort scratch and it is most of a * gigabyte that no on-screen number accounts for. * * ## Why replacing `.array` is safe * * Four properties of three 0.185 hold simultaneously, and this module is only * correct while they all do: * * 1. `createAttribute` is guarded by `if (buffer === undefined)` - once the GPU * buffer exists, every later call short-circuits without reading `.array`. * 2. `updateAttribute` *does* re-read `.array`, but `Attributes.update` only * reaches it when `data.version < attribute.version` or the attribute uses * `DynamicDrawUsage`. VLAM sets neither on the buffers released here - which * is what {@link StorageMirrorReleaser}'s drift guard verifies at runtime. * 3. The vertex-layout paths read `.array.constructor` and * `.array.BYTES_PER_ELEMENT`, never the contents - and only for *geometry* * attributes, which these are not. Both survive a zero-length array of the * same constructor. * 4. Device loss is terminal: `Renderer._onDeviceLost` sets a flag and every * later render/compute returns early. There is no re-upload-from-`.array` * path to break. * * A zero-length array rather than a detached buffer, deliberately: if a future * change ever does bump `version` on a released attribute, a zero-length array * degrades to a legal 0-byte `writeBuffer` (silent, and caught by the drift * guard) where a detached one would throw inside the render loop. * * Peak memory is unchanged - the array has to exist for the upload copy. Only * steady state improves, which is the number that was hurting. * * ## Relationship to `releaseRendererAttributes` * * They are complements, and compose in either order. * `releaseRendererAttributes` frees the **GPU** buffer at dispose; this frees * the **CPU** mirror during the first frame. If dispose somehow ran first, the * upload probe simply reports "not uploaded" and the mirror is never released - * fail-safe. * * Upstream fix: a one-line `bufferAttribute.onUploadCallback()` in * `WebGPUAttributeUtils.createAttribute` would make the documented * `BufferAttribute.onUpload()` contract work on WebGPU, and this module would * become a shim for older three. */ import type * as THREE from 'three/webgpu'; /** * Drops the JS mirrors of storage attributes three has already uploaded. * * Owners construct one over the attributes they own **outright** and call * {@link release} after each dispatch; it settles on the first frame and costs * one WeakMap probe per pending attribute until then. Attributes the owner only * borrows (a mesh's `splatIndex`/`sourceIndex` passed into a sorter) must never * be handed to it - the same ownership line `releaseRendererAttributes` draws. * * Only pass attributes that are written from the CPU *at most once*, at * construction. Anything the CPU rewrites per frame will silently stop * uploading; the drift guard turns that into a warning rather than a mystery. */ export declare class StorageMirrorReleaser { private readonly pending; /** Released attributes and the `version` they carried when released. */ private readonly released; private releasedBytesValue; private warnedDrift; constructor(attributes: readonly THREE.BufferAttribute[]); /** Nothing left to release. */ get settled(): boolean; /** Bytes still mirrored on the JS heap. */ get pendingBytes(): number; /** Bytes freed so far. Diagnostic. */ get releasedBytes(): number; /** * Releases every pending mirror three has uploaded, and checks the ones * already released for version drift. Idempotent and cheap once settled. * * @returns Bytes freed by this call. */ release(renderer: THREE.WebGPURenderer): number; /** * A released attribute whose `version` moved means someone added a CPU write * to a buffer this class promised was GPU-only. Three would then upload the * zero-length array - a legal no-op - and the data would simply never reach * the GPU. Warn once, so that failure is visible rather than a silent wrong * render. */ private checkDrift; }