@woosh/meep-engine 3.76.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 (233) 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/loaders/GroomAssetLoader.d.ts +1 -1
  7. package/src/engine/asset/loaders/GroomAssetLoader.d.ts.map +1 -1
  8. package/src/engine/asset/loaders/GroomAssetLoader.js +16 -2
  9. package/src/engine/graphics/ecs/hair/Hair.d.ts +7 -0
  10. package/src/engine/graphics/ecs/hair/Hair.d.ts.map +1 -1
  11. package/src/engine/graphics/ecs/hair/Hair.js +9 -0
  12. package/src/engine/graphics/ecs/hair/HairDynamics.d.ts +63 -0
  13. package/src/engine/graphics/ecs/hair/HairDynamics.d.ts.map +1 -0
  14. package/src/engine/graphics/ecs/hair/HairDynamics.js +82 -0
  15. package/src/engine/graphics/hair/Groom.d.ts +45 -0
  16. package/src/engine/graphics/hair/Groom.d.ts.map +1 -1
  17. package/src/engine/graphics/hair/Groom.js +56 -0
  18. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts +27 -1
  19. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts.map +1 -1
  20. package/src/engine/graphics/hair/GroomSerializationAdapter.js +105 -32
  21. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.d.ts +8 -0
  22. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.d.ts.map +1 -0
  23. package/src/engine/graphics/hair/GroomSerializationUpgrader_0_1.js +31 -0
  24. package/src/engine/graphics/hair/groom_bind_to_mesh.d.ts +85 -0
  25. package/src/engine/graphics/hair/groom_bind_to_mesh.d.ts.map +1 -0
  26. package/src/engine/graphics/hair/groom_bind_to_mesh.js +216 -0
  27. package/src/engine/graphics/hair/groom_build_group.d.ts +7 -2
  28. package/src/engine/graphics/hair/groom_build_group.d.ts.map +1 -1
  29. package/src/engine/graphics/hair/groom_build_group.js +11 -2
  30. package/src/engine/graphics/hair/groom_build_guides.d.ts +43 -0
  31. package/src/engine/graphics/hair/groom_build_guides.d.ts.map +1 -0
  32. package/src/engine/graphics/hair/groom_build_guides.js +267 -0
  33. package/src/engine/graphics/hair/groom_root_spacing.d.ts +6 -4
  34. package/src/engine/graphics/hair/groom_root_spacing.d.ts.map +1 -1
  35. package/src/engine/graphics/hair/groom_root_spacing.js +199 -66
  36. package/src/engine/graphics3/HairColliderSystem.d.ts +59 -0
  37. package/src/engine/graphics3/HairColliderSystem.d.ts.map +1 -0
  38. package/src/engine/graphics3/HairColliderSystem.js +270 -0
  39. package/src/engine/graphics3/HairSystem.d.ts +21 -4
  40. package/src/engine/graphics3/HairSystem.d.ts.map +1 -1
  41. package/src/engine/graphics3/HairSystem.js +270 -4
  42. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  43. package/src/shade/assets/textures/hair_scattering.bin +0 -0
  44. package/src/shade/playground/hair/main.js +27 -4
  45. package/src/shade/playground/hair_skinned/index.html +192 -0
  46. package/src/shade/playground/hair_skinned/main.d.ts +7 -0
  47. package/src/shade/playground/hair_skinned/main.d.ts.map +1 -0
  48. package/src/shade/playground/hair_skinned/main.js +481 -0
  49. package/src/shade/playground/hair_skinned/skinned_hair.d.ts +49 -0
  50. package/src/shade/playground/hair_skinned/skinned_hair.d.ts.map +1 -0
  51. package/src/shade/playground/hair_skinned/skinned_hair.js +362 -0
  52. package/src/shade/playground/sparks/README.md +36 -0
  53. package/src/shade/playground/sparks/index.html +145 -0
  54. package/src/shade/playground/sparks/main.d.ts +7 -0
  55. package/src/shade/playground/sparks/main.d.ts.map +1 -0
  56. package/src/shade/playground/sparks/main.js +312 -0
  57. package/src/shade/playground/sparks/sparks_effect.d.ts +72 -0
  58. package/src/shade/playground/sparks/sparks_effect.d.ts.map +1 -0
  59. package/src/shade/playground/sparks/sparks_effect.js +180 -0
  60. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  61. package/src/shade/renderer/Renderer.js +18 -3
  62. package/src/shade/renderer/acoustics/GPUAcousticLattice.d.ts +1 -1
  63. package/src/shade/renderer/acoustics/GPUAcousticProbes.d.ts +1 -1
  64. package/src/shade/renderer/acoustics/GPUAcoustics.d.ts +1 -1
  65. package/src/shade/renderer/animation/GPUAnimationManager.d.ts +3 -2
  66. package/src/shade/renderer/animation/GPUAnimationManager.d.ts.map +1 -1
  67. package/src/shade/renderer/animation/GPUAnimationManager.js +74 -3
  68. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.d.ts +13 -3
  69. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.d.ts.map +1 -1
  70. package/src/shade/renderer/animation/skinning/GPUMeshSkinningContext.js +16 -5
  71. package/src/shade/renderer/animation/skinning/shader_meshlet_apply_skinning.js +8 -8
  72. package/src/shade/renderer/deferred/chunk_shade_direct_lights.js +2 -2
  73. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts +4 -2
  74. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts.map +1 -1
  75. package/src/shade/renderer/deferred/chunk_shade_directional_lights.js +7 -3
  76. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.js +1 -1
  77. package/src/shade/renderer/extension/ShadowAtlas.d.ts +75 -0
  78. package/src/shade/renderer/extension/ShadowAtlas.d.ts.map +1 -0
  79. package/src/shade/renderer/extension/ShadowAtlas.js +133 -0
  80. package/src/shade/renderer/fluid/GPUFluidCascade.d.ts +3 -3
  81. package/src/shade/renderer/fluid/GPUFluidCascadeStack.d.ts +1 -1
  82. package/src/shade/renderer/fluid/forcing/GPUFluidForcing.d.ts +7 -7
  83. package/src/shade/renderer/fluid/pressure/GPUFluidPressureSolve.d.ts +1 -1
  84. package/src/shade/renderer/fluid/query/GPUFluidQueries.d.ts +1 -1
  85. package/src/shade/renderer/global_illumination/probe_clipmap/GPUProbeClipmapGI.d.ts +1 -1
  86. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts +37 -7
  87. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts.map +1 -1
  88. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.js +132 -8
  89. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts +3 -1
  90. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts.map +1 -1
  91. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.js +6 -2
  92. package/src/shade/renderer/global_illumination/sharc/sharc_constants.d.ts +0 -18
  93. package/src/shade/renderer/global_illumination/sharc/sharc_constants.d.ts.map +1 -1
  94. package/src/shade/renderer/global_illumination/sharc/sharc_constants.js +1 -21
  95. package/src/shade/renderer/hair/GPUHairSystem.d.ts +121 -2
  96. package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -1
  97. package/src/shade/renderer/hair/GPUHairSystem.js +1187 -56
  98. package/src/shade/renderer/hair/HAIR.d.ts +22 -3
  99. package/src/shade/renderer/hair/HAIR.d.ts.map +1 -1
  100. package/src/shade/renderer/hair/HAIR.js +25 -4
  101. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts.map +1 -1
  102. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.js +33 -0
  103. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts +4 -0
  104. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts.map +1 -1
  105. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.js +24 -2
  106. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts +139 -0
  107. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts.map +1 -0
  108. package/src/shade/renderer/hair/HAIR_SIMULATION.js +160 -0
  109. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts +9 -2
  110. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts.map +1 -1
  111. package/src/shade/renderer/hair/chunk_hair_dual_scattering.js +40 -13
  112. package/src/shade/renderer/hair/chunk_hair_guide.d.ts +59 -0
  113. package/src/shade/renderer/hair/chunk_hair_guide.d.ts.map +1 -0
  114. package/src/shade/renderer/hair/chunk_hair_guide.js +179 -0
  115. package/src/shade/renderer/hair/chunk_hair_volume.d.ts +4 -3
  116. package/src/shade/renderer/hair/chunk_hair_volume.d.ts.map +1 -1
  117. package/src/shade/renderer/hair/chunk_hair_volume.js +21 -12
  118. package/src/shade/renderer/hair/hair_lighting.d.ts.map +1 -1
  119. package/src/shade/renderer/hair/hair_lighting.js +16 -7
  120. package/src/shade/renderer/hair/hair_scattering_lut.js +13 -13
  121. package/src/shade/renderer/hair/hair_shadow_draw.d.ts +32 -0
  122. package/src/shade/renderer/hair/hair_shadow_draw.d.ts.map +1 -0
  123. package/src/shade/renderer/hair/hair_shadow_draw.js +123 -0
  124. package/src/shade/renderer/hair/shader_hair_classes.d.ts +2 -1
  125. package/src/shade/renderer/hair/shader_hair_classes.d.ts.map +1 -1
  126. package/src/shade/renderer/hair/shader_hair_classes.js +6 -2
  127. package/src/shade/renderer/hair/shader_hair_cull.d.ts +8 -2
  128. package/src/shade/renderer/hair/shader_hair_cull.d.ts.map +1 -1
  129. package/src/shade/renderer/hair/shader_hair_cull.js +74 -17
  130. package/src/shade/renderer/hair/shader_hair_lod.d.ts +9 -3
  131. package/src/shade/renderer/hair/shader_hair_lod.d.ts.map +1 -1
  132. package/src/shade/renderer/hair/shader_hair_lod.js +125 -31
  133. package/src/shade/renderer/hair/shader_hair_roots.d.ts +36 -0
  134. package/src/shade/renderer/hair/shader_hair_roots.d.ts.map +1 -0
  135. package/src/shade/renderer/hair/shader_hair_roots.js +261 -0
  136. package/src/shade/renderer/hair/shader_hair_shadow_list.d.ts +44 -0
  137. package/src/shade/renderer/hair/shader_hair_shadow_list.d.ts.map +1 -0
  138. package/src/shade/renderer/hair/shader_hair_shadow_list.js +240 -0
  139. package/src/shade/renderer/hair/shader_hair_simulate.d.ts +55 -0
  140. package/src/shade/renderer/hair/shader_hair_simulate.d.ts.map +1 -0
  141. package/src/shade/renderer/hair/shader_hair_simulate.js +563 -0
  142. package/src/shade/renderer/hair/shader_hair_volume.d.ts +3 -3
  143. package/src/shade/renderer/hair/shader_hair_volume.d.ts.map +1 -1
  144. package/src/shade/renderer/hair/shader_hair_volume.js +4 -17
  145. package/src/shade/renderer/loader/gltf/tiny-gltf.d.ts +1 -1
  146. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts +8 -3
  147. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts.map +1 -1
  148. package/src/shade/renderer/loader/usd/usd_groom_extension.js +58 -5
  149. package/src/shade/renderer/particles/GPUParticleSystem.d.ts +8 -0
  150. package/src/shade/renderer/particles/GPUParticleSystem.d.ts.map +1 -1
  151. package/src/shade/renderer/particles/GPUParticleSystem.js +28 -0
  152. package/src/shade/renderer/particles/graph/ParticleNodeRegistry.d.ts.map +1 -1
  153. package/src/shade/renderer/particles/graph/ParticleNodeRegistry.js +22 -0
  154. package/src/shade/renderer/particles/graph/groups/particle_builtin_groups.d.ts.map +1 -1
  155. package/src/shade/renderer/particles/graph/groups/particle_builtin_groups.js +4 -0
  156. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.d.ts +32 -0
  157. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.d.ts.map +1 -0
  158. package/src/shade/renderer/particles/graph/groups/particle_groups_scene.js +80 -0
  159. package/src/shade/renderer/particles/graph/particle_node_parameters.d.ts.map +1 -1
  160. package/src/shade/renderer/particles/graph/particle_node_parameters.js +1 -0
  161. package/src/shade/renderer/particles/graph/particle_node_presentation.d.ts.map +1 -1
  162. package/src/shade/renderer/particles/graph/particle_node_presentation.js +12 -0
  163. package/src/shade/renderer/particles/graph_particles.d.ts +5 -1
  164. package/src/shade/renderer/particles/graph_particles.d.ts.map +1 -1
  165. package/src/shade/renderer/particles/graph_particles.js +18 -3
  166. package/src/shade/renderer/particles/isa/InstructionStream.d.ts +25 -0
  167. package/src/shade/renderer/particles/isa/InstructionStream.d.ts.map +1 -1
  168. package/src/shade/renderer/particles/isa/InstructionStream.js +25 -0
  169. package/src/shade/renderer/particles/isa/ParticleVMISA.d.ts +9 -0
  170. package/src/shade/renderer/particles/isa/ParticleVMISA.d.ts.map +1 -1
  171. package/src/shade/renderer/particles/isa/ParticleVMISA.js +20 -0
  172. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.d.ts +9 -0
  173. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.d.ts.map +1 -0
  174. package/src/shade/renderer/particles/isa/particle_program_reads_scene_sdf.js +29 -0
  175. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.d.ts +2 -1
  176. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.d.ts.map +1 -1
  177. package/src/shade/renderer/particles/optimizer/particle_program_equivalence.js +27 -5
  178. package/src/shade/renderer/particles/optimizer/particle_program_fuzz.js +1 -1
  179. package/src/shade/renderer/particles/optimizer/particle_vm_semantics.d.ts.map +1 -1
  180. package/src/shade/renderer/particles/optimizer/particle_vm_semantics.js +5 -0
  181. package/src/shade/renderer/particles/runtime/EmitterRegistry.d.ts +7 -0
  182. package/src/shade/renderer/particles/runtime/EmitterRegistry.d.ts.map +1 -1
  183. package/src/shade/renderer/particles/runtime/EmitterRegistry.js +10 -0
  184. package/src/shade/renderer/particles/runtime/ParticleEmitter.d.ts +5 -0
  185. package/src/shade/renderer/particles/runtime/ParticleEmitter.d.ts.map +1 -1
  186. package/src/shade/renderer/particles/runtime/ParticleEmitter.js +5 -0
  187. package/src/shade/renderer/particles/runtime/ProgramHeap.d.ts +8 -0
  188. package/src/shade/renderer/particles/runtime/ProgramHeap.d.ts.map +1 -1
  189. package/src/shade/renderer/particles/runtime/ProgramHeap.js +25 -0
  190. package/src/shade/renderer/particles/shaders/shader_particle_emit.d.ts +1 -1
  191. package/src/shade/renderer/particles/shaders/shader_particle_emit.d.ts.map +1 -1
  192. package/src/shade/renderer/particles/shaders/shader_particle_emit.js +9 -3
  193. package/src/shade/renderer/particles/shaders/shader_particle_emit_wide.d.ts.map +1 -1
  194. package/src/shade/renderer/particles/shaders/shader_particle_emit_wide.js +4 -2
  195. package/src/shade/renderer/particles/shaders/shader_particle_simulate.d.ts +2 -2
  196. package/src/shade/renderer/particles/shaders/shader_particle_simulate.d.ts.map +1 -1
  197. package/src/shade/renderer/particles/shaders/shader_particle_simulate.js +10 -4
  198. package/src/shade/renderer/particles/shaders/shader_particle_simulate_wide.d.ts.map +1 -1
  199. package/src/shade/renderer/particles/shaders/shader_particle_simulate_wide.js +4 -2
  200. package/src/shade/renderer/particles/vm/ParticleVMReference.d.ts +14 -1
  201. package/src/shade/renderer/particles/vm/ParticleVMReference.d.ts.map +1 -1
  202. package/src/shade/renderer/particles/vm/ParticleVMReference.js +34 -1
  203. package/src/shade/renderer/particles/vm/chunk_particle_vm.d.ts.map +1 -1
  204. package/src/shade/renderer/particles/vm/chunk_particle_vm.js +10 -0
  205. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.d.ts +39 -0
  206. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.d.ts.map +1 -0
  207. package/src/shade/renderer/particles/vm/chunk_particle_vm_scene_sdf.js +90 -0
  208. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts +4 -1
  209. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts.map +1 -1
  210. package/src/shade/renderer/particles/warmup/graph_particle_warmup.js +14 -2
  211. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.d.ts +1 -1
  212. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.d.ts.map +1 -1
  213. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance.js +9 -3
  214. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance_wide.d.ts.map +1 -1
  215. package/src/shade/renderer/particles/warmup/shader_particle_warmup_advance_wide.js +4 -2
  216. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  217. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.d.ts +7 -4
  218. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.d.ts.map +1 -1
  219. package/src/shade/renderer/postprocess/velocity/graph_add_per_object_velocity.js +26 -16
  220. package/src/shade/renderer/scene/GPUSceneContext.d.ts +7 -0
  221. package/src/shade/renderer/scene/GPUSceneContext.d.ts.map +1 -1
  222. package/src/shade/renderer/scene/GPUSceneContext.js +18 -0
  223. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.d.ts +20 -0
  224. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.d.ts.map +1 -0
  225. package/src/shade/renderer/shader/chunk/sdf/chunk_shape_record_signed_distance_gradient.js +134 -0
  226. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts +15 -0
  227. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts.map +1 -1
  228. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.js +69 -0
  229. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.d.ts +10 -0
  230. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.d.ts.map +1 -0
  231. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_directional_atlas_casters.js +22 -0
  232. package/src/shade/renderer/water/query/GPUWaterQueries.d.ts +1 -1
  233. package/src/shade/renderer/wind/GPUSceneWind.d.ts +1 -1
@@ -1,5 +1,6 @@
1
1
  import { assert } from "../../../core/assert.js";
2
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";
3
4
  import { GROOM_POINT_WORDS } from "../../../engine/graphics/hair/Groom.js";
4
5
  import { TextureViewDescriptor } from "../../descriptor/texture/TextureViewDescriptor.js";
5
6
  import { BufferResourceDescriptor } from "../../device/graph/BufferResourceDescriptor.js";
@@ -10,6 +11,7 @@ import { GPURunPool } from "../buffer/GPURunPool.js";
10
11
  import { GPUDatabase } from "../buffer/table/GPUDatabase.js";
11
12
  import { DEFAULT_DEPTH_TEXTURE_VIEW } from "../depth/DEFAULT_DEPTH_TEXTURE_VIEW.js";
12
13
  import { GBufferTextures } from "../extension/GBufferTextures.js";
14
+ import { ShadowAtlas } from "../extension/ShadowAtlas.js";
13
15
  import { SceneColor } from "../extension/SceneColor.js";
14
16
  import { ViewLighting } from "../extension/ViewLighting.js";
15
17
  import { ViewTextures } from "../extension/ViewTextures.js";
@@ -25,25 +27,58 @@ import { shader_prefix_sum_to_command } from "../rasterize/expand/mesh/shader_pr
25
27
  import { graph_compute_indirect_pass } from "../shader/graph/compute/graph_compute_indirect_pass.js";
26
28
  import { graph_compute_pass } from "../shader/graph/compute/graph_compute_pass.js";
27
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";
28
34
  import { SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR } from "../shadow/map/SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR.js";
29
35
  import { STATIC_GRAPHICS_ENGINE_ASSETS } from "../STATIC_GRAPHICS_ENGINE_ASSETS.js";
30
36
  import { SamplerDescriptor } from "../../descriptor/texture/SamplerDescriptor.js";
31
37
  import {
32
38
  HAIR_CLASS_COUNT,
33
39
  HAIR_COUNTER_POINTS,
40
+ HAIR_COUNTER_SHADOW_STRANDS,
34
41
  HAIR_COUNTER_VOXELS,
35
42
  HAIR_COUNTER_WORDS,
36
43
  HAIR_DRAW_ARGS_WORDS,
37
44
  HAIR_ID_FORMAT,
38
45
  HAIR_ID_NONE,
46
+ HAIR_LIST_SHADOW_WORDS,
39
47
  HAIR_LOD_WORDS,
40
48
  HAIR_POINT_CAPACITY_LIMIT,
41
- HAIR_ROW_BITS
49
+ HAIR_ROW_BITS,
50
+ HAIR_VOLUME_MIPS
42
51
  } from "./HAIR.js";
43
52
  import { HAIR_DATABASE_INITIAL_SIZE, HAIR_DATABASE_SPEC, HAIR_INSTANCE_TABLE } from "./HAIR_DATABASE_SPEC.js";
44
- import { HAIR_VIEW_STRUCT } from "./HAIR_FRAME_STRUCTS.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";
45
74
  import { HairFrame } from "./HairFrame.js";
46
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";
47
82
  import { HAIR_LIGHTING_RESOURCES, hair_lighting_pipeline } from "./hair_lighting.js";
48
83
  import { HAIR_MOTION_PIPELINE, HAIR_STAMP_PIPELINE, HAIR_STAMP_RESOURCES } from "./hair_stamp.js";
49
84
  import { shader_hair_draw_args, shader_hair_regions } from "./shader_hair_classes.js";
@@ -81,6 +116,18 @@ const INITIAL_LIST_CAPACITY = 2 ** 16;
81
116
  */
82
117
  const INITIAL_VOXEL_CAPACITY = 2 ** 20;
83
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
+
84
131
  /**
85
132
  * The narrowest a strand is drawn, pixels. One pixel is the narrowest strip that rasterizes without gaps at every
86
133
  * jitter offset; below it, whether a strand is seen at a pixel is the jitter's to decide, and that flickers.
@@ -147,14 +194,63 @@ export class HairMaterial {
147
194
  }
148
195
 
149
196
  /**
150
- * A groom on the device: its run in the pool, where each group's points and strands start in it, and how many
151
- * instances wear it.
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.
152
248
  */
153
249
  class GroomEntry {
154
250
  /** @type {import("../../../core/binary/allocator/OffsetAllocator.js").Allocation} */
155
251
  run = null;
156
252
 
157
- /** @type {Uint32Array} a group's first point word, then its first strand word, a pair a group */
253
+ /** @type {Uint32Array} a group's first point word, its first strand word and its first guide word, three a group */
158
254
  offsets = new Uint32Array(0);
159
255
 
160
256
  /** @type {number} */
@@ -176,6 +272,36 @@ export class GPUHairContext {
176
272
 
177
273
  /** @type {HairMaterial} what was last staged */
178
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 = [];
179
305
  }
180
306
 
181
307
  /**
@@ -189,8 +315,10 @@ export class GPUHairContext {
189
315
  * Each frame, at the phase where the G-buffer is written into, {@link graph_draw}: the level of detail of every
190
316
  * instance, the cull and deformation of the strands it keeps with their density volume (cleared, laid, its coarser
191
317
  * levels built), their id raster under the scene's depth, and the
192
- * G-buffer stamp and velocity under it (`HAIR.js` says what the frame's buffers hold). Then, once the frame is lit,
193
- * {@link graph_light}: the strands' own light, written over the pixels they cover.
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.
194
322
  *
195
323
  * The frame's buffers grow from what the GPU asked for, read back: a frame asking for more than they hold draws what
196
324
  * fits, and the next frames hold what was asked.
@@ -209,9 +337,18 @@ export class GPUHairSystem {
209
337
  enquiry.compute.push(shader_hair_volume_command.pipeline);
210
338
  enquiry.compute.push(shader_hair_volume_clear.pipeline);
211
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);
212
348
  enquiry.compute.push(shader_prefix_sum_to_command.pipeline);
213
349
  enquire_prefix_scan_csdldf_pipelines(enquiry);
214
350
  enquiry.render.push(HAIR_ID_PIPELINE);
351
+ enquiry.render.push(HAIR_SHADOW_PIPELINE);
215
352
  enquiry.render.push(HAIR_STAMP_PIPELINE);
216
353
  enquiry.render.push(HAIR_MOTION_PIPELINE);
217
354
  }
@@ -225,6 +362,25 @@ export class GPUHairSystem {
225
362
  */
226
363
  pixel_floor = HAIR_PIXEL_FLOOR;
227
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
+
228
384
  /** @type {GraphicsContext} */
229
385
  #graphics;
230
386
 
@@ -240,6 +396,9 @@ export class GPUHairSystem {
240
396
  /** @type {Set<GPUHairContext>} */
241
397
  #contexts = new Set();
242
398
 
399
+ /** @type {GPUHairContext[]} {@link #contexts}, walked every frame without an iterator */
400
+ #context_list = [];
401
+
243
402
  /** @type {number} rows the instance passes draw: one past the highest in use */
244
403
  #row_limit = 0;
245
404
 
@@ -252,6 +411,68 @@ export class GPUHairSystem {
252
411
  #voxel_capacity = INITIAL_VOXEL_CAPACITY;
253
412
  #voxels_asked = 0;
254
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
+
255
476
  /**
256
477
  * The record a row is written from.
257
478
  * @type {Object}
@@ -274,12 +495,48 @@ export class GPUHairSystem {
274
495
  dye: new Float32Array(3),
275
496
  roughness: 0,
276
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,
277
515
  };
278
516
 
279
517
  #view_words = new ArrayBuffer(HAIR_VIEW_STRUCT.size);
280
518
  #view_f32 = new Float32Array(this.#view_words);
281
519
  #view_u32 = new Uint32Array(this.#view_words);
282
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
+
283
540
  #inverse_view_projection = new Float32Array(16);
284
541
  #view_projection_previous = new Float32Array(16);
285
542
 
@@ -299,6 +556,7 @@ export class GPUHairSystem {
299
556
  initial_data_size_bytes: HAIR_DATABASE_INITIAL_SIZE,
300
557
  });
301
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);
302
560
  }
303
561
 
304
562
  /** @returns {GPUDatabase} */
@@ -361,6 +619,219 @@ export class GPUHairSystem {
361
619
  return this.#row_limit;
362
620
  }
363
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
+
364
835
  /**
365
836
  * Register an entity's hair.
366
837
  *
@@ -395,6 +866,7 @@ export class GPUHairSystem {
395
866
  }
396
867
 
397
868
  this.#contexts.add(context);
869
+ this.#context_list.push(context);
398
870
 
399
871
  return context;
400
872
  }
@@ -443,10 +915,28 @@ export class GPUHairSystem {
443
915
  table.remove(context.rows[i]);
444
916
  }
445
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
+
446
930
  this.#release_groom(context.groom);
447
931
 
448
932
  this.#contexts.delete(context);
449
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
+
450
940
  context.rows.length = 0;
451
941
  context.groom = null;
452
942
 
@@ -515,8 +1005,19 @@ export class GPUHairSystem {
515
1005
  const scene_database = scene.scene_database_buffer;
516
1006
  const camera_buffer = camera.gpu_buffer;
517
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
+
518
1017
  graph.push_scope("hair");
519
1018
 
1019
+ const guides = this.#graph_simulate(graph, scene, capacity);
1020
+
520
1021
  const count_bytes = collection_size(capacity);
521
1022
  const lod_bytes = capacity * HAIR_LOD_WORDS * 4;
522
1023
  const counter_bytes = HAIR_COUNTER_WORDS * 4;
@@ -528,12 +1029,15 @@ export class GPUHairSystem {
528
1029
  camera: camera_buffer,
529
1030
  hair_database,
530
1031
  scene_database,
1032
+ hair_guide_bounds: guides.bounds,
531
1033
  // cleared whole: the pass writes only the rows it visits
532
1034
  hair_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
533
- hair_volume_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),
534
1037
  hair_lod: BufferResourceDescriptor.from(lod_bytes, GPUBufferUsage.STORAGE, 0, lod_bytes),
535
1038
  // read back for what was asked
536
1039
  hair_counters: BufferResourceDescriptor.from(counter_bytes, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, 0, counter_bytes),
1040
+ hair_shadow_views: shadow_views,
537
1041
  settings: {
538
1042
  resolution: [width, height],
539
1043
  pixel_floor: this.pixel_floor,
@@ -541,6 +1045,7 @@ export class GPUHairSystem {
541
1045
  capacity,
542
1046
  point_capacity,
543
1047
  voxel_capacity,
1048
+ shadow_view_count,
544
1049
  },
545
1050
  },
546
1051
  group_count: [group_count, 1, 1],
@@ -603,13 +1108,16 @@ export class GPUHairSystem {
603
1108
  hair_lod: lod.hair_lod,
604
1109
  hair_counters: regions.hair_counters,
605
1110
  hair_points: BufferResourceDescriptor.from(point_capacity * 2 * 16, GPUBufferUsage.STORAGE),
606
- hair_list: BufferResourceDescriptor.from(list_capacity * 4, 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),
607
1113
  hair_volume: cleared.hair_volume,
1114
+ hair_guide_state: guides.state,
608
1115
  settings: {
609
1116
  resolution: [width, height],
610
1117
  pixel_floor: this.pixel_floor,
611
1118
  point_capacity,
612
1119
  list_capacity,
1120
+ casts_shadows: shadow_view_count > 0 ? 1 : 0,
613
1121
  },
614
1122
  },
615
1123
  });
@@ -627,50 +1135,43 @@ export class GPUHairSystem {
627
1135
  },
628
1136
  });
629
1137
 
630
- // the volumes' coarser levels, a thread a coarse voxel
631
- const volume_sums = graph_prefix_scan_csdldf({
632
- graph,
633
- input: lod.hair_volume_counts,
634
- output: graph_create_buffer(graph, "hair/volume sums", prefix_scan_csdldf_output_descriptor(capacity)),
635
- element_max_count: capacity,
636
- });
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;
637
1140
 
638
- const mip_command = graph_compute_pass({
639
- graph,
640
- shader: shader_prefix_sum_to_command,
641
- bindings: {
642
- prefix_sums: volume_sums,
643
- command: graph_create_buffer(graph, "hair/mip command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
644
- },
645
- }).command;
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;
646
1143
 
647
- // level 1 from the voxels, then level 2 from level 1
648
- const level_1 = graph_compute_indirect_pass({
649
- graph,
650
- shader: shader_hair_volume_mips,
651
- command: mip_command,
652
- bindings: {
653
- hair_lod: lod.hair_lod,
654
- prefix_sums: volume_sums,
655
- hair_volume: cull.hair_volume,
656
- settings: { level: 1 },
657
- },
658
- }).hair_volume;
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
+ });
659
1150
 
660
- const volume = graph_compute_indirect_pass({
661
- graph,
662
- shader: shader_hair_volume_mips,
663
- command: mip_command,
664
- bindings: {
665
- hair_lod: lod.hair_lod,
666
- prefix_sums: volume_sums,
667
- hair_volume: level_1,
668
- settings: { level: 2 },
669
- },
670
- }).hair_volume;
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
+ }
671
1172
 
672
1173
  // what was asked for: a frame's buffers too small for it are made larger from the next frame
673
- graph_read_buffer({ graph, buffer: cull.hair_counters, offset: 0, size: (HAIR_COUNTER_VOXELS + 1) * 4 })
1174
+ graph_read_buffer({ graph, buffer: cull.hair_counters, offset: 0, size: (HAIR_COUNTER_SHADOW_STRANDS + HAIR_CLASS_COUNT) * 4 })
674
1175
  .then(reference => {
675
1176
  try {
676
1177
  const words = new Uint32Array(reference.getValue());
@@ -678,7 +1179,7 @@ export class GPUHairSystem {
678
1179
  let strands = 0;
679
1180
 
680
1181
  for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
681
- strands += words[c];
1182
+ strands += words[c] + words[HAIR_COUNTER_SHADOW_STRANDS + c];
682
1183
  }
683
1184
 
684
1185
  this.#strands_asked = Math.max(this.#strands_asked, strands);
@@ -751,6 +1252,10 @@ export class GPUHairSystem {
751
1252
 
752
1253
  view_textures.depth = id_data.depth;
753
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
+
754
1259
  view_velocity_matrices(view, this.#inverse_view_projection, this.#view_projection_previous);
755
1260
 
756
1261
  const inverse_view_projection = this.#inverse_view_projection.slice();
@@ -842,6 +1347,122 @@ export class GPUHairSystem {
842
1347
  graph.pop_scope();
843
1348
  }
844
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
+
845
1466
  /**
846
1467
  * Record the light of a view's hair, written over the pixels its strands cover in the scene colour, which it
847
1468
  * republishes. Records nothing for a frame {@link graph_draw} drew no hair into.
@@ -950,8 +1571,19 @@ export class GPUHairSystem {
950
1571
  destroy() {
951
1572
  this.#database.destroy();
952
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;
953
1584
  this.#grooms.clear();
954
1585
  this.#contexts.clear();
1586
+ this.#context_list.length = 0;
955
1587
  this.#row_limit = 0;
956
1588
  }
957
1589
 
@@ -965,6 +1597,80 @@ export class GPUHairSystem {
965
1597
  this.#view_f32[1] = height;
966
1598
  this.#view_f32[2] = this.pixel_floor;
967
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;
968
1674
  }
969
1675
 
970
1676
  /**
@@ -1017,8 +1723,9 @@ export class GPUHairSystem {
1017
1723
  assert.lessThanOrEqual(group.level_count, 8, 'a group of more than 129 points');
1018
1724
 
1019
1725
  record.node = context.node;
1020
- record.points = entry.offsets[g * 2];
1021
- record.strands = entry.offsets[g * 2 + 1];
1726
+ record.points = entry.offsets[g * 3];
1727
+ record.strands = entry.offsets[g * 3 + 1];
1728
+ record.guides = entry.offsets[g * 3 + 2];
1022
1729
  record.strand_count = group.strand_count;
1023
1730
  record.point_count = group.point_count;
1024
1731
  record.level_count = group.level_count;
@@ -1048,6 +1755,37 @@ export class GPUHairSystem {
1048
1755
  record.roughness = material.roughness;
1049
1756
  record.root_spacing = group.root_spacing;
1050
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
+
1051
1789
  return record;
1052
1790
  }
1053
1791
 
@@ -1073,7 +1811,8 @@ export class GPUHairSystem {
1073
1811
  for (let g = 0; g < groups.length; g++) {
1074
1812
  const group = groups[g];
1075
1813
 
1076
- words += group.strand_count * group.point_count * GROOM_POINT_WORDS + group.strand_count;
1814
+ words += group.strand_count * group.point_count * GROOM_POINT_WORDS + group.strand_count
1815
+ + group.guide_count * HAIR_GUIDE_RECORD_WORDS;
1077
1816
  }
1078
1817
 
1079
1818
  const run = this.#pool.allocate(words);
@@ -1084,7 +1823,7 @@ export class GPUHairSystem {
1084
1823
 
1085
1824
  entry = new GroomEntry();
1086
1825
  entry.run = run;
1087
- entry.offsets = new Uint32Array(groups.length * 2);
1826
+ entry.offsets = new Uint32Array(groups.length * 3);
1088
1827
  entry.references = 1;
1089
1828
 
1090
1829
  let at = run.offset;
@@ -1092,19 +1831,38 @@ export class GPUHairSystem {
1092
1831
  for (let g = 0; g < groups.length; g++) {
1093
1832
  const group = groups[g];
1094
1833
 
1095
- entry.offsets[g * 2] = at;
1834
+ entry.offsets[g * 3] = at;
1096
1835
  this.#pool.write(at, group.points, group.points.length);
1097
1836
  at += group.points.length;
1098
1837
 
1838
+ // a strand's word: its variation byte, low, and the guide it follows in the high 16 bits
1099
1839
  const strands = new Uint32Array(group.strand_count);
1100
1840
 
1101
1841
  for (let s = 0; s < group.strand_count; s++) {
1102
- strands[s] = group.variation[s];
1842
+ strands[s] = (group.variation[s] | (group.strand_guide[s] << 16)) >>> 0;
1103
1843
  }
1104
1844
 
1105
- entry.offsets[g * 2 + 1] = at;
1845
+ entry.offsets[g * 3 + 1] = at;
1106
1846
  this.#pool.write(at, strands, strands.length);
1107
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;
1108
1866
  }
1109
1867
 
1110
1868
  this.#grooms.set(groom, entry);
@@ -1112,6 +1870,379 @@ export class GPUHairSystem {
1112
1870
  return entry;
1113
1871
  }
1114
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
+
1115
2246
  /**
1116
2247
  * @param {Groom} groom
1117
2248
  */