@pmndrs/vdb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +137 -0
- package/LICENSE +21 -0
- package/README.md +448 -2
- package/THIRD_PARTY_NOTICES.md +147 -0
- package/dist/core/compute/atlas.d.ts +94 -0
- package/dist/core/compute/atlas.js +98 -0
- package/dist/core/compute/atlas.js.map +1 -0
- package/dist/core/compute/index.d.ts +13 -0
- package/dist/core/compute/index.js +9 -0
- package/dist/core/compute/index.js.map +1 -0
- package/dist/core/compute/pyramid.d.ts +105 -0
- package/dist/core/compute/pyramid.js +455 -0
- package/dist/core/compute/pyramid.js.map +1 -0
- package/dist/core/compute/shared.d.ts +115 -0
- package/dist/core/compute/shared.js +211 -0
- package/dist/core/compute/shared.js.map +1 -0
- package/dist/core/compute/stats.d.ts +71 -0
- package/dist/core/compute/stats.js +188 -0
- package/dist/core/compute/stats.js.map +1 -0
- package/dist/core/compute/value-transform.d.ts +23 -0
- package/dist/core/compute/value-transform.js +58 -0
- package/dist/core/compute/value-transform.js.map +1 -0
- package/dist/core/errors.d.ts +11 -0
- package/dist/core/errors.js +9 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +30 -0
- package/dist/core/index.js +27 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/internal.d.ts +15 -0
- package/dist/core/internal.js +15 -0
- package/dist/core/internal.js.map +1 -0
- package/dist/core/pool/affine.d.ts +34 -0
- package/dist/core/pool/affine.js +100 -0
- package/dist/core/pool/affine.js.map +1 -0
- package/dist/core/pool/allocator.d.ts +32 -0
- package/dist/core/pool/allocator.js +81 -0
- package/dist/core/pool/allocator.js.map +1 -0
- package/dist/core/pool/image-header.d.ts +82 -0
- package/dist/core/pool/image-header.js +175 -0
- package/dist/core/pool/image-header.js.map +1 -0
- package/dist/core/pool/layout.d.ts +164 -0
- package/dist/core/pool/layout.js +267 -0
- package/dist/core/pool/layout.js.map +1 -0
- package/dist/core/pool/limits.d.ts +40 -0
- package/dist/core/pool/limits.js +79 -0
- package/dist/core/pool/limits.js.map +1 -0
- package/dist/core/pool/pool.d.ts +279 -0
- package/dist/core/pool/pool.js +953 -0
- package/dist/core/pool/pool.js.map +1 -0
- package/dist/core/source/dense.d.ts +19 -0
- package/dist/core/source/dense.js +287 -0
- package/dist/core/source/dense.js.map +1 -0
- package/dist/core/source/types.d.ts +63 -0
- package/dist/core/source/types.js +9 -0
- package/dist/core/source/types.js.map +1 -0
- package/dist/core/types.d.ts +28 -0
- package/dist/core/types.js +28 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/wgsl/api.d.ts +41 -0
- package/dist/core/wgsl/api.js +463 -0
- package/dist/core/wgsl/api.js.map +1 -0
- package/dist/core/wgsl/generate.d.ts +82 -0
- package/dist/core/wgsl/generate.js +505 -0
- package/dist/core/wgsl/generate.js.map +1 -0
- package/dist/core/wgsl/level-set.d.ts +54 -0
- package/dist/core/wgsl/level-set.js +194 -0
- package/dist/core/wgsl/level-set.js.map +1 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +14 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.js +330 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -0
- package/dist/core/wgsl/reserved.d.ts +9 -0
- package/dist/core/wgsl/reserved.js +26 -0
- package/dist/core/wgsl/reserved.js.map +1 -0
- package/dist/core/wgsl/types.d.ts +104 -0
- package/dist/core/wgsl/types.js +2 -0
- package/dist/core/wgsl/types.js.map +1 -0
- package/dist/three/coarse-first.d.ts +162 -0
- package/dist/three/coarse-first.js +187 -0
- package/dist/three/coarse-first.js.map +1 -0
- package/dist/three/compute.d.ts +63 -0
- package/dist/three/compute.js +56 -0
- package/dist/three/compute.js.map +1 -0
- package/dist/three/device.d.ts +59 -0
- package/dist/three/device.js +59 -0
- package/dist/three/device.js.map +1 -0
- package/dist/three/frame/blackbody.d.ts +42 -0
- package/dist/three/frame/blackbody.js +74 -0
- package/dist/three/frame/blackbody.js.map +1 -0
- package/dist/three/frame/chunks.d.ts +157 -0
- package/dist/three/frame/chunks.js +559 -0
- package/dist/three/frame/chunks.js.map +1 -0
- package/dist/three/frame/density-material.d.ts +53 -0
- package/dist/three/frame/density-material.js +353 -0
- package/dist/three/frame/density-material.js.map +1 -0
- package/dist/three/frame/emission-light.d.ts +167 -0
- package/dist/three/frame/emission-light.js +655 -0
- package/dist/three/frame/emission-light.js.map +1 -0
- package/dist/three/frame/environment-grid-material.d.ts +21 -0
- package/dist/three/frame/environment-grid-material.js +184 -0
- package/dist/three/frame/environment-grid-material.js.map +1 -0
- package/dist/three/frame/environment-material.d.ts +22 -0
- package/dist/three/frame/environment-material.js +87 -0
- package/dist/three/frame/environment-material.js.map +1 -0
- package/dist/three/frame/grid-mip-material.d.ts +12 -0
- package/dist/three/frame/grid-mip-material.js +61 -0
- package/dist/three/frame/grid-mip-material.js.map +1 -0
- package/dist/three/frame/index.d.ts +39 -0
- package/dist/three/frame/index.js +35 -0
- package/dist/three/frame/index.js.map +1 -0
- package/dist/three/frame/inject-material.d.ts +56 -0
- package/dist/three/frame/inject-material.js +424 -0
- package/dist/three/frame/inject-material.js.map +1 -0
- package/dist/three/frame/integrate-material.d.ts +26 -0
- package/dist/three/frame/integrate-material.js +198 -0
- package/dist/three/frame/integrate-material.js.map +1 -0
- package/dist/three/frame/layout.d.ts +34 -0
- package/dist/three/frame/layout.js +48 -0
- package/dist/three/frame/layout.js.map +1 -0
- package/dist/three/frame/lighting-material.d.ts +41 -0
- package/dist/three/frame/lighting-material.js +257 -0
- package/dist/three/frame/lighting-material.js.map +1 -0
- package/dist/three/frame/lights.d.ts +11 -0
- package/dist/three/frame/lights.js +16 -0
- package/dist/three/frame/lights.js.map +1 -0
- package/dist/three/frame/medium.d.ts +21 -0
- package/dist/three/frame/medium.js +29 -0
- package/dist/three/frame/medium.js.map +1 -0
- package/dist/three/frame/neural.d.ts +14 -0
- package/dist/three/frame/neural.js +105 -0
- package/dist/three/frame/neural.js.map +1 -0
- package/dist/three/frame/phase.d.ts +44 -0
- package/dist/three/frame/phase.js +80 -0
- package/dist/three/frame/phase.js.map +1 -0
- package/dist/three/frame/renderer.d.ts +493 -0
- package/dist/three/frame/renderer.js +1498 -0
- package/dist/three/frame/renderer.js.map +1 -0
- package/dist/three/frame/shadow-density-material.d.ts +30 -0
- package/dist/three/frame/shadow-density-material.js +123 -0
- package/dist/three/frame/shadow-density-material.js.map +1 -0
- package/dist/three/frame/spectrum.d.ts +18 -0
- package/dist/three/frame/spectrum.js +36 -0
- package/dist/three/frame/spectrum.js.map +1 -0
- package/dist/three/frame/temporal-material.d.ts +29 -0
- package/dist/three/frame/temporal-material.js +152 -0
- package/dist/three/frame/temporal-material.js.map +1 -0
- package/dist/three/frame/three-internals.d.ts +65 -0
- package/dist/three/frame/three-internals.js +150 -0
- package/dist/three/frame/three-internals.js.map +1 -0
- package/dist/three/frame/transmittance-material.d.ts +31 -0
- package/dist/three/frame/transmittance-material.js +315 -0
- package/dist/three/frame/transmittance-material.js.map +1 -0
- package/dist/three/frame/view.d.ts +103 -0
- package/dist/three/frame/view.js +296 -0
- package/dist/three/frame/view.js.map +1 -0
- package/dist/three/frame/volume-shadow-material.d.ts +36 -0
- package/dist/three/frame/volume-shadow-material.js +128 -0
- package/dist/three/frame/volume-shadow-material.js.map +1 -0
- package/dist/three/frame/volume-shadows.d.ts +153 -0
- package/dist/three/frame/volume-shadows.js +756 -0
- package/dist/three/frame/volume-shadows.js.map +1 -0
- package/dist/three/frame/volume.d.ts +172 -0
- package/dist/three/frame/volume.js +256 -0
- package/dist/three/frame/volume.js.map +1 -0
- package/dist/three/grid-node.d.ts +196 -0
- package/dist/three/grid-node.js +510 -0
- package/dist/three/grid-node.js.map +1 -0
- package/dist/three/index.d.ts +43 -0
- package/dist/three/index.js +49 -0
- package/dist/three/index.js.map +1 -0
- package/dist/three/internal.d.ts +14 -0
- package/dist/three/internal.js +14 -0
- package/dist/three/internal.js.map +1 -0
- package/dist/three/level-set-material.d.ts +112 -0
- package/dist/three/level-set-material.js +204 -0
- package/dist/three/level-set-material.js.map +1 -0
- package/dist/three/material.d.ts +194 -0
- package/dist/three/material.js +359 -0
- package/dist/three/material.js.map +1 -0
- package/dist/three/points.d.ts +84 -0
- package/dist/three/points.js +130 -0
- package/dist/three/points.js.map +1 -0
- package/dist/three/sequence.d.ts +317 -0
- package/dist/three/sequence.js +526 -0
- package/dist/three/sequence.js.map +1 -0
- package/dist/three/sparse-source.d.ts +233 -0
- package/dist/three/sparse-source.js +876 -0
- package/dist/three/sparse-source.js.map +1 -0
- package/dist/three/volume-source.d.ts +167 -0
- package/dist/three/volume-source.js +353 -0
- package/dist/three/volume-source.js.map +1 -0
- package/dist/tools/cpu/compute.d.ts +97 -0
- package/dist/tools/cpu/compute.js +218 -0
- package/dist/tools/cpu/compute.js.map +1 -0
- package/dist/tools/cpu/level-set.d.ts +72 -0
- package/dist/tools/cpu/level-set.js +261 -0
- package/dist/tools/cpu/level-set.js.map +1 -0
- package/dist/tools/cpu/march.d.ts +206 -0
- package/dist/tools/cpu/march.js +575 -0
- package/dist/tools/cpu/march.js.map +1 -0
- package/dist/tools/cpu/probe-coords.d.ts +70 -0
- package/dist/tools/cpu/probe-coords.js +116 -0
- package/dist/tools/cpu/probe-coords.js.map +1 -0
- package/dist/tools/cpu/read-value.d.ts +31 -0
- package/dist/tools/cpu/read-value.js +336 -0
- package/dist/tools/cpu/read-value.js.map +1 -0
- package/dist/tools/cpu/sample-trilinear.d.ts +30 -0
- package/dist/tools/cpu/sample-trilinear.js +156 -0
- package/dist/tools/cpu/sample-trilinear.js.map +1 -0
- package/dist/tools/cpu/stride-tables.d.ts +56 -0
- package/dist/tools/cpu/stride-tables.generated.d.ts +1058 -0
- package/dist/tools/cpu/stride-tables.generated.js +1266 -0
- package/dist/tools/cpu/stride-tables.generated.js.map +1 -0
- package/dist/tools/cpu/stride-tables.js +78 -0
- package/dist/tools/cpu/stride-tables.js.map +1 -0
- package/dist/tools/errors.d.ts +57 -0
- package/dist/tools/errors.js +58 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/frame/blackbody.d.ts +42 -0
- package/dist/tools/frame/blackbody.js +116 -0
- package/dist/tools/frame/blackbody.js.map +1 -0
- package/dist/tools/frame/emission.d.ts +128 -0
- package/dist/tools/frame/emission.js +282 -0
- package/dist/tools/frame/emission.js.map +1 -0
- package/dist/tools/frame/environment.d.ts +105 -0
- package/dist/tools/frame/environment.js +268 -0
- package/dist/tools/frame/environment.js.map +1 -0
- package/dist/tools/frame/grid.d.ts +302 -0
- package/dist/tools/frame/grid.js +703 -0
- package/dist/tools/frame/grid.js.map +1 -0
- package/dist/tools/frame/index.d.ts +136 -0
- package/dist/tools/frame/index.js +348 -0
- package/dist/tools/frame/index.js.map +1 -0
- package/dist/tools/frame/medium.d.ts +61 -0
- package/dist/tools/frame/medium.js +99 -0
- package/dist/tools/frame/medium.js.map +1 -0
- package/dist/tools/frame/mie-table.d.ts +6 -0
- package/dist/tools/frame/mie-table.js +16 -0
- package/dist/tools/frame/mie-table.js.map +1 -0
- package/dist/tools/frame/mie.d.ts +54 -0
- package/dist/tools/frame/mie.js +176 -0
- package/dist/tools/frame/mie.js.map +1 -0
- package/dist/tools/frame/neural-weights.d.ts +9 -0
- package/dist/tools/frame/neural-weights.js +245 -0
- package/dist/tools/frame/neural-weights.js.map +1 -0
- package/dist/tools/frame/neural.d.ts +80 -0
- package/dist/tools/frame/neural.js +150 -0
- package/dist/tools/frame/neural.js.map +1 -0
- package/dist/tools/frame/pyramid-layout.d.ts +38 -0
- package/dist/tools/frame/pyramid-layout.js +53 -0
- package/dist/tools/frame/pyramid-layout.js.map +1 -0
- package/dist/tools/frame/shadows.d.ts +80 -0
- package/dist/tools/frame/shadows.js +182 -0
- package/dist/tools/frame/shadows.js.map +1 -0
- package/dist/tools/frame/sparse.d.ts +65 -0
- package/dist/tools/frame/sparse.js +191 -0
- package/dist/tools/frame/sparse.js.map +1 -0
- package/dist/tools/frame/spectral.d.ts +203 -0
- package/dist/tools/frame/spectral.js +550 -0
- package/dist/tools/frame/spectral.js.map +1 -0
- package/dist/tools/index.d.ts +106 -0
- package/dist/tools/index.js +88 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/internal.d.ts +46 -0
- package/dist/tools/internal.js +31 -0
- package/dist/tools/internal.js.map +1 -0
- package/dist/tools/nanovdb/build-from-vdb.d.ts +50 -0
- package/dist/tools/nanovdb/build-from-vdb.js +92 -0
- package/dist/tools/nanovdb/build-from-vdb.js.map +1 -0
- package/dist/tools/nanovdb/bytes.d.ts +220 -0
- package/dist/tools/nanovdb/bytes.js +345 -0
- package/dist/tools/nanovdb/bytes.js.map +1 -0
- package/dist/tools/nanovdb/coarse-first.d.ts +62 -0
- package/dist/tools/nanovdb/coarse-first.js +86 -0
- package/dist/tools/nanovdb/coarse-first.js.map +1 -0
- package/dist/tools/nanovdb/index.d.ts +32 -0
- package/dist/tools/nanovdb/index.js +24 -0
- package/dist/tools/nanovdb/index.js.map +1 -0
- package/dist/tools/nanovdb/inspect.d.ts +24 -0
- package/dist/tools/nanovdb/inspect.js +63 -0
- package/dist/tools/nanovdb/inspect.js.map +1 -0
- package/dist/tools/nanovdb/leaf-codec.d.ts +85 -0
- package/dist/tools/nanovdb/leaf-codec.js +320 -0
- package/dist/tools/nanovdb/leaf-codec.js.map +1 -0
- package/dist/tools/nanovdb/quantize.d.ts +43 -0
- package/dist/tools/nanovdb/quantize.js +277 -0
- package/dist/tools/nanovdb/quantize.js.map +1 -0
- package/dist/tools/nanovdb/resample.d.ts +160 -0
- package/dist/tools/nanovdb/resample.js +679 -0
- package/dist/tools/nanovdb/resample.js.map +1 -0
- package/dist/tools/nanovdb/serialize.d.ts +145 -0
- package/dist/tools/nanovdb/serialize.js +395 -0
- package/dist/tools/nanovdb/serialize.js.map +1 -0
- package/dist/tools/nanovdb/stats.d.ts +58 -0
- package/dist/tools/nanovdb/stats.js +149 -0
- package/dist/tools/nanovdb/stats.js.map +1 -0
- package/dist/tools/nanovdb/transform.d.ts +26 -0
- package/dist/tools/nanovdb/transform.js +91 -0
- package/dist/tools/nanovdb/transform.js.map +1 -0
- package/dist/tools/nanovdb/tree.d.ts +175 -0
- package/dist/tools/nanovdb/tree.js +518 -0
- package/dist/tools/nanovdb/tree.js.map +1 -0
- package/dist/tools/nanovdb/write-nvdb.d.ts +39 -0
- package/dist/tools/nanovdb/write-nvdb.js +210 -0
- package/dist/tools/nanovdb/write-nvdb.js.map +1 -0
- package/dist/tools/nvdb-file.d.ts +195 -0
- package/dist/tools/nvdb-file.js +606 -0
- package/dist/tools/nvdb-file.js.map +1 -0
- package/dist/tools/nvdb-stream.d.ts +138 -0
- package/dist/tools/nvdb-stream.js +365 -0
- package/dist/tools/nvdb-stream.js.map +1 -0
- package/dist/tools/vdb/bit-utils.d.ts +20 -0
- package/dist/tools/vdb/bit-utils.js +42 -0
- package/dist/tools/vdb/bit-utils.js.map +1 -0
- package/dist/tools/vdb/blosc.d.ts +56 -0
- package/dist/tools/vdb/blosc.js +231 -0
- package/dist/tools/vdb/blosc.js.map +1 -0
- package/dist/tools/vdb/byte-reader.d.ts +40 -0
- package/dist/tools/vdb/byte-reader.js +139 -0
- package/dist/tools/vdb/byte-reader.js.map +1 -0
- package/dist/tools/vdb/compression.d.ts +73 -0
- package/dist/tools/vdb/compression.js +249 -0
- package/dist/tools/vdb/compression.js.map +1 -0
- package/dist/tools/vdb/errors.d.ts +10 -0
- package/dist/tools/vdb/errors.js +11 -0
- package/dist/tools/vdb/errors.js.map +1 -0
- package/dist/tools/vdb/grid.d.ts +37 -0
- package/dist/tools/vdb/grid.js +147 -0
- package/dist/tools/vdb/grid.js.map +1 -0
- package/dist/tools/vdb/half-float.d.ts +9 -0
- package/dist/tools/vdb/half-float.js +24 -0
- package/dist/tools/vdb/half-float.js.map +1 -0
- package/dist/tools/vdb/index.d.ts +3 -0
- package/dist/tools/vdb/index.js +3 -0
- package/dist/tools/vdb/index.js.map +1 -0
- package/dist/tools/vdb/inflate.d.ts +24 -0
- package/dist/tools/vdb/inflate.js +334 -0
- package/dist/tools/vdb/inflate.js.map +1 -0
- package/dist/tools/vdb/metadata.d.ts +17 -0
- package/dist/tools/vdb/metadata.js +72 -0
- package/dist/tools/vdb/metadata.js.map +1 -0
- package/dist/tools/vdb/parse.d.ts +25 -0
- package/dist/tools/vdb/parse.js +171 -0
- package/dist/tools/vdb/parse.js.map +1 -0
- package/dist/tools/vdb/point-render-data.d.ts +31 -0
- package/dist/tools/vdb/point-render-data.js +57 -0
- package/dist/tools/vdb/point-render-data.js.map +1 -0
- package/dist/tools/vdb/points.d.ts +160 -0
- package/dist/tools/vdb/points.js +794 -0
- package/dist/tools/vdb/points.js.map +1 -0
- package/dist/tools/vdb/transform.d.ts +23 -0
- package/dist/tools/vdb/transform.js +54 -0
- package/dist/tools/vdb/transform.js.map +1 -0
- package/dist/tools/vdb/tree.d.ts +111 -0
- package/dist/tools/vdb/tree.js +228 -0
- package/dist/tools/vdb/tree.js.map +1 -0
- package/dist/tools/vdb/types.d.ts +138 -0
- package/dist/tools/vdb/types.js +3 -0
- package/dist/tools/vdb/types.js.map +1 -0
- package/dist/tools/wvdb/bake.d.ts +116 -0
- package/dist/tools/wvdb/bake.js +311 -0
- package/dist/tools/wvdb/bake.js.map +1 -0
- package/dist/tools/wvdb/dense.d.ts +112 -0
- package/dist/tools/wvdb/dense.js +309 -0
- package/dist/tools/wvdb/dense.js.map +1 -0
- package/dist/tools/wvdb/format.d.ts +161 -0
- package/dist/tools/wvdb/format.js +303 -0
- package/dist/tools/wvdb/format.js.map +1 -0
- package/dist/tools/wvdb/index.d.ts +13 -0
- package/dist/tools/wvdb/index.js +14 -0
- package/dist/tools/wvdb/index.js.map +1 -0
- package/dist/tools/wvdb/level-set.d.ts +48 -0
- package/dist/tools/wvdb/level-set.js +153 -0
- package/dist/tools/wvdb/level-set.js.map +1 -0
- package/dist/tools/wvdb/nanovdb.d.ts +54 -0
- package/dist/tools/wvdb/nanovdb.js +60 -0
- package/dist/tools/wvdb/nanovdb.js.map +1 -0
- package/dist/tools/wvdb/read.d.ts +260 -0
- package/dist/tools/wvdb/read.js +743 -0
- package/dist/tools/wvdb/read.js.map +1 -0
- package/dist/tools/wvdb/sha256.d.ts +7 -0
- package/dist/tools/wvdb/sha256.js +76 -0
- package/dist/tools/wvdb/sha256.js.map +1 -0
- package/dist/tools/wvdb/write.d.ts +127 -0
- package/dist/tools/wvdb/write.js +396 -0
- package/dist/tools/wvdb/write.js.map +1 -0
- package/package.json +82 -4
- package/vendor/LICENSE +201 -0
- package/vendor/NOTICE +19 -0
- package/vendor/VENDOR.md +85 -0
- package/vendor/pnanovdb.wgsl +1539 -0
|
@@ -0,0 +1,953 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `VDBGridPool`: many NanoVDB grids in one `GPUBuffer` behind a grid table
|
|
3
|
+
* (docs/dev/design/K1-core.md §1, §4; D8, D10).
|
|
4
|
+
*
|
|
5
|
+
* Model: CPU calls (`add`, `replace`, `remove`, `setTransform`, `setMajorant`)
|
|
6
|
+
* stage changes and return at once; `update(encoder)` applies them. Growth and
|
|
7
|
+
* compaction happen only in `update`, by a new buffer plus copies of the ranges
|
|
8
|
+
* that were resident before that update; `version` bumps then (and when a
|
|
9
|
+
* failed growth rolls back to the previous buffer).
|
|
10
|
+
*
|
|
11
|
+
* Lifecycle (docs/archive/handoffs/K1.md, "K1.3 pool lifecycle"):
|
|
12
|
+
* - A request that could never fit under the byte limit throws `VDB_POOL_LIMIT`
|
|
13
|
+
* at staging, with the pool unchanged.
|
|
14
|
+
* - Growth allocates inside `out-of-memory` and `validation` error scopes. The
|
|
15
|
+
* pool switches to the new buffer at once (update is synchronous) but keeps
|
|
16
|
+
* the old one alive until the scopes resolve; on an error it rolls back to the
|
|
17
|
+
* old buffer and reports through `settled()` / `lastError`.
|
|
18
|
+
* - A retired buffer is destroyed once its growth is confirmed, two `update`s
|
|
19
|
+
* have passed, and `queue.onSubmittedWorkDone()` asked at that point resolves.
|
|
20
|
+
*/
|
|
21
|
+
import { VDBError } from "../errors.js";
|
|
22
|
+
import { applyAffine, composeGridAffines } from "./affine.js";
|
|
23
|
+
import { RangeAllocator } from "./allocator.js";
|
|
24
|
+
import { gridClassName, readImageHeader } from "./image-header.js";
|
|
25
|
+
import { VDB_GPU_LAYOUT_VERSION, VDB_POOL_HEADER_WORDS, VDB_POOL_RECORD_WORDS, align32, dataStartOffset, encodeHeader, encodeRecord, recordByteOffset, } from "./layout.js";
|
|
26
|
+
import { BUFFER_USAGE, POOL_BUFFER_USAGE, deviceByteLimit, floor32, requiredLimits, unsupportedReason, watchDevice, } from "./limits.js";
|
|
27
|
+
/** Updates a retired buffer must outlive before the pool asks the queue about it (one update of slack to submit). */
|
|
28
|
+
const RETIRE_AFTER_UPDATES = 2;
|
|
29
|
+
const DEFAULT_INITIAL_BYTES = 1 << 20;
|
|
30
|
+
const DEFAULT_MAX_GRIDS = 16;
|
|
31
|
+
function badOption(message) {
|
|
32
|
+
throw new VDBError("VDB_BAD_OPTION", message);
|
|
33
|
+
}
|
|
34
|
+
function checkCount(name, value, min) {
|
|
35
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min) {
|
|
36
|
+
badOption(`${name} must be an integer ≥ ${min}, got ${String(value)}.`);
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
function copyTransform(m) {
|
|
41
|
+
if (m === null || m === undefined)
|
|
42
|
+
return null;
|
|
43
|
+
return Float64Array.from(m);
|
|
44
|
+
}
|
|
45
|
+
function validateMajorant(m) {
|
|
46
|
+
if (typeof m !== "object" || m === null)
|
|
47
|
+
badOption("majorant must be { brickLog2, dims, data } or null.");
|
|
48
|
+
checkCount("majorant.brickLog2", m.brickLog2, 0);
|
|
49
|
+
if (m.brickLog2 > 10)
|
|
50
|
+
badOption(`majorant.brickLog2 is ${m.brickLog2}; bricks larger than 1024 voxels aren't supported.`);
|
|
51
|
+
if (!Array.isArray(m.dims) && !ArrayBuffer.isView(m.dims))
|
|
52
|
+
badOption("majorant.dims must be [x, y, z].");
|
|
53
|
+
if (m.dims.length !== 3)
|
|
54
|
+
badOption(`majorant.dims must have 3 entries, got ${m.dims.length}.`);
|
|
55
|
+
const dims = [m.dims[0], m.dims[1], m.dims[2]];
|
|
56
|
+
dims.forEach((d, k) => checkCount(`majorant.dims[${k}]`, d, 1));
|
|
57
|
+
if (!(m.data instanceof Float32Array))
|
|
58
|
+
badOption("majorant.data must be a Float32Array.");
|
|
59
|
+
const cells = dims[0] * dims[1] * dims[2];
|
|
60
|
+
if (m.data.length !== cells) {
|
|
61
|
+
badOption(`majorant.data has ${m.data.length} values; dims ${dims.join("x")} need ${cells}. Rebuild the majorant.`);
|
|
62
|
+
}
|
|
63
|
+
const bytes = cells * 4;
|
|
64
|
+
if (bytes > 2 ** 32 - 32)
|
|
65
|
+
badOption(`majorant needs ${bytes} bytes; the pool addresses ranges with 32 bits.`);
|
|
66
|
+
return { brickLog2: m.brickLog2, dims, bytes, pending: m.data, retained: null, range: null, resident: false };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `queue.writeBuffer` for any typed array. The WebGPU typings want an
|
|
70
|
+
* `ArrayBuffer`-backed view; callers' arrays are typed `ArrayBufferLike`, and
|
|
71
|
+
* writeBuffer copies the bytes either way.
|
|
72
|
+
*/
|
|
73
|
+
function write(queue, buffer, offset, data) {
|
|
74
|
+
queue.writeBuffer(buffer, offset, data);
|
|
75
|
+
}
|
|
76
|
+
function checkValueMax(v) {
|
|
77
|
+
if (v === undefined)
|
|
78
|
+
return undefined;
|
|
79
|
+
if (typeof v !== "number" || Number.isNaN(v))
|
|
80
|
+
badOption(`valueMax must be a number, got ${String(v)}.`);
|
|
81
|
+
return v;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Many NanoVDB grids in one storage buffer, bound at one slot (D10). The pool
|
|
85
|
+
* takes the host's `GPUDevice`, records into the host's encoder, and frees only
|
|
86
|
+
* what it allocated (D8).
|
|
87
|
+
*
|
|
88
|
+
* Images passed to `add`/`replace` are held by reference (not copied) until the
|
|
89
|
+
* next `update` writes them, and after that while a growth is unconfirmed
|
|
90
|
+
* (until `settled()` resolves); don't mutate them before then.
|
|
91
|
+
*/
|
|
92
|
+
export class VDBGridPool {
|
|
93
|
+
/** The device the pool allocates on. */
|
|
94
|
+
device;
|
|
95
|
+
#buffer;
|
|
96
|
+
#ownsBuffer;
|
|
97
|
+
#retired = [];
|
|
98
|
+
#growths = [];
|
|
99
|
+
#updates = 0;
|
|
100
|
+
#lastError = null;
|
|
101
|
+
#version = 0;
|
|
102
|
+
#revision = 0;
|
|
103
|
+
#capacity;
|
|
104
|
+
#limit;
|
|
105
|
+
#label;
|
|
106
|
+
#usage;
|
|
107
|
+
#grids = [];
|
|
108
|
+
#alloc;
|
|
109
|
+
#dirtyRecords = new Set();
|
|
110
|
+
#headerDirty = true;
|
|
111
|
+
#disposed = false;
|
|
112
|
+
constructor(device, buffer, owns, capacity, limit, label, usage) {
|
|
113
|
+
this.device = device;
|
|
114
|
+
this.#buffer = buffer;
|
|
115
|
+
this.#ownsBuffer = owns;
|
|
116
|
+
this.#capacity = capacity;
|
|
117
|
+
this.#limit = limit;
|
|
118
|
+
this.#label = label;
|
|
119
|
+
this.#usage = usage;
|
|
120
|
+
this.#alloc = new RangeAllocator(dataStartOffset(capacity), buffer.size);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates a pool on `device`. Throws `VDB_UNSUPPORTED_DEVICE` (see
|
|
124
|
+
* {@link unsupportedReason}) or `VDB_BAD_OPTION`. The returned pool is
|
|
125
|
+
* bindable at once (an empty table).
|
|
126
|
+
*/
|
|
127
|
+
static async create(device, options = {}) {
|
|
128
|
+
const reason = unsupportedReason(device);
|
|
129
|
+
if (reason !== null)
|
|
130
|
+
throw new VDBError("VDB_UNSUPPORTED_DEVICE", reason);
|
|
131
|
+
watchDevice(device);
|
|
132
|
+
const capacity = checkCount("maxGrids", options.maxGrids ?? DEFAULT_MAX_GRIDS, 1);
|
|
133
|
+
const usage = checkCount("usage", options.usage ?? 0, 0);
|
|
134
|
+
const label = options.label ?? "vdb pool";
|
|
135
|
+
const deviceLimit = deviceByteLimit(device);
|
|
136
|
+
let limit = deviceLimit;
|
|
137
|
+
if (options.maxBytes !== undefined)
|
|
138
|
+
limit = Math.min(limit, floor32(checkCount("maxBytes", options.maxBytes, 1)));
|
|
139
|
+
const tableEnd = dataStartOffset(capacity);
|
|
140
|
+
let pool;
|
|
141
|
+
if (options.buffer !== undefined) {
|
|
142
|
+
if (options.initialBytes !== undefined)
|
|
143
|
+
badOption("initialBytes and buffer are exclusive; the host buffer's size is the pool size.");
|
|
144
|
+
const host = options.buffer;
|
|
145
|
+
const need = BUFFER_USAGE.STORAGE | BUFFER_USAGE.COPY_DST;
|
|
146
|
+
if ((host.usage & need) !== need) {
|
|
147
|
+
badOption("The host buffer needs GPUBufferUsage.STORAGE | COPY_DST. Create it with those flags.");
|
|
148
|
+
}
|
|
149
|
+
if (host.size % 4 !== 0)
|
|
150
|
+
badOption(`The host buffer is ${host.size} bytes; storage buffers must be a multiple of 4.`);
|
|
151
|
+
if (host.size > deviceLimit) {
|
|
152
|
+
badOption(`The host buffer is ${host.size} bytes; this device binds at most ${deviceLimit}. ` +
|
|
153
|
+
`Use a smaller buffer or request the device with VDBGridPool.requiredLimits(adapter).`);
|
|
154
|
+
}
|
|
155
|
+
if (host.size < tableEnd) {
|
|
156
|
+
badOption(`The host buffer is ${host.size} bytes; a ${capacity}-grid table needs ${tableEnd}. Lower maxGrids or enlarge it.`);
|
|
157
|
+
}
|
|
158
|
+
pool = new VDBGridPool(device, host, false, capacity, floor32(host.size), label, usage);
|
|
159
|
+
// A host buffer may hold old data: clear the header and table.
|
|
160
|
+
pool.#writeTable();
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
if (tableEnd > limit) {
|
|
164
|
+
badOption(`A ${capacity}-grid table needs ${tableEnd} bytes; the pool limit is ${limit}. Lower maxGrids.`);
|
|
165
|
+
}
|
|
166
|
+
const initial = checkCount("initialBytes", options.initialBytes ?? DEFAULT_INITIAL_BYTES, 1);
|
|
167
|
+
const size = Math.min(limit, Math.max(align32(initial), tableEnd));
|
|
168
|
+
device.pushErrorScope("out-of-memory");
|
|
169
|
+
const buffer = device.createBuffer({ label: `${label} (v0)`, size, usage: POOL_BUFFER_USAGE | usage });
|
|
170
|
+
const oom = await device.popErrorScope();
|
|
171
|
+
if (oom) {
|
|
172
|
+
buffer.destroy();
|
|
173
|
+
throw new VDBError("VDB_POOL_LIMIT", `The device could not allocate ${size} bytes (${oom.message}). Lower initialBytes.`);
|
|
174
|
+
}
|
|
175
|
+
pool = new VDBGridPool(device, buffer, true, capacity, limit, label, usage);
|
|
176
|
+
pool.#writeHeader();
|
|
177
|
+
}
|
|
178
|
+
return pool;
|
|
179
|
+
}
|
|
180
|
+
/** True when `device` can hold a pool. */
|
|
181
|
+
static isSupported(device) {
|
|
182
|
+
return unsupportedReason(device) === null;
|
|
183
|
+
}
|
|
184
|
+
/** Why `device` can't hold a pool, or `null`. */
|
|
185
|
+
static unsupportedReason(device) {
|
|
186
|
+
return unsupportedReason(device);
|
|
187
|
+
}
|
|
188
|
+
/** Limits for `adapter.requestDevice({ requiredLimits })` so the pool can grow to the adapter's maxima. */
|
|
189
|
+
static requiredLimits(adapter) {
|
|
190
|
+
return requiredLimits(adapter);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The pool's buffer. Replaced by a growth in `update`, or by the rollback of
|
|
194
|
+
* a growth whose allocation failed (asynchronously; see `settled`); `version`
|
|
195
|
+
* changes each time.
|
|
196
|
+
*/
|
|
197
|
+
get buffer() {
|
|
198
|
+
return this.#buffer;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* +1 whenever `buffer` changes object: rebuild bind groups. Monotonic: a
|
|
202
|
+
* failed growth's rollback to the previous buffer is a new version too.
|
|
203
|
+
*
|
|
204
|
+
* Read it after `update(encoder)`: call `update(encoder)` before building
|
|
205
|
+
* or reusing bind groups this frame. A bind group built before `update()`
|
|
206
|
+
* keeps the previous (still-alive) buffer and silently misses new grids.
|
|
207
|
+
*/
|
|
208
|
+
get version() {
|
|
209
|
+
return this.#version;
|
|
210
|
+
}
|
|
211
|
+
/** +1 on every content change (add, replace, remove, setTransform, setMajorant). */
|
|
212
|
+
get revision() {
|
|
213
|
+
return this.#revision;
|
|
214
|
+
}
|
|
215
|
+
/** {@link VDB_GPU_LAYOUT_VERSION}. */
|
|
216
|
+
get layoutVersion() {
|
|
217
|
+
return VDB_GPU_LAYOUT_VERSION;
|
|
218
|
+
}
|
|
219
|
+
/** Size of `buffer` in bytes. */
|
|
220
|
+
get byteLength() {
|
|
221
|
+
return this.#buffer.size;
|
|
222
|
+
}
|
|
223
|
+
/** Allocation high-water mark in bytes (header word 6). */
|
|
224
|
+
get usedBytes() {
|
|
225
|
+
return this.#alloc.highWater;
|
|
226
|
+
}
|
|
227
|
+
/** Bytes in replaced buffers still alive: awaiting growth confirmation or the queue (see `update`). */
|
|
228
|
+
get retiredBytes() {
|
|
229
|
+
let n = 0;
|
|
230
|
+
for (const r of this.#retired)
|
|
231
|
+
n += r.buffer.size;
|
|
232
|
+
for (const g of this.#growths)
|
|
233
|
+
n += g.from.size;
|
|
234
|
+
return n;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The error from the last growth whose allocation failed (`VDB_POOL_LIMIT`;
|
|
238
|
+
* the pool rolled back to its previous buffer), or `null`. Cleared when a
|
|
239
|
+
* later growth is confirmed.
|
|
240
|
+
*/
|
|
241
|
+
get lastError() {
|
|
242
|
+
return this.#lastError;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Resolves once every growth started so far is confirmed by the device;
|
|
246
|
+
* rejects with the `VDB_POOL_LIMIT` error if one failed and the pool rolled
|
|
247
|
+
* back (see {@link lastError}). Resolves at once when no growth is pending.
|
|
248
|
+
*/
|
|
249
|
+
settled() {
|
|
250
|
+
this.#checkAlive();
|
|
251
|
+
return Promise.all(this.#growths.map((g) => g.done)).then((errors) => {
|
|
252
|
+
const error = errors.find((e) => e !== null);
|
|
253
|
+
if (error)
|
|
254
|
+
throw error;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/** Records in the grid table. */
|
|
258
|
+
get gridCapacity() {
|
|
259
|
+
return this.#capacity;
|
|
260
|
+
}
|
|
261
|
+
/** Highest live id + 1 (header word 3). */
|
|
262
|
+
get gridCount() {
|
|
263
|
+
for (let id = this.#grids.length - 1; id >= 0; id--)
|
|
264
|
+
if (this.#grids[id])
|
|
265
|
+
return id + 1;
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Grid types of the live grids now, for `vdbCheckModule` and
|
|
270
|
+
* `vdbModuleWgsl({ types })`. It does not know about grids the host adds
|
|
271
|
+
* later: generate the module for every type the host may add (see `add`).
|
|
272
|
+
*/
|
|
273
|
+
get types() {
|
|
274
|
+
const out = new Set();
|
|
275
|
+
for (const g of this.#grids)
|
|
276
|
+
if (g)
|
|
277
|
+
out.add(g.header.gridType);
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Stages a grid image and returns its id at once. The image is validated
|
|
282
|
+
* here (`VDB_BAD_IMAGE`, `VDB_UNSUPPORTED_GRID_TYPE`) and written by the next
|
|
283
|
+
* `update`. Dead ids are reused, lowest first.
|
|
284
|
+
*
|
|
285
|
+
* The shader reading this pool decodes only the types its module was
|
|
286
|
+
* generated for; a grid of any other type reads as 0. A module must
|
|
287
|
+
* therefore be generated for every type the host may add (its known set,
|
|
288
|
+
* or `VDB_ALL_SCALAR_TYPES`), or regenerated and its pipeline rebuilt when
|
|
289
|
+
* a new type arrives. Call `vdbCheckModule(pool, options)` after `add` to
|
|
290
|
+
* catch a mismatch (`VDB_TYPE_NOT_COMPILED`).
|
|
291
|
+
*/
|
|
292
|
+
add(image, options = {}) {
|
|
293
|
+
this.#checkAlive();
|
|
294
|
+
const header = readImageHeader(image);
|
|
295
|
+
const capacity = align32(Math.max(header.byteLength, checkCount("capacity", options.capacity ?? 0, 0)));
|
|
296
|
+
const transform = copyTransform(options.transform);
|
|
297
|
+
const affines = composeGridAffines(header, transform);
|
|
298
|
+
const valueMaxOverride = checkValueMax(options.valueMax);
|
|
299
|
+
const majorant = options.majorant ? validateMajorant(options.majorant) : null;
|
|
300
|
+
let id = this.#grids.findIndex((g) => g === undefined);
|
|
301
|
+
if (id < 0)
|
|
302
|
+
id = this.#grids.length;
|
|
303
|
+
if (!this.#ownsBuffer && id >= this.#capacity) {
|
|
304
|
+
throw new VDBError("VDB_POOL_FULL", `The host buffer's table holds ${this.#capacity} grids and all are live. Remove a grid, or create the pool with a larger maxGrids.`);
|
|
305
|
+
}
|
|
306
|
+
this.#checkCanFit(Math.max(this.#grids.length, id + 1), majorant ? [capacity, majorant.bytes] : [capacity], "image");
|
|
307
|
+
const grid = {
|
|
308
|
+
header,
|
|
309
|
+
label: options.label,
|
|
310
|
+
capacity,
|
|
311
|
+
image: { range: null, resident: false },
|
|
312
|
+
pendingImage: image,
|
|
313
|
+
retainedImage: null,
|
|
314
|
+
imageSerial: 0,
|
|
315
|
+
imageRevision: 0,
|
|
316
|
+
transform,
|
|
317
|
+
affines,
|
|
318
|
+
valueMaxOverride,
|
|
319
|
+
majorant,
|
|
320
|
+
};
|
|
321
|
+
// Place now when it fits; a host buffer must fit now (it never grows).
|
|
322
|
+
const tableFits = id < this.#capacity && this.#grids.length <= this.#capacity;
|
|
323
|
+
this.#place(grid.image, capacity, "image", tableFits);
|
|
324
|
+
if (majorant) {
|
|
325
|
+
try {
|
|
326
|
+
this.#place(majorant, majorant.bytes, "majorant", tableFits);
|
|
327
|
+
}
|
|
328
|
+
catch (e) {
|
|
329
|
+
if (grid.image.range)
|
|
330
|
+
this.#alloc.free(grid.image.range.offset);
|
|
331
|
+
throw e;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
this.#grids[id] = grid;
|
|
335
|
+
this.#touch(id);
|
|
336
|
+
grid.imageRevision = this.#revision;
|
|
337
|
+
return id;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Stages a new image for grid `id`. It is written in place when it fits the
|
|
341
|
+
* grid's reserved range (no `version` change: the sequence-player path), else
|
|
342
|
+
* moved. A brick majorant set on the grid is dropped (it described the old
|
|
343
|
+
* image); call `setMajorant` again. Transform, label and `valueMax` are kept.
|
|
344
|
+
*/
|
|
345
|
+
replace(id, image) {
|
|
346
|
+
this.#checkAlive();
|
|
347
|
+
const grid = this.#grid(id);
|
|
348
|
+
const header = readImageHeader(image);
|
|
349
|
+
if (align32(header.byteLength) > grid.capacity) {
|
|
350
|
+
const capacity = align32(header.byteLength);
|
|
351
|
+
this.#checkCanFit(this.#grids.length, [capacity], "image"); // replace drops the majorant
|
|
352
|
+
const next = { range: null, resident: false };
|
|
353
|
+
this.#replaceRange(grid.image.range, () => this.#place(next, capacity, "image"));
|
|
354
|
+
grid.image = next;
|
|
355
|
+
grid.capacity = capacity;
|
|
356
|
+
}
|
|
357
|
+
grid.header = header;
|
|
358
|
+
grid.pendingImage = image;
|
|
359
|
+
grid.imageSerial++;
|
|
360
|
+
grid.affines = composeGridAffines(header, grid.transform);
|
|
361
|
+
this.#dropMajorant(grid);
|
|
362
|
+
this.#touch(id);
|
|
363
|
+
grid.imageRevision = this.#revision;
|
|
364
|
+
}
|
|
365
|
+
/** Frees grid `id`'s ranges and clears its record; the id may be reused by the next `add`. */
|
|
366
|
+
remove(id) {
|
|
367
|
+
this.#checkAlive();
|
|
368
|
+
const grid = this.#grid(id);
|
|
369
|
+
if (grid.image.range)
|
|
370
|
+
this.#alloc.free(grid.image.range.offset);
|
|
371
|
+
this.#dropMajorant(grid);
|
|
372
|
+
this.#grids[id] = undefined;
|
|
373
|
+
while (this.#grids.length > 0 && this.#grids[this.#grids.length - 1] === undefined)
|
|
374
|
+
this.#grids.pop();
|
|
375
|
+
this.#touch(id);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Sets grid `id`'s instance transform: a 4x4 column-major object-to-world
|
|
379
|
+
* matrix (WebGPU / three convention), or `null` for the file's own map. The
|
|
380
|
+
* pool composes `worldToIndex = map⁻¹ ∘ objectToWorld⁻¹` and `indexToWorld`
|
|
381
|
+
* in f64 and rounds to f32. Only the record changes; the image is untouched.
|
|
382
|
+
*/
|
|
383
|
+
setTransform(id, objectToWorld) {
|
|
384
|
+
this.#checkAlive();
|
|
385
|
+
const grid = this.#grid(id);
|
|
386
|
+
const transform = copyTransform(objectToWorld);
|
|
387
|
+
grid.affines = composeGridAffines(grid.header, transform);
|
|
388
|
+
grid.transform = transform;
|
|
389
|
+
this.#touch(id);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Stores brick-majorant bounds for grid `id` in the pool (record words
|
|
393
|
+
* 36–39, flag b2, brick log2 in b16–23), or clears them with `null`.
|
|
394
|
+
*/
|
|
395
|
+
setMajorant(id, majorant) {
|
|
396
|
+
this.#checkAlive();
|
|
397
|
+
const grid = this.#grid(id);
|
|
398
|
+
if (majorant === null) {
|
|
399
|
+
this.#dropMajorant(grid);
|
|
400
|
+
this.#touch(id);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const next = validateMajorant(majorant);
|
|
404
|
+
const current = grid.majorant;
|
|
405
|
+
if (current?.range && align32(next.bytes) <= current.range.size) {
|
|
406
|
+
next.range = current.range;
|
|
407
|
+
next.resident = current.resident;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
this.#checkCanFit(this.#grids.length, [grid.capacity, next.bytes], "majorant");
|
|
411
|
+
this.#replaceRange(current?.range ?? null, () => this.#place(next, next.bytes, "majorant"));
|
|
412
|
+
}
|
|
413
|
+
grid.majorant = next;
|
|
414
|
+
this.#touch(id);
|
|
415
|
+
}
|
|
416
|
+
/** Describes grid `id`. Throws `VDB_UNKNOWN_GRID` for a dead or out-of-range id. */
|
|
417
|
+
info(id) {
|
|
418
|
+
this.#checkAlive();
|
|
419
|
+
const g = this.#grid(id);
|
|
420
|
+
const h = g.header;
|
|
421
|
+
const lo = h.indexBBoxMin;
|
|
422
|
+
const hi = h.indexBBoxMax.map((v) => v + 1);
|
|
423
|
+
const min = [Infinity, Infinity, Infinity];
|
|
424
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
425
|
+
for (let c = 0; c < 8; c++) {
|
|
426
|
+
const p = applyAffine(g.affines.indexToWorld, [c & 1 ? hi[0] : lo[0], c & 2 ? hi[1] : lo[1], c & 4 ? hi[2] : lo[2]]);
|
|
427
|
+
for (let a = 0; a < 3; a++) {
|
|
428
|
+
min[a] = Math.min(min[a], p[a]);
|
|
429
|
+
max[a] = Math.max(max[a], p[a]);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const m = g.majorant;
|
|
433
|
+
return {
|
|
434
|
+
id,
|
|
435
|
+
type: h.gridType,
|
|
436
|
+
gridTypeId: h.gridTypeId,
|
|
437
|
+
gridClass: gridClassName(h.gridClass),
|
|
438
|
+
gridClassId: h.gridClass,
|
|
439
|
+
name: h.name,
|
|
440
|
+
label: g.label,
|
|
441
|
+
imageBytes: h.byteLength,
|
|
442
|
+
capacity: g.capacity,
|
|
443
|
+
imageOffset: g.image.range?.offset ?? null,
|
|
444
|
+
resident: g.image.resident && g.pendingImage === null,
|
|
445
|
+
imageRevision: g.imageRevision,
|
|
446
|
+
hasStats: h.hasStats,
|
|
447
|
+
hasBrickMajorant: m !== null,
|
|
448
|
+
majorantOffset: m?.range?.offset ?? null,
|
|
449
|
+
majorantDims: m ? [...m.dims] : null,
|
|
450
|
+
instanceTransform: g.transform !== null,
|
|
451
|
+
background: h.background,
|
|
452
|
+
valueMax: this.#valueMax(g),
|
|
453
|
+
rootMin: h.rootMin,
|
|
454
|
+
rootMax: h.rootMax,
|
|
455
|
+
voxelSize: [...h.voxelSize],
|
|
456
|
+
indexBBox: { min: [...lo], max: [...h.indexBBoxMax] },
|
|
457
|
+
worldBBox: { min, max },
|
|
458
|
+
worldToIndex: g.affines.worldToIndex.slice(),
|
|
459
|
+
indexToWorld: g.affines.indexToWorld.slice(),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Applies staged changes, recording any copies into `encoder`:
|
|
464
|
+
* 1. asks the queue about buffers retired at least two updates ago (once
|
|
465
|
+
* their growth is confirmed); each is destroyed when
|
|
466
|
+
* `onSubmittedWorkDone` resolves;
|
|
467
|
+
* 2. if the staged state doesn't fit, grows (or, when only fragmentation
|
|
468
|
+
* blocks a placement, compacts) into a new buffer, copying ranges that
|
|
469
|
+
* were resident before this call, and bumps `version`. The allocation is
|
|
470
|
+
* checked in error scopes; if it fails, the pool rolls back later (see
|
|
471
|
+
* {@link settled}). Planned before anything changes: when the staged
|
|
472
|
+
* grids together pass the byte limit, nothing is re-laid out;
|
|
473
|
+
* 3. writes new images, majorants, dirty records and the header with
|
|
474
|
+
* `queue.writeBuffer`, for ranges placed in the current buffer (a grid
|
|
475
|
+
* still waiting for space reads as dead on the GPU).
|
|
476
|
+
* Then, if step 2 hit the limit, throws `VDB_POOL_LIMIT` (buffer and
|
|
477
|
+
* `version` unchanged; remove grids and update again).
|
|
478
|
+
*
|
|
479
|
+
* Submit `encoder` before the second `update` after this one (one update of
|
|
480
|
+
* slack): a buffer this update retires stays alive until then.
|
|
481
|
+
*
|
|
482
|
+
* Call `update(encoder)` before building or reusing bind groups this frame,
|
|
483
|
+
* then compare `version` with the version the bind group was built at. A
|
|
484
|
+
* bind group built before `update()` keeps the previous (still-alive)
|
|
485
|
+
* buffer and silently misses new grids: no validation error, just stale
|
|
486
|
+
* data.
|
|
487
|
+
*/
|
|
488
|
+
update(encoder) {
|
|
489
|
+
this.#checkAlive();
|
|
490
|
+
this.#updates++;
|
|
491
|
+
this.#reapRetired();
|
|
492
|
+
let limitError = null;
|
|
493
|
+
const needIds = this.#grids.length;
|
|
494
|
+
const unplaced = this.#unplaced();
|
|
495
|
+
if (needIds > this.#capacity || unplaced.length > 0) {
|
|
496
|
+
if (needIds <= this.#capacity && this.#tryPlace(unplaced)) {
|
|
497
|
+
// Placed in gaps freed since staging.
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
try {
|
|
501
|
+
this.#relayout(encoder, needIds);
|
|
502
|
+
}
|
|
503
|
+
catch (e) {
|
|
504
|
+
if (!(e instanceof VDBError) || e.code !== "VDB_POOL_LIMIT")
|
|
505
|
+
throw e;
|
|
506
|
+
limitError = e; // independent writes below still reach the current buffer
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
this.#writePending();
|
|
511
|
+
if (limitError)
|
|
512
|
+
throw limitError;
|
|
513
|
+
}
|
|
514
|
+
/** Convenience: creates an encoder, calls `update`, and submits it. */
|
|
515
|
+
flush() {
|
|
516
|
+
this.#checkAlive();
|
|
517
|
+
const encoder = this.device.createCommandEncoder({ label: `${this.#label} flush` });
|
|
518
|
+
this.update(encoder);
|
|
519
|
+
this.device.queue.submit([encoder.finish()]);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The bind group entry for `slot`: `{ binding: slot.firstBinding, resource: { buffer } }`.
|
|
523
|
+
* Takes the same `VDBSlot` as `vdbModuleWgsl({ slot })` and
|
|
524
|
+
* `vdbModuleEntries(slot)`.
|
|
525
|
+
*
|
|
526
|
+
* It names the current `buffer`, so call `update(encoder)` before building
|
|
527
|
+
* or reusing bind groups this frame, and rebuild when `version` changed. A
|
|
528
|
+
* bind group built before `update()` keeps the previous (still-alive)
|
|
529
|
+
* buffer and silently misses new grids.
|
|
530
|
+
*/
|
|
531
|
+
bindGroupEntry(slot) {
|
|
532
|
+
this.#checkAlive();
|
|
533
|
+
checkCount("slot.firstBinding", slot?.firstBinding, 0);
|
|
534
|
+
return { binding: slot.firstBinding, resource: { buffer: this.#buffer } };
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Destroys every buffer this pool created (current, retired, and those of an
|
|
538
|
+
* unconfirmed growth) at once, never a host buffer or the device. Submit no
|
|
539
|
+
* work recorded by `update` after this. Idempotent.
|
|
540
|
+
*/
|
|
541
|
+
dispose() {
|
|
542
|
+
if (this.#disposed)
|
|
543
|
+
return;
|
|
544
|
+
this.#disposed = true;
|
|
545
|
+
const owned = new Set();
|
|
546
|
+
for (const r of this.#retired)
|
|
547
|
+
owned.add(r.buffer);
|
|
548
|
+
for (const g of this.#growths)
|
|
549
|
+
owned.add(g.from).add(g.to);
|
|
550
|
+
if (this.#ownsBuffer)
|
|
551
|
+
owned.add(this.#buffer);
|
|
552
|
+
for (const b of owned)
|
|
553
|
+
b.destroy();
|
|
554
|
+
this.#retired = [];
|
|
555
|
+
this.#growths = [];
|
|
556
|
+
this.#grids = [];
|
|
557
|
+
this.#dirtyRecords.clear();
|
|
558
|
+
}
|
|
559
|
+
// -------------------------------------------------------------------------
|
|
560
|
+
#checkAlive() {
|
|
561
|
+
if (this.#disposed)
|
|
562
|
+
throw new VDBError("VDB_DISPOSED", "This VDBGridPool was disposed. Create a new pool.");
|
|
563
|
+
}
|
|
564
|
+
#grid(id) {
|
|
565
|
+
const g = Number.isInteger(id) ? this.#grids[id] : undefined;
|
|
566
|
+
if (!g) {
|
|
567
|
+
throw new VDBError("VDB_UNKNOWN_GRID", `Grid id ${id} is not live in this pool (removed, or never returned by add). Use an id from add().`);
|
|
568
|
+
}
|
|
569
|
+
return g;
|
|
570
|
+
}
|
|
571
|
+
#touch(id) {
|
|
572
|
+
this.#dirtyRecords.add(id);
|
|
573
|
+
this.#headerDirty = true;
|
|
574
|
+
this.#revision++;
|
|
575
|
+
}
|
|
576
|
+
#valueMax(g) {
|
|
577
|
+
if (g.valueMaxOverride !== undefined)
|
|
578
|
+
return g.valueMaxOverride;
|
|
579
|
+
// Without stats there is no bound in the image; Infinity tells the integrator to pass valueMax.
|
|
580
|
+
return g.header.hasStats ? Math.max(g.header.rootMax, g.header.background) : Infinity;
|
|
581
|
+
}
|
|
582
|
+
/** Limit detail for `VDB_POOL_LIMIT` messages. */
|
|
583
|
+
#limitDetail() {
|
|
584
|
+
const { maxStorageBufferBindingSize, maxBufferSize } = this.device.limits;
|
|
585
|
+
return (`device maxStorageBufferBindingSize ${maxStorageBufferBindingSize}, maxBufferSize ${maxBufferSize}` +
|
|
586
|
+
`${this.#limit < deviceByteLimit(this.device) ? `, maxBytes ${this.#limit}` : ""}`);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Throws `VDB_POOL_LIMIT` when ranges of `bytes` could not fit under the
|
|
590
|
+
* limit even alone, beside a table of `ids` records (§1.5). Staged, such a
|
|
591
|
+
* request would make every later `update` fail; reject it while the pool is
|
|
592
|
+
* unchanged. Host buffers are checked by `#place` instead (`VDB_POOL_FULL`).
|
|
593
|
+
*/
|
|
594
|
+
#checkCanFit(ids, bytes, what) {
|
|
595
|
+
if (!this.#ownsBuffer)
|
|
596
|
+
return;
|
|
597
|
+
let capacity = this.#capacity;
|
|
598
|
+
while (capacity < ids)
|
|
599
|
+
capacity *= 2;
|
|
600
|
+
const start = dataStartOffset(capacity);
|
|
601
|
+
let needed = start;
|
|
602
|
+
for (const b of bytes)
|
|
603
|
+
needed += align32(b);
|
|
604
|
+
if (needed <= this.#limit)
|
|
605
|
+
return;
|
|
606
|
+
throw new VDBError("VDB_POOL_LIMIT", `This ${what} needs ${needed - start} bytes beside a ${capacity}-grid table (${start} bytes), ${needed} in all: ` +
|
|
607
|
+
`more than the pool limit of ${this.#limit} bytes (${this.#limitDetail()}) even with no other grids. ` +
|
|
608
|
+
`Use a smaller grid, or request the device with VDBGridPool.requiredLimits(adapter). The pool is unchanged.`);
|
|
609
|
+
}
|
|
610
|
+
/** Frees `old`, runs `place` (which may reuse its space), and restores `old` if `place` throws. */
|
|
611
|
+
#replaceRange(old, place) {
|
|
612
|
+
if (old)
|
|
613
|
+
this.#alloc.free(old.offset);
|
|
614
|
+
try {
|
|
615
|
+
place();
|
|
616
|
+
}
|
|
617
|
+
catch (e) {
|
|
618
|
+
if (old)
|
|
619
|
+
this.#alloc.restore(old);
|
|
620
|
+
throw e;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* First-fit placement at staging time; a host buffer must fit now. In an
|
|
625
|
+
* owned pool a range that doesn't fit (or any range while the table is
|
|
626
|
+
* over capacity) stays unplaced and the next `update` re-lays out.
|
|
627
|
+
*/
|
|
628
|
+
#place(state, bytes, what, tableFits = this.#grids.length <= this.#capacity) {
|
|
629
|
+
const offset = tableFits ? this.#alloc.alloc(bytes) : null;
|
|
630
|
+
if (offset !== null) {
|
|
631
|
+
state.range = { offset, size: align32(bytes) };
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
if (!this.#ownsBuffer) {
|
|
635
|
+
throw new VDBError("VDB_POOL_FULL", `The host buffer (${this.#buffer.size} bytes, ${this.#alloc.allocatedBytes} allocated) has no free range of ` +
|
|
636
|
+
`${align32(bytes)} bytes for this ${what}. Remove grids, or create the pool on a larger buffer (or without one, so it can grow).`);
|
|
637
|
+
}
|
|
638
|
+
state.range = null; // placed by the next update
|
|
639
|
+
}
|
|
640
|
+
#dropMajorant(grid) {
|
|
641
|
+
const m = grid.majorant;
|
|
642
|
+
if (m?.range)
|
|
643
|
+
this.#alloc.free(m.range.offset);
|
|
644
|
+
grid.majorant = null;
|
|
645
|
+
}
|
|
646
|
+
#unplaced() {
|
|
647
|
+
const out = [];
|
|
648
|
+
for (const g of this.#grids) {
|
|
649
|
+
if (!g)
|
|
650
|
+
continue;
|
|
651
|
+
if (!g.image.range)
|
|
652
|
+
out.push({ state: g.image, bytes: g.capacity });
|
|
653
|
+
if (g.majorant && !g.majorant.range)
|
|
654
|
+
out.push({ state: g.majorant, bytes: g.majorant.bytes });
|
|
655
|
+
}
|
|
656
|
+
return out;
|
|
657
|
+
}
|
|
658
|
+
/** Places every unplaced range first-fit, or changes nothing and returns false. */
|
|
659
|
+
#tryPlace(unplaced) {
|
|
660
|
+
const placed = [];
|
|
661
|
+
for (const u of unplaced) {
|
|
662
|
+
const offset = this.#alloc.alloc(u.bytes);
|
|
663
|
+
if (offset === null) {
|
|
664
|
+
for (const o of placed)
|
|
665
|
+
this.#alloc.free(o);
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
placed.push(offset);
|
|
669
|
+
}
|
|
670
|
+
unplaced.forEach((u, k) => (u.state.range = { offset: placed[k], size: align32(u.bytes) }));
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
/** Grows or compacts into a new buffer (§1.4 step 2). Plans and checks before changing anything. */
|
|
674
|
+
#relayout(encoder, needIds) {
|
|
675
|
+
if (!this.#ownsBuffer) {
|
|
676
|
+
// #place already guarantees host-buffer placements; reaching here is a pool bug.
|
|
677
|
+
throw new VDBError("VDB_POOL_FULL", "The host buffer cannot be re-laid out. Remove grids or use a larger buffer.");
|
|
678
|
+
}
|
|
679
|
+
let capacity = this.#capacity;
|
|
680
|
+
while (capacity < needIds)
|
|
681
|
+
capacity *= 2;
|
|
682
|
+
// Pack every live range in id order: image, then majorant.
|
|
683
|
+
const ranges = [];
|
|
684
|
+
for (const g of this.#grids) {
|
|
685
|
+
if (!g)
|
|
686
|
+
continue;
|
|
687
|
+
ranges.push({ state: g.image, bytes: g.capacity, copyBytes: g.pendingImage ? 0 : g.header.byteLength });
|
|
688
|
+
if (g.majorant)
|
|
689
|
+
ranges.push({ state: g.majorant, bytes: g.majorant.bytes, copyBytes: g.majorant.pending ? 0 : g.majorant.bytes });
|
|
690
|
+
}
|
|
691
|
+
const start = dataStartOffset(capacity);
|
|
692
|
+
let needed = start;
|
|
693
|
+
for (const r of ranges)
|
|
694
|
+
needed += align32(r.bytes);
|
|
695
|
+
const current = this.#buffer.size;
|
|
696
|
+
const grows = capacity > this.#capacity || needed > current;
|
|
697
|
+
const size = grows ? Math.min(this.#limit, Math.max(needed, 2 * current)) : current;
|
|
698
|
+
if (needed > size) {
|
|
699
|
+
throw new VDBError("VDB_POOL_LIMIT", `The staged grids need a ${needed}-byte pool; the limit is ${this.#limit} bytes (${this.#limitDetail()}). ` +
|
|
700
|
+
`Remove grids, or request the device with VDBGridPool.requiredLimits(adapter). The buffer is unchanged; ` +
|
|
701
|
+
`staged changes that fit it were applied.`);
|
|
702
|
+
}
|
|
703
|
+
// What `from` holds (clean, resident ranges), for a rollback if the allocation fails.
|
|
704
|
+
const snapshot = new Map();
|
|
705
|
+
for (const g of this.#grids) {
|
|
706
|
+
if (!g)
|
|
707
|
+
continue;
|
|
708
|
+
const m = g.majorant;
|
|
709
|
+
snapshot.set(g, {
|
|
710
|
+
imageSerial: g.imageSerial,
|
|
711
|
+
image: g.image.range && g.image.resident && !g.pendingImage ? { ...g.image.range } : null,
|
|
712
|
+
majorant: m,
|
|
713
|
+
majorantRange: m?.range && m.resident && !m.pending ? { ...m.range } : null,
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
// update() is synchronous: switch now, keep `from` alive, and settle when the scopes resolve.
|
|
717
|
+
const device = this.device;
|
|
718
|
+
device.pushErrorScope("out-of-memory");
|
|
719
|
+
device.pushErrorScope("validation");
|
|
720
|
+
const next = device.createBuffer({
|
|
721
|
+
label: `${this.#label} (v${this.#version + 1})`,
|
|
722
|
+
size,
|
|
723
|
+
usage: POOL_BUFFER_USAGE | this.#usage,
|
|
724
|
+
});
|
|
725
|
+
const validation = device.popErrorScope();
|
|
726
|
+
const oom = device.popErrorScope();
|
|
727
|
+
const growth = {
|
|
728
|
+
from: this.#buffer,
|
|
729
|
+
to: next,
|
|
730
|
+
capacity: this.#capacity,
|
|
731
|
+
update: this.#updates,
|
|
732
|
+
snapshot,
|
|
733
|
+
done: Promise.resolve(null),
|
|
734
|
+
};
|
|
735
|
+
// A rejected pop (e.g. the host popped our scope) tells us nothing: treat it as success.
|
|
736
|
+
const scopeError = Promise.all([oom, validation]).then(([o, v]) => o ?? v, () => null);
|
|
737
|
+
const previous = this.#growths[this.#growths.length - 1]?.done ?? Promise.resolve(null);
|
|
738
|
+
growth.done = Promise.all([scopeError, previous]).then(([e]) => this.#settleGrowth(growth, e));
|
|
739
|
+
this.#growths.push(growth);
|
|
740
|
+
const alloc = new RangeAllocator(start, size);
|
|
741
|
+
for (const r of ranges) {
|
|
742
|
+
const offset = alloc.alloc(r.bytes);
|
|
743
|
+
const old = r.state.range;
|
|
744
|
+
if (old && r.state.resident && r.copyBytes > 0) {
|
|
745
|
+
encoder.copyBufferToBuffer(this.#buffer, old.offset, next, offset, r.copyBytes);
|
|
746
|
+
}
|
|
747
|
+
r.state.range = { offset, size: align32(r.bytes) };
|
|
748
|
+
if (r.copyBytes === 0)
|
|
749
|
+
r.state.resident = false;
|
|
750
|
+
}
|
|
751
|
+
this.#buffer = next;
|
|
752
|
+
this.#alloc = alloc;
|
|
753
|
+
this.#capacity = capacity;
|
|
754
|
+
this.#version++;
|
|
755
|
+
this.#writeTable();
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Runs when a growth's error scopes resolve, in growth order. Confirmed: its
|
|
759
|
+
* old buffer is retired. Failed: roll back to that old buffer, voiding every
|
|
760
|
+
* later growth (they copied from the failed buffer).
|
|
761
|
+
*/
|
|
762
|
+
#settleGrowth(growth, scopeError) {
|
|
763
|
+
// Disposed (dispose destroyed everything), or voided by an earlier failure.
|
|
764
|
+
if (this.#disposed || this.#growths[0] !== growth)
|
|
765
|
+
return null;
|
|
766
|
+
if (!scopeError) {
|
|
767
|
+
this.#growths.shift();
|
|
768
|
+
this.#retired.push({ buffer: growth.from, since: growth.update, waiting: false });
|
|
769
|
+
this.#lastError = null;
|
|
770
|
+
if (this.#growths.length === 0)
|
|
771
|
+
this.#releaseRetained();
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
const error = new VDBError("VDB_POOL_LIMIT", `The device could not allocate a ${growth.to.size}-byte pool buffer while growing (${scopeError.message}). ` +
|
|
775
|
+
`The pool rolled back to its previous ${growth.from.size}-byte buffer as version ${this.#version + 1}; rebuild ` +
|
|
776
|
+
`bind groups. Grids resident before the growth are intact; ones staged or written since are staged again. ` +
|
|
777
|
+
`Remove grids or free GPU memory, then update again.`);
|
|
778
|
+
this.#rollBack(growth);
|
|
779
|
+
this.#lastError = error;
|
|
780
|
+
return error;
|
|
781
|
+
}
|
|
782
|
+
/** Returns to `growth.from` and what it holds; every other range is staged again. */
|
|
783
|
+
#rollBack(growth) {
|
|
784
|
+
for (const g of this.#growths)
|
|
785
|
+
g.to.destroy(); // the failed buffer and any chained after it
|
|
786
|
+
this.#growths = [];
|
|
787
|
+
const alloc = new RangeAllocator(dataStartOffset(growth.capacity), growth.from.size);
|
|
788
|
+
for (const g of this.#grids) {
|
|
789
|
+
if (!g)
|
|
790
|
+
continue;
|
|
791
|
+
const snap = growth.snapshot.get(g);
|
|
792
|
+
// Same serial: no replace since the snapshot, so `from` holds the current image and nothing is pending.
|
|
793
|
+
if (snap?.image && snap.imageSerial === g.imageSerial) {
|
|
794
|
+
alloc.restore(snap.image);
|
|
795
|
+
g.image.range = { ...snap.image };
|
|
796
|
+
g.image.resident = true;
|
|
797
|
+
}
|
|
798
|
+
else {
|
|
799
|
+
g.image.range = null;
|
|
800
|
+
g.image.resident = false;
|
|
801
|
+
g.pendingImage ??= g.retainedImage;
|
|
802
|
+
}
|
|
803
|
+
const m = g.majorant;
|
|
804
|
+
if (!m)
|
|
805
|
+
continue;
|
|
806
|
+
if (snap?.majorantRange && snap.majorant === m) {
|
|
807
|
+
alloc.restore(snap.majorantRange);
|
|
808
|
+
m.range = { ...snap.majorantRange };
|
|
809
|
+
m.resident = true;
|
|
810
|
+
}
|
|
811
|
+
else {
|
|
812
|
+
m.range = null;
|
|
813
|
+
m.resident = false;
|
|
814
|
+
m.pending ??= m.retained;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
this.#releaseRetained();
|
|
818
|
+
this.#buffer = growth.from;
|
|
819
|
+
this.#alloc = alloc;
|
|
820
|
+
this.#capacity = growth.capacity;
|
|
821
|
+
this.#version++;
|
|
822
|
+
this.#writeTable(); // `from`'s table is stale: restored grids are live, the rest read as dead until placed
|
|
823
|
+
}
|
|
824
|
+
#releaseRetained() {
|
|
825
|
+
for (const g of this.#grids) {
|
|
826
|
+
if (!g)
|
|
827
|
+
continue;
|
|
828
|
+
g.retainedImage = null;
|
|
829
|
+
if (g.majorant)
|
|
830
|
+
g.majorant.retained = null;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* §1.4 step 1: for retired buffers that are confirmed and at least
|
|
835
|
+
* {@link RETIRE_AFTER_UPDATES} updates old (so the encoder that copied out of
|
|
836
|
+
* them has been submitted), asks the queue once and destroys them when that
|
|
837
|
+
* work is done.
|
|
838
|
+
*/
|
|
839
|
+
#reapRetired() {
|
|
840
|
+
const ready = this.#retired.filter((r) => !r.waiting && this.#updates - r.since >= RETIRE_AFTER_UPDATES);
|
|
841
|
+
if (ready.length === 0)
|
|
842
|
+
return;
|
|
843
|
+
for (const r of ready)
|
|
844
|
+
r.waiting = true;
|
|
845
|
+
const destroy = () => {
|
|
846
|
+
if (this.#disposed)
|
|
847
|
+
return; // dispose destroyed them
|
|
848
|
+
this.#retired = this.#retired.filter((r) => !ready.includes(r));
|
|
849
|
+
for (const r of ready)
|
|
850
|
+
r.buffer.destroy();
|
|
851
|
+
};
|
|
852
|
+
this.device.queue.onSubmittedWorkDone().then(destroy, destroy);
|
|
853
|
+
}
|
|
854
|
+
/** Every range of `g` is placed. Until then its record stays dirty. */
|
|
855
|
+
#complete(g) {
|
|
856
|
+
return g.image.range !== null && (g.majorant === null || g.majorant.range !== null);
|
|
857
|
+
}
|
|
858
|
+
/** The header's grid count: highest live id + 1, clamped to the table (ids past it can't be read). */
|
|
859
|
+
#headerGridCount() {
|
|
860
|
+
return Math.min(this.gridCount, this.#capacity);
|
|
861
|
+
}
|
|
862
|
+
/** Writes the header and every record (after create on a host buffer, a relayout, or a rollback). */
|
|
863
|
+
#writeTable() {
|
|
864
|
+
const words = new Uint32Array(VDB_POOL_HEADER_WORDS + this.#capacity * VDB_POOL_RECORD_WORDS);
|
|
865
|
+
const dirty = new Set();
|
|
866
|
+
this.#grids.forEach((g, id) => {
|
|
867
|
+
if (!g)
|
|
868
|
+
return;
|
|
869
|
+
if (id < this.#capacity && g.image.range) {
|
|
870
|
+
encodeRecord(this.#record(g), words, VDB_POOL_HEADER_WORDS + id * VDB_POOL_RECORD_WORDS);
|
|
871
|
+
}
|
|
872
|
+
if (!this.#complete(g))
|
|
873
|
+
dirty.add(id);
|
|
874
|
+
});
|
|
875
|
+
encodeHeader({ gridCapacity: this.#capacity, gridCount: this.#headerGridCount(), usedBytes: this.usedBytes }, words, 0);
|
|
876
|
+
write(this.device.queue, this.#buffer, 0, words);
|
|
877
|
+
this.#dirtyRecords = dirty;
|
|
878
|
+
this.#headerDirty = false;
|
|
879
|
+
}
|
|
880
|
+
#writeHeader() {
|
|
881
|
+
const words = encodeHeader({ gridCapacity: this.#capacity, gridCount: this.#headerGridCount(), usedBytes: this.usedBytes });
|
|
882
|
+
write(this.device.queue, this.#buffer, 0, words);
|
|
883
|
+
this.#headerDirty = false;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* §1.4 step 3: images, majorants, dirty records, header. Only ranges placed
|
|
887
|
+
* in the current buffer are written; a grid whose image is unplaced gets a
|
|
888
|
+
* dead record, and its id stays dirty until every range of it is placed.
|
|
889
|
+
*/
|
|
890
|
+
#writePending() {
|
|
891
|
+
const queue = this.device.queue;
|
|
892
|
+
const keep = this.#growths.length > 0; // a rollback may have to stage these again
|
|
893
|
+
for (const g of this.#grids) {
|
|
894
|
+
if (!g)
|
|
895
|
+
continue;
|
|
896
|
+
if (g.pendingImage && g.image.range) {
|
|
897
|
+
write(queue, this.#buffer, g.image.range.offset, g.pendingImage);
|
|
898
|
+
if (keep)
|
|
899
|
+
g.retainedImage = g.pendingImage;
|
|
900
|
+
g.pendingImage = null;
|
|
901
|
+
g.image.resident = true;
|
|
902
|
+
}
|
|
903
|
+
const m = g.majorant;
|
|
904
|
+
if (m?.pending && m.range) {
|
|
905
|
+
write(queue, this.#buffer, m.range.offset, m.pending);
|
|
906
|
+
if (keep)
|
|
907
|
+
m.retained = m.pending;
|
|
908
|
+
m.pending = null;
|
|
909
|
+
m.resident = true;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
const dirty = new Set();
|
|
913
|
+
for (const id of [...this.#dirtyRecords].sort((a, b) => a - b)) {
|
|
914
|
+
const g = this.#grids[id];
|
|
915
|
+
if (g && !this.#complete(g))
|
|
916
|
+
dirty.add(id);
|
|
917
|
+
if (id >= this.#capacity)
|
|
918
|
+
continue; // past the table end: the relayout that grows the table writes it
|
|
919
|
+
const words = new Uint32Array(VDB_POOL_RECORD_WORDS);
|
|
920
|
+
if (g?.image.range)
|
|
921
|
+
encodeRecord(this.#record(g), words, 0);
|
|
922
|
+
write(queue, this.#buffer, recordByteOffset(id), words);
|
|
923
|
+
}
|
|
924
|
+
this.#dirtyRecords = dirty;
|
|
925
|
+
if (this.#headerDirty)
|
|
926
|
+
this.#writeHeader();
|
|
927
|
+
}
|
|
928
|
+
/** The record for `g`, whose image is placed. A majorant is recorded only once it is placed. */
|
|
929
|
+
#record(g) {
|
|
930
|
+
const h = g.header;
|
|
931
|
+
const m = g.majorant?.range ? g.majorant : null;
|
|
932
|
+
return {
|
|
933
|
+
imageOffset: g.image.range.offset,
|
|
934
|
+
imageBytes: h.byteLength,
|
|
935
|
+
gridType: h.gridTypeId,
|
|
936
|
+
live: true,
|
|
937
|
+
hasStats: h.hasStats,
|
|
938
|
+
hasBrickMajorant: m !== null,
|
|
939
|
+
instanceTransform: g.transform !== null,
|
|
940
|
+
gridClass: h.gridClass,
|
|
941
|
+
brickLog2: m?.brickLog2 ?? 0,
|
|
942
|
+
worldToIndex: Array.from(g.affines.worldToIndex),
|
|
943
|
+
indexToWorld: Array.from(g.affines.indexToWorld),
|
|
944
|
+
indexBBoxMin: h.indexBBoxMin,
|
|
945
|
+
indexBBoxMax: h.indexBBoxMax,
|
|
946
|
+
background: h.background,
|
|
947
|
+
valueMax: this.#valueMax(g),
|
|
948
|
+
majorantOffset: m?.range?.offset ?? 0,
|
|
949
|
+
majorantDims: m ? m.dims : [0, 0, 0],
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
//# sourceMappingURL=pool.js.map
|