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