basisu_transcoder_uastc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // basisu_transcoder_uastc.h
  2. #pragma once
  3. #include "basisu_transcoder_internal.h"
  4. namespace basist
  5. {
  6. struct color_quad_u8
  7. {
  8. uint8_t m_c[4];
  9. };
  10. const uint32_t TOTAL_UASTC_MODES = 19;
  11. const uint32_t UASTC_MODE_INDEX_SOLID_COLOR = 8;
  12. const uint32_t TOTAL_ASTC_BC7_COMMON_PARTITIONS2 = 30;
  13. const uint32_t TOTAL_ASTC_BC7_COMMON_PARTITIONS3 = 11;
  14. const uint32_t TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS = 19;
  15. extern const uint8_t g_uastc_mode_weight_bits[TOTAL_UASTC_MODES];
  16. extern const uint8_t g_uastc_mode_weight_ranges[TOTAL_UASTC_MODES];
  17. extern const uint8_t g_uastc_mode_endpoint_ranges[TOTAL_UASTC_MODES];
  18. extern const uint8_t g_uastc_mode_subsets[TOTAL_UASTC_MODES];
  19. extern const uint8_t g_uastc_mode_planes[TOTAL_UASTC_MODES];
  20. extern const uint8_t g_uastc_mode_comps[TOTAL_UASTC_MODES];
  21. extern const uint8_t g_uastc_mode_has_etc1_bias[TOTAL_UASTC_MODES];
  22. extern const uint8_t g_uastc_mode_has_bc1_hint0[TOTAL_UASTC_MODES];
  23. extern const uint8_t g_uastc_mode_has_bc1_hint1[TOTAL_UASTC_MODES];
  24. extern const uint8_t g_uastc_mode_has_alpha[TOTAL_UASTC_MODES];
  25. extern const uint8_t g_uastc_mode_is_la[TOTAL_UASTC_MODES];
  26. struct astc_bc7_common_partition2_desc
  27. {
  28. uint8_t m_bc7;
  29. uint16_t m_astc;
  30. bool m_invert;
  31. };
  32. extern const astc_bc7_common_partition2_desc g_astc_bc7_common_partitions2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2];
  33. struct bc73_astc2_common_partition_desc
  34. {
  35. uint8_t m_bc73;
  36. uint16_t m_astc2;
  37. uint8_t k; // 0-5 - how to modify the BC7 3-subset pattern to match the ASTC pattern (LSB=invert)
  38. };
  39. extern const bc73_astc2_common_partition_desc g_bc7_3_astc2_common_partitions[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS];
  40. struct astc_bc7_common_partition3_desc
  41. {
  42. uint8_t m_bc7;
  43. uint16_t m_astc;
  44. uint8_t m_astc_to_bc7_perm; // converts ASTC to BC7 partition using g_astc_bc7_partition_index_perm_tables[][]
  45. };
  46. extern const astc_bc7_common_partition3_desc g_astc_bc7_common_partitions3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3];
  47. extern const uint8_t g_astc_bc7_patterns2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][16];
  48. extern const uint8_t g_astc_bc7_patterns3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][16];
  49. extern const uint8_t g_bc7_3_astc2_patterns2[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][16];
  50. extern const uint8_t g_astc_bc7_pattern2_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][3];
  51. extern const uint8_t g_astc_bc7_pattern3_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][3];
  52. extern const uint8_t g_bc7_3_astc2_patterns2_anchors[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][3];
  53. extern const uint32_t g_uastc_mode_huff_codes[TOTAL_UASTC_MODES + 1][2];
  54. extern const uint8_t g_astc_to_bc7_partition_index_perm_tables[6][3];
  55. extern const uint8_t g_bc7_to_astc_partition_index_perm_tables[6][3]; // inverse of g_astc_to_bc7_partition_index_perm_tables
  56. extern const uint8_t* s_uastc_to_bc1_weights[6];
  57. uint32_t bc7_convert_partition_index_3_to_2(uint32_t p, uint32_t k);
  58. inline uint32_t astc_interpolate(uint32_t l, uint32_t h, uint32_t w, bool srgb)
  59. {
  60. if (srgb)
  61. {
  62. l = (l << 8) | 0x80;
  63. h = (h << 8) | 0x80;
  64. }
  65. else
  66. {
  67. l = (l << 8) | l;
  68. h = (h << 8) | h;
  69. }
  70. uint32_t k = (l * (64 - w) + h * w + 32) >> 6;
  71. return k >> 8;
  72. }
  73. struct astc_block_desc
  74. {
  75. int m_weight_range; // weight BISE range
  76. int m_subsets; // number of ASTC partitions
  77. int m_partition_seed; // partition pattern seed
  78. int m_cem; // color endpoint mode used by all subsets
  79. int m_ccs; // color component selector (dual plane only)
  80. bool m_dual_plane; // true if dual plane
  81. // Weight and endpoint BISE values.
  82. // Note these values are NOT linear, they must be BISE encoded. See Table 97 and Table 107.
  83. uint8_t m_endpoints[18]; // endpoint values, in RR GG BB etc. order
  84. uint8_t m_weights[64]; // weight index values, raster order, in P0 P1, P0 P1, etc. or P0, P0, P0, P0, etc. order
  85. };
  86. const uint32_t BC7ENC_TOTAL_ASTC_RANGES = 21;
  87. // See tables 81, 93, 18.13.Endpoint Unquantization
  88. const uint32_t TOTAL_ASTC_RANGES = 21;
  89. extern const int g_astc_bise_range_table[TOTAL_ASTC_RANGES][3];
  90. struct astc_quant_bin
  91. {
  92. uint8_t m_unquant; // unquantized value
  93. uint8_t m_index; // sorted index
  94. };
  95. extern astc_quant_bin g_astc_unquant[BC7ENC_TOTAL_ASTC_RANGES][256]; // [ASTC encoded endpoint index]
  96. int astc_get_levels(int range);
  97. bool astc_is_valid_endpoint_range(uint32_t range);
  98. uint32_t unquant_astc_endpoint(uint32_t packed_bits, uint32_t packed_trits, uint32_t packed_quints, uint32_t range);
  99. uint32_t unquant_astc_endpoint_val(uint32_t packed_val, uint32_t range);
  100. const uint8_t* get_anchor_indices(uint32_t subsets, uint32_t mode, uint32_t common_pattern, const uint8_t*& pPartition_pattern);
  101. // BC7
  102. const uint32_t BC7ENC_BLOCK_SIZE = 16;
  103. struct bc7_block
  104. {
  105. uint64_t m_qwords[2];
  106. };
  107. struct bc7_optimization_results
  108. {
  109. uint32_t m_mode;
  110. uint32_t m_partition;
  111. uint8_t m_selectors[16];
  112. uint8_t m_alpha_selectors[16];
  113. color_quad_u8 m_low[3];
  114. color_quad_u8 m_high[3];
  115. uint32_t m_pbits[3][2];
  116. uint32_t m_index_selector;
  117. uint32_t m_rotation;
  118. };
  119. extern const uint32_t g_bc7_weights1[2];
  120. extern const uint32_t g_bc7_weights2[4];
  121. extern const uint32_t g_bc7_weights3[8];
  122. extern const uint32_t g_bc7_weights4[16];
  123. extern const uint32_t g_astc_weights4[16];
  124. extern const uint32_t g_astc_weights5[32];
  125. extern const uint32_t g_astc_weights_3levels[3];
  126. extern const uint8_t g_bc7_partition1[16];
  127. extern const uint8_t g_bc7_partition2[64 * 16];
  128. extern const uint8_t g_bc7_partition3[64 * 16];
  129. extern const uint8_t g_bc7_table_anchor_index_second_subset[64];
  130. extern const uint8_t g_bc7_table_anchor_index_third_subset_1[64];
  131. extern const uint8_t g_bc7_table_anchor_index_third_subset_2[64];
  132. extern const uint8_t g_bc7_num_subsets[8];
  133. extern const uint8_t g_bc7_partition_bits[8];
  134. extern const uint8_t g_bc7_color_index_bitcount[8];
  135. extern const uint8_t g_bc7_mode_has_p_bits[8];
  136. extern const uint8_t g_bc7_mode_has_shared_p_bits[8];
  137. extern const uint8_t g_bc7_color_precision_table[8];
  138. extern const int8_t g_bc7_alpha_precision_table[8];
  139. extern const uint8_t g_bc7_alpha_index_bitcount[8];
  140. inline bool get_bc7_mode_has_seperate_alpha_selectors(int mode) { return (mode == 4) || (mode == 5); }
  141. inline int get_bc7_color_index_size(int mode, int index_selection_bit) { return g_bc7_color_index_bitcount[mode] + index_selection_bit; }
  142. inline int get_bc7_alpha_index_size(int mode, int index_selection_bit) { return g_bc7_alpha_index_bitcount[mode] - index_selection_bit; }
  143. struct endpoint_err
  144. {
  145. uint16_t m_error; uint8_t m_lo; uint8_t m_hi;
  146. };
  147. extern endpoint_err g_bc7_mode_6_optimal_endpoints[256][2]; // [c][pbit]
  148. const uint32_t BC7ENC_MODE_6_OPTIMAL_INDEX = 5;
  149. extern endpoint_err g_bc7_mode_5_optimal_endpoints[256]; // [c]
  150. const uint32_t BC7ENC_MODE_5_OPTIMAL_INDEX = 1;
  151. // Packs a BC7 block from a high-level description. Handles all BC7 modes.
  152. void encode_bc7_block(void* pBlock, const bc7_optimization_results* pResults);
  153. // Packs an ASTC block
  154. // Constraints: Always 4x4, all subset CEM's must be equal, only tested with LDR CEM's.
  155. bool pack_astc_block(uint32_t* pDst, const astc_block_desc* pBlock, uint32_t mode);
  156. void pack_astc_solid_block(void* pDst_block, const color32& color);
  157. #ifdef _DEBUG
  158. int astc_compute_texel_partition(int seed, int x, int y, int z, int partitioncount, bool small_block);
  159. #endif
  160. struct uastc_block
  161. {
  162. union
  163. {
  164. uint8_t m_bytes[16];
  165. uint32_t m_dwords[4];
  166. #ifndef __EMSCRIPTEN__
  167. uint64_t m_qwords[2];
  168. #endif
  169. };
  170. };
  171. struct unpacked_uastc_block
  172. {
  173. astc_block_desc m_astc;
  174. uint32_t m_mode;
  175. uint32_t m_common_pattern;
  176. color32 m_solid_color;
  177. bool m_bc1_hint0;
  178. bool m_bc1_hint1;
  179. bool m_etc1_flip;
  180. bool m_etc1_diff;
  181. uint32_t m_etc1_inten0;
  182. uint32_t m_etc1_inten1;
  183. uint32_t m_etc1_bias;
  184. uint32_t m_etc2_hints;
  185. uint32_t m_etc1_selector;
  186. uint32_t m_etc1_r, m_etc1_g, m_etc1_b;
  187. };
  188. color32 apply_etc1_bias(const color32 &block_color, uint32_t bias, uint32_t limit, uint32_t subblock);
  189. struct decoder_etc_block;
  190. struct eac_block;
  191. bool unpack_uastc(uint32_t mode, uint32_t common_pattern, const color32& solid_color, const astc_block_desc& astc, color32* pPixels, bool srgb);
  192. bool unpack_uastc(const unpacked_uastc_block& unpacked_blk, color32* pPixels, bool srgb);
  193. bool unpack_uastc(const uastc_block& blk, color32* pPixels, bool srgb);
  194. bool unpack_uastc(const uastc_block& blk, unpacked_uastc_block& unpacked, bool undo_blue_contract, bool read_hints = true);
  195. bool transcode_uastc_to_astc(const uastc_block& src_blk, void* pDst);
  196. bool transcode_uastc_to_bc7(const unpacked_uastc_block& unpacked_src_blk, bc7_optimization_results& dst_blk);
  197. bool transcode_uastc_to_bc7(const uastc_block& src_blk, bc7_optimization_results& dst_blk);
  198. bool transcode_uastc_to_bc7(const uastc_block& src_blk, void* pDst);
  199. void transcode_uastc_to_etc1(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst);
  200. bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst);
  201. bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst, uint32_t channel);
  202. void transcode_uastc_to_etc2_eac_a8(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst);
  203. bool transcode_uastc_to_etc2_rgba(const uastc_block& src_blk, void* pDst);
  204. // Packs 16 scalar values to BC4. Same PSNR as stb_dxt's BC4 encoder, around 13% faster.
  205. void encode_bc4(void* pDst, const uint8_t* pPixels, uint32_t stride);
  206. void encode_bc1_solid_block(void* pDst, uint32_t fr, uint32_t fg, uint32_t fb);
  207. enum
  208. {
  209. cEncodeBC1HighQuality = 1,
  210. cEncodeBC1HigherQuality = 2,
  211. cEncodeBC1UseSelectors = 4,
  212. };
  213. void encode_bc1(void* pDst, const uint8_t* pPixels, uint32_t flags);
  214. // Alternate PCA-free encoder, around 15% faster, same (or slightly higher) avg. PSNR
  215. void encode_bc1_alt(void* pDst, const uint8_t* pPixels, uint32_t flags);
  216. void transcode_uastc_to_bc1_hint0(const unpacked_uastc_block& unpacked_src_blk, void* pDst);
  217. void transcode_uastc_to_bc1_hint1(const unpacked_uastc_block& unpacked_src_blk, const color32 block_pixels[4][4], void* pDst, bool high_quality);
  218. bool transcode_uastc_to_bc1(const uastc_block& src_blk, void* pDst, bool high_quality);
  219. bool transcode_uastc_to_bc3(const uastc_block& src_blk, void* pDst, bool high_quality);
  220. bool transcode_uastc_to_bc4(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0);
  221. bool transcode_uastc_to_bc5(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1);
  222. bool transcode_uastc_to_etc2_eac_r11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0);
  223. bool transcode_uastc_to_etc2_eac_rg11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1);
  224. bool transcode_uastc_to_pvrtc1_4_rgb(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality, bool from_alpha);
  225. bool transcode_uastc_to_pvrtc1_4_rgba(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality);
  226. // uastc_init() MUST be called before using this module.
  227. void uastc_init();
  228. } // namespace basist