@woosh/meep-engine 3.79.0 → 3.80.1

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 (309) hide show
  1. package/CHANGELOG.md +62 -3
  2. package/README.md +1 -1
  3. package/package.json +1 -1
  4. package/src/format/image/webp/vp8/VP8BoolDecoder.d.ts +45 -0
  5. package/src/format/image/webp/vp8/VP8BoolDecoder.d.ts.map +1 -0
  6. package/src/format/image/webp/vp8/VP8BoolDecoder.js +137 -0
  7. package/src/format/image/webp/vp8/vp8_decode.d.ts +21 -0
  8. package/src/format/image/webp/vp8/vp8_decode.d.ts.map +1 -0
  9. package/src/format/image/webp/vp8/vp8_decode.js +756 -0
  10. package/src/format/image/webp/vp8/vp8_loop_filter.d.ts +31 -0
  11. package/src/format/image/webp/vp8/vp8_loop_filter.d.ts.map +1 -0
  12. package/src/format/image/webp/vp8/vp8_loop_filter.js +189 -0
  13. package/src/format/image/webp/vp8/vp8_predict.d.ts +48 -0
  14. package/src/format/image/webp/vp8/vp8_predict.d.ts.map +1 -0
  15. package/src/format/image/webp/vp8/vp8_predict.js +290 -0
  16. package/src/format/image/webp/vp8/vp8_tables.d.ts +34 -0
  17. package/src/format/image/webp/vp8/vp8_tables.d.ts.map +1 -0
  18. package/src/format/image/webp/vp8/vp8_tables.js +350 -0
  19. package/src/format/image/webp/vp8/vp8_transform.d.ts +22 -0
  20. package/src/format/image/webp/vp8/vp8_transform.d.ts.map +1 -0
  21. package/src/format/image/webp/vp8/vp8_transform.js +114 -0
  22. package/src/format/image/webp/vp8/vp8_yuv_to_rgba.d.ts +23 -0
  23. package/src/format/image/webp/vp8/vp8_yuv_to_rgba.d.ts.map +1 -0
  24. package/src/format/image/webp/vp8/vp8_yuv_to_rgba.js +133 -0
  25. package/src/format/image/webp/vp8l/VP8LBitReader.d.ts +47 -0
  26. package/src/format/image/webp/vp8l/VP8LBitReader.d.ts.map +1 -0
  27. package/src/format/image/webp/vp8l/VP8LBitReader.js +84 -0
  28. package/src/format/image/webp/vp8l/VP8LPrefixCode.d.ts +27 -0
  29. package/src/format/image/webp/vp8l/VP8LPrefixCode.d.ts.map +1 -0
  30. package/src/format/image/webp/vp8l/VP8LPrefixCode.js +184 -0
  31. package/src/format/image/webp/vp8l/vp8l_decode.d.ts +28 -0
  32. package/src/format/image/webp/vp8l/vp8l_decode.d.ts.map +1 -0
  33. package/src/format/image/webp/vp8l/vp8l_decode.js +261 -0
  34. package/src/format/image/webp/vp8l/vp8l_distance.d.ts +22 -0
  35. package/src/format/image/webp/vp8l/vp8l_distance.d.ts.map +1 -0
  36. package/src/format/image/webp/vp8l/vp8l_distance.js +62 -0
  37. package/src/format/image/webp/vp8l/vp8l_inverse_transforms.d.ts +50 -0
  38. package/src/format/image/webp/vp8l/vp8l_inverse_transforms.d.ts.map +1 -0
  39. package/src/format/image/webp/vp8l/vp8l_inverse_transforms.js +267 -0
  40. package/src/format/image/webp/vp8l/vp8l_read_prefix_code.d.ts +14 -0
  41. package/src/format/image/webp/vp8l/vp8l_read_prefix_code.d.ts.map +1 -0
  42. package/src/format/image/webp/vp8l/vp8l_read_prefix_code.js +99 -0
  43. package/src/format/image/webp/webp_decode.d.ts +23 -0
  44. package/src/format/image/webp/webp_decode.d.ts.map +1 -0
  45. package/src/format/image/webp/webp_decode.js +179 -0
  46. package/src/format/image/webp/webp_decode_alpha.d.ts +18 -0
  47. package/src/format/image/webp/webp_decode_alpha.d.ts.map +1 -0
  48. package/src/format/image/webp/webp_decode_alpha.js +84 -0
  49. package/src/format/texture/{basis → astc}/astc_block.d.ts +41 -0
  50. package/src/format/texture/astc/astc_block.d.ts.map +1 -0
  51. package/src/format/texture/{basis → astc}/astc_block.js +284 -164
  52. package/src/format/texture/astc/astc_decode_block.d.ts +68 -0
  53. package/src/format/texture/astc/astc_decode_block.d.ts.map +1 -0
  54. package/src/format/texture/astc/astc_decode_block.js +1119 -0
  55. package/src/format/texture/astc/astc_encode_block_4x4.d.ts +32 -0
  56. package/src/format/texture/astc/astc_encode_block_4x4.d.ts.map +1 -0
  57. package/src/format/texture/astc/astc_encode_block_4x4.js +181 -0
  58. package/src/format/texture/astc/astc_encode_block_4x4_rgba.d.ts +32 -0
  59. package/src/format/texture/astc/astc_encode_block_4x4_rgba.d.ts.map +1 -0
  60. package/src/format/texture/astc/astc_encode_block_4x4_rgba.js +337 -0
  61. package/src/format/texture/astc/astc_integer_sequence.d.ts.map +1 -0
  62. package/src/format/texture/{basis → astc}/astc_integer_sequence.js +251 -251
  63. package/src/format/texture/astc/astc_partition.d.ts.map +1 -0
  64. package/src/format/texture/{basis → astc}/astc_partition.js +199 -199
  65. package/src/format/texture/basis/transcode/etc1s_to_astc.js +329 -329
  66. package/src/format/texture/basis/transcode/etc1s_to_etc2.d.ts.map +1 -1
  67. package/src/format/texture/basis/transcode/etc1s_to_etc2.js +305 -332
  68. package/src/format/texture/basis/transcode/fit_endpoints.d.ts.map +1 -1
  69. package/src/format/texture/basis/transcode/fit_endpoints.js +14 -27
  70. package/src/format/texture/basis/uastc/uastc_bc7_tables.d.ts +66 -0
  71. package/src/format/texture/basis/uastc/uastc_bc7_tables.d.ts.map +1 -0
  72. package/src/format/texture/basis/uastc/uastc_bc7_tables.js +148 -0
  73. package/src/format/texture/basis/uastc/uastc_decode_block.js +463 -463
  74. package/src/format/texture/basis/uastc/uastc_to_astc.js +421 -421
  75. package/src/format/texture/basis/uastc/uastc_to_bc5.d.ts +1 -6
  76. package/src/format/texture/basis/uastc/uastc_to_bc5.d.ts.map +1 -1
  77. package/src/format/texture/basis/uastc/uastc_to_bc5.js +48 -115
  78. package/src/format/texture/basis/uastc/uastc_to_bc7.d.ts.map +1 -1
  79. package/src/format/texture/basis/uastc/uastc_to_bc7.js +604 -602
  80. package/src/format/texture/bc/bc1_decode_block.d.ts +19 -0
  81. package/src/format/texture/bc/bc1_decode_block.d.ts.map +1 -0
  82. package/src/format/texture/bc/bc1_decode_block.js +22 -0
  83. package/src/format/texture/bc/bc1_decode_colors.d.ts +27 -0
  84. package/src/format/texture/bc/bc1_decode_colors.d.ts.map +1 -0
  85. package/src/format/texture/bc/bc1_decode_colors.js +100 -0
  86. package/src/format/texture/bc/bc2_decode_block.d.ts +18 -0
  87. package/src/format/texture/bc/bc2_decode_block.d.ts.map +1 -0
  88. package/src/format/texture/bc/bc2_decode_block.js +28 -0
  89. package/src/format/texture/bc/bc3_decode_block.d.ts +18 -0
  90. package/src/format/texture/bc/bc3_decode_block.d.ts.map +1 -0
  91. package/src/format/texture/bc/bc3_decode_block.js +23 -0
  92. package/src/format/texture/bc/bc4_decode_channel.d.ts +56 -0
  93. package/src/format/texture/bc/bc4_decode_channel.d.ts.map +1 -0
  94. package/src/format/texture/bc/bc4_decode_channel.js +138 -0
  95. package/src/format/texture/bc/bc4_encode_block.d.ts +43 -0
  96. package/src/format/texture/bc/bc4_encode_block.d.ts.map +1 -0
  97. package/src/format/texture/bc/bc4_encode_block.js +324 -0
  98. package/src/format/texture/bc/bc4_snorm_decode_block.d.ts +18 -0
  99. package/src/format/texture/bc/bc4_snorm_decode_block.d.ts.map +1 -0
  100. package/src/format/texture/bc/bc4_snorm_decode_block.js +27 -0
  101. package/src/format/texture/bc/bc4_unorm_decode_block.d.ts +17 -0
  102. package/src/format/texture/bc/bc4_unorm_decode_block.d.ts.map +1 -0
  103. package/src/format/texture/bc/bc4_unorm_decode_block.js +26 -0
  104. package/src/format/texture/bc/bc5_encode_block.d.ts +25 -0
  105. package/src/format/texture/bc/bc5_encode_block.d.ts.map +1 -0
  106. package/src/format/texture/bc/bc5_encode_block.js +34 -0
  107. package/src/format/texture/bc/bc5_snorm_decode_block.d.ts +18 -0
  108. package/src/format/texture/bc/bc5_snorm_decode_block.d.ts.map +1 -0
  109. package/src/format/texture/bc/bc5_snorm_decode_block.js +27 -0
  110. package/src/format/texture/bc/bc5_unorm_decode_block.d.ts +17 -0
  111. package/src/format/texture/bc/bc5_unorm_decode_block.d.ts.map +1 -0
  112. package/src/format/texture/bc/bc5_unorm_decode_block.js +26 -0
  113. package/src/format/texture/bc/bc6h_decode_texels.d.ts +30 -0
  114. package/src/format/texture/bc/bc6h_decode_texels.d.ts.map +1 -0
  115. package/src/format/texture/bc/bc6h_decode_texels.js +430 -0
  116. package/src/format/texture/bc/bc6h_sfloat_decode_block.d.ts +20 -0
  117. package/src/format/texture/bc/bc6h_sfloat_decode_block.d.ts.map +1 -0
  118. package/src/format/texture/bc/bc6h_sfloat_decode_block.js +23 -0
  119. package/src/format/texture/bc/bc6h_ufloat_decode_block.d.ts +18 -0
  120. package/src/format/texture/bc/bc6h_ufloat_decode_block.d.ts.map +1 -0
  121. package/src/format/texture/bc/bc6h_ufloat_decode_block.js +21 -0
  122. package/src/format/texture/bc/bc7_decode_block.d.ts +30 -0
  123. package/src/format/texture/bc/bc7_decode_block.d.ts.map +1 -0
  124. package/src/format/texture/bc/bc7_decode_block.js +232 -0
  125. package/src/format/texture/bc/bc7_encode_block.d.ts +39 -0
  126. package/src/format/texture/bc/bc7_encode_block.d.ts.map +1 -0
  127. package/src/format/texture/bc/bc7_encode_block.js +423 -0
  128. package/src/format/texture/bc/bc7_tables.d.ts +126 -0
  129. package/src/format/texture/bc/bc7_tables.d.ts.map +1 -0
  130. package/src/format/texture/{basis/uastc → bc}/bc7_tables.js +225 -276
  131. package/src/format/texture/bc/bc7_write_block.d.ts +65 -0
  132. package/src/format/texture/bc/bc7_write_block.d.ts.map +1 -0
  133. package/src/format/texture/{basis/uastc → bc}/bc7_write_block.js +185 -227
  134. package/src/format/texture/etc/eac_decode_channel.d.ts +50 -0
  135. package/src/format/texture/etc/eac_decode_channel.d.ts.map +1 -0
  136. package/src/format/texture/etc/eac_decode_channel.js +142 -0
  137. package/src/format/texture/etc/eac_modifiers.d.ts +15 -0
  138. package/src/format/texture/etc/eac_modifiers.d.ts.map +1 -0
  139. package/src/format/texture/etc/eac_modifiers.js +31 -0
  140. package/src/format/texture/etc/eac_r11_snorm_decode_block.d.ts +21 -0
  141. package/src/format/texture/etc/eac_r11_snorm_decode_block.d.ts.map +1 -0
  142. package/src/format/texture/etc/eac_r11_snorm_decode_block.js +30 -0
  143. package/src/format/texture/etc/eac_r11_unorm_decode_block.d.ts +20 -0
  144. package/src/format/texture/etc/eac_r11_unorm_decode_block.d.ts.map +1 -0
  145. package/src/format/texture/etc/eac_r11_unorm_decode_block.js +29 -0
  146. package/src/format/texture/etc/eac_rg11_snorm_decode_block.d.ts +19 -0
  147. package/src/format/texture/etc/eac_rg11_snorm_decode_block.d.ts.map +1 -0
  148. package/src/format/texture/etc/eac_rg11_snorm_decode_block.js +28 -0
  149. package/src/format/texture/etc/eac_rg11_unorm_decode_block.d.ts +19 -0
  150. package/src/format/texture/etc/eac_rg11_unorm_decode_block.d.ts.map +1 -0
  151. package/src/format/texture/etc/eac_rg11_unorm_decode_block.js +28 -0
  152. package/src/format/texture/etc/etc2_decode_color.d.ts +25 -0
  153. package/src/format/texture/etc/etc2_decode_color.d.ts.map +1 -0
  154. package/src/format/texture/etc/etc2_decode_color.js +370 -0
  155. package/src/format/texture/etc/etc2_rgb8_decode_block.d.ts +18 -0
  156. package/src/format/texture/etc/etc2_rgb8_decode_block.d.ts.map +1 -0
  157. package/src/format/texture/etc/etc2_rgb8_decode_block.js +21 -0
  158. package/src/format/texture/etc/etc2_rgb8a1_decode_block.d.ts +21 -0
  159. package/src/format/texture/etc/etc2_rgb8a1_decode_block.d.ts.map +1 -0
  160. package/src/format/texture/etc/etc2_rgb8a1_decode_block.js +24 -0
  161. package/src/format/texture/etc/etc2_rgba8_decode_block.d.ts +18 -0
  162. package/src/format/texture/etc/etc2_rgba8_decode_block.d.ts.map +1 -0
  163. package/src/format/texture/etc/etc2_rgba8_decode_block.js +24 -0
  164. package/src/format/texture/fit/fit_line.d.ts +23 -0
  165. package/src/format/texture/fit/fit_line.d.ts.map +1 -0
  166. package/src/format/texture/fit/fit_line.js +123 -0
  167. package/src/format/texture/fit/fit_line_refine.d.ts +21 -0
  168. package/src/format/texture/fit/fit_line_refine.d.ts.map +1 -0
  169. package/src/format/texture/fit/fit_line_refine.js +69 -0
  170. package/src/format/texture/fit/fit_line_solve.d.ts +23 -0
  171. package/src/format/texture/fit/fit_line_solve.d.ts.map +1 -0
  172. package/src/format/texture/fit/fit_line_solve.js +71 -0
  173. package/src/format/texture/texel_decode_image.d.ts +44 -0
  174. package/src/format/texture/texel_decode_image.d.ts.map +1 -0
  175. package/src/format/texture/texel_decode_image.js +251 -0
  176. package/src/format/texture/texel_encode_image.d.ts +77 -0
  177. package/src/format/texture/texel_encode_image.d.ts.map +1 -0
  178. package/src/format/texture/texel_encode_image.js +187 -0
  179. package/src/shade/renderer/ShadeIndirectLightingMode.js +2 -0
  180. package/src/shade/renderer/acoustics/GPUAcousticLattice.d.ts +1 -1
  181. package/src/shade/renderer/acoustics/GPUAcoustics.d.ts +1 -1
  182. package/src/shade/renderer/loader/mtf/mtf_encode_texture.d.ts +1 -0
  183. package/src/shade/renderer/loader/mtf/mtf_encode_texture.d.ts.map +1 -1
  184. package/src/shade/renderer/loader/mtf/mtf_encode_texture.js +102 -28
  185. package/src/shade/renderer/loader/mtf/mtf_replace_textures.d.ts +19 -0
  186. package/src/shade/renderer/loader/mtf/mtf_replace_textures.d.ts.map +1 -0
  187. package/src/shade/renderer/loader/mtf/mtf_replace_textures.js +60 -0
  188. package/src/shade/renderer/loader/mtf/mtf_write.d.ts.map +1 -1
  189. package/src/shade/renderer/loader/mtf/mtf_write.js +7 -31
  190. package/src/shade/renderer/loader/mtf/mtf_write_collection.d.ts +23 -0
  191. package/src/shade/renderer/loader/mtf/mtf_write_collection.d.ts.map +1 -0
  192. package/src/shade/renderer/loader/mtf/mtf_write_collection.js +36 -0
  193. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4.d.ts +13 -0
  194. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4.d.ts.map +1 -0
  195. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4.js +39 -0
  196. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgb.d.ts +3 -2
  197. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgb.d.ts.map +1 -1
  198. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgb.js +6 -21
  199. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgba.d.ts +42 -0
  200. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgba.d.ts.map +1 -0
  201. package/src/shade/renderer/texture/encode/chunk_astc_encode_block_4x4_rgba.js +612 -0
  202. package/src/shade/renderer/texture/encode/chunk_bc1_encode_block.d.ts +5 -4
  203. package/src/shade/renderer/texture/encode/chunk_bc1_encode_block.d.ts.map +1 -1
  204. package/src/shade/renderer/texture/encode/chunk_bc1_encode_block.js +8 -25
  205. package/src/shade/renderer/texture/encode/chunk_bc6h_encode_block_mode11.d.ts.map +1 -1
  206. package/src/shade/renderer/texture/encode/chunk_bc6h_encode_block_mode11.js +3 -19
  207. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block.d.ts +14 -4
  208. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block.d.ts.map +1 -1
  209. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block.js +65 -54
  210. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block_mode6.d.ts +2 -2
  211. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block_mode6.d.ts.map +1 -1
  212. package/src/shade/renderer/texture/encode/chunk_bc7_encode_block_mode6.js +5 -22
  213. package/src/shade/renderer/texture/encode/chunk_bc7_partition_2.js +1 -1
  214. package/src/shade/renderer/texture/encode/chunk_scatter_principal_axis.d.ts +27 -0
  215. package/src/shade/renderer/texture/encode/chunk_scatter_principal_axis.d.ts.map +1 -0
  216. package/src/shade/renderer/texture/encode/chunk_scatter_principal_axis.js +147 -0
  217. package/src/shade/renderer/texture/encode/shader_texture_encode_astc_4x4.d.ts +3 -4
  218. package/src/shade/renderer/texture/encode/shader_texture_encode_astc_4x4.d.ts.map +1 -1
  219. package/src/shade/renderer/texture/encode/shader_texture_encode_astc_4x4.js +6 -13
  220. package/src/shade/renderer/texture/encode/shader_texture_encode_bc7_rgb.d.ts +13 -0
  221. package/src/shade/renderer/texture/encode/shader_texture_encode_bc7_rgb.d.ts.map +1 -0
  222. package/src/shade/renderer/texture/encode/shader_texture_encode_bc7_rgb.js +66 -0
  223. package/src/shade/renderer/texture/encode/texture_encode.d.ts +28 -1
  224. package/src/shade/renderer/texture/encode/texture_encode.d.ts.map +1 -1
  225. package/src/shade/renderer/texture/encode/texture_encode.js +202 -168
  226. package/src/shade/renderer/texture/encode/texture_encode_shader.d.ts +13 -1
  227. package/src/shade/renderer/texture/encode/texture_encode_shader.d.ts.map +1 -1
  228. package/src/shade/renderer/texture/encode/texture_encode_shader.js +146 -108
  229. package/src/shade/renderer/texture/format/texture_formats_to_build.d.ts +20 -0
  230. package/src/shade/renderer/texture/format/texture_formats_to_build.d.ts.map +1 -0
  231. package/src/shade/renderer/texture/format/texture_formats_to_build.js +98 -0
  232. package/src/shade/renderer/texture/mipmaps/WebGPUMipmapGenerator.d.ts.map +1 -1
  233. package/src/shade/renderer/texture/mipmaps/WebGPUMipmapGenerator.js +4 -23
  234. package/src/shade/renderer/texture/mipmaps/cpu/MipmapBilinearAxis.d.ts +53 -0
  235. package/src/shade/renderer/texture/mipmaps/cpu/MipmapBilinearAxis.d.ts.map +1 -0
  236. package/src/shade/renderer/texture/mipmaps/cpu/MipmapBilinearAxis.js +76 -0
  237. package/src/shade/renderer/texture/mipmaps/cpu/MipmapKernelAxis.d.ts +40 -0
  238. package/src/shade/renderer/texture/mipmaps/cpu/MipmapKernelAxis.d.ts.map +1 -0
  239. package/src/shade/renderer/texture/mipmaps/cpu/MipmapKernelAxis.js +43 -0
  240. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_centres.d.ts +17 -0
  241. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_centres.d.ts.map +1 -0
  242. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_centres.js +29 -0
  243. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_sample.d.ts +21 -0
  244. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_sample.d.ts.map +1 -0
  245. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_bilinear_sample.js +91 -0
  246. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_chain_cpu.d.ts +32 -0
  247. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_chain_cpu.d.ts.map +1 -0
  248. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_chain_cpu.js +216 -0
  249. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_cubic_filter_cpu.d.ts +30 -0
  250. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_cubic_filter_cpu.d.ts.map +1 -0
  251. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_cubic_filter_cpu.js +134 -0
  252. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_catmull_rom_cpu.d.ts +16 -0
  253. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_catmull_rom_cpu.d.ts.map +1 -0
  254. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_catmull_rom_cpu.js +33 -0
  255. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_cpu.d.ts +18 -0
  256. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_cpu.d.ts.map +1 -0
  257. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_cpu.js +71 -0
  258. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_normal_cpu.d.ts +20 -0
  259. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_normal_cpu.d.ts.map +1 -0
  260. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_linear_normal_cpu.js +57 -0
  261. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_magic_kernel_cpu.d.ts +18 -0
  262. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_magic_kernel_cpu.d.ts.map +1 -0
  263. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_magic_kernel_cpu.js +111 -0
  264. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mitchell_cpu.d.ts +16 -0
  265. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mitchell_cpu.d.ts.map +1 -0
  266. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mitchell_cpu.js +33 -0
  267. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mks_cpu.d.ts +18 -0
  268. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mks_cpu.d.ts.map +1 -0
  269. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_mks_cpu.js +128 -0
  270. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_wronski2021_cpu.d.ts +21 -0
  271. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_wronski2021_cpu.d.ts.map +1 -0
  272. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_filter_wronski2021_cpu.js +169 -0
  273. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_kernel_reduce.d.ts +21 -0
  274. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_kernel_reduce.d.ts.map +1 -0
  275. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_kernel_reduce.js +119 -0
  276. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_source_centre.d.ts +16 -0
  277. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_source_centre.d.ts.map +1 -0
  278. package/src/shade/renderer/texture/mipmaps/cpu/mipmap_source_centre.js +19 -0
  279. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm.d.ts +24 -0
  280. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm.d.ts.map +1 -0
  281. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm.js +41 -0
  282. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm_srgb.d.ts +24 -0
  283. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm_srgb.d.ts.map +1 -0
  284. package/src/shade/renderer/texture/mipmaps/cpu/texels_f32_to_rgba8unorm_srgb.js +140 -0
  285. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_srgb_to_f32.d.ts +21 -0
  286. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_srgb_to_f32.d.ts.map +1 -0
  287. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_srgb_to_f32.js +35 -0
  288. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_to_f32.d.ts +19 -0
  289. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_to_f32.d.ts.map +1 -0
  290. package/src/shade/renderer/texture/mipmaps/cpu/texels_rgba8unorm_to_f32.js +27 -0
  291. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_catmull_rom.js +4 -4
  292. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_linear.d.ts +13 -0
  293. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_linear.d.ts.map +1 -1
  294. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_linear.js +54 -4
  295. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_mitchell.js +4 -4
  296. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_mks.d.ts.map +1 -1
  297. package/src/shade/renderer/texture/mipmaps/filters/mipmap_shader_code_mks.js +9 -13
  298. package/src/shade/renderer/texture/texture_block_encodings.d.ts +33 -0
  299. package/src/shade/renderer/texture/texture_block_encodings.d.ts.map +1 -0
  300. package/src/shade/renderer/texture/texture_block_encodings.js +89 -0
  301. package/src/format/texture/basis/astc_block.d.ts.map +0 -1
  302. package/src/format/texture/basis/astc_integer_sequence.d.ts.map +0 -1
  303. package/src/format/texture/basis/astc_partition.d.ts.map +0 -1
  304. package/src/format/texture/basis/uastc/bc7_tables.d.ts +0 -111
  305. package/src/format/texture/basis/uastc/bc7_tables.d.ts.map +0 -1
  306. package/src/format/texture/basis/uastc/bc7_write_block.d.ts +0 -124
  307. package/src/format/texture/basis/uastc/bc7_write_block.d.ts.map +0 -1
  308. /package/src/format/texture/{basis → astc}/astc_integer_sequence.d.ts +0 -0
  309. /package/src/format/texture/{basis → astc}/astc_partition.d.ts +0 -0
@@ -1,602 +1,604 @@
1
- import {
2
- bc7_expand_endpoint,
3
- bc7_quantise_endpoint,
4
- bc7_write_block,
5
- BC7_BLOCK_BYTES,
6
- BC7_MODES,
7
- } from "./bc7_write_block.js";
8
- import {
9
- bc7_fold_subset_3_to_2,
10
- BC7_ANCHOR_2,
11
- BC7_ANCHOR_3_SECOND,
12
- BC7_ANCHOR_3_THIRD,
13
- BC7_PARTITIONS_2,
14
- BC7_PARTITIONS_3,
15
- SUBSET_PERMUTATIONS,
16
- UASTC_TO_BC7_2,
17
- UASTC_TO_BC7_2_VIA_3,
18
- UASTC_TO_BC7_3,
19
- } from "./bc7_tables.js";
20
- import { uastc_decode_block, UASTC_BLOCK_BYTES } from "./uastc_decode_block.js";
21
- import { UASTC_WEIGHT_VALUE } from "./uastc_tables.js";
22
-
23
- /**
24
- * BC7's interpolation weights, by index width. Two- and three-bit are ASTC's tables exactly, which
25
- * is what lets most weights cross over untouched.
26
- *
27
- * @readonly
28
- * @type {Object<number, Int32Array>}
29
- */
30
- const BC7_WEIGHTS = {
31
- 2: new Int32Array([0, 21, 43, 64]),
32
- 3: new Int32Array([0, 9, 18, 27, 37, 46, 55, 64]),
33
- 4: new Int32Array([0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64]),
34
- };
35
-
36
- /**
37
- * Which BC7 mode each UASTC mode is carried into, and how its partition maps.
38
- *
39
- * Taken from the specification, which names a BC7 mode against every UASTC mode. `via` says which
40
- * of the three partition mappings applies — the odd one is mode 7, two subsets in UASTC and three in
41
- * BC7.
42
- *
43
- * @readonly
44
- * @type {Object<number, {bc7: number, via: string}>}
45
- */
46
- const MODE_MAP = {
47
- 0: { bc7: 6, via: "single" },
48
-
49
- /*
50
- One subset, and the specification still pairs it with a *two*-subset BC7 mode — written as
51
- partition zero with both subsets given the same endpoints. That is not a quirk: mode 1 carries
52
- eight-bit endpoints and two-bit weights, and BC7 mode 3 is the only mode that holds eight-bit
53
- endpoints without also having an alpha channel whose parity bit would have to be traded against
54
- colour. Mode 6 would cost a bit of every channel to keep alpha at 255.
55
- */
56
- 1: { bc7: 3, via: "single_as_two" },
57
- 2: { bc7: 1, via: "two" },
58
- 3: { bc7: 2, via: "three" },
59
- 4: { bc7: 3, via: "two" },
60
- 5: { bc7: 6, via: "single" },
61
- 6: { bc7: 5, via: "dual" },
62
- 7: { bc7: 2, via: "two_via_three" },
63
- 8: { bc7: 6, via: "single" },
64
- 9: { bc7: 7, via: "two" },
65
- 10: { bc7: 6, via: "single" },
66
- 11: { bc7: 5, via: "dual" },
67
- 12: { bc7: 6, via: "single" },
68
- 13: { bc7: 5, via: "dual" },
69
- 14: { bc7: 6, via: "single" },
70
- 15: { bc7: 6, via: "single" },
71
- 16: { bc7: 7, via: "two" },
72
- 17: { bc7: 5, via: "dual" },
73
- 18: { bc7: 6, via: "single" },
74
- };
75
-
76
- /**
77
- * Transcode UASTC blocks to BC7.
78
- *
79
- * **Every UASTC mode is carried into the BC7 mode the specification pairs it with**, rather than
80
- * decoded to texels and re-encoded. A UASTC block already *is* a partition, a pair of endpoints per
81
- * subset and a weight per texel, which is exactly what BC7 stores — so the transcode is a change of
82
- * representation and the only loss is endpoint precision. Re-fitting from texels instead costs
83
- * around eighteen decibels on the dual plane modes and far more on the partitioned ones, because it
84
- * throws away the answer the encoder already found.
85
- *
86
- * The pairings, all from the specification's mode table:
87
- *
88
- * | UASTC | BC7 | why |
89
- * |---|---|---|
90
- * | 0, 1, 5, 10, 12, 14, 15, 18 | 6 | one subset, one index set |
91
- * | 8 | 6 | a solid colour, which BC7 has no mode for and mode 6 holds exactly |
92
- * | 6, 11, 13, 17 | 5 | dual plane — one channel with its own weights, which BC7 spells as a rotation |
93
- * | 2 | 1 | two subsets, three-bit weights |
94
- * | 4 | 3 | two subsets, two-bit weights, seven-bit endpoints |
95
- * | 9, 16 | 7 | two subsets with alpha |
96
- * | 3 | 2 | three subsets |
97
- * | 7 | 2 | two subsets in UASTC, three in BC7 — two of BC7's endpoint pairs are written alike |
98
- *
99
- * **The weights need no remapping.** ASTC's two- and three-bit weight tables are BC7's, and the
100
- * four-bit ones agree too, so an index crosses over as itself. Where a UASTC mode's weight range is
101
- * finer than the BC7 mode's index — only mode 18, at five bits — the nearest is taken.
102
- *
103
- * @param {Uint8Array} blocks UASTC blocks, tightly packed
104
- * @param {number} block_count
105
- * @returns {Uint8Array} tightly packed BC7 blocks, in raster order
106
- *
107
- * @author Alex Goldring
108
- * @copyright Company Named Limited (c) 2026
109
- */
110
- export function uastc_to_bc7(blocks, block_count) {
111
- const out = new Uint8Array(block_count * BC7_BLOCK_BYTES);
112
-
113
- const scratch = {
114
- endpoints: new Uint8Array(3 * 2 * 4),
115
- decoded: new Uint8Array(3 * 2 * 4),
116
- parity: new Int8Array(6),
117
- indices: new Uint8Array(16),
118
- secondary_indices: new Uint8Array(16),
119
- anchors: new Uint8Array(3),
120
- subset_of_texel: new Uint8Array(16),
121
- source_subset: new Uint8Array(3),
122
- };
123
-
124
- for (let block = 0; block < block_count; block++) {
125
- const decoded = uastc_decode_block(blocks, block * UASTC_BLOCK_BYTES);
126
-
127
- const mapping = MODE_MAP[decoded.mode];
128
-
129
- if (mapping.via === "dual") {
130
- write_dual_plane(out, block * BC7_BLOCK_BYTES, decoded, scratch);
131
- } else if (mapping.via === "single") {
132
- write_single_subset(out, block * BC7_BLOCK_BYTES, decoded, scratch);
133
- } else {
134
- write_partitioned(out, block * BC7_BLOCK_BYTES, decoded, mapping, scratch);
135
- }
136
- }
137
-
138
- return out;
139
- }
140
-
141
- /**
142
- * Map a UASTC weight index into a BC7 index of the given width.
143
- *
144
- * @param {number} index
145
- * @param {number} weight_range
146
- * @param {number} bits
147
- * @returns {number}
148
- */
149
- function map_index(index, weight_range, bits) {
150
- const from = UASTC_WEIGHT_VALUE[weight_range];
151
- const to = BC7_WEIGHTS[bits];
152
-
153
- const weight = from[index];
154
-
155
- let best = 0;
156
- let best_error = Infinity;
157
-
158
- for (let i = 0; i < to.length; i++) {
159
- const error = Math.abs(to[i] - weight);
160
-
161
- if (error < best_error) {
162
- best_error = error;
163
- best = i;
164
- }
165
- }
166
-
167
- return best;
168
- }
169
-
170
- /**
171
- * Quantise one subset's two endpoints, choosing the parity that suits them best.
172
- *
173
- * A parity bit is shared — by an endpoint's four channels, or by a subset's two endpoints — so it
174
- * cannot be fitted per channel. Both values are tried and the one with less total error kept, which
175
- * is what the specification means by an "optimal p-bit".
176
- *
177
- * @param {Uint8Array} source eight bytes, low then high RGBA
178
- * @param {number} source_offset
179
- * @param {Uint8Array} out stored fields, written
180
- * @param {number} out_offset
181
- * @param {Uint8Array} decoded what those fields decode to, written
182
- * @param {Int8Array} parity written
183
- * @param {number} parity_offset
184
- * @param {Bc7Mode} spec
185
- * @param {number} channels how many carry data — three where the mode has no alpha
186
- */
187
- function quantise_subset(
188
- source, source_offset, out, out_offset, decoded, parity, parity_offset, spec, channels
189
- ) {
190
- const per_endpoint = spec.endpoint_parity_bits > 0;
191
- const shared = spec.shared_parity_bits > 0;
192
-
193
- const color_width = spec.color_bits + (per_endpoint || shared ? 1 : 0);
194
- const alpha_width = spec.alpha_bits + (per_endpoint || shared ? 1 : 0);
195
-
196
- /**
197
- * @param {number[]} candidate one parity per endpoint
198
- * @returns {number} total squared error
199
- */
200
- const error_of = candidate => {
201
- let total = 0;
202
-
203
- for (let end = 0; end < 2; end++) {
204
- for (let c = 0; c < channels; c++) {
205
- const width = c === 3 ? alpha_width : color_width;
206
-
207
- const value = source[source_offset + end * 4 + c];
208
-
209
- const stored = bc7_quantise_endpoint(value, width, candidate[end]);
210
-
211
- const delta = bc7_expand_endpoint(stored, width) - value;
212
-
213
- total += delta * delta;
214
- }
215
- }
216
-
217
- return total;
218
- };
219
-
220
- let chosen = [-1, -1];
221
-
222
- if (per_endpoint) {
223
- // one bit each, so the two endpoints are independent and each takes its own better value
224
- for (let end = 0; end < 2; end++) {
225
- chosen[end] = error_of_end(source, source_offset, end, 0, spec, channels)
226
- <= error_of_end(source, source_offset, end, 1, spec, channels) ? 0 : 1;
227
- }
228
- } else if (shared) {
229
- // one bit for the whole subset, so both endpoints have to agree
230
- chosen = error_of([0, 0]) <= error_of([1, 1]) ? [0, 0] : [1, 1];
231
- }
232
-
233
- for (let end = 0; end < 2; end++) {
234
- for (let c = 0; c < 4; c++) {
235
- const width = c === 3 ? alpha_width : color_width;
236
-
237
- if (c === 3 && spec.alpha_bits === 0) {
238
- out[out_offset + end * 4 + c] = 0;
239
- decoded[out_offset + end * 4 + c] = 255;
240
-
241
- continue;
242
- }
243
-
244
- const value = source[source_offset + end * 4 + c];
245
-
246
- const stored = bc7_quantise_endpoint(value, width, chosen[end]);
247
-
248
- decoded[out_offset + end * 4 + c] = bc7_expand_endpoint(stored, width);
249
-
250
- // the parity bit is carried separately, so the stored field is the rest
251
- out[out_offset + end * 4 + c] = (per_endpoint || shared) ? stored >> 1 : stored;
252
- }
253
- }
254
-
255
- if (per_endpoint) {
256
- parity[parity_offset] = chosen[0];
257
- parity[parity_offset + 1] = chosen[1];
258
- } else if (shared) {
259
- parity[parity_offset] = chosen[0];
260
- }
261
- }
262
-
263
- /**
264
- * Total squared error of one endpoint under a candidate parity.
265
- *
266
- * @param {Uint8Array} source
267
- * @param {number} source_offset
268
- * @param {number} end
269
- * @param {number} parity
270
- * @param {Bc7Mode} spec
271
- * @param {number} channels
272
- * @returns {number}
273
- */
274
- function error_of_end(source, source_offset, end, parity, spec, channels) {
275
- const color_width = spec.color_bits + 1;
276
- const alpha_width = spec.alpha_bits + 1;
277
-
278
- let total = 0;
279
-
280
- for (let c = 0; c < channels; c++) {
281
- const width = c === 3 ? alpha_width : color_width;
282
-
283
- const value = source[source_offset + end * 4 + c];
284
-
285
- const delta = bc7_expand_endpoint(bc7_quantise_endpoint(value, width, parity), width)
286
- - value;
287
-
288
- total += delta * delta;
289
- }
290
-
291
- return total;
292
- }
293
-
294
- /**
295
- * Flip a subset's endpoints and invert its indices, so its anchor index fits the bit it is given.
296
- *
297
- * @param {Uint8Array} endpoints stored fields
298
- * @param {Uint8Array} decoded
299
- * @param {Int8Array} parity
300
- * @param {number} subset
301
- * @param {Bc7Mode} spec
302
- * @param {Uint8Array} indices
303
- * @param {Uint8Array} subset_of_texel
304
- * @param {number} index_count
305
- */
306
- function flip_subset(
307
- endpoints, decoded, parity, subset, spec, indices, subset_of_texel, index_count
308
- ) {
309
- for (let c = 0; c < 4; c++) {
310
- const low = (subset * 2) * 4 + c;
311
- const high = (subset * 2 + 1) * 4 + c;
312
-
313
- const swap = endpoints[low];
314
-
315
- endpoints[low] = endpoints[high];
316
- endpoints[high] = swap;
317
-
318
- const swap_decoded = decoded[low];
319
-
320
- decoded[low] = decoded[high];
321
- decoded[high] = swap_decoded;
322
- }
323
-
324
- if (spec.endpoint_parity_bits > 0) {
325
- const swap = parity[subset * 2];
326
-
327
- parity[subset * 2] = parity[subset * 2 + 1];
328
- parity[subset * 2 + 1] = swap;
329
- }
330
-
331
- for (let texel = 0; texel < 16; texel++) {
332
- if (subset_of_texel[texel] === subset) {
333
- indices[texel] = index_count - 1 - indices[texel];
334
- }
335
- }
336
- }
337
-
338
- /**
339
- * Carry a partitioned block — two or three subsets — into its BC7 counterpart.
340
- *
341
- * @param {Uint8Array} out
342
- * @param {number} offset
343
- * @param {UastcBlock} block
344
- * @param {{bc7: number, via: string}} mapping
345
- * @param {object} scratch
346
- */
347
- function write_partitioned(out, offset, block, mapping, scratch) {
348
- const spec = BC7_MODES[mapping.bc7];
349
-
350
- const { endpoints, decoded, parity, indices, anchors, subset_of_texel, source_subset } = scratch;
351
-
352
- /*
353
- Which BC7 subset each texel lands in, and which UASTC subset each BC7 subset draws its endpoints
354
- from. The two questions have different answers for mode 7, where BC7 has a subset UASTC does not
355
- — two of its three endpoint pairs end up identical.
356
- */
357
- if (mapping.via === "single_as_two") {
358
- /*
359
- One UASTC subset written as two identical BC7 ones. Partition zero divides the block, and
360
- giving both halves the same endpoints makes that division invisible — which is how a
361
- one-subset block reaches a mode that has no one-subset form.
362
- */
363
- scratch.partition = 0;
364
-
365
- subset_of_texel.set(BC7_PARTITIONS_2.subarray(0, 16));
366
-
367
- source_subset[0] = 0;
368
- source_subset[1] = 0;
369
- } else if (mapping.via === "two") {
370
- const { bc7, invert } = UASTC_TO_BC7_2[block.pattern];
371
-
372
- scratch.partition = bc7;
373
-
374
- for (let texel = 0; texel < 16; texel++) {
375
- subset_of_texel[texel] = invert ? 1 - block.partition[texel] : block.partition[texel];
376
- }
377
-
378
- source_subset[0] = invert ? 1 : 0;
379
- source_subset[1] = invert ? 0 : 1;
380
- } else if (mapping.via === "three") {
381
- const { bc7, permutation } = UASTC_TO_BC7_3[block.pattern];
382
-
383
- scratch.partition = bc7;
384
-
385
- const perm = SUBSET_PERMUTATIONS[permutation];
386
-
387
- for (let texel = 0; texel < 16; texel++) {
388
- subset_of_texel[texel] = perm[block.partition[texel]];
389
- }
390
-
391
- for (let s = 0; s < 3; s++) {
392
- source_subset[perm[s]] = s;
393
- }
394
- } else {
395
- const { bc7, fold } = UASTC_TO_BC7_2_VIA_3[block.pattern];
396
-
397
- scratch.partition = bc7;
398
-
399
- /*
400
- BC7's three subsets fold onto UASTC's two, so two of them share a source — and writing the
401
- same endpoints twice is exactly what makes a three-subset BC7 block describe two.
402
- */
403
- for (let s = 0; s < 3; s++) {
404
- source_subset[s] = bc7_fold_subset_3_to_2(s, fold);
405
- }
406
-
407
- for (let texel = 0; texel < 16; texel++) {
408
- // the BC7 pattern is the authority here; UASTC's is what it folds to
409
- subset_of_texel[texel] = bc7_subset_of(bc7, texel);
410
- }
411
- }
412
-
413
- /*
414
- **The parity is chosen over the channels that carry something.** It is shared by an endpoint's
415
- four, so choosing it by total error across all four lets a constant alpha outvote the colour —
416
- and on an RGB source that alpha is 255 by construction, invented by the colour endpoint mode
417
- rather than authored. Letting it vote costs 1.66 dB of colour on the photographic fixture, to
418
- perfect a channel nothing reads: meep declares a texture's alpha mode rather than inferring it
419
- from texels (§9.2), so an opaque source decoding to 254 changes no decision anywhere. The
420
- reference transcoder makes the same trade.
421
- */
422
- const channels = (spec.alpha_bits > 0 && block.has_alpha) ? 4 : 3;
423
-
424
- for (let s = 0; s < spec.subsets; s++) {
425
- quantise_subset(
426
- block.endpoints, source_subset[s] * 8,
427
- endpoints, s * 8, decoded, parity, spec.endpoint_parity_bits > 0 ? s * 2 : s,
428
- spec, channels
429
- );
430
- }
431
-
432
- for (let texel = 0; texel < 16; texel++) {
433
- indices[texel] = map_index(
434
- block.weight_indices[texel], block.weight_range, spec.index_bits
435
- );
436
- }
437
-
438
- anchors[0] = 0;
439
-
440
- if (spec.subsets === 2) {
441
- anchors[1] = BC7_ANCHOR_2[scratch.partition];
442
- } else {
443
- anchors[1] = BC7_ANCHOR_3_SECOND[scratch.partition];
444
- anchors[2] = BC7_ANCHOR_3_THIRD[scratch.partition];
445
- }
446
-
447
- const index_count = 1 << spec.index_bits;
448
-
449
- for (let s = 0; s < spec.subsets; s++) {
450
- if (indices[anchors[s]] >= index_count / 2) {
451
- flip_subset(
452
- endpoints, decoded, parity, s, spec, indices, subset_of_texel, index_count
453
- );
454
- }
455
- }
456
-
457
- bc7_write_block(out, offset, mapping.bc7, {
458
- partition: scratch.partition,
459
- endpoints,
460
- indices,
461
- anchors,
462
- parity,
463
- });
464
- }
465
-
466
- /**
467
- * Which BC7 three-subset subset a texel is in, for a given partition.
468
- *
469
- * @param {number} partition
470
- * @param {number} texel
471
- * @returns {number}
472
- */
473
- function bc7_subset_of(partition, texel) {
474
- return BC7_PARTITIONS_3[partition * 16 + texel];
475
- }
476
-
477
- /**
478
- * Carry a single-subset block into BC7 mode 6.
479
- *
480
- * @param {Uint8Array} out
481
- * @param {number} offset
482
- * @param {UastcBlock} block
483
- * @param {object} scratch
484
- */
485
- function write_single_subset(out, offset, block, scratch) {
486
- const spec = BC7_MODES[6];
487
-
488
- const { endpoints, decoded, parity, indices, anchors, subset_of_texel } = scratch;
489
-
490
- subset_of_texel.fill(0);
491
- quantise_subset(
492
- block.endpoints, 0, endpoints, 0, decoded, parity, 0, spec, block.has_alpha ? 4 : 3
493
- );
494
-
495
- for (let texel = 0; texel < 16; texel++) {
496
- indices[texel] = map_index(
497
- block.weight_indices[texel], block.weight_range, spec.index_bits
498
- );
499
- }
500
-
501
- anchors[0] = 0;
502
-
503
- const index_count = 1 << spec.index_bits;
504
-
505
- if (indices[0] >= index_count / 2) {
506
- flip_subset(endpoints, decoded, parity, 0, spec, indices, subset_of_texel, index_count);
507
- }
508
-
509
- bc7_write_block(out, offset, 6, { endpoints, indices, anchors, parity });
510
- }
511
-
512
- /**
513
- * Carry a dual plane block into BC7 mode 5.
514
- *
515
- * @param {Uint8Array} out
516
- * @param {number} offset
517
- * @param {UastcBlock} block
518
- * @param {object} scratch
519
- */
520
- function write_dual_plane(out, offset, block, scratch) {
521
- const spec = BC7_MODES[5];
522
-
523
- const {
524
- endpoints, decoded, parity, indices, secondary_indices, anchors, subset_of_texel,
525
- } = scratch;
526
-
527
- subset_of_texel.fill(0);
528
-
529
- const selector = block.component_selector;
530
-
531
- /*
532
- BC7 always interpolates *alpha* with the second index set, so the channel ASTC nominated is
533
- swapped into alpha's place — endpoints included — and the rotation tells the decoder to swap it
534
- back. This is the one place the two formats disagree about dual planes rather than merely
535
- spelling them differently.
536
- */
537
- const source = new Uint8Array(8);
538
-
539
- source.set(block.endpoints.subarray(0, 8));
540
-
541
- if (selector !== 3) {
542
- for (let end = 0; end < 2; end++) {
543
- const swap = source[end * 4 + selector];
544
-
545
- source[end * 4 + selector] = source[end * 4 + 3];
546
- source[end * 4 + 3] = swap;
547
- }
548
- }
549
-
550
- quantise_subset(source, 0, endpoints, 0, decoded, parity, 0, spec, 4);
551
-
552
- for (let texel = 0; texel < 16; texel++) {
553
- indices[texel] = map_index(
554
- block.weight_indices[texel * 2], block.weight_range, spec.index_bits
555
- );
556
-
557
- secondary_indices[texel] = map_index(
558
- block.weight_indices[texel * 2 + 1], block.weight_range, spec.secondary_index_bits
559
- );
560
- }
561
-
562
- anchors[0] = 0;
563
-
564
- const index_count = 1 << spec.index_bits;
565
-
566
- /*
567
- Mode 5's two index sets each have their own anchor, and flipping one must not disturb the other
568
- — the colour flip swaps only the three colour channels and the alpha flip only alpha.
569
- */
570
- if (indices[0] >= index_count / 2) {
571
- for (let c = 0; c < 3; c++) {
572
- const swap = endpoints[c];
573
-
574
- endpoints[c] = endpoints[4 + c];
575
- endpoints[4 + c] = swap;
576
- }
577
-
578
- for (let texel = 0; texel < 16; texel++) {
579
- indices[texel] = index_count - 1 - indices[texel];
580
- }
581
- }
582
-
583
- if (secondary_indices[0] >= index_count / 2) {
584
- const swap = endpoints[3];
585
-
586
- endpoints[3] = endpoints[7];
587
- endpoints[7] = swap;
588
-
589
- for (let texel = 0; texel < 16; texel++) {
590
- secondary_indices[texel] = index_count - 1 - secondary_indices[texel];
591
- }
592
- }
593
-
594
- bc7_write_block(out, offset, 5, {
595
- rotation: selector + 1,
596
- endpoints,
597
- indices,
598
- secondary_indices,
599
- anchors,
600
- parity,
601
- });
602
- }
1
+ import {
2
+ bc7_expand_endpoint,
3
+ bc7_quantise_endpoint,
4
+ bc7_write_block,
5
+ BC7_BLOCK_BYTES,
6
+ } from "../../bc/bc7_write_block.js";
7
+ import {
8
+ BC7_ANCHOR_2,
9
+ BC7_ANCHOR_3_SECOND,
10
+ BC7_ANCHOR_3_THIRD,
11
+ BC7_MODES,
12
+ BC7_PARTITIONS_2,
13
+ BC7_PARTITIONS_3,
14
+ } from "../../bc/bc7_tables.js";
15
+ import {
16
+ bc7_fold_subset_3_to_2,
17
+ SUBSET_PERMUTATIONS,
18
+ UASTC_TO_BC7_2,
19
+ UASTC_TO_BC7_2_VIA_3,
20
+ UASTC_TO_BC7_3,
21
+ } from "./uastc_bc7_tables.js";
22
+ import { uastc_decode_block, UASTC_BLOCK_BYTES } from "./uastc_decode_block.js";
23
+ import { UASTC_WEIGHT_VALUE } from "./uastc_tables.js";
24
+
25
+ /**
26
+ * BC7's interpolation weights, by index width. Two- and three-bit are ASTC's tables exactly, which
27
+ * is what lets most weights cross over untouched.
28
+ *
29
+ * @readonly
30
+ * @type {Object<number, Int32Array>}
31
+ */
32
+ const BC7_WEIGHTS = {
33
+ 2: new Int32Array([0, 21, 43, 64]),
34
+ 3: new Int32Array([0, 9, 18, 27, 37, 46, 55, 64]),
35
+ 4: new Int32Array([0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64]),
36
+ };
37
+
38
+ /**
39
+ * Which BC7 mode each UASTC mode is carried into, and how its partition maps.
40
+ *
41
+ * Taken from the specification, which names a BC7 mode against every UASTC mode. `via` says which
42
+ * of the three partition mappings applies — the odd one is mode 7, two subsets in UASTC and three in
43
+ * BC7.
44
+ *
45
+ * @readonly
46
+ * @type {Object<number, {bc7: number, via: string}>}
47
+ */
48
+ const MODE_MAP = {
49
+ 0: { bc7: 6, via: "single" },
50
+
51
+ /*
52
+ One subset, and the specification still pairs it with a *two*-subset BC7 mode — written as
53
+ partition zero with both subsets given the same endpoints. That is not a quirk: mode 1 carries
54
+ eight-bit endpoints and two-bit weights, and BC7 mode 3 is the only mode that holds eight-bit
55
+ endpoints without also having an alpha channel whose parity bit would have to be traded against
56
+ colour. Mode 6 would cost a bit of every channel to keep alpha at 255.
57
+ */
58
+ 1: { bc7: 3, via: "single_as_two" },
59
+ 2: { bc7: 1, via: "two" },
60
+ 3: { bc7: 2, via: "three" },
61
+ 4: { bc7: 3, via: "two" },
62
+ 5: { bc7: 6, via: "single" },
63
+ 6: { bc7: 5, via: "dual" },
64
+ 7: { bc7: 2, via: "two_via_three" },
65
+ 8: { bc7: 6, via: "single" },
66
+ 9: { bc7: 7, via: "two" },
67
+ 10: { bc7: 6, via: "single" },
68
+ 11: { bc7: 5, via: "dual" },
69
+ 12: { bc7: 6, via: "single" },
70
+ 13: { bc7: 5, via: "dual" },
71
+ 14: { bc7: 6, via: "single" },
72
+ 15: { bc7: 6, via: "single" },
73
+ 16: { bc7: 7, via: "two" },
74
+ 17: { bc7: 5, via: "dual" },
75
+ 18: { bc7: 6, via: "single" },
76
+ };
77
+
78
+ /**
79
+ * Transcode UASTC blocks to BC7.
80
+ *
81
+ * **Every UASTC mode is carried into the BC7 mode the specification pairs it with**, rather than
82
+ * decoded to texels and re-encoded. A UASTC block already *is* a partition, a pair of endpoints per
83
+ * subset and a weight per texel, which is exactly what BC7 stores — so the transcode is a change of
84
+ * representation and the only loss is endpoint precision. Re-fitting from texels instead costs
85
+ * around eighteen decibels on the dual plane modes and far more on the partitioned ones, because it
86
+ * throws away the answer the encoder already found.
87
+ *
88
+ * The pairings, all from the specification's mode table:
89
+ *
90
+ * | UASTC | BC7 | why |
91
+ * |---|---|---|
92
+ * | 0, 1, 5, 10, 12, 14, 15, 18 | 6 | one subset, one index set |
93
+ * | 8 | 6 | a solid colour, which BC7 has no mode for and mode 6 holds exactly |
94
+ * | 6, 11, 13, 17 | 5 | dual plane — one channel with its own weights, which BC7 spells as a rotation |
95
+ * | 2 | 1 | two subsets, three-bit weights |
96
+ * | 4 | 3 | two subsets, two-bit weights, seven-bit endpoints |
97
+ * | 9, 16 | 7 | two subsets with alpha |
98
+ * | 3 | 2 | three subsets |
99
+ * | 7 | 2 | two subsets in UASTC, three in BC7 — two of BC7's endpoint pairs are written alike |
100
+ *
101
+ * **The weights need no remapping.** ASTC's two- and three-bit weight tables are BC7's, and the
102
+ * four-bit ones agree too, so an index crosses over as itself. Where a UASTC mode's weight range is
103
+ * finer than the BC7 mode's index — only mode 18, at five bits — the nearest is taken.
104
+ *
105
+ * @param {Uint8Array} blocks UASTC blocks, tightly packed
106
+ * @param {number} block_count
107
+ * @returns {Uint8Array} tightly packed BC7 blocks, in raster order
108
+ *
109
+ * @author Alex Goldring
110
+ * @copyright Company Named Limited (c) 2026
111
+ */
112
+ export function uastc_to_bc7(blocks, block_count) {
113
+ const out = new Uint8Array(block_count * BC7_BLOCK_BYTES);
114
+
115
+ const scratch = {
116
+ endpoints: new Uint8Array(3 * 2 * 4),
117
+ decoded: new Uint8Array(3 * 2 * 4),
118
+ parity: new Int8Array(6),
119
+ indices: new Uint8Array(16),
120
+ secondary_indices: new Uint8Array(16),
121
+ anchors: new Uint8Array(3),
122
+ subset_of_texel: new Uint8Array(16),
123
+ source_subset: new Uint8Array(3),
124
+ };
125
+
126
+ for (let block = 0; block < block_count; block++) {
127
+ const decoded = uastc_decode_block(blocks, block * UASTC_BLOCK_BYTES);
128
+
129
+ const mapping = MODE_MAP[decoded.mode];
130
+
131
+ if (mapping.via === "dual") {
132
+ write_dual_plane(out, block * BC7_BLOCK_BYTES, decoded, scratch);
133
+ } else if (mapping.via === "single") {
134
+ write_single_subset(out, block * BC7_BLOCK_BYTES, decoded, scratch);
135
+ } else {
136
+ write_partitioned(out, block * BC7_BLOCK_BYTES, decoded, mapping, scratch);
137
+ }
138
+ }
139
+
140
+ return out;
141
+ }
142
+
143
+ /**
144
+ * Map a UASTC weight index into a BC7 index of the given width.
145
+ *
146
+ * @param {number} index
147
+ * @param {number} weight_range
148
+ * @param {number} bits
149
+ * @returns {number}
150
+ */
151
+ function map_index(index, weight_range, bits) {
152
+ const from = UASTC_WEIGHT_VALUE[weight_range];
153
+ const to = BC7_WEIGHTS[bits];
154
+
155
+ const weight = from[index];
156
+
157
+ let best = 0;
158
+ let best_error = Infinity;
159
+
160
+ for (let i = 0; i < to.length; i++) {
161
+ const error = Math.abs(to[i] - weight);
162
+
163
+ if (error < best_error) {
164
+ best_error = error;
165
+ best = i;
166
+ }
167
+ }
168
+
169
+ return best;
170
+ }
171
+
172
+ /**
173
+ * Quantise one subset's two endpoints, choosing the parity that suits them best.
174
+ *
175
+ * A parity bit is shared — by an endpoint's four channels, or by a subset's two endpoints — so it
176
+ * cannot be fitted per channel. Both values are tried and the one with less total error kept, which
177
+ * is what the specification means by an "optimal p-bit".
178
+ *
179
+ * @param {Uint8Array} source eight bytes, low then high RGBA
180
+ * @param {number} source_offset
181
+ * @param {Uint8Array} out stored fields, written
182
+ * @param {number} out_offset
183
+ * @param {Uint8Array} decoded what those fields decode to, written
184
+ * @param {Int8Array} parity written
185
+ * @param {number} parity_offset
186
+ * @param {Bc7Mode} spec
187
+ * @param {number} channels how many carry data — three where the mode has no alpha
188
+ */
189
+ function quantise_subset(
190
+ source, source_offset, out, out_offset, decoded, parity, parity_offset, spec, channels
191
+ ) {
192
+ const per_endpoint = spec.endpoint_parity_bits > 0;
193
+ const shared = spec.shared_parity_bits > 0;
194
+
195
+ const color_width = spec.color_bits + (per_endpoint || shared ? 1 : 0);
196
+ const alpha_width = spec.alpha_bits + (per_endpoint || shared ? 1 : 0);
197
+
198
+ /**
199
+ * @param {number[]} candidate one parity per endpoint
200
+ * @returns {number} total squared error
201
+ */
202
+ const error_of = candidate => {
203
+ let total = 0;
204
+
205
+ for (let end = 0; end < 2; end++) {
206
+ for (let c = 0; c < channels; c++) {
207
+ const width = c === 3 ? alpha_width : color_width;
208
+
209
+ const value = source[source_offset + end * 4 + c];
210
+
211
+ const stored = bc7_quantise_endpoint(value, width, candidate[end]);
212
+
213
+ const delta = bc7_expand_endpoint(stored, width) - value;
214
+
215
+ total += delta * delta;
216
+ }
217
+ }
218
+
219
+ return total;
220
+ };
221
+
222
+ let chosen = [-1, -1];
223
+
224
+ if (per_endpoint) {
225
+ // one bit each, so the two endpoints are independent and each takes its own better value
226
+ for (let end = 0; end < 2; end++) {
227
+ chosen[end] = error_of_end(source, source_offset, end, 0, spec, channels)
228
+ <= error_of_end(source, source_offset, end, 1, spec, channels) ? 0 : 1;
229
+ }
230
+ } else if (shared) {
231
+ // one bit for the whole subset, so both endpoints have to agree
232
+ chosen = error_of([0, 0]) <= error_of([1, 1]) ? [0, 0] : [1, 1];
233
+ }
234
+
235
+ for (let end = 0; end < 2; end++) {
236
+ for (let c = 0; c < 4; c++) {
237
+ const width = c === 3 ? alpha_width : color_width;
238
+
239
+ if (c === 3 && spec.alpha_bits === 0) {
240
+ out[out_offset + end * 4 + c] = 0;
241
+ decoded[out_offset + end * 4 + c] = 255;
242
+
243
+ continue;
244
+ }
245
+
246
+ const value = source[source_offset + end * 4 + c];
247
+
248
+ const stored = bc7_quantise_endpoint(value, width, chosen[end]);
249
+
250
+ decoded[out_offset + end * 4 + c] = bc7_expand_endpoint(stored, width);
251
+
252
+ // the parity bit is carried separately, so the stored field is the rest
253
+ out[out_offset + end * 4 + c] = (per_endpoint || shared) ? stored >> 1 : stored;
254
+ }
255
+ }
256
+
257
+ if (per_endpoint) {
258
+ parity[parity_offset] = chosen[0];
259
+ parity[parity_offset + 1] = chosen[1];
260
+ } else if (shared) {
261
+ parity[parity_offset] = chosen[0];
262
+ }
263
+ }
264
+
265
+ /**
266
+ * Total squared error of one endpoint under a candidate parity.
267
+ *
268
+ * @param {Uint8Array} source
269
+ * @param {number} source_offset
270
+ * @param {number} end
271
+ * @param {number} parity
272
+ * @param {Bc7Mode} spec
273
+ * @param {number} channels
274
+ * @returns {number}
275
+ */
276
+ function error_of_end(source, source_offset, end, parity, spec, channels) {
277
+ const color_width = spec.color_bits + 1;
278
+ const alpha_width = spec.alpha_bits + 1;
279
+
280
+ let total = 0;
281
+
282
+ for (let c = 0; c < channels; c++) {
283
+ const width = c === 3 ? alpha_width : color_width;
284
+
285
+ const value = source[source_offset + end * 4 + c];
286
+
287
+ const delta = bc7_expand_endpoint(bc7_quantise_endpoint(value, width, parity), width)
288
+ - value;
289
+
290
+ total += delta * delta;
291
+ }
292
+
293
+ return total;
294
+ }
295
+
296
+ /**
297
+ * Flip a subset's endpoints and invert its indices, so its anchor index fits the bit it is given.
298
+ *
299
+ * @param {Uint8Array} endpoints stored fields
300
+ * @param {Uint8Array} decoded
301
+ * @param {Int8Array} parity
302
+ * @param {number} subset
303
+ * @param {Bc7Mode} spec
304
+ * @param {Uint8Array} indices
305
+ * @param {Uint8Array} subset_of_texel
306
+ * @param {number} index_count
307
+ */
308
+ function flip_subset(
309
+ endpoints, decoded, parity, subset, spec, indices, subset_of_texel, index_count
310
+ ) {
311
+ for (let c = 0; c < 4; c++) {
312
+ const low = (subset * 2) * 4 + c;
313
+ const high = (subset * 2 + 1) * 4 + c;
314
+
315
+ const swap = endpoints[low];
316
+
317
+ endpoints[low] = endpoints[high];
318
+ endpoints[high] = swap;
319
+
320
+ const swap_decoded = decoded[low];
321
+
322
+ decoded[low] = decoded[high];
323
+ decoded[high] = swap_decoded;
324
+ }
325
+
326
+ if (spec.endpoint_parity_bits > 0) {
327
+ const swap = parity[subset * 2];
328
+
329
+ parity[subset * 2] = parity[subset * 2 + 1];
330
+ parity[subset * 2 + 1] = swap;
331
+ }
332
+
333
+ for (let texel = 0; texel < 16; texel++) {
334
+ if (subset_of_texel[texel] === subset) {
335
+ indices[texel] = index_count - 1 - indices[texel];
336
+ }
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Carry a partitioned block — two or three subsets — into its BC7 counterpart.
342
+ *
343
+ * @param {Uint8Array} out
344
+ * @param {number} offset
345
+ * @param {UastcBlock} block
346
+ * @param {{bc7: number, via: string}} mapping
347
+ * @param {object} scratch
348
+ */
349
+ function write_partitioned(out, offset, block, mapping, scratch) {
350
+ const spec = BC7_MODES[mapping.bc7];
351
+
352
+ const { endpoints, decoded, parity, indices, anchors, subset_of_texel, source_subset } = scratch;
353
+
354
+ /*
355
+ Which BC7 subset each texel lands in, and which UASTC subset each BC7 subset draws its endpoints
356
+ from. The two questions have different answers for mode 7, where BC7 has a subset UASTC does not
357
+ — two of its three endpoint pairs end up identical.
358
+ */
359
+ if (mapping.via === "single_as_two") {
360
+ /*
361
+ One UASTC subset written as two identical BC7 ones. Partition zero divides the block, and
362
+ giving both halves the same endpoints makes that division invisible — which is how a
363
+ one-subset block reaches a mode that has no one-subset form.
364
+ */
365
+ scratch.partition = 0;
366
+
367
+ subset_of_texel.set(BC7_PARTITIONS_2.subarray(0, 16));
368
+
369
+ source_subset[0] = 0;
370
+ source_subset[1] = 0;
371
+ } else if (mapping.via === "two") {
372
+ const { bc7, invert } = UASTC_TO_BC7_2[block.pattern];
373
+
374
+ scratch.partition = bc7;
375
+
376
+ for (let texel = 0; texel < 16; texel++) {
377
+ subset_of_texel[texel] = invert ? 1 - block.partition[texel] : block.partition[texel];
378
+ }
379
+
380
+ source_subset[0] = invert ? 1 : 0;
381
+ source_subset[1] = invert ? 0 : 1;
382
+ } else if (mapping.via === "three") {
383
+ const { bc7, permutation } = UASTC_TO_BC7_3[block.pattern];
384
+
385
+ scratch.partition = bc7;
386
+
387
+ const perm = SUBSET_PERMUTATIONS[permutation];
388
+
389
+ for (let texel = 0; texel < 16; texel++) {
390
+ subset_of_texel[texel] = perm[block.partition[texel]];
391
+ }
392
+
393
+ for (let s = 0; s < 3; s++) {
394
+ source_subset[perm[s]] = s;
395
+ }
396
+ } else {
397
+ const { bc7, fold } = UASTC_TO_BC7_2_VIA_3[block.pattern];
398
+
399
+ scratch.partition = bc7;
400
+
401
+ /*
402
+ BC7's three subsets fold onto UASTC's two, so two of them share a source — and writing the
403
+ same endpoints twice is exactly what makes a three-subset BC7 block describe two.
404
+ */
405
+ for (let s = 0; s < 3; s++) {
406
+ source_subset[s] = bc7_fold_subset_3_to_2(s, fold);
407
+ }
408
+
409
+ for (let texel = 0; texel < 16; texel++) {
410
+ // the BC7 pattern is the authority here; UASTC's is what it folds to
411
+ subset_of_texel[texel] = bc7_subset_of(bc7, texel);
412
+ }
413
+ }
414
+
415
+ /*
416
+ **The parity is chosen over the channels that carry something.** It is shared by an endpoint's
417
+ four, so choosing it by total error across all four lets a constant alpha outvote the colour —
418
+ and on an RGB source that alpha is 255 by construction, invented by the colour endpoint mode
419
+ rather than authored. Letting it vote costs 1.66 dB of colour on the photographic fixture, to
420
+ perfect a channel nothing reads: meep declares a texture's alpha mode rather than inferring it
421
+ from texels (§9.2), so an opaque source decoding to 254 changes no decision anywhere. The
422
+ reference transcoder makes the same trade.
423
+ */
424
+ const channels = (spec.alpha_bits > 0 && block.has_alpha) ? 4 : 3;
425
+
426
+ for (let s = 0; s < spec.subsets; s++) {
427
+ quantise_subset(
428
+ block.endpoints, source_subset[s] * 8,
429
+ endpoints, s * 8, decoded, parity, spec.endpoint_parity_bits > 0 ? s * 2 : s,
430
+ spec, channels
431
+ );
432
+ }
433
+
434
+ for (let texel = 0; texel < 16; texel++) {
435
+ indices[texel] = map_index(
436
+ block.weight_indices[texel], block.weight_range, spec.index_bits
437
+ );
438
+ }
439
+
440
+ anchors[0] = 0;
441
+
442
+ if (spec.subsets === 2) {
443
+ anchors[1] = BC7_ANCHOR_2[scratch.partition];
444
+ } else {
445
+ anchors[1] = BC7_ANCHOR_3_SECOND[scratch.partition];
446
+ anchors[2] = BC7_ANCHOR_3_THIRD[scratch.partition];
447
+ }
448
+
449
+ const index_count = 1 << spec.index_bits;
450
+
451
+ for (let s = 0; s < spec.subsets; s++) {
452
+ if (indices[anchors[s]] >= index_count / 2) {
453
+ flip_subset(
454
+ endpoints, decoded, parity, s, spec, indices, subset_of_texel, index_count
455
+ );
456
+ }
457
+ }
458
+
459
+ bc7_write_block(out, offset, mapping.bc7, {
460
+ partition: scratch.partition,
461
+ endpoints,
462
+ indices,
463
+ anchors,
464
+ parity,
465
+ });
466
+ }
467
+
468
+ /**
469
+ * Which BC7 three-subset subset a texel is in, for a given partition.
470
+ *
471
+ * @param {number} partition
472
+ * @param {number} texel
473
+ * @returns {number}
474
+ */
475
+ function bc7_subset_of(partition, texel) {
476
+ return BC7_PARTITIONS_3[partition * 16 + texel];
477
+ }
478
+
479
+ /**
480
+ * Carry a single-subset block into BC7 mode 6.
481
+ *
482
+ * @param {Uint8Array} out
483
+ * @param {number} offset
484
+ * @param {UastcBlock} block
485
+ * @param {object} scratch
486
+ */
487
+ function write_single_subset(out, offset, block, scratch) {
488
+ const spec = BC7_MODES[6];
489
+
490
+ const { endpoints, decoded, parity, indices, anchors, subset_of_texel } = scratch;
491
+
492
+ subset_of_texel.fill(0);
493
+ quantise_subset(
494
+ block.endpoints, 0, endpoints, 0, decoded, parity, 0, spec, block.has_alpha ? 4 : 3
495
+ );
496
+
497
+ for (let texel = 0; texel < 16; texel++) {
498
+ indices[texel] = map_index(
499
+ block.weight_indices[texel], block.weight_range, spec.index_bits
500
+ );
501
+ }
502
+
503
+ anchors[0] = 0;
504
+
505
+ const index_count = 1 << spec.index_bits;
506
+
507
+ if (indices[0] >= index_count / 2) {
508
+ flip_subset(endpoints, decoded, parity, 0, spec, indices, subset_of_texel, index_count);
509
+ }
510
+
511
+ bc7_write_block(out, offset, 6, { endpoints, indices, anchors, parity });
512
+ }
513
+
514
+ /**
515
+ * Carry a dual plane block into BC7 mode 5.
516
+ *
517
+ * @param {Uint8Array} out
518
+ * @param {number} offset
519
+ * @param {UastcBlock} block
520
+ * @param {object} scratch
521
+ */
522
+ function write_dual_plane(out, offset, block, scratch) {
523
+ const spec = BC7_MODES[5];
524
+
525
+ const {
526
+ endpoints, decoded, parity, indices, secondary_indices, anchors, subset_of_texel,
527
+ } = scratch;
528
+
529
+ subset_of_texel.fill(0);
530
+
531
+ const selector = block.component_selector;
532
+
533
+ /*
534
+ BC7 always interpolates *alpha* with the second index set, so the channel ASTC nominated is
535
+ swapped into alpha's place — endpoints included — and the rotation tells the decoder to swap it
536
+ back. This is the one place the two formats disagree about dual planes rather than merely
537
+ spelling them differently.
538
+ */
539
+ const source = new Uint8Array(8);
540
+
541
+ source.set(block.endpoints.subarray(0, 8));
542
+
543
+ if (selector !== 3) {
544
+ for (let end = 0; end < 2; end++) {
545
+ const swap = source[end * 4 + selector];
546
+
547
+ source[end * 4 + selector] = source[end * 4 + 3];
548
+ source[end * 4 + 3] = swap;
549
+ }
550
+ }
551
+
552
+ quantise_subset(source, 0, endpoints, 0, decoded, parity, 0, spec, 4);
553
+
554
+ for (let texel = 0; texel < 16; texel++) {
555
+ indices[texel] = map_index(
556
+ block.weight_indices[texel * 2], block.weight_range, spec.index_bits
557
+ );
558
+
559
+ secondary_indices[texel] = map_index(
560
+ block.weight_indices[texel * 2 + 1], block.weight_range, spec.secondary_index_bits
561
+ );
562
+ }
563
+
564
+ anchors[0] = 0;
565
+
566
+ const index_count = 1 << spec.index_bits;
567
+
568
+ /*
569
+ Mode 5's two index sets each have their own anchor, and flipping one must not disturb the other
570
+ — the colour flip swaps only the three colour channels and the alpha flip only alpha.
571
+ */
572
+ if (indices[0] >= index_count / 2) {
573
+ for (let c = 0; c < 3; c++) {
574
+ const swap = endpoints[c];
575
+
576
+ endpoints[c] = endpoints[4 + c];
577
+ endpoints[4 + c] = swap;
578
+ }
579
+
580
+ for (let texel = 0; texel < 16; texel++) {
581
+ indices[texel] = index_count - 1 - indices[texel];
582
+ }
583
+ }
584
+
585
+ if (secondary_indices[0] >= index_count / 2) {
586
+ const swap = endpoints[3];
587
+
588
+ endpoints[3] = endpoints[7];
589
+ endpoints[7] = swap;
590
+
591
+ for (let texel = 0; texel < 16; texel++) {
592
+ secondary_indices[texel] = index_count - 1 - secondary_indices[texel];
593
+ }
594
+ }
595
+
596
+ bc7_write_block(out, offset, 5, {
597
+ rotation: selector + 1,
598
+ endpoints,
599
+ indices,
600
+ secondary_indices,
601
+ anchors,
602
+ parity,
603
+ });
604
+ }