@woosh/meep-engine 3.75.0 → 3.76.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 (216) hide show
  1. package/package.json +1 -1
  2. package/src/engine/asset/GameAssetType.d.ts +1 -0
  3. package/src/engine/asset/GameAssetType.js +5 -1
  4. package/src/engine/asset/loaders/GroomAssetLoader.d.ts +33 -0
  5. package/src/engine/asset/loaders/GroomAssetLoader.d.ts.map +1 -0
  6. package/src/engine/asset/loaders/GroomAssetLoader.js +99 -0
  7. package/src/engine/graphics/ecs/hair/Hair.d.ts +47 -0
  8. package/src/engine/graphics/ecs/hair/Hair.d.ts.map +1 -0
  9. package/src/engine/graphics/ecs/hair/Hair.js +61 -0
  10. package/src/engine/graphics/hair/Groom.d.ts +130 -0
  11. package/src/engine/graphics/hair/Groom.d.ts.map +1 -0
  12. package/src/engine/graphics/hair/Groom.js +193 -0
  13. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts +24 -0
  14. package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts.map +1 -0
  15. package/src/engine/graphics/hair/GroomSerializationAdapter.js +77 -0
  16. package/src/engine/graphics/hair/groom_build_group.d.ts +85 -0
  17. package/src/engine/graphics/hair/groom_build_group.d.ts.map +1 -0
  18. package/src/engine/graphics/hair/groom_build_group.js +487 -0
  19. package/src/engine/graphics/hair/groom_generate_scalp.d.ts +29 -0
  20. package/src/engine/graphics/hair/groom_generate_scalp.d.ts.map +1 -0
  21. package/src/engine/graphics/hair/groom_generate_scalp.js +184 -0
  22. package/src/engine/graphics/hair/groom_rank_order.d.ts +15 -0
  23. package/src/engine/graphics/hair/groom_rank_order.d.ts.map +1 -0
  24. package/src/engine/graphics/hair/groom_rank_order.js +53 -0
  25. package/src/engine/graphics/hair/groom_root_spacing.d.ts +12 -0
  26. package/src/engine/graphics/hair/groom_root_spacing.d.ts.map +1 -0
  27. package/src/engine/graphics/hair/groom_root_spacing.js +120 -0
  28. package/src/engine/graphics3/HairSystem.d.ts +102 -0
  29. package/src/engine/graphics3/HairSystem.d.ts.map +1 -0
  30. package/src/engine/graphics3/HairSystem.js +456 -0
  31. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  32. package/src/shade/assets/textures/hair_scattering.bin +0 -0
  33. package/src/shade/device/pipeline/PipelineEnquiry.d.ts +11 -0
  34. package/src/shade/device/pipeline/PipelineEnquiry.d.ts.map +1 -1
  35. package/src/shade/device/pipeline/PipelineEnquiry.js +13 -0
  36. package/src/shade/playground/hair/index.html +50 -0
  37. package/src/shade/playground/hair/main.d.ts +2 -0
  38. package/src/shade/playground/hair/main.d.ts.map +1 -0
  39. package/src/shade/playground/hair/main.js +274 -0
  40. package/src/shade/playground/screen_probes/README.md +54 -0
  41. package/src/shade/playground/screen_probes/index.html +271 -0
  42. package/src/shade/playground/screen_probes/main.d.ts +8 -0
  43. package/src/shade/playground/screen_probes/main.d.ts.map +1 -0
  44. package/src/shade/playground/screen_probes/main.js +527 -0
  45. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  46. package/src/shade/renderer/Renderer.js +54 -8
  47. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.d.ts +6 -0
  48. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.d.ts.map +1 -1
  49. package/src/shade/renderer/STATIC_GRAPHICS_ENGINE_ASSETS.js +10 -0
  50. package/src/shade/renderer/acoustics/GPUAcousticLattice.d.ts +1 -1
  51. package/src/shade/renderer/acoustics/GPUAcousticProbes.d.ts +1 -1
  52. package/src/shade/renderer/acoustics/GPUAcoustics.d.ts +1 -1
  53. package/src/shade/renderer/deferred/chunk_read_gBuffer_material.d.ts.map +1 -1
  54. package/src/shade/renderer/deferred/chunk_read_gBuffer_material.js +3 -1
  55. package/src/shade/renderer/deferred/chunk_shade_direct_lights.d.ts +23 -0
  56. package/src/shade/renderer/deferred/chunk_shade_direct_lights.d.ts.map +1 -0
  57. package/src/shade/renderer/deferred/chunk_shade_direct_lights.js +111 -0
  58. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts +17 -6
  59. package/src/shade/renderer/deferred/chunk_shade_directional_lights.d.ts.map +1 -1
  60. package/src/shade/renderer/deferred/chunk_shade_directional_lights.js +26 -21
  61. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.d.ts +12 -22
  62. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.d.ts.map +1 -1
  63. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct.js +20 -100
  64. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct_restir.d.ts.map +1 -1
  65. package/src/shade/renderer/deferred/chunk_shade_standard_material_direct_restir.js +6 -4
  66. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.d.ts +15 -0
  67. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.d.ts.map +1 -0
  68. package/src/shade/renderer/deferred/chunk_standard_material_direct_light_lobe.js +47 -0
  69. package/src/shade/renderer/extension/ViewLighting.d.ts +101 -0
  70. package/src/shade/renderer/extension/ViewLighting.d.ts.map +1 -0
  71. package/src/shade/renderer/extension/ViewLighting.js +168 -0
  72. package/src/shade/renderer/fluid/GPUFluidCascade.d.ts +3 -3
  73. package/src/shade/renderer/fluid/GPUFluidCascadeStack.d.ts +1 -1
  74. package/src/shade/renderer/fluid/forcing/GPUFluidForcing.d.ts +7 -7
  75. package/src/shade/renderer/fluid/pressure/GPUFluidPressureSolve.d.ts +1 -1
  76. package/src/shade/renderer/fluid/query/GPUFluidQueries.d.ts +1 -1
  77. package/src/shade/renderer/global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.d.ts.map +1 -1
  78. package/src/shade/renderer/global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.js +3 -1
  79. package/src/shade/renderer/global_illumination/probe_clipmap/GPUProbeClipmapGI.d.ts +1 -1
  80. package/src/shade/renderer/global_illumination/probe_clipmap/probe/shader_probe_clipmap_indirect_lighting.d.ts.map +1 -1
  81. package/src/shade/renderer/global_illumination/probe_clipmap/probe/shader_probe_clipmap_indirect_lighting.js +3 -1
  82. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts +15 -7
  83. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts.map +1 -1
  84. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.js +40 -9
  85. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.d.ts +23 -0
  86. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.d.ts.map +1 -1
  87. package/src/shade/renderer/global_illumination/screen_probe/SCREEN_PROBE.js +26 -0
  88. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeDebugView.d.ts +1 -0
  89. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeDebugView.js +6 -0
  90. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts +13 -1
  91. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts.map +1 -1
  92. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.js +23 -1
  93. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe.d.ts.map +1 -1
  94. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe.js +2 -0
  95. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.d.ts +29 -0
  96. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.d.ts.map +1 -0
  97. package/src/shade/renderer/global_illumination/screen_probe/chunk_screen_probe_rough_specular.js +102 -0
  98. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.d.ts +8 -1
  99. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.d.ts.map +1 -1
  100. package/src/shade/renderer/global_illumination/screen_probe/graph_screen_probe_debug_view.js +27 -1
  101. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.d.ts +4 -1
  102. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.d.ts.map +1 -1
  103. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_gather.js +4 -1
  104. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.d.ts +11 -5
  105. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.d.ts.map +1 -1
  106. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_indirect_lighting.js +28 -10
  107. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.d.ts +36 -2
  108. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.d.ts.map +1 -1
  109. package/src/shade/renderer/global_illumination/screen_probe/shader_screen_probe_resolve.js +149 -4
  110. package/src/shade/renderer/global_illumination/shader_resolve_indirect_lighting.d.ts.map +1 -1
  111. package/src/shade/renderer/global_illumination/shader_resolve_indirect_lighting.js +3 -1
  112. package/src/shade/renderer/hair/GPUHairSystem.d.ts +159 -0
  113. package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -0
  114. package/src/shade/renderer/hair/GPUHairSystem.js +1132 -0
  115. package/src/shade/renderer/hair/HAIR.d.ts +102 -0
  116. package/src/shade/renderer/hair/HAIR.d.ts.map +1 -0
  117. package/src/shade/renderer/hair/HAIR.js +115 -0
  118. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts +17 -0
  119. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts.map +1 -0
  120. package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.js +71 -0
  121. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts +11 -0
  122. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.d.ts.map +1 -0
  123. package/src/shade/renderer/hair/HAIR_FRAME_STRUCTS.js +48 -0
  124. package/src/shade/renderer/hair/HairFrame.d.ts +41 -0
  125. package/src/shade/renderer/hair/HairFrame.d.ts.map +1 -0
  126. package/src/shade/renderer/hair/HairFrame.js +76 -0
  127. package/src/shade/renderer/hair/chunk_hair_bsdf.d.ts +42 -0
  128. package/src/shade/renderer/hair/chunk_hair_bsdf.d.ts.map +1 -0
  129. package/src/shade/renderer/hair/chunk_hair_bsdf.js +170 -0
  130. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts +30 -0
  131. package/src/shade/renderer/hair/chunk_hair_dual_scattering.d.ts.map +1 -0
  132. package/src/shade/renderer/hair/chunk_hair_dual_scattering.js +131 -0
  133. package/src/shade/renderer/hair/chunk_hair_pixel.d.ts +32 -0
  134. package/src/shade/renderer/hair/chunk_hair_pixel.d.ts.map +1 -0
  135. package/src/shade/renderer/hair/chunk_hair_pixel.js +116 -0
  136. package/src/shade/renderer/hair/chunk_hair_strand.d.ts +40 -0
  137. package/src/shade/renderer/hair/chunk_hair_strand.d.ts.map +1 -0
  138. package/src/shade/renderer/hair/chunk_hair_strand.js +85 -0
  139. package/src/shade/renderer/hair/chunk_hair_volume.d.ts +31 -0
  140. package/src/shade/renderer/hair/chunk_hair_volume.d.ts.map +1 -0
  141. package/src/shade/renderer/hair/chunk_hair_volume.js +106 -0
  142. package/src/shade/renderer/hair/hair_id_draw.d.ts +24 -0
  143. package/src/shade/renderer/hair/hair_id_draw.d.ts.map +1 -0
  144. package/src/shade/renderer/hair/hair_id_draw.js +135 -0
  145. package/src/shade/renderer/hair/hair_lighting.d.ts +53 -0
  146. package/src/shade/renderer/hair/hair_lighting.d.ts.map +1 -0
  147. package/src/shade/renderer/hair/hair_lighting.js +270 -0
  148. package/src/shade/renderer/hair/hair_scattering_lut.d.ts +75 -0
  149. package/src/shade/renderer/hair/hair_scattering_lut.d.ts.map +1 -0
  150. package/src/shade/renderer/hair/hair_scattering_lut.js +271 -0
  151. package/src/shade/renderer/hair/hair_stamp.d.ts +33 -0
  152. package/src/shade/renderer/hair/hair_stamp.d.ts.map +1 -0
  153. package/src/shade/renderer/hair/hair_stamp.js +175 -0
  154. package/src/shade/renderer/hair/shader_hair_classes.d.ts +21 -0
  155. package/src/shade/renderer/hair/shader_hair_classes.d.ts.map +1 -0
  156. package/src/shade/renderer/hair/shader_hair_classes.js +86 -0
  157. package/src/shade/renderer/hair/shader_hair_cull.d.ts +31 -0
  158. package/src/shade/renderer/hair/shader_hair_cull.d.ts.map +1 -0
  159. package/src/shade/renderer/hair/shader_hair_cull.js +194 -0
  160. package/src/shade/renderer/hair/shader_hair_lod.d.ts +33 -0
  161. package/src/shade/renderer/hair/shader_hair_lod.d.ts.map +1 -0
  162. package/src/shade/renderer/hair/shader_hair_lod.js +268 -0
  163. package/src/shade/renderer/hair/shader_hair_volume.d.ts +28 -0
  164. package/src/shade/renderer/hair/shader_hair_volume.d.ts.map +1 -0
  165. package/src/shade/renderer/hair/shader_hair_volume.js +167 -0
  166. package/src/shade/renderer/loader/gltf/tiny-gltf.d.ts +1 -1
  167. package/src/shade/renderer/loader/usd/usd_build_scene.d.ts +31 -0
  168. package/src/shade/renderer/loader/usd/usd_build_scene.d.ts.map +1 -1
  169. package/src/shade/renderer/loader/usd/usd_build_scene.js +36 -21
  170. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts +60 -0
  171. package/src/shade/renderer/loader/usd/usd_groom_extension.d.ts.map +1 -0
  172. package/src/shade/renderer/loader/usd/usd_groom_extension.js +417 -0
  173. package/src/shade/renderer/path_tracer/shader/chunk_sample_material_data.d.ts.map +1 -1
  174. package/src/shade/renderer/path_tracer/shader/chunk_sample_material_data.js +4 -1
  175. package/src/shade/renderer/path_tracer/shader/chunk_shading_closure_from_material_data.d.ts.map +1 -1
  176. package/src/shade/renderer/path_tracer/shader/chunk_shading_closure_from_material_data.js +3 -1
  177. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  178. package/src/shade/renderer/postprocess/ssr/SSR.d.ts +4 -1
  179. package/src/shade/renderer/postprocess/ssr/SSR.d.ts.map +1 -1
  180. package/src/shade/renderer/postprocess/ssr/SSR.js +9 -0
  181. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.d.ts +16 -1
  182. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.d.ts.map +1 -1
  183. package/src/shade/renderer/postprocess/ssr/resolve/chunk_ssr_sample_radiance.js +47 -3
  184. package/src/shade/renderer/postprocess/ssr/resolve/ssr_resolve_shader.d.ts.map +1 -1
  185. package/src/shade/renderer/postprocess/ssr/resolve/ssr_resolve_shader.js +5 -3
  186. package/src/shade/renderer/postprocess/ssr/ssr_combine_shader.d.ts.map +1 -1
  187. package/src/shade/renderer/postprocess/ssr/ssr_combine_shader.js +6 -5
  188. package/src/shade/renderer/rasterize/forward/chunk_forward_shade_standard_fragment.d.ts.map +1 -1
  189. package/src/shade/renderer/rasterize/forward/chunk_forward_shade_standard_fragment.js +5 -2
  190. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.d.ts.map +1 -1
  191. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.js +5 -0
  192. package/src/shade/renderer/rasterize/native/avboit/shader_avboit_splat.d.ts.map +1 -1
  193. package/src/shade/renderer/rasterize/native/avboit/shader_avboit_splat.js +4 -1
  194. package/src/shade/renderer/rasterize/native/oit/oit_resolve_fragment_body.d.ts.map +1 -1
  195. package/src/shade/renderer/rasterize/native/oit/oit_resolve_fragment_body.js +5 -1
  196. package/src/shade/renderer/rasterize/native/oit/shader_oit_generate_moments.d.ts.map +1 -1
  197. package/src/shade/renderer/rasterize/native/oit/shader_oit_generate_moments.js +7 -4
  198. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.d.ts +13 -8
  199. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.d.ts.map +1 -1
  200. package/src/shade/renderer/shader/chunk/brdf/chunk_shadowed_F90.js +18 -14
  201. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.d.ts +3 -2
  202. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.d.ts.map +1 -1
  203. package/src/shade/renderer/shader/chunk/light/chunk_compute_multiscattering_specular.js +12 -4
  204. package/src/shade/renderer/shader/chunk/material/MATERIAL_SHADING_INFO_STRUCT.d.ts.map +1 -1
  205. package/src/shade/renderer/shader/chunk/material/MATERIAL_SHADING_INFO_STRUCT.js +1 -0
  206. package/src/shade/renderer/shader/chunk/material/chunk_compute_indirect_specular.js +1 -1
  207. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.d.ts +16 -0
  208. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.d.ts.map +1 -0
  209. package/src/shade/renderer/shader/chunk/material/chunk_metalness_to_specular_F90.js +25 -0
  210. package/src/shade/renderer/shader/chunk/material/chunk_re_direct_multiscatter.js +2 -2
  211. package/src/shade/renderer/shader/compiler/ShaderSwitch.d.ts +1 -1
  212. package/src/shade/renderer/shader/compiler/ShaderSwitchSet.d.ts +1 -1
  213. package/src/shade/renderer/shader/resource/ShaderResourceDescriptor.d.ts +1 -1
  214. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.d.ts +2 -2
  215. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.js +2 -2
  216. package/src/shade/renderer/water/query/GPUWaterQueries.d.ts +1 -1
@@ -0,0 +1,1132 @@
1
+ import { assert } from "../../../core/assert.js";
2
+ import { ALLOCATOR_NO_SPACE } from "../../../core/binary/allocator/OffsetAllocator.js";
3
+ import { GROOM_POINT_WORDS } from "../../../engine/graphics/hair/Groom.js";
4
+ import { TextureViewDescriptor } from "../../descriptor/texture/TextureViewDescriptor.js";
5
+ import { BufferResourceDescriptor } from "../../device/graph/BufferResourceDescriptor.js";
6
+ import { TextureResourceDescriptor } from "../../device/graph/TextureResourceDescriptor.js";
7
+ import { ShadeGPUCommandContext } from "../../device/ShadeGPUCommandContext.js";
8
+ import { graph_read_buffer } from "../buffer/graph_read_buffer.js";
9
+ import { GPURunPool } from "../buffer/GPURunPool.js";
10
+ import { GPUDatabase } from "../buffer/table/GPUDatabase.js";
11
+ import { DEFAULT_DEPTH_TEXTURE_VIEW } from "../depth/DEFAULT_DEPTH_TEXTURE_VIEW.js";
12
+ import { GBufferTextures } from "../extension/GBufferTextures.js";
13
+ import { SceneColor } from "../extension/SceneColor.js";
14
+ import { ViewLighting } from "../extension/ViewLighting.js";
15
+ import { ViewTextures } from "../extension/ViewTextures.js";
16
+ import { INDIRECT_COMPUTE_COMMAND_DESCRIPTOR } from "../gpu_primitive/indirect/IndirectComputeCommandBuildPass.js";
17
+ import {
18
+ enquire_prefix_scan_csdldf_pipelines,
19
+ graph_prefix_scan_csdldf,
20
+ prefix_scan_csdldf_output_descriptor
21
+ } from "../gpu_primitive/prefix_sum/v1/graph_prefix_scan_csdldf.js";
22
+ import { ENVIRONMENT_MAP_SAMPLER_DESCRIPTOR } from "../light/environment/ENVIRONMENT_MAP_SAMPLER_RESOURCE.js";
23
+ import { view_velocity_matrices } from "../postprocess/velocity/view_velocity_matrices.js";
24
+ import { shader_prefix_sum_to_command } from "../rasterize/expand/mesh/shader_prefix_sum_to_command.js";
25
+ import { graph_compute_indirect_pass } from "../shader/graph/compute/graph_compute_indirect_pass.js";
26
+ import { graph_compute_pass } from "../shader/graph/compute/graph_compute_pass.js";
27
+ import { graph_create_buffer } from "../shader/graph/graph_create_buffer.js";
28
+ import { SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR } from "../shadow/map/SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR.js";
29
+ import { STATIC_GRAPHICS_ENGINE_ASSETS } from "../STATIC_GRAPHICS_ENGINE_ASSETS.js";
30
+ import { SamplerDescriptor } from "../../descriptor/texture/SamplerDescriptor.js";
31
+ import {
32
+ HAIR_CLASS_COUNT,
33
+ HAIR_COUNTER_POINTS,
34
+ HAIR_COUNTER_VOXELS,
35
+ HAIR_COUNTER_WORDS,
36
+ HAIR_DRAW_ARGS_WORDS,
37
+ HAIR_ID_FORMAT,
38
+ HAIR_ID_NONE,
39
+ HAIR_LOD_WORDS,
40
+ HAIR_POINT_CAPACITY_LIMIT,
41
+ HAIR_ROW_BITS
42
+ } from "./HAIR.js";
43
+ 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";
45
+ import { HairFrame } from "./HairFrame.js";
46
+ import { HAIR_ID_PIPELINE, HAIR_ID_RESOURCES } from "./hair_id_draw.js";
47
+ import { HAIR_LIGHTING_RESOURCES, hair_lighting_pipeline } from "./hair_lighting.js";
48
+ import { HAIR_MOTION_PIPELINE, HAIR_STAMP_PIPELINE, HAIR_STAMP_RESOURCES } from "./hair_stamp.js";
49
+ import { shader_hair_draw_args, shader_hair_regions } from "./shader_hair_classes.js";
50
+ import { shader_hair_cull } from "./shader_hair_cull.js";
51
+ import { shader_hair_volume_clear, shader_hair_volume_command, shader_hair_volume_mips } from "./shader_hair_volume.js";
52
+ import { HAIR_LOD_WORKGROUP_SIZE, shader_hair_lod } from "./shader_hair_lod.js";
53
+
54
+ /**
55
+ * @typedef {import("../../../engine/graphics/hair/Groom.js").Groom} Groom
56
+ * @typedef {import("../extension/FrameContext.js").FrameContext} FrameContext
57
+ * @typedef {import("../GraphicsContext.js").GraphicsContext} GraphicsContext
58
+ */
59
+
60
+ /**
61
+ * Words the groom pool starts with room for.
62
+ * @type {number}
63
+ */
64
+ const INITIAL_POOL_WORDS = 2 ** 20;
65
+
66
+ /**
67
+ * Points a frame's deformed buffer is first made with room for.
68
+ * @type {number}
69
+ */
70
+ const INITIAL_POINT_CAPACITY = 2 ** 18;
71
+
72
+ /**
73
+ * Strands a frame's draw list is first made with room for.
74
+ * @type {number}
75
+ */
76
+ const INITIAL_LIST_CAPACITY = 2 ** 16;
77
+
78
+ /**
79
+ * Voxels a frame's density volumes are first made with room for, every level of every volume.
80
+ * @type {number}
81
+ */
82
+ const INITIAL_VOXEL_CAPACITY = 2 ** 20;
83
+
84
+ /**
85
+ * The narrowest a strand is drawn, pixels. One pixel is the narrowest strip that rasterizes without gaps at every
86
+ * jitter offset; below it, whether a strand is seen at a pixel is the jitter's to decide, and that flickers.
87
+ * @type {number}
88
+ */
89
+ export const HAIR_PIXEL_FLOOR = 1;
90
+
91
+ /**
92
+ * The most a dropped point may stray from the chord its kept neighbours make, pixels at the instance's nearest point.
93
+ * @type {number}
94
+ */
95
+ export const HAIR_DECIMATION_TOLERANCE = 0.5;
96
+
97
+ const MIP_0 = TextureViewDescriptor.from({ mipLevelCount: 1, baseMipLevel: 0 });
98
+
99
+ /**
100
+ * Bytes of a `u32` collection with room for `count` elements: the count, padded to 16 bytes, then whole vec4s.
101
+ *
102
+ * @param {number} count
103
+ * @returns {number}
104
+ */
105
+ function collection_size(count) {
106
+ return 16 + Math.max(1, Math.ceil(count / 4)) * 16;
107
+ }
108
+
109
+ /**
110
+ * How an entity's hair looks: the colour of its fibres and how rough they are.
111
+ */
112
+ export class HairMaterial {
113
+ /** @type {number} 0 for none, 1 for black */
114
+ melanin = 0.5;
115
+
116
+ /** @type {number} the share of the melanin that is pheomelanin */
117
+ redness = 0;
118
+
119
+ /** @type {Float32Array} linear; white is no dye */
120
+ dye = new Float32Array([1, 1, 1]);
121
+
122
+ /** @type {number} longitudinal roughness, 0 to 1 */
123
+ roughness = 0.3;
124
+
125
+ /**
126
+ * @param {HairMaterial} other
127
+ */
128
+ copy(other) {
129
+ this.melanin = other.melanin;
130
+ this.redness = other.redness;
131
+ this.dye.set(other.dye);
132
+ this.roughness = other.roughness;
133
+ }
134
+
135
+ /**
136
+ * @param {HairMaterial} other
137
+ * @returns {boolean}
138
+ */
139
+ equals(other) {
140
+ return this.melanin === other.melanin
141
+ && this.redness === other.redness
142
+ && this.dye[0] === other.dye[0]
143
+ && this.dye[1] === other.dye[1]
144
+ && this.dye[2] === other.dye[2]
145
+ && this.roughness === other.roughness;
146
+ }
147
+ }
148
+
149
+ /**
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.
152
+ */
153
+ class GroomEntry {
154
+ /** @type {import("../../../core/binary/allocator/OffsetAllocator.js").Allocation} */
155
+ run = null;
156
+
157
+ /** @type {Uint32Array} a group's first point word, then its first strand word, a pair a group */
158
+ offsets = new Uint32Array(0);
159
+
160
+ /** @type {number} */
161
+ references = 0;
162
+ }
163
+
164
+ /**
165
+ * One entity's hair in the runtime: a row a group of its groom.
166
+ */
167
+ export class GPUHairContext {
168
+ /** @type {Groom|null} */
169
+ groom = null;
170
+
171
+ /** @type {number[]} a row of the instance table a group, in the groom's order */
172
+ rows = [];
173
+
174
+ /** @type {number} the `transforms` row of the entity */
175
+ node = 0;
176
+
177
+ /** @type {HairMaterial} what was last staged */
178
+ material = new HairMaterial();
179
+ }
180
+
181
+ /**
182
+ * The strand hair of one scene context: the instance table, the groom pool, and the passes that draw a view's hair
183
+ * into its G-buffer and light it.
184
+ *
185
+ * **The CPU registers and stages; the GPU decides what is drawn.** {@link add} uploads the groom once, however many
186
+ * entities wear it, and claims a row a group; {@link set_node} and {@link set_material} restage; {@link remove} lets
187
+ * go. Nothing per strand happens on the CPU.
188
+ *
189
+ * Each frame, at the phase where the G-buffer is written into, {@link graph_draw}: the level of detail of every
190
+ * instance, the cull and deformation of the strands it keeps with their density volume (cleared, laid, its coarser
191
+ * 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.
194
+ *
195
+ * The frame's buffers grow from what the GPU asked for, read back: a frame asking for more than they hold draws what
196
+ * fits, and the next frames hold what was asked.
197
+ */
198
+ export class GPUHairSystem {
199
+ /**
200
+ * Offer the pipelines the hair's passes run — see `PipelineEnquiry`.
201
+ *
202
+ * @param {import("../../device/pipeline/PipelineEnquiry.js").PipelineEnquiry} enquiry
203
+ */
204
+ static enquirePipelines(enquiry) {
205
+ enquiry.compute.push(shader_hair_lod.pipeline);
206
+ enquiry.compute.push(shader_hair_regions.pipeline);
207
+ enquiry.compute.push(shader_hair_cull.pipeline);
208
+ enquiry.compute.push(shader_hair_draw_args.pipeline);
209
+ enquiry.compute.push(shader_hair_volume_command.pipeline);
210
+ enquiry.compute.push(shader_hair_volume_clear.pipeline);
211
+ enquiry.compute.push(shader_hair_volume_mips.pipeline);
212
+ enquiry.compute.push(shader_prefix_sum_to_command.pipeline);
213
+ enquire_prefix_scan_csdldf_pipelines(enquiry);
214
+ enquiry.render.push(HAIR_ID_PIPELINE);
215
+ enquiry.render.push(HAIR_STAMP_PIPELINE);
216
+ enquiry.render.push(HAIR_MOTION_PIPELINE);
217
+ }
218
+
219
+ /**
220
+ * The narrowest a strand is drawn, pixels: the level of detail keeps a group's strands at least this wide and
221
+ * fewer of them in proportion. At 0 every strand is drawn at its own width, which a frame can only sample, not
222
+ * show: what the area-conserving thinning is measured against.
223
+ *
224
+ * @type {number}
225
+ */
226
+ pixel_floor = HAIR_PIXEL_FLOOR;
227
+
228
+ /** @type {GraphicsContext} */
229
+ #graphics;
230
+
231
+ /** @type {GPUDatabase} */
232
+ #database;
233
+
234
+ /** @type {GPURunPool} every groom's points and strand words, a run a groom, in `u32` words */
235
+ #pool;
236
+
237
+ /** @type {Map<Groom, GroomEntry>} */
238
+ #grooms = new Map();
239
+
240
+ /** @type {Set<GPUHairContext>} */
241
+ #contexts = new Set();
242
+
243
+ /** @type {number} rows the instance passes draw: one past the highest in use */
244
+ #row_limit = 0;
245
+
246
+ #point_capacity = INITIAL_POINT_CAPACITY;
247
+ #points_asked = 0;
248
+
249
+ #list_capacity = INITIAL_LIST_CAPACITY;
250
+ #strands_asked = 0;
251
+
252
+ #voxel_capacity = INITIAL_VOXEL_CAPACITY;
253
+ #voxels_asked = 0;
254
+
255
+ /**
256
+ * The record a row is written from.
257
+ * @type {Object}
258
+ */
259
+ #record = {
260
+ node: 0,
261
+ points: 0,
262
+ strands: 0,
263
+ strand_count: 0,
264
+ point_count: 0,
265
+ level_count: 0,
266
+ melanin: 0,
267
+ redness: 0,
268
+ bounds_min: new Float32Array(3),
269
+ width_max: 0,
270
+ bounds_extent: new Float32Array(3),
271
+ width_mean: 0,
272
+ decimation_low: new Float32Array(4),
273
+ decimation_high: new Float32Array(4),
274
+ dye: new Float32Array(3),
275
+ roughness: 0,
276
+ root_spacing: 0,
277
+ };
278
+
279
+ #view_words = new ArrayBuffer(HAIR_VIEW_STRUCT.size);
280
+ #view_f32 = new Float32Array(this.#view_words);
281
+ #view_u32 = new Uint32Array(this.#view_words);
282
+
283
+ #inverse_view_projection = new Float32Array(16);
284
+ #view_projection_previous = new Float32Array(16);
285
+
286
+ /**
287
+ * @param {GraphicsContext} graphics
288
+ */
289
+ constructor(graphics) {
290
+ assert.defined(graphics, 'graphics');
291
+
292
+ const device = graphics.device;
293
+
294
+ this.#graphics = graphics;
295
+ this.#database = new GPUDatabase({
296
+ device,
297
+ definition: HAIR_DATABASE_SPEC,
298
+ label: "Hair",
299
+ initial_data_size_bytes: HAIR_DATABASE_INITIAL_SIZE,
300
+ });
301
+ this.#pool = new GPURunPool(device, "Hair / grooms", 4, INITIAL_POOL_WORDS);
302
+ }
303
+
304
+ /** @returns {GPUDatabase} */
305
+ get database() {
306
+ return this.#database;
307
+ }
308
+
309
+ /** @returns {import("../buffer/table/GPUTypedTable.js").GPUTypedTable} */
310
+ get table() {
311
+ return this.#database.get(HAIR_INSTANCE_TABLE);
312
+ }
313
+
314
+ /** @returns {GPURunPool} */
315
+ get pool() {
316
+ return this.#pool;
317
+ }
318
+
319
+ /** @returns {number} grooms on the device */
320
+ get groom_count() {
321
+ return this.#grooms.size;
322
+ }
323
+
324
+ /** @returns {number} entities registered */
325
+ get count() {
326
+ return this.#contexts.size;
327
+ }
328
+
329
+ /** @returns {number} points a frame's deformed buffer holds */
330
+ get point_capacity() {
331
+ return this.#point_capacity;
332
+ }
333
+
334
+ /** @returns {number} the most points a frame asked for, as last read off the device */
335
+ get points_asked() {
336
+ return this.#points_asked;
337
+ }
338
+
339
+ /** @returns {number} strands a frame's draw list holds */
340
+ get list_capacity() {
341
+ return this.#list_capacity;
342
+ }
343
+
344
+ /** @returns {number} the most strands a frame's draw list was asked to hold, as last read off the device */
345
+ get strands_asked() {
346
+ return this.#strands_asked;
347
+ }
348
+
349
+ /** @returns {number} voxels a frame's density volumes hold */
350
+ get voxel_capacity() {
351
+ return this.#voxel_capacity;
352
+ }
353
+
354
+ /** @returns {number} the most voxels a frame asked for, as last read off the device */
355
+ get voxels_asked() {
356
+ return this.#voxels_asked;
357
+ }
358
+
359
+ /** @returns {number} rows the instance passes draw */
360
+ get row_limit() {
361
+ return this.#row_limit;
362
+ }
363
+
364
+ /**
365
+ * Register an entity's hair.
366
+ *
367
+ * @param {Groom} groom
368
+ * @param {number} node the `transforms` row of the entity
369
+ * @param {HairMaterial} material
370
+ * @returns {GPUHairContext|null} null when the pool cannot house the groom on this device
371
+ */
372
+ add(groom, node, material) {
373
+ assert.defined(groom, 'groom');
374
+ assert.isNonNegativeInteger(node, 'node');
375
+ assert.greaterThan(groom.groups.length, 0, 'a groom holds a group');
376
+
377
+ const entry = this.#acquire_groom(groom);
378
+
379
+ if (entry === null) {
380
+ return null;
381
+ }
382
+
383
+ const context = new GPUHairContext();
384
+
385
+ context.groom = groom;
386
+ context.node = node;
387
+ context.material.copy(material);
388
+
389
+ for (let g = 0; g < groom.groups.length; g++) {
390
+ const row = this.table.add(this.#write_record(context, entry, g));
391
+
392
+ context.rows.push(row);
393
+
394
+ this.#row_limit = Math.max(this.#row_limit, row + 1);
395
+ }
396
+
397
+ this.#contexts.add(context);
398
+
399
+ return context;
400
+ }
401
+
402
+ /**
403
+ * Point an entity's hair at another `transforms` row.
404
+ *
405
+ * @param {GPUHairContext} context
406
+ * @param {number} node
407
+ */
408
+ set_node(context, node) {
409
+ assert.isNonNegativeInteger(node, 'node');
410
+
411
+ if (context.node === node) {
412
+ return;
413
+ }
414
+
415
+ context.node = node;
416
+
417
+ this.#restage(context);
418
+ }
419
+
420
+ /**
421
+ * @param {GPUHairContext} context
422
+ * @param {HairMaterial} material
423
+ */
424
+ set_material(context, material) {
425
+ if (context.material.equals(material)) {
426
+ return;
427
+ }
428
+
429
+ context.material.copy(material);
430
+
431
+ this.#restage(context);
432
+ }
433
+
434
+ /**
435
+ * @param {GPUHairContext} context
436
+ */
437
+ remove(context) {
438
+ assert.equal(this.#contexts.has(context), true, 'the hair is not registered here');
439
+
440
+ const table = this.table;
441
+
442
+ for (let i = 0; i < context.rows.length; i++) {
443
+ table.remove(context.rows[i]);
444
+ }
445
+
446
+ this.#release_groom(context.groom);
447
+
448
+ this.#contexts.delete(context);
449
+
450
+ context.rows.length = 0;
451
+ context.groom = null;
452
+
453
+ let limit = 0;
454
+
455
+ for (const other of this.#contexts) {
456
+ for (let i = 0; i < other.rows.length; i++) {
457
+ limit = Math.max(limit, other.rows[i] + 1);
458
+ }
459
+ }
460
+
461
+ this.#row_limit = limit;
462
+ }
463
+
464
+ /**
465
+ * Send what changed since the last frame.
466
+ */
467
+ upload() {
468
+ const database = this.#database;
469
+
470
+ if (database.has_pending_upload) {
471
+ const cmd = ShadeGPUCommandContext.create(this.#graphics, "hair/upload");
472
+
473
+ database.update(cmd);
474
+
475
+ cmd.finish();
476
+ }
477
+ }
478
+
479
+ /**
480
+ * Record a view's hair into its G-buffer: the level of detail, the cull and deformation, the id raster under the
481
+ * scene's depth, the stamp and the velocity. Publishes the depth, the G-buffer targets, the visibility and the
482
+ * velocity it wrote, and creates the frame's {@link HairFrame}. Records nothing while no hair is registered.
483
+ *
484
+ * @param {FrameContext} frame at the phase where the G-buffer is written into
485
+ */
486
+ graph_draw(frame) {
487
+ if (this.#contexts.size === 0) {
488
+ return;
489
+ }
490
+
491
+ this.upload();
492
+ this.#grow();
493
+
494
+ const table = this.table;
495
+ const group_count = table.dispatch_group_count(HAIR_LOD_WORKGROUP_SIZE);
496
+
497
+ if (group_count === 0) {
498
+ return;
499
+ }
500
+
501
+ const graph = frame.graph;
502
+ const view = frame.view;
503
+ const camera = view.camera;
504
+ const scene = view.scene;
505
+
506
+ const [width, height] = frame.resolution;
507
+
508
+ const capacity = Math.max(1, table.element_capacity);
509
+ const point_capacity = this.#point_capacity;
510
+ const list_capacity = this.#list_capacity;
511
+ const voxel_capacity = this.#voxel_capacity;
512
+
513
+ const hair_database = this.#database.buffer;
514
+ const hair_grooms = this.#pool.buffer;
515
+ const scene_database = scene.scene_database_buffer;
516
+ const camera_buffer = camera.gpu_buffer;
517
+
518
+ graph.push_scope("hair");
519
+
520
+ const count_bytes = collection_size(capacity);
521
+ const lod_bytes = capacity * HAIR_LOD_WORDS * 4;
522
+ const counter_bytes = HAIR_COUNTER_WORDS * 4;
523
+
524
+ const lod = graph_compute_pass({
525
+ graph,
526
+ shader: shader_hair_lod,
527
+ bindings: {
528
+ camera: camera_buffer,
529
+ hair_database,
530
+ scene_database,
531
+ // cleared whole: the pass writes only the rows it visits
532
+ hair_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
533
+ hair_volume_counts: BufferResourceDescriptor.from(count_bytes, GPUBufferUsage.STORAGE, 0, count_bytes),
534
+ hair_lod: BufferResourceDescriptor.from(lod_bytes, GPUBufferUsage.STORAGE, 0, lod_bytes),
535
+ // read back for what was asked
536
+ hair_counters: BufferResourceDescriptor.from(counter_bytes, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, 0, counter_bytes),
537
+ settings: {
538
+ resolution: [width, height],
539
+ pixel_floor: this.pixel_floor,
540
+ tolerance: HAIR_DECIMATION_TOLERANCE,
541
+ capacity,
542
+ point_capacity,
543
+ voxel_capacity,
544
+ },
545
+ },
546
+ group_count: [group_count, 1, 1],
547
+ });
548
+
549
+ // the voxels the level of detail reserved, zeroed before the cull lays the strands into them
550
+ const volume_command = graph_compute_pass({
551
+ graph,
552
+ shader: shader_hair_volume_command,
553
+ bindings: {
554
+ hair_counters: lod.hair_counters,
555
+ command: graph_create_buffer(graph, "hair/volume command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
556
+ settings: { voxel_capacity },
557
+ },
558
+ }).command;
559
+
560
+ const cleared = graph_compute_indirect_pass({
561
+ graph,
562
+ shader: shader_hair_volume_clear,
563
+ command: volume_command,
564
+ bindings: {
565
+ hair_counters: lod.hair_counters,
566
+ hair_volume: BufferResourceDescriptor.from(voxel_capacity * 4, GPUBufferUsage.STORAGE),
567
+ settings: { voxel_capacity },
568
+ },
569
+ });
570
+
571
+ const regions = graph_compute_pass({
572
+ graph,
573
+ shader: shader_hair_regions,
574
+ bindings: { hair_counters: lod.hair_counters },
575
+ });
576
+
577
+ const sums = graph_prefix_scan_csdldf({
578
+ graph,
579
+ input: lod.hair_counts,
580
+ output: graph_create_buffer(graph, "hair/strand sums", prefix_scan_csdldf_output_descriptor(capacity)),
581
+ element_max_count: capacity,
582
+ });
583
+
584
+ const command = graph_compute_pass({
585
+ graph,
586
+ shader: shader_prefix_sum_to_command,
587
+ bindings: {
588
+ prefix_sums: sums,
589
+ command: graph_create_buffer(graph, "hair/cull command", INDIRECT_COMPUTE_COMMAND_DESCRIPTOR),
590
+ },
591
+ }).command;
592
+
593
+ const cull = graph_compute_indirect_pass({
594
+ graph,
595
+ shader: shader_hair_cull,
596
+ command,
597
+ bindings: {
598
+ camera: camera_buffer,
599
+ hair_grooms,
600
+ hair_database,
601
+ scene_database,
602
+ prefix_sums: sums,
603
+ hair_lod: lod.hair_lod,
604
+ hair_counters: regions.hair_counters,
605
+ hair_points: BufferResourceDescriptor.from(point_capacity * 2 * 16, GPUBufferUsage.STORAGE),
606
+ hair_list: BufferResourceDescriptor.from(list_capacity * 4, GPUBufferUsage.STORAGE),
607
+ hair_volume: cleared.hair_volume,
608
+ settings: {
609
+ resolution: [width, height],
610
+ pixel_floor: this.pixel_floor,
611
+ point_capacity,
612
+ list_capacity,
613
+ },
614
+ },
615
+ });
616
+
617
+ const args = graph_compute_pass({
618
+ graph,
619
+ shader: shader_hair_draw_args,
620
+ bindings: {
621
+ hair_counters: cull.hair_counters,
622
+ hair_draw_args: BufferResourceDescriptor.from(
623
+ HAIR_CLASS_COUNT * HAIR_DRAW_ARGS_WORDS * 4,
624
+ GPUBufferUsage.STORAGE | GPUBufferUsage.INDIRECT,
625
+ ),
626
+ settings: { list_capacity },
627
+ },
628
+ });
629
+
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
+ });
637
+
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;
646
+
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;
659
+
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;
671
+
672
+ // 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 })
674
+ .then(reference => {
675
+ try {
676
+ const words = new Uint32Array(reference.getValue());
677
+
678
+ let strands = 0;
679
+
680
+ for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
681
+ strands += words[c];
682
+ }
683
+
684
+ this.#strands_asked = Math.max(this.#strands_asked, strands);
685
+ this.#points_asked = Math.max(this.#points_asked, words[HAIR_COUNTER_POINTS]);
686
+ this.#voxels_asked = Math.max(this.#voxels_asked, words[HAIR_COUNTER_VOXELS]);
687
+ } finally {
688
+ reference.release();
689
+ }
690
+ })
691
+ .catch(() => {
692
+ // a reading lost to a timeout or a lost device: a later frame's carries it
693
+ });
694
+
695
+ this.#write_view(width, height, point_capacity);
696
+
697
+ const view_words = this.#view_words;
698
+
699
+ const view_textures = frame.get(ViewTextures);
700
+ const gbuffer = frame.get(GBufferTextures);
701
+
702
+ // the id raster: every class's strands under the scene's depth
703
+ const id_data = {};
704
+
705
+ const id_pass = graph.add("draw hair ids", id_data, (data, resources, context) => {
706
+ const encoder = context.encoder;
707
+
708
+ const bindings = HAIR_ID_RESOURCES.generateBindings({
709
+ camera: camera_buffer,
710
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
711
+ hair_list: resources.get(data.list),
712
+ hair_points: resources.get(data.points),
713
+ }, encoder);
714
+
715
+ const pass = encoder.constructRenderPass({
716
+ label: "Hair Ids",
717
+ pipeline: HAIR_ID_PIPELINE,
718
+ bindings,
719
+ colorAttachments: [{
720
+ view: resources.get(data.ids).obtainView(MIP_0),
721
+ loadOp: "clear",
722
+ clearValue: [HAIR_ID_NONE, 0, 0, 0],
723
+ storeOp: "store",
724
+ }],
725
+ depthStencilAttachment: {
726
+ view: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
727
+ // the scene's depth is what strands are hidden by, and what they then hide
728
+ depthLoadOp: "load",
729
+ depthStoreOp: "store",
730
+ },
731
+ });
732
+
733
+ const draw_args = resources.get(data.args);
734
+
735
+ for (let c = 0; c < HAIR_CLASS_COUNT; c++) {
736
+ pass.drawIndirect(draw_args, c * HAIR_DRAW_ARGS_WORDS * 4);
737
+ }
738
+
739
+ pass.end();
740
+ });
741
+
742
+ id_data.list = id_pass.read(cull.hair_list);
743
+ id_data.points = id_pass.read(cull.hair_points);
744
+ id_data.args = id_pass.read(args.hair_draw_args);
745
+ id_data.depth = id_pass.write(view_textures.depth);
746
+ id_data.ids = id_pass.create("hair ids", TextureResourceDescriptor.from({
747
+ format: HAIR_ID_FORMAT,
748
+ resolution: [width, height],
749
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
750
+ }));
751
+
752
+ view_textures.depth = id_data.depth;
753
+
754
+ view_velocity_matrices(view, this.#inverse_view_projection, this.#view_projection_previous);
755
+
756
+ const inverse_view_projection = this.#inverse_view_projection.slice();
757
+ const view_projection_previous = this.#view_projection_previous.slice();
758
+
759
+ const row_limit = this.#row_limit;
760
+
761
+ // the G-buffer and the velocity under every pixel a strand covers, an instance's rectangle at a time
762
+ const stamp_data = {};
763
+
764
+ const stamp_pass = graph.add("stamp hair", stamp_data, (data, resources, context) => {
765
+ const encoder = context.encoder;
766
+
767
+ const bindings = HAIR_STAMP_RESOURCES.generateBindings({
768
+ camera: camera_buffer,
769
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
770
+ hair_lod: resources.get(data.lod),
771
+ hair_points: resources.get(data.points),
772
+ hair_ids: resources.get(data.ids).obtainView(MIP_0),
773
+ scene_depth: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
774
+ inverse_view_projection: encoder.allocateTransientBufferAndLoad(inverse_view_projection.buffer, GPUBufferUsage.UNIFORM),
775
+ view_projection_previous: encoder.allocateTransientBufferAndLoad(view_projection_previous.buffer, GPUBufferUsage.UNIFORM),
776
+ }, encoder);
777
+
778
+ const attachment = handle => ({
779
+ view: resources.get(handle).obtainView(MIP_0),
780
+ // every pixel no strand covers keeps what the scene left there
781
+ loadOp: "load",
782
+ storeOp: "store",
783
+ });
784
+
785
+ const pass = encoder.constructRenderPass({
786
+ label: "Hair Stamp",
787
+ pipeline: HAIR_STAMP_PIPELINE,
788
+ bindings,
789
+ colorAttachments: [
790
+ attachment(data.pbr),
791
+ attachment(data.normal),
792
+ attachment(data.albedo),
793
+ attachment(data.emissive),
794
+ attachment(data.visibility),
795
+ ],
796
+ });
797
+
798
+ pass.draw(4, row_limit);
799
+ pass.end();
800
+
801
+ // the velocity on its own: with the surface's targets it would pass the device floor's attachment bytes
802
+ const motion = encoder.constructRenderPass({
803
+ label: "Hair Motion",
804
+ pipeline: HAIR_MOTION_PIPELINE,
805
+ bindings,
806
+ colorAttachments: [attachment(data.velocity)],
807
+ });
808
+
809
+ motion.draw(4, row_limit);
810
+ motion.end();
811
+ });
812
+
813
+ stamp_data.lod = stamp_pass.read(lod.hair_lod);
814
+ stamp_data.points = stamp_pass.read(cull.hair_points);
815
+ stamp_data.ids = stamp_pass.read(id_data.ids);
816
+ stamp_data.depth = stamp_pass.read(id_data.depth);
817
+
818
+ // in place: the written handles are what the records hand on
819
+ stamp_data.pbr = stamp_pass.write(gbuffer.pbr);
820
+ stamp_data.normal = stamp_pass.write(gbuffer.normal);
821
+ stamp_data.albedo = stamp_pass.write(gbuffer.albedo);
822
+ stamp_data.emissive = stamp_pass.write(gbuffer.emissive);
823
+
824
+ gbuffer.pbr = stamp_data.pbr;
825
+ gbuffer.normal = stamp_data.normal;
826
+ gbuffer.albedo = stamp_data.albedo;
827
+ gbuffer.emissive = stamp_data.emissive;
828
+
829
+ stamp_data.visibility = stamp_pass.write(view_textures.visibility);
830
+ stamp_data.velocity = stamp_pass.write(view_textures.velocity);
831
+
832
+ view_textures.visibility = stamp_data.visibility;
833
+ view_textures.velocity = stamp_data.velocity;
834
+
835
+ const record = frame.create(HairFrame);
836
+
837
+ record.ids = id_data.ids;
838
+ record.points = cull.hair_points;
839
+ record.lod = lod.hair_lod;
840
+ record.volume = volume;
841
+
842
+ graph.pop_scope();
843
+ }
844
+
845
+ /**
846
+ * Record the light of a view's hair, written over the pixels its strands cover in the scene colour, which it
847
+ * republishes. Records nothing for a frame {@link graph_draw} drew no hair into.
848
+ *
849
+ * @param {FrameContext} frame at the phase where the frame has been lit
850
+ */
851
+ graph_light(frame) {
852
+ if (!frame.has(HairFrame)) {
853
+ return;
854
+ }
855
+
856
+ const hair = frame.get(HairFrame);
857
+ const lighting = frame.get(ViewLighting);
858
+ const scene_color = frame.get(SceneColor);
859
+ const view_textures = frame.get(ViewTextures);
860
+
861
+ const graph = frame.graph;
862
+ const view = frame.view;
863
+ const scene = view.scene;
864
+
865
+ const [width, height] = frame.resolution;
866
+
867
+ this.#write_view(width, height, this.#point_capacity);
868
+
869
+ const view_words = this.#view_words.slice(0);
870
+ const row_limit = this.#row_limit;
871
+ const hair_database = this.#database.buffer;
872
+
873
+ const color_format = frame.describe(scene_color.color).format;
874
+ const pipeline = hair_lighting_pipeline(lighting.shader_switches, color_format);
875
+ const probes = lighting.probes;
876
+
877
+ const data = {};
878
+
879
+ const pass = graph.add("light hair", data, (data, resources, context) => {
880
+ const encoder = context.encoder;
881
+ const graphics = context.graphics;
882
+
883
+ const sampler_linear = graphics.samplers.obtain(SamplerDescriptor.from({ magFilter: "linear" }));
884
+
885
+ const inputs = probes.write_sampling_bindings({
886
+ camera: view.camera.gpu_buffer,
887
+ view: view.uniform_buffer.buffer,
888
+ hair_view: encoder.allocateTransientBufferAndLoad(view_words, GPUBufferUsage.UNIFORM),
889
+ hair_lod: resources.get(data.lod),
890
+ // the lights
891
+ light_data: scene.lights.buffer_data,
892
+ tEnvironment: scene.lights.environment.obtainView(),
893
+ sEnvironment: graphics.samplers.obtain(ENVIRONMENT_MAP_SAMPLER_DESCRIPTOR),
894
+ cluster_parameters: resources.get(data.light_cluster_parameters),
895
+ cluster_lookup: resources.get(data.light_cluster_lookup),
896
+ cluster_data: resources.get(data.light_cluster_data),
897
+ shadowmapAtlas: resources.get(data.shadowmap_atlas).obtainView(),
898
+ sCompareLinear: graphics.samplers.obtain(SHADOWMAP_COMPARE_SAMPLER_DESCRIPTOR),
899
+ // the strands
900
+ hair_points: resources.get(data.points),
901
+ hair_database,
902
+ hair_ids: resources.get(data.ids).obtainView(MIP_0),
903
+ scene_depth: resources.get(data.depth).obtainView(DEFAULT_DEPTH_TEXTURE_VIEW),
904
+ // indirect light and fog
905
+ brick4: scene.volumetric_light_map.buffer,
906
+ tVolumetrics: resources.get(data.volumetrics_lut),
907
+ volumetrics_metadata: resources.get(data.volumetrics_metadata),
908
+ tSTBN_vec3: graphics.textures.obtain(STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec3),
909
+ sLinear: sampler_linear,
910
+ hair_volume: resources.get(data.volume),
911
+ tHairScattering: graphics.textures.obtain(STATIC_GRAPHICS_ENGINE_ASSETS.hair_scattering),
912
+ });
913
+
914
+ const bindings = HAIR_LIGHTING_RESOURCES.generateBindings(inputs, encoder);
915
+
916
+ const render = encoder.constructRenderPass({
917
+ label: "Hair Lighting",
918
+ pipeline,
919
+ bindings,
920
+ colorAttachments: [{
921
+ view: resources.get(data.color).obtainView(MIP_0),
922
+ // every pixel no strand covers keeps its colour
923
+ loadOp: "load",
924
+ storeOp: "store",
925
+ }],
926
+ });
927
+
928
+ render.draw(4, row_limit);
929
+ render.end();
930
+ });
931
+
932
+ data.lod = pass.read(hair.lod);
933
+ data.points = pass.read(hair.points);
934
+ data.volume = pass.read(hair.volume);
935
+ data.ids = pass.read(hair.ids);
936
+ data.depth = pass.read(view_textures.depth);
937
+ data.light_cluster_parameters = pass.read(lighting.light_cluster_parameters);
938
+ data.light_cluster_lookup = pass.read(lighting.light_cluster_lookup);
939
+ data.light_cluster_data = pass.read(lighting.light_cluster_data);
940
+ data.shadowmap_atlas = pass.read(lighting.shadowmap_atlas);
941
+ data.volumetrics_lut = pass.read(lighting.volumetrics_lut);
942
+ data.volumetrics_metadata = pass.read(lighting.volumetrics_metadata);
943
+
944
+ pass.read(scene_color.color);
945
+ data.color = pass.write(scene_color.color);
946
+
947
+ scene_color.color = data.color;
948
+ }
949
+
950
+ destroy() {
951
+ this.#database.destroy();
952
+ this.#pool.destroy();
953
+ this.#grooms.clear();
954
+ this.#contexts.clear();
955
+ this.#row_limit = 0;
956
+ }
957
+
958
+ /**
959
+ * @param {number} width
960
+ * @param {number} height
961
+ * @param {number} point_capacity
962
+ */
963
+ #write_view(width, height, point_capacity) {
964
+ this.#view_f32[0] = width;
965
+ this.#view_f32[1] = height;
966
+ this.#view_f32[2] = this.pixel_floor;
967
+ this.#view_u32[3] = point_capacity;
968
+ }
969
+
970
+ /**
971
+ * Make the frame's buffers hold what the frames before asked for, within what a binding can hold.
972
+ */
973
+ #grow() {
974
+ const limits = this.#graphics.device.limits;
975
+ const binding_bytes = Math.min(limits.maxStorageBufferBindingSize, limits.maxBufferSize);
976
+
977
+ // both halves of the deformed points in one binding
978
+ const point_limit = Math.min(HAIR_POINT_CAPACITY_LIMIT, Math.floor(binding_bytes / 32));
979
+ const list_limit = Math.floor(binding_bytes / 4);
980
+
981
+ while (this.#points_asked > this.#point_capacity && this.#point_capacity < point_limit) {
982
+ this.#point_capacity = Math.min(this.#point_capacity * 2, point_limit);
983
+ }
984
+
985
+ while (this.#strands_asked > this.#list_capacity && this.#list_capacity < list_limit) {
986
+ this.#list_capacity = Math.min(this.#list_capacity * 2, list_limit);
987
+ }
988
+
989
+ while (this.#voxels_asked > this.#voxel_capacity && this.#voxel_capacity < list_limit) {
990
+ this.#voxel_capacity = Math.min(this.#voxel_capacity * 2, list_limit);
991
+ }
992
+ }
993
+
994
+ /**
995
+ * @param {GPUHairContext} context
996
+ */
997
+ #restage(context) {
998
+ const entry = this.#grooms.get(context.groom);
999
+ const table = this.table;
1000
+
1001
+ for (let g = 0; g < context.rows.length; g++) {
1002
+ table.set(context.rows[g], this.#write_record(context, entry, g));
1003
+ }
1004
+ }
1005
+
1006
+ /**
1007
+ * @param {GPUHairContext} context
1008
+ * @param {GroomEntry} entry
1009
+ * @param {number} g the group
1010
+ * @returns {Object} the record, for the table to copy
1011
+ */
1012
+ #write_record(context, entry, g) {
1013
+ const group = context.groom.groups[g];
1014
+ const material = context.material;
1015
+ const record = this.#record;
1016
+
1017
+ assert.lessThanOrEqual(group.level_count, 8, 'a group of more than 129 points');
1018
+
1019
+ record.node = context.node;
1020
+ record.points = entry.offsets[g * 2];
1021
+ record.strands = entry.offsets[g * 2 + 1];
1022
+ record.strand_count = group.strand_count;
1023
+ record.point_count = group.point_count;
1024
+ record.level_count = group.level_count;
1025
+ record.melanin = material.melanin;
1026
+ record.redness = material.redness;
1027
+
1028
+ const bounds = group.bounds;
1029
+
1030
+ record.bounds_min[0] = bounds[0];
1031
+ record.bounds_min[1] = bounds[1];
1032
+ record.bounds_min[2] = bounds[2];
1033
+ record.bounds_extent[0] = bounds[3] - bounds[0];
1034
+ record.bounds_extent[1] = bounds[4] - bounds[1];
1035
+ record.bounds_extent[2] = bounds[5] - bounds[2];
1036
+
1037
+ record.width_max = group.width_max;
1038
+ record.width_mean = group.width_mean;
1039
+
1040
+ const decimation = group.decimation;
1041
+
1042
+ for (let i = 0; i < 4; i++) {
1043
+ record.decimation_low[i] = i < decimation.length ? decimation[i] : 0;
1044
+ record.decimation_high[i] = i + 4 < decimation.length ? decimation[i + 4] : 0;
1045
+ }
1046
+
1047
+ record.dye.set(material.dye);
1048
+ record.roughness = material.roughness;
1049
+ record.root_spacing = group.root_spacing;
1050
+
1051
+ return record;
1052
+ }
1053
+
1054
+ /**
1055
+ * The groom on the device, uploaded on first use.
1056
+ *
1057
+ * @param {Groom} groom
1058
+ * @returns {GroomEntry|null}
1059
+ */
1060
+ #acquire_groom(groom) {
1061
+ let entry = this.#grooms.get(groom);
1062
+
1063
+ if (entry !== undefined) {
1064
+ entry.references++;
1065
+
1066
+ return entry;
1067
+ }
1068
+
1069
+ const groups = groom.groups;
1070
+
1071
+ let words = 0;
1072
+
1073
+ for (let g = 0; g < groups.length; g++) {
1074
+ const group = groups[g];
1075
+
1076
+ words += group.strand_count * group.point_count * GROOM_POINT_WORDS + group.strand_count;
1077
+ }
1078
+
1079
+ const run = this.#pool.allocate(words);
1080
+
1081
+ if (run.offset === ALLOCATOR_NO_SPACE) {
1082
+ return null;
1083
+ }
1084
+
1085
+ entry = new GroomEntry();
1086
+ entry.run = run;
1087
+ entry.offsets = new Uint32Array(groups.length * 2);
1088
+ entry.references = 1;
1089
+
1090
+ let at = run.offset;
1091
+
1092
+ for (let g = 0; g < groups.length; g++) {
1093
+ const group = groups[g];
1094
+
1095
+ entry.offsets[g * 2] = at;
1096
+ this.#pool.write(at, group.points, group.points.length);
1097
+ at += group.points.length;
1098
+
1099
+ const strands = new Uint32Array(group.strand_count);
1100
+
1101
+ for (let s = 0; s < group.strand_count; s++) {
1102
+ strands[s] = group.variation[s];
1103
+ }
1104
+
1105
+ entry.offsets[g * 2 + 1] = at;
1106
+ this.#pool.write(at, strands, strands.length);
1107
+ at += strands.length;
1108
+ }
1109
+
1110
+ this.#grooms.set(groom, entry);
1111
+
1112
+ return entry;
1113
+ }
1114
+
1115
+ /**
1116
+ * @param {Groom} groom
1117
+ */
1118
+ #release_groom(groom) {
1119
+ const entry = this.#grooms.get(groom);
1120
+
1121
+ entry.references--;
1122
+
1123
+ if (entry.references > 0) {
1124
+ return;
1125
+ }
1126
+
1127
+ this.#pool.free(entry.run);
1128
+ this.#grooms.delete(groom);
1129
+ }
1130
+ }
1131
+
1132
+ assert.lessThan(HAIR_ROW_BITS, 32, 'a tag holds a row and a byte');