@woosh/meep-engine 3.75.0 → 3.77.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 (369) hide show
  1. package/editor/particles/effect/ParticleReferenceSimulation.d.ts +11 -0
  2. package/editor/particles/effect/ParticleReferenceSimulation.d.ts.map +1 -1
  3. package/editor/particles/effect/ParticleReferenceSimulation.js +14 -0
  4. package/editor/view/particles/effect/ParticleGraphEditorView.js +1 -1
  5. package/package.json +1 -1
  6. package/src/engine/asset/GameAssetType.d.ts +1 -0
  7. package/src/engine/asset/GameAssetType.js +5 -1
  8. package/src/engine/asset/loaders/GroomAssetLoader.d.ts +33 -0
  9. package/src/engine/asset/loaders/GroomAssetLoader.d.ts.map +1 -0
  10. package/src/engine/asset/loaders/GroomAssetLoader.js +113 -0
  11. package/src/engine/graphics/ecs/hair/Hair.d.ts +54 -0
  12. package/src/engine/graphics/ecs/hair/Hair.d.ts.map +1 -0
  13. package/src/engine/graphics/ecs/hair/Hair.js +70 -0
  14. package/src/engine/graphics/ecs/hair/HairDynamics.d.ts +63 -0
  15. package/src/engine/graphics/ecs/hair/HairDynamics.d.ts.map +1 -0
  16. package/src/engine/graphics/ecs/hair/HairDynamics.js +82 -0
  17. package/src/engine/graphics/hair/Groom.d.ts +175 -0
  18. package/src/engine/graphics/hair/Groom.d.ts.map +1 -0
  19. package/src/engine/graphics/hair/Groom.js +249 -0
  20. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts +50 -0
  21. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts.map +1 -0
  22. package/src/engine/graphics/hair/GroomSerializationAdapter.js +150 -0
  23. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.d.ts +8 -0
  24. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.d.ts.map +1 -0
  25. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.js +31 -0
  26. package/src/engine/graphics/hair/groom_bind_to_mesh.d.ts +85 -0
  27. package/src/engine/graphics/hair/groom_bind_to_mesh.d.ts.map +1 -0
  28. package/src/engine/graphics/hair/groom_bind_to_mesh.js +216 -0
  29. package/src/engine/graphics/hair/groom_build_group.d.ts +90 -0
  30. package/src/engine/graphics/hair/groom_build_group.d.ts.map +1 -0
  31. package/src/engine/graphics/hair/groom_build_group.js +496 -0
  32. package/src/engine/graphics/hair/groom_build_guides.d.ts +43 -0
  33. package/src/engine/graphics/hair/groom_build_guides.d.ts.map +1 -0
  34. package/src/engine/graphics/hair/groom_build_guides.js +267 -0
  35. package/src/engine/graphics/hair/groom_generate_scalp.d.ts +29 -0
  36. package/src/engine/graphics/hair/groom_generate_scalp.d.ts.map +1 -0
  37. package/src/engine/graphics/hair/groom_generate_scalp.js +184 -0
  38. package/src/engine/graphics/hair/groom_rank_order.d.ts +15 -0
  39. package/src/engine/graphics/hair/groom_rank_order.d.ts.map +1 -0
  40. package/src/engine/graphics/hair/groom_rank_order.js +53 -0
  41. package/src/engine/graphics/hair/groom_root_spacing.d.ts +14 -0
  42. package/src/engine/graphics/hair/groom_root_spacing.d.ts.map +1 -0
  43. package/src/engine/graphics/hair/groom_root_spacing.js +253 -0
  44. package/src/engine/graphics3/HairColliderSystem.d.ts +59 -0
  45. package/src/engine/graphics3/HairColliderSystem.d.ts.map +1 -0
  46. package/src/engine/graphics3/HairColliderSystem.js +270 -0
  47. package/src/engine/graphics3/HairSystem.d.ts +119 -0
  48. package/src/engine/graphics3/HairSystem.d.ts.map +1 -0
  49. package/src/engine/graphics3/HairSystem.js +722 -0
  50. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  51. package/src/shade/assets/textures/hair_scattering.bin +0 -0
  52. package/src/shade/device/pipeline/PipelineEnquiry.d.ts +11 -0
  53. package/src/shade/device/pipeline/PipelineEnquiry.d.ts.map +1 -1
  54. package/src/shade/device/pipeline/PipelineEnquiry.js +13 -0
  55. package/src/shade/playground/hair/index.html +50 -0
  56. package/src/shade/playground/hair/main.d.ts +2 -0
  57. package/src/shade/playground/hair/main.d.ts.map +1 -0
  58. package/src/shade/playground/hair/main.js +297 -0
  59. package/src/shade/playground/hair_skinned/index.html +192 -0
  60. package/src/shade/playground/hair_skinned/main.d.ts +7 -0
  61. package/src/shade/playground/hair_skinned/main.d.ts.map +1 -0
  62. package/src/shade/playground/hair_skinned/main.js +481 -0
  63. package/src/shade/playground/hair_skinned/skinned_hair.d.ts +49 -0
  64. package/src/shade/playground/hair_skinned/skinned_hair.d.ts.map +1 -0
  65. package/src/shade/playground/hair_skinned/skinned_hair.js +362 -0
  66. package/src/shade/playground/screen_probes/README.md +54 -0
  67. package/src/shade/playground/screen_probes/index.html +271 -0
  68. package/src/shade/playground/screen_probes/main.d.ts +8 -0
  69. package/src/shade/playground/screen_probes/main.d.ts.map +1 -0
  70. package/src/shade/playground/screen_probes/main.js +527 -0
  71. package/src/shade/playground/sparks/README.md +36 -0
  72. package/src/shade/playground/sparks/index.html +145 -0
  73. package/src/shade/playground/sparks/main.d.ts +7 -0
  74. package/src/shade/playground/sparks/main.d.ts.map +1 -0
  75. package/src/shade/playground/sparks/main.js +312 -0
  76. package/src/shade/playground/sparks/sparks_effect.d.ts +72 -0
  77. package/src/shade/playground/sparks/sparks_effect.d.ts.map +1 -0
  78. package/src/shade/playground/sparks/sparks_effect.js +180 -0
  79. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  80. package/src/shade/renderer/Renderer.js +72 -11
  81. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.d.ts +6 -0
  82. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.d.ts.map +1 -1
  83. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.js +10 -0
  84. package/src/shade/renderer/acoustics/GPUAcousticLattice.d.ts +1 -1
  85. package/src/shade/renderer/acoustics/GPUAcousticProbes.d.ts +1 -1
  86. package/src/shade/renderer/acoustics/GPUAcoustics.d.ts +1 -1
  87. package/src/shade/renderer/animation/GPUAnimationManager.d.ts +3 -2
  88. package/src/shade/renderer/animation/GPUAnimationManager.d.ts.map +1 -1
  89. package/src/shade/renderer/animation/GPUAnimationManager.js +74 -3
  90. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.d.ts +13 -3
  91. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.d.ts.map +1 -1
  92. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.js +16 -5
  93. package/src/shade/renderer/animation/skinning/shader_meshlet_apply_skinning.js +8 -8
  94. package/src/shade/renderer/deferred/chunk_read_gBuffer_material.d.ts.map +1 -1
  95. package/src/shade/renderer/deferred/chunk_read_gBuffer_material.js +3 -1
  96. package/src/shade/renderer/deferred/chunk_shade_direct_lights.d.ts +23 -0
  97. package/src/shade/renderer/deferred/chunk_shade_direct_lights.d.ts.map +1 -0
  98. package/src/shade/renderer/deferred/chunk_shade_direct_lights.js +111 -0
  99. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts +19 -6
  100. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts.map +1 -1
  101. package/src/shade/renderer/deferred/chunk_shade_directional_lights.js +30 -21
  102. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.d.ts +12 -22
  103. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.d.ts.map +1 -1
  104. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.js +20 -100
  105. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct_restir.d.ts.map +1 -1
  106. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct_restir.js +6 -4
  107. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.d.ts +15 -0
  108. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.d.ts.map +1 -0
  109. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.js +47 -0
  110. package/src/shade/renderer/extension/ShadowAtlas.d.ts +75 -0
  111. package/src/shade/renderer/extension/ShadowAtlas.d.ts.map +1 -0
  112. package/src/shade/renderer/extension/ShadowAtlas.js +133 -0
  113. package/src/shade/renderer/extension/ViewLighting.d.ts +101 -0
  114. package/src/shade/renderer/extension/ViewLighting.d.ts.map +1 -0
  115. package/src/shade/renderer/extension/ViewLighting.js +168 -0
  116. package/src/shade/renderer/fluid/GPUFluidCascade.d.ts +3 -3
  117. package/src/shade/renderer/fluid/GPUFluidCascadeStack.d.ts +1 -1
  118. package/src/shade/renderer/fluid/forcing/GPUFluidForcing.d.ts +7 -7
  119. package/src/shade/renderer/fluid/pressure/GPUFluidPressureSolve.d.ts +1 -1
  120. package/src/shade/renderer/fluid/query/GPUFluidQueries.d.ts +1 -1
  121. package/src/shade/renderer/global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.d.ts.map +1 -1
  122. package/src/shade/renderer/global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.js +3 -1
  123. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts +37 -7
  124. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts.map +1 -1
  125. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.js +132 -8
  126. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts +3 -1
  127. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts.map +1 -1
  128. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.js +6 -2
  129. package/src/shade/renderer/global_illumination/probe_clipmap/probe/shader_probe_clipmap_indirect_lighting.d.ts.map +1 -1
  130. package/src/shade/renderer/global_illumination/probe_clipmap/probe/shader_probe_clipmap_indirect_lighting.js +3 -1
  131. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts +15 -7
  132. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts.map +1 -1
  133. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.js +40 -9
  134. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.d.ts +23 -0
  135. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.d.ts.map +1 -1
  136. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.js +26 -0
  137. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeDebugView.d.ts +1 -0
  138. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeDebugView.js +6 -0
  139. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts +13 -1
  140. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts.map +1 -1
  141. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.js +23 -1
  142. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe.d.ts.map +1 -1
  143. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe.js +2 -0
  144. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.d.ts +29 -0
  145. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.d.ts.map +1 -0
  146. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.js +102 -0
  147. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.d.ts +8 -1
  148. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.d.ts.map +1 -1
  149. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.js +27 -1
  150. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.d.ts +4 -1
  151. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.d.ts.map +1 -1
  152. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.js +4 -1
  153. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.d.ts +11 -5
  154. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.d.ts.map +1 -1
  155. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.js +28 -10
  156. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.d.ts +36 -2
  157. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.d.ts.map +1 -1
  158. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.js +149 -4
  159. package/src/shade/renderer/global_illumination/shader_resolve_indirect_lighting.d.ts.map +1 -1
  160. package/src/shade/renderer/global_illumination/shader_resolve_indirect_lighting.js +3 -1
  161. package/src/shade/renderer/global_illumination/sharc/sharc_constants.d.ts +0 -18
  162. package/src/shade/renderer/global_illumination/sharc/sharc_constants.d.ts.map +1 -1
  163. package/src/shade/renderer/global_illumination/sharc/sharc_constants.js +1 -21
  164. package/src/shade/renderer/hair/GPUHairSystem.d.ts +278 -0
  165. package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -0
  166. package/src/shade/renderer/hair/GPUHairSystem.js +2263 -0
  167. package/src/shade/renderer/hair/HAIR.d.ts +121 -0
  168. package/src/shade/renderer/hair/HAIR.d.ts.map +1 -0
  169. package/src/shade/renderer/hair/HAIR.js +136 -0
  170. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts +17 -0
  171. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts.map +1 -0
  172. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.js +104 -0
  173. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts +15 -0
  174. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts.map +1 -0
  175. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.js +70 -0
  176. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts +139 -0
  177. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts.map +1 -0
  178. package/src/shade/renderer/hair/HAIR_SIMULATION.js +160 -0
  179. package/src/shade/renderer/hair/HairFrame.d.ts +41 -0
  180. package/src/shade/renderer/hair/HairFrame.d.ts.map +1 -0
  181. package/src/shade/renderer/hair/HairFrame.js +76 -0
  182. package/src/shade/renderer/hair/chunk_hair_bsdf.d.ts +42 -0
  183. package/src/shade/renderer/hair/chunk_hair_bsdf.d.ts.map +1 -0
  184. package/src/shade/renderer/hair/chunk_hair_bsdf.js +170 -0
  185. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts +37 -0
  186. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts.map +1 -0
  187. package/src/shade/renderer/hair/chunk_hair_dual_scattering.js +158 -0
  188. package/src/shade/renderer/hair/chunk_hair_guide.d.ts +59 -0
  189. package/src/shade/renderer/hair/chunk_hair_guide.d.ts.map +1 -0
  190. package/src/shade/renderer/hair/chunk_hair_guide.js +179 -0
  191. package/src/shade/renderer/hair/chunk_hair_pixel.d.ts +32 -0
  192. package/src/shade/renderer/hair/chunk_hair_pixel.d.ts.map +1 -0
  193. package/src/shade/renderer/hair/chunk_hair_pixel.js +116 -0
  194. package/src/shade/renderer/hair/chunk_hair_strand.d.ts +40 -0
  195. package/src/shade/renderer/hair/chunk_hair_strand.d.ts.map +1 -0
  196. package/src/shade/renderer/hair/chunk_hair_strand.js +85 -0
  197. package/src/shade/renderer/hair/chunk_hair_volume.d.ts +32 -0
  198. package/src/shade/renderer/hair/chunk_hair_volume.d.ts.map +1 -0
  199. package/src/shade/renderer/hair/chunk_hair_volume.js +115 -0
  200. package/src/shade/renderer/hair/hair_id_draw.d.ts +24 -0
  201. package/src/shade/renderer/hair/hair_id_draw.d.ts.map +1 -0
  202. package/src/shade/renderer/hair/hair_id_draw.js +135 -0
  203. package/src/shade/renderer/hair/hair_lighting.d.ts +53 -0
  204. package/src/shade/renderer/hair/hair_lighting.d.ts.map +1 -0
  205. package/src/shade/renderer/hair/hair_lighting.js +279 -0
  206. package/src/shade/renderer/hair/hair_scattering_lut.d.ts +75 -0
  207. package/src/shade/renderer/hair/hair_scattering_lut.d.ts.map +1 -0
  208. package/src/shade/renderer/hair/hair_scattering_lut.js +271 -0
  209. package/src/shade/renderer/hair/hair_shadow_draw.d.ts +32 -0
  210. package/src/shade/renderer/hair/hair_shadow_draw.d.ts.map +1 -0
  211. package/src/shade/renderer/hair/hair_shadow_draw.js +123 -0
  212. package/src/shade/renderer/hair/hair_stamp.d.ts +33 -0
  213. package/src/shade/renderer/hair/hair_stamp.d.ts.map +1 -0
  214. package/src/shade/renderer/hair/hair_stamp.js +175 -0
  215. package/src/shade/renderer/hair/shader_hair_classes.d.ts +22 -0
  216. package/src/shade/renderer/hair/shader_hair_classes.d.ts.map +1 -0
  217. package/src/shade/renderer/hair/shader_hair_classes.js +90 -0
  218. package/src/shade/renderer/hair/shader_hair_cull.d.ts +37 -0
  219. package/src/shade/renderer/hair/shader_hair_cull.d.ts.map +1 -0
  220. package/src/shade/renderer/hair/shader_hair_cull.js +251 -0
  221. package/src/shade/renderer/hair/shader_hair_lod.d.ts +39 -0
  222. package/src/shade/renderer/hair/shader_hair_lod.d.ts.map +1 -0
  223. package/src/shade/renderer/hair/shader_hair_lod.js +362 -0
  224. package/src/shade/renderer/hair/shader_hair_roots.d.ts +36 -0
  225. package/src/shade/renderer/hair/shader_hair_roots.d.ts.map +1 -0
  226. package/src/shade/renderer/hair/shader_hair_roots.js +261 -0
  227. package/src/shade/renderer/hair/shader_hair_shadow_list.d.ts +44 -0
  228. package/src/shade/renderer/hair/shader_hair_shadow_list.d.ts.map +1 -0
  229. package/src/shade/renderer/hair/shader_hair_shadow_list.js +240 -0
  230. package/src/shade/renderer/hair/shader_hair_simulate.d.ts +55 -0
  231. package/src/shade/renderer/hair/shader_hair_simulate.d.ts.map +1 -0
  232. package/src/shade/renderer/hair/shader_hair_simulate.js +563 -0
  233. package/src/shade/renderer/hair/shader_hair_volume.d.ts +28 -0
  234. package/src/shade/renderer/hair/shader_hair_volume.d.ts.map +1 -0
  235. package/src/shade/renderer/hair/shader_hair_volume.js +154 -0
  236. package/src/shade/renderer/loader/gltf/tiny-gltf.d.ts +1 -1
  237. package/src/shade/renderer/loader/usd/usd_build_scene.d.ts +31 -0
  238. package/src/shade/renderer/loader/usd/usd_build_scene.d.ts.map +1 -1
  239. package/src/shade/renderer/loader/usd/usd_build_scene.js +36 -21
  240. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts +65 -0
  241. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts.map +1 -0
  242. package/src/shade/renderer/loader/usd/usd_groom_extension.js +470 -0
  243. package/src/shade/renderer/particles/GPUParticleSystem.d.ts +8 -0
  244. package/src/shade/renderer/particles/GPUParticleSystem.d.ts.map +1 -1
  245. package/src/shade/renderer/particles/GPUParticleSystem.js +28 -0
  246. package/src/shade/renderer/particles/graph/ParticleNodeRegistry.d.ts.map +1 -1
  247. package/src/shade/renderer/particles/graph/ParticleNodeRegistry.js +22 -0
  248. package/src/shade/renderer/particles/graph/groups/particle_builtin_groups.d.ts.map +1 -1
  249. package/src/shade/renderer/particles/graph/groups/particle_builtin_groups.js +4 -0
  250. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.d.ts +32 -0
  251. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.d.ts.map +1 -0
  252. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.js +80 -0
  253. package/src/shade/renderer/particles/graph/particle_node_parameters.d.ts.map +1 -1
  254. package/src/shade/renderer/particles/graph/particle_node_parameters.js +1 -0
  255. package/src/shade/renderer/particles/graph/particle_node_presentation.d.ts.map +1 -1
  256. package/src/shade/renderer/particles/graph/particle_node_presentation.js +12 -0
  257. package/src/shade/renderer/particles/graph_particles.d.ts +5 -1
  258. package/src/shade/renderer/particles/graph_particles.d.ts.map +1 -1
  259. package/src/shade/renderer/particles/graph_particles.js +18 -3
  260. package/src/shade/renderer/particles/isa/InstructionStream.d.ts +25 -0
  261. package/src/shade/renderer/particles/isa/InstructionStream.d.ts.map +1 -1
  262. package/src/shade/renderer/particles/isa/InstructionStream.js +25 -0
  263. package/src/shade/renderer/particles/isa/ParticleVMISA.d.ts +9 -0
  264. package/src/shade/renderer/particles/isa/ParticleVMISA.d.ts.map +1 -1
  265. package/src/shade/renderer/particles/isa/ParticleVMISA.js +20 -0
  266. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.d.ts +9 -0
  267. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.d.ts.map +1 -0
  268. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.js +29 -0
  269. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.d.ts +2 -1
  270. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.d.ts.map +1 -1
  271. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.js +27 -5
  272. package/src/shade/renderer/particles/optimizer/particle_program_fuzz.js +1 -1
  273. package/src/shade/renderer/particles/optimizer/particle_vm_semantics.d.ts.map +1 -1
  274. package/src/shade/renderer/particles/optimizer/particle_vm_semantics.js +5 -0
  275. package/src/shade/renderer/particles/runtime/EmitterRegistry.d.ts +7 -0
  276. package/src/shade/renderer/particles/runtime/EmitterRegistry.d.ts.map +1 -1
  277. package/src/shade/renderer/particles/runtime/EmitterRegistry.js +10 -0
  278. package/src/shade/renderer/particles/runtime/ParticleEmitter.d.ts +5 -0
  279. package/src/shade/renderer/particles/runtime/ParticleEmitter.d.ts.map +1 -1
  280. package/src/shade/renderer/particles/runtime/ParticleEmitter.js +5 -0
  281. package/src/shade/renderer/particles/runtime/ProgramHeap.d.ts +8 -0
  282. package/src/shade/renderer/particles/runtime/ProgramHeap.d.ts.map +1 -1
  283. package/src/shade/renderer/particles/runtime/ProgramHeap.js +25 -0
  284. package/src/shade/renderer/particles/shaders/shader_particle_emit.d.ts +1 -1
  285. package/src/shade/renderer/particles/shaders/shader_particle_emit.d.ts.map +1 -1
  286. package/src/shade/renderer/particles/shaders/shader_particle_emit.js +9 -3
  287. package/src/shade/renderer/particles/shaders/shader_particle_emit_wide.d.ts.map +1 -1
  288. package/src/shade/renderer/particles/shaders/shader_particle_emit_wide.js +4 -2
  289. package/src/shade/renderer/particles/shaders/shader_particle_simulate.d.ts +2 -2
  290. package/src/shade/renderer/particles/shaders/shader_particle_simulate.d.ts.map +1 -1
  291. package/src/shade/renderer/particles/shaders/shader_particle_simulate.js +10 -4
  292. package/src/shade/renderer/particles/shaders/shader_particle_simulate_wide.d.ts.map +1 -1
  293. package/src/shade/renderer/particles/shaders/shader_particle_simulate_wide.js +4 -2
  294. package/src/shade/renderer/particles/vm/ParticleVMReference.d.ts +14 -1
  295. package/src/shade/renderer/particles/vm/ParticleVMReference.d.ts.map +1 -1
  296. package/src/shade/renderer/particles/vm/ParticleVMReference.js +34 -1
  297. package/src/shade/renderer/particles/vm/chunk_particle_vm.d.ts.map +1 -1
  298. package/src/shade/renderer/particles/vm/chunk_particle_vm.js +10 -0
  299. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.d.ts +39 -0
  300. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.d.ts.map +1 -0
  301. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.js +90 -0
  302. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts +4 -1
  303. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts.map +1 -1
  304. package/src/shade/renderer/particles/warmup/graph_particle_warmup.js +14 -2
  305. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.d.ts +1 -1
  306. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.d.ts.map +1 -1
  307. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.js +9 -3
  308. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance_wide.d.ts.map +1 -1
  309. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance_wide.js +4 -2
  310. package/src/shade/renderer/path_tracer/shader/chunk_sample_material_data.d.ts.map +1 -1
  311. package/src/shade/renderer/path_tracer/shader/chunk_sample_material_data.js +4 -1
  312. package/src/shade/renderer/path_tracer/shader/chunk_shading_closure_from_material_data.d.ts.map +1 -1
  313. package/src/shade/renderer/path_tracer/shader/chunk_shading_closure_from_material_data.js +3 -1
  314. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  315. package/src/shade/renderer/postprocess/ssr/SSR.d.ts +4 -1
  316. package/src/shade/renderer/postprocess/ssr/SSR.d.ts.map +1 -1
  317. package/src/shade/renderer/postprocess/ssr/SSR.js +9 -0
  318. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.d.ts +16 -1
  319. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.d.ts.map +1 -1
  320. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.js +47 -3
  321. package/src/shade/renderer/postprocess/ssr/resolve/ssr_resolve_shader.d.ts.map +1 -1
  322. package/src/shade/renderer/postprocess/ssr/resolve/ssr_resolve_shader.js +5 -3
  323. package/src/shade/renderer/postprocess/ssr/ssr_combine_shader.d.ts.map +1 -1
  324. package/src/shade/renderer/postprocess/ssr/ssr_combine_shader.js +6 -5
  325. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.d.ts +7 -4
  326. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.d.ts.map +1 -1
  327. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.js +26 -16
  328. package/src/shade/renderer/rasterize/forward/chunk_forward_shade_standard_fragment.d.ts.map +1 -1
  329. package/src/shade/renderer/rasterize/forward/chunk_forward_shade_standard_fragment.js +5 -2
  330. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.d.ts.map +1 -1
  331. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.js +5 -0
  332. package/src/shade/renderer/rasterize/native/avboit/shader_avboit_splat.d.ts.map +1 -1
  333. package/src/shade/renderer/rasterize/native/avboit/shader_avboit_splat.js +4 -1
  334. package/src/shade/renderer/rasterize/native/oit/oit_resolve_fragment_body.d.ts.map +1 -1
  335. package/src/shade/renderer/rasterize/native/oit/oit_resolve_fragment_body.js +5 -1
  336. package/src/shade/renderer/rasterize/native/oit/shader_oit_generate_moments.d.ts.map +1 -1
  337. package/src/shade/renderer/rasterize/native/oit/shader_oit_generate_moments.js +7 -4
  338. package/src/shade/renderer/scene/GPUSceneContext.d.ts +7 -0
  339. package/src/shade/renderer/scene/GPUSceneContext.d.ts.map +1 -1
  340. package/src/shade/renderer/scene/GPUSceneContext.js +18 -0
  341. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.d.ts +13 -8
  342. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.d.ts.map +1 -1
  343. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.js +18 -14
  344. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.d.ts +3 -2
  345. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.d.ts.map +1 -1
  346. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.js +12 -4
  347. package/src/shade/renderer/shader/chunk/material/MATERIAL_SHADING_INFO_STRUCT.d.ts.map +1 -1
  348. package/src/shade/renderer/shader/chunk/material/MATERIAL_SHADING_INFO_STRUCT.js +1 -0
  349. package/src/shade/renderer/shader/chunk/material/chunk_compute_indirect_specular.js +1 -1
  350. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.d.ts +16 -0
  351. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.d.ts.map +1 -0
  352. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.js +25 -0
  353. package/src/shade/renderer/shader/chunk/material/chunk_re_direct_multiscatter.js +2 -2
  354. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.d.ts +20 -0
  355. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.d.ts.map +1 -0
  356. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.js +134 -0
  357. package/src/shade/renderer/shader/compiler/ShaderSwitch.d.ts +1 -1
  358. package/src/shade/renderer/shader/compiler/ShaderSwitchSet.d.ts +1 -1
  359. package/src/shade/renderer/shader/resource/ShaderResourceDescriptor.d.ts +1 -1
  360. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts +15 -0
  361. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts.map +1 -1
  362. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.js +69 -0
  363. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.d.ts +10 -0
  364. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.d.ts.map +1 -0
  365. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.js +22 -0
  366. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.d.ts +2 -2
  367. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.js +2 -2
  368. package/src/shade/renderer/water/query/GPUWaterQueries.d.ts +1 -1
  369. package/src/shade/renderer/wind/GPUSceneWind.d.ts +1 -1
@@ -0,0 +1,2263 @@
1
+ import { assert } from "../../../core/assert.js";
2
+ import { ALLOCATOR_NO_SPACE } from "../../../core/binary/allocator/OffsetAllocator.js";
3
+ import { groom_bind_to_mesh, groom_mesh_binder } from "../../../engine/graphics/hair/groom_bind_to_mesh.js";
4
+ import { GROOM_POINT_WORDS } from "../../../engine/graphics/hair/Groom.js";
5
+ import { TextureViewDescriptor } from "../../descriptor/texture/TextureViewDescriptor.js";
6
+ import { BufferResourceDescriptor } from "../../device/graph/BufferResourceDescriptor.js";
7
+ import { TextureResourceDescriptor } from "../../device/graph/TextureResourceDescriptor.js";
8
+ import { ShadeGPUCommandContext } from "../../device/ShadeGPUCommandContext.js";
9
+ import { graph_read_buffer } from "../buffer/graph_read_buffer.js";
10
+ import { GPURunPool } from "../buffer/GPURunPool.js";
11
+ import { GPUDatabase } from "../buffer/table/GPUDatabase.js";
12
+ import { DEFAULT_DEPTH_TEXTURE_VIEW } from "../depth/DEFAULT_DEPTH_TEXTURE_VIEW.js";
13
+ import { GBufferTextures } from "../extension/GBufferTextures.js";
14
+ import { ShadowAtlas } from "../extension/ShadowAtlas.js";
15
+ import { SceneColor } from "../extension/SceneColor.js";
16
+ import { ViewLighting } from "../extension/ViewLighting.js";
17
+ import { ViewTextures } from "../extension/ViewTextures.js";
18
+ import { INDIRECT_COMPUTE_COMMAND_DESCRIPTOR } from "../gpu_primitive/indirect/IndirectComputeCommandBuildPass.js";
19
+ import {
20
+ enquire_prefix_scan_csdldf_pipelines,
21
+ graph_prefix_scan_csdldf,
22
+ prefix_scan_csdldf_output_descriptor
23
+ } from "../gpu_primitive/prefix_sum/v1/graph_prefix_scan_csdldf.js";
24
+ import { ENVIRONMENT_MAP_SAMPLER_DESCRIPTOR } from "../light/environment/ENVIRONMENT_MAP_SAMPLER_RESOURCE.js";
25
+ import { view_velocity_matrices } from "../postprocess/velocity/view_velocity_matrices.js";
26
+ import { shader_prefix_sum_to_command } from "../rasterize/expand/mesh/shader_prefix_sum_to_command.js";
27
+ import { graph_compute_indirect_pass } from "../shader/graph/compute/graph_compute_indirect_pass.js";
28
+ import { graph_compute_pass } from "../shader/graph/compute/graph_compute_pass.js";
29
+ import { graph_create_buffer } from "../shader/graph/graph_create_buffer.js";
30
+ import { graph_import_buffer } from "../shader/graph/graph_import_buffer.js";
31
+ import { COMPUTE_SHADER_DEFAULT_GROUP_SIZE } from "../shader/COMPUTE_SHADER_DEFAULT_GROUP_SIZE.js";
32
+ import { ShaderSwitchSet } from "../shader/compiler/ShaderSwitchSet.js";
33
+ import { WIND_FIELD_UNIFORM } from "../wind/WIND_FIELD_UNIFORM.js";
34
+ import { SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR } from "../shadow/map/SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR.js";
35
+ import { STATIC_GRAPHICS_ENGINE_ASSETS } from "../STATIC_GRAPHICS_ENGINE_ASSETS.js";
36
+ import { SamplerDescriptor } from "../../descriptor/texture/SamplerDescriptor.js";
37
+ import {
38
+ HAIR_CLASS_COUNT,
39
+ HAIR_COUNTER_POINTS,
40
+ HAIR_COUNTER_SHADOW_STRANDS,
41
+ HAIR_COUNTER_VOXELS,
42
+ HAIR_COUNTER_WORDS,
43
+ HAIR_DRAW_ARGS_WORDS,
44
+ HAIR_ID_FORMAT,
45
+ HAIR_ID_NONE,
46
+ HAIR_LIST_SHADOW_WORDS,
47
+ HAIR_LOD_WORDS,
48
+ HAIR_POINT_CAPACITY_LIMIT,
49
+ HAIR_ROW_BITS,
50
+ HAIR_VOLUME_MIPS
51
+ } from "./HAIR.js";
52
+ import { HAIR_DATABASE_INITIAL_SIZE, HAIR_DATABASE_SPEC, HAIR_INSTANCE_TABLE } from "./HAIR_DATABASE_SPEC.js";
53
+ import { HAIR_SHADOW_VIEW_STRUCT, HAIR_VIEW_STRUCT } from "./HAIR_FRAME_STRUCTS.js";
54
+ import { shader_hair_roots } from "./shader_hair_roots.js";
55
+ import {
56
+ HAIR_BINDING_NONE,
57
+ HAIR_COLLIDER_LIMIT,
58
+ HAIR_COLLIDER_WORDS,
59
+ HAIR_FRAME_STEP_MAX,
60
+ HAIR_ROOT_FRAME_VECTORS,
61
+ HAIR_GUIDE_BOUNDS_WORDS,
62
+ HAIR_GUIDE_RECORD_WORDS,
63
+ HAIR_STATE_HEADER,
64
+ HAIR_STATE_NONE,
65
+ HAIR_STATE_PLANES,
66
+ HAIR_SUBSTEP_MAX
67
+ } from "./HAIR_SIMULATION.js";
68
+ import { HAIR_EPOCH_MASK } from "./chunk_hair_guide.js";
69
+ import {
70
+ shader_hair_collider_poses,
71
+ shader_hair_simulate,
72
+ shader_hair_simulate_begin
73
+ } from "./shader_hair_simulate.js";
74
+ import { HairFrame } from "./HairFrame.js";
75
+ import { HAIR_ID_PIPELINE, HAIR_ID_RESOURCES } from "./hair_id_draw.js";
76
+ import { HAIR_SHADOW_PIPELINE, HAIR_SHADOW_RESOURCES, HAIR_SHADOW_TARGET_STRUCT } from "./hair_shadow_draw.js";
77
+ import {
78
+ shader_hair_shadow_draw_args,
79
+ shader_hair_shadow_list,
80
+ shader_hair_shadow_list_command
81
+ } from "./shader_hair_shadow_list.js";
82
+ import { HAIR_LIGHTING_RESOURCES, hair_lighting_pipeline } from "./hair_lighting.js";
83
+ import { HAIR_MOTION_PIPELINE, HAIR_STAMP_PIPELINE, HAIR_STAMP_RESOURCES } from "./hair_stamp.js";
84
+ import { shader_hair_draw_args, shader_hair_regions } from "./shader_hair_classes.js";
85
+ import { shader_hair_cull } from "./shader_hair_cull.js";
86
+ import { shader_hair_volume_clear, shader_hair_volume_command, shader_hair_volume_mips } from "./shader_hair_volume.js";
87
+ import { HAIR_LOD_WORKGROUP_SIZE, shader_hair_lod } from "./shader_hair_lod.js";
88
+
89
+ /**
90
+ * @typedef {import("../../../engine/graphics/hair/Groom.js").Groom} Groom
91
+ * @typedef {import("../extension/FrameContext.js").FrameContext} FrameContext
92
+ * @typedef {import("../GraphicsContext.js").GraphicsContext} GraphicsContext
93
+ */
94
+
95
+ /**
96
+ * Words the groom pool starts with room for.
97
+ * @type {number}
98
+ */
99
+ const INITIAL_POOL_WORDS = 2 ** 20;
100
+
101
+ /**
102
+ * Points a frame's deformed buffer is first made with room for.
103
+ * @type {number}
104
+ */
105
+ const INITIAL_POINT_CAPACITY = 2 ** 18;
106
+
107
+ /**
108
+ * Strands a frame's draw list is first made with room for.
109
+ * @type {number}
110
+ */
111
+ const INITIAL_LIST_CAPACITY = 2 ** 16;
112
+
113
+ /**
114
+ * Voxels a frame's density volumes are first made with room for, every level of every volume.
115
+ * @type {number}
116
+ */
117
+ const INITIAL_VOXEL_CAPACITY = 2 ** 20;
118
+
119
+ /**
120
+ * Vectors the guide state pool starts with room for.
121
+ * @type {number}
122
+ */
123
+ const INITIAL_STATE_CAPACITY = 2 ** 14;
124
+
125
+ /**
126
+ * The largest workgroup count of a dispatch's dimension that WebGPU guarantees, as the folded index unfolds it.
127
+ * @type {number}
128
+ */
129
+ const GROUPS_PER_DIMENSION = 65535;
130
+
131
+ /**
132
+ * The narrowest a strand is drawn, pixels. One pixel is the narrowest strip that rasterizes without gaps at every
133
+ * jitter offset; below it, whether a strand is seen at a pixel is the jitter's to decide, and that flickers.
134
+ * @type {number}
135
+ */
136
+ export const HAIR_PIXEL_FLOOR = 1;
137
+
138
+ /**
139
+ * The most a dropped point may stray from the chord its kept neighbours make, pixels at the instance's nearest point.
140
+ * @type {number}
141
+ */
142
+ export const HAIR_DECIMATION_TOLERANCE = 0.5;
143
+
144
+ const MIP_0 = TextureViewDescriptor.from({ mipLevelCount: 1, baseMipLevel: 0 });
145
+
146
+ /**
147
+ * Bytes of a `u32` collection with room for `count` elements: the count, padded to 16 bytes, then whole vec4s.
148
+ *
149
+ * @param {number} count
150
+ * @returns {number}
151
+ */
152
+ function collection_size(count) {
153
+ return 16 + Math.max(1, Math.ceil(count / 4)) * 16;
154
+ }
155
+
156
+ /**
157
+ * How an entity's hair looks: the colour of its fibres and how rough they are.
158
+ */
159
+ export class HairMaterial {
160
+ /** @type {number} 0 for none, 1 for black */
161
+ melanin = 0.5;
162
+
163
+ /** @type {number} the share of the melanin that is pheomelanin */
164
+ redness = 0;
165
+
166
+ /** @type {Float32Array} linear; white is no dye */
167
+ dye = new Float32Array([1, 1, 1]);
168
+
169
+ /** @type {number} longitudinal roughness, 0 to 1 */
170
+ roughness = 0.3;
171
+
172
+ /**
173
+ * @param {HairMaterial} other
174
+ */
175
+ copy(other) {
176
+ this.melanin = other.melanin;
177
+ this.redness = other.redness;
178
+ this.dye.set(other.dye);
179
+ this.roughness = other.roughness;
180
+ }
181
+
182
+ /**
183
+ * @param {HairMaterial} other
184
+ * @returns {boolean}
185
+ */
186
+ equals(other) {
187
+ return this.melanin === other.melanin
188
+ && this.redness === other.redness
189
+ && this.dye[0] === other.dye[0]
190
+ && this.dye[1] === other.dye[1]
191
+ && this.dye[2] === other.dye[2]
192
+ && this.roughness === other.roughness;
193
+ }
194
+ }
195
+
196
+ /**
197
+ * How an entity's hair moves: the rates its guides are held to their shape by and lose their motion at, and how it
198
+ * meets colliders. The fields are `HairDynamics`'s.
199
+ */
200
+ export class HairDynamicsSettings {
201
+ /** @type {number} per second */
202
+ damping = 1;
203
+
204
+ /** @type {number} per second */
205
+ global_shape_rate = 10;
206
+
207
+ /** @type {number} share of a guide's length */
208
+ global_shape_range = 0.5;
209
+
210
+ /** @type {number} per second */
211
+ local_shape_rate = 20;
212
+
213
+ /** @type {number} */
214
+ friction = 0.3;
215
+
216
+ /** @type {number} metres */
217
+ thickness = 0;
218
+
219
+ /**
220
+ * @param {HairDynamicsSettings} other
221
+ */
222
+ copy(other) {
223
+ this.damping = other.damping;
224
+ this.global_shape_rate = other.global_shape_rate;
225
+ this.global_shape_range = other.global_shape_range;
226
+ this.local_shape_rate = other.local_shape_rate;
227
+ this.friction = other.friction;
228
+ this.thickness = other.thickness;
229
+ }
230
+
231
+ /**
232
+ * @param {HairDynamicsSettings} other
233
+ * @returns {boolean}
234
+ */
235
+ equals(other) {
236
+ return this.damping === other.damping
237
+ && this.global_shape_rate === other.global_shape_rate
238
+ && this.global_shape_range === other.global_shape_range
239
+ && this.local_shape_rate === other.local_shape_rate
240
+ && this.friction === other.friction
241
+ && this.thickness === other.thickness;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * A groom on the device: its run in the pool, where each group's points, strands and guides start in it, and how
247
+ * many instances wear it.
248
+ */
249
+ class GroomEntry {
250
+ /** @type {import("../../../core/binary/allocator/OffsetAllocator.js").Allocation} */
251
+ run = null;
252
+
253
+ /** @type {Uint32Array} a group's first point word, its first strand word and its first guide word, three a group */
254
+ offsets = new Uint32Array(0);
255
+
256
+ /** @type {number} */
257
+ references = 0;
258
+ }
259
+
260
+ /**
261
+ * One entity's hair in the runtime: a row a group of its groom.
262
+ */
263
+ export class GPUHairContext {
264
+ /** @type {Groom|null} */
265
+ groom = null;
266
+
267
+ /** @type {number[]} a row of the instance table a group, in the groom's order */
268
+ rows = [];
269
+
270
+ /** @type {number} the `transforms` row of the entity */
271
+ node = 0;
272
+
273
+ /** @type {HairMaterial} what was last staged */
274
+ material = new HairMaterial();
275
+
276
+ /** @type {HairDynamicsSettings|null} what was last staged; null for hair that rides its entity */
277
+ dynamics = null;
278
+
279
+ /** @type {import("../../../core/binary/allocator/OffsetAllocator.js").Allocation[]} a guide state a group */
280
+ states = [];
281
+
282
+ /** @type {number[]} each group's state's epoch */
283
+ epochs = [];
284
+
285
+ /** @type {Uint32Array} the colliders it meets this frame, `HAIR_COLLIDER_WORDS` each, the pose left to the GPU */
286
+ colliders = new Uint32Array(HAIR_COLLIDER_LIMIT * HAIR_COLLIDER_WORDS);
287
+
288
+ /** @type {number} */
289
+ collider_count = 0;
290
+
291
+ /** @type {number} the `transforms` row of the entity whose mesh holds the roots, or `HAIR_BINDING_NONE` */
292
+ mesh_node = HAIR_BINDING_NONE;
293
+
294
+ /** @type {import("../geometry/MeshletGeometry.js").MeshletGeometry|null} the bound mesh's geometry */
295
+ mesh_geometry = null;
296
+
297
+ /** @type {number} the bound mesh's first meshlet row now */
298
+ meshlet_base = 0;
299
+
300
+ /** @type {Array<{run: Object, references: number}|null>} each group's bindings on the mesh */
301
+ bindings = [];
302
+
303
+ /** @type {Array<import("../../../core/binary/allocator/OffsetAllocator.js").Allocation|null>} root frames a group */
304
+ roots = [];
305
+ }
306
+
307
+ /**
308
+ * The strand hair of one scene context: the instance table, the groom pool, and the passes that draw a view's hair
309
+ * into its G-buffer and light it.
310
+ *
311
+ * **The CPU registers and stages; the GPU decides what is drawn.** {@link add} uploads the groom once, however many
312
+ * entities wear it, and claims a row a group; {@link set_node} and {@link set_material} restage; {@link remove} lets
313
+ * go. Nothing per strand happens on the CPU.
314
+ *
315
+ * Each frame, at the phase where the G-buffer is written into, {@link graph_draw}: the level of detail of every
316
+ * instance, the cull and deformation of the strands it keeps with their density volume (cleared, laid, its coarser
317
+ * levels built), their id raster under the scene's depth, and the
318
+ * G-buffer stamp and velocity under it (`HAIR.js` says what the frame's buffers hold). Where the strands cast
319
+ * shadows ({@link cast_shadows}), also the strands of instances off the view that the shadow views drawn this frame
320
+ * reach, and for each shadow view, the list of the strands it draws and their depth drawn into it. Then, once the
321
+ * frame is lit, {@link graph_light}: the strands' own light, written over the pixels they cover.
322
+ *
323
+ * The frame's buffers grow from what the GPU asked for, read back: a frame asking for more than they hold draws what
324
+ * fits, and the next frames hold what was asked.
325
+ */
326
+ export class GPUHairSystem {
327
+ /**
328
+ * Offer the pipelines the hair's passes run — see `PipelineEnquiry`.
329
+ *
330
+ * @param {import("../../device/pipeline/PipelineEnquiry.js").PipelineEnquiry} enquiry
331
+ */
332
+ static enquirePipelines(enquiry) {
333
+ enquiry.compute.push(shader_hair_lod.pipeline);
334
+ enquiry.compute.push(shader_hair_regions.pipeline);
335
+ enquiry.compute.push(shader_hair_cull.pipeline);
336
+ enquiry.compute.push(shader_hair_draw_args.pipeline);
337
+ enquiry.compute.push(shader_hair_volume_command.pipeline);
338
+ enquiry.compute.push(shader_hair_volume_clear.pipeline);
339
+ enquiry.compute.push(shader_hair_volume_mips.pipeline);
340
+ enquiry.compute.push(shader_hair_shadow_list_command.pipeline);
341
+ enquiry.compute.push(shader_hair_shadow_list.pipeline);
342
+ enquiry.compute.push(shader_hair_shadow_draw_args.pipeline);
343
+ enquiry.compute.push(shader_hair_simulate_begin.pipeline);
344
+ enquiry.compute.push(shader_hair_roots.pipeline);
345
+ enquiry.compute.push(shader_hair_collider_poses.pipeline);
346
+ enquiry.compute.push(shader_hair_simulate.variant(ShaderSwitchSet.EMPTY.with(WIND_FIELD_UNIFORM, false)).pipeline);
347
+ enquiry.compute.push(shader_hair_simulate.variant(ShaderSwitchSet.EMPTY.with(WIND_FIELD_UNIFORM, true)).pipeline);
348
+ enquiry.compute.push(shader_prefix_sum_to_command.pipeline);
349
+ enquire_prefix_scan_csdldf_pipelines(enquiry);
350
+ enquiry.render.push(HAIR_ID_PIPELINE);
351
+ enquiry.render.push(HAIR_SHADOW_PIPELINE);
352
+ enquiry.render.push(HAIR_STAMP_PIPELINE);
353
+ enquiry.render.push(HAIR_MOTION_PIPELINE);
354
+ }
355
+
356
+ /**
357
+ * The narrowest a strand is drawn, pixels: the level of detail keeps a group's strands at least this wide and
358
+ * fewer of them in proportion. At 0 every strand is drawn at its own width, which a frame can only sample, not
359
+ * show: what the area-conserving thinning is measured against.
360
+ *
361
+ * @type {number}
362
+ */
363
+ pixel_floor = HAIR_PIXEL_FLOOR;
364
+
365
+ /**
366
+ * Whether the strands draw into the shadow maps drawn this frame (`ShadowAtlas`), so that they shadow everything
367
+ * the maps reach, the skin under them included. Toward a light whose map holds them, the strands are then
368
+ * shadowed by the map alone, their own among the casters, and the density volume no longer takes the strands
369
+ * short of the light off: it would take them off twice. Toward a light whose map cannot hold them, a point
370
+ * light's or a virtual shadow map, the volume still does.
371
+ *
372
+ * Off, the strands are in no shadow map: they shadow nothing else, and only the volume shadows them.
373
+ *
374
+ * @type {boolean}
375
+ */
376
+ cast_shadows = true;
377
+
378
+ /**
379
+ * The world's gravity, metres a second squared.
380
+ * @type {Float32Array}
381
+ */
382
+ gravity = new Float32Array([0, -9.80665, 0]);
383
+
384
+ /** @type {GraphicsContext} */
385
+ #graphics;
386
+
387
+ /** @type {GPUDatabase} */
388
+ #database;
389
+
390
+ /** @type {GPURunPool} every groom's points and strand words, a run a groom, in `u32` words */
391
+ #pool;
392
+
393
+ /** @type {Map<Groom, GroomEntry>} */
394
+ #grooms = new Map();
395
+
396
+ /** @type {Set<GPUHairContext>} */
397
+ #contexts = new Set();
398
+
399
+ /** @type {GPUHairContext[]} {@link #contexts}, walked every frame without an iterator */
400
+ #context_list = [];
401
+
402
+ /** @type {number} rows the instance passes draw: one past the highest in use */
403
+ #row_limit = 0;
404
+
405
+ #point_capacity = INITIAL_POINT_CAPACITY;
406
+ #points_asked = 0;
407
+
408
+ #list_capacity = INITIAL_LIST_CAPACITY;
409
+ #strands_asked = 0;
410
+
411
+ #voxel_capacity = INITIAL_VOXEL_CAPACITY;
412
+ #voxels_asked = 0;
413
+
414
+ /** @type {GPURunPool} every simulated instance's guide state, in `vec4<f32>` */
415
+ #state_pool;
416
+
417
+ /** @type {GPUBuffer|null} `HAIR_GUIDE_BOUNDS_WORDS` a row */
418
+ #bounds_buffer = null;
419
+
420
+ /** @type {GPUBuffer|null} the inclusive sums of the rows' guide counts, as a collection */
421
+ #sums_buffer = null;
422
+
423
+ /** @type {Uint32Array} */
424
+ #sums_words = new Uint32Array(0);
425
+
426
+ /** @type {number} guides every simulated instance has, the last of the sums */
427
+ #guide_total = 0;
428
+
429
+ /** whether a row's guides or state changed since the sums were made */
430
+ #sums_dirty = true;
431
+
432
+ /** @type {GPUBuffer|null} the frame's collider list: a pair a row, then the colliders */
433
+ #collider_buffer = null;
434
+
435
+ /** @type {Uint32Array} */
436
+ #collider_words = new Uint32Array(0);
437
+
438
+ /** @type {number} entities whose hair is bound to a mesh */
439
+ #bound = 0;
440
+
441
+ /**
442
+ * Each group's bindings on each mesh geometry, in the groom pool, shared by every entity that wears the group on
443
+ * that mesh.
444
+ * @type {WeakMap<import("../../../engine/graphics/hair/Groom.js").GroomGroup, Map<Object, {run: Object, references: number}>>}
445
+ */
446
+ #binding_cache = new WeakMap();
447
+
448
+ /**
449
+ * Each mesh geometry's binder (`groom_mesh_binder`): its triangles and their BVH, made once and shared by every group
450
+ * bound to it.
451
+ * @type {WeakMap<Object, import("../../../engine/graphics/hair/groom_bind_to_mesh.js").GroomMeshBinder>}
452
+ */
453
+ #binders = new WeakMap();
454
+
455
+ /** @type {GPUBuffer|null} the inclusive sums of the rows' strand counts, a bound row's, as a collection */
456
+ #root_sums_buffer = null;
457
+
458
+ /** @type {Uint32Array} */
459
+ #root_sums_words = new Uint32Array(0);
460
+
461
+ /** @type {number} strands every bound instance has, the last of the sums */
462
+ #root_total = 0;
463
+
464
+ /** @type {number} entities whose hair is simulated */
465
+ #simulated = 0;
466
+
467
+ /** @type {number} seconds the scene's clock moved that the guides were not stepped through */
468
+ #pending_time = 0;
469
+
470
+ /** @type {number} the scene context's frame the guides were last stepped in, -1 for none */
471
+ #simulated_frame = -1;
472
+
473
+ /** @type {number} the last epoch handed out */
474
+ #epoch = 0;
475
+
476
+ /**
477
+ * The record a row is written from.
478
+ * @type {Object}
479
+ */
480
+ #record = {
481
+ node: 0,
482
+ points: 0,
483
+ strands: 0,
484
+ strand_count: 0,
485
+ point_count: 0,
486
+ level_count: 0,
487
+ melanin: 0,
488
+ redness: 0,
489
+ bounds_min: new Float32Array(3),
490
+ width_max: 0,
491
+ bounds_extent: new Float32Array(3),
492
+ width_mean: 0,
493
+ decimation_low: new Float32Array(4),
494
+ decimation_high: new Float32Array(4),
495
+ dye: new Float32Array(3),
496
+ roughness: 0,
497
+ root_spacing: 0,
498
+ guides: 0,
499
+ guide_count: 0,
500
+ guide_point_count: 0,
501
+ state: HAIR_STATE_NONE,
502
+ epoch: 0,
503
+ guide_reach: 0,
504
+ guide_length_max: 0,
505
+ damping: 0,
506
+ global_shape_rate: 0,
507
+ global_shape_range: 0,
508
+ local_shape_rate: 0,
509
+ friction: 0,
510
+ thickness: 0,
511
+ mesh_node: HAIR_BINDING_NONE,
512
+ meshlet_base: 0,
513
+ bindings: 0,
514
+ roots: 0,
515
+ };
516
+
517
+ #view_words = new ArrayBuffer(HAIR_VIEW_STRUCT.size);
518
+ #view_f32 = new Float32Array(this.#view_words);
519
+ #view_u32 = new Uint32Array(this.#view_words);
520
+
521
+ /**
522
+ * A shadow view's rectangle, `HAIR_SHADOW_TARGET_STRUCT`, written a view at a time.
523
+ * @type {Float32Array}
524
+ */
525
+ #shadow_view_words = new Float32Array(HAIR_SHADOW_TARGET_STRUCT.size / 4);
526
+
527
+ /**
528
+ * The frame's shadow views the strands draw into, `HAIR_SHADOW_VIEW_STRUCT` each; made with the first frame that
529
+ * draws hair, grown to the views asked for.
530
+ * @type {GPUBuffer|null}
531
+ */
532
+ #shadow_views_buffer = null;
533
+
534
+ /** @type {Float32Array} the shadow views' words as they are written, grown with the buffer */
535
+ #shadow_views_words = new Float32Array(0);
536
+
537
+ /** whether this frame's strands draw into the shadow maps, as {@link graph_draw} found */
538
+ #casting = false;
539
+
540
+ #inverse_view_projection = new Float32Array(16);
541
+ #view_projection_previous = new Float32Array(16);
542
+
543
+ /**
544
+ * @param {GraphicsContext} graphics
545
+ */
546
+ constructor(graphics) {
547
+ assert.defined(graphics, 'graphics');
548
+
549
+ const device = graphics.device;
550
+
551
+ this.#graphics = graphics;
552
+ this.#database = new GPUDatabase({
553
+ device,
554
+ definition: HAIR_DATABASE_SPEC,
555
+ label: "Hair",
556
+ initial_data_size_bytes: HAIR_DATABASE_INITIAL_SIZE,
557
+ });
558
+ this.#pool = new GPURunPool(device, "Hair / grooms", 4, INITIAL_POOL_WORDS);
559
+ this.#state_pool = new GPURunPool(device, "Hair / guide state", 16, INITIAL_STATE_CAPACITY);
560
+ }
561
+
562
+ /** @returns {GPUDatabase} */
563
+ get database() {
564
+ return this.#database;
565
+ }
566
+
567
+ /** @returns {import("../buffer/table/GPUTypedTable.js").GPUTypedTable} */
568
+ get table() {
569
+ return this.#database.get(HAIR_INSTANCE_TABLE);
570
+ }
571
+
572
+ /** @returns {GPURunPool} */
573
+ get pool() {
574
+ return this.#pool;
575
+ }
576
+
577
+ /** @returns {number} grooms on the device */
578
+ get groom_count() {
579
+ return this.#grooms.size;
580
+ }
581
+
582
+ /** @returns {number} entities registered */
583
+ get count() {
584
+ return this.#contexts.size;
585
+ }
586
+
587
+ /** @returns {number} points a frame's deformed buffer holds */
588
+ get point_capacity() {
589
+ return this.#point_capacity;
590
+ }
591
+
592
+ /** @returns {number} the most points a frame asked for, as last read off the device */
593
+ get points_asked() {
594
+ return this.#points_asked;
595
+ }
596
+
597
+ /** @returns {number} strands a frame's draw list holds */
598
+ get list_capacity() {
599
+ return this.#list_capacity;
600
+ }
601
+
602
+ /** @returns {number} the most strands a frame's draw list was asked to hold, as last read off the device */
603
+ get strands_asked() {
604
+ return this.#strands_asked;
605
+ }
606
+
607
+ /** @returns {number} voxels a frame's density volumes hold */
608
+ get voxel_capacity() {
609
+ return this.#voxel_capacity;
610
+ }
611
+
612
+ /** @returns {number} the most voxels a frame asked for, as last read off the device */
613
+ get voxels_asked() {
614
+ return this.#voxels_asked;
615
+ }
616
+
617
+ /** @returns {number} rows the instance passes draw */
618
+ get row_limit() {
619
+ return this.#row_limit;
620
+ }
621
+
622
+ /** @returns {GPURunPool} */
623
+ get state_pool() {
624
+ return this.#state_pool;
625
+ }
626
+
627
+ /** @returns {number} guides every simulated instance has */
628
+ get guide_total() {
629
+ return this.#guide_total;
630
+ }
631
+
632
+ /**
633
+ * Begin a frame: the scene's clock moved on by `seconds`, which the guides are stepped through when the frame's
634
+ * hair is next drawn. Call once a frame, before any view's {@link graph_draw}.
635
+ *
636
+ * @param {number} seconds
637
+ */
638
+ step(seconds) {
639
+ assert.greaterThanOrEqual(seconds, 0, 'seconds');
640
+
641
+ this.#pending_time += seconds;
642
+ }
643
+
644
+ /**
645
+ * Simulate an entity's hair, or let it ride its entity again. Simulated, each group of its groom has a guide
646
+ * state, laid at rest in the entity's frame on the next step. A state the pool cannot house leaves the group
647
+ * riding its entity.
648
+ *
649
+ * @param {GPUHairContext} context
650
+ * @param {HairDynamicsSettings|null} dynamics
651
+ */
652
+ set_dynamics(context, dynamics) {
653
+ if (dynamics === null) {
654
+ if (context.dynamics === null) {
655
+ return;
656
+ }
657
+
658
+ this.#release_states(context);
659
+
660
+ context.dynamics = null;
661
+ this.#simulated--;
662
+ } else if (context.dynamics === null) {
663
+ context.dynamics = new HairDynamicsSettings();
664
+ context.dynamics.copy(dynamics);
665
+
666
+ this.#simulated++;
667
+
668
+ const groups = context.groom.groups;
669
+
670
+ for (let g = 0; g < groups.length; g++) {
671
+ const group = groups[g];
672
+ const run = group.guide_count > 0
673
+ ? this.#state_pool.allocate(HAIR_STATE_HEADER + HAIR_STATE_PLANES * group.guide_point_count * group.guide_count)
674
+ : null;
675
+
676
+ context.states.push(run !== null && run.offset !== ALLOCATOR_NO_SPACE ? run : null);
677
+
678
+ // never zero, which a fresh pool holds
679
+ this.#epoch = (this.#epoch % HAIR_EPOCH_MASK) + 1;
680
+ context.epochs.push(this.#epoch);
681
+ }
682
+ } else if (context.dynamics.equals(dynamics)) {
683
+ return;
684
+ } else {
685
+ context.dynamics.copy(dynamics);
686
+ }
687
+
688
+ this.#sums_dirty = true;
689
+ this.#restage(context);
690
+ }
691
+
692
+ /**
693
+ * Grow an entity's strands from a mesh: each root rides the triangle of the mesh it was bound to at the mesh's
694
+ * bind pose (`groom_bind_to_mesh`, made once a group a geometry and shared), as the mesh deforms. The groom's
695
+ * points are in the mesh's own space at its bind pose. Call again when the mesh's meshlets move (a skinned
696
+ * clone's are compacted); with `HAIR_BINDING_NONE`, the strands ride their own entity again. A group the pool
697
+ * cannot house the frames of rides its entity. A binding to another mesh, or none, lays simulated hair at rest
698
+ * where the roots now are (`reset`); the same mesh's meshlets moving does not.
699
+ *
700
+ * @param {GPUHairContext} context
701
+ * @param {number} mesh_node the `transforms` row of the mesh's entity, or `HAIR_BINDING_NONE`
702
+ * @param {import("../geometry/MeshletGeometry.js").MeshletGeometry|null} geometry the mesh's geometry, as loaded
703
+ * @param {number} meshlet_base the mesh's first meshlet row now: a skinned clone's own
704
+ */
705
+ set_binding(context, mesh_node, geometry, meshlet_base) {
706
+ if (mesh_node === HAIR_BINDING_NONE || geometry === null) {
707
+ if (context.mesh_node === HAIR_BINDING_NONE) {
708
+ return;
709
+ }
710
+
711
+ this.#release_binding(context);
712
+ this.reset(context);
713
+ this.#restage(context);
714
+
715
+ return;
716
+ }
717
+
718
+ if (context.mesh_node === mesh_node && context.mesh_geometry === geometry) {
719
+ if (context.meshlet_base !== meshlet_base) {
720
+ context.meshlet_base = meshlet_base;
721
+ this.#restage(context);
722
+ }
723
+
724
+ return;
725
+ }
726
+
727
+ if (context.mesh_node !== HAIR_BINDING_NONE) {
728
+ this.#release_binding(context);
729
+ }
730
+
731
+ let binder = this.#binders.get(geometry);
732
+
733
+ if (binder === undefined) {
734
+ binder = groom_mesh_binder(geometry);
735
+ this.#binders.set(geometry, binder);
736
+ }
737
+
738
+ const groups = context.groom.groups;
739
+
740
+ for (let g = 0; g < groups.length; g++) {
741
+ const group = groups[g];
742
+
743
+ let by_geometry = this.#binding_cache.get(group);
744
+
745
+ if (by_geometry === undefined) {
746
+ by_geometry = new Map();
747
+ this.#binding_cache.set(group, by_geometry);
748
+ }
749
+
750
+ let binding = by_geometry.get(geometry) ?? null;
751
+
752
+ if (binding === null) {
753
+ const words = groom_bind_to_mesh(group, binder);
754
+ const run = words === null ? null : this.#pool.allocate(words.length);
755
+
756
+ if (run !== null && run.offset !== ALLOCATOR_NO_SPACE) {
757
+ this.#pool.write(run.offset, words, words.length);
758
+
759
+ binding = { run, references: 0 };
760
+ by_geometry.set(geometry, binding);
761
+ }
762
+ }
763
+
764
+ const roots = binding === null ? null : this.#state_pool.allocate(group.strand_count * HAIR_ROOT_FRAME_VECTORS);
765
+
766
+ if (binding === null || roots.offset === ALLOCATOR_NO_SPACE) {
767
+ // a binding no entity holds goes, so the pool is not kept from what it could house
768
+ if (binding !== null && binding.references === 0) {
769
+ this.#pool.free(binding.run);
770
+ by_geometry.delete(geometry);
771
+ }
772
+
773
+ context.bindings.push(null);
774
+ context.roots.push(null);
775
+ continue;
776
+ }
777
+
778
+ binding.references++;
779
+
780
+ context.bindings.push(binding);
781
+ context.roots.push(roots);
782
+ }
783
+
784
+ context.mesh_node = mesh_node;
785
+ context.mesh_geometry = geometry;
786
+ context.meshlet_base = meshlet_base;
787
+
788
+ this.#bound++;
789
+ this.#sums_dirty = true;
790
+
791
+ // the roots are somewhere else now: simulated hair starts over there rather than whipping across
792
+ this.reset(context);
793
+ this.#restage(context);
794
+ }
795
+
796
+ /**
797
+ * Lay an entity's simulated hair at rest on the next step, where its entity then is: for an entity placed rather
798
+ * than moved, a teleport no motion answers. Each group's state becomes a new tenant's.
799
+ *
800
+ * @param {GPUHairContext} context
801
+ */
802
+ reset(context) {
803
+ if (context.dynamics === null) {
804
+ return;
805
+ }
806
+
807
+ for (let g = 0; g < context.epochs.length; g++) {
808
+ this.#epoch = (this.#epoch % HAIR_EPOCH_MASK) + 1;
809
+ context.epochs[g] = this.#epoch;
810
+ }
811
+
812
+ this.#restage(context);
813
+ }
814
+
815
+ /**
816
+ * The colliders an entity's hair meets this frame: `count` of them, each `HAIR_COLLIDER_WORDS` words of
817
+ * `words` laid out as `HAIR_SIMULATION.js` says, the pose words left for the GPU to write.
818
+ *
819
+ * @param {GPUHairContext} context
820
+ * @param {Uint32Array} words
821
+ * @param {number} count at most `HAIR_COLLIDER_LIMIT`
822
+ */
823
+ set_colliders(context, words, count) {
824
+ assert.lessThanOrEqual(count, HAIR_COLLIDER_LIMIT, 'count');
825
+
826
+ const colliders = context.colliders;
827
+
828
+ for (let i = 0; i < count * HAIR_COLLIDER_WORDS; i++) {
829
+ colliders[i] = words[i];
830
+ }
831
+
832
+ context.collider_count = count;
833
+ }
834
+
835
+ /**
836
+ * Register an entity's hair.
837
+ *
838
+ * @param {Groom} groom
839
+ * @param {number} node the `transforms` row of the entity
840
+ * @param {HairMaterial} material
841
+ * @returns {GPUHairContext|null} null when the pool cannot house the groom on this device
842
+ */
843
+ add(groom, node, material) {
844
+ assert.defined(groom, 'groom');
845
+ assert.isNonNegativeInteger(node, 'node');
846
+ assert.greaterThan(groom.groups.length, 0, 'a groom holds a group');
847
+
848
+ const entry = this.#acquire_groom(groom);
849
+
850
+ if (entry === null) {
851
+ return null;
852
+ }
853
+
854
+ const context = new GPUHairContext();
855
+
856
+ context.groom = groom;
857
+ context.node = node;
858
+ context.material.copy(material);
859
+
860
+ for (let g = 0; g < groom.groups.length; g++) {
861
+ const row = this.table.add(this.#write_record(context, entry, g));
862
+
863
+ context.rows.push(row);
864
+
865
+ this.#row_limit = Math.max(this.#row_limit, row + 1);
866
+ }
867
+
868
+ this.#contexts.add(context);
869
+ this.#context_list.push(context);
870
+
871
+ return context;
872
+ }
873
+
874
+ /**
875
+ * Point an entity's hair at another `transforms` row.
876
+ *
877
+ * @param {GPUHairContext} context
878
+ * @param {number} node
879
+ */
880
+ set_node(context, node) {
881
+ assert.isNonNegativeInteger(node, 'node');
882
+
883
+ if (context.node === node) {
884
+ return;
885
+ }
886
+
887
+ context.node = node;
888
+
889
+ this.#restage(context);
890
+ }
891
+
892
+ /**
893
+ * @param {GPUHairContext} context
894
+ * @param {HairMaterial} material
895
+ */
896
+ set_material(context, material) {
897
+ if (context.material.equals(material)) {
898
+ return;
899
+ }
900
+
901
+ context.material.copy(material);
902
+
903
+ this.#restage(context);
904
+ }
905
+
906
+ /**
907
+ * @param {GPUHairContext} context
908
+ */
909
+ remove(context) {
910
+ assert.equal(this.#contexts.has(context), true, 'the hair is not registered here');
911
+
912
+ const table = this.table;
913
+
914
+ for (let i = 0; i < context.rows.length; i++) {
915
+ table.remove(context.rows[i]);
916
+ }
917
+
918
+ if (context.dynamics !== null) {
919
+ this.#release_states(context);
920
+
921
+ context.dynamics = null;
922
+ this.#simulated--;
923
+ this.#sums_dirty = true;
924
+ }
925
+
926
+ if (context.mesh_node !== HAIR_BINDING_NONE) {
927
+ this.#release_binding(context);
928
+ }
929
+
930
+ this.#release_groom(context.groom);
931
+
932
+ this.#contexts.delete(context);
933
+
934
+ const list = this.#context_list;
935
+ const at = list.indexOf(context);
936
+
937
+ list[at] = list[list.length - 1];
938
+ list.pop();
939
+
940
+ context.rows.length = 0;
941
+ context.groom = null;
942
+
943
+ let limit = 0;
944
+
945
+ for (const other of this.#contexts) {
946
+ for (let i = 0; i < other.rows.length; i++) {
947
+ limit = Math.max(limit, other.rows[i] + 1);
948
+ }
949
+ }
950
+
951
+ this.#row_limit = limit;
952
+ }
953
+
954
+ /**
955
+ * Send what changed since the last frame.
956
+ */
957
+ upload() {
958
+ const database = this.#database;
959
+
960
+ if (database.has_pending_upload) {
961
+ const cmd = ShadeGPUCommandContext.create(this.#graphics, "hair/upload");
962
+
963
+ database.update(cmd);
964
+
965
+ cmd.finish();
966
+ }
967
+ }
968
+
969
+ /**
970
+ * Record a view's hair into its G-buffer: the level of detail, the cull and deformation, the id raster under the
971
+ * scene's depth, the stamp and the velocity. Publishes the depth, the G-buffer targets, the visibility and the
972
+ * velocity it wrote, and creates the frame's {@link HairFrame}. Records nothing while no hair is registered.
973
+ *
974
+ * @param {FrameContext} frame at the phase where the G-buffer is written into
975
+ */
976
+ graph_draw(frame) {
977
+ if (this.#contexts.size === 0) {
978
+ return;
979
+ }
980
+
981
+ this.upload();
982
+ this.#grow();
983
+
984
+ const table = this.table;
985
+ const group_count = table.dispatch_group_count(HAIR_LOD_WORKGROUP_SIZE);
986
+
987
+ if (group_count === 0) {
988
+ return;
989
+ }
990
+
991
+ const graph = frame.graph;
992
+ const view = frame.view;
993
+ const camera = view.camera;
994
+ const scene = view.scene;
995
+
996
+ const [width, height] = frame.resolution;
997
+
998
+ const capacity = Math.max(1, table.element_capacity);
999
+ const point_capacity = this.#point_capacity;
1000
+ const list_capacity = this.#list_capacity;
1001
+ const voxel_capacity = this.#voxel_capacity;
1002
+
1003
+ const hair_database = this.#database.buffer;
1004
+ const hair_grooms = this.#pool.buffer;
1005
+ const scene_database = scene.scene_database_buffer;
1006
+ const camera_buffer = camera.gpu_buffer;
1007
+
1008
+ // the shadow views the strands draw into this frame: none where they cast no shadows or the lights are not
1009
+ // shadow-mapped
1010
+ const shadows = this.cast_shadows && frame.has(ShadowAtlas) ? frame.get(ShadowAtlas) : null;
1011
+
1012
+ this.#casting = shadows !== null && shadows.mapped;
1013
+
1014
+ const shadow_view_count = this.#write_shadow_views(this.#casting ? shadows : null, list_capacity);
1015
+ const shadow_views = this.#shadow_views_buffer;
1016
+
1017
+ graph.push_scope("hair");
1018
+
1019
+ const guides = this.#graph_simulate(graph, scene, capacity);
1020
+
1021
+ const count_bytes = collection_size(capacity);
1022
+ const lod_bytes = capacity * HAIR_LOD_WORDS * 4;
1023
+ const counter_bytes = HAIR_COUNTER_WORDS * 4;
1024
+
1025
+ const lod = graph_compute_pass({
1026
+ graph,
1027
+ shader: shader_hair_lod,
1028
+ bindings: {
1029
+ camera: camera_buffer,
1030
+ hair_database,
1031
+ scene_database,
1032
+ hair_guide_bounds: guides.bounds,
1033
+ // cleared whole: the pass writes only the rows it visits
1034
+ hair_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
1035
+ hair_mip_1_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
1036
+ hair_mip_2_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
1037
+ hair_lod: BufferResourceDescriptor.from(lod_bytes, GPUBufferUsage.STORAGE, 0, lod_bytes),
1038
+ // read back for what was asked
1039
+ hair_counters: BufferResourceDescriptor.from(counter_bytes, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, 0, counter_bytes),
1040
+ hair_shadow_views: shadow_views,
1041
+ settings: {
1042
+ resolution: [width, height],
1043
+ pixel_floor: this.pixel_floor,
1044
+ tolerance: HAIR_DECIMATION_TOLERANCE,
1045
+ capacity,
1046
+ point_capacity,
1047
+ voxel_capacity,
1048
+ shadow_view_count,
1049
+ },
1050
+ },
1051
+ group_count: [group_count, 1, 1],
1052
+ });
1053
+
1054
+ // the voxels the level of detail reserved, zeroed before the cull lays the strands into them
1055
+ const volume_command = graph_compute_pass({
1056
+ graph,
1057
+ shader: shader_hair_volume_command,
1058
+ bindings: {
1059
+ hair_counters: lod.hair_counters,
1060
+ command: graph_create_buffer(graph, "hair/volume command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
1061
+ settings: { voxel_capacity },
1062
+ },
1063
+ }).command;
1064
+
1065
+ const cleared = graph_compute_indirect_pass({
1066
+ graph,
1067
+ shader: shader_hair_volume_clear,
1068
+ command: volume_command,
1069
+ bindings: {
1070
+ hair_counters: lod.hair_counters,
1071
+ hair_volume: BufferResourceDescriptor.from(voxel_capacity * 4, GPUBufferUsage.STORAGE),
1072
+ settings: { voxel_capacity },
1073
+ },
1074
+ });
1075
+
1076
+ const regions = graph_compute_pass({
1077
+ graph,
1078
+ shader: shader_hair_regions,
1079
+ bindings: { hair_counters: lod.hair_counters },
1080
+ });
1081
+
1082
+ const sums = graph_prefix_scan_csdldf({
1083
+ graph,
1084
+ input: lod.hair_counts,
1085
+ output: graph_create_buffer(graph, "hair/strand sums", prefix_scan_csdldf_output_descriptor(capacity)),
1086
+ element_max_count: capacity,
1087
+ });
1088
+
1089
+ const command = graph_compute_pass({
1090
+ graph,
1091
+ shader: shader_prefix_sum_to_command,
1092
+ bindings: {
1093
+ prefix_sums: sums,
1094
+ command: graph_create_buffer(graph, "hair/cull command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
1095
+ },
1096
+ }).command;
1097
+
1098
+ const cull = graph_compute_indirect_pass({
1099
+ graph,
1100
+ shader: shader_hair_cull,
1101
+ command,
1102
+ bindings: {
1103
+ camera: camera_buffer,
1104
+ hair_grooms,
1105
+ hair_database,
1106
+ scene_database,
1107
+ prefix_sums: sums,
1108
+ hair_lod: lod.hair_lod,
1109
+ hair_counters: regions.hair_counters,
1110
+ hair_points: BufferResourceDescriptor.from(point_capacity * 2 * 16, GPUBufferUsage.STORAGE),
1111
+ // the entries, then their shadow words where the strands draw into shadow views
1112
+ hair_list: BufferResourceDescriptor.from(list_capacity * (shadow_view_count > 0 ? 1 + HAIR_LIST_SHADOW_WORDS : 1) * 4, GPUBufferUsage.STORAGE),
1113
+ hair_volume: cleared.hair_volume,
1114
+ hair_guide_state: guides.state,
1115
+ settings: {
1116
+ resolution: [width, height],
1117
+ pixel_floor: this.pixel_floor,
1118
+ point_capacity,
1119
+ list_capacity,
1120
+ casts_shadows: shadow_view_count > 0 ? 1 : 0,
1121
+ },
1122
+ },
1123
+ });
1124
+
1125
+ const args = graph_compute_pass({
1126
+ graph,
1127
+ shader: shader_hair_draw_args,
1128
+ bindings: {
1129
+ hair_counters: cull.hair_counters,
1130
+ hair_draw_args: BufferResourceDescriptor.from(
1131
+ HAIR_CLASS_COUNT * HAIR_DRAW_ARGS_WORDS * 4,
1132
+ GPUBufferUsage.STORAGE | GPUBufferUsage.INDIRECT,
1133
+ ),
1134
+ settings: { list_capacity },
1135
+ },
1136
+ });
1137
+
1138
+ // the volumes' coarser levels, a thread a voxel of each: level 1 from the voxels, then level 2 from level 1
1139
+ let volume = cull.hair_volume;
1140
+
1141
+ for (let level = 1; level < HAIR_VOLUME_MIPS; level++) {
1142
+ const counts = level === 1 ? lod.hair_mip_1_counts : lod.hair_mip_2_counts;
1143
+
1144
+ const sums = graph_prefix_scan_csdldf({
1145
+ graph,
1146
+ input: counts,
1147
+ output: graph_create_buffer(graph, "hair/mip sums", prefix_scan_csdldf_output_descriptor(capacity)),
1148
+ element_max_count: capacity,
1149
+ });
1150
+
1151
+ const mip_command = graph_compute_pass({
1152
+ graph,
1153
+ shader: shader_prefix_sum_to_command,
1154
+ bindings: {
1155
+ prefix_sums: sums,
1156
+ command: graph_create_buffer(graph, "hair/mip command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
1157
+ },
1158
+ }).command;
1159
+
1160
+ volume = graph_compute_indirect_pass({
1161
+ graph,
1162
+ shader: shader_hair_volume_mips,
1163
+ command: mip_command,
1164
+ bindings: {
1165
+ hair_lod: lod.hair_lod,
1166
+ prefix_sums: sums,
1167
+ hair_volume: volume,
1168
+ settings: { level },
1169
+ },
1170
+ }).hair_volume;
1171
+ }
1172
+
1173
+ // what was asked for: a frame's buffers too small for it are made larger from the next frame
1174
+ graph_read_buffer({ graph, buffer: cull.hair_counters, offset: 0, size: (HAIR_COUNTER_SHADOW_STRANDS + HAIR_CLASS_COUNT) * 4 })
1175
+ .then(reference => {
1176
+ try {
1177
+ const words = new Uint32Array(reference.getValue());
1178
+
1179
+ let strands = 0;
1180
+
1181
+ for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
1182
+ strands += words[c] + words[HAIR_COUNTER_SHADOW_STRANDS + c];
1183
+ }
1184
+
1185
+ this.#strands_asked = Math.max(this.#strands_asked, strands);
1186
+ this.#points_asked = Math.max(this.#points_asked, words[HAIR_COUNTER_POINTS]);
1187
+ this.#voxels_asked = Math.max(this.#voxels_asked, words[HAIR_COUNTER_VOXELS]);
1188
+ } finally {
1189
+ reference.release();
1190
+ }
1191
+ })
1192
+ .catch(() => {
1193
+ // a reading lost to a timeout or a lost device: a later frame's carries it
1194
+ });
1195
+
1196
+ this.#write_view(width, height, point_capacity);
1197
+
1198
+ const view_words = this.#view_words;
1199
+
1200
+ const view_textures = frame.get(ViewTextures);
1201
+ const gbuffer = frame.get(GBufferTextures);
1202
+
1203
+ // the id raster: every class's strands under the scene's depth
1204
+ const id_data = {};
1205
+
1206
+ const id_pass = graph.add("draw hair ids", id_data, (data, resources, context) => {
1207
+ const encoder = context.encoder;
1208
+
1209
+ const bindings = HAIR_ID_RESOURCES.generateBindings({
1210
+ camera: camera_buffer,
1211
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
1212
+ hair_list: resources.get(data.list),
1213
+ hair_points: resources.get(data.points),
1214
+ }, encoder);
1215
+
1216
+ const pass = encoder.constructRenderPass({
1217
+ label: "Hair Ids",
1218
+ pipeline: HAIR_ID_PIPELINE,
1219
+ bindings,
1220
+ colorAttachments: [{
1221
+ view: resources.get(data.ids).obtainView(MIP_0),
1222
+ loadOp: "clear",
1223
+ clearValue: [HAIR_ID_NONE, 0, 0, 0],
1224
+ storeOp: "store",
1225
+ }],
1226
+ depthStencilAttachment: {
1227
+ view: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
1228
+ // the scene's depth is what strands are hidden by, and what they then hide
1229
+ depthLoadOp: "load",
1230
+ depthStoreOp: "store",
1231
+ },
1232
+ });
1233
+
1234
+ const draw_args = resources.get(data.args);
1235
+
1236
+ for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
1237
+ pass.drawIndirect(draw_args, c * HAIR_DRAW_ARGS_WORDS * 4);
1238
+ }
1239
+
1240
+ pass.end();
1241
+ });
1242
+
1243
+ id_data.list = id_pass.read(cull.hair_list);
1244
+ id_data.points = id_pass.read(cull.hair_points);
1245
+ id_data.args = id_pass.read(args.hair_draw_args);
1246
+ id_data.depth = id_pass.write(view_textures.depth);
1247
+ id_data.ids = id_pass.create("hair ids", TextureResourceDescriptor.from({
1248
+ format: HAIR_ID_FORMAT,
1249
+ resolution: [width, height],
1250
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
1251
+ }));
1252
+
1253
+ view_textures.depth = id_data.depth;
1254
+
1255
+ if (shadow_view_count > 0) {
1256
+ this.#graph_cast_shadows(graph, shadows, shadow_view_count, list_capacity, id_data.list, id_data.points, cull.hair_counters);
1257
+ }
1258
+
1259
+ view_velocity_matrices(view, this.#inverse_view_projection, this.#view_projection_previous);
1260
+
1261
+ const inverse_view_projection = this.#inverse_view_projection.slice();
1262
+ const view_projection_previous = this.#view_projection_previous.slice();
1263
+
1264
+ const row_limit = this.#row_limit;
1265
+
1266
+ // the G-buffer and the velocity under every pixel a strand covers, an instance's rectangle at a time
1267
+ const stamp_data = {};
1268
+
1269
+ const stamp_pass = graph.add("stamp hair", stamp_data, (data, resources, context) => {
1270
+ const encoder = context.encoder;
1271
+
1272
+ const bindings = HAIR_STAMP_RESOURCES.generateBindings({
1273
+ camera: camera_buffer,
1274
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
1275
+ hair_lod: resources.get(data.lod),
1276
+ hair_points: resources.get(data.points),
1277
+ hair_ids: resources.get(data.ids).obtainView(MIP_0),
1278
+ scene_depth: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
1279
+ inverse_view_projection: encoder.allocateTransientBufferAndLoad(inverse_view_projection.buffer, GPUBufferUsage.UNIFORM),
1280
+ view_projection_previous: encoder.allocateTransientBufferAndLoad(view_projection_previous.buffer, GPUBufferUsage.UNIFORM),
1281
+ }, encoder);
1282
+
1283
+ const attachment = handle => ({
1284
+ view: resources.get(handle).obtainView(MIP_0),
1285
+ // every pixel no strand covers keeps what the scene left there
1286
+ loadOp: "load",
1287
+ storeOp: "store",
1288
+ });
1289
+
1290
+ const pass = encoder.constructRenderPass({
1291
+ label: "Hair Stamp",
1292
+ pipeline: HAIR_STAMP_PIPELINE,
1293
+ bindings,
1294
+ colorAttachments: [
1295
+ attachment(data.pbr),
1296
+ attachment(data.normal),
1297
+ attachment(data.albedo),
1298
+ attachment(data.emissive),
1299
+ attachment(data.visibility),
1300
+ ],
1301
+ });
1302
+
1303
+ pass.draw(4, row_limit);
1304
+ pass.end();
1305
+
1306
+ // the velocity on its own: with the surface's targets it would pass the device floor's attachment bytes
1307
+ const motion = encoder.constructRenderPass({
1308
+ label: "Hair Motion",
1309
+ pipeline: HAIR_MOTION_PIPELINE,
1310
+ bindings,
1311
+ colorAttachments: [attachment(data.velocity)],
1312
+ });
1313
+
1314
+ motion.draw(4, row_limit);
1315
+ motion.end();
1316
+ });
1317
+
1318
+ stamp_data.lod = stamp_pass.read(lod.hair_lod);
1319
+ stamp_data.points = stamp_pass.read(cull.hair_points);
1320
+ stamp_data.ids = stamp_pass.read(id_data.ids);
1321
+ stamp_data.depth = stamp_pass.read(id_data.depth);
1322
+
1323
+ // in place: the written handles are what the records hand on
1324
+ stamp_data.pbr = stamp_pass.write(gbuffer.pbr);
1325
+ stamp_data.normal = stamp_pass.write(gbuffer.normal);
1326
+ stamp_data.albedo = stamp_pass.write(gbuffer.albedo);
1327
+ stamp_data.emissive = stamp_pass.write(gbuffer.emissive);
1328
+
1329
+ gbuffer.pbr = stamp_data.pbr;
1330
+ gbuffer.normal = stamp_data.normal;
1331
+ gbuffer.albedo = stamp_data.albedo;
1332
+ gbuffer.emissive = stamp_data.emissive;
1333
+
1334
+ stamp_data.visibility = stamp_pass.write(view_textures.visibility);
1335
+ stamp_data.velocity = stamp_pass.write(view_textures.velocity);
1336
+
1337
+ view_textures.visibility = stamp_data.visibility;
1338
+ view_textures.velocity = stamp_data.velocity;
1339
+
1340
+ const record = frame.create(HairFrame);
1341
+
1342
+ record.ids = id_data.ids;
1343
+ record.points = cull.hair_points;
1344
+ record.lod = lod.hair_lod;
1345
+ record.volume = volume;
1346
+
1347
+ graph.pop_scope();
1348
+ }
1349
+
1350
+ /**
1351
+ * Draw the frame's strands into the shadow views drawn this frame and publish the atlas they were drawn into:
1352
+ * each view's list of the strands it draws (`shader_hair_shadow_list`), its draws (`shader_hair_shadow_draw_args`),
1353
+ * then a render pass a view through them.
1354
+ *
1355
+ * @param {FrameGraph} graph
1356
+ * @param {ShadowAtlas} shadows
1357
+ * @param {number} view_count the first of the atlas's views, as {@link #write_shadow_views} wrote them
1358
+ * @param {number} list_capacity
1359
+ * @param {number} list the frame's draw list, with its shadow words
1360
+ * @param {number} points the frame's deformed points
1361
+ * @param {number} counters the frame's counters, as the cull left them
1362
+ */
1363
+ #graph_cast_shadows(graph, shadows, view_count, list_capacity, list, points, counters) {
1364
+ const settings = { list_capacity, view_count };
1365
+ const count_bytes = view_count * HAIR_CLASS_COUNT * 4;
1366
+
1367
+ const command = graph_compute_pass({
1368
+ graph,
1369
+ shader: shader_hair_shadow_list_command,
1370
+ bindings: {
1371
+ hair_counters: counters,
1372
+ command: graph_create_buffer(graph, "hair/shadow list command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
1373
+ settings,
1374
+ },
1375
+ }).command;
1376
+
1377
+ const listed = graph_compute_indirect_pass({
1378
+ graph,
1379
+ shader: shader_hair_shadow_list,
1380
+ command,
1381
+ bindings: {
1382
+ hair_counters: counters,
1383
+ hair_list: list,
1384
+ hair_points: points,
1385
+ hair_shadow_views: this.#shadow_views_buffer,
1386
+ // cleared whole: the views' counts start from nothing
1387
+ hair_shadow_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
1388
+ hair_shadow_list: BufferResourceDescriptor.from(view_count * list_capacity * 4, GPUBufferUsage.STORAGE),
1389
+ settings,
1390
+ },
1391
+ });
1392
+
1393
+ const args = graph_compute_pass({
1394
+ graph,
1395
+ shader: shader_hair_shadow_draw_args,
1396
+ bindings: {
1397
+ hair_counters: counters,
1398
+ hair_shadow_counts: listed.hair_shadow_counts,
1399
+ hair_shadow_draw_args: BufferResourceDescriptor.from(
1400
+ view_count * HAIR_CLASS_COUNT * HAIR_DRAW_ARGS_WORDS * 4,
1401
+ GPUBufferUsage.STORAGE | GPUBufferUsage.INDIRECT,
1402
+ ),
1403
+ settings,
1404
+ },
1405
+ group_count: [view_count, 1, 1],
1406
+ }).hair_shadow_draw_args;
1407
+
1408
+ const views = shadows.views;
1409
+ const view_words = this.#shadow_view_words;
1410
+
1411
+ const data = {};
1412
+
1413
+ const pass = graph.add("draw hair shadows", data, (data, resources, context) => {
1414
+ const encoder = context.encoder;
1415
+ const atlas = resources.get(data.atlas).obtainView();
1416
+ const hair_list = resources.get(data.list);
1417
+ const hair_points = resources.get(data.points);
1418
+ const draw_args = resources.get(data.args);
1419
+
1420
+ for (let v = 0; v < view_count; v++) {
1421
+ const view = views[v];
1422
+ const [x, y, width, height] = view.viewport;
1423
+
1424
+ view_words[0] = width;
1425
+ view_words[1] = height;
1426
+
1427
+ const bindings = HAIR_SHADOW_RESOURCES.generateBindings({
1428
+ camera: view.camera_buffer,
1429
+ hair_shadow_view: encoder.allocateTransientBufferAndLoad(view_words.buffer, GPUBufferUsage.UNIFORM),
1430
+ hair_list,
1431
+ hair_points,
1432
+ }, encoder);
1433
+
1434
+ const render = encoder.constructRenderPass({
1435
+ label: "Hair Shadow",
1436
+ pipeline: HAIR_SHADOW_PIPELINE,
1437
+ bindings,
1438
+ colorAttachments: [],
1439
+ depthStencilAttachment: {
1440
+ view: atlas,
1441
+ // the scene's casters, drawn this frame, hide strands behind them and are hidden by those
1442
+ // in front
1443
+ depthLoadOp: "load",
1444
+ depthStoreOp: "store",
1445
+ },
1446
+ });
1447
+
1448
+ render.setViewport(x, y, width, height, 0, 1);
1449
+
1450
+ for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
1451
+ render.drawIndirect(draw_args, (v * HAIR_CLASS_COUNT + c) * HAIR_DRAW_ARGS_WORDS * 4);
1452
+ }
1453
+
1454
+ render.end();
1455
+ }
1456
+ });
1457
+
1458
+ data.list = pass.read(listed.hair_shadow_list);
1459
+ data.points = pass.read(points);
1460
+ data.args = pass.read(args);
1461
+ data.atlas = pass.write(shadows.atlas);
1462
+
1463
+ shadows.atlas = data.atlas;
1464
+ }
1465
+
1466
+ /**
1467
+ * Record the light of a view's hair, written over the pixels its strands cover in the scene colour, which it
1468
+ * republishes. Records nothing for a frame {@link graph_draw} drew no hair into.
1469
+ *
1470
+ * @param {FrameContext} frame at the phase where the frame has been lit
1471
+ */
1472
+ graph_light(frame) {
1473
+ if (!frame.has(HairFrame)) {
1474
+ return;
1475
+ }
1476
+
1477
+ const hair = frame.get(HairFrame);
1478
+ const lighting = frame.get(ViewLighting);
1479
+ const scene_color = frame.get(SceneColor);
1480
+ const view_textures = frame.get(ViewTextures);
1481
+
1482
+ const graph = frame.graph;
1483
+ const view = frame.view;
1484
+ const scene = view.scene;
1485
+
1486
+ const [width, height] = frame.resolution;
1487
+
1488
+ this.#write_view(width, height, this.#point_capacity);
1489
+
1490
+ const view_words = this.#view_words.slice(0);
1491
+ const row_limit = this.#row_limit;
1492
+ const hair_database = this.#database.buffer;
1493
+
1494
+ const color_format = frame.describe(scene_color.color).format;
1495
+ const pipeline = hair_lighting_pipeline(lighting.shader_switches, color_format);
1496
+ const probes = lighting.probes;
1497
+
1498
+ const data = {};
1499
+
1500
+ const pass = graph.add("light hair", data, (data, resources, context) => {
1501
+ const encoder = context.encoder;
1502
+ const graphics = context.graphics;
1503
+
1504
+ const sampler_linear = graphics.samplers.obtain(SamplerDescriptor.from({ magFilter: "linear" }));
1505
+
1506
+ const inputs = probes.write_sampling_bindings({
1507
+ camera: view.camera.gpu_buffer,
1508
+ view: view.uniform_buffer.buffer,
1509
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
1510
+ hair_lod: resources.get(data.lod),
1511
+ // the lights
1512
+ light_data: scene.lights.buffer_data,
1513
+ tEnvironment: scene.lights.environment.obtainView(),
1514
+ sEnvironment: graphics.samplers.obtain(ENVIRONMENT_MAP_SAMPLER_DESCRIPTOR),
1515
+ cluster_parameters: resources.get(data.light_cluster_parameters),
1516
+ cluster_lookup: resources.get(data.light_cluster_lookup),
1517
+ cluster_data: resources.get(data.light_cluster_data),
1518
+ shadowmapAtlas: resources.get(data.shadowmap_atlas).obtainView(),
1519
+ sCompareLinear: graphics.samplers.obtain(SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR),
1520
+ // the strands
1521
+ hair_points: resources.get(data.points),
1522
+ hair_database,
1523
+ hair_ids: resources.get(data.ids).obtainView(MIP_0),
1524
+ scene_depth: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
1525
+ // indirect light and fog
1526
+ brick4: scene.volumetric_light_map.buffer,
1527
+ tVolumetrics: resources.get(data.volumetrics_lut),
1528
+ volumetrics_metadata: resources.get(data.volumetrics_metadata),
1529
+ tSTBN_vec3: graphics.textures.obtain(STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec3),
1530
+ sLinear: sampler_linear,
1531
+ hair_volume: resources.get(data.volume),
1532
+ tHairScattering: graphics.textures.obtain(STATIC_GRAPHICS_ENGINE_ASSETS.hair_scattering),
1533
+ });
1534
+
1535
+ const bindings = HAIR_LIGHTING_RESOURCES.generateBindings(inputs, encoder);
1536
+
1537
+ const render = encoder.constructRenderPass({
1538
+ label: "Hair Lighting",
1539
+ pipeline,
1540
+ bindings,
1541
+ colorAttachments: [{
1542
+ view: resources.get(data.color).obtainView(MIP_0),
1543
+ // every pixel no strand covers keeps its colour
1544
+ loadOp: "load",
1545
+ storeOp: "store",
1546
+ }],
1547
+ });
1548
+
1549
+ render.draw(4, row_limit);
1550
+ render.end();
1551
+ });
1552
+
1553
+ data.lod = pass.read(hair.lod);
1554
+ data.points = pass.read(hair.points);
1555
+ data.volume = pass.read(hair.volume);
1556
+ data.ids = pass.read(hair.ids);
1557
+ data.depth = pass.read(view_textures.depth);
1558
+ data.light_cluster_parameters = pass.read(lighting.light_cluster_parameters);
1559
+ data.light_cluster_lookup = pass.read(lighting.light_cluster_lookup);
1560
+ data.light_cluster_data = pass.read(lighting.light_cluster_data);
1561
+ data.shadowmap_atlas = pass.read(lighting.shadowmap_atlas);
1562
+ data.volumetrics_lut = pass.read(lighting.volumetrics_lut);
1563
+ data.volumetrics_metadata = pass.read(lighting.volumetrics_metadata);
1564
+
1565
+ pass.read(scene_color.color);
1566
+ data.color = pass.write(scene_color.color);
1567
+
1568
+ scene_color.color = data.color;
1569
+ }
1570
+
1571
+ destroy() {
1572
+ this.#database.destroy();
1573
+ this.#pool.destroy();
1574
+ this.#state_pool.destroy();
1575
+ this.#bounds_buffer?.destroy();
1576
+ this.#shadow_views_buffer?.destroy();
1577
+ this.#sums_buffer?.destroy();
1578
+ this.#root_sums_buffer?.destroy();
1579
+ this.#collider_buffer?.destroy();
1580
+ this.#root_sums_buffer = null;
1581
+ this.#bounds_buffer = null;
1582
+ this.#sums_buffer = null;
1583
+ this.#collider_buffer = null;
1584
+ this.#grooms.clear();
1585
+ this.#contexts.clear();
1586
+ this.#context_list.length = 0;
1587
+ this.#row_limit = 0;
1588
+ }
1589
+
1590
+ /**
1591
+ * @param {number} width
1592
+ * @param {number} height
1593
+ * @param {number} point_capacity
1594
+ */
1595
+ #write_view(width, height, point_capacity) {
1596
+ this.#view_f32[0] = width;
1597
+ this.#view_f32[1] = height;
1598
+ this.#view_f32[2] = this.pixel_floor;
1599
+ this.#view_u32[3] = point_capacity;
1600
+ this.#view_u32[4] = this.#casting ? 1 : 0;
1601
+ }
1602
+
1603
+ /**
1604
+ * Write the shadow views the strands draw into this frame into {@link #shadow_views_buffer}, which it makes or
1605
+ * grows to hold them, at least one. No more views than a list of each can be bound for, and a thread an entry in
1606
+ * each indexed by a `u32`, are drawn into.
1607
+ *
1608
+ * @param {ShadowAtlas|null} shadows the frame's, or null where the strands draw into none
1609
+ * @param {number} list_capacity
1610
+ * @returns {number} the views written
1611
+ */
1612
+ #write_shadow_views(shadows, list_capacity) {
1613
+ const device = this.#graphics.device;
1614
+ const limits = device.limits;
1615
+ const binding_bytes = Math.min(limits.maxStorageBufferBindingSize, limits.maxBufferSize);
1616
+
1617
+ const count = shadows === null
1618
+ ? 0
1619
+ : Math.min(shadows.view_count, Math.floor(binding_bytes / (list_capacity * 4)));
1620
+
1621
+ const view_words = HAIR_SHADOW_VIEW_STRUCT.size / 4;
1622
+ const needed = Math.max(1, count) * HAIR_SHADOW_VIEW_STRUCT.size;
1623
+
1624
+ if (this.#shadow_views_buffer === null || this.#shadow_views_buffer.size < needed) {
1625
+ this.#shadow_views_buffer?.destroy();
1626
+ this.#shadow_views_buffer = device.createBuffer({
1627
+ label: "Hair / shadow views",
1628
+ size: needed,
1629
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1630
+ });
1631
+ this.#shadow_views_words = new Float32Array(needed / 4);
1632
+ }
1633
+
1634
+ if (count === 0) {
1635
+ return 0;
1636
+ }
1637
+
1638
+ const words = this.#shadow_views_words;
1639
+ const views = shadows.views;
1640
+
1641
+ for (let v = 0; v < count; v++) {
1642
+ const view = views[v];
1643
+ const camera = view.camera;
1644
+ const frustum = camera.frustum;
1645
+ const vp = camera.view_projection_matrix;
1646
+ const p = camera.projection_matrix;
1647
+ const width = view.viewport[2];
1648
+ const height = view.viewport[3];
1649
+ const at = v * view_words;
1650
+
1651
+ // left, right, bottom, top, then far; the near plane, frustum[4], is not bound
1652
+ for (let i = 0; i < 16; i++) {
1653
+ words[at + i] = frustum[i];
1654
+ }
1655
+
1656
+ for (let i = 0; i < 4; i++) {
1657
+ words[at + 16 + i] = frustum[20 + i];
1658
+ }
1659
+
1660
+ words[at + 20] = vp[3];
1661
+ words[at + 21] = vp[7];
1662
+ words[at + 22] = vp[11];
1663
+ words[at + 23] = vp[15];
1664
+
1665
+ words[at + 24] = width;
1666
+ words[at + 25] = height;
1667
+ words[at + 26] = 2 / Math.max(Math.abs(p[0]) * width, Math.abs(p[5]) * height);
1668
+ words[at + 27] = 0;
1669
+ }
1670
+
1671
+ device.queue.writeBuffer(this.#shadow_views_buffer, 0, words.buffer, 0, count * HAIR_SHADOW_VIEW_STRUCT.size);
1672
+
1673
+ return count;
1674
+ }
1675
+
1676
+ /**
1677
+ * Make the frame's buffers hold what the frames before asked for, within what a binding can hold.
1678
+ */
1679
+ #grow() {
1680
+ const limits = this.#graphics.device.limits;
1681
+ const binding_bytes = Math.min(limits.maxStorageBufferBindingSize, limits.maxBufferSize);
1682
+
1683
+ // both halves of the deformed points in one binding
1684
+ const point_limit = Math.min(HAIR_POINT_CAPACITY_LIMIT, Math.floor(binding_bytes / 32));
1685
+ const list_limit = Math.floor(binding_bytes / 4);
1686
+
1687
+ while (this.#points_asked > this.#point_capacity && this.#point_capacity < point_limit) {
1688
+ this.#point_capacity = Math.min(this.#point_capacity * 2, point_limit);
1689
+ }
1690
+
1691
+ while (this.#strands_asked > this.#list_capacity && this.#list_capacity < list_limit) {
1692
+ this.#list_capacity = Math.min(this.#list_capacity * 2, list_limit);
1693
+ }
1694
+
1695
+ while (this.#voxels_asked > this.#voxel_capacity && this.#voxel_capacity < list_limit) {
1696
+ this.#voxel_capacity = Math.min(this.#voxel_capacity * 2, list_limit);
1697
+ }
1698
+ }
1699
+
1700
+ /**
1701
+ * @param {GPUHairContext} context
1702
+ */
1703
+ #restage(context) {
1704
+ const entry = this.#grooms.get(context.groom);
1705
+ const table = this.table;
1706
+
1707
+ for (let g = 0; g < context.rows.length; g++) {
1708
+ table.set(context.rows[g], this.#write_record(context, entry, g));
1709
+ }
1710
+ }
1711
+
1712
+ /**
1713
+ * @param {GPUHairContext} context
1714
+ * @param {GroomEntry} entry
1715
+ * @param {number} g the group
1716
+ * @returns {Object} the record, for the table to copy
1717
+ */
1718
+ #write_record(context, entry, g) {
1719
+ const group = context.groom.groups[g];
1720
+ const material = context.material;
1721
+ const record = this.#record;
1722
+
1723
+ assert.lessThanOrEqual(group.level_count, 8, 'a group of more than 129 points');
1724
+
1725
+ record.node = context.node;
1726
+ record.points = entry.offsets[g * 3];
1727
+ record.strands = entry.offsets[g * 3 + 1];
1728
+ record.guides = entry.offsets[g * 3 + 2];
1729
+ record.strand_count = group.strand_count;
1730
+ record.point_count = group.point_count;
1731
+ record.level_count = group.level_count;
1732
+ record.melanin = material.melanin;
1733
+ record.redness = material.redness;
1734
+
1735
+ const bounds = group.bounds;
1736
+
1737
+ record.bounds_min[0] = bounds[0];
1738
+ record.bounds_min[1] = bounds[1];
1739
+ record.bounds_min[2] = bounds[2];
1740
+ record.bounds_extent[0] = bounds[3] - bounds[0];
1741
+ record.bounds_extent[1] = bounds[4] - bounds[1];
1742
+ record.bounds_extent[2] = bounds[5] - bounds[2];
1743
+
1744
+ record.width_max = group.width_max;
1745
+ record.width_mean = group.width_mean;
1746
+
1747
+ const decimation = group.decimation;
1748
+
1749
+ for (let i = 0; i < 4; i++) {
1750
+ record.decimation_low[i] = i < decimation.length ? decimation[i] : 0;
1751
+ record.decimation_high[i] = i + 4 < decimation.length ? decimation[i + 4] : 0;
1752
+ }
1753
+
1754
+ record.dye.set(material.dye);
1755
+ record.roughness = material.roughness;
1756
+ record.root_spacing = group.root_spacing;
1757
+
1758
+ record.guide_count = group.guide_count;
1759
+ record.guide_point_count = group.guide_point_count;
1760
+ record.guide_reach = group.guide_reach;
1761
+
1762
+ let length_max = 0;
1763
+
1764
+ for (let i = 0; i < group.guide_count; i++) {
1765
+ length_max = Math.max(length_max, group.guide_length[i]);
1766
+ }
1767
+
1768
+ record.guide_length_max = length_max;
1769
+
1770
+ const dynamics = context.dynamics;
1771
+ const state = dynamics === null ? null : context.states[g];
1772
+
1773
+ record.state = state === null || state === undefined ? HAIR_STATE_NONE : state.offset;
1774
+ record.epoch = dynamics === null ? 0 : context.epochs[g];
1775
+ record.damping = dynamics === null ? 0 : dynamics.damping;
1776
+ record.global_shape_rate = dynamics === null ? 0 : dynamics.global_shape_rate;
1777
+ record.global_shape_range = dynamics === null ? 0 : dynamics.global_shape_range;
1778
+ record.local_shape_rate = dynamics === null ? 0 : dynamics.local_shape_rate;
1779
+ record.friction = dynamics === null ? 0 : dynamics.friction;
1780
+ record.thickness = dynamics === null ? 0 : dynamics.thickness;
1781
+
1782
+ const binding = context.mesh_node === HAIR_BINDING_NONE ? null : context.bindings[g];
1783
+
1784
+ record.mesh_node = binding === null || binding === undefined ? HAIR_BINDING_NONE : context.mesh_node;
1785
+ record.meshlet_base = context.meshlet_base;
1786
+ record.bindings = binding === null || binding === undefined ? 0 : binding.run.offset;
1787
+ record.roots = binding === null || binding === undefined ? 0 : context.roots[g].offset;
1788
+
1789
+ return record;
1790
+ }
1791
+
1792
+ /**
1793
+ * The groom on the device, uploaded on first use.
1794
+ *
1795
+ * @param {Groom} groom
1796
+ * @returns {GroomEntry|null}
1797
+ */
1798
+ #acquire_groom(groom) {
1799
+ let entry = this.#grooms.get(groom);
1800
+
1801
+ if (entry !== undefined) {
1802
+ entry.references++;
1803
+
1804
+ return entry;
1805
+ }
1806
+
1807
+ const groups = groom.groups;
1808
+
1809
+ let words = 0;
1810
+
1811
+ for (let g = 0; g < groups.length; g++) {
1812
+ const group = groups[g];
1813
+
1814
+ words += group.strand_count * group.point_count * GROOM_POINT_WORDS + group.strand_count
1815
+ + group.guide_count * HAIR_GUIDE_RECORD_WORDS;
1816
+ }
1817
+
1818
+ const run = this.#pool.allocate(words);
1819
+
1820
+ if (run.offset === ALLOCATOR_NO_SPACE) {
1821
+ return null;
1822
+ }
1823
+
1824
+ entry = new GroomEntry();
1825
+ entry.run = run;
1826
+ entry.offsets = new Uint32Array(groups.length * 3);
1827
+ entry.references = 1;
1828
+
1829
+ let at = run.offset;
1830
+
1831
+ for (let g = 0; g < groups.length; g++) {
1832
+ const group = groups[g];
1833
+
1834
+ entry.offsets[g * 3] = at;
1835
+ this.#pool.write(at, group.points, group.points.length);
1836
+ at += group.points.length;
1837
+
1838
+ // a strand's word: its variation byte, low, and the guide it follows in the high 16 bits
1839
+ const strands = new Uint32Array(group.strand_count);
1840
+
1841
+ for (let s = 0; s < group.strand_count; s++) {
1842
+ strands[s] = (group.variation[s] | (group.strand_guide[s] << 16)) >>> 0;
1843
+ }
1844
+
1845
+ entry.offsets[g * 3 + 1] = at;
1846
+ this.#pool.write(at, strands, strands.length);
1847
+ at += strands.length;
1848
+
1849
+ const guides = new Uint32Array(group.guide_count * HAIR_GUIDE_RECORD_WORDS);
1850
+ const guides_f32 = new Float32Array(guides.buffer);
1851
+
1852
+ for (let i = 0; i < group.guide_count; i++) {
1853
+ guides[i * 4] = group.guide_strand[i];
1854
+ guides[i * 4 + 1] = group.guide_strand_count[i];
1855
+ guides_f32[i * 4 + 2] = group.guide_diameter[i];
1856
+ guides_f32[i * 4 + 3] = group.guide_length[i];
1857
+ }
1858
+
1859
+ entry.offsets[g * 3 + 2] = at;
1860
+
1861
+ if (guides.length > 0) {
1862
+ this.#pool.write(at, guides, guides.length);
1863
+ }
1864
+
1865
+ at += guides.length;
1866
+ }
1867
+
1868
+ this.#grooms.set(groom, entry);
1869
+
1870
+ return entry;
1871
+ }
1872
+
1873
+ /**
1874
+ * Let go of an entity's binding: its root frames, and its share of each group's bindings on the mesh.
1875
+ *
1876
+ * @param {GPUHairContext} context
1877
+ */
1878
+ #release_binding(context) {
1879
+ const groups = context.groom.groups;
1880
+
1881
+ for (let g = 0; g < context.bindings.length; g++) {
1882
+ const binding = context.bindings[g];
1883
+
1884
+ if (binding === null) {
1885
+ continue;
1886
+ }
1887
+
1888
+ this.#state_pool.free(context.roots[g]);
1889
+
1890
+ binding.references--;
1891
+
1892
+ if (binding.references === 0) {
1893
+ this.#pool.free(binding.run);
1894
+ this.#binding_cache.get(groups[g]).delete(context.mesh_geometry);
1895
+ }
1896
+ }
1897
+
1898
+ context.bindings.length = 0;
1899
+ context.roots.length = 0;
1900
+ context.mesh_node = HAIR_BINDING_NONE;
1901
+ context.mesh_geometry = null;
1902
+
1903
+ this.#bound--;
1904
+ this.#sums_dirty = true;
1905
+ }
1906
+
1907
+ /**
1908
+ * @param {GPUHairContext} context
1909
+ */
1910
+ #release_states(context) {
1911
+ for (let g = 0; g < context.states.length; g++) {
1912
+ const run = context.states[g];
1913
+
1914
+ if (run !== null) {
1915
+ this.#state_pool.free(run);
1916
+ }
1917
+ }
1918
+
1919
+ context.states.length = 0;
1920
+ context.epochs.length = 0;
1921
+ }
1922
+
1923
+ /**
1924
+ * Step the guides of every simulated instance through the time the scene's clock moved, once a frame: the first
1925
+ * view to draw hair records it, the begin pass, the colliders' poses and the solver (`shader_hair_simulate.js`).
1926
+ * A frame step is the time owed, at most `HAIR_FRAME_STEP_MAX`, in as few substeps as keep each within
1927
+ * `HAIR_SUBSTEP_MAX`; a frame owing none still lays new states at rest and follows the anchors.
1928
+ *
1929
+ * @param {import("../../../engine/graphics/render/frame_graph/FrameGraph.js").FrameGraph} graph
1930
+ * @param {Object} scene the view's scene context
1931
+ * @param {number} capacity rows of the instance table
1932
+ * @returns {{state: number, bounds: number}} the guide state and the rows' guide bounds, as the graph holds them
1933
+ */
1934
+ #graph_simulate(graph, scene, capacity) {
1935
+ const device = this.#graphics.device;
1936
+
1937
+ if (this.#bounds_buffer === null || this.#bounds_buffer.size < capacity * HAIR_GUIDE_BOUNDS_WORDS * 4) {
1938
+ this.#bounds_buffer?.destroy();
1939
+ this.#bounds_buffer = device.createBuffer({
1940
+ label: "Hair / guide bounds",
1941
+ size: Math.max(1, capacity) * HAIR_GUIDE_BOUNDS_WORDS * 4,
1942
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1943
+ });
1944
+ }
1945
+
1946
+ const state = graph_import_buffer(graph, this.#state_pool.buffer, "hair/guide state");
1947
+ const bounds = graph_import_buffer(graph, this.#bounds_buffer, "hair/guide bounds");
1948
+
1949
+ // once a frame, whether or not the clock moved: a frame of no time still follows the entities and lays new
1950
+ // states at rest
1951
+ if ((this.#simulated === 0 && this.#bound === 0) || scene.frame === this.#simulated_frame) {
1952
+ return { state, bounds };
1953
+ }
1954
+
1955
+ this.#write_sums();
1956
+
1957
+ if (this.#guide_total === 0 && this.#root_total === 0) {
1958
+ return { state, bounds };
1959
+ }
1960
+
1961
+ const seconds = Math.min(this.#pending_time, HAIR_FRAME_STEP_MAX);
1962
+ const substeps = seconds > 0 ? Math.ceil(seconds / HAIR_SUBSTEP_MAX) : 0;
1963
+
1964
+ this.#pending_time = 0;
1965
+ this.#simulated_frame = scene.frame;
1966
+
1967
+ const collider_count = this.#write_colliders();
1968
+ const collider_first = this.#row_limit * 2;
1969
+
1970
+ const hair_database = this.#database.buffer;
1971
+ const scene_database = scene.scene_database_buffer;
1972
+
1973
+ const begun = graph_compute_pass({
1974
+ graph,
1975
+ shader: shader_hair_simulate_begin,
1976
+ bindings: {
1977
+ hair_database,
1978
+ scene_database,
1979
+ hair_guide_state: state,
1980
+ hair_guide_bounds: bounds,
1981
+ },
1982
+ group_count: [this.table.dispatch_group_count(HAIR_LOD_WORKGROUP_SIZE), 1, 1],
1983
+ });
1984
+
1985
+ let current = { state: begun.hair_guide_state, bounds: begun.hair_guide_bounds };
1986
+
1987
+ // the bound strands' root frames, before the guides that ride them
1988
+ if (this.#root_total > 0) {
1989
+ const skinning = scene.skinning;
1990
+ const skinning_active = skinning.prev_position_offsets_buffer !== null && skinning.prev_positions_buffer !== null;
1991
+ const meshlets = scene.geometries.meshlets;
1992
+ const groups = Math.ceil(this.#root_total / COMPUTE_SHADER_DEFAULT_GROUP_SIZE);
1993
+
1994
+ const rooted = graph_compute_pass({
1995
+ graph,
1996
+ shader: shader_hair_roots,
1997
+ bindings: {
1998
+ hair_grooms: this.#pool.buffer,
1999
+ hair_database,
2000
+ scene_database,
2001
+ root_sums: this.#root_sums_buffer,
2002
+ meshlet_metadata: graph_import_buffer(graph, meshlets.buffer_metadata, "hair/meshlet metadata"),
2003
+ meshlet_data: graph_import_buffer(graph, meshlets.buffer_data, "hair/meshlet data"),
2004
+ // placeholders the shader leaves unread while no skin is registered
2005
+ prev_position_offsets: skinning.prev_position_offsets_buffer ?? scene_database,
2006
+ prev_positions: skinning.prev_positions_buffer ?? scene_database,
2007
+ hair_guide_state: current.state,
2008
+ hair_guide_bounds: current.bounds,
2009
+ settings: { skinning_active: skinning_active ? 1 : 0 },
2010
+ },
2011
+ group_count: [Math.min(groups, GROUPS_PER_DIMENSION), Math.ceil(groups / GROUPS_PER_DIMENSION), 1],
2012
+ });
2013
+
2014
+ current = { state: rooted.hair_guide_state, bounds: rooted.hair_guide_bounds };
2015
+ }
2016
+
2017
+ if (this.#guide_total === 0) {
2018
+ return current;
2019
+ }
2020
+
2021
+ let colliders = graph_import_buffer(graph, this.#collider_buffer, "hair/colliders");
2022
+
2023
+ if (collider_count > 0) {
2024
+ colliders = graph_compute_pass({
2025
+ graph,
2026
+ shader: shader_hair_collider_poses,
2027
+ bindings: {
2028
+ scene_database,
2029
+ hair_colliders: colliders,
2030
+ settings: { first: collider_first, count: collider_count },
2031
+ },
2032
+ group_count: [Math.ceil(collider_count / COMPUTE_SHADER_DEFAULT_GROUP_SIZE), 1, 1],
2033
+ }).hair_colliders;
2034
+ }
2035
+
2036
+ const wind = scene.wind;
2037
+ const bindings = {
2038
+ hair_grooms: this.#pool.buffer,
2039
+ hair_database,
2040
+ guide_sums: this.#sums_buffer,
2041
+ hair_colliders: colliders,
2042
+ hair_guide_state: current.state,
2043
+ hair_guide_bounds: current.bounds,
2044
+ settings: {
2045
+ gravity: this.gravity,
2046
+ step: substeps > 0 ? seconds / substeps : 0,
2047
+ substeps,
2048
+ },
2049
+ };
2050
+
2051
+ wind.bind(bindings);
2052
+
2053
+ const groups = Math.ceil(this.#guide_total / COMPUTE_SHADER_DEFAULT_GROUP_SIZE);
2054
+
2055
+ const simulated = graph_compute_pass({
2056
+ graph,
2057
+ shader: shader_hair_simulate.variant(ShaderSwitchSet.EMPTY.with(WIND_FIELD_UNIFORM, wind.is_uniform)),
2058
+ bindings,
2059
+ group_count: [Math.min(groups, GROUPS_PER_DIMENSION), Math.ceil(groups / GROUPS_PER_DIMENSION), 1],
2060
+ });
2061
+
2062
+ return { state: simulated.hair_guide_state, bounds: simulated.hair_guide_bounds };
2063
+ }
2064
+
2065
+ /**
2066
+ * The inclusive sums of the rows' guide counts, a simulated row's its groups', every other row's none, sent when
2067
+ * they changed.
2068
+ */
2069
+ #write_sums() {
2070
+ if (!this.#sums_dirty && this.#sums_buffer !== null) {
2071
+ return;
2072
+ }
2073
+
2074
+ this.#sums_dirty = false;
2075
+
2076
+ const rows = this.#row_limit;
2077
+ const counts = new Uint32Array(rows);
2078
+
2079
+ const contexts = this.#context_list;
2080
+
2081
+ for (let c = 0; c < contexts.length; c++) {
2082
+ const context = contexts[c];
2083
+
2084
+ if (context.dynamics === null) {
2085
+ continue;
2086
+ }
2087
+
2088
+ for (let g = 0; g < context.rows.length; g++) {
2089
+ if (context.states[g] !== null) {
2090
+ counts[context.rows[g]] = context.groom.groups[g].guide_count;
2091
+ }
2092
+ }
2093
+ }
2094
+
2095
+ // a collection: the count, padded to 16 bytes, then the elements
2096
+ const words = 4 + Math.max(4, Math.ceil(rows / 4) * 4);
2097
+
2098
+ if (this.#sums_words.length < words) {
2099
+ this.#sums_words = new Uint32Array(words);
2100
+ }
2101
+
2102
+ const sums = this.#sums_words;
2103
+
2104
+ sums.fill(0);
2105
+ sums[0] = rows;
2106
+
2107
+ let total = 0;
2108
+
2109
+ for (let r = 0; r < rows; r++) {
2110
+ total += counts[r];
2111
+ sums[4 + r] = total;
2112
+ }
2113
+
2114
+ this.#guide_total = total;
2115
+
2116
+ const device = this.#graphics.device;
2117
+
2118
+ if (this.#sums_buffer === null || this.#sums_buffer.size < sums.byteLength) {
2119
+ this.#sums_buffer?.destroy();
2120
+ this.#sums_buffer = device.createBuffer({
2121
+ label: "Hair / guide sums",
2122
+ size: sums.byteLength,
2123
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2124
+ });
2125
+ }
2126
+
2127
+ device.queue.writeBuffer(this.#sums_buffer, 0, sums.buffer, 0, words * 4);
2128
+
2129
+ // the bound rows' strands, for the root frames
2130
+ counts.fill(0);
2131
+
2132
+ for (let c = 0; c < contexts.length; c++) {
2133
+ const context = contexts[c];
2134
+
2135
+ if (context.mesh_node === HAIR_BINDING_NONE) {
2136
+ continue;
2137
+ }
2138
+
2139
+ for (let g = 0; g < context.rows.length; g++) {
2140
+ if (context.bindings[g] !== null) {
2141
+ counts[context.rows[g]] = context.groom.groups[g].strand_count;
2142
+ }
2143
+ }
2144
+ }
2145
+
2146
+ if (this.#root_sums_words.length < words) {
2147
+ this.#root_sums_words = new Uint32Array(words);
2148
+ }
2149
+
2150
+ const root_sums = this.#root_sums_words;
2151
+
2152
+ root_sums.fill(0);
2153
+ root_sums[0] = rows;
2154
+
2155
+ let root_total = 0;
2156
+
2157
+ for (let r = 0; r < rows; r++) {
2158
+ root_total += counts[r];
2159
+ root_sums[4 + r] = root_total;
2160
+ }
2161
+
2162
+ this.#root_total = root_total;
2163
+
2164
+ if (this.#root_sums_buffer === null || this.#root_sums_buffer.size < root_sums.byteLength) {
2165
+ this.#root_sums_buffer?.destroy();
2166
+ this.#root_sums_buffer = device.createBuffer({
2167
+ label: "Hair / root sums",
2168
+ size: root_sums.byteLength,
2169
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2170
+ });
2171
+ }
2172
+
2173
+ device.queue.writeBuffer(this.#root_sums_buffer, 0, root_sums.buffer, 0, words * 4);
2174
+ }
2175
+
2176
+ /**
2177
+ * The frame's collider list: a pair a row, the row's first collider word and its count, then each simulated
2178
+ * entity's colliders, which its rows share.
2179
+ *
2180
+ * @returns {number} colliders in the list
2181
+ */
2182
+ #write_colliders() {
2183
+ const header = this.#row_limit * 2;
2184
+
2185
+ const contexts = this.#context_list;
2186
+
2187
+ let count = 0;
2188
+
2189
+ for (let c = 0; c < contexts.length; c++) {
2190
+ if (contexts[c].dynamics !== null) {
2191
+ count += contexts[c].collider_count;
2192
+ }
2193
+ }
2194
+
2195
+ const words = header + Math.max(1, count) * HAIR_COLLIDER_WORDS;
2196
+
2197
+ if (this.#collider_words.length < words) {
2198
+ this.#collider_words = new Uint32Array(words);
2199
+ }
2200
+
2201
+ const list = this.#collider_words;
2202
+
2203
+ list.fill(0, 0, header);
2204
+
2205
+ let at = header;
2206
+
2207
+ for (let c = 0; c < contexts.length; c++) {
2208
+ const context = contexts[c];
2209
+
2210
+ if (context.dynamics === null) {
2211
+ continue;
2212
+ }
2213
+
2214
+ const n = context.collider_count;
2215
+
2216
+ for (let g = 0; g < context.rows.length; g++) {
2217
+ list[context.rows[g] * 2] = at;
2218
+ list[context.rows[g] * 2 + 1] = n;
2219
+ }
2220
+
2221
+ const colliders = context.colliders;
2222
+
2223
+ for (let i = 0; i < n * HAIR_COLLIDER_WORDS; i++) {
2224
+ list[at + i] = colliders[i];
2225
+ }
2226
+
2227
+ at += n * HAIR_COLLIDER_WORDS;
2228
+ }
2229
+
2230
+ const device = this.#graphics.device;
2231
+
2232
+ if (this.#collider_buffer === null || this.#collider_buffer.size < words * 4) {
2233
+ this.#collider_buffer?.destroy();
2234
+ this.#collider_buffer = device.createBuffer({
2235
+ label: "Hair / colliders",
2236
+ size: words * 4,
2237
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2238
+ });
2239
+ }
2240
+
2241
+ device.queue.writeBuffer(this.#collider_buffer, 0, list.buffer, 0, words * 4);
2242
+
2243
+ return count;
2244
+ }
2245
+
2246
+ /**
2247
+ * @param {Groom} groom
2248
+ */
2249
+ #release_groom(groom) {
2250
+ const entry = this.#grooms.get(groom);
2251
+
2252
+ entry.references--;
2253
+
2254
+ if (entry.references > 0) {
2255
+ return;
2256
+ }
2257
+
2258
+ this.#pool.free(entry.run);
2259
+ this.#grooms.delete(groom);
2260
+ }
2261
+ }
2262
+
2263
+ assert.lessThan(HAIR_ROW_BITS, 32, 'a tag holds a row and a byte');