@driftengine/core 3.63.0 → 4.0.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 (428) hide show
  1. package/CHANGELOG.json +111 -0
  2. package/NOTICE +20 -0
  3. package/README.md +1 -1
  4. package/dist/core/loop.js +18 -2
  5. package/dist/core/storage.js +5 -0
  6. package/dist/geometry/meshBuilder.d.ts +1 -1
  7. package/dist/geometry/meshBuilder.js +6 -2
  8. package/dist/index.d.ts +69 -4
  9. package/dist/index.js +112 -2
  10. package/dist/input/input.js +6 -1
  11. package/dist/math/exact.d.ts +13 -3
  12. package/dist/math/exact.js +68 -3
  13. package/dist/math/frustum.d.ts +17 -2
  14. package/dist/math/frustum.js +24 -0
  15. package/dist/render/backend/api.d.ts +34 -1
  16. package/dist/render/backend/api.js +22 -1
  17. package/dist/render/backend/budget.d.ts +7 -6
  18. package/dist/render/backend/budget.js +7 -6
  19. package/dist/render/backend/createRenderer.d.ts +57 -0
  20. package/dist/render/backend/createRenderer.js +51 -2
  21. package/dist/render/backend/materialChanges.d.ts +41 -0
  22. package/dist/render/backend/materialChanges.js +38 -0
  23. package/dist/render/backend/select.js +8 -0
  24. package/dist/render/backend/webgl2/renderer.d.ts +80 -1
  25. package/dist/render/backend/webgl2/renderer.js +208 -10
  26. package/dist/render/backend/webgpu/buffers.d.ts +20 -1
  27. package/dist/render/backend/webgpu/buffers.js +116 -58
  28. package/dist/render/backend/webgpu/fieldCompose.d.ts +156 -0
  29. package/dist/render/backend/webgpu/fieldCompose.js +396 -0
  30. package/dist/render/backend/webgpu/giFieldPass.d.ts +66 -0
  31. package/dist/render/backend/webgpu/giFieldPass.js +137 -0
  32. package/dist/render/backend/webgpu/gpuDrivenPass.d.ts +728 -0
  33. package/dist/render/backend/webgpu/gpuDrivenPass.js +2473 -0
  34. package/dist/render/backend/webgpu/linePass.d.ts +4 -14
  35. package/dist/render/backend/webgpu/linePass.js +4 -33
  36. package/dist/render/backend/webgpu/probeBake.d.ts +148 -0
  37. package/dist/render/backend/webgpu/probeBake.js +493 -0
  38. package/dist/render/backend/webgpu/renderer.d.ts +379 -24
  39. package/dist/render/backend/webgpu/renderer.js +1369 -76
  40. package/dist/render/backend/webgpu/scatterPass.js +15 -1
  41. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +19 -9
  42. package/dist/render/backend/webgpu/surfaceTexturePass.js +58 -32
  43. package/dist/render/causticsRenderer.d.ts +3 -1
  44. package/dist/render/causticsRenderer.js +4 -1
  45. package/dist/render/depthConvention.d.ts +12 -0
  46. package/dist/render/depthConvention.js +14 -0
  47. package/dist/render/deviceFeatures.d.ts +32 -0
  48. package/dist/render/deviceFeatures.js +41 -0
  49. package/dist/render/fog.d.ts +79 -0
  50. package/dist/render/fog.js +138 -0
  51. package/dist/render/frame/alias.d.ts +30 -0
  52. package/dist/render/frame/alias.js +61 -0
  53. package/dist/render/frame/deps.d.ts +37 -0
  54. package/dist/render/frame/deps.js +122 -0
  55. package/dist/render/frame/flushGraph.d.ts +43 -0
  56. package/dist/render/frame/flushGraph.js +84 -0
  57. package/dist/render/frame/graphSchedule.d.ts +49 -0
  58. package/dist/render/frame/graphSchedule.js +120 -0
  59. package/dist/render/frame/index.d.ts +15 -0
  60. package/dist/render/frame/index.js +8 -0
  61. package/dist/render/frame/lifetime.d.ts +24 -0
  62. package/dist/render/frame/lifetime.js +36 -0
  63. package/dist/render/frame/passTimings.d.ts +42 -0
  64. package/dist/render/frame/passTimings.js +38 -0
  65. package/dist/render/frame/validate.d.ts +15 -0
  66. package/dist/render/frame/validate.js +22 -0
  67. package/dist/render/frame/virtual.d.ts +44 -0
  68. package/dist/render/frame/virtual.js +61 -0
  69. package/dist/render/gi/chain.d.ts +82 -0
  70. package/dist/render/gi/chain.js +135 -0
  71. package/dist/render/gi/denoise.d.ts +114 -0
  72. package/dist/render/gi/denoise.js +169 -0
  73. package/dist/render/gi/fieldScene.d.ts +125 -0
  74. package/dist/render/gi/fieldScene.js +219 -0
  75. package/dist/render/gi/globalField.d.ts +157 -0
  76. package/dist/render/gi/globalField.js +373 -0
  77. package/dist/render/gi/probeGridFit.d.ts +24 -0
  78. package/dist/render/gi/probeGridFit.js +71 -0
  79. package/dist/render/gi/probeTrace.d.ts +82 -0
  80. package/dist/render/gi/probeTrace.js +128 -0
  81. package/dist/render/gi/probeVolume.d.ts +136 -0
  82. package/dist/render/gi/probeVolume.js +191 -0
  83. package/dist/render/gi/reflection.d.ts +73 -0
  84. package/dist/render/gi/reflection.js +109 -0
  85. package/dist/render/gi/traceField.d.ts +91 -0
  86. package/dist/render/gi/traceField.js +129 -0
  87. package/dist/render/gi/traceScreen.d.ts +68 -0
  88. package/dist/render/gi/traceScreen.js +82 -0
  89. package/dist/render/gpuScene.d.ts +38 -0
  90. package/dist/render/gpuScene.js +41 -0
  91. package/dist/render/gpudriven/alphaTest.d.ts +28 -0
  92. package/dist/render/gpudriven/alphaTest.js +32 -0
  93. package/dist/render/gpudriven/clusterUpload.d.ts +59 -0
  94. package/dist/render/gpudriven/clusterUpload.js +75 -0
  95. package/dist/render/gpudriven/clusterWorld.d.ts +57 -0
  96. package/dist/render/gpudriven/clusterWorld.js +155 -0
  97. package/dist/render/gpudriven/compact.d.ts +51 -0
  98. package/dist/render/gpudriven/compact.js +68 -0
  99. package/dist/render/gpudriven/cullClusters.d.ts +129 -0
  100. package/dist/render/gpudriven/cullClusters.js +237 -0
  101. package/dist/render/gpudriven/decodeTables.d.ts +64 -0
  102. package/dist/render/gpudriven/decodeTables.js +219 -0
  103. package/dist/render/gpudriven/frustum.d.ts +25 -0
  104. package/dist/render/gpudriven/frustum.js +59 -0
  105. package/dist/render/gpudriven/geometryHarness.d.ts +15 -0
  106. package/dist/render/gpudriven/geometryHarness.js +31 -0
  107. package/dist/render/gpudriven/hzb.d.ts +60 -0
  108. package/dist/render/gpudriven/hzb.js +97 -0
  109. package/dist/render/gpudriven/ibl.d.ts +85 -0
  110. package/dist/render/gpudriven/ibl.js +129 -0
  111. package/dist/render/gpudriven/indirect.d.ts +67 -0
  112. package/dist/render/gpudriven/indirect.js +77 -0
  113. package/dist/render/gpudriven/instances.d.ts +43 -0
  114. package/dist/render/gpudriven/instances.js +172 -0
  115. package/dist/render/gpudriven/lit.d.ts +165 -0
  116. package/dist/render/gpudriven/lit.js +165 -0
  117. package/dist/render/gpudriven/lodCut.d.ts +28 -0
  118. package/dist/render/gpudriven/lodCut.js +38 -0
  119. package/dist/render/gpudriven/materialBin.d.ts +60 -0
  120. package/dist/render/gpudriven/materialBin.js +108 -0
  121. package/dist/render/gpudriven/materialTable.d.ts +88 -0
  122. package/dist/render/gpudriven/materialTable.js +115 -0
  123. package/dist/render/gpudriven/pipeline.d.ts +135 -0
  124. package/dist/render/gpudriven/pipeline.js +237 -0
  125. package/dist/render/gpudriven/rangeAlloc.d.ts +50 -0
  126. package/dist/render/gpudriven/rangeAlloc.js +112 -0
  127. package/dist/render/gpudriven/sceneUpload.d.ts +96 -0
  128. package/dist/render/gpudriven/sceneUpload.js +115 -0
  129. package/dist/render/gpudriven/shadeBins.d.ts +92 -0
  130. package/dist/render/gpudriven/shadeBins.js +182 -0
  131. package/dist/render/gpudriven/shadow.d.ts +136 -0
  132. package/dist/render/gpudriven/shadow.js +224 -0
  133. package/dist/render/gpudriven/shadowCamera.d.ts +87 -0
  134. package/dist/render/gpudriven/shadowCamera.js +345 -0
  135. package/dist/render/gpudriven/streamScene.d.ts +194 -0
  136. package/dist/render/gpudriven/streamScene.js +459 -0
  137. package/dist/render/gpudriven/surfaceFrame.d.ts +25 -0
  138. package/dist/render/gpudriven/surfaceFrame.js +51 -0
  139. package/dist/render/gpudriven/twoPhase.d.ts +122 -0
  140. package/dist/render/gpudriven/twoPhase.js +160 -0
  141. package/dist/render/gpudriven/visbuffer.d.ts +25 -0
  142. package/dist/render/gpudriven/visbuffer.js +37 -0
  143. package/dist/render/halfFloat.d.ts +10 -0
  144. package/dist/render/halfFloat.js +23 -4
  145. package/dist/render/inference/attentionTiled.d.ts +27 -0
  146. package/dist/render/inference/attentionTiled.js +117 -0
  147. package/dist/render/inference/denseKernels.d.ts +29 -0
  148. package/dist/render/inference/denseKernels.js +312 -0
  149. package/dist/render/inference/device.d.ts +21 -0
  150. package/dist/render/inference/device.js +32 -0
  151. package/dist/render/inference/deviceGraph.d.ts +34 -0
  152. package/dist/render/inference/deviceGraph.js +17 -0
  153. package/dist/render/inference/fuse.d.ts +15 -0
  154. package/dist/render/inference/fuse.js +37 -0
  155. package/dist/render/inference/kernelKit.d.ts +52 -0
  156. package/dist/render/inference/kernelKit.js +70 -0
  157. package/dist/render/inference/kernels.d.ts +3 -0
  158. package/dist/render/inference/kernels.js +11 -0
  159. package/dist/render/inference/plan.d.ts +13 -0
  160. package/dist/render/inference/plan.js +18 -0
  161. package/dist/render/inference/reuse.d.ts +39 -0
  162. package/dist/render/inference/reuse.js +65 -0
  163. package/dist/render/inference/runner.d.ts +31 -0
  164. package/dist/render/inference/runner.js +175 -0
  165. package/dist/render/inference/schedule.d.ts +39 -0
  166. package/dist/render/inference/schedule.js +59 -0
  167. package/dist/render/inference/shapeKernels.d.ts +10 -0
  168. package/dist/render/inference/shapeKernels.js +80 -0
  169. package/dist/render/inference/spatialKernels.d.ts +21 -0
  170. package/dist/render/inference/spatialKernels.js +179 -0
  171. package/dist/render/inference/splatRaster.wgsl.d.ts +75 -0
  172. package/dist/render/inference/splatRaster.wgsl.js +337 -0
  173. package/dist/render/inference/splatTiles.d.ts +79 -0
  174. package/dist/render/inference/splatTiles.js +161 -0
  175. package/dist/render/lineBatch.d.ts +2 -5
  176. package/dist/render/lineBatch.js +4 -24
  177. package/dist/render/mesh.d.ts +4 -0
  178. package/dist/render/mesh.js +45 -1
  179. package/dist/render/pass.d.ts +145 -8
  180. package/dist/render/prefilterEnvMap.d.ts +22 -0
  181. package/dist/render/prefilterEnvMap.js +31 -0
  182. package/dist/render/readback.d.ts +30 -0
  183. package/dist/render/readback.js +47 -0
  184. package/dist/render/recon/clamp.d.ts +36 -0
  185. package/dist/render/recon/clamp.js +92 -0
  186. package/dist/render/recon/disocclusion.d.ts +60 -0
  187. package/dist/render/recon/disocclusion.js +109 -0
  188. package/dist/render/recon/frameGen.d.ts +81 -0
  189. package/dist/render/recon/frameGen.js +150 -0
  190. package/dist/render/recon/frameSizes.d.ts +34 -0
  191. package/dist/render/recon/frameSizes.js +58 -0
  192. package/dist/render/recon/jitter.d.ts +60 -0
  193. package/dist/render/recon/jitter.js +118 -0
  194. package/dist/render/recon/motionVectors.d.ts +55 -0
  195. package/dist/render/recon/motionVectors.js +107 -0
  196. package/dist/render/recon/reproject.d.ts +41 -0
  197. package/dist/render/recon/reproject.js +131 -0
  198. package/dist/render/recon/resolve.d.ts +90 -0
  199. package/dist/render/recon/resolve.js +313 -0
  200. package/dist/render/recon/tier.d.ts +24 -0
  201. package/dist/render/recon/tier.js +8 -0
  202. package/dist/render/reflectionProbe.d.ts +26 -0
  203. package/dist/render/reflectionProbe.js +33 -0
  204. package/dist/render/renderQuality.d.ts +69 -0
  205. package/dist/render/renderQuality.js +22 -0
  206. package/dist/render/screenSpaceReflection.d.ts +23 -0
  207. package/dist/render/screenSpaceReflection.js +22 -3
  208. package/dist/render/sdfFont.js +2 -0
  209. package/dist/render/segmentQuads.d.ts +13 -0
  210. package/dist/render/segmentQuads.js +32 -0
  211. package/dist/render/shaders/flat/lobes.d.ts +27 -0
  212. package/dist/render/shaders/flat/lobes.js +33 -3
  213. package/dist/render/shaders/flat/preamble.d.ts +7 -0
  214. package/dist/render/shaders/flat/preamble.js +24 -1
  215. package/dist/render/shaders/flat/probeGrid.d.ts +1 -1
  216. package/dist/render/shaders/flat/probeGrid.js +71 -2
  217. package/dist/render/shaders/generated/flat.wgsl.d.ts +48 -8
  218. package/dist/render/shaders/generated/flat.wgsl.js +64 -24
  219. package/dist/render/shaders/gi/composeField.wgsl.d.ts +56 -0
  220. package/dist/render/shaders/gi/composeField.wgsl.js +231 -0
  221. package/dist/render/shaders/gi/marchField.wgsl.d.ts +30 -0
  222. package/dist/render/shaders/gi/marchField.wgsl.js +141 -0
  223. package/dist/render/shaders/gi/probeBake.wgsl.d.ts +113 -0
  224. package/dist/render/shaders/gi/probeBake.wgsl.js +635 -0
  225. package/dist/render/shaders/gi/probeTrace.wgsl.d.ts +72 -0
  226. package/dist/render/shaders/gi/probeTrace.wgsl.js +261 -0
  227. package/dist/render/shaders/gi/probeVolume.wgsl.d.ts +49 -0
  228. package/dist/render/shaders/gi/probeVolume.wgsl.js +242 -0
  229. package/dist/render/shaders/gi/sampleField.wgsl.d.ts +32 -0
  230. package/dist/render/shaders/gi/sampleField.wgsl.js +134 -0
  231. package/dist/render/shaders/gpudriven/blendRaster.wgsl.d.ts +26 -0
  232. package/dist/render/shaders/gpudriven/blendRaster.wgsl.js +339 -0
  233. package/dist/render/shaders/gpudriven/compact.wgsl.d.ts +29 -0
  234. package/dist/render/shaders/gpudriven/compact.wgsl.js +58 -0
  235. package/dist/render/shaders/gpudriven/cull.wgsl.d.ts +90 -0
  236. package/dist/render/shaders/gpudriven/cull.wgsl.js +423 -0
  237. package/dist/render/shaders/gpudriven/decode.wgsl.d.ts +72 -0
  238. package/dist/render/shaders/gpudriven/decode.wgsl.js +349 -0
  239. package/dist/render/shaders/gpudriven/environment.wgsl.d.ts +44 -0
  240. package/dist/render/shaders/gpudriven/environment.wgsl.js +137 -0
  241. package/dist/render/shaders/gpudriven/fog.wgsl.d.ts +28 -0
  242. package/dist/render/shaders/gpudriven/fog.wgsl.js +107 -0
  243. package/dist/render/shaders/gpudriven/frameBlock.wgsl.d.ts +13 -0
  244. package/dist/render/shaders/gpudriven/frameBlock.wgsl.js +62 -0
  245. package/dist/render/shaders/gpudriven/materialBin.wgsl.d.ts +51 -0
  246. package/dist/render/shaders/gpudriven/materialBin.wgsl.js +112 -0
  247. package/dist/render/shaders/gpudriven/materialTable.wgsl.d.ts +25 -0
  248. package/dist/render/shaders/gpudriven/materialTable.wgsl.js +43 -0
  249. package/dist/render/shaders/gpudriven/meshTransform.wgsl.d.ts +18 -0
  250. package/dist/render/shaders/gpudriven/meshTransform.wgsl.js +37 -0
  251. package/dist/render/shaders/gpudriven/present.wgsl.d.ts +43 -0
  252. package/dist/render/shaders/gpudriven/present.wgsl.js +127 -0
  253. package/dist/render/shaders/gpudriven/shade.wgsl.d.ts +127 -0
  254. package/dist/render/shaders/gpudriven/shade.wgsl.js +874 -0
  255. package/dist/render/shaders/gpudriven/shadow.wgsl.d.ts +44 -0
  256. package/dist/render/shaders/gpudriven/shadow.wgsl.js +308 -0
  257. package/dist/render/shaders/gpudriven/visbufferRaster.wgsl.d.ts +28 -0
  258. package/dist/render/shaders/gpudriven/visbufferRaster.wgsl.js +245 -0
  259. package/dist/render/shaders/network.wgsl.d.ts +101 -0
  260. package/dist/render/shaders/network.wgsl.js +206 -0
  261. package/dist/render/shaders/recon/motion.wgsl.d.ts +44 -0
  262. package/dist/render/shaders/recon/motion.wgsl.js +96 -0
  263. package/dist/render/shaders/recon/resolve.wgsl.d.ts +51 -0
  264. package/dist/render/shaders/recon/resolve.wgsl.js +733 -0
  265. package/dist/render/temporalAa.js +8 -33
  266. package/dist/render/textLayout.js +12 -6
  267. package/dist/render/textRenderer.d.ts +3 -1
  268. package/dist/render/textRenderer.js +4 -1
  269. package/dist/render/translucentQueue.d.ts +6 -0
  270. package/dist/render/translucentQueue.js +11 -0
  271. package/dist/render/windStreakRenderer.d.ts +2 -1
  272. package/dist/render/windStreakRenderer.js +3 -1
  273. package/dist/world/cell.d.ts +63 -0
  274. package/dist/world/cell.js +181 -0
  275. package/dist/world/cellStream.d.ts +103 -0
  276. package/dist/world/cellStream.js +198 -0
  277. package/dist/world/freeze.d.ts +81 -0
  278. package/dist/world/freeze.js +114 -0
  279. package/dist/world/rebase.d.ts +29 -0
  280. package/dist/world/rebase.js +41 -0
  281. package/package.json +8 -3
  282. package/scripts/frames.mjs +92 -0
  283. package/scripts/gpuCompute.mjs +323 -0
  284. package/scripts/png.d.mts +16 -0
  285. package/scripts/png.mjs +25 -0
  286. package/src/core/loop.ts +19 -2
  287. package/src/core/storage.ts +5 -0
  288. package/src/geometry/meshBuilder.ts +5 -1
  289. package/src/index.ts +246 -4
  290. package/src/input/input.ts +6 -1
  291. package/src/math/exact.ts +69 -3
  292. package/src/math/frustum.ts +52 -3
  293. package/src/render/backend/api.ts +42 -1
  294. package/src/render/backend/budget.ts +7 -6
  295. package/src/render/backend/createRenderer.ts +92 -3
  296. package/src/render/backend/materialChanges.ts +54 -0
  297. package/src/render/backend/select.ts +8 -0
  298. package/src/render/backend/webgl2/renderer.ts +211 -9
  299. package/src/render/backend/webgpu/buffers.ts +182 -69
  300. package/src/render/backend/webgpu/fieldCompose.ts +482 -0
  301. package/src/render/backend/webgpu/giFieldPass.ts +200 -0
  302. package/src/render/backend/webgpu/gpuDrivenPass.ts +2993 -0
  303. package/src/render/backend/webgpu/linePass.ts +4 -39
  304. package/src/render/backend/webgpu/probeBake.ts +588 -0
  305. package/src/render/backend/webgpu/renderer.ts +1481 -76
  306. package/src/render/backend/webgpu/scatterPass.ts +15 -1
  307. package/src/render/backend/webgpu/surfaceTexturePass.ts +63 -36
  308. package/src/render/causticsRenderer.ts +5 -2
  309. package/src/render/depthConvention.ts +15 -0
  310. package/src/render/deviceFeatures.ts +77 -0
  311. package/src/render/fog.ts +185 -0
  312. package/src/render/frame/alias.ts +97 -0
  313. package/src/render/frame/deps.ts +155 -0
  314. package/src/render/frame/flushGraph.ts +135 -0
  315. package/src/render/frame/graphSchedule.ts +170 -0
  316. package/src/render/frame/index.ts +52 -0
  317. package/src/render/frame/lifetime.ts +61 -0
  318. package/src/render/frame/passTimings.ts +69 -0
  319. package/src/render/frame/validate.ts +40 -0
  320. package/src/render/frame/virtual.ts +91 -0
  321. package/src/render/gi/chain.ts +225 -0
  322. package/src/render/gi/denoise.ts +276 -0
  323. package/src/render/gi/fieldScene.ts +264 -0
  324. package/src/render/gi/globalField.ts +543 -0
  325. package/src/render/gi/probeGridFit.ts +89 -0
  326. package/src/render/gi/probeTrace.ts +143 -0
  327. package/src/render/gi/probeVolume.ts +288 -0
  328. package/src/render/gi/reflection.ts +163 -0
  329. package/src/render/gi/traceField.ts +223 -0
  330. package/src/render/gi/traceScreen.ts +106 -0
  331. package/src/render/gpuScene.ts +80 -0
  332. package/src/render/gpudriven/alphaTest.ts +33 -0
  333. package/src/render/gpudriven/clusterUpload.ts +123 -0
  334. package/src/render/gpudriven/clusterWorld.ts +239 -0
  335. package/src/render/gpudriven/compact.ts +80 -0
  336. package/src/render/gpudriven/cullClusters.ts +333 -0
  337. package/src/render/gpudriven/decodeTables.ts +332 -0
  338. package/src/render/gpudriven/frustum.ts +70 -0
  339. package/src/render/gpudriven/geometryHarness.ts +51 -0
  340. package/src/render/gpudriven/hzb.ts +113 -0
  341. package/src/render/gpudriven/ibl.ts +147 -0
  342. package/src/render/gpudriven/indirect.ts +89 -0
  343. package/src/render/gpudriven/instances.ts +234 -0
  344. package/src/render/gpudriven/lit.ts +329 -0
  345. package/src/render/gpudriven/lodCut.ts +62 -0
  346. package/src/render/gpudriven/materialBin.ts +131 -0
  347. package/src/render/gpudriven/materialTable.ts +164 -0
  348. package/src/render/gpudriven/pipeline.ts +336 -0
  349. package/src/render/gpudriven/rangeAlloc.ts +122 -0
  350. package/src/render/gpudriven/sceneUpload.ts +173 -0
  351. package/src/render/gpudriven/shadeBins.ts +222 -0
  352. package/src/render/gpudriven/shadow.ts +285 -0
  353. package/src/render/gpudriven/shadowCamera.ts +406 -0
  354. package/src/render/gpudriven/streamScene.ts +575 -0
  355. package/src/render/gpudriven/surfaceFrame.ts +68 -0
  356. package/src/render/gpudriven/twoPhase.ts +214 -0
  357. package/src/render/gpudriven/visbuffer.ts +41 -0
  358. package/src/render/halfFloat.ts +22 -4
  359. package/src/render/inference/attentionTiled.ts +132 -0
  360. package/src/render/inference/denseKernels.ts +369 -0
  361. package/src/render/inference/device.ts +44 -0
  362. package/src/render/inference/deviceGraph.ts +39 -0
  363. package/src/render/inference/fuse.ts +47 -0
  364. package/src/render/inference/kernelKit.ts +120 -0
  365. package/src/render/inference/kernels.ts +18 -0
  366. package/src/render/inference/plan.ts +26 -0
  367. package/src/render/inference/reuse.ts +79 -0
  368. package/src/render/inference/runner.ts +204 -0
  369. package/src/render/inference/schedule.ts +80 -0
  370. package/src/render/inference/shapeKernels.ts +112 -0
  371. package/src/render/inference/spatialKernels.ts +220 -0
  372. package/src/render/inference/splatRaster.wgsl.ts +348 -0
  373. package/src/render/inference/splatTiles.ts +186 -0
  374. package/src/render/lineBatch.ts +4 -24
  375. package/src/render/mesh.ts +46 -1
  376. package/src/render/pass.ts +155 -10
  377. package/src/render/prefilterEnvMap.ts +33 -0
  378. package/src/render/readback.ts +73 -0
  379. package/src/render/recon/clamp.ts +113 -0
  380. package/src/render/recon/disocclusion.ts +143 -0
  381. package/src/render/recon/frameGen.ts +191 -0
  382. package/src/render/recon/frameSizes.ts +64 -0
  383. package/src/render/recon/jitter.ts +126 -0
  384. package/src/render/recon/motionVectors.ts +138 -0
  385. package/src/render/recon/reproject.ts +179 -0
  386. package/src/render/recon/resolve.ts +444 -0
  387. package/src/render/recon/tier.ts +31 -0
  388. package/src/render/reflectionProbe.ts +34 -0
  389. package/src/render/renderQuality.ts +87 -0
  390. package/src/render/screenSpaceReflection.ts +37 -3
  391. package/src/render/sdfFont.ts +2 -0
  392. package/src/render/segmentQuads.ts +39 -0
  393. package/src/render/shaders/flat/lobes.ts +35 -3
  394. package/src/render/shaders/flat/preamble.ts +25 -1
  395. package/src/render/shaders/flat/probeGrid.ts +77 -2
  396. package/src/render/shaders/generated/flat.wgsl.ts +64 -24
  397. package/src/render/shaders/gi/composeField.wgsl.ts +239 -0
  398. package/src/render/shaders/gi/marchField.wgsl.ts +146 -0
  399. package/src/render/shaders/gi/probeBake.wgsl.ts +649 -0
  400. package/src/render/shaders/gi/probeTrace.wgsl.ts +269 -0
  401. package/src/render/shaders/gi/probeVolume.wgsl.ts +246 -0
  402. package/src/render/shaders/gi/sampleField.wgsl.ts +140 -0
  403. package/src/render/shaders/gpudriven/blendRaster.wgsl.ts +343 -0
  404. package/src/render/shaders/gpudriven/compact.wgsl.ts +59 -0
  405. package/src/render/shaders/gpudriven/cull.wgsl.ts +433 -0
  406. package/src/render/shaders/gpudriven/decode.wgsl.ts +373 -0
  407. package/src/render/shaders/gpudriven/environment.wgsl.ts +141 -0
  408. package/src/render/shaders/gpudriven/fog.wgsl.ts +110 -0
  409. package/src/render/shaders/gpudriven/frameBlock.wgsl.ts +62 -0
  410. package/src/render/shaders/gpudriven/materialBin.wgsl.ts +116 -0
  411. package/src/render/shaders/gpudriven/materialTable.wgsl.ts +46 -0
  412. package/src/render/shaders/gpudriven/meshTransform.wgsl.ts +37 -0
  413. package/src/render/shaders/gpudriven/present.wgsl.ts +129 -0
  414. package/src/render/shaders/gpudriven/shade.wgsl.ts +884 -0
  415. package/src/render/shaders/gpudriven/shadow.wgsl.ts +322 -0
  416. package/src/render/shaders/gpudriven/visbufferRaster.wgsl.ts +251 -0
  417. package/src/render/shaders/network.wgsl.ts +238 -0
  418. package/src/render/shaders/recon/motion.wgsl.ts +101 -0
  419. package/src/render/shaders/recon/resolve.wgsl.ts +741 -0
  420. package/src/render/temporalAa.ts +9 -33
  421. package/src/render/textLayout.ts +12 -6
  422. package/src/render/textRenderer.ts +5 -2
  423. package/src/render/translucentQueue.ts +11 -0
  424. package/src/render/windStreakRenderer.ts +4 -2
  425. package/src/world/cell.ts +231 -0
  426. package/src/world/cellStream.ts +278 -0
  427. package/src/world/freeze.ts +157 -0
  428. package/src/world/rebase.ts +56 -0
@@ -245,7 +245,21 @@ export function scatterDepthPipeline(
245
245
  targets: [],
246
246
  },
247
247
  primitive: { topology: 'triangle-list', cullMode: 'none' },
248
- depthStencil: { format: shadowFormat, depthWriteEnabled: true, depthCompare: DEPTH_COMPARE },
248
+ /*
249
+ * **`less`, not the frame's `DEPTH_COMPARE`, because a shadow map is not reversed.**
250
+ * `depthConvention.ts` says so in as many words: the cascades are orthographic, where depth
251
+ * is already linear and a float buffer gains nothing, so `SHADOW_DEPTH_CLEAR` is 1 and
252
+ * `beginShadowPass` clears to 1. This pipeline took the scene's compare, which under
253
+ * `REVERSED_DEPTH` is `greater` — and a fragment at depth 0.3 tested with `greater` against a
254
+ * map cleared to 1 fails. **Nothing was ever written**, so a scatter batch cast no shadow at
255
+ * all on this backend: no error, no warning, an ordinary-looking frame.
256
+ *
257
+ * `depthPass.ts` has had `'less'` for the mesh path all along, which is why meshes cast and
258
+ * scatter did not, and why it took a scene whose only caster is a scatter batch to show it.
259
+ * `demo/windField.ts` is that scene — turning directional shadows off there changed 2,628
260
+ * pixels on WebGL2 and 0 on WebGPU.
261
+ */
262
+ depthStencil: { format: shadowFormat, depthWriteEnabled: true, depthCompare: 'less' },
249
263
  }));
250
264
  }
251
265
 
@@ -163,15 +163,15 @@ function sourceSize(source: TexImageSource): { width: number; height: number } {
163
163
 
164
164
  /** An image uploaded once and bound per material. */
165
165
  export class GpuSurfaceTexture {
166
- readonly view: GPUTextureView;
167
166
  readonly sampler: GPUSampler;
168
167
 
169
168
  private texture: GPUTexture | null;
169
+ private current: GPUTextureView;
170
170
  private readonly format: GPUTextureFormat;
171
171
  private readonly mipmapped: boolean;
172
- private readonly levels: number;
173
- private readonly width: number;
174
- private readonly height: number;
172
+ private levels: number;
173
+ private width: number;
174
+ private height: number;
175
175
 
176
176
  constructor(
177
177
  private readonly device: GPUDevice,
@@ -180,36 +180,15 @@ export class GpuSurfaceTexture {
180
180
  options: SurfaceTextureOptions = {},
181
181
  ) {
182
182
  const { width, height } = sourceSize(source);
183
- this.width = width;
184
- this.height = height;
185
183
  this.mipmapped = options.mipmap ?? true;
186
- this.levels = this.mipmapped ? mipLevelCount(width, height) : 1;
187
184
  /* `SRGB8_ALPHA8`'s equivalent. Decoded in the sampler, before filtering, which is the only
188
185
  place it is correct — `surfaceTexture.ts` makes the argument in full. */
189
186
  this.format = (options.colorSpace ?? 'linear') === 'srgb' ? 'rgba8unorm-srgb' : 'rgba8unorm';
190
-
191
- this.texture = device.createTexture({
192
- label: 'surface.texture',
193
- size: [width, height],
194
- format: this.format,
195
- mipLevelCount: this.levels,
196
- /*
197
- * **`RENDER_ATTACHMENT` is required by `copyExternalImageToTexture`, not by the mip
198
- * chain**, and the difference matters enough to state.
199
- *
200
- * This used to say the flag was there "because building the chain renders into levels 1
201
- * and below", which is true and is not the reason. The upload itself demands it: Dawn
202
- * rejects a destination without `CopyDst | RenderAttachment` outright, whatever its mip
203
- * count. Gating it on `levels > 1` therefore breaks every unmipped texture, and the read
204
- * of the old comment that led there cost a broken build to disprove.
205
- *
206
- * So it cannot be dropped to make a sampled-only allocation, and anything that wants one
207
- * has to leave `copyExternalImageToTexture` behind and write raw pixels instead, which
208
- * trades a driver cost for a CPU readback.
209
- */
210
- usage: 0x2 | 0x4 | 0x10, // COPY_DST | TEXTURE_BINDING | RENDER_ATTACHMENT
211
- });
212
- this.view = this.texture.createView();
187
+ this.width = width;
188
+ this.height = height;
189
+ this.levels = this.mipmapped ? mipLevelCount(width, height) : 1;
190
+ this.texture = this.allocate();
191
+ this.current = this.texture.createView();
213
192
 
214
193
  const wrap: GPUAddressMode =
215
194
  (options.wrap ?? 'repeat') === 'repeat' ? 'repeat' : 'clamp-to-edge';
@@ -247,16 +226,64 @@ export class GpuSurfaceTexture {
247
226
  this.upload(source);
248
227
  }
249
228
 
229
+ /** The view every binding of this image holds. A new one when `update` changes the size. */
230
+ get view(): GPUTextureView {
231
+ return this.current;
232
+ }
233
+
250
234
  /**
251
- * Replace the pixels, keeping the texture, its view and its sampler.
235
+ * Replace the pixels, keeping the handle and its sampler.
252
236
  *
253
- * The binding a draw loop already holds stays valid, so the swap is a swap rather than a
254
- * rebuild — the same argument `surfaceTexture.ts` makes. Not a hot path: it re-uploads the
255
- * whole image and rebuilds the chain.
237
+ * **At the same size, the texture and its view are kept**, so a binding a draw loop already holds
238
+ * stays valid and the swap is a swap rather than a rebuild — the same argument `surfaceTexture.ts`
239
+ * makes. Not a hot path: it re-uploads the whole image and rebuilds the chain.
240
+ *
241
+ * **At another size, a texture of that size replaces it**, which is what WebGL2's `texImage2D`
242
+ * does by itself. This used to keep the old size and copy that much of the new image — its
243
+ * top-left corner, stretched — and a model loader updating a small preview with the real image
244
+ * hit that whenever the preview's decode won the race. The view is new then, and the texture it
245
+ * replaced is handed back rather than destroyed: a draw recorded earlier in this frame may still
246
+ * read it, so the renderer destroys it once nothing can, and drops every binding of the old view.
256
247
  */
257
- update(source: TexImageSource): void {
258
- if (this.texture === null) return;
248
+ update(source: TexImageSource): GPUTexture | null {
249
+ const replaced = this.texture;
250
+ if (replaced === null) return null;
251
+ const { width, height } = sourceSize(source);
252
+ if (width === this.width && height === this.height) {
253
+ this.upload(source);
254
+ return null;
255
+ }
256
+ this.width = width;
257
+ this.height = height;
258
+ this.levels = this.mipmapped ? mipLevelCount(width, height) : 1;
259
+ this.texture = this.allocate();
260
+ this.current = this.texture.createView();
259
261
  this.upload(source);
262
+ return replaced;
263
+ }
264
+
265
+ private allocate(): GPUTexture {
266
+ return this.device.createTexture({
267
+ label: 'surface.texture',
268
+ size: [this.width, this.height],
269
+ format: this.format,
270
+ mipLevelCount: this.levels,
271
+ /*
272
+ * **`RENDER_ATTACHMENT` is required by `copyExternalImageToTexture`, not by the mip
273
+ * chain**, and the difference matters enough to state.
274
+ *
275
+ * This used to say the flag was there "because building the chain renders into levels 1
276
+ * and below", which is true and is not the reason. The upload itself demands it: Dawn
277
+ * rejects a destination without `CopyDst | RenderAttachment` outright, whatever its mip
278
+ * count. Gating it on `levels > 1` therefore breaks every unmipped texture, and the read
279
+ * of the old comment that led there cost a broken build to disprove.
280
+ *
281
+ * So it cannot be dropped to make a sampled-only allocation, and anything that wants one
282
+ * has to leave `copyExternalImageToTexture` behind and write raw pixels instead, which
283
+ * trades a driver cost for a CPU readback.
284
+ */
285
+ usage: 0x2 | 0x4 | 0x10, // COPY_DST | TEXTURE_BINDING | RENDER_ATTACHMENT
286
+ });
260
287
  }
261
288
 
262
289
  dispose(): void {
@@ -100,6 +100,8 @@ export class CausticsRenderer {
100
100
  * pass: this is light arriving at a surface, not the surface.
101
101
  *
102
102
  * @param strength scales the whole effect; 1 is the calibrated default.
103
+ *
104
+ * Answers whether it drew, which the renderer counts on its frame budget.
103
105
  */
104
106
  draw(
105
107
  gl: WebGL2RenderingContext,
@@ -113,8 +115,8 @@ export class CausticsRenderer {
113
115
  windX = 0,
114
116
  windZ = 0,
115
117
  strength = 1,
116
- ): void {
117
- if (this.vertexCount === 0) return;
118
+ ): boolean {
119
+ if (this.vertexCount === 0) return false;
118
120
  const u = this.uniforms;
119
121
  gl.useProgram(this.program);
120
122
  gl.uniformMatrix4fv(u['uViewProj'] ?? null, false, camera.viewProjection);
@@ -161,6 +163,7 @@ export class CausticsRenderer {
161
163
  gl.enable(gl.CULL_FACE);
162
164
  gl.depthMask(true);
163
165
  gl.disable(gl.BLEND);
166
+ return true;
164
167
  }
165
168
 
166
169
  dispose(gl: WebGL2RenderingContext): void {
@@ -153,6 +153,21 @@ export function glslSceneDepthToNdc(depth: string): string {
153
153
  return REVERSED_DEPTH ? `(1.0 - ${depth} * 2.0)` : `(${depth} * 2.0 - 1.0)`;
154
154
  }
155
155
 
156
+ /**
157
+ * The same conversion as WGSL, for the hand-written compute shaders that have no GLSL to generate
158
+ * from.
159
+ *
160
+ * **It is the same text, and that is the point of it being here.** The reconstruction resolve picks
161
+ * the *nearest* of nine depths and dilates that texel's motion, which is the rule every temporal
162
+ * upscaler uses because an edge's motion belongs to the nearer of the two surfaces meeting there.
163
+ * Under a reversed buffer the nearest is the largest number, so a shader reading the attachment
164
+ * without this conversion picks the furthest and drags the background's motion over every
165
+ * silhouette — a defect that draws a plausible picture and moves the wrong way.
166
+ */
167
+ export function wgslSceneDepthToNdc(depth: string): string {
168
+ return REVERSED_DEPTH ? `(1.0 - ${depth} * 2.0)` : `(${depth} * 2.0 - 1.0)`;
169
+ }
170
+
156
171
  /**
157
172
  * The clip-space z of the far plane, as GLSL, for geometry that writes a fixed depth.
158
173
  *
@@ -0,0 +1,77 @@
1
+ /**
2
+ * What this device can do, asked once, in one place.
3
+ *
4
+ * **Not `gpuCapability.ts`, which answers a different question.** That module identifies the
5
+ * *part* — a renderer string, matched against families known to struggle — so that a consumer can
6
+ * choose a quality profile. This one reads the optional features and limits a device actually
7
+ * reports. A weak part may support half precision and a strong one may not; neither answer
8
+ * predicts the other.
9
+ *
10
+ * **One place, because the alternative is each subsystem asking the device itself.** Every
11
+ * capability below has more than one consumer waiting for it, and a feature string spelled two
12
+ * ways in two subsystems is a capability that is present in one and absent in the other on the
13
+ * same machine.
14
+ *
15
+ * **Takes the device's shape as a parameter rather than reaching for a global**, which is the
16
+ * rule `AGENTS.md` applies to persistence applied here — and is what lets every branch below be
17
+ * asserted without a graphics device.
18
+ */
19
+ import { indirectSupport } from './backend/api.ts';
20
+
21
+ /** The subset of a device this needs. A real `GPUDevice` satisfies it. */
22
+ export interface DeviceShape {
23
+ readonly features: { has(name: string): boolean };
24
+ readonly limits: Readonly<Record<string, number | undefined>>;
25
+ }
26
+
27
+ export interface GpuCapabilities {
28
+ /** Half-precision arithmetic in shaders. */
29
+ readonly shaderF16: boolean;
30
+ /** Subgroup operations, which make a compute reduction far cheaper where present. */
31
+ readonly subgroups: boolean;
32
+ /** Timestamp queries. Absent by default in more browsers than people expect. */
33
+ readonly timestampQuery: boolean;
34
+ /** Whether this backend can be driven from buffers the GPU wrote. See `indirectSupport`. */
35
+ readonly indirect: boolean;
36
+ /**
37
+ * The three compressed families, reported separately.
38
+ *
39
+ * **Separately, because a device may have exactly one.** Desktop parts carry block compression,
40
+ * mobile carries one of the other two, and a single `compressed: boolean` would be true on
41
+ * every device and useful on none.
42
+ */
43
+ readonly compressedBc: boolean;
44
+ readonly compressedEtc2: boolean;
45
+ readonly compressedAstc: boolean;
46
+ /** Limits, with an absent one reported as 0 so arithmetic on it is safe. */
47
+ readonly maxStorageBufferBytes: number;
48
+ readonly maxComputeWorkgroupStorageBytes: number;
49
+ readonly maxComputeInvocationsPerWorkgroup: number;
50
+ }
51
+
52
+ function limit(device: DeviceShape, name: string): number {
53
+ /*
54
+ * Zero rather than undefined for a limit the device does not report. A consumer sizing a buffer
55
+ * against `undefined` gets NaN and allocates nothing while believing it allocated something;
56
+ * against 0 it gets a refusal it can see.
57
+ */
58
+ return device.limits[name] ?? 0;
59
+ }
60
+
61
+ export function probeCapabilities(
62
+ device: DeviceShape,
63
+ backend: 'webgl2' | 'webgpu',
64
+ ): GpuCapabilities {
65
+ return {
66
+ shaderF16: device.features.has('shader-f16'),
67
+ subgroups: device.features.has('subgroups'),
68
+ timestampQuery: device.features.has('timestamp-query'),
69
+ indirect: indirectSupport(backend),
70
+ compressedBc: device.features.has('texture-compression-bc'),
71
+ compressedEtc2: device.features.has('texture-compression-etc2'),
72
+ compressedAstc: device.features.has('texture-compression-astc'),
73
+ maxStorageBufferBytes: limit(device, 'maxStorageBufferBindingSize'),
74
+ maxComputeWorkgroupStorageBytes: limit(device, 'maxComputeWorkgroupStorageSize'),
75
+ maxComputeInvocationsPerWorkgroup: limit(device, 'maxComputeInvocationsPerWorkgroup'),
76
+ };
77
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * The medium between the camera and everything it can see, as arithmetic.
3
+ *
4
+ * **`shaders/fog.ts` was the only copy and it is GLSL**, which two things now make a problem. The
5
+ * GPU-driven pipeline is written in WGSL and cannot include it, so the second pipeline drew no fog
6
+ * at all; and a term that exists only as a shader is a term nothing can disagree with, which is the
7
+ * argument `gpudriven/lit.ts` and `gpudriven/alphaTest.ts` were both written under.
8
+ *
9
+ * So this is the expression in TypeScript, and both shaders are checked against it:
10
+ * `scripts/gpu-parity.mjs` runs the WGSL over generated cases, and `shaders/fog.ts` stays exactly
11
+ * as it is. **This is a second reader of one decision rather than a second decision** — every
12
+ * guard, every constant and the order of the two mixes are the GLSL's, and where a line looks odd
13
+ * that file's header is the long version of why.
14
+ *
15
+ * **What it does not carry** is the uniform block. `shaders/fog.ts` declares ten uniforms because a
16
+ * block that declares everything it uses can be pasted into any program; a function takes them as
17
+ * one argument instead, and the backends keep their own packing.
18
+ */
19
+
20
+ import { resolveAtmosphere, type Atmosphere, type ResolvedAtmosphere } from './atmosphere.ts';
21
+
22
+ /** A colour as three numbers, matching `render/orderIndependent.ts`'s own `Rgb`. */
23
+ export type Rgb = readonly [number, number, number];
24
+
25
+ /** Beer-Lambert through a haze layer that thins with height. `uFogMode` 0. */
26
+ export const MEDIUM_FOG = 0;
27
+ /** A ramp between two distances, which is a look rather than a medium. `uFogMode` 1. */
28
+ export const LINEAR_FOG = 1;
29
+
30
+ export interface FogOptions {
31
+ readonly colour: Rgb;
32
+ /** Extinction per metre **at the camera's own height**, not at the base. */
33
+ readonly density: number;
34
+ /** Reciprocal scale height of the haze, per metre. 0 is a uniform medium. */
35
+ readonly heightFalloff: number;
36
+ /** The camera's height, so a fragment's own height is one subtraction away. */
37
+ readonly eyeY: number;
38
+ readonly underwaterColour: Rgb;
39
+ readonly underwaterDensity: number;
40
+ readonly underwaterFactor: number;
41
+ /** `MEDIUM_FOG` integrates a medium; `LINEAR_FOG` ramps between near and far. */
42
+ readonly mode: number;
43
+ /** Linear mode only: where haze begins, and where it is total. */
44
+ readonly near: number;
45
+ readonly far: number;
46
+ }
47
+
48
+ /**
49
+ * Linear interpolation **written the way `mix` is defined**, which is not the same arithmetic as
50
+ * the fused form.
51
+ *
52
+ * GLSL and WGSL both define `mix(x, y, a)` as `x * (1 - a) + y * a`. Written `x + (y - x) * a` the
53
+ * two agree to within rounding everywhere except the endpoints, where the fused form is off by an
54
+ * ulp — `0.82 + (0.2 - 0.82) * 1` is 0.20000000000000007 rather than 0.2. That is invisible in a
55
+ * picture and fatal in a parity check, and it is what the first run of this file's tests found: the
56
+ * reference and the device have to be the same *expression*, not the same function.
57
+ */
58
+ function mix(x: number, y: number, a: number): number {
59
+ return x * (1 - a) + y * a;
60
+ }
61
+
62
+ /** What distance converges toward: the day's haze, or the water column. */
63
+ export function mediumColour(fog: FogOptions): [number, number, number] {
64
+ const t = fog.underwaterFactor;
65
+ return [
66
+ mix(fog.colour[0], fog.underwaterColour[0], t),
67
+ mix(fog.colour[1], fog.underwaterColour[1], t),
68
+ mix(fog.colour[2], fog.underwaterColour[2], t),
69
+ ];
70
+ }
71
+
72
+ /**
73
+ * How much of a surface standing at `pointY`, `dist` metres away, is lost to the medium between.
74
+ * 0 is perfectly clear, 1 is the medium's own colour.
75
+ */
76
+ export function mediumFog(fog: FogOptions, dist: number, pointY: number): number {
77
+ /*
78
+ * **A depth ramp rather than a medium, for worlds authored against one.** Before `near` the air
79
+ * is perfectly clear, which Beer-Lambert can never be, and height falloff has no meaning here
80
+ * because a ramp is defined on distance alone. `shaders/fog.ts` argues the whole of it.
81
+ */
82
+ if (fog.mode === LINEAR_FOG) {
83
+ /* A consumer asking for a hard cut writes `far === near`; the guard is why that is a cut. */
84
+ const span = Math.max(fog.far - fog.near, 1e-4);
85
+ const ramp = Math.min(1, Math.max(0, (dist - fog.near) / span));
86
+ if (fog.underwaterFactor <= 0) return ramp;
87
+ const wetLinear = fog.underwaterDensity * dist;
88
+ return mix(ramp, 1 - Math.pow(2, -wetLinear * wetLinear * 1.442695), fog.underwaterFactor);
89
+ }
90
+
91
+ const t = (pointY - fog.eyeY) * fog.heightFalloff;
92
+ /*
93
+ * `(1 - e^-t)/t`, which tends to 1 as the ray levels out. **Guarded rather than branched**: at
94
+ * `t = 0` the quotient is 0/0, and 1e-4 of a scale height is 8 mm.
95
+ */
96
+ const denom = Math.abs(t) < 1e-4 ? 1e-4 : t;
97
+ const air = 1 - Math.exp((-fog.density * dist * (1 - Math.exp(-denom))) / denom);
98
+ if (fog.underwaterFactor <= 0) return air;
99
+ /* Water is its own medium and keeps its own curve, so crossing the surface changes nothing
100
+ about how the water column reads. */
101
+ const wet = fog.underwaterDensity * dist;
102
+ return mix(air, 1 - Math.pow(2, -wet * wet * 1.442695), fog.underwaterFactor);
103
+ }
104
+
105
+ /** `FogOptions` with its fields writable, which is what a caller holds and refills a frame at a time. */
106
+ export interface FogTarget {
107
+ colour: [number, number, number];
108
+ density: number;
109
+ heightFalloff: number;
110
+ eyeY: number;
111
+ underwaterColour: [number, number, number];
112
+ underwaterDensity: number;
113
+ underwaterFactor: number;
114
+ mode: number;
115
+ near: number;
116
+ far: number;
117
+ }
118
+
119
+ /** A target for `atmosphereFog`, allocated once, outside the frame loop. */
120
+ export function createFogTarget(): FogTarget {
121
+ return {
122
+ colour: [0, 0, 0],
123
+ density: 0,
124
+ heightFalloff: 0,
125
+ eyeY: 0,
126
+ underwaterColour: [0, 0, 0],
127
+ underwaterDensity: 0,
128
+ underwaterFactor: 0,
129
+ mode: MEDIUM_FOG,
130
+ near: 0,
131
+ far: 1,
132
+ };
133
+ }
134
+
135
+ /** `resolveAtmosphere`'s own target, reused, because this runs a frame at a time. */
136
+ const RESOLVED: ResolvedAtmosphere = {
137
+ fogColor: new Float32Array(3),
138
+ underwaterColor: new Float32Array(3),
139
+ fogDensity: 0,
140
+ fogHeightFalloff: 0,
141
+ fogEyeY: 0,
142
+ fogMode: 0,
143
+ fogNear: 0,
144
+ fogFar: 1,
145
+ underwaterFogDensity: 0,
146
+ underwaterFactor: 0,
147
+ };
148
+
149
+ /**
150
+ * The haze an `Environment` makes at a camera's height, as `GpuDrivenView.fog` takes it.
151
+ *
152
+ * **The forward path's own decision, handed over rather than made again.** A consumer drawing
153
+ * both pipelines in one frame has one environment and needs one haze from it, and
154
+ * `resolveAtmosphere` is where the forward path decides which medium the camera is in and what it
155
+ * looks like — the density at the eye, the linear ramp's two radii, how far under water the eye
156
+ * is. This calls it and copies the answer across field for field, so the two pipelines cannot
157
+ * disagree about where the fog begins or whether the camera is submerged. Both mode numbers are
158
+ * the shaders' `uFogMode`, which is why no mapping stands between them.
159
+ *
160
+ * @param underwater The renderer's `underwaterAtmosphere` quality, which gates the second medium
161
+ * on the forward path; pass the same.
162
+ */
163
+ export function atmosphereFog(
164
+ atmosphere: Atmosphere,
165
+ eyeY: number,
166
+ underwater: boolean,
167
+ out: FogTarget,
168
+ ): FogTarget {
169
+ const medium = resolveAtmosphere(atmosphere, eyeY, underwater, RESOLVED);
170
+ out.colour[0] = medium.fogColor[0] as number;
171
+ out.colour[1] = medium.fogColor[1] as number;
172
+ out.colour[2] = medium.fogColor[2] as number;
173
+ out.density = medium.fogDensity;
174
+ out.heightFalloff = medium.fogHeightFalloff;
175
+ out.eyeY = medium.fogEyeY;
176
+ out.underwaterColour[0] = medium.underwaterColor[0] as number;
177
+ out.underwaterColour[1] = medium.underwaterColor[1] as number;
178
+ out.underwaterColour[2] = medium.underwaterColor[2] as number;
179
+ out.underwaterDensity = medium.underwaterFogDensity;
180
+ out.underwaterFactor = medium.underwaterFactor;
181
+ out.mode = medium.fogMode;
182
+ out.near = medium.fogNear;
183
+ out.far = medium.fogFar;
184
+ return out;
185
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Where each transient resource lives in the frame's arena, so that two which are never alive
3
+ * together share the memory.
4
+ *
5
+ * **First fit over slots, in declaration order.** Not best fit and not a bin-packing search:
6
+ * the input is tens of resources whose order is already meaningful, the packing runs once per
7
+ * frame composition rather than per frame, and a first-fit result is stable — which matters far
8
+ * more than a few saved bytes, because an allocator whose output moves between frames moves
9
+ * every binding with it.
10
+ *
11
+ * A slot records the widest resource it has held and the node at which its current occupant
12
+ * dies. A later resource may take the slot when it is born strictly after that node.
13
+ */
14
+ import type { Lifetimes } from './lifetime.ts';
15
+ import { firstWrite, lastRead } from './lifetime.ts';
16
+ import type { VirtualTable } from './virtual.ts';
17
+ import { virtualBytes } from './virtual.ts';
18
+
19
+ export interface AliasPlan {
20
+ /** Byte offset into the arena per identifier, or -1 when nothing writes it. */
21
+ offsets: Int32Array;
22
+ /** Byte offset of each slot, parallel to `slotEnd` and `slotSize`. */
23
+ slotAt: Int32Array;
24
+ /** Node at which each slot's current occupant is last read. */
25
+ slotEnd: Int32Array;
26
+ /** Widest resource each slot has held. */
27
+ slotSize: Int32Array;
28
+ /** How many slots are in use. */
29
+ slots: number;
30
+ }
31
+
32
+ export function createAliasPlan(capacity: number): AliasPlan {
33
+ return {
34
+ offsets: new Int32Array(capacity),
35
+ slotAt: new Int32Array(capacity),
36
+ slotEnd: new Int32Array(capacity),
37
+ slotSize: new Int32Array(capacity),
38
+ slots: 0,
39
+ };
40
+ }
41
+
42
+ /** Alignment every slot starts on. 256 is the widest uniform-buffer alignment WebGPU requires. */
43
+ const ALIGN = 256;
44
+
45
+ function aligned(bytes: number): number {
46
+ return Math.ceil(bytes / ALIGN) * ALIGN;
47
+ }
48
+
49
+ export function planAliases(table: VirtualTable, lifetimes: Lifetimes, out: AliasPlan): number {
50
+ out.offsets.fill(-1);
51
+ out.slots = 0;
52
+ let total = 0;
53
+
54
+ for (let id = 0; id < table.count; id += 1) {
55
+ const born = firstWrite(lifetimes, id);
56
+ if (born === -1) continue;
57
+ const dies = lastRead(lifetimes, id);
58
+ const size = aligned(virtualBytes(table, id));
59
+
60
+ let placed = -1;
61
+ for (let slot = 0; slot < out.slots; slot += 1) {
62
+ if ((out.slotEnd[slot] ?? 0) < born) {
63
+ placed = slot;
64
+ break;
65
+ }
66
+ }
67
+
68
+ if (placed === -1) {
69
+ placed = out.slots;
70
+ out.slotAt[placed] = total;
71
+ out.slotSize[placed] = size;
72
+ total += size;
73
+ out.slots = placed + 1;
74
+ } else if (size > (out.slotSize[placed] ?? 0)) {
75
+ /*
76
+ * The slot has to widen, and widening in place would overlap whatever follows it. Retiring
77
+ * the slot and opening a new one is correct and costs one wasted slot; compacting would
78
+ * move every offset after it, which is the stability the header refuses to give up.
79
+ */
80
+ out.slotEnd[placed] = Number.MAX_SAFE_INTEGER;
81
+ placed = out.slots;
82
+ out.slotAt[placed] = total;
83
+ out.slotSize[placed] = size;
84
+ total += size;
85
+ out.slots = placed + 1;
86
+ }
87
+
88
+ out.offsets[id] = out.slotAt[placed] ?? 0;
89
+ out.slotEnd[placed] = dies;
90
+ }
91
+
92
+ return total;
93
+ }
94
+
95
+ export function offsetOf(plan: AliasPlan, id: number): number {
96
+ return plan.offsets[id] ?? -1;
97
+ }