@woosh/meep-engine 3.9.0 → 3.11.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 (832) hide show
  1. package/README.md +168 -152
  2. package/build/bundle-worker-image-decoder.js +1 -1
  3. package/build/bundle-worker-terrain.js +1 -1
  4. package/package.json +3 -2
  5. package/src/REVIEW_2026_08_06.md +610 -610
  6. package/src/avif/encode_image_source.d.ts +70 -0
  7. package/src/avif/encode_image_source.d.ts.map +1 -0
  8. package/src/avif/encode_image_source.js +117 -0
  9. package/src/avif/encoder_worker.js +15 -126
  10. package/src/avif/index.d.ts +4 -2
  11. package/src/avif/index.js +21 -2
  12. package/src/avif/native/DECISIONS.md +537 -0
  13. package/src/avif/native/NOTICE.md +61 -0
  14. package/src/avif/native/api/AvifDecoder.d.ts +67 -0
  15. package/src/avif/native/api/AvifDecoder.d.ts.map +1 -0
  16. package/src/avif/native/api/AvifDecoder.js +121 -0
  17. package/src/avif/native/api/apply_transformations.d.ts +30 -0
  18. package/src/avif/native/api/apply_transformations.d.ts.map +1 -0
  19. package/src/avif/native/api/apply_transformations.js +120 -0
  20. package/src/avif/native/api/avif_to_sampler2d.d.ts +25 -0
  21. package/src/avif/native/api/avif_to_sampler2d.d.ts.map +1 -0
  22. package/src/avif/native/api/avif_to_sampler2d.js +42 -0
  23. package/src/avif/native/api/convert_to_rgba.d.ts +54 -0
  24. package/src/avif/native/api/convert_to_rgba.d.ts.map +1 -0
  25. package/src/avif/native/api/convert_to_rgba.js +204 -0
  26. package/src/avif/native/api/decode_avif.d.ts +24 -0
  27. package/src/avif/native/api/decode_avif.d.ts.map +1 -0
  28. package/src/avif/native/api/decode_avif.js +111 -0
  29. package/src/avif/native/api/decode_image_item.d.ts +18 -0
  30. package/src/avif/native/api/decode_image_item.d.ts.map +1 -0
  31. package/src/avif/native/api/decode_image_item.js +234 -0
  32. package/src/avif/native/api/encode_avif.d.ts +73 -0
  33. package/src/avif/native/api/encode_avif.d.ts.map +1 -0
  34. package/src/avif/native/api/encode_avif.js +488 -0
  35. package/src/avif/native/api/sampler2d_to_avif.d.ts +22 -0
  36. package/src/avif/native/api/sampler2d_to_avif.d.ts.map +1 -0
  37. package/src/avif/native/api/sampler2d_to_avif.js +108 -0
  38. package/src/avif/native/av1/decode/Av1FrameContext.d.ts +286 -0
  39. package/src/avif/native/av1/decode/Av1FrameContext.d.ts.map +1 -0
  40. package/src/avif/native/av1/decode/Av1FrameContext.js +622 -0
  41. package/src/avif/native/av1/decode/block_decoded.d.ts +27 -0
  42. package/src/avif/native/av1/decode/block_decoded.d.ts.map +1 -0
  43. package/src/avif/native/av1/decode/block_decoded.js +59 -0
  44. package/src/avif/native/av1/decode/decode_av1_still.d.ts +22 -0
  45. package/src/avif/native/av1/decode/decode_av1_still.d.ts.map +1 -0
  46. package/src/avif/native/av1/decode/decode_av1_still.js +309 -0
  47. package/src/avif/native/av1/decode/decode_coefficients.d.ts +20 -0
  48. package/src/avif/native/av1/decode/decode_coefficients.d.ts.map +1 -0
  49. package/src/avif/native/av1/decode/decode_coefficients.js +259 -0
  50. package/src/avif/native/av1/decode/decode_palette.d.ts +28 -0
  51. package/src/avif/native/av1/decode/decode_palette.d.ts.map +1 -0
  52. package/src/avif/native/av1/decode/decode_palette.js +442 -0
  53. package/src/avif/native/av1/decode/decode_tile.d.ts +14 -0
  54. package/src/avif/native/av1/decode/decode_tile.d.ts.map +1 -0
  55. package/src/avif/native/av1/decode/decode_tile.js +1213 -0
  56. package/src/avif/native/av1/decode/get_scan.d.ts +16 -0
  57. package/src/avif/native/av1/decode/get_scan.d.ts.map +1 -0
  58. package/src/avif/native/av1/decode/get_scan.js +138 -0
  59. package/src/avif/native/av1/decode/read_lr.d.ts +35 -0
  60. package/src/avif/native/av1/decode/read_lr.d.ts.map +1 -0
  61. package/src/avif/native/av1/decode/read_lr.js +277 -0
  62. package/src/avif/native/av1/decode/read_transform_type.d.ts +19 -0
  63. package/src/avif/native/av1/decode/read_transform_type.d.ts.map +1 -0
  64. package/src/avif/native/av1/decode/read_transform_type.js +92 -0
  65. package/src/avif/native/av1/decode/reconstruct.d.ts +36 -0
  66. package/src/avif/native/av1/decode/reconstruct.d.ts.map +1 -0
  67. package/src/avif/native/av1/decode/reconstruct.js +241 -0
  68. package/src/avif/native/av1/decode/transform_type.d.ts +49 -0
  69. package/src/avif/native/av1/decode/transform_type.d.ts.map +1 -0
  70. package/src/avif/native/av1/decode/transform_type.js +137 -0
  71. package/src/avif/native/av1/encode/Av1BlockSnapshot.d.ts +81 -0
  72. package/src/avif/native/av1/encode/Av1BlockSnapshot.d.ts.map +1 -0
  73. package/src/avif/native/av1/encode/Av1BlockSnapshot.js +299 -0
  74. package/src/avif/native/av1/encode/Av1EncodeContext.d.ts +384 -0
  75. package/src/avif/native/av1/encode/Av1EncodeContext.d.ts.map +1 -0
  76. package/src/avif/native/av1/encode/Av1EncodeContext.js +590 -0
  77. package/src/avif/native/av1/encode/build_headers.d.ts +50 -0
  78. package/src/avif/native/av1/encode/build_headers.d.ts.map +1 -0
  79. package/src/avif/native/av1/encode/build_headers.js +359 -0
  80. package/src/avif/native/av1/encode/choose_chroma_alpha.d.ts +26 -0
  81. package/src/avif/native/av1/encode/choose_chroma_alpha.d.ts.map +1 -0
  82. package/src/avif/native/av1/encode/choose_chroma_alpha.js +90 -0
  83. package/src/avif/native/av1/encode/choose_filters.d.ts +21 -0
  84. package/src/avif/native/av1/encode/choose_filters.d.ts.map +1 -0
  85. package/src/avif/native/av1/encode/choose_filters.js +318 -0
  86. package/src/avif/native/av1/encode/choose_restoration.d.ts +35 -0
  87. package/src/avif/native/av1/encode/choose_restoration.d.ts.map +1 -0
  88. package/src/avif/native/av1/encode/choose_restoration.js +616 -0
  89. package/src/avif/native/av1/encode/decode_effort.d.ts +57 -0
  90. package/src/avif/native/av1/encode/decode_effort.d.ts.map +1 -0
  91. package/src/avif/native/av1/encode/decode_effort.js +59 -0
  92. package/src/avif/native/av1/encode/encode_av1_still.d.ts +17 -0
  93. package/src/avif/native/av1/encode/encode_av1_still.d.ts.map +1 -0
  94. package/src/avif/native/av1/encode/encode_av1_still.js +194 -0
  95. package/src/avif/native/av1/encode/encode_effort.d.ts +56 -0
  96. package/src/avif/native/av1/encode/encode_effort.d.ts.map +1 -0
  97. package/src/avif/native/av1/encode/encode_effort.js +97 -0
  98. package/src/avif/native/av1/encode/encode_tile.d.ts +26 -0
  99. package/src/avif/native/av1/encode/encode_tile.d.ts.map +1 -0
  100. package/src/avif/native/av1/encode/encode_tile.js +2331 -0
  101. package/src/avif/native/av1/encode/quantise.d.ts +51 -0
  102. package/src/avif/native/av1/encode/quantise.d.ts.map +1 -0
  103. package/src/avif/native/av1/encode/quantise.js +212 -0
  104. package/src/avif/native/av1/encode/write_coefficients.d.ts +42 -0
  105. package/src/avif/native/av1/encode/write_coefficients.d.ts.map +1 -0
  106. package/src/avif/native/av1/encode/write_coefficients.js +367 -0
  107. package/src/avif/native/av1/encode/write_lr.d.ts +33 -0
  108. package/src/avif/native/av1/encode/write_lr.d.ts.map +1 -0
  109. package/src/avif/native/av1/encode/write_lr.js +174 -0
  110. package/src/avif/native/av1/encode/write_transform_type.d.ts +21 -0
  111. package/src/avif/native/av1/encode/write_transform_type.d.ts.map +1 -0
  112. package/src/avif/native/av1/encode/write_transform_type.js +70 -0
  113. package/src/avif/native/av1/entropy/CdfContext.d.ts +167 -0
  114. package/src/avif/native/av1/entropy/CdfContext.d.ts.map +1 -0
  115. package/src/avif/native/av1/entropy/CdfContext.js +433 -0
  116. package/src/avif/native/av1/entropy/SymbolReader.d.ts +103 -0
  117. package/src/avif/native/av1/entropy/SymbolReader.d.ts.map +1 -0
  118. package/src/avif/native/av1/entropy/SymbolReader.js +317 -0
  119. package/src/avif/native/av1/entropy/SymbolWriter.d.ts +107 -0
  120. package/src/avif/native/av1/entropy/SymbolWriter.d.ts.map +1 -0
  121. package/src/avif/native/av1/entropy/SymbolWriter.js +352 -0
  122. package/src/avif/native/av1/entropy/coefficient_context.d.ts +71 -0
  123. package/src/avif/native/av1/entropy/coefficient_context.d.ts.map +1 -0
  124. package/src/avif/native/av1/entropy/coefficient_context.js +325 -0
  125. package/src/avif/native/av1/entropy/partition_cdf.d.ts +54 -0
  126. package/src/avif/native/av1/entropy/partition_cdf.d.ts.map +1 -0
  127. package/src/avif/native/av1/entropy/partition_cdf.js +98 -0
  128. package/src/avif/native/av1/entropy/symbol_cost.d.ts +37 -0
  129. package/src/avif/native/av1/entropy/symbol_cost.d.ts.map +1 -0
  130. package/src/avif/native/av1/entropy/symbol_cost.js +76 -0
  131. package/src/avif/native/av1/filter/cdef.d.ts +15 -0
  132. package/src/avif/native/av1/filter/cdef.d.ts.map +1 -0
  133. package/src/avif/native/av1/filter/cdef.js +313 -0
  134. package/src/avif/native/av1/filter/loop_filter.d.ts +14 -0
  135. package/src/avif/native/av1/filter/loop_filter.d.ts.map +1 -0
  136. package/src/avif/native/av1/filter/loop_filter.js +377 -0
  137. package/src/avif/native/av1/filter/loop_restoration.d.ts +42 -0
  138. package/src/avif/native/av1/filter/loop_restoration.d.ts.map +1 -0
  139. package/src/avif/native/av1/filter/loop_restoration.js +451 -0
  140. package/src/avif/native/av1/filter/superres.d.ts +18 -0
  141. package/src/avif/native/av1/filter/superres.d.ts.map +1 -0
  142. package/src/avif/native/av1/filter/superres.js +86 -0
  143. package/src/avif/native/av1/grain/FilmGrainState.d.ts +62 -0
  144. package/src/avif/native/av1/grain/FilmGrainState.d.ts.map +1 -0
  145. package/src/avif/native/av1/grain/FilmGrainState.js +87 -0
  146. package/src/avif/native/av1/grain/film_grain.d.ts +16 -0
  147. package/src/avif/native/av1/grain/film_grain.d.ts.map +1 -0
  148. package/src/avif/native/av1/grain/film_grain.js +623 -0
  149. package/src/avif/native/av1/obu/FrameHeader.d.ts +248 -0
  150. package/src/avif/native/av1/obu/FrameHeader.d.ts.map +1 -0
  151. package/src/avif/native/av1/obu/FrameHeader.js +381 -0
  152. package/src/avif/native/av1/obu/ObuHeader.d.ts +53 -0
  153. package/src/avif/native/av1/obu/ObuHeader.d.ts.map +1 -0
  154. package/src/avif/native/av1/obu/ObuHeader.js +62 -0
  155. package/src/avif/native/av1/obu/SequenceHeader.d.ts +157 -0
  156. package/src/avif/native/av1/obu/SequenceHeader.d.ts.map +1 -0
  157. package/src/avif/native/av1/obu/SequenceHeader.js +225 -0
  158. package/src/avif/native/av1/obu/for_each_obu.d.ts +24 -0
  159. package/src/avif/native/av1/obu/for_each_obu.d.ts.map +1 -0
  160. package/src/avif/native/av1/obu/for_each_obu.js +50 -0
  161. package/src/avif/native/av1/obu/parse_frame_header.d.ts +18 -0
  162. package/src/avif/native/av1/obu/parse_frame_header.d.ts.map +1 -0
  163. package/src/avif/native/av1/obu/parse_frame_header.js +841 -0
  164. package/src/avif/native/av1/obu/parse_obu_header.d.ts +23 -0
  165. package/src/avif/native/av1/obu/parse_obu_header.d.ts.map +1 -0
  166. package/src/avif/native/av1/obu/parse_obu_header.js +69 -0
  167. package/src/avif/native/av1/obu/parse_sequence_header.d.ts +17 -0
  168. package/src/avif/native/av1/obu/parse_sequence_header.d.ts.map +1 -0
  169. package/src/avif/native/av1/obu/parse_sequence_header.js +263 -0
  170. package/src/avif/native/av1/obu/write_frame_header.d.ts +16 -0
  171. package/src/avif/native/av1/obu/write_frame_header.d.ts.map +1 -0
  172. package/src/avif/native/av1/obu/write_frame_header.js +579 -0
  173. package/src/avif/native/av1/obu/write_sequence_header.d.ts +17 -0
  174. package/src/avif/native/av1/obu/write_sequence_header.d.ts.map +1 -0
  175. package/src/avif/native/av1/obu/write_sequence_header.js +189 -0
  176. package/src/avif/native/av1/predict/IntraPredictionState.d.ts +49 -0
  177. package/src/avif/native/av1/predict/IntraPredictionState.d.ts.map +1 -0
  178. package/src/avif/native/av1/predict/IntraPredictionState.js +58 -0
  179. package/src/avif/native/av1/predict/intra_filter_type.d.ts +17 -0
  180. package/src/avif/native/av1/predict/intra_filter_type.d.ts.map +1 -0
  181. package/src/avif/native/av1/predict/intra_filter_type.js +79 -0
  182. package/src/avif/native/av1/predict/predict_chroma_from_luma.d.ts +50 -0
  183. package/src/avif/native/av1/predict/predict_chroma_from_luma.d.ts.map +1 -0
  184. package/src/avif/native/av1/predict/predict_chroma_from_luma.js +153 -0
  185. package/src/avif/native/av1/predict/predict_intra.d.ts +42 -0
  186. package/src/avif/native/av1/predict/predict_intra.d.ts.map +1 -0
  187. package/src/avif/native/av1/predict/predict_intra.js +803 -0
  188. package/src/avif/native/av1/predict/predict_palette.d.ts +23 -0
  189. package/src/avif/native/av1/predict/predict_palette.d.ts.map +1 -0
  190. package/src/avif/native/av1/predict/predict_palette.js +36 -0
  191. package/src/avif/native/av1/tables/av1_symbols.d.ts +411 -0
  192. package/src/avif/native/av1/tables/av1_symbols.d.ts.map +1 -0
  193. package/src/avif/native/av1/tables/av1_symbols.js +419 -0
  194. package/src/avif/native/av1/tables/block_tables.d.ts +181 -0
  195. package/src/avif/native/av1/tables/block_tables.d.ts.map +1 -0
  196. package/src/avif/native/av1/tables/block_tables.js +270 -0
  197. package/src/avif/native/av1/tables/cdf_tables.d.ts +961 -0
  198. package/src/avif/native/av1/tables/cdf_tables.d.ts.map +1 -0
  199. package/src/avif/native/av1/tables/cdf_tables.js +1893 -0
  200. package/src/avif/native/av1/tables/coefficient_tables.d.ts +41 -0
  201. package/src/avif/native/av1/tables/coefficient_tables.d.ts.map +1 -0
  202. package/src/avif/native/av1/tables/coefficient_tables.js +82 -0
  203. package/src/avif/native/av1/tables/derived_block_tables.d.ts +15 -0
  204. package/src/avif/native/av1/tables/derived_block_tables.d.ts.map +1 -0
  205. package/src/avif/native/av1/tables/derived_block_tables.js +23 -0
  206. package/src/avif/native/av1/tables/filter_tables.d.ts +171 -0
  207. package/src/avif/native/av1/tables/filter_tables.d.ts.map +1 -0
  208. package/src/avif/native/av1/tables/filter_tables.js +313 -0
  209. package/src/avif/native/av1/tables/grain_tables.d.ts +11 -0
  210. package/src/avif/native/av1/tables/grain_tables.d.ts.map +1 -0
  211. package/src/avif/native/av1/tables/grain_tables.js +190 -0
  212. package/src/avif/native/av1/tables/prediction_tables.d.ts +141 -0
  213. package/src/avif/native/av1/tables/prediction_tables.d.ts.map +1 -0
  214. package/src/avif/native/av1/tables/prediction_tables.js +223 -0
  215. package/src/avif/native/av1/tables/quantizer_matrix_tables.d.ts +21 -0
  216. package/src/avif/native/av1/tables/quantizer_matrix_tables.d.ts.map +1 -0
  217. package/src/avif/native/av1/tables/quantizer_matrix_tables.js +38 -0
  218. package/src/avif/native/av1/tables/quantizer_tables.d.ts +21 -0
  219. package/src/avif/native/av1/tables/quantizer_tables.d.ts.map +1 -0
  220. package/src/avif/native/av1/tables/quantizer_tables.js +160 -0
  221. package/src/avif/native/av1/tables/scan_tables.d.ts +321 -0
  222. package/src/avif/native/av1/tables/scan_tables.d.ts.map +1 -0
  223. package/src/avif/native/av1/tables/scan_tables.js +727 -0
  224. package/src/avif/native/av1/tables/segmentation_tables.d.ts +31 -0
  225. package/src/avif/native/av1/tables/segmentation_tables.d.ts.map +1 -0
  226. package/src/avif/native/av1/tables/segmentation_tables.js +48 -0
  227. package/src/avif/native/av1/tables/transform_tables.d.ts +91 -0
  228. package/src/avif/native/av1/tables/transform_tables.d.ts.map +1 -0
  229. package/src/avif/native/av1/tables/transform_tables.js +140 -0
  230. package/src/avif/native/av1/tables/unpack_table.d.ts +33 -0
  231. package/src/avif/native/av1/tables/unpack_table.d.ts.map +1 -0
  232. package/src/avif/native/av1/tables/unpack_table.js +73 -0
  233. package/src/avif/native/av1/transform/forward_transform_2d.d.ts +61 -0
  234. package/src/avif/native/av1/transform/forward_transform_2d.d.ts.map +1 -0
  235. package/src/avif/native/av1/transform/forward_transform_2d.js +574 -0
  236. package/src/avif/native/av1/transform/inverse_transform_1d.d.ts +76 -0
  237. package/src/avif/native/av1/transform/inverse_transform_1d.d.ts.map +1 -0
  238. package/src/avif/native/av1/transform/inverse_transform_1d.js +590 -0
  239. package/src/avif/native/av1/transform/inverse_transform_2d.d.ts +46 -0
  240. package/src/avif/native/av1/transform/inverse_transform_2d.d.ts.map +1 -0
  241. package/src/avif/native/av1/transform/inverse_transform_2d.js +180 -0
  242. package/src/avif/native/av1/util/ceil_log2.d.ts +13 -0
  243. package/src/avif/native/av1/util/ceil_log2.d.ts.map +1 -0
  244. package/src/avif/native/av1/util/ceil_log2.js +18 -0
  245. package/src/avif/native/av1/util/floor_log2.d.ts +20 -0
  246. package/src/avif/native/av1/util/floor_log2.d.ts.map +1 -0
  247. package/src/avif/native/av1/util/floor_log2.js +20 -0
  248. package/src/avif/native/av1/util/round2.d.ts +22 -0
  249. package/src/avif/native/av1/util/round2.d.ts.map +1 -0
  250. package/src/avif/native/av1/util/round2.js +27 -0
  251. package/src/avif/native/av1/util/round2_signed.d.ts +14 -0
  252. package/src/avif/native/av1/util/round2_signed.d.ts.map +1 -0
  253. package/src/avif/native/av1/util/round2_signed.js +17 -0
  254. package/src/avif/native/av1/util/tile_log2.d.ts +16 -0
  255. package/src/avif/native/av1/util/tile_log2.d.ts.map +1 -0
  256. package/src/avif/native/av1/util/tile_log2.js +23 -0
  257. package/src/avif/native/bitstream/BitReader.d.ts +174 -0
  258. package/src/avif/native/bitstream/BitReader.d.ts.map +1 -0
  259. package/src/avif/native/bitstream/BitReader.js +391 -0
  260. package/src/avif/native/bitstream/BitWriter.d.ts +122 -0
  261. package/src/avif/native/bitstream/BitWriter.d.ts.map +1 -0
  262. package/src/avif/native/bitstream/BitWriter.js +351 -0
  263. package/src/avif/native/color/ColourTransform.d.ts +62 -0
  264. package/src/avif/native/color/ColourTransform.d.ts.map +1 -0
  265. package/src/avif/native/color/ColourTransform.js +189 -0
  266. package/src/avif/native/color/YuvImage.d.ts +51 -0
  267. package/src/avif/native/color/YuvImage.d.ts.map +1 -0
  268. package/src/avif/native/color/YuvImage.js +66 -0
  269. package/src/avif/native/color/clamp_sample.d.ts +18 -0
  270. package/src/avif/native/color/clamp_sample.d.ts.map +1 -0
  271. package/src/avif/native/color/clamp_sample.js +25 -0
  272. package/src/avif/native/color/linear_to_transfer.d.ts +24 -0
  273. package/src/avif/native/color/linear_to_transfer.d.ts.map +1 -0
  274. package/src/avif/native/color/linear_to_transfer.js +126 -0
  275. package/src/avif/native/color/primaries.d.ts +20 -0
  276. package/src/avif/native/color/primaries.d.ts.map +1 -0
  277. package/src/avif/native/color/primaries.js +94 -0
  278. package/src/avif/native/color/rgb_row_to_ycbcr.d.ts +20 -0
  279. package/src/avif/native/color/rgb_row_to_ycbcr.d.ts.map +1 -0
  280. package/src/avif/native/color/rgb_row_to_ycbcr.js +78 -0
  281. package/src/avif/native/color/transfer_to_linear.d.ts +49 -0
  282. package/src/avif/native/color/transfer_to_linear.d.ts.map +1 -0
  283. package/src/avif/native/color/transfer_to_linear.js +171 -0
  284. package/src/avif/native/color/upsample_chroma_row.d.ts +35 -0
  285. package/src/avif/native/color/upsample_chroma_row.d.ts.map +1 -0
  286. package/src/avif/native/color/upsample_chroma_row.js +89 -0
  287. package/src/avif/native/color/ycbcr_row_to_rgb.d.ts +23 -0
  288. package/src/avif/native/color/ycbcr_row_to_rgb.d.ts.map +1 -0
  289. package/src/avif/native/color/ycbcr_row_to_rgb.js +78 -0
  290. package/src/avif/native/heif/AvifFile.d.ts +112 -0
  291. package/src/avif/native/heif/AvifFile.d.ts.map +1 -0
  292. package/src/avif/native/heif/AvifFile.js +142 -0
  293. package/src/avif/native/heif/ItemProperty.d.ts +244 -0
  294. package/src/avif/native/heif/ItemProperty.d.ts.map +1 -0
  295. package/src/avif/native/heif/ItemProperty.js +321 -0
  296. package/src/avif/native/heif/find_item_property.d.ts +17 -0
  297. package/src/avif/native/heif/find_item_property.d.ts.map +1 -0
  298. package/src/avif/native/heif/find_item_property.js +28 -0
  299. package/src/avif/native/heif/find_item_references.d.ts +16 -0
  300. package/src/avif/native/heif/find_item_references.d.ts.map +1 -0
  301. package/src/avif/native/heif/find_item_references.js +29 -0
  302. package/src/avif/native/heif/find_items_referencing.d.ts +17 -0
  303. package/src/avif/native/heif/find_items_referencing.d.ts.map +1 -0
  304. package/src/avif/native/heif/find_items_referencing.js +36 -0
  305. package/src/avif/native/heif/parse_avif_file.d.ts +15 -0
  306. package/src/avif/native/heif/parse_avif_file.d.ts.map +1 -0
  307. package/src/avif/native/heif/parse_avif_file.js +383 -0
  308. package/src/avif/native/heif/parse_image_grid.d.ts +18 -0
  309. package/src/avif/native/heif/parse_image_grid.d.ts.map +1 -0
  310. package/src/avif/native/heif/parse_image_grid.js +48 -0
  311. package/src/avif/native/heif/parse_item_property.d.ts +18 -0
  312. package/src/avif/native/heif/parse_item_property.d.ts.map +1 -0
  313. package/src/avif/native/heif/parse_item_property.js +243 -0
  314. package/src/avif/native/heif/read_item_data.d.ts +22 -0
  315. package/src/avif/native/heif/read_item_data.d.ts.map +1 -0
  316. package/src/avif/native/heif/read_item_data.js +97 -0
  317. package/src/avif/native/heif/write_avif_file.d.ts +50 -0
  318. package/src/avif/native/heif/write_avif_file.d.ts.map +1 -0
  319. package/src/avif/native/heif/write_avif_file.js +348 -0
  320. package/src/avif/native/heif/write_item_property.d.ts +16 -0
  321. package/src/avif/native/heif/write_item_property.d.ts.map +1 -0
  322. package/src/avif/native/heif/write_item_property.js +182 -0
  323. package/src/avif/native/index.d.ts +14 -0
  324. package/src/avif/native/index.d.ts.map +1 -0
  325. package/src/avif/native/index.js +56 -0
  326. package/src/avif/native/isobmff/BoxHeader.d.ts +50 -0
  327. package/src/avif/native/isobmff/BoxHeader.d.ts.map +1 -0
  328. package/src/avif/native/isobmff/BoxHeader.js +57 -0
  329. package/src/avif/native/isobmff/BoxWriter.d.ts +118 -0
  330. package/src/avif/native/isobmff/BoxWriter.d.ts.map +1 -0
  331. package/src/avif/native/isobmff/BoxWriter.js +264 -0
  332. package/src/avif/native/isobmff/ByteCursor.d.ts +125 -0
  333. package/src/avif/native/isobmff/ByteCursor.d.ts.map +1 -0
  334. package/src/avif/native/isobmff/ByteCursor.js +298 -0
  335. package/src/avif/native/isobmff/for_each_box.d.ts +23 -0
  336. package/src/avif/native/isobmff/for_each_box.d.ts.map +1 -0
  337. package/src/avif/native/isobmff/for_each_box.js +39 -0
  338. package/src/avif/native/isobmff/read_box_header.d.ts +22 -0
  339. package/src/avif/native/isobmff/read_box_header.d.ts.map +1 -0
  340. package/src/avif/native/isobmff/read_box_header.js +59 -0
  341. package/src/avif/native/isobmff/read_full_box_header.d.ts +26 -0
  342. package/src/avif/native/isobmff/read_full_box_header.d.ts.map +1 -0
  343. package/src/avif/native/isobmff/read_full_box_header.js +33 -0
  344. package/src/avif/threaded_image_encoder.d.ts +7 -2
  345. package/src/avif/threaded_image_encoder.d.ts.map +1 -1
  346. package/src/avif/threaded_image_encoder.js +107 -21
  347. package/src/core/binary/BinaryBuffer.d.ts +40 -0
  348. package/src/core/binary/BinaryBuffer.d.ts.map +1 -1
  349. package/src/core/binary/BinaryBuffer.js +100 -0
  350. package/src/core/binary/hash/crc32.d.ts +16 -0
  351. package/src/core/binary/hash/crc32.d.ts.map +1 -0
  352. package/src/core/binary/hash/crc32.js +96 -0
  353. package/src/core/binary/utf8/utf8_decode.d.ts +21 -0
  354. package/src/core/binary/utf8/utf8_decode.d.ts.map +1 -0
  355. package/src/core/binary/utf8/utf8_decode.js +43 -0
  356. package/src/core/binary/utf8/utf8_encode_into.d.ts +20 -0
  357. package/src/core/binary/utf8/utf8_encode_into.d.ts.map +1 -0
  358. package/src/core/binary/utf8/utf8_encode_into.js +48 -0
  359. package/src/core/binary/utf8/utf8_encoded_length.d.ts +14 -0
  360. package/src/core/binary/utf8/utf8_encoded_length.d.ts.map +1 -0
  361. package/src/core/binary/utf8/utf8_encoded_length.js +36 -0
  362. package/src/core/color/COLOR_FIX_PLAN_2026_08_28.md +417 -0
  363. package/src/core/color/COLOR_REVIEW_2026_08_28.md +779 -0
  364. package/src/core/color/Color.d.ts +128 -20
  365. package/src/core/color/Color.d.ts.map +1 -1
  366. package/src/core/color/Color.js +959 -866
  367. package/src/core/color/PQ/PQ_constants.d.ts +10 -0
  368. package/src/core/color/PQ/PQ_constants.d.ts.map +1 -1
  369. package/src/core/color/PQ/PQ_constants.js +10 -0
  370. package/src/core/color/PQ/linear_to_PQ.d.ts.map +1 -1
  371. package/src/core/color/PQ/linear_to_PQ.js +6 -0
  372. package/src/core/color/REC709_PRIMARIES.d.ts +50 -0
  373. package/src/core/color/REC709_PRIMARIES.d.ts.map +1 -0
  374. package/src/core/color/REC709_PRIMARIES.js +53 -0
  375. package/src/core/color/construct/color_from_hex.d.ts +21 -0
  376. package/src/core/color/construct/color_from_hex.d.ts.map +1 -0
  377. package/src/core/color/construct/color_from_hex.js +33 -0
  378. package/src/core/color/construct/color_from_temperature.d.ts +22 -0
  379. package/src/core/color/construct/color_from_temperature.d.ts.map +1 -0
  380. package/src/core/color/construct/color_from_temperature.js +33 -0
  381. package/src/core/color/construct/color_from_uint24.d.ts +19 -0
  382. package/src/core/color/construct/color_from_uint24.d.ts.map +1 -0
  383. package/src/core/color/construct/color_from_uint24.js +26 -0
  384. package/src/core/color/construct/color_from_uint32.d.ts +20 -0
  385. package/src/core/color/construct/color_from_uint32.d.ts.map +1 -0
  386. package/src/core/color/construct/color_from_uint32.js +39 -0
  387. package/src/core/color/construct/color_gray.d.ts +17 -0
  388. package/src/core/color/construct/color_gray.d.ts.map +1 -0
  389. package/src/core/color/construct/color_gray.js +22 -0
  390. package/src/core/color/hex/hex2rgb.d.ts +1 -11
  391. package/src/core/color/hex/hex2rgb.d.ts.map +1 -1
  392. package/src/core/color/hex/hex2rgb.js +7 -24
  393. package/src/core/color/hex/hex_to_rgb.d.ts +18 -0
  394. package/src/core/color/hex/hex_to_rgb.d.ts.map +1 -0
  395. package/src/core/color/hex/hex_to_rgb.js +62 -0
  396. package/src/core/color/hex/rgb2hex.d.ts +1 -8
  397. package/src/core/color/hex/rgb2hex.d.ts.map +1 -1
  398. package/src/core/color/hex/rgb2hex.js +7 -13
  399. package/src/core/color/hex/rgb_to_hex.d.ts +9 -0
  400. package/src/core/color/hex/rgb_to_hex.d.ts.map +1 -0
  401. package/src/core/color/hex/rgb_to_hex.js +13 -0
  402. package/src/core/color/hex/rgba_to_hex.d.ts +19 -0
  403. package/src/core/color/hex/rgba_to_hex.d.ts.map +1 -0
  404. package/src/core/color/hex/rgba_to_hex.js +23 -0
  405. package/src/core/color/hsv/hsv2rgb.d.ts +1 -12
  406. package/src/core/color/hsv/hsv2rgb.d.ts.map +1 -1
  407. package/src/core/color/hsv/hsv2rgb.js +8 -21
  408. package/src/core/color/hsv/hsv2rgb_float.d.ts +1 -12
  409. package/src/core/color/hsv/hsv2rgb_float.d.ts.map +1 -1
  410. package/src/core/color/hsv/hsv2rgb_float.js +8 -56
  411. package/src/core/color/hsv/hsv_to_rgb.d.ts +13 -0
  412. package/src/core/color/hsv/hsv_to_rgb.d.ts.map +1 -0
  413. package/src/core/color/hsv/hsv_to_rgb.js +39 -0
  414. package/src/core/color/hsv/hsv_to_rgb_uint8.d.ts +13 -0
  415. package/src/core/color/hsv/hsv_to_rgb_uint8.d.ts.map +1 -0
  416. package/src/core/color/hsv/hsv_to_rgb_uint8.js +21 -0
  417. package/src/core/color/hsv/hue_to_rgb_sextant.d.ts +24 -0
  418. package/src/core/color/hsv/hue_to_rgb_sextant.d.ts.map +1 -0
  419. package/src/core/color/hsv/hue_to_rgb_sextant.js +67 -0
  420. package/src/core/color/hsv/rgb2hsv.d.ts +1 -12
  421. package/src/core/color/hsv/rgb2hsv.d.ts.map +1 -1
  422. package/src/core/color/hsv/rgb2hsv.js +7 -42
  423. package/src/core/color/hsv/rgb_to_hsv.d.ts +13 -0
  424. package/src/core/color/hsv/rgb_to_hsv.d.ts.map +1 -0
  425. package/src/core/color/hsv/rgb_to_hsv.js +42 -0
  426. package/src/core/color/hunt/hpe_to_xyz.d.ts +2 -0
  427. package/src/core/color/hunt/hpe_to_xyz.d.ts.map +1 -0
  428. package/src/core/color/hunt/hpe_to_xyz.js +4 -0
  429. package/src/core/color/hunt/xyz_to_hpe.d.ts +1 -6
  430. package/src/core/color/hunt/xyz_to_hpe.d.ts.map +1 -1
  431. package/src/core/color/hunt/xyz_to_hpe.js +12 -15
  432. package/src/core/color/illuminant/D65_spd_tabulated.d.ts.map +1 -1
  433. package/src/core/color/illuminant/D65_spd_tabulated.js +107 -104
  434. package/src/core/color/int2rgb.d.ts +1 -10
  435. package/src/core/color/int2rgb.d.ts.map +1 -1
  436. package/src/core/color/int2rgb.js +7 -12
  437. package/src/core/color/int_to_rgb.d.ts +21 -0
  438. package/src/core/color/int_to_rgb.d.ts.map +1 -0
  439. package/src/core/color/int_to_rgb.js +33 -0
  440. package/src/core/color/kelvin/kelvin_to_rgb.d.ts.map +1 -1
  441. package/src/core/color/kelvin/kelvin_to_rgb.js +22 -2
  442. package/src/core/color/kelvin/rgb_to_kelvin.d.ts +22 -2
  443. package/src/core/color/kelvin/rgb_to_kelvin.d.ts.map +1 -1
  444. package/src/core/color/kelvin/rgb_to_kelvin.js +22 -2
  445. package/src/core/color/lab/lab_to_xyz.d.ts +20 -0
  446. package/src/core/color/lab/lab_to_xyz.d.ts.map +1 -0
  447. package/src/core/color/lab/lab_to_xyz.js +69 -0
  448. package/src/core/color/oklab/gamut_clip_adaptive_l0.d.ts +34 -0
  449. package/src/core/color/oklab/gamut_clip_adaptive_l0.d.ts.map +1 -0
  450. package/src/core/color/oklab/gamut_clip_adaptive_l0.js +46 -0
  451. package/src/core/color/oklab/gamut_clip_at_l0.d.ts +33 -0
  452. package/src/core/color/oklab/gamut_clip_at_l0.d.ts.map +1 -0
  453. package/src/core/color/oklab/gamut_clip_at_l0.js +84 -0
  454. package/src/core/color/oklab/gamut_clip_preserve_chroma.d.ts +24 -0
  455. package/src/core/color/oklab/gamut_clip_preserve_chroma.d.ts.map +1 -0
  456. package/src/core/color/oklab/gamut_clip_preserve_chroma.js +37 -0
  457. package/src/core/color/oklab/gamut_clip_project_to_lcusp.d.ts +23 -0
  458. package/src/core/color/oklab/gamut_clip_project_to_lcusp.d.ts.map +1 -0
  459. package/src/core/color/oklab/gamut_clip_project_to_lcusp.js +46 -0
  460. package/src/core/color/oklab/linear_srgb_to_okhsl.d.ts +19 -0
  461. package/src/core/color/oklab/linear_srgb_to_okhsl.d.ts.map +1 -0
  462. package/src/core/color/oklab/linear_srgb_to_okhsl.js +93 -0
  463. package/src/core/color/oklab/linear_srgb_to_okhsv.d.ts +12 -4
  464. package/src/core/color/oklab/linear_srgb_to_okhsv.d.ts.map +1 -1
  465. package/src/core/color/oklab/linear_srgb_to_okhsv.js +108 -84
  466. package/src/core/color/oklab/linear_srgb_to_oklab.d.ts +13 -4
  467. package/src/core/color/oklab/linear_srgb_to_oklab.d.ts.map +1 -1
  468. package/src/core/color/oklab/linear_srgb_to_oklab.js +28 -19
  469. package/src/core/color/oklab/okhsl_chroma_bounds.d.ts +51 -0
  470. package/src/core/color/oklab/okhsl_chroma_bounds.d.ts.map +1 -0
  471. package/src/core/color/oklab/okhsl_chroma_bounds.js +128 -0
  472. package/src/core/color/oklab/okhsl_to_linear_srgb.d.ts +19 -0
  473. package/src/core/color/oklab/okhsl_to_linear_srgb.d.ts.map +1 -0
  474. package/src/core/color/oklab/okhsl_to_linear_srgb.js +85 -0
  475. package/src/core/color/oklab/oklab_to_oklch.d.ts +24 -0
  476. package/src/core/color/oklab/oklab_to_oklch.d.ts.map +1 -0
  477. package/src/core/color/oklab/oklab_to_oklch.js +35 -0
  478. package/src/core/color/oklab/oklch_to_oklab.d.ts +18 -0
  479. package/src/core/color/oklab/oklch_to_oklab.d.ts.map +1 -0
  480. package/src/core/color/oklab/oklch_to_oklab.js +26 -0
  481. package/src/core/color/oklab/toe.d.ts +12 -0
  482. package/src/core/color/oklab/toe.d.ts.map +1 -1
  483. package/src/core/color/oklab/toe.js +34 -22
  484. package/src/core/color/operations/color_add.d.ts +21 -0
  485. package/src/core/color/operations/color_add.d.ts.map +1 -0
  486. package/src/core/color/operations/color_add.js +23 -0
  487. package/src/core/color/operations/color_add_scaled.d.ts +20 -0
  488. package/src/core/color/operations/color_add_scaled.d.ts.map +1 -0
  489. package/src/core/color/operations/color_add_scaled.js +30 -0
  490. package/src/core/color/operations/color_clamp.d.ts +22 -0
  491. package/src/core/color/operations/color_clamp.d.ts.map +1 -0
  492. package/src/core/color/operations/color_clamp.js +25 -0
  493. package/src/core/color/operations/color_darken.d.ts +2 -1
  494. package/src/core/color/operations/color_darken.d.ts.map +1 -1
  495. package/src/core/color/operations/color_darken.js +25 -24
  496. package/src/core/color/operations/color_desaturate.d.ts +2 -1
  497. package/src/core/color/operations/color_desaturate.d.ts.map +1 -1
  498. package/src/core/color/operations/color_desaturate.js +25 -24
  499. package/src/core/color/operations/color_get_hsl.d.ts +18 -0
  500. package/src/core/color/operations/color_get_hsl.d.ts.map +1 -0
  501. package/src/core/color/operations/color_get_hsl.js +60 -0
  502. package/src/core/color/operations/color_is_in_gamut.d.ts +20 -0
  503. package/src/core/color/operations/color_is_in_gamut.d.ts.map +1 -0
  504. package/src/core/color/operations/color_is_in_gamut.js +30 -0
  505. package/src/core/color/operations/color_lerp.d.ts +12 -3
  506. package/src/core/color/operations/color_lerp.d.ts.map +1 -1
  507. package/src/core/color/operations/color_lerp.js +55 -46
  508. package/src/core/color/operations/color_lighten.d.ts +9 -5
  509. package/src/core/color/operations/color_lighten.d.ts.map +1 -1
  510. package/src/core/color/operations/color_lighten.js +29 -24
  511. package/src/core/color/operations/color_mix_okhsv_channel.d.ts +24 -0
  512. package/src/core/color/operations/color_mix_okhsv_channel.d.ts.map +1 -0
  513. package/src/core/color/operations/color_mix_okhsv_channel.js +48 -0
  514. package/src/core/color/operations/color_multiply_rgb.d.ts +21 -0
  515. package/src/core/color/operations/color_multiply_rgb.d.ts.map +1 -0
  516. package/src/core/color/operations/color_multiply_rgb.js +26 -0
  517. package/src/core/color/operations/color_saturate.d.ts +9 -5
  518. package/src/core/color/operations/color_saturate.d.ts.map +1 -1
  519. package/src/core/color/operations/color_saturate.js +29 -24
  520. package/src/core/color/operations/color_scale_okhsv_channel.d.ts +1 -1
  521. package/src/core/color/operations/color_scale_okhsv_channel.d.ts.map +1 -1
  522. package/src/core/color/operations/color_scale_okhsv_channel.js +58 -45
  523. package/src/core/color/operations/color_srgb_apply.d.ts +28 -0
  524. package/src/core/color/operations/color_srgb_apply.d.ts.map +1 -0
  525. package/src/core/color/operations/color_srgb_apply.js +40 -0
  526. package/src/core/color/operations/color_sub.d.ts +16 -0
  527. package/src/core/color/operations/color_sub.d.ts.map +1 -0
  528. package/src/core/color/operations/color_sub.js +18 -0
  529. package/src/core/color/parse_color.d.ts +10 -1
  530. package/src/core/color/parse_color.d.ts.map +1 -1
  531. package/src/core/color/parse_color.js +100 -84
  532. package/src/core/color/parse_color_normalized.d.ts +24 -0
  533. package/src/core/color/parse_color_normalized.d.ts.map +1 -0
  534. package/src/core/color/parse_color_normalized.js +38 -0
  535. package/src/core/color/rgb2uint24.d.ts +1 -8
  536. package/src/core/color/rgb2uint24.d.ts.map +1 -1
  537. package/src/core/color/rgb2uint24.js +7 -20
  538. package/src/core/color/rgb2uint32.d.ts +1 -9
  539. package/src/core/color/rgb2uint32.d.ts.map +1 -1
  540. package/src/core/color/rgb2uint32.js +7 -18
  541. package/src/core/color/rgb_to_luminance.d.ts +14 -5
  542. package/src/core/color/rgb_to_luminance.d.ts.map +1 -1
  543. package/src/core/color/rgb_to_luminance.js +24 -13
  544. package/src/core/color/rgb_to_uint24.d.ts +9 -0
  545. package/src/core/color/rgb_to_uint24.d.ts.map +1 -0
  546. package/src/core/color/rgb_to_uint24.js +20 -0
  547. package/src/core/color/rgb_to_uint32.d.ts +10 -0
  548. package/src/core/color/rgb_to_uint32.d.ts.map +1 -0
  549. package/src/core/color/rgb_to_uint32.js +21 -0
  550. package/src/core/color/sRGB/linear_to_sRGB.d.ts +7 -0
  551. package/src/core/color/sRGB/linear_to_sRGB.d.ts.map +1 -1
  552. package/src/core/color/sRGB/linear_to_sRGB.js +39 -32
  553. package/src/core/color/xyz/hpe_to_xyz.d.ts +24 -0
  554. package/src/core/color/xyz/hpe_to_xyz.d.ts.map +1 -0
  555. package/src/core/color/xyz/hpe_to_xyz.js +39 -0
  556. package/src/core/color/xyz/rgb_to_xyz.d.ts +17 -2
  557. package/src/core/color/xyz/rgb_to_xyz.d.ts.map +1 -1
  558. package/src/core/color/xyz/rgb_to_xyz.js +44 -18
  559. package/src/core/color/xyz/xyz_cmf_tabulated.d.ts.map +1 -1
  560. package/src/core/color/xyz/xyz_cmf_tabulated.js +217 -211
  561. package/src/core/color/xyz/xyz_cmf_wyman.d.ts +4 -0
  562. package/src/core/color/xyz/xyz_cmf_wyman.d.ts.map +1 -1
  563. package/src/core/color/xyz/xyz_cmf_wyman.js +74 -65
  564. package/src/core/color/xyz/xyz_to_hpe.d.ts +18 -0
  565. package/src/core/color/xyz/xyz_to_hpe.d.ts.map +1 -0
  566. package/src/core/color/xyz/xyz_to_hpe.js +33 -0
  567. package/src/core/color/xyz/xyz_to_rgb.d.ts +17 -2
  568. package/src/core/color/xyz/xyz_to_rgb.d.ts.map +1 -1
  569. package/src/core/color/xyz/xyz_to_rgb.js +39 -16
  570. package/src/core/color/ycxcz/xyz_to_ycxcz.d.ts +7 -2
  571. package/src/core/color/ycxcz/xyz_to_ycxcz.d.ts.map +1 -1
  572. package/src/core/color/ycxcz/xyz_to_ycxcz.js +33 -28
  573. package/src/core/events/signal/Signal.js +820 -820
  574. package/src/core/geom/3d/shape/ConvexHullShape3D.d.ts.map +1 -1
  575. package/src/core/geom/3d/shape/ConvexHullShape3D.js +28 -4
  576. package/src/core/math/clamp_index.d.ts +18 -0
  577. package/src/core/math/clamp_index.d.ts.map +1 -0
  578. package/src/core/math/clamp_index.js +23 -0
  579. package/src/engine/asset/loaders/image/IMAGE_CODEC_REVIEW_2026_08_28.md +803 -0
  580. package/src/engine/asset/loaders/image/jpeg/JpegFrame.d.ts +5 -3
  581. package/src/engine/asset/loaders/image/jpeg/JpegFrame.d.ts.map +1 -1
  582. package/src/engine/asset/loaders/image/jpeg/JpegFrame.js +5 -3
  583. package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.d.ts +2 -1
  584. package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.d.ts.map +1 -1
  585. package/src/engine/asset/loaders/image/jpeg/JpegFrameComponent.js +2 -1
  586. package/src/engine/asset/loaders/image/jpeg/JpegImage.d.ts +17 -0
  587. package/src/engine/asset/loaders/image/jpeg/JpegImage.d.ts.map +1 -1
  588. package/src/engine/asset/loaders/image/jpeg/JpegImage.js +197 -157
  589. package/src/engine/asset/loaders/image/jpeg/idct8x8_float.d.ts +0 -1
  590. package/src/engine/asset/loaders/image/jpeg/idct8x8_float.d.ts.map +1 -1
  591. package/src/engine/asset/loaders/image/jpeg/idct8x8_float.js +0 -1
  592. package/src/engine/asset/loaders/image/jpeg/jpeg_component_row.d.ts +28 -0
  593. package/src/engine/asset/loaders/image/jpeg/jpeg_component_row.d.ts.map +1 -0
  594. package/src/engine/asset/loaders/image/jpeg/jpeg_component_row.js +31 -0
  595. package/src/engine/asset/loaders/image/png/PNG.d.ts +3 -18
  596. package/src/engine/asset/loaders/image/png/PNG.d.ts.map +1 -1
  597. package/src/engine/asset/loaders/image/png/PNG.js +3 -139
  598. package/src/engine/asset/loaders/image/png/PNGReader.d.ts +16 -16
  599. package/src/engine/asset/loaders/image/png/PNGReader.d.ts.map +1 -1
  600. package/src/engine/asset/loaders/image/png/PNGReader.js +143 -160
  601. package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_iTXt.js +3 -3
  602. package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_zTXt.js +3 -3
  603. package/src/engine/asset/loaders/image/png/crc32.d.ts +1 -15
  604. package/src/engine/asset/loaders/image/png/crc32.d.ts.map +1 -1
  605. package/src/engine/asset/loaders/image/png/crc32.js +5 -89
  606. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.d.ts +12 -10
  607. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.d.ts.map +1 -1
  608. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.js +20 -45
  609. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.d.ts +2 -1
  610. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.d.ts.map +1 -1
  611. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.js +2 -0
  612. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.d.ts +2 -1
  613. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.d.ts.map +1 -1
  614. package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.js +2 -0
  615. package/src/engine/asset/loaders/image/png/filter/png_unfilter.d.ts +22 -0
  616. package/src/engine/asset/loaders/image/png/filter/png_unfilter.d.ts.map +1 -0
  617. package/src/engine/asset/loaders/image/png/filter/png_unfilter.js +68 -0
  618. package/src/engine/asset/loaders/image/png/png_inflate.d.ts +20 -0
  619. package/src/engine/asset/loaders/image/png/png_inflate.d.ts.map +1 -0
  620. package/src/engine/asset/loaders/image/png/png_inflate.js +85 -0
  621. package/src/engine/asset/loaders/image/png/png_unpack_samples.d.ts +22 -0
  622. package/src/engine/asset/loaders/image/png/png_unpack_samples.d.ts.map +1 -0
  623. package/src/engine/asset/loaders/image/png/png_unpack_samples.js +38 -0
  624. package/src/engine/graphics/ecs/sprite/SpriteSystemPE.d.ts.map +1 -1
  625. package/src/engine/graphics/ecs/sprite/SpriteSystemPE.js +135 -132
  626. package/src/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.d.ts +16 -3
  627. package/src/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.d.ts.map +1 -1
  628. package/src/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.js +1157 -1149
  629. package/src/engine/graphics/render/frame_graph/FrameGraph.d.ts +26 -0
  630. package/src/engine/graphics/render/frame_graph/FrameGraph.d.ts.map +1 -1
  631. package/src/engine/graphics/render/frame_graph/FrameGraph.js +947 -900
  632. package/src/engine/physics/ecs/PhysicsSystem.d.ts +69 -81
  633. package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
  634. package/src/engine/physics/ecs/PhysicsSystem.js +83 -77
  635. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  636. package/src/shade/device/IMMEDIATE_DATA_PLAN_2026_08_28.md +451 -0
  637. package/src/shade/device/ShadeGPUCommandContext.d.ts +21 -0
  638. package/src/shade/device/ShadeGPUCommandContext.d.ts.map +1 -1
  639. package/src/shade/device/ShadeGPUCommandContext.js +940 -749
  640. package/src/shade/device/timing/GPUTimerArray.d.ts +22 -4
  641. package/src/shade/device/timing/GPUTimerArray.d.ts.map +1 -1
  642. package/src/shade/device/timing/GPUTimerArray.js +88 -14
  643. package/src/shade/device/timing/GPU_PROFILER_PROPOSAL_2026_08_28.md +876 -0
  644. package/src/shade/device/timing/profile/GPUFrameRecorder.d.ts +93 -0
  645. package/src/shade/device/timing/profile/GPUFrameRecorder.d.ts.map +1 -0
  646. package/src/shade/device/timing/profile/GPUFrameRecorder.js +297 -0
  647. package/src/shade/device/timing/profile/GPUProfileCapture.d.ts +62 -0
  648. package/src/shade/device/timing/profile/GPUProfileCapture.d.ts.map +1 -0
  649. package/src/shade/device/timing/profile/GPUProfileCapture.js +77 -0
  650. package/src/shade/device/timing/profile/GPUProfileFrame.d.ts +73 -0
  651. package/src/shade/device/timing/profile/GPUProfileFrame.d.ts.map +1 -0
  652. package/src/shade/device/timing/profile/GPUProfileFrame.js +88 -0
  653. package/src/shade/device/timing/profile/GPUProfileLevel.d.ts +20 -0
  654. package/src/shade/device/timing/profile/GPUProfileLevel.d.ts.map +1 -0
  655. package/src/shade/device/timing/profile/GPUProfileLevel.js +50 -0
  656. package/src/shade/device/timing/profile/GPUProfileMeta.d.ts +63 -0
  657. package/src/shade/device/timing/profile/GPUProfileMeta.d.ts.map +1 -0
  658. package/src/shade/device/timing/profile/GPUProfileMeta.js +73 -0
  659. package/src/shade/device/timing/profile/GPUProfileSession.d.ts +173 -0
  660. package/src/shade/device/timing/profile/GPUProfileSession.d.ts.map +1 -0
  661. package/src/shade/device/timing/profile/GPUProfileSession.js +470 -0
  662. package/src/shade/device/timing/profile/GPUProfileSpan.d.ts +60 -0
  663. package/src/shade/device/timing/profile/GPUProfileSpan.d.ts.map +1 -0
  664. package/src/shade/device/timing/profile/GPUProfileSpan.js +65 -0
  665. package/src/shade/device/timing/profile/GPUProfileSpanKind.d.ts +19 -0
  666. package/src/shade/device/timing/profile/GPUProfileSpanKind.d.ts.map +1 -0
  667. package/src/shade/device/timing/profile/GPUProfileSpanKind.js +25 -0
  668. package/src/shade/device/timing/profile/GPUProfileTopology.d.ts +159 -0
  669. package/src/shade/device/timing/profile/GPUProfileTopology.d.ts.map +1 -0
  670. package/src/shade/device/timing/profile/GPUProfileTopology.js +202 -0
  671. package/src/shade/device/timing/profile/GPUProfileWork.d.ts +90 -0
  672. package/src/shade/device/timing/profile/GPUProfileWork.d.ts.map +1 -0
  673. package/src/shade/device/timing/profile/GPUProfileWork.js +115 -0
  674. package/src/shade/device/timing/profile/SGPTDefect.d.ts +43 -0
  675. package/src/shade/device/timing/profile/SGPTDefect.d.ts.map +1 -0
  676. package/src/shade/device/timing/profile/SGPTDefect.js +65 -0
  677. package/src/shade/device/timing/profile/SGPTHeader.d.ts +38 -0
  678. package/src/shade/device/timing/profile/SGPTHeader.d.ts.map +1 -0
  679. package/src/shade/device/timing/profile/SGPTHeader.js +46 -0
  680. package/src/shade/device/timing/profile/SGPTRecordRef.d.ts +37 -0
  681. package/src/shade/device/timing/profile/SGPTRecordRef.d.ts.map +1 -0
  682. package/src/shade/device/timing/profile/SGPTRecordRef.js +45 -0
  683. package/src/shade/device/timing/profile/SGPTWriteStream.d.ts +103 -0
  684. package/src/shade/device/timing/profile/SGPTWriteStream.d.ts.map +1 -0
  685. package/src/shade/device/timing/profile/SGPTWriteStream.js +287 -0
  686. package/src/shade/device/timing/profile/SGPT_DEFECT.d.ts +19 -0
  687. package/src/shade/device/timing/profile/SGPT_DEFECT.d.ts.map +1 -0
  688. package/src/shade/device/timing/profile/SGPT_DEFECT.js +26 -0
  689. package/src/shade/device/timing/profile/SGPT_FLAG.d.ts +8 -0
  690. package/src/shade/device/timing/profile/SGPT_FLAG.d.ts.map +1 -0
  691. package/src/shade/device/timing/profile/SGPT_FLAG.js +15 -0
  692. package/src/shade/device/timing/profile/SGPT_FORMAT.md +345 -0
  693. package/src/shade/device/timing/profile/SGPT_FORMAT_VERSION.d.ts +10 -0
  694. package/src/shade/device/timing/profile/SGPT_FORMAT_VERSION.d.ts.map +1 -0
  695. package/src/shade/device/timing/profile/SGPT_FORMAT_VERSION.js +9 -0
  696. package/src/shade/device/timing/profile/SGPT_FRAME_SLACK.d.ts +17 -0
  697. package/src/shade/device/timing/profile/SGPT_FRAME_SLACK.d.ts.map +1 -0
  698. package/src/shade/device/timing/profile/SGPT_FRAME_SLACK.js +16 -0
  699. package/src/shade/device/timing/profile/SGPT_HEADER_CHECKSUM_COVERAGE.d.ts +16 -0
  700. package/src/shade/device/timing/profile/SGPT_HEADER_CHECKSUM_COVERAGE.d.ts.map +1 -0
  701. package/src/shade/device/timing/profile/SGPT_HEADER_CHECKSUM_COVERAGE.js +15 -0
  702. package/src/shade/device/timing/profile/SGPT_HEADER_OFFSET.d.ts +15 -0
  703. package/src/shade/device/timing/profile/SGPT_HEADER_OFFSET.d.ts.map +1 -0
  704. package/src/shade/device/timing/profile/SGPT_HEADER_OFFSET.js +29 -0
  705. package/src/shade/device/timing/profile/SGPT_HEADER_SIZE.d.ts +7 -0
  706. package/src/shade/device/timing/profile/SGPT_HEADER_SIZE.d.ts.map +1 -0
  707. package/src/shade/device/timing/profile/SGPT_HEADER_SIZE.js +6 -0
  708. package/src/shade/device/timing/profile/SGPT_MAGIC.d.ts +7 -0
  709. package/src/shade/device/timing/profile/SGPT_MAGIC.d.ts.map +1 -0
  710. package/src/shade/device/timing/profile/SGPT_MAGIC.js +6 -0
  711. package/src/shade/device/timing/profile/SGPT_MIN_READER_VERSION.d.ts +16 -0
  712. package/src/shade/device/timing/profile/SGPT_MIN_READER_VERSION.d.ts.map +1 -0
  713. package/src/shade/device/timing/profile/SGPT_MIN_READER_VERSION.js +15 -0
  714. package/src/shade/device/timing/profile/SGPT_RECORD_HEADER_SIZE.d.ts +7 -0
  715. package/src/shade/device/timing/profile/SGPT_RECORD_HEADER_SIZE.d.ts.map +1 -0
  716. package/src/shade/device/timing/profile/SGPT_RECORD_HEADER_SIZE.js +6 -0
  717. package/src/shade/device/timing/profile/SGPT_RECORD_OFFSET.d.ts +13 -0
  718. package/src/shade/device/timing/profile/SGPT_RECORD_OFFSET.d.ts.map +1 -0
  719. package/src/shade/device/timing/profile/SGPT_RECORD_OFFSET.js +23 -0
  720. package/src/shade/device/timing/profile/SGPT_RECORD_SYNC.d.ts +12 -0
  721. package/src/shade/device/timing/profile/SGPT_RECORD_SYNC.d.ts.map +1 -0
  722. package/src/shade/device/timing/profile/SGPT_RECORD_SYNC.js +11 -0
  723. package/src/shade/device/timing/profile/SGPT_RECORD_TYPE.d.ts +18 -0
  724. package/src/shade/device/timing/profile/SGPT_RECORD_TYPE.d.ts.map +1 -0
  725. package/src/shade/device/timing/profile/SGPT_RECORD_TYPE.js +46 -0
  726. package/src/shade/device/timing/profile/frame_graph_extract_topology.d.ts +20 -0
  727. package/src/shade/device/timing/profile/frame_graph_extract_topology.d.ts.map +1 -0
  728. package/src/shade/device/timing/profile/frame_graph_extract_topology.js +174 -0
  729. package/src/shade/device/timing/profile/make_profiling_pass_encoder.d.ts +21 -0
  730. package/src/shade/device/timing/profile/make_profiling_pass_encoder.d.ts.map +1 -0
  731. package/src/shade/device/timing/profile/make_profiling_pass_encoder.js +114 -0
  732. package/src/shade/device/timing/profile/parse_workgroup_size.d.ts +20 -0
  733. package/src/shade/device/timing/profile/parse_workgroup_size.d.ts.map +1 -0
  734. package/src/shade/device/timing/profile/parse_workgroup_size.js +43 -0
  735. package/src/shade/device/timing/profile/sgpt_fourcc.d.ts +18 -0
  736. package/src/shade/device/timing/profile/sgpt_fourcc.d.ts.map +1 -0
  737. package/src/shade/device/timing/profile/sgpt_fourcc.js +34 -0
  738. package/src/shade/device/timing/profile/sgpt_frame_codec.d.ts +42 -0
  739. package/src/shade/device/timing/profile/sgpt_frame_codec.d.ts.map +1 -0
  740. package/src/shade/device/timing/profile/sgpt_frame_codec.js +172 -0
  741. package/src/shade/device/timing/profile/sgpt_meta_codec.d.ts +20 -0
  742. package/src/shade/device/timing/profile/sgpt_meta_codec.d.ts.map +1 -0
  743. package/src/shade/device/timing/profile/sgpt_meta_codec.js +65 -0
  744. package/src/shade/device/timing/profile/sgpt_read_capture.d.ts +19 -0
  745. package/src/shade/device/timing/profile/sgpt_read_capture.d.ts.map +1 -0
  746. package/src/shade/device/timing/profile/sgpt_read_capture.js +134 -0
  747. package/src/shade/device/timing/profile/sgpt_read_header.d.ts +22 -0
  748. package/src/shade/device/timing/profile/sgpt_read_header.d.ts.map +1 -0
  749. package/src/shade/device/timing/profile/sgpt_read_header.js +94 -0
  750. package/src/shade/device/timing/profile/sgpt_read_records.d.ts +27 -0
  751. package/src/shade/device/timing/profile/sgpt_read_records.d.ts.map +1 -0
  752. package/src/shade/device/timing/profile/sgpt_read_records.js +153 -0
  753. package/src/shade/device/timing/profile/sgpt_topology_codec.d.ts +54 -0
  754. package/src/shade/device/timing/profile/sgpt_topology_codec.d.ts.map +1 -0
  755. package/src/shade/device/timing/profile/sgpt_topology_codec.js +329 -0
  756. package/src/shade/device/timing/profile/sgpt_write_header.d.ts +22 -0
  757. package/src/shade/device/timing/profile/sgpt_write_header.d.ts.map +1 -0
  758. package/src/shade/device/timing/profile/sgpt_write_header.js +47 -0
  759. package/src/shade/device/timing/profile/sgpt_write_record.d.ts +23 -0
  760. package/src/shade/device/timing/profile/sgpt_write_record.d.ts.map +1 -0
  761. package/src/shade/device/timing/profile/sgpt_write_record.js +62 -0
  762. package/src/shade/renderer/Renderer.d.ts +21 -0
  763. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  764. package/src/shade/renderer/Renderer.js +2513 -2455
  765. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  766. package/src/shade/renderer/scene/serialization/decode_image_source_hdr.d.ts +14 -3
  767. package/src/shade/renderer/scene/serialization/decode_image_source_hdr.d.ts.map +1 -1
  768. package/src/shade/renderer/scene/serialization/decode_image_source_hdr.js +50 -51
  769. package/src/shade/renderer/scene/serialization/deserialize_scene.d.ts.map +1 -1
  770. package/src/shade/renderer/scene/serialization/deserialize_scene.js +531 -521
  771. package/src/shade/renderer/scene/serialization/write_image_source.d.ts.map +1 -1
  772. package/src/shade/renderer/scene/serialization/write_image_source.js +6 -4
  773. package/src/shade/renderer/shader/chunk/color/chunk_rgb_to_luminance.d.ts.map +1 -1
  774. package/src/shade/renderer/shader/chunk/color/chunk_rgb_to_luminance.js +34 -32
  775. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_eotf_st2084.d.ts +31 -0
  776. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_eotf_st2084.d.ts.map +1 -1
  777. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_eotf_st2084.js +62 -31
  778. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_inverse_eotf_st2084.d.ts +10 -0
  779. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_inverse_eotf_st2084.d.ts.map +1 -1
  780. package/src/shade/renderer/shader/chunk/color/iCtCp/chunk_inverse_eotf_st2084.js +32 -22
  781. package/src/shade/renderer/shader/chunk/color/sRGB/chunk_sRGBTransferOETF.d.ts.map +1 -1
  782. package/src/shade/renderer/shader/chunk/color/sRGB/chunk_sRGBTransferOETF.js +22 -21
  783. package/src/shade/renderer/shader/chunk/color/tonemap/gt7/chunk_tonemap_gt7.d.ts +6 -0
  784. package/src/shade/renderer/shader/chunk/color/tonemap/gt7/chunk_tonemap_gt7.d.ts.map +1 -1
  785. package/src/shade/renderer/shader/chunk/color/tonemap/gt7/chunk_tonemap_gt7.js +110 -104
  786. package/src/shade/renderer/shader/chunk/color/xyz/chunk_rgb_to_xyz.d.ts.map +1 -1
  787. package/src/shade/renderer/shader/chunk/color/xyz/chunk_rgb_to_xyz.js +23 -21
  788. package/src/view/elements/ColorPickerView.js +296 -296
  789. package/src/avif/codec/dec/Readme.md +0 -14
  790. package/src/avif/codec/dec/avif_dec.cpp +0 -234
  791. package/src/avif/codec/dec/avif_dec.d.ts +0 -3
  792. package/src/avif/codec/dec/avif_dec.d.ts.map +0 -1
  793. package/src/avif/codec/dec/avif_dec.js +0 -16
  794. package/src/avif/codec/dec/avif_dec.wasm +0 -0
  795. package/src/avif/codec/enc/README.md +0 -37
  796. package/src/avif/codec/enc/avif_enc.cpp +0 -216
  797. package/src/avif/codec/enc/avif_enc.d.ts +0 -3
  798. package/src/avif/codec/enc/avif_enc.d.ts.map +0 -1
  799. package/src/avif/codec/enc/avif_enc.js +0 -6144
  800. package/src/avif/codec/enc/avif_enc.wasm +0 -0
  801. package/src/avif/codec/enc/avif_enc_mt.d.ts +0 -3
  802. package/src/avif/codec/enc/avif_enc_mt.d.ts.map +0 -1
  803. package/src/avif/codec/enc/avif_enc_mt.js +0 -6811
  804. package/src/avif/codec/enc/avif_enc_mt.wasm +0 -0
  805. package/src/avif/codec/enc/avif_enc_mt.worker.d.mts +0 -2
  806. package/src/avif/codec/enc/avif_enc_mt.worker.d.mts.map +0 -1
  807. package/src/avif/codec/enc/avif_enc_mt.worker.d.ts +0 -11
  808. package/src/avif/codec/enc/avif_enc_mt.worker.d.ts.map +0 -1
  809. package/src/avif/codec/enc/avif_enc_mt.worker.js +0 -171
  810. package/src/avif/codec/enc/avif_enc_mt.worker.mjs +0 -1
  811. package/src/avif/decode.d.ts +0 -3
  812. package/src/avif/decode.d.ts.map +0 -1
  813. package/src/avif/decode.js +0 -42
  814. package/src/avif/encode.d.ts +0 -3
  815. package/src/avif/encode.d.ts.map +0 -1
  816. package/src/avif/encode.js +0 -75
  817. package/src/avif/meta.d.ts +0 -24
  818. package/src/avif/meta.d.ts.map +0 -1
  819. package/src/avif/meta.js +0 -23
  820. package/src/avif/tsconfig.tsbuildinfo +0 -1
  821. package/src/avif/utils.d.ts +0 -17
  822. package/src/avif/utils.d.ts.map +0 -1
  823. package/src/avif/utils.js +0 -31
  824. package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.d.ts +0 -23
  825. package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.d.ts.map +0 -1
  826. package/src/engine/asset/loaders/image/jpeg/idct8x8_fixed.js +0 -214
  827. package/src/engine/asset/loaders/image/png/inflate.d.ts +0 -7
  828. package/src/engine/asset/loaders/image/png/inflate.d.ts.map +0 -1
  829. package/src/engine/asset/loaders/image/png/inflate.js +0 -20
  830. package/src/engine/save/GameStateLoader.d.ts +0 -49
  831. package/src/engine/save/GameStateLoader.d.ts.map +0 -1
  832. package/src/engine/save/GameStateLoader.js +0 -168
@@ -1,2455 +1,2513 @@
1
- import { assert } from "../../core/assert.js";
2
- import Signal from "../../core/events/signal/Signal.js";
3
- import Vector2 from "../../core/geom/Vector2.js";
4
- import { clamp } from "../../core/math/clamp.js";
5
- import { remap } from "../../core/math/remap.js";
6
- import { MetricCollection } from "../../engine/development/performance/MetricCollection.js";
7
- import {
8
- MetricCollectionConsoleMonitor
9
- } from "../../engine/development/performance/monitor/MetricCollectionConsoleMonitor.js";
10
- import { FrameGraph } from "../../engine/graphics/render/frame_graph/FrameGraph.js";
11
- import { TextureDescriptor } from "../descriptor/texture/TextureDescriptor.js";
12
- import { WebGPUExtensionType } from "../descriptor/WebGPUExtensionType.js";
13
- import { ShadeDeviceFailure } from "../device/ShadeDeviceFailure.js";
14
- import { ShadeGPUCommandContext } from "../device/ShadeGPUCommandContext.js";
15
- import { CameraManager } from "./camera/CameraManager.js";
16
- import { shader_background } from "./deferred/shader_background.js";
17
- import { shader_composite_volumetrics } from "./deferred/shader_composite_volumetrics.js";
18
- import { shader_deferred_main, shader_deferred_main_restir } from "./deferred/shader_deferred_main.js";
19
- import { ReSTIRDI } from "./restir/di/ReSTIRDI.js";
20
- import { shader_tonemap_HDR } from "./deferred/shader_tonemap_HDR.js";
21
- import { shader_tonemap_LDR } from "./deferred/shader_tonemap_LDR.js";
22
- import { G_BUFFER_MIP_LEVELS } from "./gbuffer/G_BUFFER_MIP_LEVELS.js";
23
- import {
24
- graph_rasterize_meshes_transparent_oit
25
- } from "./rasterize/native/oit/graph_rasterize_meshes_transparent_oit.js";
26
- import {
27
- viz_rasterization_alpha_tested_pass_descriptor
28
- } from "./rasterize/native/viz/viz_rasterization_alpha_tested_pass_descriptor.js";
29
- import {
30
- viz_rasterization_opaque_pass_descriptor
31
- } from "./rasterize/native/viz/viz_rasterization_opaque_pass_descriptor.js";
32
- import { RasterizationJob } from "./rasterize/RasterizationJob.js";
33
- import { graph_rasterize_scene } from "./rasterize/standard/graph_rasterize_scene.js";
34
- import { FrameContext } from "./extension/FrameContext.js";
35
- import { FramePhase } from "./extension/FramePhase.js";
36
- import { GBufferTextures } from "./extension/GBufferTextures.js";
37
- import { PresentTarget } from "./extension/PresentTarget.js";
38
- import { RenderExtensionRegistry } from "./extension/RenderExtensionRegistry.js";
39
- import { SceneColor } from "./extension/SceneColor.js";
40
- import { ViewTextures } from "./extension/ViewTextures.js";
41
- import { shader_brick4_diffuse } from "./global_illumination/brick4/gpu/draw/shader_brick4_diffuse.js";
42
- import {
43
- shader_brick4_indirect_lighting
44
- } from "./global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.js";
45
- import { shader_brick4_specular } from "./global_illumination/brick4/gpu/draw/shader_brick4_specular.js";
46
- import { shader_diffuse_IBL } from "./global_illumination/ibl/shader_diffuse_IBL.js";
47
- import { shader_specular_IBL } from "./global_illumination/ibl/shader_specular_IBL.js";
48
- import { GPULightProbeVolumeRenderer } from "./global_illumination/lpv/bake/GPULightProbeVolumeRenderer.js";
49
- import { shader_diffuse_LPV } from "./global_illumination/lpv/shader_diffuse_LPV.js";
50
- import { shader_resolve_indirect_lighting } from "./global_illumination/shader_resolve_indirect_lighting.js";
51
- import { GraphicsContext } from "./GraphicsContext.js";
52
- import { graph_build_light_clusters } from "./light/cluster/graph_build_light_clusters.js";
53
- import { AccumulatingPathTracer } from "./path_tracer/accumulating/AccumulatingPathTracer.js";
54
- import { graph_postprocess_bloom } from "./postprocess/bloom/graph_postprocess_bloom.js";
55
- import { shader_ffx_rcas } from "./postprocess/cas/shader_ffx_rcas.js";
56
- import { GPUCameraExposureManager } from "./postprocess/eye/GPUCameraExposureManager.js";
57
- import { NSS } from "./postprocess/nss/NSS.js";
58
- import { PostProcess } from "./postprocess/PostProcess.js";
59
- import { shader_depth_occlusion_clip } from "./postprocess/taa/shader_depth_occlusion_clip.js";
60
- import { TAA } from "./postprocess/taa/TAA.js";
61
- import { MotionBlur } from "./postprocess/motion_blur/MotionBlur.js";
62
- import { DepthOfField } from "./postprocess/dof/raymarch/DepthOfField.js";
63
- import { DepthOfFieldU } from "./postprocess/dof/gather/DepthOfFieldU.js";
64
- import { graph_add_per_object_velocity } from "./postprocess/velocity/graph_add_per_object_velocity.js";
65
- import { SceneManager } from "./scene/SceneManager.js";
66
- import { ShadeIndirectLightingMode } from "./ShadeIndirectLightingMode.js";
67
- import { ShadeUpscalerType } from "./ShadeUpscalerType.js";
68
- import { graph_image_pass } from "./shader/graph/graph_image_pass.js";
69
- import { graph_import_buffer } from "./shader/graph/graph_import_buffer.js";
70
- import { graph_import_texture } from "./shader/graph/graph_import_texture.js";
71
- import { CascadedSceneSDF } from "./shadow/sdf/cascade/CascadedSceneSDF.js";
72
- import { STATIC_GRAPHICS_ENGINE_ASSETS } from "./STATIC_GRAPHICS_ENGINE_ASSETS.js";
73
- import { shader_debug_velocity } from "./postprocess/velocity/shader_debug_velocity.js";
74
- import { GPUTextureContext } from "./texture/GPUTextureContext.js";
75
- import { RenderTarget } from "./texture/RenderTarget.js";
76
- import { View } from "./view/View.js";
77
- import { ViewManager } from "./view/ViewManager.js";
78
-
79
- const METRICS = new MetricCollection();
80
- METRICS.create({ name: 'frame_time' });
81
-
82
- /**
83
- * Depth and G-buffer textures carry {@link G_BUFFER_MIP_LEVELS} mip levels.
84
- * WebGPU's createTexture validation requires mipLevelCount <= floor(log2(max(w,h))) + 1,
85
- * so the internal resolution must never drop below 2^(G_BUFFER_MIP_LEVELS - 1)
86
- * or texture allocation will fail.
87
- * @type {number}
88
- */
89
- const INTERNAL_RESOLUTION_MIN = 1 << (G_BUFFER_MIP_LEVELS - 1);
90
-
91
- MetricCollectionConsoleMonitor.from(METRICS);
92
-
93
- export class Renderer {
94
- /**
95
- * Indexes current drawn frame
96
- * @type {number}
97
- */
98
- #frame_count = 0;
99
-
100
- get frame_count() {
101
- return this.#frame_count;
102
- }
103
-
104
- /**
105
- * @type {GPUCanvasContext}
106
- */
107
- context;
108
-
109
- /**
110
- * The HTMLCanvasElement (or OffscreenCanvas) bound to this renderer.
111
- * If `initialize()` was called without a `context`, this is the canvas
112
- * the renderer created — append it to the DOM to display the output.
113
- *
114
- * @returns {HTMLCanvasElement|OffscreenCanvas|undefined}
115
- */
116
- get canvas() {
117
- return this.context?.canvas;
118
- }
119
-
120
- /**
121
- * @type {GPUDevice}
122
- */
123
- device;
124
-
125
- /**
126
- * @type {GPUCameraExposureManager}
127
- */
128
- #autoexposure;
129
-
130
- /**
131
- * @type {SceneManager}
132
- */
133
- #scenes;
134
-
135
- /**
136
- * Only use for read-access
137
- * @returns {SceneManager}
138
- */
139
- get scenes() {
140
- return this.#scenes;
141
- }
142
-
143
- /**
144
- * @type {CameraManager}
145
- */
146
- #cameras
147
-
148
- /**
149
- * @type {ViewManager}
150
- */
151
- #views
152
-
153
-
154
- set feature_shadows_enabled(v) {
155
- assert.isBoolean(v, 'v');
156
- this.#feature_shadows_enabled = v;
157
- }
158
-
159
- get feature_shadows_enabled() {
160
- return this.#feature_shadows_enabled;
161
- }
162
-
163
- /**
164
- * Controls which indirect lighting technique is used for
165
- * both deferred resolve and forward transparency passes.
166
- * @type {ShadeIndirectLightingMode}
167
- */
168
- indirect_lighting_mode = ShadeIndirectLightingMode.IBL;
169
-
170
- feature_ssr_enabled = false;
171
- feature_ssao_enabled = true;
172
- /**
173
- * ReSTIR DI — stochastic screen-bounded direct shadowing of local
174
- * (point+spot) lights. When on, the deferred pass skips its froxel cluster
175
- * loop and consumes the ReSTIR-resolved radiance instead.
176
- */
177
- feature_restir_di_enabled = false;
178
- /**
179
- * Generate point-light shadows with the tetrahedral (4-face) path instead of the default
180
- * cube (6-face) path. Both produce the same octahedral atlas encoding, so this is safe to
181
- * flip at runtime and is meant for A/B comparison of the two generators. Pushed to the
182
- * scene's shadow context each frame; see
183
- * {@link GPUSceneShadowmapContext.use_tetrahedron_point_shadows}.
184
- * @type {boolean}
185
- */
186
- feature_tetrahedron_point_shadows = false;
187
- feature_bloom_enabled = true;
188
- feature_automatic_exposure_enabled = true;
189
-
190
- /**
191
- * Virtual texture streaming (opt-in per material via
192
- * {@link StandardShadeMaterial#vt_stack}). This flag is the system-wide
193
- * kill switch: when off, the feedback pass and residency maintenance
194
- * stop running — already-resident pages keep rendering, nothing new
195
- * streams in. Costs nothing while no stack is registered.
196
- * @type {boolean}
197
- */
198
- feature_virtual_textures = true;
199
-
200
- #feature_taa_enabled = true;
201
-
202
- get feature_taa_enabled() {
203
- return this.#feature_taa_enabled;
204
- }
205
-
206
- /**
207
- * While TAA is off nothing writes the history textures, but the frame counter that
208
- * indexes them keeps advancing — re-enabling would blend with arbitrarily stale
209
- * history of arbitrary parity. Reset temporal history on the transition.
210
- * @param {boolean} v
211
- */
212
- set feature_taa_enabled(v) {
213
- if (this.#feature_taa_enabled === v) {
214
- return;
215
- }
216
-
217
- this.#feature_taa_enabled = v;
218
- this.indicate_view_change();
219
- }
220
-
221
- /**
222
- * Jimenez 2014 reconstruction-filter motion blur, runs between
223
- * TAA and sharpening on the HDR colour. Reads the velocity buffer
224
- * the same way TAA does — per-pixel pixel-space deltas — and
225
- * scatter-gathers along the dilated tile velocity. Off by default
226
- * because the reconstruction pass is a few-ms-budget item and not
227
- * every scene needs it.
228
- *
229
- * @type {boolean}
230
- */
231
- feature_motion_blur_enabled = false;
232
-
233
- /**
234
- * Motion-blur subsystem (Jimenez 2014 reconstruction filter). A
235
- * self-contained manager — same pattern as {@link #autoexposure} /
236
- * {@link #depth_of_field} — that owns the motion-blur parameters
237
- * (currently just `strength`). Constructed in init.
238
- * @type {MotionBlur}
239
- */
240
- #motion_blur;
241
-
242
- /**
243
- * The motion-blur subsystem. Configure it via `renderer.motion_blur.*`
244
- * (currently `strength`); toggle the effect with
245
- * {@link feature_motion_blur_enabled}.
246
- * @returns {MotionBlur}
247
- */
248
- get motion_blur() { return this.#motion_blur; }
249
-
250
- /**
251
- * Debug-only override: when true, the per-object velocity texture
252
- * is drawn straight into the post-TAA color path so the screen
253
- * shows the velocity buffer instead of the rendered image. Wired
254
- * just before TAA — TAA then operates on the velocity texture,
255
- * which is nonsense but cheap to set up. Default false.
256
- *
257
- * @type {boolean}
258
- */
259
- feature_velocity_debug_view = false;
260
-
261
- /**
262
- * When true, {@link path_tracer} replaces the rasterized+shaded color for the frame. The
263
- * traced result still goes through TAA and tone mapping like any other color.
264
- *
265
- * The path tracer accumulates across frames itself, one tile per call, and resets whenever the
266
- * camera moves. TAA on top of that is two temporal filters fighting over the same image — turn
267
- * `feature_taa_enabled` off while this is on.
268
- *
269
- * This is the traversal-heaviest consumer of the TLAS — it binds `scene_ctx.tlas.buffer`
270
- * directly — which is what made it the check that the GPU TLAS build traces correctly, and what
271
- * makes it the first place a regression in that tree would show. Default false: it is a
272
- * prototype-grade path, not a shipping render mode.
273
- *
274
- * @type {boolean}
275
- */
276
- feature_path_tracing_enabled = false;
277
-
278
- /**
279
- * Screen-space ray-marched depth of field — the Tiny Glade model
280
- * (Stachowiak, GPC 2024; see {@link DepthOfField}): aperture sub-rays
281
- * marched through the half-res depth/CoC buffer with nearest-hit
282
- * occlusion, plus tilt-shift. Runs on the resolved HDR colour after TAA
283
- * (which resolves the march's sample noise) and before motion blur. Off
284
- * by default — a half-res march is a few-ms item, only worth it for a
285
- * deliberate shallow-focus / diorama look. Configure via `renderer.dof`.
286
- *
287
- * @type {boolean}
288
- */
289
- feature_dof_enabled = false;
290
-
291
- /**
292
- * Depth-of-field subsystem (screen-space ray-marched). A self-contained
293
- * manager — same pattern as {@link #autoexposure} — that owns all DoF
294
- * parameters, focus state, and GPU buffers. Constructed in init.
295
- * @type {DepthOfField}
296
- */
297
- #depth_of_field;
298
-
299
- /**
300
- * The depth-of-field subsystem. Configure it via `renderer.dof.*`
301
- * (`f_number`, `quality`, the `focus*` helpers, `applyPreset`, …);
302
- * toggle the effect with {@link feature_dof_enabled}.
303
- * @returns {DepthOfField}
304
- */
305
- get dof() { return this.#depth_of_field; }
306
-
307
- /**
308
- * Which depth-of-field implementation runs when {@link feature_dof_enabled}
309
- * is on. `"raymarch"` (default) is the screen-space ray-march
310
- * ({@link DepthOfField}); `"unreal"` is the gather-based Diaphragm /
311
- * Cinematic DoF ({@link DepthOfFieldU}) — half-res near/far gather with a
312
- * foreground coverage bleed. Mutually exclusive by construction (only one
313
- * runs at the single DoF call site).
314
- * @type {"raymarch"|"unreal"}
315
- */
316
- dof_algorithm = "raymarch";
317
-
318
- /**
319
- * Unreal-style (Diaphragm / Cinematic) gather depth of field — a drop-in
320
- * alternative to {@link #depth_of_field}, selected via {@link dof_algorithm}.
321
- * Constructed in init.
322
- * @type {DepthOfFieldU}
323
- */
324
- #depth_of_field_unreal;
325
-
326
- /**
327
- * The Unreal-style depth-of-field subsystem. Configure it via
328
- * `renderer.dof_unreal.*` and select it with `renderer.dof_algorithm = "unreal"`.
329
- * @returns {DepthOfFieldU}
330
- */
331
- get dof_unreal() { return this.#depth_of_field_unreal; }
332
-
333
- /**
334
- * When true and {@link indirect_lighting_mode} is Brick4, run a single fused
335
- * pass that produces the resolved indirect lighting directly (combining the
336
- * brick4 diffuse, brick4 specular and indirect resolve passes). Trades a
337
- * bit of cache pressure inside the pass for substantially less memory
338
- * bandwidth: no intermediate rgba16float diffuse/specular targets, the
339
- * g-buffer is read once, and a single brick4 probe pair is shared between
340
- * the diffuse and specular evaluations.
341
- *
342
- * SSR is mutually exclusive with the fused path (it needs the standalone
343
- * specular target as input), so when SSR is enabled the split path is used.
344
- * @type {boolean}
345
- */
346
- fused_indirect = true;
347
-
348
- /**
349
- * RCAS
350
- * @type {boolean}
351
- */
352
- feature_sharpening_enabled = true;
353
-
354
- #feature_shadows_enabled = true;
355
- #feature_enabled_transparencies = true;
356
-
357
- /**
358
- *
359
- * @return {ViewManager}
360
- */
361
- get views() {
362
- return this.#views;
363
- }
364
-
365
- /**
366
- * @type {GraphicsContext}
367
- */
368
- #graphics;
369
-
370
- /**
371
- *
372
- * @return {GraphicsContext}
373
- */
374
- get graphics() {
375
- return this.#graphics;
376
- }
377
-
378
-
379
- /**
380
- *
381
- * @type {Signal<number>}
382
- */
383
- /**
384
- * Extensions an outside caller has put into the frame, by phase.
385
- *
386
- * @type {RenderExtensionRegistry}
387
- */
388
- #extensions = new RenderExtensionRegistry();
389
-
390
- /**
391
- * Raised when there is no usable device — the browser has no WebGPU, the hardware is below the
392
- * floor, or a working device was lost while running. Carries a {@link ShadeDeviceFailure}.
393
- *
394
- * The floor is deliberate and there are no fallbacks (D6/I-8), so telling the player plainly is
395
- * the whole of what the engine owes here. Loss is not recovered from (R6): everything built on
396
- * the device went with it, and the honest response is a message and a reload.
397
- *
398
- * @type {Signal<ShadeDeviceFailure>}
399
- */
400
- onDeviceFailure = new Signal();
401
-
402
- onFrameFinished = new Signal();
403
-
404
- /**
405
- * First argument is frame index, second is table of debug data
406
- * @type {Signal<number, Object[]>}
407
- */
408
- onFrameDebug = new Signal();
409
-
410
-
411
- /**
412
- * Fraction of the output resolution.
413
- * Controls {@link #internal_resolution}.
414
- * If this is set to 0.5 for example - internal resolution will be 50% of the output resolution.
415
- * @type {number}
416
- */
417
- #internal_resolution_scale = 1;
418
-
419
- /**
420
- *
421
- * @type {boolean}
422
- */
423
- #internal_resolution_needs_update = true;
424
-
425
- /**
426
- *
427
- * @param {number} v
428
- */
429
- set internal_resolution_scale(v) {
430
- assert.isNumber(v, 'v');
431
- assert.notNaN(v, 'v');
432
- assert.greaterThan(v, 0);
433
- assert.isFinite(v, 'v');
434
-
435
- if (this.#internal_resolution_scale === v) {
436
- // no change
437
- return;
438
- }
439
-
440
- this.#internal_resolution_scale = v;
441
-
442
- // schedule resolution recalculation
443
- this.#internal_resolution_needs_update = true;
444
- }
445
-
446
- /**
447
- *
448
- * @returns {number}
449
- */
450
- get internal_resolution_scale() {
451
- return this.#internal_resolution_scale;
452
- }
453
-
454
- /**
455
- * This is the resolution that the rendering happens at before upscaling.
456
- * Derived from input resolution via {@link internal_resolution_scale}
457
- * @type {Vector2}
458
- */
459
- #internal_resolution = new Vector2(1, 1);
460
-
461
-
462
- /**
463
- * Output viewport size
464
- * @type {Vector2}
465
- */
466
- #size = new Vector2(1, 1);
467
-
468
- /**
469
- * Output resolution.
470
- * Equal to {@link size} * {@link pixel_ratio}
471
- * Don't write directly
472
- * @type {Vector2}
473
- * @see {#size}
474
- * @see {#pixel_ratio}
475
- */
476
- #output_resolution = new Vector2(1, 1);
477
-
478
- /**
479
- *
480
- * @returns {Vector2}
481
- */
482
- get output_resolution() {
483
- return this.#output_resolution.clone();
484
- }
485
-
486
- /**
487
- * width/height
488
- * Useful for camera projection setup
489
- * @return {number}
490
- */
491
- get aspect_ratio() {
492
- return this.#internal_resolution.x / this.#internal_resolution.y;
493
- }
494
-
495
- /**
496
- * TODO rework ping-ping buffer usage to release old buffer when no longer needed. Can reach much better resource utilization that way. Same for all other TAA-like techniques
497
- * @type {GPUTextureContext[]}
498
- */
499
- #textures_depth = new Array(2);
500
-
501
- /**
502
- *
503
- * @returns {GPUTextureContext}
504
- */
505
- get texture_depth_current() {
506
- const tx = this.#textures_depth;
507
-
508
- return tx[this.#frame_count % tx.length];
509
- }
510
-
511
- /**
512
- *
513
- * @returns {GPUTextureContext}
514
- */
515
- get texture_depth_previous() {
516
- const tx = this.#textures_depth;
517
-
518
- return tx[(this.#frame_count - 1 + tx.length) % tx.length];
519
- }
520
-
521
- /**
522
- *
523
- * @type {GPUTextureContext[]}
524
- */
525
- #textures_taa_history = new Array(2);
526
-
527
- /**
528
- *
529
- * @returns {GPUTextureContext}
530
- */
531
- #get_texture_taa_history(x) {
532
- const tx = this.#textures_taa_history;
533
-
534
- return tx[(this.#frame_count - x + tx.length) % tx.length];
535
- }
536
-
537
-
538
- /**
539
- * @type {GPUTextureFormat}
540
- */
541
- #presentation_format = "rgba8unorm";
542
-
543
- /**
544
- * How many pixels to draw per screen pixel.
545
- * Typically, this is used to match physical screen resolution to virtual resolution.
546
- * On the high pixel density displays, CSS pixels usually take up more than one screen pixel.
547
- * @type {number}
548
- */
549
- #pixel_ratio = window.devicePixelRatio;
550
-
551
- /**
552
- *
553
- * @return {number}
554
- */
555
- get pixel_ratio() {
556
- return this.#pixel_ratio;
557
- }
558
-
559
- /**
560
- *
561
- * @param {number} ratio
562
- */
563
- set pixel_ratio(ratio) {
564
- assert.isNumber(ratio, 'ratio');
565
- assert.notNaN(ratio, 'ratio');
566
- assert.greaterThan(ratio, 0);
567
- assert.isFinite(ratio, 'ratio');
568
-
569
- if (ratio === this.#pixel_ratio) {
570
- // no change
571
- return;
572
- }
573
-
574
- this.#pixel_ratio = ratio;
575
-
576
- this.#update_resolution();
577
- }
578
-
579
- /**
580
- * How many future frames should be debugged
581
- * @type {number}
582
- */
583
- #debug_frame_count = 0;
584
-
585
- /**
586
- * Increase the number of future debug frames by X
587
- */
588
- /**
589
- * Record work into the frame at a named point in it.
590
- *
591
- * The extension is called once per frame, when its phase comes round, and is handed the
592
- * {@link FrameContext} — the frame's graph and the typed records holding the handles that exist
593
- * at that moment. It records with the `graph_*` helpers exactly as the built-in passes do, and
594
- * everything it produces reaches the rest of the frame by publishing a handle into a record.
595
- *
596
- * Handles come from the records rather than from the renderer on purpose: the graph is what
597
- * knows which resources a pass touched, and work recorded around it cannot be ordered or aliased
598
- * correctly against the rest of the frame.
599
- *
600
- * @param {RenderExtension} extension
601
- * @returns {RenderExtension} `extension`, for handing back to {@link remove_extension}
602
- */
603
- add_extension(extension) {
604
- return this.#extensions.add(extension);
605
- }
606
-
607
- /**
608
- * Take an extension back out of the frame.
609
- *
610
- * @param {RenderExtension} extension
611
- * @returns {boolean} whether it was registered
612
- */
613
- remove_extension(extension) {
614
- return this.#extensions.remove(extension);
615
- }
616
-
617
- /**
618
- * @param {FramePhase} phase
619
- * @returns {number} how many extensions are registered on `phase`
620
- */
621
- extension_count(phase) {
622
- return this.#extensions.count(phase);
623
- }
624
-
625
-
626
- add_debug_frame(count = 1) {
627
- assert.isNonNegativeInteger(count, 'count');
628
-
629
- this.#debug_frame_count += count;
630
- }
631
-
632
- /**
633
- *
634
- * @type {Map<Scene, GPULightProbeVolumeRenderer>}
635
- */
636
- #probe_bakers = new Map();
637
-
638
-
639
- /**
640
- *
641
- * @type {Map<Scene, CascadedSceneSDF>}
642
- */
643
- #scene_sdfs = new Map();
644
-
645
- /**
646
- *
647
- * @param {Scene} scene
648
- * @return {CascadedSceneSDF}
649
- */
650
- obtains_scene_sdf(scene) {
651
- let sdf = this.#scene_sdfs.get(scene);
652
-
653
- if (sdf === undefined) {
654
- sdf = new CascadedSceneSDF(this.device);
655
- this.#scene_sdfs.set(scene, sdf);
656
- }
657
-
658
- return sdf;
659
- }
660
-
661
- /**
662
- * Used to instruct renderer that either the scene or camera has changed significantly
663
- * Primarily instructs resetting history on temporal accumulation
664
- */
665
- indicate_view_change() {
666
- this.#postprocess.reset_history();
667
-
668
- if (this.#path_tracer !== undefined) {
669
- this.#path_tracer.clear_history = true;
670
- }
671
-
672
- this.#taa.reset_history = true;
673
-
674
- if (this.#nss !== undefined) {
675
- this.#nss.reset_history = true;
676
- }
677
- }
678
-
679
- /**
680
- * For internal and debug use only
681
- * @param {Scene} scene
682
- * @returns {GPULightProbeVolumeRenderer}
683
- */
684
- getProbeRendererForScene(scene) {
685
-
686
- const scene_ctx = this.#scenes.obtain(scene);
687
-
688
- let baker = this.#probe_bakers.get(scene);
689
-
690
- if (baker === undefined) {
691
- baker = new GPULightProbeVolumeRenderer(this.#graphics, scene_ctx);
692
- this.#probe_bakers.set(scene, baker);
693
- }
694
-
695
- return baker;
696
- }
697
-
698
-
699
- /**
700
- *
701
- * @param {Scene} scene
702
- */
703
- update_lpv(scene) {
704
-
705
- //
706
- // let baker = this.getProbeRendererForScene(scene);
707
- //
708
- // baker.autoSetRaysPerProbe(1);
709
- // baker.bake();
710
-
711
- const scene_ctx = this.#scenes.obtain(scene);
712
-
713
- const graph = new FrameGraph("LPV");
714
-
715
- const cmd = ShadeGPUCommandContext.create(this.graphics, 'LPV');
716
-
717
- scene_ctx.light_probe_volume.atlas.graph_update({
718
- graph,
719
- scene: scene_ctx,
720
- graphics: this.graphics,
721
- update_ray_count: 100_000
722
- });
723
-
724
- cmd.encodeGraph(graph);
725
- cmd.finish();
726
- }
727
-
728
- /**
729
- * Compute deferred indirect specular and diffuse contributions
730
- * based on the active indirect lighting mode.
731
- *
732
- * @param {object} params
733
- * @param {FrameGraph} params.graph
734
- * @param {GPUViewContext} params.view_ctx
735
- * @param {GPUSceneContext} params.scene_ctx
736
- * @param {number} params.bent_normals - graph resource ID
737
- * @param {number} params.current_color_out - graph resource ID (scene color, needed for SSR)
738
- * @param {number} params.gr_texture_depth_current
739
- * @param {number} params.gr_texture_hzb
740
- * @param {number} params.gr_texture_material_albedo_ao
741
- * @param {number} params.gr_texture_pbr
742
- * @param {number} params.gr_texture_normal
743
- * @param {number} params.gr_texture_environment
744
- * @param {number} params.gr_velocity
745
- * @param {number} params.gr_occlusion_clip
746
- * @param {number} params.gr_buffer_camera_current
747
- * @param {number} params.gr_buffer_camera_previous
748
- * @param {Vector2} params.resolution
749
- * @returns {{indirect_specular: number, indirect_diffuse: number}}
750
- */
751
- #graph_indirect_lighting({
752
- graph,
753
- view_ctx,
754
- scene_ctx,
755
- bent_normals,
756
- current_color_out,
757
- //
758
- gr_texture_depth_current,
759
- gr_texture_hzb,
760
- gr_texture_material_albedo_ao,
761
- gr_texture_pbr,
762
- gr_texture_normal,
763
- gr_texture_environment,
764
- gr_velocity,
765
- gr_occlusion_clip,
766
- gr_buffer_camera_current,
767
- gr_buffer_camera_previous,
768
- resolution,
769
- }) {
770
-
771
- const mode = this.indirect_lighting_mode;
772
-
773
- let indirect_specular = -1;
774
- let indirect_diffuse = -1;
775
-
776
- const lpv = scene_ctx.light_probe_volume;
777
-
778
- //
779
- // ── SPECULAR ────────────────────────────────────────────
780
- //
781
-
782
- // SSR replaces the specular IBL fallback when enabled.
783
- // NOTE: SSR is currently not supported in Brick4 mode — this is a known
784
- // limitation and will be addressed in a future update.
785
- if (this.feature_ssr_enabled && mode !== ShadeIndirectLightingMode.Brick4) {
786
-
787
- const ssr = this.#postprocess.ssr.graph_pass({
788
- graph,
789
- input_depth: gr_texture_depth_current,
790
- input_hzb: gr_texture_hzb,
791
- input_scene_color: current_color_out,
792
- input_albedo: gr_texture_material_albedo_ao,
793
- input_pbr: gr_texture_pbr,
794
- input_normal: gr_texture_normal,
795
- input_velocity: gr_velocity,
796
- input_occlusion: gr_occlusion_clip,
797
- input_environment: gr_texture_environment,
798
- camera_current: gr_buffer_camera_current,
799
- camera_previous: gr_buffer_camera_previous,
800
- lpv: mode === ShadeIndirectLightingMode.LPV ? lpv : undefined,
801
- resolution,
802
- });
803
-
804
- indirect_specular = ssr.denoised;
805
-
806
- } else if (mode === ShadeIndirectLightingMode.Brick4) {
807
-
808
- // Brick4 specular from volumetric lightmap
809
- [indirect_specular] = graph_image_pass({
810
- graph,
811
- shader: shader_brick4_specular,
812
- input_depth: gr_texture_depth_current,
813
- inputs: {
814
- tDepth: gr_texture_depth_current,
815
- tNormals: gr_texture_normal,
816
- tPBR: gr_texture_pbr,
817
- tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
818
- camera: gr_buffer_camera_current,
819
- view: view_ctx.uniform_buffer.buffer,
820
- brick4: scene_ctx.volumetric_light_map.buffer,
821
- },
822
- output_resolution: view_ctx.resolution,
823
- });
824
-
825
- } else {
826
- // IBL / LPV fallback — environment map specular
827
- [indirect_specular] = graph_image_pass({
828
- graph,
829
- shader: shader_specular_IBL,
830
- input_depth: gr_texture_depth_current,
831
- inputs: {
832
- tBentNormals: bent_normals,
833
- tNormals: gr_texture_normal,
834
- tDepth: gr_texture_depth_current,
835
- tPBR: gr_texture_pbr,
836
- tEnvironment: gr_texture_environment,
837
- camera: gr_buffer_camera_current,
838
- },
839
- output_resolution: view_ctx.resolution,
840
- });
841
- }
842
-
843
- //
844
- // ── DIFFUSE ─────────────────────────────────────────────
845
- //
846
-
847
- switch (mode) {
848
-
849
- case ShadeIndirectLightingMode.IBL:
850
- // Environment map irradiance
851
- [indirect_diffuse] = graph_image_pass({
852
- graph,
853
- shader: shader_diffuse_IBL,
854
- input_depth: gr_texture_depth_current,
855
- inputs: {
856
- tBentNormals: bent_normals,
857
- tGBufferAlbedo: gr_texture_material_albedo_ao,
858
- tEnvironment: gr_texture_environment,
859
- },
860
- output_resolution: view_ctx.resolution,
861
- });
862
- break;
863
-
864
- case ShadeIndirectLightingMode.Brick4:
865
- // Brick4 diffuse from volumetric lightmap
866
- [indirect_diffuse] = graph_image_pass({
867
- graph,
868
- shader: shader_brick4_diffuse,
869
- input_depth: gr_texture_depth_current,
870
- inputs: {
871
- tDepth: gr_texture_depth_current,
872
- tBentNormals: bent_normals,
873
- tAlbedoAO: gr_texture_material_albedo_ao,
874
- tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
875
- camera: gr_buffer_camera_current,
876
- view: view_ctx.uniform_buffer.buffer,
877
- brick4: scene_ctx.volumetric_light_map.buffer,
878
- },
879
- output_resolution: view_ctx.resolution,
880
- });
881
- break;
882
-
883
- case ShadeIndirectLightingMode.LPV:
884
- // Light Probe Volume diffuse
885
- [indirect_diffuse] = graph_image_pass({
886
- graph,
887
- shader: shader_diffuse_LPV,
888
- input_depth: gr_texture_depth_current,
889
- inputs: {
890
- camera: gr_buffer_camera_current,
891
- tDepth: gr_texture_depth_current,
892
- tBentNormals: bent_normals,
893
- tGBufferAlbedo: gr_texture_material_albedo_ao,
894
- //
895
- lpv_atlas_radiance: lpv.atlas.texture_radiance,
896
- lpv_atlas_depth: lpv.atlas.texture_depth,
897
- //
898
- lpv_lookup: lpv.buffer_mesh_bvh,
899
- lpv_metadata: lpv.buffer_metadata,
900
- lpv_mesh: lpv.buffer_mesh,
901
- lpv_probes: lpv.buffer_probes,
902
- },
903
- output_resolution: view_ctx.resolution,
904
- });
905
- break;
906
- }
907
-
908
- return { indirect_specular, indirect_diffuse };
909
- }
910
-
911
- /**
912
- * @type {PostProcess}
913
- */
914
- #postprocess
915
-
916
- /**
917
- * @type {ReSTIRDI}
918
- */
919
- #restir_di
920
-
921
- /**
922
- * ReSTIR DI subsystem — exposed for runtime tuning (e.g. GUI toggles like
923
- * `use_screen_space_shadow`, `m_initial`).
924
- * @returns {ReSTIRDI}
925
- */
926
- get restir_di() {
927
- return this.#restir_di;
928
- }
929
-
930
- /**
931
- *
932
- * @type {TAA}
933
- */
934
- #taa = new TAA();
935
-
936
- /**
937
- * Lazily constructed in {@link initialize} once a graphics context is
938
- * available — NSS owns persistent GPU textures, so it can't be a
939
- * field initializer like {@link #taa}.
940
- * @type {NSS | undefined}
941
- */
942
- #nss
943
-
944
- /**
945
- * Selects which temporal-AA / upscaler runs at the temporal-resolve
946
- * stage. Defaults to {@link ShadeUpscalerType.TAA}; assign
947
- * {@link ShadeUpscalerType.NSS} to use the bundled neural upscaler
948
- * (no extra setup — the NSS instance ships with placeholder weights).
949
- * @type {ShadeUpscalerType}
950
- */
951
- upscale_type = ShadeUpscalerType.TAA;
952
-
953
- /**
954
- * Public read access to the NSS instance. Lazily constructed on
955
- * first access, which is also when the bundled weights blob is
956
- * decoded so the cost of selecting NSS is amortized to a single
957
- * frame.
958
- * @returns {NSS}
959
- */
960
- get nss() {
961
- if (this.#nss === undefined) {
962
- assert.defined(this.#graphics, 'graphics context not yet initialized — call Renderer.initialize() first');
963
- this.#nss = new NSS(this.#graphics);
964
-
965
- // First-time setup: align the jitter sequence size with the
966
- // current upscale ratio. Subsequent resolution changes are
967
- // picked up by #update_internal_resolution.
968
- const out_w = this.#output_resolution.x;
969
- const in_w = this.#internal_resolution.x;
970
- if (out_w > 0 && in_w > 0) {
971
- const upscale_ratio = out_w / in_w;
972
- this.#nss.jitter_sequence_size = NSS.recommended_jitter_sequence_size(upscale_ratio);
973
- }
974
- }
975
- return this.#nss;
976
- }
977
-
978
- #device_lost = false;
979
-
980
- /**
981
- * Handles event when GPUDevice is lost
982
- * @param {GPUDeviceLostInfo} info
983
- */
984
- #handle_device_lost(info) {
985
- this.#device_lost = true;
986
-
987
- if (info.reason === "destroyed") {
988
- // most likely renderer was destroyed, this is expected behavior
989
- // TODO check if renderer owns the device, if not, log a warning
990
- return;
991
- }
992
-
993
- console.error('GPUDevice lost', info);
994
-
995
- // Everything the renderer built lives on the device that just went away, so there is
996
- // nothing to carry over and no attempt to (R6). Whoever is hosting this gets told, and a
997
- // reload is the way back.
998
- this.onDeviceFailure.send1(ShadeDeviceFailure.device_lost(info.message ?? info.reason ?? ""));
999
- }
1000
-
1001
- #hdr_supported = false;
1002
-
1003
- /**
1004
- * What's the peak brightness of the display?
1005
- * Only valid for HDR.
1006
- *
1007
- * @type {number}
1008
- */
1009
- #display_peak_brightness_nits = 1000;
1010
-
1011
- #update_presentation_format() {
1012
-
1013
- const format8bit = navigator.gpu.getPreferredCanvasFormat();
1014
-
1015
- const format = this.#hdr_supported ? "rgba16float" : format8bit;
1016
-
1017
- if (format === this.#presentation_format) {
1018
- return;
1019
- }
1020
-
1021
- this.#presentation_format = format;
1022
-
1023
- this.#context_needs_update = true;
1024
- }
1025
-
1026
- /**
1027
- * Needs to be a lambda, because we use it as a listener.
1028
- */
1029
- #update_hdr_support = () => {
1030
- const isHDR = window.matchMedia("(dynamic-range: high)").matches;
1031
-
1032
- const previous_value = this.#hdr_supported;
1033
-
1034
-
1035
- this.#hdr_supported = isHDR;
1036
-
1037
- if (isHDR) {
1038
- // Currently, there is no way to interrogate this from the browser, so going with an assumption
1039
- this.#display_peak_brightness_nits = 1000;
1040
- } else {
1041
- // Assume SDR nits
1042
- this.#display_peak_brightness_nits = 80;
1043
- }
1044
-
1045
- if (isHDR === previous_value) {
1046
- // no change
1047
- return;
1048
- }
1049
-
1050
- console.log(`HDR support: ${isHDR ? 'enabled' : 'disabled'}`);
1051
-
1052
- this.#update_presentation_format();
1053
-
1054
- this.#context_needs_update = true;
1055
- }
1056
-
1057
- /**
1058
- *
1059
- * @param {GPUCanvasContext} [context] Optional, if not provided - one will be created.
1060
- * @param {GPUDevice} [device] Optional, if not provided - one will be created.
1061
- * @param {number} [pixelRatio] Optiona, if not provided - will be taken from browser context
1062
- */
1063
- async initialize({
1064
- context,
1065
- device,
1066
- pixelRatio = window.devicePixelRatio,
1067
- } = {}) {
1068
-
1069
- if (!('gpu' in navigator)) {
1070
- throw new Error('navigator.gpu not available — WebGPU disabled or unsupported');
1071
- }
1072
-
1073
- this.#update_presentation_format();
1074
- this.#update_hdr_support();
1075
-
1076
- if (context === undefined) {
1077
- const canvas = document.createElement('canvas');
1078
- canvas.width = window.innerWidth;
1079
- canvas.height = window.innerHeight;
1080
- // Sensible default styling so a freshly-appended canvas covers
1081
- // the viewport without depending on the host page's CSS reset.
1082
- // Override `style` after `initialize()` returns if the host
1083
- // page wants the canvas embedded somewhere else.
1084
- canvas.style.cssText = 'position:fixed;inset:0;width:100vw;height:100vh;display:block';
1085
-
1086
- context = canvas.getContext('webgpu');
1087
-
1088
- if (context === null) {
1089
- throw new Error('Failed to bind GPUCanvasContext');
1090
- }
1091
- }
1092
-
1093
- if (device === undefined) {
1094
- // no device present, let's create one
1095
-
1096
- const gpu = navigator.gpu;
1097
-
1098
- if (gpu === undefined) {
1099
- throw ShadeDeviceFailure.webgpu_unavailable();
1100
- }
1101
-
1102
- const adapter = await gpu.requestAdapter({
1103
- powerPreference: "high-performance",
1104
- });
1105
-
1106
- if (adapter === null) {
1107
- throw ShadeDeviceFailure.adapter_unavailable();
1108
- }
1109
-
1110
- if (adapter.isFallbackAdapter) {
1111
- // see https://www.w3.org/TR/webgpu/#dom-gpurequestadapteroptions-forcefallbackadapter
1112
- // fallback device is typically a software implementation and is going to be slow as sin
1113
- console.warn('GPU provided a fallback adapter (typically because no other appropriate adapter was available). Fallback adapter is typically a software implementation and will be slow.');
1114
- }
1115
-
1116
- // log details of the adapter
1117
- console.log(`GPU adapter is ${adapter.info.vendor}/${adapter.info.architecture}, device='${adapter.info.device}', description='${adapter.info.description}'`);
1118
-
1119
- // console.warn(`Buffer size limit: ${number_format_by_thousands(adapter.limits.maxBufferSize)}`);
1120
- //
1121
- // console.warn('adapter limits:', adapter.limits);
1122
-
1123
- const maxStorageBuffersPerShaderStage = adapter.limits.maxStorageBuffersPerShaderStage;
1124
-
1125
- if (maxStorageBuffersPerShaderStage < 10) {
1126
- throw ShadeDeviceFailure.below_floor(
1127
- `requires at least 10 storage buffers per shader stage, adapter offers ${maxStorageBuffersPerShaderStage}`
1128
- );
1129
- }
1130
-
1131
- const requiredFeatures = [
1132
- // required for indirect draw that's used extensively in Shade
1133
- WebGPUExtensionType.IndirectFirstInstance,
1134
- // required for OIT (might be unavailable)
1135
- WebGPUExtensionType.Float32Blendable,
1136
- ];
1137
-
1138
- // add features if they are available
1139
- const optional_desired = [
1140
- // Profiling only, and some browsers withhold it on hardware that is otherwise
1141
- // fine so it is taken when offered and the timers go quiet when it is not
1142
- // (E7, sponsor 2026-08-16). Requiring it would refuse to start on those browsers
1143
- // for the sake of a feature the game does not need to run.
1144
- WebGPUExtensionType.TimestampQuery,
1145
- WebGPUExtensionType.Subgroups,
1146
- WebGPUExtensionType.TextureFormatsTier1,
1147
- ];
1148
-
1149
- // valid required features
1150
- for (const feature of requiredFeatures) {
1151
- if (!adapter.features.has(feature)) {
1152
- throw ShadeDeviceFailure.below_floor(`adapter does not support required feature '${feature}'`);
1153
- }
1154
- }
1155
-
1156
- for (const feature of optional_desired) {
1157
- if (adapter.features.has(feature)) {
1158
- requiredFeatures.push(feature);
1159
- }
1160
- }
1161
-
1162
-
1163
- device = await adapter.requestDevice({
1164
- requiredLimits: {
1165
- // required for large G-buffers
1166
- maxColorAttachmentBytesPerSample: 32,
1167
-
1168
- /*
1169
- grab the largest buffer limit we can get, default limits are super-low
1170
- */
1171
- maxBufferSize: adapter.limits.maxBufferSize,
1172
- maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
1173
-
1174
- maxStorageBuffersPerShaderStage: 10,
1175
- },
1176
- requiredFeatures
1177
- });
1178
-
1179
- // NOTE: according to spec (as of 2025/09/19) - adapter can only be used to create one device, after which it becomes "consumed"
1180
- // see https://www.w3.org/TR/webgpu/#adapters
1181
-
1182
- // TODO obtained device might be "lost" if something goes wrong, would make sense to check for that
1183
- }
1184
-
1185
- device.lost.then(info => {
1186
- this.#handle_device_lost(info);
1187
- })
1188
-
1189
- if (context === undefined) {
1190
- const canvas = document.createElement('canvas');
1191
- canvas.width = window.innerWidth;
1192
- canvas.height = window.innerHeight;
1193
-
1194
- context = canvas.getContext('webgpu');
1195
-
1196
- if (context === null) {
1197
- throw new Error('Failed to bind GPUCanvasContext');
1198
- }
1199
- }
1200
-
1201
- assert.ok(device.features.has("indirect-first-instance"), '"indirect-first-instance" feature needs to be enabled for non-zero firstInstance values to be used.')
1202
- assert.isInstanceOf(context, GPUCanvasContext, 'context', 'GPUCanvasContext');
1203
-
1204
- this.context = context;
1205
- this.device = device;
1206
-
1207
- this.#pixel_ratio = pixelRatio;
1208
-
1209
- // clientWidth/clientHeight are 0 for a canvas that is not attached to the DOM,
1210
- // which is always the case for the renderer-owned canvas created above.
1211
- // Fall back to the backing-store size so that #configure_context doesn't
1212
- // overwrite the just-set canvas size with the 1x1 minimum resolution.
1213
- this.#size.set(
1214
- context.canvas.clientWidth || context.canvas.width,
1215
- context.canvas.clientHeight || context.canvas.height
1216
- );
1217
-
1218
- this.#update_output_resolution();
1219
-
1220
- this.#graphics = new GraphicsContext(device);
1221
-
1222
- const graphics = this.#graphics;
1223
-
1224
- await graphics.initialize();
1225
-
1226
-
1227
- this.#scenes = new SceneManager(graphics);
1228
-
1229
- this.#cameras = new CameraManager(this.device);
1230
- this.#views = new ViewManager(graphics, this.#cameras, this.#scenes);
1231
-
1232
- const resolution_internal = this.#internal_resolution.asArray();
1233
-
1234
- for (let i = 0; i < this.#textures_depth.length; i++) {
1235
-
1236
- this.#textures_depth[i] = graphics.textures.contextFromDescriptor(TextureDescriptor.from({
1237
- label: `Depth ${i}`,
1238
- size: resolution_internal,
1239
- format: 'depth32float',
1240
- mipLevelCount: G_BUFFER_MIP_LEVELS,
1241
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC,
1242
- }));
1243
- }
1244
-
1245
-
1246
- const resolution_out = this.#output_resolution.asArray();
1247
-
1248
- for (let i = 0; i < this.#textures_taa_history.length; i++) {
1249
-
1250
- this.#textures_taa_history[i] = graphics.textures.contextFromDescriptor(TextureDescriptor.from({
1251
- label: `Color ${i}`,
1252
- size: resolution_out,
1253
- format: "rgba16float",
1254
- mipLevelCount: 1,
1255
- // STORAGE_BINDING is required for the NSS postprocess
1256
- // compute shader to write here. TAA continues to use
1257
- // RENDER_ATTACHMENT — the flags coexist on the same
1258
- // texture and the active upscaler picks its preferred
1259
- // write path each frame.
1260
- usage: GPUTextureUsage.RENDER_ATTACHMENT
1261
- | GPUTextureUsage.TEXTURE_BINDING
1262
- | GPUTextureUsage.STORAGE_BINDING
1263
- | GPUTextureUsage.COPY_SRC,
1264
- }));
1265
- }
1266
-
1267
- this.#autoexposure = new GPUCameraExposureManager(device);
1268
- this.#depth_of_field = new DepthOfField(device);
1269
- this.#depth_of_field_unreal = new DepthOfFieldU(device);
1270
- this.#motion_blur = new MotionBlur();
1271
-
1272
- this.#postprocess = new PostProcess(this.#graphics);
1273
-
1274
- this.#restir_di = new ReSTIRDI(this.#graphics);
1275
-
1276
- this.#configure_context();
1277
- this.init_render_targets();
1278
-
1279
- window.matchMedia("(dynamic-range: high)").addEventListener("change", this.#update_hdr_support);
1280
-
1281
- }
1282
-
1283
- /**
1284
- * Cleanup
1285
- */
1286
- destroy() {
1287
-
1288
- window.matchMedia("(dynamic-range: high)").removeEventListener("change", this.#update_hdr_support);
1289
- }
1290
-
1291
-
1292
- /**
1293
- * @type {RenderTarget}
1294
- */
1295
- #vis_render_target;
1296
-
1297
- init_render_targets() {
1298
- const vis_rt = new RenderTarget();
1299
- this.#vis_render_target = vis_rt;
1300
-
1301
- const textures = this.#graphics.textures;
1302
-
1303
- // TODO these can be moved to frame graph instead and managed in a transient manner
1304
- const internal_resolution_array = this.#internal_resolution.asArray();
1305
-
1306
- vis_rt.color_attachments = [
1307
- textures.contextFromDescriptor(TextureDescriptor.from({
1308
- size: internal_resolution_array,
1309
- format: "r32uint",
1310
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
1311
- })),
1312
- textures.contextFromDescriptor(TextureDescriptor.from({
1313
- size: internal_resolution_array,
1314
- format: "r32uint",
1315
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
1316
- })),
1317
- ];
1318
-
1319
- vis_rt.depth_attachment = this.#textures_depth[0];
1320
-
1321
- }
1322
-
1323
- /**
1324
- * If set to true, will force {@link #configure_context} on next render
1325
- * @type {boolean}
1326
- */
1327
- #context_needs_update = false;
1328
-
1329
- #configure_context() {
1330
- const context = this.context;
1331
-
1332
- const resolution = this.#output_resolution;
1333
-
1334
- context.canvas.width = resolution.x;
1335
- context.canvas.height = resolution.y;
1336
-
1337
- // canvas must be scaled on-screen differently from resolution to account for pixel ratio
1338
- const size = this.#size;
1339
- context.canvas.style.width = `${size.x}px`;
1340
- context.canvas.style.height = `${size.y}px`;
1341
-
1342
- const preferred_color_space = 'display-p3';
1343
-
1344
-
1345
- const config = {
1346
- device: this.device,
1347
- format: this.#presentation_format,
1348
- alphaMode: 'opaque',
1349
- colorSpace: preferred_color_space,
1350
- toneMapping: {
1351
- // see https://www.w3.org/TR/webgpu/#dom-gpucanvastonemappingmode-extended
1352
- mode: this.#hdr_supported ? "extended" : "standard"
1353
- }
1354
- };
1355
-
1356
-
1357
- try {
1358
- context.configure(config);
1359
- } catch (e) {
1360
- // try fall-back colorspace
1361
- context.configure({
1362
- ...config,
1363
- colorSpace: 'srgb'
1364
- });
1365
- }
1366
-
1367
- }
1368
-
1369
- #apply_size() {
1370
- const out_w = this.#output_resolution.x;
1371
- const out_h = this.#output_resolution.y;
1372
-
1373
- this.#textures_taa_history.forEach(t => t.resize(out_w, out_h));
1374
-
1375
-
1376
- this.#context_needs_update = true;
1377
- }
1378
-
1379
-
1380
- #update_internal_resolution() {
1381
- const out_w = this.#output_resolution.x;
1382
- const out_h = this.#output_resolution.y;
1383
-
1384
- const dimension_limit = this.device.limits.maxTextureDimension2D;
1385
-
1386
- const irs = this.#internal_resolution_scale;
1387
-
1388
- const _iw = clamp(Math.floor(out_w * irs), INTERNAL_RESOLUTION_MIN, dimension_limit);
1389
- const _ih = clamp(Math.floor(out_h * irs), INTERNAL_RESOLUTION_MIN, dimension_limit);
1390
-
1391
- this.#internal_resolution.set(_iw, _ih);
1392
-
1393
- // figure out exact x/y resolution scale
1394
- const irs_x_inv = out_w / _iw;
1395
- const irs_y_int = out_h / _ih;
1396
-
1397
- // we need to make TAA sequence long enough to provide full coverage over output resolution texels
1398
- const taa_sequence_scale = Math.max(1, irs_x_inv * irs_y_int);
1399
-
1400
- const upscale_factor = 1 / irs;
1401
-
1402
- // shrink the sequence as upscale factor increases
1403
- // we lose crispness of the edges, but we have to trade it to get faster convergence
1404
- const base_sequence_size = remap(
1405
- 1, 2,
1406
- 16, 4,
1407
- clamp(upscale_factor, 1, 2),
1408
- );
1409
-
1410
- this.#taa.jitter_sequence_size = Math.ceil(base_sequence_size * taa_sequence_scale);
1411
-
1412
- // ARM's NSS expects `8 * upscale_ratio^2` jitter phases. Apply the
1413
- // same coverage scaling as TAA so DPR-mismatch resolutions still
1414
- // get full coverage. Only applied if the NSS instance has been
1415
- // created no point spinning it up just to set a number.
1416
- if (this.#nss !== undefined) {
1417
- const nss_base = NSS.recommended_jitter_sequence_size(upscale_factor);
1418
- this.#nss.jitter_sequence_size = Math.ceil(nss_base * taa_sequence_scale);
1419
- }
1420
-
1421
- const in_w = this.#internal_resolution.x;
1422
- const in_h = this.#internal_resolution.y;
1423
-
1424
- this.#textures_depth.forEach(t => {
1425
- t.resize(in_w, in_h)
1426
- });
1427
-
1428
- this.#vis_render_target.resize(in_w, in_h);
1429
-
1430
- // resizing destroys BOTH ping-pong depth textures, including the one holding last
1431
- // frame's depth — disocclusion/SSAO would read a zeroed "previous depth" this frame
1432
- // and silently corrupt temporal history. The external resize() path resets history
1433
- // via indicate_view_change(); internal-resolution changes (DRS steps) must too.
1434
- this.indicate_view_change();
1435
-
1436
- this.#internal_resolution_needs_update = false;
1437
- }
1438
-
1439
- #update_output_resolution() {
1440
- const size = this.#size;
1441
- const pixel_ratio = this.#pixel_ratio;
1442
-
1443
- const dimension_limit = this.device.limits.maxTextureDimension2D;
1444
-
1445
- // limits are there to prevent crashing out
1446
- const w = clamp(Math.ceil(size.x * pixel_ratio), 1, dimension_limit);
1447
- const h = clamp(Math.ceil(size.y * pixel_ratio), 1, dimension_limit);
1448
-
1449
- this.#output_resolution.set(w, h);
1450
-
1451
- this.#internal_resolution_needs_update = true;
1452
- }
1453
-
1454
- /**
1455
- * Make sure to resize at the start of the frame to avoid flicker
1456
- *
1457
- * @param {number} x
1458
- * @param {number} y
1459
- */
1460
- resize(x, y) {
1461
- assert.isNonNegativeInteger(x, 'x');
1462
- assert.isNonNegativeInteger(y, 'y');
1463
-
1464
- const size = this.#size;
1465
-
1466
- if (size.x === x && size.y === y) {
1467
- // no change
1468
- return;
1469
- }
1470
-
1471
- size.set(x, y);
1472
-
1473
- this.#update_resolution();
1474
- }
1475
-
1476
- /**
1477
- * Does not check for changes, so make sure to call this sparingly
1478
- */
1479
- #update_resolution() {
1480
-
1481
- this.#update_output_resolution();
1482
-
1483
- if (this.#internal_resolution_needs_update) {
1484
- this.#update_internal_resolution();
1485
- }
1486
-
1487
- this.#apply_size();
1488
-
1489
- // drop history buffers to prevent artifacts
1490
- this.indicate_view_change();
1491
- }
1492
-
1493
- /**
1494
- * Bring the canvas context up to date with the size the renderer is drawing at.
1495
- *
1496
- * **Only {@link render} calls this, and that is the point.** Configuring a context resizes the
1497
- * canvas and invalidates the texture it last handed out, so it has to happen before the frame
1498
- * asks for one — and a frame going somewhere other than the canvas must not do it at all, or
1499
- * every offscreen picture would resize the page's canvas to its own size and back.
1500
- */
1501
- #ensure_context_configured() {
1502
- this.#update_resolution_if_needed();
1503
-
1504
- if (this.#context_needs_update) {
1505
- this.#configure_context();
1506
- this.#context_needs_update = false;
1507
- }
1508
- }
1509
-
1510
- #update_resolution_if_needed() {
1511
- if (this.#internal_resolution_needs_update) {
1512
- this.#update_internal_resolution();
1513
- }
1514
- }
1515
-
1516
- #begin_frame() {
1517
-
1518
- this.#update_resolution_if_needed();
1519
-
1520
- this.#graphics.update();
1521
-
1522
- // Virtual texture maintenance: apply arrived pages (budgeted
1523
- // uploads), flush page-table dirty rects and stack info, issue
1524
- // streaming loads. Queue-ordered before this frame's submits, so
1525
- // the material pass samples a consistent table+cache snapshot.
1526
- if (this.feature_virtual_textures) {
1527
- this.#graphics.virtual_textures.update();
1528
- }
1529
-
1530
- // cycle depth attachment
1531
- this.#vis_render_target.depth_attachment = this.texture_depth_current;
1532
- this.#postprocess.update(this.#frame_count);
1533
-
1534
- // Advance whichever upscaler we're going to ask for jitter on this
1535
- // frame. Both share the same Halton(2, 3) sequence; we only tick
1536
- // the active one to avoid spinning up NSS just to drive a counter.
1537
- if (this.upscale_type === ShadeUpscalerType.NSS) {
1538
- const nss = this.nss;
1539
- nss.frame_count = this.#frame_count;
1540
- nss.frame_index = this.#frame_count;
1541
- } else {
1542
- this.#taa.frame_index = this.#frame_count;
1543
- }
1544
- }
1545
-
1546
- #end_frame() {
1547
- this.#frame_count++;
1548
-
1549
- // dispatch event
1550
- this.onFrameFinished.send1(this.#frame_count);
1551
- }
1552
-
1553
- /**
1554
- * @type {AccumulatingPathTracer}
1555
- */
1556
- #path_tracer;
1557
-
1558
- get path_tracer() {
1559
- if (this.#path_tracer === undefined) {
1560
- this.#path_tracer = new AccumulatingPathTracer(this.#graphics);
1561
- }
1562
-
1563
- return this.#path_tracer;
1564
- }
1565
-
1566
- /**
1567
- * Draw a frame onto the canvas this renderer is bound to.
1568
- *
1569
- * The canvas is one target among others: this is {@link render_to_target} with the canvas's
1570
- * current texture as the target, and nothing else.
1571
- *
1572
- * @param {PerspectiveCamera} camera
1573
- * @param {Scene} scene
1574
- * @param {number} [time_delta_seconds]
1575
- * @returns {boolean} whether frame was rendered, will return false if previous frame hasn't finished yet
1576
- */
1577
- render(
1578
- camera,
1579
- scene,
1580
- time_delta_seconds = 0.01666
1581
- ) {
1582
- if (this.#device_lost) {
1583
- // device is lost, can't render — and asking the context for a texture would throw
1584
- return false;
1585
- }
1586
-
1587
- this.#ensure_context_configured();
1588
-
1589
- const canvas_texture = this.context.getCurrentTexture();
1590
-
1591
- return this.render_to_target(
1592
- camera,
1593
- scene,
1594
- time_delta_seconds,
1595
- GPUTextureContext.fromTexture(canvas_texture, this.device)
1596
- );
1597
- }
1598
-
1599
- /**
1600
- * Draw a frame into a texture of the caller's choosing.
1601
- *
1602
- * **The target says where the picture goes, not how big it is.** Everything the frame is built
1603
- * out of — the G-buffer, the visibility buffer, the depth ping-pong, the temporal history — is
1604
- * sized from {@link output_resolution}, so a target has to be that size; a caller wanting a
1605
- * different one calls {@link resize} first and puts the size back afterwards. Rendering into a
1606
- * texture the caller owns is what makes a frame readable: a canvas texture is configured by the
1607
- * presentation path and cannot carry `COPY_SRC`, and one of these can.
1608
- *
1609
- * @param {PerspectiveCamera} camera
1610
- * @param {Scene} scene
1611
- * @param {number} time_delta_seconds
1612
- * @param {GPUTextureContext} target must match {@link output_resolution}
1613
- * @returns {boolean} whether frame was rendered, will return false if previous frame hasn't finished yet
1614
- */
1615
- render_to_target(
1616
- camera,
1617
- scene,
1618
- time_delta_seconds,
1619
- target
1620
- ) {
1621
- assert.defined(target, 'target');
1622
- assert.equal(target.isGPUTextureContext, true, 'target.isGPUTextureContext !== true');
1623
-
1624
- if (this.#device_lost) {
1625
- // device is lost, can't render
1626
- return false;
1627
- }
1628
-
1629
- this.#begin_frame();
1630
-
1631
-
1632
- if (this.indirect_lighting_mode === ShadeIndirectLightingMode.LPV) {
1633
- this.update_lpv(scene);
1634
- }
1635
-
1636
- const view = View.from(camera, scene);
1637
- view.label = "Main View";
1638
- const view_ctx = this.#views.obtain(view);
1639
-
1640
- const graphics = this.#graphics;
1641
-
1642
- // Shadow allocation + per-light shadow_id assignment happens inside
1643
- // GPULightCollection.update (which view_ctx.update will trigger via
1644
- // GPUSceneContext.update below), so by the time we reach select_for_draw the
1645
- // light records on the GPU already carry their up-to-date shadow_id. Forward the
1646
- // feature flag so process_lights inside lights.update can evict everything when
1647
- // the renderer has shadows turned off — keeps CASTS_SHADOW_BIT cleared on every
1648
- // light record without further plumbing.
1649
- const sm = view_ctx.scene.lights.shadow_context;
1650
- sm.enabled = this.#feature_shadows_enabled;
1651
- // Which point-light shadow generator to use this frame; a flip triggers a rebuild of
1652
- // existing point maps inside process_lights (run via view_ctx.update above/below).
1653
- sm.use_tetrahedron_point_shadows = this.feature_tetrahedron_point_shadows;
1654
-
1655
- const taa = this.#taa;
1656
-
1657
- // Whichever upscaler is going to consume the temporal sequence
1658
- // also has to dictate the jitter applied to the camera projection.
1659
- // Both share Halton(2, 3); they just drive different jitter
1660
- // sequence sizes via #update_internal_resolution.
1661
- const jitter_source = this.upscale_type === ShadeUpscalerType.NSS
1662
- ? this.nss
1663
- : taa;
1664
-
1665
- const resolution_internal = this.#internal_resolution;
1666
- {
1667
- const jitter_x = jitter_source.Jitter[0];
1668
- const jitter_y = jitter_source.Jitter[1];
1669
-
1670
- view_ctx.setJitter(jitter_x, jitter_y);
1671
- view_ctx.setViewportSize(resolution_internal.x, resolution_internal.y);
1672
- view_ctx.setUpscaleRatio(
1673
- this.#output_resolution.x / resolution_internal.x,
1674
- this.#output_resolution.y / resolution_internal.y
1675
- );
1676
- // 2x offsets because the offsets are in NDC (-1, 1)
1677
- view_ctx.camera.setViewportOffset(
1678
- (2 * jitter_x) / resolution_internal.x,
1679
- (2 * jitter_y) / resolution_internal.y
1680
- );
1681
-
1682
- }
1683
-
1684
- view_ctx.update(graphics);
1685
-
1686
- const command_ctx = ShadeGPUCommandContext.create(
1687
- graphics,
1688
- "Renderer/main-0"
1689
- );
1690
-
1691
- if (this.#debug_frame_count > 0) {
1692
- this.#debug_frame_count--;
1693
-
1694
- const current_frame = this.#frame_count;
1695
-
1696
- command_ctx.enable_debug_timers((table) => {
1697
- this.onFrameDebug.send2(current_frame, table);
1698
- });
1699
- }
1700
-
1701
- // Per-frame animation tick — runs before shadow allocation so
1702
- // animated transforms (joint poses, mesh.global from animated
1703
- // parents) are in the scene database by the time shadow
1704
- // culling samples it. No-op when nothing's registered.
1705
- view_ctx.scene.tick(command_ctx, time_delta_seconds);
1706
-
1707
- const viz_rt = this.#vis_render_target;
1708
-
1709
- // prepare shadows (process_lights already ran inside lights.update via view_ctx.update,
1710
- // so the GPU light records already carry their up-to-date shadow_id)
1711
- if (this.#feature_shadows_enabled) {
1712
- // Two-stage: (1) score+select which maps refresh this frame and publish their metadata,
1713
- // (2) rasterize the selected maps. The context internally dispatches to the right
1714
- // raster path per light type — directional/spot go direct, point lights do a cube
1715
- // pass plus octahedral remap — so we don't have to juggle job arrays out here.
1716
- // draw() also writes per-shadow records into the scene's GPU light database
1717
- // (shadow_point/shadow_spot/shadow_directional tables) via its owner reference.
1718
- sm.select_for_draw(camera, view_ctx.scene, view_ctx.frame_index, view_ctx.resolution);
1719
- sm.draw(command_ctx);
1720
- }
1721
-
1722
-
1723
- const graph = new FrameGraph("Shading");
1724
-
1725
- /*
1726
- The view is updated and nothing has been rasterized, so an extension here is doing the
1727
- per-frame compute that later phases depend on. There are no records yet — the frame has
1728
- built nothing to hand out.
1729
- */
1730
- const frame = new FrameContext(graph, view_ctx);
1731
-
1732
- frame.phase = FramePhase.FrameStart;
1733
-
1734
- this.#extensions.run(frame);
1735
-
1736
- // build main view rasterization job
1737
- const raster_main = new RasterizationJob();
1738
- raster_main.view_ctx = view_ctx;
1739
- raster_main.render_target = viz_rt;
1740
- raster_main.opaque_pass = viz_rasterization_opaque_pass_descriptor;
1741
- raster_main.alpha_tested_pass = viz_rasterization_alpha_tested_pass_descriptor;
1742
-
1743
- const gr_render_buckets = graph_rasterize_scene({
1744
- job: raster_main,
1745
- graph,
1746
- limits: graphics.collection_limits,
1747
- });
1748
-
1749
- // `let` rather than `const` because an extension at `AfterGBuffer` may publish replacements
1750
- let gr_viz_buffer_triangle = graph_import_texture(graph, this.#vis_render_target.color_attachments[0], "viz_triangle");
1751
- let gr_viz_buffer_mesh = graph_import_texture(graph, this.#vis_render_target.color_attachments[1], "viz_mesh");
1752
-
1753
- // Virtual texture feedback: derive per-pixel page usage from the viz
1754
- // buffer (no extra geometry pass) into a unique-request hash set and
1755
- // kick its async readback. No-op unless VT stacks are registered.
1756
- if (this.feature_virtual_textures) {
1757
- graphics.virtual_textures.graph_feedback({
1758
- graph,
1759
- view: view_ctx,
1760
- texture_viz_mesh: gr_viz_buffer_mesh,
1761
- texture_viz_triangle: gr_viz_buffer_triangle,
1762
- material_metadata_buffer: graphics.materials.metadata_table.buffer,
1763
- });
1764
- }
1765
-
1766
- // run materials
1767
- const mat_g = graphics.materials.graph_viz_render({
1768
- graph,
1769
- view: view_ctx,
1770
- texture_viz_mesh: gr_viz_buffer_mesh,
1771
- texture_viz_triangle: gr_viz_buffer_triangle,
1772
- });
1773
-
1774
- // update texture references
1775
- // `let` rather than `const` because a pass injected at `AfterGBuffer` may replace any of
1776
- // them; see the read-back below.
1777
- let gr_texture_pbr = mat_g.g_pbr;
1778
- let gr_texture_normal = mat_g.g_normal;
1779
- let gr_texture_material_albedo_ao = mat_g.g_albedo;
1780
- let gr_texture_material_emissive = mat_g.g_emissive;
1781
-
1782
- // do mips for G-buffer
1783
- // mipmap_nearest(command_ctx, this.texture_depth_current);
1784
- // this.gBuffer.mipmap(command_ctx);
1785
-
1786
- // Update lights position
1787
- command_ctx.pushDebugGroup("Shade");
1788
-
1789
- const resolution = resolution_internal.asArray();
1790
-
1791
- const scene_ctx = view_ctx.scene;
1792
- const lights = scene_ctx.lights;
1793
-
1794
-
1795
- const gr_texture_canvas = graph_import_texture(graph, target, "canvas");
1796
- let gr_texture_depth_current = graph_import_texture(graph, this.texture_depth_current, "depth");
1797
- const gr_buffer_camera_current = graph_import_buffer(graph, view_ctx.camera.gpu_buffer, "camera/current");
1798
- const gr_buffer_camera_previous = graph_import_buffer(graph, view_ctx.gpu_previous_camera_state.gpu_buffer, "camera/previous");
1799
- const gr_texture_hzb = graph_import_texture(graph, view_ctx.hierarchical_z_buffer.texture, "HZB");
1800
- const gr_texture_environment = graph_import_texture(graph, view_ctx.scene.lights.environment);
1801
-
1802
- const taa_history_0 = this.#get_texture_taa_history(0);
1803
- const taa_history_1 = this.#get_texture_taa_history(-1);
1804
-
1805
- const gr_texture_taa_history = graph_import_texture(graph, taa_history_0, "taa history");
1806
- const gr_texture_taa_out = graph_import_texture(graph, taa_history_1, "taa out");
1807
- let gr_texture_depth_previous = graph_import_texture(graph, this.texture_depth_previous, "depth previous");
1808
-
1809
- const gr_shadowmap_atlas = graph_import_texture(graph, sm.texture);
1810
-
1811
- // The frame's surface handles exist now, so this is where the records that carry them are
1812
- // published. There is no colour yet, which is what makes this the phase for G-buffer work.
1813
- frame.phase = FramePhase.AfterGBuffer;
1814
-
1815
- const record_gbuffer = frame.create(GBufferTextures);
1816
- const record_view_textures = frame.create(ViewTextures);
1817
-
1818
- record_gbuffer.albedo = gr_texture_material_albedo_ao;
1819
- record_gbuffer.normal = gr_texture_normal;
1820
- record_gbuffer.pbr = gr_texture_pbr;
1821
- record_gbuffer.emissive = gr_texture_material_emissive;
1822
-
1823
- record_view_textures.depth = gr_texture_depth_current;
1824
- record_view_textures.depth_previous = gr_texture_depth_previous;
1825
- record_view_textures.visibility_mesh = gr_viz_buffer_mesh;
1826
- record_view_textures.visibility_triangle = gr_viz_buffer_triangle;
1827
-
1828
- this.#extensions.run(frame);
1829
-
1830
- /*
1831
- `GBufferTextures` is in/out at this phase, and frozen after it. An extension that wants to
1832
- *change* surface properties — decals are the reason this exists — cannot blend into these
1833
- targets: two of the four are integer formats, and one texture cannot be a render attachment
1834
- and a sampled source in the same pass. So it reads them, writes replacements, and publishes
1835
- the new handles. Everything downstream carries what it left.
1836
-
1837
- Writing the originals in place would work by accident rather than by contract: a written
1838
- handle is a *renamed* clone of the same physical resource, so the change would land in the
1839
- texture the shading pass reads only because the graph happens to execute in insertion
1840
- order, with no dependency edge saying so. Threading the handles makes the edge real.
1841
- */
1842
- gr_texture_pbr = record_gbuffer.pbr;
1843
- gr_texture_normal = record_gbuffer.normal;
1844
- gr_texture_material_albedo_ao = record_gbuffer.albedo;
1845
- gr_texture_material_emissive = record_gbuffer.emissive;
1846
-
1847
- gr_texture_depth_current = record_view_textures.depth;
1848
- gr_texture_depth_previous = record_view_textures.depth_previous;
1849
- gr_viz_buffer_mesh = record_view_textures.visibility_mesh;
1850
- gr_viz_buffer_triangle = record_view_textures.visibility_triangle;
1851
-
1852
- // Velocity pass with three branches: rotation-reprojection
1853
- // background, rigid per-object foreground (mesh.global delta),
1854
- // and skinned per-vertex foreground (barycentric interpolation
1855
- // of the triangle's mesh-local `position_prev` written by
1856
- // skinning into the side-buffer the skinning context owns).
1857
- // Per-vertex limb deformation lands in the final pixel-space
1858
- // velocity, so TAA reads correct history for character animation.
1859
- const gr_scene_database_for_velocity = graph_import_buffer(
1860
- graph, scene_ctx.scene_database_buffer, "scene_database/velocity"
1861
- );
1862
- const gr_meshlet_metadata_for_velocity = graph_import_buffer(
1863
- graph, scene_ctx.geometries.meshlets.buffer_metadata, "meshlet_metadata/velocity"
1864
- );
1865
- const gr_meshlet_data_for_velocity = graph_import_buffer(
1866
- graph, scene_ctx.geometries.meshlets.buffer_data, "meshlet_data/velocity"
1867
- );
1868
- // Skinning side-buffers — both null when no SkinnedMesh has
1869
- // been registered. The velocity shader still references these
1870
- // bindings (the dormant per-vertex branch), so the bind group
1871
- // must supply *something*; use the scene_database buffer as a
1872
- // placeholder when skinning is inactive. The shader is told via
1873
- // `skinning_active` whether the bindings are real when false
1874
- // it never reads them and forces the rigid path, so the
1875
- // placeholder is never sampled. (Binding the placeholder
1876
- // without that guard was the source of phantom velocity on
1877
- // static meshes: `prev_position_offsets[meshlet_id]` read an
1878
- // unrelated scene_database word, and any non-0xFFFFFFFF value
1879
- // sent a static triangle down the skinned branch.)
1880
- const skinning_ctx = scene_ctx.skinning;
1881
- const skinning_active = skinning_ctx.prev_position_offsets_buffer !== null
1882
- && skinning_ctx.prev_positions_buffer !== null;
1883
- const gr_prev_position_offsets = graph_import_buffer(
1884
- graph,
1885
- skinning_ctx.prev_position_offsets_buffer ?? scene_ctx.scene_database_buffer,
1886
- "prev_position_offsets/velocity",
1887
- );
1888
- const gr_prev_positions = graph_import_buffer(
1889
- graph,
1890
- skinning_ctx.prev_positions_buffer ?? scene_ctx.scene_database_buffer,
1891
- "prev_positions/velocity",
1892
- );
1893
-
1894
- let gr_velocity = graph_add_per_object_velocity({
1895
- graph,
1896
- texture_depth: gr_texture_depth_current,
1897
- texture_viz_mesh: gr_viz_buffer_mesh,
1898
- texture_viz_triangle: gr_viz_buffer_triangle,
1899
- buffer_scene_database: gr_scene_database_for_velocity,
1900
- buffer_meshlet_metadata: gr_meshlet_metadata_for_velocity,
1901
- buffer_meshlet_data: gr_meshlet_data_for_velocity,
1902
- buffer_prev_position_offsets: gr_prev_position_offsets,
1903
- buffer_prev_positions: gr_prev_positions,
1904
- skinning_active,
1905
- resolution,
1906
- view: view_ctx,
1907
- enable_mipmap: true
1908
- });
1909
-
1910
- // gr_velocity = graph_mipmap_nearest(graph, gr_velocity, G_BUFFER_MIP_LEVELS);
1911
-
1912
- let [gr_occlusion_clip] = graph_image_pass({
1913
- graph,
1914
- shader: shader_depth_occlusion_clip,
1915
- output_resolution: view_ctx.resolution,
1916
- // mipmaps are going to be necessary for half-resolution post-process passes
1917
- output_mipmaps: true,
1918
- inputs: {
1919
- camera_current: gr_buffer_camera_current,
1920
- camera_previous: gr_buffer_camera_previous,
1921
- DepthBuffer: gr_texture_depth_current,
1922
- PrevDepthBuffer: gr_texture_depth_previous,
1923
- VelocityBuffer: gr_velocity
1924
- }
1925
- });
1926
-
1927
- const light_clusters = graph_build_light_clusters({
1928
- graph,
1929
- lights: lights,
1930
- camera: view_ctx.camera,
1931
- hzb: gr_texture_hzb,
1932
- resolution: view_ctx.resolution,
1933
- });
1934
-
1935
- // Build the view's volumetric fog LUTs (participating media -> per-froxel lighting ->
1936
- // ray-marched integration). Needs the light clusters and shadow atlas. Built every frame so
1937
- // the transparency (OIT) pass always has a valid LUT to sample — an empty scene integrates
1938
- // to a no-op (transmittance 1, in-scatter 0). The opaque composite below is still skipped
1939
- // when the scene has no fog.
1940
- const has_volumetrics = view_ctx.scene.volumetrics.source.volumes.length > 0;
1941
-
1942
- const volumetrics = view_ctx.volumetrics.graph_update({
1943
- graph,
1944
- light_cluster_data: light_clusters.data,
1945
- light_cluster_lookup: light_clusters.lookup,
1946
- light_cluster_parameters: light_clusters.parameters,
1947
- shadowmap_atlas: gr_shadowmap_atlas,
1948
- });
1949
-
1950
- /**
1951
- *
1952
- * @type {number}
1953
- */
1954
- let current_color_out = -1;
1955
-
1956
- // ReSTIR DI — stochastic direct lighting of local (point+spot) lights.
1957
- // When enabled, resolve per-pixel point+spot radiance and shade with the
1958
- // ReSTIR deferred variant (no brute-force cluster loop). The variant is a
1959
- // separate compiled shader, selected here at the JS level rather than via
1960
- // a runtime branch inside the shader.
1961
- let deferred_shader = shader_deferred_main;
1962
-
1963
- const deferred_inputs = {
1964
- gBufferDepth: gr_texture_depth_current,
1965
- gBufferPBR: gr_texture_pbr,
1966
- gBufferNormal: gr_texture_normal,
1967
- gBufferAlbedo: gr_texture_material_albedo_ao,
1968
- gBufferEmissive: gr_texture_material_emissive,
1969
-
1970
- //
1971
- light_data: lights.buffer_data,
1972
- tEnvironment: gr_texture_environment,
1973
-
1974
- // light clusters
1975
- cluster_parameters: light_clusters.parameters,
1976
- cluster_lookup: light_clusters.lookup,
1977
- cluster_data: light_clusters.data,
1978
-
1979
- // shadows
1980
- shadowmapAtlas: gr_shadowmap_atlas,
1981
-
1982
- //
1983
- camera: view_ctx.camera.buffer,
1984
- view: view_ctx.uniform_buffer.buffer,
1985
- };
1986
-
1987
- if (this.feature_restir_di_enabled) {
1988
- deferred_shader = shader_deferred_main_restir;
1989
-
1990
- deferred_inputs.tRestirRadiance = this.#restir_di.execute({
1991
- graph,
1992
- view: view_ctx,
1993
- depth: gr_texture_depth_current,
1994
- pbr: gr_texture_pbr,
1995
- normal: gr_texture_normal,
1996
- albedo: gr_texture_material_albedo_ao,
1997
- emissive: gr_texture_material_emissive,
1998
- velocity: gr_velocity,
1999
- occlusion_clip: gr_occlusion_clip,
2000
- hzb: gr_texture_hzb,
2001
- cluster_parameters: light_clusters.parameters,
2002
- cluster_lookup: light_clusters.lookup,
2003
- cluster_data: light_clusters.data,
2004
- });
2005
- }
2006
-
2007
- // Main shading pass
2008
- [current_color_out] = graph_image_pass({
2009
- graph,
2010
- shader: deferred_shader,
2011
- input_depth: gr_texture_depth_current,
2012
- inputs: deferred_inputs,
2013
- output_resolution: view_ctx.resolution,
2014
- });
2015
-
2016
-
2017
- let bent_normals = -1;
2018
-
2019
- if (this.feature_ssao_enabled) {
2020
-
2021
- const ssao = this.#postprocess.ssao.graph_pass({
2022
- graph: graph,
2023
- frame_index: this.#frame_count,
2024
- input_depth: gr_texture_depth_current,
2025
- input_color: current_color_out,
2026
- input_depth_previous: gr_texture_depth_previous,
2027
- input_velocity: gr_velocity,
2028
- input_normal: gr_texture_normal,
2029
- input_occlusion: gr_occlusion_clip,
2030
- // NOTE: AO is blended into the G-buffer
2031
- output: gr_texture_material_albedo_ao,
2032
- camera: gr_buffer_camera_current,
2033
- resolution: resolution,
2034
- });
2035
-
2036
- gr_texture_material_albedo_ao = ssao.occlusion;
2037
- bent_normals = ssao.bent_normals;
2038
-
2039
- } else {
2040
-
2041
- // default to shading normals
2042
- bent_normals = gr_texture_normal;
2043
-
2044
- }
2045
-
2046
-
2047
- // Shade background
2048
- [current_color_out] = graph_image_pass({
2049
- graph,
2050
- shader: shader_background,
2051
- inputs: {
2052
- camera: view_ctx.camera.buffer,
2053
- view: view_ctx.uniform_buffer.buffer,
2054
- tEnvironment: gr_texture_environment,
2055
- },
2056
- input_depth: gr_texture_depth_current,
2057
- load_op: "load",
2058
- output: [current_color_out],
2059
- });
2060
-
2061
- {
2062
- // Fused brick4 path: skip the intermediate diffuse/specular targets and the
2063
- // separate resolve, doing it all in a single shader. SSR isn't compatible
2064
- // since it consumes the standalone specular target.
2065
- const use_fused_indirect = this.fused_indirect
2066
- && this.indirect_lighting_mode === ShadeIndirectLightingMode.Brick4
2067
- && !this.feature_ssr_enabled;
2068
-
2069
- if (use_fused_indirect) {
2070
-
2071
- [current_color_out] = graph_image_pass({
2072
- graph,
2073
- shader: shader_brick4_indirect_lighting,
2074
- output: [current_color_out],
2075
- input_depth: gr_texture_depth_current,
2076
- load_op: "load",
2077
- inputs: {
2078
- tDepth: gr_texture_depth_current,
2079
- tGBufferNormals: gr_texture_normal,
2080
- tBentNormals: bent_normals,
2081
- tGBufferAlbedo: gr_texture_material_albedo_ao,
2082
- tGBufferPBR: gr_texture_pbr,
2083
- tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
2084
- view: view_ctx.uniform_buffer.buffer,
2085
- camera: gr_buffer_camera_current,
2086
- //
2087
- brick4: scene_ctx.volumetric_light_map.buffer,
2088
- tSplitSum: STATIC_GRAPHICS_ENGINE_ASSETS.split_sum,
2089
- },
2090
- });
2091
-
2092
- } else {
2093
-
2094
- const { indirect_specular, indirect_diffuse } = this.#graph_indirect_lighting({
2095
- graph,
2096
- view_ctx,
2097
- scene_ctx,
2098
- bent_normals,
2099
- current_color_out,
2100
- //
2101
- gr_texture_depth_current,
2102
- gr_texture_hzb,
2103
- gr_texture_material_albedo_ao,
2104
- gr_texture_pbr,
2105
- gr_texture_normal,
2106
- gr_texture_environment,
2107
- gr_velocity,
2108
- gr_occlusion_clip,
2109
- gr_buffer_camera_current,
2110
- gr_buffer_camera_previous,
2111
- resolution,
2112
- });
2113
-
2114
- // resolve indirect lighting
2115
- [current_color_out] = graph_image_pass({
2116
- graph,
2117
- shader: shader_resolve_indirect_lighting,
2118
- output: [current_color_out],
2119
- input_depth: gr_texture_depth_current,
2120
- load_op: "load",
2121
- inputs: {
2122
- tIndirectDiffuse: indirect_diffuse,
2123
- tIndirectSpecular: indirect_specular,
2124
- //
2125
- tGBufferNormals: gr_texture_normal,
2126
- tBentNormals: bent_normals,
2127
- tGBufferAlbedo: gr_texture_material_albedo_ao,
2128
- tGBufferPBR: gr_texture_pbr,
2129
- tDepth: gr_texture_depth_current,
2130
- //
2131
- camera: gr_buffer_camera_current,
2132
- //
2133
- tSplitSum: STATIC_GRAPHICS_ENGINE_ASSETS.split_sum,
2134
- },
2135
- });
2136
-
2137
- }
2138
-
2139
- // current_color_out = indirect_diffuse
2140
- // current_color_out = indirect_specular
2141
-
2142
- }
2143
-
2144
- // current_color_out = graph_decode_rgbe9995_to_rgb16float({
2145
- // graph,
2146
- // input:indirect_diffuse_near_field,
2147
- // resolution: view_ctx.resolution,
2148
- // });
2149
- //
2150
- // const [norm] = graph_image_pass({
2151
- // shader: shader_decode_octahedral_normal,
2152
- // inputs:{
2153
- // tInput: bent_normals,
2154
- // },
2155
- // graph,
2156
- // output_resolution: view_ctx.resolution,
2157
- // });
2158
- //
2159
- // [current_color_out] = graph_image_pass({
2160
- // graph,
2161
- // shader: shader_draw_normals,
2162
- // inputs:{
2163
- // tInput: norm,
2164
- // },
2165
- // output_resolution: view_ctx.resolution,
2166
- // });
2167
-
2168
- // Composite volumetric fog over the finalized opaque + background + indirect colour.
2169
- // A dedicated pass (not inline in the shading shaders) because indirect lighting is added
2170
- // in a separate additively-blended pass, so the full surface radiance only exists here.
2171
- // Runs before transparency (transparent surfaces blend over the fog) and before the main
2172
- // TAA (which then smooths the fog's per-pixel sampling noise).
2173
- if (has_volumetrics) {
2174
- [current_color_out] = graph_image_pass({
2175
- graph,
2176
- shader: shader_composite_volumetrics,
2177
- output_resolution: view_ctx.resolution,
2178
- inputs: {
2179
- tColor: current_color_out,
2180
- gBufferDepth: gr_texture_depth_current,
2181
- tVolumetrics: volumetrics.resolved,
2182
- tSTBN_vec3: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec3,
2183
- volumetrics_metadata: volumetrics.metadata,
2184
- camera: view_ctx.camera.buffer,
2185
- view: view_ctx.uniform_buffer.buffer,
2186
- },
2187
- });
2188
- }
2189
-
2190
- /*
2191
- The frame has a colour for the first time, so this is where `SceneColor` is published. It is
2192
- republished before each later phase because the built-in passes between them replace it —
2193
- the record names which handle currently *means* the scene colour, and the graph already
2194
- holds the dependency edge underneath.
2195
- */
2196
- frame.phase = FramePhase.AfterLighting;
2197
-
2198
- const record_scene_color = frame.create(SceneColor);
2199
-
2200
- record_scene_color.color = current_color_out;
2201
-
2202
- this.#extensions.run(frame);
2203
-
2204
- current_color_out = record_scene_color.color;
2205
- gr_texture_depth_current = record_view_textures.depth;
2206
-
2207
- if (this.#feature_enabled_transparencies) {
2208
- // do transparent alpha pass
2209
- current_color_out = graph_rasterize_meshes_transparent_oit({
2210
- graph,
2211
- buckets: gr_render_buckets,
2212
- view_ctx,
2213
- viewport: [0, 0, resolution_internal.x, resolution_internal.y],
2214
- color_texture: current_color_out,
2215
- depth_texture: gr_texture_depth_current,
2216
-
2217
- limits: this.graphics.collection_limits,
2218
-
2219
- light_cluster_data: light_clusters.data,
2220
- light_cluster_lookup: light_clusters.lookup,
2221
- light_cluster_parameters: light_clusters.parameters,
2222
-
2223
- shadowmap_atlas: gr_shadowmap_atlas,
2224
-
2225
- volumetrics_lut: volumetrics.resolved,
2226
- volumetrics_metadata: volumetrics.metadata,
2227
-
2228
- indirect_lighting_mode: this.indirect_lighting_mode,
2229
- });
2230
- }
2231
-
2232
- frame.phase = FramePhase.AfterTransparency;
2233
-
2234
- record_scene_color.color = current_color_out;
2235
-
2236
- this.#extensions.run(frame);
2237
-
2238
- current_color_out = record_scene_color.color;
2239
- gr_texture_depth_current = record_view_textures.depth;
2240
-
2241
- // current_color_out = gr_texture_material_albedo_ao;
2242
-
2243
- if (this.feature_path_tracing_enabled) {
2244
- current_color_out = this.path_tracer.render({
2245
- graph,
2246
- view: view_ctx
2247
- });
2248
- }
2249
-
2250
- // Debug override: replace the post-shading color with a
2251
- // visualization of the velocity buffer via shader_debug_velocity.
2252
- // Direction is encoded as hue (red = +X, cyan = -X, etc.) and
2253
- // magnitude as brightness; the AO channel of the albedo+AO
2254
- // G-buffer modulates the static base so geometry shape is
2255
- // still readable where motion is zero or near-zero. Done
2256
- // BEFORE TAA so the chain stays well-formed; TAA blending
2257
- // the debug view is meaningless, but it's debug.
2258
- if (this.feature_velocity_debug_view) {
2259
- [current_color_out] = graph_image_pass({
2260
- graph,
2261
- shader: shader_debug_velocity,
2262
- inputs: {
2263
- tInput: gr_velocity,
2264
- tAlbedoAO: gr_texture_material_albedo_ao,
2265
- },
2266
- output_resolution: resolution_internal.asArray(),
2267
- });
2268
- }
2269
-
2270
- if (this.feature_taa_enabled) {
2271
-
2272
- if (this.upscale_type === ShadeUpscalerType.NSS) {
2273
- current_color_out = this.nss.graph_pass({
2274
- graph,
2275
- texture_color_current: current_color_out,
2276
- texture_depth_current: gr_texture_depth_current,
2277
- texture_velocity: gr_velocity,
2278
- texture_disocclusion_confidence: gr_occlusion_clip,
2279
- texture_color_history: gr_texture_taa_history,
2280
- texture_output: gr_texture_taa_out,
2281
- render_resolution: resolution_internal.asArray(),
2282
- output_resolution: this.#output_resolution.asArray(),
2283
- });
2284
- } else {
2285
- current_color_out = taa.graph_pass({
2286
- graph,
2287
- texture_output: gr_texture_taa_out,
2288
- texture_color_current: current_color_out,
2289
- texture_color_history: gr_texture_taa_history,
2290
- texture_velocity: gr_velocity,
2291
- texture_occlusion: gr_occlusion_clip,
2292
- camera_current: gr_buffer_camera_current,
2293
- camera_previous: gr_buffer_camera_previous,
2294
- });
2295
- }
2296
- }
2297
-
2298
- // Bokeh depth of field on the resolved HDR colour. Runs after
2299
- // TAA (blurs a clean, converged image — TAA's neighbourhood
2300
- // clamp would otherwise fight the defocus) and before motion
2301
- // blur / bloom, so out-of-focus highlights bloom and the bokeh
2302
- // carries through the rest of the post chain. Perspective-only:
2303
- // the thin-lens CoC has no meaning under orthographic.
2304
- if (this.feature_dof_enabled && camera.isPerspectiveCamera === true) {
2305
- // Both implementations share the same render() signature, so the
2306
- // call site is identical — only the subsystem differs.
2307
- const dof = this.dof_algorithm === "unreal"
2308
- ? this.#depth_of_field_unreal
2309
- : this.#depth_of_field;
2310
-
2311
- current_color_out = dof.render({
2312
- graph,
2313
- input_color: current_color_out,
2314
- depth: gr_texture_depth_current,
2315
- camera,
2316
- camera_buffer: view_ctx.camera.buffer,
2317
- resolution: this.#output_resolution.asArray(),
2318
- });
2319
- }
2320
-
2321
- // Jimenez 2014 motion blur runs on the resolved-but-not-
2322
- // sharpened HDR colour. TAA has already converged the
2323
- // jittered samples; sharpening, bloom, and tonemap operate
2324
- // on the blurred output after this. Skipped when the toggle
2325
- // is off so the rest of the chain stays bit-identical.
2326
- if (this.feature_motion_blur_enabled) {
2327
- current_color_out = this.#motion_blur.render({
2328
- graph,
2329
- input_color: current_color_out,
2330
- velocity: gr_velocity,
2331
- depth: gr_texture_depth_current,
2332
- resolution: this.#output_resolution.asArray(),
2333
- });
2334
- }
2335
-
2336
- // TODO if TAA is disabled, and internal resolution is < 100% - we need to do upscale
2337
-
2338
- if (this.feature_sharpening_enabled) {
2339
- [current_color_out] = graph_image_pass({
2340
- graph,
2341
- shader: shader_ffx_rcas,
2342
- output_resolution: this.#output_resolution.asArray(),
2343
- inputs: {
2344
- tInput: current_color_out,
2345
- uSharpness: {
2346
- value: 0.8
2347
- }
2348
- }
2349
- });
2350
- }
2351
-
2352
- let exposure = -1;
2353
-
2354
- if (this.feature_bloom_enabled || this.feature_automatic_exposure_enabled) {
2355
- const bloom = graph_postprocess_bloom({
2356
- graph,
2357
- input: current_color_out,
2358
- resolution: this.#output_resolution.asArray(),
2359
- });
2360
-
2361
- if (this.feature_bloom_enabled) {
2362
- current_color_out = bloom.composited;
2363
- }
2364
-
2365
- if (this.feature_automatic_exposure_enabled) {
2366
- // TODO force time_delta_second to a very high value on view transition
2367
-
2368
- exposure = this.#autoexposure.update({
2369
- graph,
2370
- input_color: bloom.downsampled,
2371
- time_delta_seconds,
2372
- });
2373
- }
2374
-
2375
- }
2376
-
2377
-
2378
- if (this.feature_automatic_exposure_enabled === false) {
2379
- exposure = this.#autoexposure.unadapted({ graph });
2380
- }
2381
-
2382
-
2383
- // current_color_out = graph_draw_texture_as_rgba({
2384
- // graph,
2385
- // source: gr_shadowmap_atlas,
2386
- // resolution: this.#output_resolution.asArray(),
2387
- // });
2388
-
2389
- /*
2390
- Past the upscale: the colour published here is output-sized while the G-buffer beside it is
2391
- still internal-sized, and `FrameContext.resolution` reads whichever of them the phase draws
2392
- into rather than a number anybody wrote down. With `feature_taa_enabled === false` no
2393
- upscale happened and the two agree — which a constant could not express.
2394
- */
2395
- frame.phase = FramePhase.BeforePresent;
2396
-
2397
- record_scene_color.color = current_color_out;
2398
-
2399
- this.#extensions.run(frame);
2400
-
2401
- current_color_out = record_scene_color.color;
2402
-
2403
- if (this.#hdr_supported) {
2404
- [current_color_out] = graph_image_pass({
2405
- graph,
2406
- shader: shader_tonemap_HDR,
2407
- inputs: {
2408
- input_color: current_color_out,
2409
- settings: {
2410
- peak_nits: this.#display_peak_brightness_nits,
2411
- paper_white_nits: 100, // SDR spec is 80, but 100 has been accepted as de-facto in recent years
2412
- },
2413
- exposure
2414
- },
2415
- output: [gr_texture_canvas]
2416
- });
2417
- } else {
2418
- [current_color_out] = graph_image_pass({
2419
- graph,
2420
- shader: shader_tonemap_LDR,
2421
- inputs: {
2422
- input_color: current_color_out,
2423
- exposure
2424
- },
2425
- output: [gr_texture_canvas]
2426
- });
2427
- }
2428
-
2429
- /*
2430
- The canvas is written here and cannot be read: a texture may not be a render attachment and
2431
- a sampled source in the same pass. So what this phase carries is a `PresentTarget` with one
2432
- field and no colour to be tempted by — an extension that wants the finished image as input
2433
- finds that out at `get` time and belongs at `BeforePresent`.
2434
- */
2435
- frame.phase = FramePhase.Overlay;
2436
-
2437
- frame.create(PresentTarget).canvas = current_color_out;
2438
-
2439
- this.#extensions.run(frame);
2440
-
2441
- frame.close();
2442
-
2443
- command_ctx.encodeGraph(graph);
2444
-
2445
- command_ctx.popDebugGroup();
2446
-
2447
- view_ctx.finish_frame(command_ctx);
2448
-
2449
- command_ctx.finish();
2450
-
2451
- this.#end_frame();
2452
-
2453
- return true;
2454
- }
2455
- }
1
+ import { assert } from "../../core/assert.js";
2
+ import Signal from "../../core/events/signal/Signal.js";
3
+ import Vector2 from "../../core/geom/Vector2.js";
4
+ import { clamp } from "../../core/math/clamp.js";
5
+ import { remap } from "../../core/math/remap.js";
6
+ import { MetricCollection } from "../../engine/development/performance/MetricCollection.js";
7
+ import {
8
+ MetricCollectionConsoleMonitor
9
+ } from "../../engine/development/performance/monitor/MetricCollectionConsoleMonitor.js";
10
+ import { FrameGraph } from "../../engine/graphics/render/frame_graph/FrameGraph.js";
11
+ import { TextureDescriptor } from "../descriptor/texture/TextureDescriptor.js";
12
+ import { WebGPUExtensionType } from "../descriptor/WebGPUExtensionType.js";
13
+ import { ShadeDeviceFailure } from "../device/ShadeDeviceFailure.js";
14
+ import { ShadeGPUCommandContext } from "../device/ShadeGPUCommandContext.js";
15
+ import { CameraManager } from "./camera/CameraManager.js";
16
+ import { shader_background } from "./deferred/shader_background.js";
17
+ import { shader_composite_volumetrics } from "./deferred/shader_composite_volumetrics.js";
18
+ import { shader_deferred_main, shader_deferred_main_restir } from "./deferred/shader_deferred_main.js";
19
+ import { ReSTIRDI } from "./restir/di/ReSTIRDI.js";
20
+ import { shader_tonemap_HDR } from "./deferred/shader_tonemap_HDR.js";
21
+ import { shader_tonemap_LDR } from "./deferred/shader_tonemap_LDR.js";
22
+ import { G_BUFFER_MIP_LEVELS } from "./gbuffer/G_BUFFER_MIP_LEVELS.js";
23
+ import {
24
+ graph_rasterize_meshes_transparent_oit
25
+ } from "./rasterize/native/oit/graph_rasterize_meshes_transparent_oit.js";
26
+ import {
27
+ viz_rasterization_alpha_tested_pass_descriptor
28
+ } from "./rasterize/native/viz/viz_rasterization_alpha_tested_pass_descriptor.js";
29
+ import {
30
+ viz_rasterization_opaque_pass_descriptor
31
+ } from "./rasterize/native/viz/viz_rasterization_opaque_pass_descriptor.js";
32
+ import { RasterizationJob } from "./rasterize/RasterizationJob.js";
33
+ import { graph_rasterize_scene } from "./rasterize/standard/graph_rasterize_scene.js";
34
+ import { FrameContext } from "./extension/FrameContext.js";
35
+ import { FramePhase } from "./extension/FramePhase.js";
36
+ import { GBufferTextures } from "./extension/GBufferTextures.js";
37
+ import { PresentTarget } from "./extension/PresentTarget.js";
38
+ import { RenderExtensionRegistry } from "./extension/RenderExtensionRegistry.js";
39
+ import { SceneColor } from "./extension/SceneColor.js";
40
+ import { ViewTextures } from "./extension/ViewTextures.js";
41
+ import { shader_brick4_diffuse } from "./global_illumination/brick4/gpu/draw/shader_brick4_diffuse.js";
42
+ import {
43
+ shader_brick4_indirect_lighting
44
+ } from "./global_illumination/brick4/gpu/draw/shader_brick4_indirect_lighting.js";
45
+ import { shader_brick4_specular } from "./global_illumination/brick4/gpu/draw/shader_brick4_specular.js";
46
+ import { shader_diffuse_IBL } from "./global_illumination/ibl/shader_diffuse_IBL.js";
47
+ import { shader_specular_IBL } from "./global_illumination/ibl/shader_specular_IBL.js";
48
+ import { GPULightProbeVolumeRenderer } from "./global_illumination/lpv/bake/GPULightProbeVolumeRenderer.js";
49
+ import { shader_diffuse_LPV } from "./global_illumination/lpv/shader_diffuse_LPV.js";
50
+ import { shader_resolve_indirect_lighting } from "./global_illumination/shader_resolve_indirect_lighting.js";
51
+ import { GraphicsContext } from "./GraphicsContext.js";
52
+ import { graph_build_light_clusters } from "./light/cluster/graph_build_light_clusters.js";
53
+ import { AccumulatingPathTracer } from "./path_tracer/accumulating/AccumulatingPathTracer.js";
54
+ import { graph_postprocess_bloom } from "./postprocess/bloom/graph_postprocess_bloom.js";
55
+ import { shader_ffx_rcas } from "./postprocess/cas/shader_ffx_rcas.js";
56
+ import { GPUCameraExposureManager } from "./postprocess/eye/GPUCameraExposureManager.js";
57
+ import { NSS } from "./postprocess/nss/NSS.js";
58
+ import { PostProcess } from "./postprocess/PostProcess.js";
59
+ import { shader_depth_occlusion_clip } from "./postprocess/taa/shader_depth_occlusion_clip.js";
60
+ import { TAA } from "./postprocess/taa/TAA.js";
61
+ import { MotionBlur } from "./postprocess/motion_blur/MotionBlur.js";
62
+ import { DepthOfField } from "./postprocess/dof/raymarch/DepthOfField.js";
63
+ import { DepthOfFieldU } from "./postprocess/dof/gather/DepthOfFieldU.js";
64
+ import { graph_add_per_object_velocity } from "./postprocess/velocity/graph_add_per_object_velocity.js";
65
+ import { SceneManager } from "./scene/SceneManager.js";
66
+ import { ShadeIndirectLightingMode } from "./ShadeIndirectLightingMode.js";
67
+ import { ShadeUpscalerType } from "./ShadeUpscalerType.js";
68
+ import { graph_image_pass } from "./shader/graph/graph_image_pass.js";
69
+ import { graph_import_buffer } from "./shader/graph/graph_import_buffer.js";
70
+ import { graph_import_texture } from "./shader/graph/graph_import_texture.js";
71
+ import { CascadedSceneSDF } from "./shadow/sdf/cascade/CascadedSceneSDF.js";
72
+ import { STATIC_GRAPHICS_ENGINE_ASSETS } from "./STATIC_GRAPHICS_ENGINE_ASSETS.js";
73
+ import { shader_debug_velocity } from "./postprocess/velocity/shader_debug_velocity.js";
74
+ import { GPUTextureContext } from "./texture/GPUTextureContext.js";
75
+ import { RenderTarget } from "./texture/RenderTarget.js";
76
+ import { View } from "./view/View.js";
77
+ import { ViewManager } from "./view/ViewManager.js";
78
+
79
+ const METRICS = new MetricCollection();
80
+ METRICS.create({ name: 'frame_time' });
81
+
82
+ /**
83
+ * Depth and G-buffer textures carry {@link G_BUFFER_MIP_LEVELS} mip levels.
84
+ * WebGPU's createTexture validation requires mipLevelCount <= floor(log2(max(w,h))) + 1,
85
+ * so the internal resolution must never drop below 2^(G_BUFFER_MIP_LEVELS - 1)
86
+ * or texture allocation will fail.
87
+ * @type {number}
88
+ */
89
+ const INTERNAL_RESOLUTION_MIN = 1 << (G_BUFFER_MIP_LEVELS - 1);
90
+
91
+ MetricCollectionConsoleMonitor.from(METRICS);
92
+
93
+ export class Renderer {
94
+ /**
95
+ * Indexes current drawn frame
96
+ * @type {number}
97
+ */
98
+ #frame_count = 0;
99
+
100
+ get frame_count() {
101
+ return this.#frame_count;
102
+ }
103
+
104
+ /**
105
+ * @type {GPUCanvasContext}
106
+ */
107
+ context;
108
+
109
+ /**
110
+ * The HTMLCanvasElement (or OffscreenCanvas) bound to this renderer.
111
+ * If `initialize()` was called without a `context`, this is the canvas
112
+ * the renderer created — append it to the DOM to display the output.
113
+ *
114
+ * @returns {HTMLCanvasElement|OffscreenCanvas|undefined}
115
+ */
116
+ get canvas() {
117
+ return this.context?.canvas;
118
+ }
119
+
120
+ /**
121
+ * @type {GPUDevice}
122
+ */
123
+ device;
124
+
125
+ /**
126
+ * @type {GPUCameraExposureManager}
127
+ */
128
+ #autoexposure;
129
+
130
+ /**
131
+ * @type {SceneManager}
132
+ */
133
+ #scenes;
134
+
135
+ /**
136
+ * Only use for read-access
137
+ * @returns {SceneManager}
138
+ */
139
+ get scenes() {
140
+ return this.#scenes;
141
+ }
142
+
143
+ /**
144
+ * @type {CameraManager}
145
+ */
146
+ #cameras
147
+
148
+ /**
149
+ * @type {ViewManager}
150
+ */
151
+ #views
152
+
153
+
154
+ set feature_shadows_enabled(v) {
155
+ assert.isBoolean(v, 'v');
156
+ this.#feature_shadows_enabled = v;
157
+ }
158
+
159
+ get feature_shadows_enabled() {
160
+ return this.#feature_shadows_enabled;
161
+ }
162
+
163
+ /**
164
+ * Controls which indirect lighting technique is used for
165
+ * both deferred resolve and forward transparency passes.
166
+ * @type {ShadeIndirectLightingMode}
167
+ */
168
+ indirect_lighting_mode = ShadeIndirectLightingMode.IBL;
169
+
170
+ feature_ssr_enabled = false;
171
+ feature_ssao_enabled = true;
172
+ /**
173
+ * ReSTIR DI — stochastic screen-bounded direct shadowing of local
174
+ * (point+spot) lights. When on, the deferred pass skips its froxel cluster
175
+ * loop and consumes the ReSTIR-resolved radiance instead.
176
+ */
177
+ feature_restir_di_enabled = false;
178
+ /**
179
+ * Generate point-light shadows with the tetrahedral (4-face) path instead of the default
180
+ * cube (6-face) path. Both produce the same octahedral atlas encoding, so this is safe to
181
+ * flip at runtime and is meant for A/B comparison of the two generators. Pushed to the
182
+ * scene's shadow context each frame; see
183
+ * {@link GPUSceneShadowmapContext.use_tetrahedron_point_shadows}.
184
+ * @type {boolean}
185
+ */
186
+ feature_tetrahedron_point_shadows = false;
187
+ feature_bloom_enabled = true;
188
+ feature_automatic_exposure_enabled = true;
189
+
190
+ /**
191
+ * Virtual texture streaming (opt-in per material via
192
+ * {@link StandardShadeMaterial#vt_stack}). This flag is the system-wide
193
+ * kill switch: when off, the feedback pass and residency maintenance
194
+ * stop running — already-resident pages keep rendering, nothing new
195
+ * streams in. Costs nothing while no stack is registered.
196
+ * @type {boolean}
197
+ */
198
+ feature_virtual_textures = true;
199
+
200
+ #feature_taa_enabled = true;
201
+
202
+ get feature_taa_enabled() {
203
+ return this.#feature_taa_enabled;
204
+ }
205
+
206
+ /**
207
+ * While TAA is off nothing writes the history textures, but the frame counter that
208
+ * indexes them keeps advancing — re-enabling would blend with arbitrarily stale
209
+ * history of arbitrary parity. Reset temporal history on the transition.
210
+ * @param {boolean} v
211
+ */
212
+ set feature_taa_enabled(v) {
213
+ if (this.#feature_taa_enabled === v) {
214
+ return;
215
+ }
216
+
217
+ this.#feature_taa_enabled = v;
218
+ this.indicate_view_change();
219
+ }
220
+
221
+ /**
222
+ * Jimenez 2014 reconstruction-filter motion blur, runs between
223
+ * TAA and sharpening on the HDR colour. Reads the velocity buffer
224
+ * the same way TAA does — per-pixel pixel-space deltas — and
225
+ * scatter-gathers along the dilated tile velocity. Off by default
226
+ * because the reconstruction pass is a few-ms-budget item and not
227
+ * every scene needs it.
228
+ *
229
+ * @type {boolean}
230
+ */
231
+ feature_motion_blur_enabled = false;
232
+
233
+ /**
234
+ * Motion-blur subsystem (Jimenez 2014 reconstruction filter). A
235
+ * self-contained manager — same pattern as {@link #autoexposure} /
236
+ * {@link #depth_of_field} — that owns the motion-blur parameters
237
+ * (currently just `strength`). Constructed in init.
238
+ * @type {MotionBlur}
239
+ */
240
+ #motion_blur;
241
+
242
+ /**
243
+ * The motion-blur subsystem. Configure it via `renderer.motion_blur.*`
244
+ * (currently `strength`); toggle the effect with
245
+ * {@link feature_motion_blur_enabled}.
246
+ * @returns {MotionBlur}
247
+ */
248
+ get motion_blur() { return this.#motion_blur; }
249
+
250
+ /**
251
+ * Debug-only override: when true, the per-object velocity texture
252
+ * is drawn straight into the post-TAA color path so the screen
253
+ * shows the velocity buffer instead of the rendered image. Wired
254
+ * just before TAA — TAA then operates on the velocity texture,
255
+ * which is nonsense but cheap to set up. Default false.
256
+ *
257
+ * @type {boolean}
258
+ */
259
+ feature_velocity_debug_view = false;
260
+
261
+ /**
262
+ * When true, {@link path_tracer} replaces the rasterized+shaded color for the frame. The
263
+ * traced result still goes through TAA and tone mapping like any other color.
264
+ *
265
+ * The path tracer accumulates across frames itself, one tile per call, and resets whenever the
266
+ * camera moves. TAA on top of that is two temporal filters fighting over the same image — turn
267
+ * `feature_taa_enabled` off while this is on.
268
+ *
269
+ * This is the traversal-heaviest consumer of the TLAS — it binds `scene_ctx.tlas.buffer`
270
+ * directly — which is what made it the check that the GPU TLAS build traces correctly, and what
271
+ * makes it the first place a regression in that tree would show. Default false: it is a
272
+ * prototype-grade path, not a shipping render mode.
273
+ *
274
+ * @type {boolean}
275
+ */
276
+ feature_path_tracing_enabled = false;
277
+
278
+ /**
279
+ * Screen-space ray-marched depth of field — the Tiny Glade model
280
+ * (Stachowiak, GPC 2024; see {@link DepthOfField}): aperture sub-rays
281
+ * marched through the half-res depth/CoC buffer with nearest-hit
282
+ * occlusion, plus tilt-shift. Runs on the resolved HDR colour after TAA
283
+ * (which resolves the march's sample noise) and before motion blur. Off
284
+ * by default — a half-res march is a few-ms item, only worth it for a
285
+ * deliberate shallow-focus / diorama look. Configure via `renderer.dof`.
286
+ *
287
+ * @type {boolean}
288
+ */
289
+ feature_dof_enabled = false;
290
+
291
+ /**
292
+ * Depth-of-field subsystem (screen-space ray-marched). A self-contained
293
+ * manager — same pattern as {@link #autoexposure} — that owns all DoF
294
+ * parameters, focus state, and GPU buffers. Constructed in init.
295
+ * @type {DepthOfField}
296
+ */
297
+ #depth_of_field;
298
+
299
+ /**
300
+ * The depth-of-field subsystem. Configure it via `renderer.dof.*`
301
+ * (`f_number`, `quality`, the `focus*` helpers, `applyPreset`, …);
302
+ * toggle the effect with {@link feature_dof_enabled}.
303
+ * @returns {DepthOfField}
304
+ */
305
+ get dof() { return this.#depth_of_field; }
306
+
307
+ /**
308
+ * Which depth-of-field implementation runs when {@link feature_dof_enabled}
309
+ * is on. `"raymarch"` (default) is the screen-space ray-march
310
+ * ({@link DepthOfField}); `"unreal"` is the gather-based Diaphragm /
311
+ * Cinematic DoF ({@link DepthOfFieldU}) — half-res near/far gather with a
312
+ * foreground coverage bleed. Mutually exclusive by construction (only one
313
+ * runs at the single DoF call site).
314
+ * @type {"raymarch"|"unreal"}
315
+ */
316
+ dof_algorithm = "raymarch";
317
+
318
+ /**
319
+ * Unreal-style (Diaphragm / Cinematic) gather depth of field — a drop-in
320
+ * alternative to {@link #depth_of_field}, selected via {@link dof_algorithm}.
321
+ * Constructed in init.
322
+ * @type {DepthOfFieldU}
323
+ */
324
+ #depth_of_field_unreal;
325
+
326
+ /**
327
+ * The Unreal-style depth-of-field subsystem. Configure it via
328
+ * `renderer.dof_unreal.*` and select it with `renderer.dof_algorithm = "unreal"`.
329
+ * @returns {DepthOfFieldU}
330
+ */
331
+ get dof_unreal() { return this.#depth_of_field_unreal; }
332
+
333
+ /**
334
+ * When true and {@link indirect_lighting_mode} is Brick4, run a single fused
335
+ * pass that produces the resolved indirect lighting directly (combining the
336
+ * brick4 diffuse, brick4 specular and indirect resolve passes). Trades a
337
+ * bit of cache pressure inside the pass for substantially less memory
338
+ * bandwidth: no intermediate rgba16float diffuse/specular targets, the
339
+ * g-buffer is read once, and a single brick4 probe pair is shared between
340
+ * the diffuse and specular evaluations.
341
+ *
342
+ * SSR is mutually exclusive with the fused path (it needs the standalone
343
+ * specular target as input), so when SSR is enabled the split path is used.
344
+ * @type {boolean}
345
+ */
346
+ fused_indirect = true;
347
+
348
+ /**
349
+ * RCAS
350
+ * @type {boolean}
351
+ */
352
+ feature_sharpening_enabled = true;
353
+
354
+ #feature_shadows_enabled = true;
355
+ #feature_enabled_transparencies = true;
356
+
357
+ /**
358
+ *
359
+ * @return {ViewManager}
360
+ */
361
+ get views() {
362
+ return this.#views;
363
+ }
364
+
365
+ /**
366
+ * @type {GraphicsContext}
367
+ */
368
+ #graphics;
369
+
370
+ /**
371
+ *
372
+ * @return {GraphicsContext}
373
+ */
374
+ get graphics() {
375
+ return this.#graphics;
376
+ }
377
+
378
+
379
+ /**
380
+ *
381
+ * @type {Signal<number>}
382
+ */
383
+ /**
384
+ * Extensions an outside caller has put into the frame, by phase.
385
+ *
386
+ * @type {RenderExtensionRegistry}
387
+ */
388
+ #extensions = new RenderExtensionRegistry();
389
+
390
+ /**
391
+ * Raised when there is no usable device — the browser has no WebGPU, the hardware is below the
392
+ * floor, or a working device was lost while running. Carries a {@link ShadeDeviceFailure}.
393
+ *
394
+ * The floor is deliberate and there are no fallbacks (D6/I-8), so telling the player plainly is
395
+ * the whole of what the engine owes here. Loss is not recovered from (R6): everything built on
396
+ * the device went with it, and the honest response is a message and a reload.
397
+ *
398
+ * @type {Signal<ShadeDeviceFailure>}
399
+ */
400
+ onDeviceFailure = new Signal();
401
+
402
+ onFrameFinished = new Signal();
403
+
404
+ /**
405
+ * First argument is frame index, second is table of debug data
406
+ * @type {Signal<number, Object[]>}
407
+ */
408
+ onFrameDebug = new Signal();
409
+
410
+
411
+ /**
412
+ * Fraction of the output resolution.
413
+ * Controls {@link #internal_resolution}.
414
+ * If this is set to 0.5 for example - internal resolution will be 50% of the output resolution.
415
+ * @type {number}
416
+ */
417
+ #internal_resolution_scale = 1;
418
+
419
+ /**
420
+ *
421
+ * @type {boolean}
422
+ */
423
+ #internal_resolution_needs_update = true;
424
+
425
+ /**
426
+ *
427
+ * @param {number} v
428
+ */
429
+ set internal_resolution_scale(v) {
430
+ assert.isNumber(v, 'v');
431
+ assert.notNaN(v, 'v');
432
+ assert.greaterThan(v, 0);
433
+ assert.isFinite(v, 'v');
434
+
435
+ if (this.#internal_resolution_scale === v) {
436
+ // no change
437
+ return;
438
+ }
439
+
440
+ this.#internal_resolution_scale = v;
441
+
442
+ // schedule resolution recalculation
443
+ this.#internal_resolution_needs_update = true;
444
+ }
445
+
446
+ /**
447
+ *
448
+ * @returns {number}
449
+ */
450
+ get internal_resolution_scale() {
451
+ return this.#internal_resolution_scale;
452
+ }
453
+
454
+ /**
455
+ * This is the resolution that the rendering happens at before upscaling.
456
+ * Derived from input resolution via {@link internal_resolution_scale}
457
+ * @type {Vector2}
458
+ */
459
+ #internal_resolution = new Vector2(1, 1);
460
+
461
+
462
+ /**
463
+ * Output viewport size
464
+ * @type {Vector2}
465
+ */
466
+ #size = new Vector2(1, 1);
467
+
468
+ /**
469
+ * Output resolution.
470
+ * Equal to {@link size} * {@link pixel_ratio}
471
+ * Don't write directly
472
+ * @type {Vector2}
473
+ * @see {#size}
474
+ * @see {#pixel_ratio}
475
+ */
476
+ #output_resolution = new Vector2(1, 1);
477
+
478
+ /**
479
+ *
480
+ * @returns {Vector2}
481
+ */
482
+ get output_resolution() {
483
+ return this.#output_resolution.clone();
484
+ }
485
+
486
+ /**
487
+ * width/height
488
+ * Useful for camera projection setup
489
+ * @return {number}
490
+ */
491
+ get aspect_ratio() {
492
+ return this.#internal_resolution.x / this.#internal_resolution.y;
493
+ }
494
+
495
+ /**
496
+ * TODO rework ping-ping buffer usage to release old buffer when no longer needed. Can reach much better resource utilization that way. Same for all other TAA-like techniques
497
+ * @type {GPUTextureContext[]}
498
+ */
499
+ #textures_depth = new Array(2);
500
+
501
+ /**
502
+ *
503
+ * @returns {GPUTextureContext}
504
+ */
505
+ get texture_depth_current() {
506
+ const tx = this.#textures_depth;
507
+
508
+ return tx[this.#frame_count % tx.length];
509
+ }
510
+
511
+ /**
512
+ *
513
+ * @returns {GPUTextureContext}
514
+ */
515
+ get texture_depth_previous() {
516
+ const tx = this.#textures_depth;
517
+
518
+ return tx[(this.#frame_count - 1 + tx.length) % tx.length];
519
+ }
520
+
521
+ /**
522
+ *
523
+ * @type {GPUTextureContext[]}
524
+ */
525
+ #textures_taa_history = new Array(2);
526
+
527
+ /**
528
+ *
529
+ * @returns {GPUTextureContext}
530
+ */
531
+ #get_texture_taa_history(x) {
532
+ const tx = this.#textures_taa_history;
533
+
534
+ return tx[(this.#frame_count - x + tx.length) % tx.length];
535
+ }
536
+
537
+
538
+ /**
539
+ * @type {GPUTextureFormat}
540
+ */
541
+ #presentation_format = "rgba8unorm";
542
+
543
+ /**
544
+ * How many pixels to draw per screen pixel.
545
+ * Typically, this is used to match physical screen resolution to virtual resolution.
546
+ * On the high pixel density displays, CSS pixels usually take up more than one screen pixel.
547
+ * @type {number}
548
+ */
549
+ #pixel_ratio = window.devicePixelRatio;
550
+
551
+ /**
552
+ *
553
+ * @return {number}
554
+ */
555
+ get pixel_ratio() {
556
+ return this.#pixel_ratio;
557
+ }
558
+
559
+ /**
560
+ *
561
+ * @param {number} ratio
562
+ */
563
+ set pixel_ratio(ratio) {
564
+ assert.isNumber(ratio, 'ratio');
565
+ assert.notNaN(ratio, 'ratio');
566
+ assert.greaterThan(ratio, 0);
567
+ assert.isFinite(ratio, 'ratio');
568
+
569
+ if (ratio === this.#pixel_ratio) {
570
+ // no change
571
+ return;
572
+ }
573
+
574
+ this.#pixel_ratio = ratio;
575
+
576
+ this.#update_resolution();
577
+ }
578
+
579
+ /**
580
+ * How many future frames should be debugged
581
+ * @type {number}
582
+ */
583
+ #debug_frame_count = 0;
584
+
585
+ /**
586
+ * The profile recording to feed, or null.
587
+ *
588
+ * **Assign one; the renderer never makes one.** The profiler is a leaf nothing in the engine
589
+ * imports, so an application that does not mention it does not carry it — see
590
+ * {@link GPUProfileSession}. This field and the null checks around it are the whole
591
+ * integration.
592
+ *
593
+ * ```js
594
+ * const session = new GPUProfileSession({ level: GPUProfileLevel.TIMING });
595
+ *
596
+ * renderer.profile_session = session;
597
+ * session.start();
598
+ * // ... frames ...
599
+ * const bytes = session.stop();
600
+ * renderer.profile_session = null;
601
+ * ```
602
+ *
603
+ * @type {GPUProfileSession|null}
604
+ */
605
+ profile_session = null;
606
+
607
+ /**
608
+ * Increase the number of future debug frames by X
609
+ */
610
+ /**
611
+ * Record work into the frame at a named point in it.
612
+ *
613
+ * The extension is called once per frame, when its phase comes round, and is handed the
614
+ * {@link FrameContext} — the frame's graph and the typed records holding the handles that exist
615
+ * at that moment. It records with the `graph_*` helpers exactly as the built-in passes do, and
616
+ * everything it produces reaches the rest of the frame by publishing a handle into a record.
617
+ *
618
+ * Handles come from the records rather than from the renderer on purpose: the graph is what
619
+ * knows which resources a pass touched, and work recorded around it cannot be ordered or aliased
620
+ * correctly against the rest of the frame.
621
+ *
622
+ * @param {RenderExtension} extension
623
+ * @returns {RenderExtension} `extension`, for handing back to {@link remove_extension}
624
+ */
625
+ add_extension(extension) {
626
+ return this.#extensions.add(extension);
627
+ }
628
+
629
+ /**
630
+ * Take an extension back out of the frame.
631
+ *
632
+ * @param {RenderExtension} extension
633
+ * @returns {boolean} whether it was registered
634
+ */
635
+ remove_extension(extension) {
636
+ return this.#extensions.remove(extension);
637
+ }
638
+
639
+ /**
640
+ * @param {FramePhase} phase
641
+ * @returns {number} how many extensions are registered on `phase`
642
+ */
643
+ extension_count(phase) {
644
+ return this.#extensions.count(phase);
645
+ }
646
+
647
+
648
+ add_debug_frame(count = 1) {
649
+ assert.isNonNegativeInteger(count, 'count');
650
+
651
+ this.#debug_frame_count += count;
652
+ }
653
+
654
+ /**
655
+ *
656
+ * @type {Map<Scene, GPULightProbeVolumeRenderer>}
657
+ */
658
+ #probe_bakers = new Map();
659
+
660
+
661
+ /**
662
+ *
663
+ * @type {Map<Scene, CascadedSceneSDF>}
664
+ */
665
+ #scene_sdfs = new Map();
666
+
667
+ /**
668
+ *
669
+ * @param {Scene} scene
670
+ * @return {CascadedSceneSDF}
671
+ */
672
+ obtains_scene_sdf(scene) {
673
+ let sdf = this.#scene_sdfs.get(scene);
674
+
675
+ if (sdf === undefined) {
676
+ sdf = new CascadedSceneSDF(this.device);
677
+ this.#scene_sdfs.set(scene, sdf);
678
+ }
679
+
680
+ return sdf;
681
+ }
682
+
683
+ /**
684
+ * Used to instruct renderer that either the scene or camera has changed significantly
685
+ * Primarily instructs resetting history on temporal accumulation
686
+ */
687
+ indicate_view_change() {
688
+ this.#postprocess.reset_history();
689
+
690
+ if (this.#path_tracer !== undefined) {
691
+ this.#path_tracer.clear_history = true;
692
+ }
693
+
694
+ this.#taa.reset_history = true;
695
+
696
+ if (this.#nss !== undefined) {
697
+ this.#nss.reset_history = true;
698
+ }
699
+ }
700
+
701
+ /**
702
+ * For internal and debug use only
703
+ * @param {Scene} scene
704
+ * @returns {GPULightProbeVolumeRenderer}
705
+ */
706
+ getProbeRendererForScene(scene) {
707
+
708
+ const scene_ctx = this.#scenes.obtain(scene);
709
+
710
+ let baker = this.#probe_bakers.get(scene);
711
+
712
+ if (baker === undefined) {
713
+ baker = new GPULightProbeVolumeRenderer(this.#graphics, scene_ctx);
714
+ this.#probe_bakers.set(scene, baker);
715
+ }
716
+
717
+ return baker;
718
+ }
719
+
720
+
721
+ /**
722
+ *
723
+ * @param {Scene} scene
724
+ */
725
+ update_lpv(scene) {
726
+
727
+ //
728
+ // let baker = this.getProbeRendererForScene(scene);
729
+ //
730
+ // baker.autoSetRaysPerProbe(1);
731
+ // baker.bake();
732
+
733
+ const scene_ctx = this.#scenes.obtain(scene);
734
+
735
+ const graph = new FrameGraph("LPV");
736
+
737
+ const cmd = ShadeGPUCommandContext.create(this.graphics, 'LPV');
738
+
739
+ scene_ctx.light_probe_volume.atlas.graph_update({
740
+ graph,
741
+ scene: scene_ctx,
742
+ graphics: this.graphics,
743
+ update_ray_count: 100_000
744
+ });
745
+
746
+ cmd.encodeGraph(graph);
747
+ cmd.finish();
748
+ }
749
+
750
+ /**
751
+ * Compute deferred indirect specular and diffuse contributions
752
+ * based on the active indirect lighting mode.
753
+ *
754
+ * @param {object} params
755
+ * @param {FrameGraph} params.graph
756
+ * @param {GPUViewContext} params.view_ctx
757
+ * @param {GPUSceneContext} params.scene_ctx
758
+ * @param {number} params.bent_normals - graph resource ID
759
+ * @param {number} params.current_color_out - graph resource ID (scene color, needed for SSR)
760
+ * @param {number} params.gr_texture_depth_current
761
+ * @param {number} params.gr_texture_hzb
762
+ * @param {number} params.gr_texture_material_albedo_ao
763
+ * @param {number} params.gr_texture_pbr
764
+ * @param {number} params.gr_texture_normal
765
+ * @param {number} params.gr_texture_environment
766
+ * @param {number} params.gr_velocity
767
+ * @param {number} params.gr_occlusion_clip
768
+ * @param {number} params.gr_buffer_camera_current
769
+ * @param {number} params.gr_buffer_camera_previous
770
+ * @param {Vector2} params.resolution
771
+ * @returns {{indirect_specular: number, indirect_diffuse: number}}
772
+ */
773
+ #graph_indirect_lighting({
774
+ graph,
775
+ view_ctx,
776
+ scene_ctx,
777
+ bent_normals,
778
+ current_color_out,
779
+ //
780
+ gr_texture_depth_current,
781
+ gr_texture_hzb,
782
+ gr_texture_material_albedo_ao,
783
+ gr_texture_pbr,
784
+ gr_texture_normal,
785
+ gr_texture_environment,
786
+ gr_velocity,
787
+ gr_occlusion_clip,
788
+ gr_buffer_camera_current,
789
+ gr_buffer_camera_previous,
790
+ resolution,
791
+ }) {
792
+
793
+ const mode = this.indirect_lighting_mode;
794
+
795
+ let indirect_specular = -1;
796
+ let indirect_diffuse = -1;
797
+
798
+ const lpv = scene_ctx.light_probe_volume;
799
+
800
+ //
801
+ // ── SPECULAR ────────────────────────────────────────────
802
+ //
803
+
804
+ // SSR replaces the specular IBL fallback when enabled.
805
+ // NOTE: SSR is currently not supported in Brick4 mode — this is a known
806
+ // limitation and will be addressed in a future update.
807
+ if (this.feature_ssr_enabled && mode !== ShadeIndirectLightingMode.Brick4) {
808
+
809
+ const ssr = this.#postprocess.ssr.graph_pass({
810
+ graph,
811
+ input_depth: gr_texture_depth_current,
812
+ input_hzb: gr_texture_hzb,
813
+ input_scene_color: current_color_out,
814
+ input_albedo: gr_texture_material_albedo_ao,
815
+ input_pbr: gr_texture_pbr,
816
+ input_normal: gr_texture_normal,
817
+ input_velocity: gr_velocity,
818
+ input_occlusion: gr_occlusion_clip,
819
+ input_environment: gr_texture_environment,
820
+ camera_current: gr_buffer_camera_current,
821
+ camera_previous: gr_buffer_camera_previous,
822
+ lpv: mode === ShadeIndirectLightingMode.LPV ? lpv : undefined,
823
+ resolution,
824
+ });
825
+
826
+ indirect_specular = ssr.denoised;
827
+
828
+ } else if (mode === ShadeIndirectLightingMode.Brick4) {
829
+
830
+ // Brick4 specular from volumetric lightmap
831
+ [indirect_specular] = graph_image_pass({
832
+ graph,
833
+ shader: shader_brick4_specular,
834
+ input_depth: gr_texture_depth_current,
835
+ inputs: {
836
+ tDepth: gr_texture_depth_current,
837
+ tNormals: gr_texture_normal,
838
+ tPBR: gr_texture_pbr,
839
+ tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
840
+ camera: gr_buffer_camera_current,
841
+ view: view_ctx.uniform_buffer.buffer,
842
+ brick4: scene_ctx.volumetric_light_map.buffer,
843
+ },
844
+ output_resolution: view_ctx.resolution,
845
+ });
846
+
847
+ } else {
848
+ // IBL / LPV fallback — environment map specular
849
+ [indirect_specular] = graph_image_pass({
850
+ graph,
851
+ shader: shader_specular_IBL,
852
+ input_depth: gr_texture_depth_current,
853
+ inputs: {
854
+ tBentNormals: bent_normals,
855
+ tNormals: gr_texture_normal,
856
+ tDepth: gr_texture_depth_current,
857
+ tPBR: gr_texture_pbr,
858
+ tEnvironment: gr_texture_environment,
859
+ camera: gr_buffer_camera_current,
860
+ },
861
+ output_resolution: view_ctx.resolution,
862
+ });
863
+ }
864
+
865
+ //
866
+ // ── DIFFUSE ─────────────────────────────────────────────
867
+ //
868
+
869
+ switch (mode) {
870
+
871
+ case ShadeIndirectLightingMode.IBL:
872
+ // Environment map irradiance
873
+ [indirect_diffuse] = graph_image_pass({
874
+ graph,
875
+ shader: shader_diffuse_IBL,
876
+ input_depth: gr_texture_depth_current,
877
+ inputs: {
878
+ tBentNormals: bent_normals,
879
+ tGBufferAlbedo: gr_texture_material_albedo_ao,
880
+ tEnvironment: gr_texture_environment,
881
+ },
882
+ output_resolution: view_ctx.resolution,
883
+ });
884
+ break;
885
+
886
+ case ShadeIndirectLightingMode.Brick4:
887
+ // Brick4 diffuse from volumetric lightmap
888
+ [indirect_diffuse] = graph_image_pass({
889
+ graph,
890
+ shader: shader_brick4_diffuse,
891
+ input_depth: gr_texture_depth_current,
892
+ inputs: {
893
+ tDepth: gr_texture_depth_current,
894
+ tBentNormals: bent_normals,
895
+ tAlbedoAO: gr_texture_material_albedo_ao,
896
+ tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
897
+ camera: gr_buffer_camera_current,
898
+ view: view_ctx.uniform_buffer.buffer,
899
+ brick4: scene_ctx.volumetric_light_map.buffer,
900
+ },
901
+ output_resolution: view_ctx.resolution,
902
+ });
903
+ break;
904
+
905
+ case ShadeIndirectLightingMode.LPV:
906
+ // Light Probe Volume diffuse
907
+ [indirect_diffuse] = graph_image_pass({
908
+ graph,
909
+ shader: shader_diffuse_LPV,
910
+ input_depth: gr_texture_depth_current,
911
+ inputs: {
912
+ camera: gr_buffer_camera_current,
913
+ tDepth: gr_texture_depth_current,
914
+ tBentNormals: bent_normals,
915
+ tGBufferAlbedo: gr_texture_material_albedo_ao,
916
+ //
917
+ lpv_atlas_radiance: lpv.atlas.texture_radiance,
918
+ lpv_atlas_depth: lpv.atlas.texture_depth,
919
+ //
920
+ lpv_lookup: lpv.buffer_mesh_bvh,
921
+ lpv_metadata: lpv.buffer_metadata,
922
+ lpv_mesh: lpv.buffer_mesh,
923
+ lpv_probes: lpv.buffer_probes,
924
+ },
925
+ output_resolution: view_ctx.resolution,
926
+ });
927
+ break;
928
+ }
929
+
930
+ return { indirect_specular, indirect_diffuse };
931
+ }
932
+
933
+ /**
934
+ * @type {PostProcess}
935
+ */
936
+ #postprocess
937
+
938
+ /**
939
+ * @type {ReSTIRDI}
940
+ */
941
+ #restir_di
942
+
943
+ /**
944
+ * ReSTIR DI subsystem — exposed for runtime tuning (e.g. GUI toggles like
945
+ * `use_screen_space_shadow`, `m_initial`).
946
+ * @returns {ReSTIRDI}
947
+ */
948
+ get restir_di() {
949
+ return this.#restir_di;
950
+ }
951
+
952
+ /**
953
+ *
954
+ * @type {TAA}
955
+ */
956
+ #taa = new TAA();
957
+
958
+ /**
959
+ * Lazily constructed in {@link initialize} once a graphics context is
960
+ * available — NSS owns persistent GPU textures, so it can't be a
961
+ * field initializer like {@link #taa}.
962
+ * @type {NSS | undefined}
963
+ */
964
+ #nss
965
+
966
+ /**
967
+ * Selects which temporal-AA / upscaler runs at the temporal-resolve
968
+ * stage. Defaults to {@link ShadeUpscalerType.TAA}; assign
969
+ * {@link ShadeUpscalerType.NSS} to use the bundled neural upscaler
970
+ * (no extra setup the NSS instance ships with placeholder weights).
971
+ * @type {ShadeUpscalerType}
972
+ */
973
+ upscale_type = ShadeUpscalerType.TAA;
974
+
975
+ /**
976
+ * Public read access to the NSS instance. Lazily constructed on
977
+ * first access, which is also when the bundled weights blob is
978
+ * decoded — so the cost of selecting NSS is amortized to a single
979
+ * frame.
980
+ * @returns {NSS}
981
+ */
982
+ get nss() {
983
+ if (this.#nss === undefined) {
984
+ assert.defined(this.#graphics, 'graphics context not yet initialized — call Renderer.initialize() first');
985
+ this.#nss = new NSS(this.#graphics);
986
+
987
+ // First-time setup: align the jitter sequence size with the
988
+ // current upscale ratio. Subsequent resolution changes are
989
+ // picked up by #update_internal_resolution.
990
+ const out_w = this.#output_resolution.x;
991
+ const in_w = this.#internal_resolution.x;
992
+ if (out_w > 0 && in_w > 0) {
993
+ const upscale_ratio = out_w / in_w;
994
+ this.#nss.jitter_sequence_size = NSS.recommended_jitter_sequence_size(upscale_ratio);
995
+ }
996
+ }
997
+ return this.#nss;
998
+ }
999
+
1000
+ #device_lost = false;
1001
+
1002
+ /**
1003
+ * Handles event when GPUDevice is lost
1004
+ * @param {GPUDeviceLostInfo} info
1005
+ */
1006
+ #handle_device_lost(info) {
1007
+ this.#device_lost = true;
1008
+
1009
+ if (info.reason === "destroyed") {
1010
+ // most likely renderer was destroyed, this is expected behavior
1011
+ // TODO check if renderer owns the device, if not, log a warning
1012
+ return;
1013
+ }
1014
+
1015
+ console.error('GPUDevice lost', info);
1016
+
1017
+ // Everything the renderer built lives on the device that just went away, so there is
1018
+ // nothing to carry over and no attempt to (R6). Whoever is hosting this gets told, and a
1019
+ // reload is the way back.
1020
+ this.onDeviceFailure.send1(ShadeDeviceFailure.device_lost(info.message ?? info.reason ?? ""));
1021
+ }
1022
+
1023
+ #hdr_supported = false;
1024
+
1025
+ /**
1026
+ * What's the peak brightness of the display?
1027
+ * Only valid for HDR.
1028
+ *
1029
+ * @type {number}
1030
+ */
1031
+ #display_peak_brightness_nits = 1000;
1032
+
1033
+ #update_presentation_format() {
1034
+
1035
+ const format8bit = navigator.gpu.getPreferredCanvasFormat();
1036
+
1037
+ const format = this.#hdr_supported ? "rgba16float" : format8bit;
1038
+
1039
+ if (format === this.#presentation_format) {
1040
+ return;
1041
+ }
1042
+
1043
+ this.#presentation_format = format;
1044
+
1045
+ this.#context_needs_update = true;
1046
+ }
1047
+
1048
+ /**
1049
+ * Needs to be a lambda, because we use it as a listener.
1050
+ */
1051
+ #update_hdr_support = () => {
1052
+ const isHDR = window.matchMedia("(dynamic-range: high)").matches;
1053
+
1054
+ const previous_value = this.#hdr_supported;
1055
+
1056
+
1057
+ this.#hdr_supported = isHDR;
1058
+
1059
+ if (isHDR) {
1060
+ // Currently, there is no way to interrogate this from the browser, so going with an assumption
1061
+ this.#display_peak_brightness_nits = 1000;
1062
+ } else {
1063
+ // Assume SDR nits
1064
+ this.#display_peak_brightness_nits = 80;
1065
+ }
1066
+
1067
+ if (isHDR === previous_value) {
1068
+ // no change
1069
+ return;
1070
+ }
1071
+
1072
+ console.log(`HDR support: ${isHDR ? 'enabled' : 'disabled'}`);
1073
+
1074
+ this.#update_presentation_format();
1075
+
1076
+ this.#context_needs_update = true;
1077
+ }
1078
+
1079
+ /**
1080
+ *
1081
+ * @param {GPUCanvasContext} [context] Optional, if not provided - one will be created.
1082
+ * @param {GPUDevice} [device] Optional, if not provided - one will be created.
1083
+ * @param {number} [pixelRatio] Optiona, if not provided - will be taken from browser context
1084
+ */
1085
+ async initialize({
1086
+ context,
1087
+ device,
1088
+ pixelRatio = window.devicePixelRatio,
1089
+ } = {}) {
1090
+
1091
+ if (!('gpu' in navigator)) {
1092
+ throw new Error('navigator.gpu not available — WebGPU disabled or unsupported');
1093
+ }
1094
+
1095
+ this.#update_presentation_format();
1096
+ this.#update_hdr_support();
1097
+
1098
+ if (context === undefined) {
1099
+ const canvas = document.createElement('canvas');
1100
+ canvas.width = window.innerWidth;
1101
+ canvas.height = window.innerHeight;
1102
+ // Sensible default styling so a freshly-appended canvas covers
1103
+ // the viewport without depending on the host page's CSS reset.
1104
+ // Override `style` after `initialize()` returns if the host
1105
+ // page wants the canvas embedded somewhere else.
1106
+ canvas.style.cssText = 'position:fixed;inset:0;width:100vw;height:100vh;display:block';
1107
+
1108
+ context = canvas.getContext('webgpu');
1109
+
1110
+ if (context === null) {
1111
+ throw new Error('Failed to bind GPUCanvasContext');
1112
+ }
1113
+ }
1114
+
1115
+ if (device === undefined) {
1116
+ // no device present, let's create one
1117
+
1118
+ const gpu = navigator.gpu;
1119
+
1120
+ if (gpu === undefined) {
1121
+ throw ShadeDeviceFailure.webgpu_unavailable();
1122
+ }
1123
+
1124
+ const adapter = await gpu.requestAdapter({
1125
+ powerPreference: "high-performance",
1126
+ });
1127
+
1128
+ if (adapter === null) {
1129
+ throw ShadeDeviceFailure.adapter_unavailable();
1130
+ }
1131
+
1132
+ if (adapter.isFallbackAdapter) {
1133
+ // see https://www.w3.org/TR/webgpu/#dom-gpurequestadapteroptions-forcefallbackadapter
1134
+ // fallback device is typically a software implementation and is going to be slow as sin
1135
+ console.warn('GPU provided a fallback adapter (typically because no other appropriate adapter was available). Fallback adapter is typically a software implementation and will be slow.');
1136
+ }
1137
+
1138
+ // log details of the adapter
1139
+ console.log(`GPU adapter is ${adapter.info.vendor}/${adapter.info.architecture}, device='${adapter.info.device}', description='${adapter.info.description}'`);
1140
+
1141
+ // console.warn(`Buffer size limit: ${number_format_by_thousands(adapter.limits.maxBufferSize)}`);
1142
+ //
1143
+ // console.warn('adapter limits:', adapter.limits);
1144
+
1145
+ const maxStorageBuffersPerShaderStage = adapter.limits.maxStorageBuffersPerShaderStage;
1146
+
1147
+ if (maxStorageBuffersPerShaderStage < 10) {
1148
+ throw ShadeDeviceFailure.below_floor(
1149
+ `requires at least 10 storage buffers per shader stage, adapter offers ${maxStorageBuffersPerShaderStage}`
1150
+ );
1151
+ }
1152
+
1153
+ const requiredFeatures = [
1154
+ // required for indirect draw that's used extensively in Shade
1155
+ WebGPUExtensionType.IndirectFirstInstance,
1156
+ // required for OIT (might be unavailable)
1157
+ WebGPUExtensionType.Float32Blendable,
1158
+ ];
1159
+
1160
+ // add features if they are available
1161
+ const optional_desired = [
1162
+ // Profiling only, and some browsers withhold it on hardware that is otherwise
1163
+ // fine so it is taken when offered and the timers go quiet when it is not
1164
+ // (E7, sponsor 2026-08-16). Requiring it would refuse to start on those browsers
1165
+ // for the sake of a feature the game does not need to run.
1166
+ WebGPUExtensionType.TimestampQuery,
1167
+ WebGPUExtensionType.Subgroups,
1168
+ WebGPUExtensionType.TextureFormatsTier1,
1169
+ ];
1170
+
1171
+ // valid required features
1172
+ for (const feature of requiredFeatures) {
1173
+ if (!adapter.features.has(feature)) {
1174
+ throw ShadeDeviceFailure.below_floor(`adapter does not support required feature '${feature}'`);
1175
+ }
1176
+ }
1177
+
1178
+ for (const feature of optional_desired) {
1179
+ if (adapter.features.has(feature)) {
1180
+ requiredFeatures.push(feature);
1181
+ }
1182
+ }
1183
+
1184
+
1185
+ device = await adapter.requestDevice({
1186
+ requiredLimits: {
1187
+ // required for large G-buffers
1188
+ maxColorAttachmentBytesPerSample: 32,
1189
+
1190
+ /*
1191
+ grab the largest buffer limit we can get, default limits are super-low
1192
+ */
1193
+ maxBufferSize: adapter.limits.maxBufferSize,
1194
+ maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
1195
+
1196
+ maxStorageBuffersPerShaderStage: 10,
1197
+ },
1198
+ requiredFeatures
1199
+ });
1200
+
1201
+ // NOTE: according to spec (as of 2025/09/19) - adapter can only be used to create one device, after which it becomes "consumed"
1202
+ // see https://www.w3.org/TR/webgpu/#adapters
1203
+
1204
+ // TODO obtained device might be "lost" if something goes wrong, would make sense to check for that
1205
+ }
1206
+
1207
+ device.lost.then(info => {
1208
+ this.#handle_device_lost(info);
1209
+ })
1210
+
1211
+ if (context === undefined) {
1212
+ const canvas = document.createElement('canvas');
1213
+ canvas.width = window.innerWidth;
1214
+ canvas.height = window.innerHeight;
1215
+
1216
+ context = canvas.getContext('webgpu');
1217
+
1218
+ if (context === null) {
1219
+ throw new Error('Failed to bind GPUCanvasContext');
1220
+ }
1221
+ }
1222
+
1223
+ assert.ok(device.features.has("indirect-first-instance"), '"indirect-first-instance" feature needs to be enabled for non-zero firstInstance values to be used.')
1224
+ assert.isInstanceOf(context, GPUCanvasContext, 'context', 'GPUCanvasContext');
1225
+
1226
+ this.context = context;
1227
+ this.device = device;
1228
+
1229
+ this.#pixel_ratio = pixelRatio;
1230
+
1231
+ // clientWidth/clientHeight are 0 for a canvas that is not attached to the DOM,
1232
+ // which is always the case for the renderer-owned canvas created above.
1233
+ // Fall back to the backing-store size so that #configure_context doesn't
1234
+ // overwrite the just-set canvas size with the 1x1 minimum resolution.
1235
+ this.#size.set(
1236
+ context.canvas.clientWidth || context.canvas.width,
1237
+ context.canvas.clientHeight || context.canvas.height
1238
+ );
1239
+
1240
+ this.#update_output_resolution();
1241
+
1242
+ this.#graphics = new GraphicsContext(device);
1243
+
1244
+ const graphics = this.#graphics;
1245
+
1246
+ await graphics.initialize();
1247
+
1248
+
1249
+ this.#scenes = new SceneManager(graphics);
1250
+
1251
+ this.#cameras = new CameraManager(this.device);
1252
+ this.#views = new ViewManager(graphics, this.#cameras, this.#scenes);
1253
+
1254
+ const resolution_internal = this.#internal_resolution.asArray();
1255
+
1256
+ for (let i = 0; i < this.#textures_depth.length; i++) {
1257
+
1258
+ this.#textures_depth[i] = graphics.textures.contextFromDescriptor(TextureDescriptor.from({
1259
+ label: `Depth ${i}`,
1260
+ size: resolution_internal,
1261
+ format: 'depth32float',
1262
+ mipLevelCount: G_BUFFER_MIP_LEVELS,
1263
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC,
1264
+ }));
1265
+ }
1266
+
1267
+
1268
+ const resolution_out = this.#output_resolution.asArray();
1269
+
1270
+ for (let i = 0; i < this.#textures_taa_history.length; i++) {
1271
+
1272
+ this.#textures_taa_history[i] = graphics.textures.contextFromDescriptor(TextureDescriptor.from({
1273
+ label: `Color ${i}`,
1274
+ size: resolution_out,
1275
+ format: "rgba16float",
1276
+ mipLevelCount: 1,
1277
+ // STORAGE_BINDING is required for the NSS postprocess
1278
+ // compute shader to write here. TAA continues to use
1279
+ // RENDER_ATTACHMENT — the flags coexist on the same
1280
+ // texture and the active upscaler picks its preferred
1281
+ // write path each frame.
1282
+ usage: GPUTextureUsage.RENDER_ATTACHMENT
1283
+ | GPUTextureUsage.TEXTURE_BINDING
1284
+ | GPUTextureUsage.STORAGE_BINDING
1285
+ | GPUTextureUsage.COPY_SRC,
1286
+ }));
1287
+ }
1288
+
1289
+ this.#autoexposure = new GPUCameraExposureManager(device);
1290
+ this.#depth_of_field = new DepthOfField(device);
1291
+ this.#depth_of_field_unreal = new DepthOfFieldU(device);
1292
+ this.#motion_blur = new MotionBlur();
1293
+
1294
+ this.#postprocess = new PostProcess(this.#graphics);
1295
+
1296
+ this.#restir_di = new ReSTIRDI(this.#graphics);
1297
+
1298
+ this.#configure_context();
1299
+ this.init_render_targets();
1300
+
1301
+ window.matchMedia("(dynamic-range: high)").addEventListener("change", this.#update_hdr_support);
1302
+
1303
+ }
1304
+
1305
+ /**
1306
+ * Cleanup
1307
+ */
1308
+ destroy() {
1309
+
1310
+ window.matchMedia("(dynamic-range: high)").removeEventListener("change", this.#update_hdr_support);
1311
+ }
1312
+
1313
+
1314
+ /**
1315
+ * @type {RenderTarget}
1316
+ */
1317
+ #vis_render_target;
1318
+
1319
+ init_render_targets() {
1320
+ const vis_rt = new RenderTarget();
1321
+ this.#vis_render_target = vis_rt;
1322
+
1323
+ const textures = this.#graphics.textures;
1324
+
1325
+ // TODO these can be moved to frame graph instead and managed in a transient manner
1326
+ const internal_resolution_array = this.#internal_resolution.asArray();
1327
+
1328
+ vis_rt.color_attachments = [
1329
+ textures.contextFromDescriptor(TextureDescriptor.from({
1330
+ size: internal_resolution_array,
1331
+ format: "r32uint",
1332
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
1333
+ })),
1334
+ textures.contextFromDescriptor(TextureDescriptor.from({
1335
+ size: internal_resolution_array,
1336
+ format: "r32uint",
1337
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
1338
+ })),
1339
+ ];
1340
+
1341
+ vis_rt.depth_attachment = this.#textures_depth[0];
1342
+
1343
+ }
1344
+
1345
+ /**
1346
+ * If set to true, will force {@link #configure_context} on next render
1347
+ * @type {boolean}
1348
+ */
1349
+ #context_needs_update = false;
1350
+
1351
+ #configure_context() {
1352
+ const context = this.context;
1353
+
1354
+ const resolution = this.#output_resolution;
1355
+
1356
+ context.canvas.width = resolution.x;
1357
+ context.canvas.height = resolution.y;
1358
+
1359
+ // canvas must be scaled on-screen differently from resolution to account for pixel ratio
1360
+ const size = this.#size;
1361
+ context.canvas.style.width = `${size.x}px`;
1362
+ context.canvas.style.height = `${size.y}px`;
1363
+
1364
+ const preferred_color_space = 'display-p3';
1365
+
1366
+
1367
+ const config = {
1368
+ device: this.device,
1369
+ format: this.#presentation_format,
1370
+ alphaMode: 'opaque',
1371
+ colorSpace: preferred_color_space,
1372
+ toneMapping: {
1373
+ // see https://www.w3.org/TR/webgpu/#dom-gpucanvastonemappingmode-extended
1374
+ mode: this.#hdr_supported ? "extended" : "standard"
1375
+ }
1376
+ };
1377
+
1378
+
1379
+ try {
1380
+ context.configure(config);
1381
+ } catch (e) {
1382
+ // try fall-back colorspace
1383
+ context.configure({
1384
+ ...config,
1385
+ colorSpace: 'srgb'
1386
+ });
1387
+ }
1388
+
1389
+ }
1390
+
1391
+ #apply_size() {
1392
+ const out_w = this.#output_resolution.x;
1393
+ const out_h = this.#output_resolution.y;
1394
+
1395
+ this.#textures_taa_history.forEach(t => t.resize(out_w, out_h));
1396
+
1397
+
1398
+ this.#context_needs_update = true;
1399
+ }
1400
+
1401
+
1402
+ #update_internal_resolution() {
1403
+ const out_w = this.#output_resolution.x;
1404
+ const out_h = this.#output_resolution.y;
1405
+
1406
+ const dimension_limit = this.device.limits.maxTextureDimension2D;
1407
+
1408
+ const irs = this.#internal_resolution_scale;
1409
+
1410
+ const _iw = clamp(Math.floor(out_w * irs), INTERNAL_RESOLUTION_MIN, dimension_limit);
1411
+ const _ih = clamp(Math.floor(out_h * irs), INTERNAL_RESOLUTION_MIN, dimension_limit);
1412
+
1413
+ this.#internal_resolution.set(_iw, _ih);
1414
+
1415
+ // figure out exact x/y resolution scale
1416
+ const irs_x_inv = out_w / _iw;
1417
+ const irs_y_int = out_h / _ih;
1418
+
1419
+ // we need to make TAA sequence long enough to provide full coverage over output resolution texels
1420
+ const taa_sequence_scale = Math.max(1, irs_x_inv * irs_y_int);
1421
+
1422
+ const upscale_factor = 1 / irs;
1423
+
1424
+ // shrink the sequence as upscale factor increases
1425
+ // we lose crispness of the edges, but we have to trade it to get faster convergence
1426
+ const base_sequence_size = remap(
1427
+ 1, 2,
1428
+ 16, 4,
1429
+ clamp(upscale_factor, 1, 2),
1430
+ );
1431
+
1432
+ this.#taa.jitter_sequence_size = Math.ceil(base_sequence_size * taa_sequence_scale);
1433
+
1434
+ // ARM's NSS expects `8 * upscale_ratio^2` jitter phases. Apply the
1435
+ // same coverage scaling as TAA so DPR-mismatch resolutions still
1436
+ // get full coverage. Only applied if the NSS instance has been
1437
+ // created — no point spinning it up just to set a number.
1438
+ if (this.#nss !== undefined) {
1439
+ const nss_base = NSS.recommended_jitter_sequence_size(upscale_factor);
1440
+ this.#nss.jitter_sequence_size = Math.ceil(nss_base * taa_sequence_scale);
1441
+ }
1442
+
1443
+ const in_w = this.#internal_resolution.x;
1444
+ const in_h = this.#internal_resolution.y;
1445
+
1446
+ this.#textures_depth.forEach(t => {
1447
+ t.resize(in_w, in_h)
1448
+ });
1449
+
1450
+ this.#vis_render_target.resize(in_w, in_h);
1451
+
1452
+ // resizing destroys BOTH ping-pong depth textures, including the one holding last
1453
+ // frame's depth — disocclusion/SSAO would read a zeroed "previous depth" this frame
1454
+ // and silently corrupt temporal history. The external resize() path resets history
1455
+ // via indicate_view_change(); internal-resolution changes (DRS steps) must too.
1456
+ this.indicate_view_change();
1457
+
1458
+ this.#internal_resolution_needs_update = false;
1459
+ }
1460
+
1461
+ #update_output_resolution() {
1462
+ const size = this.#size;
1463
+ const pixel_ratio = this.#pixel_ratio;
1464
+
1465
+ const dimension_limit = this.device.limits.maxTextureDimension2D;
1466
+
1467
+ // limits are there to prevent crashing out
1468
+ const w = clamp(Math.ceil(size.x * pixel_ratio), 1, dimension_limit);
1469
+ const h = clamp(Math.ceil(size.y * pixel_ratio), 1, dimension_limit);
1470
+
1471
+ this.#output_resolution.set(w, h);
1472
+
1473
+ this.#internal_resolution_needs_update = true;
1474
+ }
1475
+
1476
+ /**
1477
+ * Make sure to resize at the start of the frame to avoid flicker
1478
+ *
1479
+ * @param {number} x
1480
+ * @param {number} y
1481
+ */
1482
+ resize(x, y) {
1483
+ assert.isNonNegativeInteger(x, 'x');
1484
+ assert.isNonNegativeInteger(y, 'y');
1485
+
1486
+ const size = this.#size;
1487
+
1488
+ if (size.x === x && size.y === y) {
1489
+ // no change
1490
+ return;
1491
+ }
1492
+
1493
+ size.set(x, y);
1494
+
1495
+ this.#update_resolution();
1496
+ }
1497
+
1498
+ /**
1499
+ * Does not check for changes, so make sure to call this sparingly
1500
+ */
1501
+ #update_resolution() {
1502
+
1503
+ this.#update_output_resolution();
1504
+
1505
+ if (this.#internal_resolution_needs_update) {
1506
+ this.#update_internal_resolution();
1507
+ }
1508
+
1509
+ this.#apply_size();
1510
+
1511
+ // drop history buffers to prevent artifacts
1512
+ this.indicate_view_change();
1513
+ }
1514
+
1515
+ /**
1516
+ * Bring the canvas context up to date with the size the renderer is drawing at.
1517
+ *
1518
+ * **Only {@link render} calls this, and that is the point.** Configuring a context resizes the
1519
+ * canvas and invalidates the texture it last handed out, so it has to happen before the frame
1520
+ * asks for one — and a frame going somewhere other than the canvas must not do it at all, or
1521
+ * every offscreen picture would resize the page's canvas to its own size and back.
1522
+ */
1523
+ #ensure_context_configured() {
1524
+ this.#update_resolution_if_needed();
1525
+
1526
+ if (this.#context_needs_update) {
1527
+ this.#configure_context();
1528
+ this.#context_needs_update = false;
1529
+ }
1530
+ }
1531
+
1532
+ #update_resolution_if_needed() {
1533
+ if (this.#internal_resolution_needs_update) {
1534
+ this.#update_internal_resolution();
1535
+ }
1536
+ }
1537
+
1538
+ #begin_frame() {
1539
+
1540
+ this.#update_resolution_if_needed();
1541
+
1542
+ this.#graphics.update();
1543
+
1544
+ // Virtual texture maintenance: apply arrived pages (budgeted
1545
+ // uploads), flush page-table dirty rects and stack info, issue
1546
+ // streaming loads. Queue-ordered before this frame's submits, so
1547
+ // the material pass samples a consistent table+cache snapshot.
1548
+ if (this.feature_virtual_textures) {
1549
+ this.#graphics.virtual_textures.update();
1550
+ }
1551
+
1552
+ // cycle depth attachment
1553
+ this.#vis_render_target.depth_attachment = this.texture_depth_current;
1554
+ this.#postprocess.update(this.#frame_count);
1555
+
1556
+ // Advance whichever upscaler we're going to ask for jitter on this
1557
+ // frame. Both share the same Halton(2, 3) sequence; we only tick
1558
+ // the active one to avoid spinning up NSS just to drive a counter.
1559
+ if (this.upscale_type === ShadeUpscalerType.NSS) {
1560
+ const nss = this.nss;
1561
+ nss.frame_count = this.#frame_count;
1562
+ nss.frame_index = this.#frame_count;
1563
+ } else {
1564
+ this.#taa.frame_index = this.#frame_count;
1565
+ }
1566
+ }
1567
+
1568
+ #end_frame() {
1569
+ this.#frame_count++;
1570
+
1571
+ // dispatch event
1572
+ this.onFrameFinished.send1(this.#frame_count);
1573
+ }
1574
+
1575
+ /**
1576
+ * @type {AccumulatingPathTracer}
1577
+ */
1578
+ #path_tracer;
1579
+
1580
+ get path_tracer() {
1581
+ if (this.#path_tracer === undefined) {
1582
+ this.#path_tracer = new AccumulatingPathTracer(this.#graphics);
1583
+ }
1584
+
1585
+ return this.#path_tracer;
1586
+ }
1587
+
1588
+ /**
1589
+ * Draw a frame onto the canvas this renderer is bound to.
1590
+ *
1591
+ * The canvas is one target among others: this is {@link render_to_target} with the canvas's
1592
+ * current texture as the target, and nothing else.
1593
+ *
1594
+ * @param {PerspectiveCamera} camera
1595
+ * @param {Scene} scene
1596
+ * @param {number} [time_delta_seconds]
1597
+ * @returns {boolean} whether frame was rendered, will return false if previous frame hasn't finished yet
1598
+ */
1599
+ render(
1600
+ camera,
1601
+ scene,
1602
+ time_delta_seconds = 0.01666
1603
+ ) {
1604
+ if (this.#device_lost) {
1605
+ // device is lost, can't render and asking the context for a texture would throw
1606
+ return false;
1607
+ }
1608
+
1609
+ this.#ensure_context_configured();
1610
+
1611
+ const canvas_texture = this.context.getCurrentTexture();
1612
+
1613
+ return this.render_to_target(
1614
+ camera,
1615
+ scene,
1616
+ time_delta_seconds,
1617
+ GPUTextureContext.fromTexture(canvas_texture, this.device)
1618
+ );
1619
+ }
1620
+
1621
+ /**
1622
+ * Draw a frame into a texture of the caller's choosing.
1623
+ *
1624
+ * **The target says where the picture goes, not how big it is.** Everything the frame is built
1625
+ * out of — the G-buffer, the visibility buffer, the depth ping-pong, the temporal history — is
1626
+ * sized from {@link output_resolution}, so a target has to be that size; a caller wanting a
1627
+ * different one calls {@link resize} first and puts the size back afterwards. Rendering into a
1628
+ * texture the caller owns is what makes a frame readable: a canvas texture is configured by the
1629
+ * presentation path and cannot carry `COPY_SRC`, and one of these can.
1630
+ *
1631
+ * @param {PerspectiveCamera} camera
1632
+ * @param {Scene} scene
1633
+ * @param {number} time_delta_seconds
1634
+ * @param {GPUTextureContext} target must match {@link output_resolution}
1635
+ * @returns {boolean} whether frame was rendered, will return false if previous frame hasn't finished yet
1636
+ */
1637
+ render_to_target(
1638
+ camera,
1639
+ scene,
1640
+ time_delta_seconds,
1641
+ target
1642
+ ) {
1643
+ assert.defined(target, 'target');
1644
+ assert.equal(target.isGPUTextureContext, true, 'target.isGPUTextureContext !== true');
1645
+
1646
+ if (this.#device_lost) {
1647
+ // device is lost, can't render
1648
+ return false;
1649
+ }
1650
+
1651
+ this.#begin_frame();
1652
+
1653
+
1654
+ if (this.indirect_lighting_mode === ShadeIndirectLightingMode.LPV) {
1655
+ this.update_lpv(scene);
1656
+ }
1657
+
1658
+ const view = View.from(camera, scene);
1659
+ view.label = "Main View";
1660
+ const view_ctx = this.#views.obtain(view);
1661
+
1662
+ const graphics = this.#graphics;
1663
+
1664
+ // Shadow allocation + per-light shadow_id assignment happens inside
1665
+ // GPULightCollection.update (which view_ctx.update will trigger via
1666
+ // GPUSceneContext.update below), so by the time we reach select_for_draw the
1667
+ // light records on the GPU already carry their up-to-date shadow_id. Forward the
1668
+ // feature flag so process_lights inside lights.update can evict everything when
1669
+ // the renderer has shadows turned off — keeps CASTS_SHADOW_BIT cleared on every
1670
+ // light record without further plumbing.
1671
+ const sm = view_ctx.scene.lights.shadow_context;
1672
+ sm.enabled = this.#feature_shadows_enabled;
1673
+ // Which point-light shadow generator to use this frame; a flip triggers a rebuild of
1674
+ // existing point maps inside process_lights (run via view_ctx.update above/below).
1675
+ sm.use_tetrahedron_point_shadows = this.feature_tetrahedron_point_shadows;
1676
+
1677
+ const taa = this.#taa;
1678
+
1679
+ // Whichever upscaler is going to consume the temporal sequence
1680
+ // also has to dictate the jitter applied to the camera projection.
1681
+ // Both share Halton(2, 3); they just drive different jitter
1682
+ // sequence sizes via #update_internal_resolution.
1683
+ const jitter_source = this.upscale_type === ShadeUpscalerType.NSS
1684
+ ? this.nss
1685
+ : taa;
1686
+
1687
+ const resolution_internal = this.#internal_resolution;
1688
+ {
1689
+ const jitter_x = jitter_source.Jitter[0];
1690
+ const jitter_y = jitter_source.Jitter[1];
1691
+
1692
+ view_ctx.setJitter(jitter_x, jitter_y);
1693
+ view_ctx.setViewportSize(resolution_internal.x, resolution_internal.y);
1694
+ view_ctx.setUpscaleRatio(
1695
+ this.#output_resolution.x / resolution_internal.x,
1696
+ this.#output_resolution.y / resolution_internal.y
1697
+ );
1698
+ // 2x offsets because the offsets are in NDC (-1, 1)
1699
+ view_ctx.camera.setViewportOffset(
1700
+ (2 * jitter_x) / resolution_internal.x,
1701
+ (2 * jitter_y) / resolution_internal.y
1702
+ );
1703
+
1704
+ }
1705
+
1706
+ view_ctx.update(graphics);
1707
+
1708
+ const command_ctx = ShadeGPUCommandContext.create(
1709
+ graphics,
1710
+ "Renderer/main-0"
1711
+ );
1712
+
1713
+ if (this.#debug_frame_count > 0) {
1714
+ this.#debug_frame_count--;
1715
+
1716
+ const current_frame = this.#frame_count;
1717
+
1718
+ command_ctx.enable_debug_timers((table) => {
1719
+ this.onFrameDebug.send2(current_frame, table);
1720
+ });
1721
+ }
1722
+
1723
+ /**
1724
+ * The frame recorder for this frame, or null when nothing is profiling.
1725
+ * @type {GPUFrameRecorder|null}
1726
+ */
1727
+ let profile_recorder = null;
1728
+
1729
+ if (this.profile_session !== null) {
1730
+ // The session hands out the recorder; nothing here imports the profiler, which is what
1731
+ // keeps it out of a bundle that never asks for it. Null when the session is not running.
1732
+ profile_recorder = this.profile_session.begin_frame(this.#frame_count, performance.now());
1733
+
1734
+ if (profile_recorder !== null) {
1735
+ command_ctx.enable_profiling(profile_recorder, 0);
1736
+ }
1737
+ }
1738
+
1739
+ // Per-frame animation tick — runs before shadow allocation so
1740
+ // animated transforms (joint poses, mesh.global from animated
1741
+ // parents) are in the scene database by the time shadow
1742
+ // culling samples it. No-op when nothing's registered.
1743
+ view_ctx.scene.tick(command_ctx, time_delta_seconds);
1744
+
1745
+ const viz_rt = this.#vis_render_target;
1746
+
1747
+ // prepare shadows (process_lights already ran inside lights.update via view_ctx.update,
1748
+ // so the GPU light records already carry their up-to-date shadow_id)
1749
+ if (this.#feature_shadows_enabled) {
1750
+ // Two-stage: (1) score+select which maps refresh this frame and publish their metadata,
1751
+ // (2) rasterize the selected maps. The context internally dispatches to the right
1752
+ // raster path per light type — directional/spot go direct, point lights do a cube
1753
+ // pass plus octahedral remap so we don't have to juggle job arrays out here.
1754
+ // draw() also writes per-shadow records into the scene's GPU light database
1755
+ // (shadow_point/shadow_spot/shadow_directional tables) via its owner reference.
1756
+ sm.select_for_draw(camera, view_ctx.scene, view_ctx.frame_index, view_ctx.resolution);
1757
+ sm.draw(command_ctx);
1758
+ }
1759
+
1760
+
1761
+ const graph = new FrameGraph("Shading");
1762
+
1763
+ /*
1764
+ The view is updated and nothing has been rasterized, so an extension here is doing the
1765
+ per-frame compute that later phases depend on. There are no records yet — the frame has
1766
+ built nothing to hand out.
1767
+ */
1768
+ const frame = new FrameContext(graph, view_ctx);
1769
+
1770
+ frame.phase = FramePhase.FrameStart;
1771
+
1772
+ this.#extensions.run(frame);
1773
+
1774
+ // build main view rasterization job
1775
+ const raster_main = new RasterizationJob();
1776
+ raster_main.view_ctx = view_ctx;
1777
+ raster_main.render_target = viz_rt;
1778
+ raster_main.opaque_pass = viz_rasterization_opaque_pass_descriptor;
1779
+ raster_main.alpha_tested_pass = viz_rasterization_alpha_tested_pass_descriptor;
1780
+
1781
+ const gr_render_buckets = graph_rasterize_scene({
1782
+ job: raster_main,
1783
+ graph,
1784
+ limits: graphics.collection_limits,
1785
+ });
1786
+
1787
+ // `let` rather than `const` because an extension at `AfterGBuffer` may publish replacements
1788
+ let gr_viz_buffer_triangle = graph_import_texture(graph, this.#vis_render_target.color_attachments[0], "viz_triangle");
1789
+ let gr_viz_buffer_mesh = graph_import_texture(graph, this.#vis_render_target.color_attachments[1], "viz_mesh");
1790
+
1791
+ // Virtual texture feedback: derive per-pixel page usage from the viz
1792
+ // buffer (no extra geometry pass) into a unique-request hash set and
1793
+ // kick its async readback. No-op unless VT stacks are registered.
1794
+ if (this.feature_virtual_textures) {
1795
+ graphics.virtual_textures.graph_feedback({
1796
+ graph,
1797
+ view: view_ctx,
1798
+ texture_viz_mesh: gr_viz_buffer_mesh,
1799
+ texture_viz_triangle: gr_viz_buffer_triangle,
1800
+ material_metadata_buffer: graphics.materials.metadata_table.buffer,
1801
+ });
1802
+ }
1803
+
1804
+ // run materials
1805
+ const mat_g = graphics.materials.graph_viz_render({
1806
+ graph,
1807
+ view: view_ctx,
1808
+ texture_viz_mesh: gr_viz_buffer_mesh,
1809
+ texture_viz_triangle: gr_viz_buffer_triangle,
1810
+ });
1811
+
1812
+ // update texture references
1813
+ // `let` rather than `const` because a pass injected at `AfterGBuffer` may replace any of
1814
+ // them; see the read-back below.
1815
+ let gr_texture_pbr = mat_g.g_pbr;
1816
+ let gr_texture_normal = mat_g.g_normal;
1817
+ let gr_texture_material_albedo_ao = mat_g.g_albedo;
1818
+ let gr_texture_material_emissive = mat_g.g_emissive;
1819
+
1820
+ // do mips for G-buffer
1821
+ // mipmap_nearest(command_ctx, this.texture_depth_current);
1822
+ // this.gBuffer.mipmap(command_ctx);
1823
+
1824
+ // Update lights position
1825
+ command_ctx.pushDebugGroup("Shade");
1826
+
1827
+ const resolution = resolution_internal.asArray();
1828
+
1829
+ const scene_ctx = view_ctx.scene;
1830
+ const lights = scene_ctx.lights;
1831
+
1832
+
1833
+ const gr_texture_canvas = graph_import_texture(graph, target, "canvas");
1834
+ let gr_texture_depth_current = graph_import_texture(graph, this.texture_depth_current, "depth");
1835
+ const gr_buffer_camera_current = graph_import_buffer(graph, view_ctx.camera.gpu_buffer, "camera/current");
1836
+ const gr_buffer_camera_previous = graph_import_buffer(graph, view_ctx.gpu_previous_camera_state.gpu_buffer, "camera/previous");
1837
+ const gr_texture_hzb = graph_import_texture(graph, view_ctx.hierarchical_z_buffer.texture, "HZB");
1838
+ const gr_texture_environment = graph_import_texture(graph, view_ctx.scene.lights.environment);
1839
+
1840
+ const taa_history_0 = this.#get_texture_taa_history(0);
1841
+ const taa_history_1 = this.#get_texture_taa_history(-1);
1842
+
1843
+ const gr_texture_taa_history = graph_import_texture(graph, taa_history_0, "taa history");
1844
+ const gr_texture_taa_out = graph_import_texture(graph, taa_history_1, "taa out");
1845
+ let gr_texture_depth_previous = graph_import_texture(graph, this.texture_depth_previous, "depth previous");
1846
+
1847
+ const gr_shadowmap_atlas = graph_import_texture(graph, sm.texture);
1848
+
1849
+ // The frame's surface handles exist now, so this is where the records that carry them are
1850
+ // published. There is no colour yet, which is what makes this the phase for G-buffer work.
1851
+ frame.phase = FramePhase.AfterGBuffer;
1852
+
1853
+ const record_gbuffer = frame.create(GBufferTextures);
1854
+ const record_view_textures = frame.create(ViewTextures);
1855
+
1856
+ record_gbuffer.albedo = gr_texture_material_albedo_ao;
1857
+ record_gbuffer.normal = gr_texture_normal;
1858
+ record_gbuffer.pbr = gr_texture_pbr;
1859
+ record_gbuffer.emissive = gr_texture_material_emissive;
1860
+
1861
+ record_view_textures.depth = gr_texture_depth_current;
1862
+ record_view_textures.depth_previous = gr_texture_depth_previous;
1863
+ record_view_textures.visibility_mesh = gr_viz_buffer_mesh;
1864
+ record_view_textures.visibility_triangle = gr_viz_buffer_triangle;
1865
+
1866
+ this.#extensions.run(frame);
1867
+
1868
+ /*
1869
+ `GBufferTextures` is in/out at this phase, and frozen after it. An extension that wants to
1870
+ *change* surface properties — decals are the reason this exists cannot blend into these
1871
+ targets: two of the four are integer formats, and one texture cannot be a render attachment
1872
+ and a sampled source in the same pass. So it reads them, writes replacements, and publishes
1873
+ the new handles. Everything downstream carries what it left.
1874
+
1875
+ Writing the originals in place would work by accident rather than by contract: a written
1876
+ handle is a *renamed* clone of the same physical resource, so the change would land in the
1877
+ texture the shading pass reads only because the graph happens to execute in insertion
1878
+ order, with no dependency edge saying so. Threading the handles makes the edge real.
1879
+ */
1880
+ gr_texture_pbr = record_gbuffer.pbr;
1881
+ gr_texture_normal = record_gbuffer.normal;
1882
+ gr_texture_material_albedo_ao = record_gbuffer.albedo;
1883
+ gr_texture_material_emissive = record_gbuffer.emissive;
1884
+
1885
+ gr_texture_depth_current = record_view_textures.depth;
1886
+ gr_texture_depth_previous = record_view_textures.depth_previous;
1887
+ gr_viz_buffer_mesh = record_view_textures.visibility_mesh;
1888
+ gr_viz_buffer_triangle = record_view_textures.visibility_triangle;
1889
+
1890
+ // Velocity pass with three branches: rotation-reprojection
1891
+ // background, rigid per-object foreground (mesh.global delta),
1892
+ // and skinned per-vertex foreground (barycentric interpolation
1893
+ // of the triangle's mesh-local `position_prev` written by
1894
+ // skinning into the side-buffer the skinning context owns).
1895
+ // Per-vertex limb deformation lands in the final pixel-space
1896
+ // velocity, so TAA reads correct history for character animation.
1897
+ const gr_scene_database_for_velocity = graph_import_buffer(
1898
+ graph, scene_ctx.scene_database_buffer, "scene_database/velocity"
1899
+ );
1900
+ const gr_meshlet_metadata_for_velocity = graph_import_buffer(
1901
+ graph, scene_ctx.geometries.meshlets.buffer_metadata, "meshlet_metadata/velocity"
1902
+ );
1903
+ const gr_meshlet_data_for_velocity = graph_import_buffer(
1904
+ graph, scene_ctx.geometries.meshlets.buffer_data, "meshlet_data/velocity"
1905
+ );
1906
+ // Skinning side-buffers — both null when no SkinnedMesh has
1907
+ // been registered. The velocity shader still references these
1908
+ // bindings (the dormant per-vertex branch), so the bind group
1909
+ // must supply *something*; use the scene_database buffer as a
1910
+ // placeholder when skinning is inactive. The shader is told via
1911
+ // `skinning_active` whether the bindings are real — when false
1912
+ // it never reads them and forces the rigid path, so the
1913
+ // placeholder is never sampled. (Binding the placeholder
1914
+ // without that guard was the source of phantom velocity on
1915
+ // static meshes: `prev_position_offsets[meshlet_id]` read an
1916
+ // unrelated scene_database word, and any non-0xFFFFFFFF value
1917
+ // sent a static triangle down the skinned branch.)
1918
+ const skinning_ctx = scene_ctx.skinning;
1919
+ const skinning_active = skinning_ctx.prev_position_offsets_buffer !== null
1920
+ && skinning_ctx.prev_positions_buffer !== null;
1921
+ const gr_prev_position_offsets = graph_import_buffer(
1922
+ graph,
1923
+ skinning_ctx.prev_position_offsets_buffer ?? scene_ctx.scene_database_buffer,
1924
+ "prev_position_offsets/velocity",
1925
+ );
1926
+ const gr_prev_positions = graph_import_buffer(
1927
+ graph,
1928
+ skinning_ctx.prev_positions_buffer ?? scene_ctx.scene_database_buffer,
1929
+ "prev_positions/velocity",
1930
+ );
1931
+
1932
+ let gr_velocity = graph_add_per_object_velocity({
1933
+ graph,
1934
+ texture_depth: gr_texture_depth_current,
1935
+ texture_viz_mesh: gr_viz_buffer_mesh,
1936
+ texture_viz_triangle: gr_viz_buffer_triangle,
1937
+ buffer_scene_database: gr_scene_database_for_velocity,
1938
+ buffer_meshlet_metadata: gr_meshlet_metadata_for_velocity,
1939
+ buffer_meshlet_data: gr_meshlet_data_for_velocity,
1940
+ buffer_prev_position_offsets: gr_prev_position_offsets,
1941
+ buffer_prev_positions: gr_prev_positions,
1942
+ skinning_active,
1943
+ resolution,
1944
+ view: view_ctx,
1945
+ enable_mipmap: true
1946
+ });
1947
+
1948
+ // gr_velocity = graph_mipmap_nearest(graph, gr_velocity, G_BUFFER_MIP_LEVELS);
1949
+
1950
+ let [gr_occlusion_clip] = graph_image_pass({
1951
+ graph,
1952
+ shader: shader_depth_occlusion_clip,
1953
+ output_resolution: view_ctx.resolution,
1954
+ // mipmaps are going to be necessary for half-resolution post-process passes
1955
+ output_mipmaps: true,
1956
+ inputs: {
1957
+ camera_current: gr_buffer_camera_current,
1958
+ camera_previous: gr_buffer_camera_previous,
1959
+ DepthBuffer: gr_texture_depth_current,
1960
+ PrevDepthBuffer: gr_texture_depth_previous,
1961
+ VelocityBuffer: gr_velocity
1962
+ }
1963
+ });
1964
+
1965
+ const light_clusters = graph_build_light_clusters({
1966
+ graph,
1967
+ lights: lights,
1968
+ camera: view_ctx.camera,
1969
+ hzb: gr_texture_hzb,
1970
+ resolution: view_ctx.resolution,
1971
+ });
1972
+
1973
+ // Build the view's volumetric fog LUTs (participating media -> per-froxel lighting ->
1974
+ // ray-marched integration). Needs the light clusters and shadow atlas. Built every frame so
1975
+ // the transparency (OIT) pass always has a valid LUT to sample — an empty scene integrates
1976
+ // to a no-op (transmittance 1, in-scatter 0). The opaque composite below is still skipped
1977
+ // when the scene has no fog.
1978
+ const has_volumetrics = view_ctx.scene.volumetrics.source.volumes.length > 0;
1979
+
1980
+ const volumetrics = view_ctx.volumetrics.graph_update({
1981
+ graph,
1982
+ light_cluster_data: light_clusters.data,
1983
+ light_cluster_lookup: light_clusters.lookup,
1984
+ light_cluster_parameters: light_clusters.parameters,
1985
+ shadowmap_atlas: gr_shadowmap_atlas,
1986
+ });
1987
+
1988
+ /**
1989
+ *
1990
+ * @type {number}
1991
+ */
1992
+ let current_color_out = -1;
1993
+
1994
+ // ReSTIR DI — stochastic direct lighting of local (point+spot) lights.
1995
+ // When enabled, resolve per-pixel point+spot radiance and shade with the
1996
+ // ReSTIR deferred variant (no brute-force cluster loop). The variant is a
1997
+ // separate compiled shader, selected here at the JS level rather than via
1998
+ // a runtime branch inside the shader.
1999
+ let deferred_shader = shader_deferred_main;
2000
+
2001
+ const deferred_inputs = {
2002
+ gBufferDepth: gr_texture_depth_current,
2003
+ gBufferPBR: gr_texture_pbr,
2004
+ gBufferNormal: gr_texture_normal,
2005
+ gBufferAlbedo: gr_texture_material_albedo_ao,
2006
+ gBufferEmissive: gr_texture_material_emissive,
2007
+
2008
+ //
2009
+ light_data: lights.buffer_data,
2010
+ tEnvironment: gr_texture_environment,
2011
+
2012
+ // light clusters
2013
+ cluster_parameters: light_clusters.parameters,
2014
+ cluster_lookup: light_clusters.lookup,
2015
+ cluster_data: light_clusters.data,
2016
+
2017
+ // shadows
2018
+ shadowmapAtlas: gr_shadowmap_atlas,
2019
+
2020
+ //
2021
+ camera: view_ctx.camera.buffer,
2022
+ view: view_ctx.uniform_buffer.buffer,
2023
+ };
2024
+
2025
+ if (this.feature_restir_di_enabled) {
2026
+ deferred_shader = shader_deferred_main_restir;
2027
+
2028
+ deferred_inputs.tRestirRadiance = this.#restir_di.execute({
2029
+ graph,
2030
+ view: view_ctx,
2031
+ depth: gr_texture_depth_current,
2032
+ pbr: gr_texture_pbr,
2033
+ normal: gr_texture_normal,
2034
+ albedo: gr_texture_material_albedo_ao,
2035
+ emissive: gr_texture_material_emissive,
2036
+ velocity: gr_velocity,
2037
+ occlusion_clip: gr_occlusion_clip,
2038
+ hzb: gr_texture_hzb,
2039
+ cluster_parameters: light_clusters.parameters,
2040
+ cluster_lookup: light_clusters.lookup,
2041
+ cluster_data: light_clusters.data,
2042
+ });
2043
+ }
2044
+
2045
+ // Main shading pass
2046
+ [current_color_out] = graph_image_pass({
2047
+ graph,
2048
+ shader: deferred_shader,
2049
+ input_depth: gr_texture_depth_current,
2050
+ inputs: deferred_inputs,
2051
+ output_resolution: view_ctx.resolution,
2052
+ });
2053
+
2054
+
2055
+ let bent_normals = -1;
2056
+
2057
+ if (this.feature_ssao_enabled) {
2058
+
2059
+ const ssao = this.#postprocess.ssao.graph_pass({
2060
+ graph: graph,
2061
+ frame_index: this.#frame_count,
2062
+ input_depth: gr_texture_depth_current,
2063
+ input_color: current_color_out,
2064
+ input_depth_previous: gr_texture_depth_previous,
2065
+ input_velocity: gr_velocity,
2066
+ input_normal: gr_texture_normal,
2067
+ input_occlusion: gr_occlusion_clip,
2068
+ // NOTE: AO is blended into the G-buffer
2069
+ output: gr_texture_material_albedo_ao,
2070
+ camera: gr_buffer_camera_current,
2071
+ resolution: resolution,
2072
+ });
2073
+
2074
+ gr_texture_material_albedo_ao = ssao.occlusion;
2075
+ bent_normals = ssao.bent_normals;
2076
+
2077
+ } else {
2078
+
2079
+ // default to shading normals
2080
+ bent_normals = gr_texture_normal;
2081
+
2082
+ }
2083
+
2084
+
2085
+ // Shade background
2086
+ [current_color_out] = graph_image_pass({
2087
+ graph,
2088
+ shader: shader_background,
2089
+ inputs: {
2090
+ camera: view_ctx.camera.buffer,
2091
+ view: view_ctx.uniform_buffer.buffer,
2092
+ tEnvironment: gr_texture_environment,
2093
+ },
2094
+ input_depth: gr_texture_depth_current,
2095
+ load_op: "load",
2096
+ output: [current_color_out],
2097
+ });
2098
+
2099
+ {
2100
+ // Fused brick4 path: skip the intermediate diffuse/specular targets and the
2101
+ // separate resolve, doing it all in a single shader. SSR isn't compatible
2102
+ // since it consumes the standalone specular target.
2103
+ const use_fused_indirect = this.fused_indirect
2104
+ && this.indirect_lighting_mode === ShadeIndirectLightingMode.Brick4
2105
+ && !this.feature_ssr_enabled;
2106
+
2107
+ if (use_fused_indirect) {
2108
+
2109
+ [current_color_out] = graph_image_pass({
2110
+ graph,
2111
+ shader: shader_brick4_indirect_lighting,
2112
+ output: [current_color_out],
2113
+ input_depth: gr_texture_depth_current,
2114
+ load_op: "load",
2115
+ inputs: {
2116
+ tDepth: gr_texture_depth_current,
2117
+ tGBufferNormals: gr_texture_normal,
2118
+ tBentNormals: bent_normals,
2119
+ tGBufferAlbedo: gr_texture_material_albedo_ao,
2120
+ tGBufferPBR: gr_texture_pbr,
2121
+ tSTBN_vec2: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec2,
2122
+ view: view_ctx.uniform_buffer.buffer,
2123
+ camera: gr_buffer_camera_current,
2124
+ //
2125
+ brick4: scene_ctx.volumetric_light_map.buffer,
2126
+ tSplitSum: STATIC_GRAPHICS_ENGINE_ASSETS.split_sum,
2127
+ },
2128
+ });
2129
+
2130
+ } else {
2131
+
2132
+ const { indirect_specular, indirect_diffuse } = this.#graph_indirect_lighting({
2133
+ graph,
2134
+ view_ctx,
2135
+ scene_ctx,
2136
+ bent_normals,
2137
+ current_color_out,
2138
+ //
2139
+ gr_texture_depth_current,
2140
+ gr_texture_hzb,
2141
+ gr_texture_material_albedo_ao,
2142
+ gr_texture_pbr,
2143
+ gr_texture_normal,
2144
+ gr_texture_environment,
2145
+ gr_velocity,
2146
+ gr_occlusion_clip,
2147
+ gr_buffer_camera_current,
2148
+ gr_buffer_camera_previous,
2149
+ resolution,
2150
+ });
2151
+
2152
+ // resolve indirect lighting
2153
+ [current_color_out] = graph_image_pass({
2154
+ graph,
2155
+ shader: shader_resolve_indirect_lighting,
2156
+ output: [current_color_out],
2157
+ input_depth: gr_texture_depth_current,
2158
+ load_op: "load",
2159
+ inputs: {
2160
+ tIndirectDiffuse: indirect_diffuse,
2161
+ tIndirectSpecular: indirect_specular,
2162
+ //
2163
+ tGBufferNormals: gr_texture_normal,
2164
+ tBentNormals: bent_normals,
2165
+ tGBufferAlbedo: gr_texture_material_albedo_ao,
2166
+ tGBufferPBR: gr_texture_pbr,
2167
+ tDepth: gr_texture_depth_current,
2168
+ //
2169
+ camera: gr_buffer_camera_current,
2170
+ //
2171
+ tSplitSum: STATIC_GRAPHICS_ENGINE_ASSETS.split_sum,
2172
+ },
2173
+ });
2174
+
2175
+ }
2176
+
2177
+ // current_color_out = indirect_diffuse
2178
+ // current_color_out = indirect_specular
2179
+
2180
+ }
2181
+
2182
+ // current_color_out = graph_decode_rgbe9995_to_rgb16float({
2183
+ // graph,
2184
+ // input:indirect_diffuse_near_field,
2185
+ // resolution: view_ctx.resolution,
2186
+ // });
2187
+ //
2188
+ // const [norm] = graph_image_pass({
2189
+ // shader: shader_decode_octahedral_normal,
2190
+ // inputs:{
2191
+ // tInput: bent_normals,
2192
+ // },
2193
+ // graph,
2194
+ // output_resolution: view_ctx.resolution,
2195
+ // });
2196
+ //
2197
+ // [current_color_out] = graph_image_pass({
2198
+ // graph,
2199
+ // shader: shader_draw_normals,
2200
+ // inputs:{
2201
+ // tInput: norm,
2202
+ // },
2203
+ // output_resolution: view_ctx.resolution,
2204
+ // });
2205
+
2206
+ // Composite volumetric fog over the finalized opaque + background + indirect colour.
2207
+ // A dedicated pass (not inline in the shading shaders) because indirect lighting is added
2208
+ // in a separate additively-blended pass, so the full surface radiance only exists here.
2209
+ // Runs before transparency (transparent surfaces blend over the fog) and before the main
2210
+ // TAA (which then smooths the fog's per-pixel sampling noise).
2211
+ if (has_volumetrics) {
2212
+ [current_color_out] = graph_image_pass({
2213
+ graph,
2214
+ shader: shader_composite_volumetrics,
2215
+ output_resolution: view_ctx.resolution,
2216
+ inputs: {
2217
+ tColor: current_color_out,
2218
+ gBufferDepth: gr_texture_depth_current,
2219
+ tVolumetrics: volumetrics.resolved,
2220
+ tSTBN_vec3: STATIC_GRAPHICS_ENGINE_ASSETS.stbn_vec3,
2221
+ volumetrics_metadata: volumetrics.metadata,
2222
+ camera: view_ctx.camera.buffer,
2223
+ view: view_ctx.uniform_buffer.buffer,
2224
+ },
2225
+ });
2226
+ }
2227
+
2228
+ /*
2229
+ The frame has a colour for the first time, so this is where `SceneColor` is published. It is
2230
+ republished before each later phase because the built-in passes between them replace it —
2231
+ the record names which handle currently *means* the scene colour, and the graph already
2232
+ holds the dependency edge underneath.
2233
+ */
2234
+ frame.phase = FramePhase.AfterLighting;
2235
+
2236
+ const record_scene_color = frame.create(SceneColor);
2237
+
2238
+ record_scene_color.color = current_color_out;
2239
+
2240
+ this.#extensions.run(frame);
2241
+
2242
+ current_color_out = record_scene_color.color;
2243
+ gr_texture_depth_current = record_view_textures.depth;
2244
+
2245
+ if (this.#feature_enabled_transparencies) {
2246
+ // do transparent alpha pass
2247
+ current_color_out = graph_rasterize_meshes_transparent_oit({
2248
+ graph,
2249
+ buckets: gr_render_buckets,
2250
+ view_ctx,
2251
+ viewport: [0, 0, resolution_internal.x, resolution_internal.y],
2252
+ color_texture: current_color_out,
2253
+ depth_texture: gr_texture_depth_current,
2254
+
2255
+ limits: this.graphics.collection_limits,
2256
+
2257
+ light_cluster_data: light_clusters.data,
2258
+ light_cluster_lookup: light_clusters.lookup,
2259
+ light_cluster_parameters: light_clusters.parameters,
2260
+
2261
+ shadowmap_atlas: gr_shadowmap_atlas,
2262
+
2263
+ volumetrics_lut: volumetrics.resolved,
2264
+ volumetrics_metadata: volumetrics.metadata,
2265
+
2266
+ indirect_lighting_mode: this.indirect_lighting_mode,
2267
+ });
2268
+ }
2269
+
2270
+ frame.phase = FramePhase.AfterTransparency;
2271
+
2272
+ record_scene_color.color = current_color_out;
2273
+
2274
+ this.#extensions.run(frame);
2275
+
2276
+ current_color_out = record_scene_color.color;
2277
+ gr_texture_depth_current = record_view_textures.depth;
2278
+
2279
+ // current_color_out = gr_texture_material_albedo_ao;
2280
+
2281
+ if (this.feature_path_tracing_enabled) {
2282
+ current_color_out = this.path_tracer.render({
2283
+ graph,
2284
+ view: view_ctx
2285
+ });
2286
+ }
2287
+
2288
+ // Debug override: replace the post-shading color with a
2289
+ // visualization of the velocity buffer via shader_debug_velocity.
2290
+ // Direction is encoded as hue (red = +X, cyan = -X, etc.) and
2291
+ // magnitude as brightness; the AO channel of the albedo+AO
2292
+ // G-buffer modulates the static base so geometry shape is
2293
+ // still readable where motion is zero or near-zero. Done
2294
+ // BEFORE TAA so the chain stays well-formed; TAA blending
2295
+ // the debug view is meaningless, but it's debug.
2296
+ if (this.feature_velocity_debug_view) {
2297
+ [current_color_out] = graph_image_pass({
2298
+ graph,
2299
+ shader: shader_debug_velocity,
2300
+ inputs: {
2301
+ tInput: gr_velocity,
2302
+ tAlbedoAO: gr_texture_material_albedo_ao,
2303
+ },
2304
+ output_resolution: resolution_internal.asArray(),
2305
+ });
2306
+ }
2307
+
2308
+ if (this.feature_taa_enabled) {
2309
+
2310
+ if (this.upscale_type === ShadeUpscalerType.NSS) {
2311
+ current_color_out = this.nss.graph_pass({
2312
+ graph,
2313
+ texture_color_current: current_color_out,
2314
+ texture_depth_current: gr_texture_depth_current,
2315
+ texture_velocity: gr_velocity,
2316
+ texture_disocclusion_confidence: gr_occlusion_clip,
2317
+ texture_color_history: gr_texture_taa_history,
2318
+ texture_output: gr_texture_taa_out,
2319
+ render_resolution: resolution_internal.asArray(),
2320
+ output_resolution: this.#output_resolution.asArray(),
2321
+ });
2322
+ } else {
2323
+ current_color_out = taa.graph_pass({
2324
+ graph,
2325
+ texture_output: gr_texture_taa_out,
2326
+ texture_color_current: current_color_out,
2327
+ texture_color_history: gr_texture_taa_history,
2328
+ texture_velocity: gr_velocity,
2329
+ texture_occlusion: gr_occlusion_clip,
2330
+ camera_current: gr_buffer_camera_current,
2331
+ camera_previous: gr_buffer_camera_previous,
2332
+ });
2333
+ }
2334
+ }
2335
+
2336
+ // Bokeh depth of field on the resolved HDR colour. Runs after
2337
+ // TAA (blurs a clean, converged image — TAA's neighbourhood
2338
+ // clamp would otherwise fight the defocus) and before motion
2339
+ // blur / bloom, so out-of-focus highlights bloom and the bokeh
2340
+ // carries through the rest of the post chain. Perspective-only:
2341
+ // the thin-lens CoC has no meaning under orthographic.
2342
+ if (this.feature_dof_enabled && camera.isPerspectiveCamera === true) {
2343
+ // Both implementations share the same render() signature, so the
2344
+ // call site is identical — only the subsystem differs.
2345
+ const dof = this.dof_algorithm === "unreal"
2346
+ ? this.#depth_of_field_unreal
2347
+ : this.#depth_of_field;
2348
+
2349
+ current_color_out = dof.render({
2350
+ graph,
2351
+ input_color: current_color_out,
2352
+ depth: gr_texture_depth_current,
2353
+ camera,
2354
+ camera_buffer: view_ctx.camera.buffer,
2355
+ resolution: this.#output_resolution.asArray(),
2356
+ });
2357
+ }
2358
+
2359
+ // Jimenez 2014 motion blur runs on the resolved-but-not-
2360
+ // sharpened HDR colour. TAA has already converged the
2361
+ // jittered samples; sharpening, bloom, and tonemap operate
2362
+ // on the blurred output after this. Skipped when the toggle
2363
+ // is off so the rest of the chain stays bit-identical.
2364
+ if (this.feature_motion_blur_enabled) {
2365
+ current_color_out = this.#motion_blur.render({
2366
+ graph,
2367
+ input_color: current_color_out,
2368
+ velocity: gr_velocity,
2369
+ depth: gr_texture_depth_current,
2370
+ resolution: this.#output_resolution.asArray(),
2371
+ });
2372
+ }
2373
+
2374
+ // TODO if TAA is disabled, and internal resolution is < 100% - we need to do upscale
2375
+
2376
+ if (this.feature_sharpening_enabled) {
2377
+ [current_color_out] = graph_image_pass({
2378
+ graph,
2379
+ shader: shader_ffx_rcas,
2380
+ output_resolution: this.#output_resolution.asArray(),
2381
+ inputs: {
2382
+ tInput: current_color_out,
2383
+ uSharpness: {
2384
+ value: 0.8
2385
+ }
2386
+ }
2387
+ });
2388
+ }
2389
+
2390
+ let exposure = -1;
2391
+
2392
+ if (this.feature_bloom_enabled || this.feature_automatic_exposure_enabled) {
2393
+ const bloom = graph_postprocess_bloom({
2394
+ graph,
2395
+ input: current_color_out,
2396
+ resolution: this.#output_resolution.asArray(),
2397
+ });
2398
+
2399
+ if (this.feature_bloom_enabled) {
2400
+ current_color_out = bloom.composited;
2401
+ }
2402
+
2403
+ if (this.feature_automatic_exposure_enabled) {
2404
+ // TODO force time_delta_second to a very high value on view transition
2405
+
2406
+ exposure = this.#autoexposure.update({
2407
+ graph,
2408
+ input_color: bloom.downsampled,
2409
+ time_delta_seconds,
2410
+ });
2411
+ }
2412
+
2413
+ }
2414
+
2415
+
2416
+ if (this.feature_automatic_exposure_enabled === false) {
2417
+ exposure = this.#autoexposure.unadapted({ graph });
2418
+ }
2419
+
2420
+
2421
+ // current_color_out = graph_draw_texture_as_rgba({
2422
+ // graph,
2423
+ // source: gr_shadowmap_atlas,
2424
+ // resolution: this.#output_resolution.asArray(),
2425
+ // });
2426
+
2427
+ /*
2428
+ Past the upscale: the colour published here is output-sized while the G-buffer beside it is
2429
+ still internal-sized, and `FrameContext.resolution` reads whichever of them the phase draws
2430
+ into rather than a number anybody wrote down. With `feature_taa_enabled === false` no
2431
+ upscale happened and the two agree which a constant could not express.
2432
+ */
2433
+ frame.phase = FramePhase.BeforePresent;
2434
+
2435
+ record_scene_color.color = current_color_out;
2436
+
2437
+ this.#extensions.run(frame);
2438
+
2439
+ current_color_out = record_scene_color.color;
2440
+
2441
+ if (this.#hdr_supported) {
2442
+ [current_color_out] = graph_image_pass({
2443
+ graph,
2444
+ shader: shader_tonemap_HDR,
2445
+ inputs: {
2446
+ input_color: current_color_out,
2447
+ settings: {
2448
+ peak_nits: this.#display_peak_brightness_nits,
2449
+ paper_white_nits: 100, // SDR spec is 80, but 100 has been accepted as de-facto in recent years
2450
+ },
2451
+ exposure
2452
+ },
2453
+ output: [gr_texture_canvas]
2454
+ });
2455
+ } else {
2456
+ [current_color_out] = graph_image_pass({
2457
+ graph,
2458
+ shader: shader_tonemap_LDR,
2459
+ inputs: {
2460
+ input_color: current_color_out,
2461
+ exposure
2462
+ },
2463
+ output: [gr_texture_canvas]
2464
+ });
2465
+ }
2466
+
2467
+ /*
2468
+ The canvas is written here and cannot be read: a texture may not be a render attachment and
2469
+ a sampled source in the same pass. So what this phase carries is a `PresentTarget` with one
2470
+ field and no colour to be tempted by — an extension that wants the finished image as input
2471
+ finds that out at `get` time and belongs at `BeforePresent`.
2472
+ */
2473
+ frame.phase = FramePhase.Overlay;
2474
+
2475
+ frame.create(PresentTarget).canvas = current_color_out;
2476
+
2477
+ this.#extensions.run(frame);
2478
+
2479
+ frame.close();
2480
+
2481
+ if (profile_recorder !== null) {
2482
+ // Subscribed before the graph runs; the recorder drops the handlers when it closes.
2483
+ // A frame graph is single-use, so this never outlives the frame either way.
2484
+ profile_recorder.attach_graph(graph);
2485
+ }
2486
+
2487
+ command_ctx.encodeGraph(graph);
2488
+
2489
+ command_ctx.popDebugGroup();
2490
+
2491
+ view_ctx.finish_frame(command_ctx);
2492
+
2493
+ command_ctx.finish();
2494
+
2495
+ if (profile_recorder !== null) {
2496
+ /*
2497
+ Waits on `profiling_absorbed`, not on `done`. `done` resolves from `onFinished`, which
2498
+ fires synchronously at submit — the timings have not been read back yet at that point,
2499
+ and closing there would commit an empty frame every time.
2500
+ */
2501
+ const recorder = profile_recorder;
2502
+ const session = this.profile_session;
2503
+
2504
+ command_ctx.profiling_absorbed.then(() => {
2505
+ session.end_frame(recorder, performance.now());
2506
+ });
2507
+ }
2508
+
2509
+ this.#end_frame();
2510
+
2511
+ return true;
2512
+ }
2513
+ }