basisu_transcoder_internal.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. // basisu_transcoder_internal.h - Universal texture format transcoder library.
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #pragma once
  18. #ifdef _MSC_VER
  19. #pragma warning (disable: 4127) // conditional expression is constant
  20. #endif
  21. #define BASISD_LIB_VERSION 107
  22. #define BASISD_VERSION_STRING "01.11"
  23. #ifdef _DEBUG
  24. #define BASISD_BUILD_DEBUG
  25. #else
  26. #define BASISD_BUILD_RELEASE
  27. #endif
  28. #include "basisu.h"
  29. #define BASISD_znew (z = 36969 * (z & 65535) + (z >> 16))
  30. namespace basisu
  31. {
  32. extern bool g_debug_printf;
  33. }
  34. namespace basist
  35. {
  36. // Low-level formats directly supported by the transcoder (other supported texture formats are combinations of these low-level block formats).
  37. // You probably don't care about these enum's unless you are going pretty low-level and calling the transcoder to decode individual slices.
  38. enum class block_format
  39. {
  40. cETC1, // ETC1S RGB
  41. cBC1, // DXT1 RGB
  42. cBC4, // DXT5A (alpha block only)
  43. cPVRTC1_4_RGB, // opaque-only PVRTC1 4bpp
  44. cPVRTC1_4_RGBA, // PVRTC1 4bpp RGBA
  45. cBC7_M6_OPAQUE_ONLY, // RGB BC7 mode 6
  46. cBC7_M5_COLOR, // RGB BC7 mode 5 color (writes an opaque mode 5 block)
  47. cBC7_M5_ALPHA, // alpha portion of BC7 mode 5 (cBC7_M5_COLOR output data must have been written to the output buffer first to set the mode/rot fields etc.)
  48. cETC2_EAC_A8, // alpha block of ETC2 EAC (first 8 bytes of the 16-bit ETC2 EAC RGBA format)
  49. cASTC_4x4, // ASTC 4x4 (either color-only or color+alpha). Note that the transcoder always currently assumes sRGB is not enabled when outputting ASTC
  50. // data. If you use a sRGB ASTC format you'll get ~1 LSB of additional error, because of the different way ASTC decoders scale 8-bit endpoints to 16-bits during unpacking.
  51. cATC_RGB,
  52. cATC_RGBA_INTERPOLATED_ALPHA,
  53. cFXT1_RGB, // Opaque-only, has oddball 8x4 pixel block size
  54. cIndices, // Used internally: Write 16-bit endpoint and selector indices directly to output (output block must be at least 32-bits)
  55. cRGB32, // Writes RGB components to 32bpp output pixels
  56. cRGBA32, // Writes RGB255 components to 32bpp output pixels
  57. cA32, // Writes alpha component to 32bpp output pixels
  58. cRGB565,
  59. cBGR565,
  60. cRGBA4444_COLOR,
  61. cRGBA4444_ALPHA,
  62. cRGBA4444_COLOR_OPAQUE,
  63. cPVRTC2_4_RGB,
  64. cPVRTC2_4_RGBA,
  65. cETC2_EAC_R11,
  66. cTotalBlockFormats
  67. };
  68. const int COLOR5_PAL0_PREV_HI = 9, COLOR5_PAL0_DELTA_LO = -9, COLOR5_PAL0_DELTA_HI = 31;
  69. const int COLOR5_PAL1_PREV_HI = 21, COLOR5_PAL1_DELTA_LO = -21, COLOR5_PAL1_DELTA_HI = 21;
  70. const int COLOR5_PAL2_PREV_HI = 31, COLOR5_PAL2_DELTA_LO = -31, COLOR5_PAL2_DELTA_HI = 9;
  71. const int COLOR5_PAL_MIN_DELTA_B_RUNLEN = 3, COLOR5_PAL_DELTA_5_RUNLEN_VLC_BITS = 3;
  72. const uint32_t ENDPOINT_PRED_TOTAL_SYMBOLS = (4 * 4 * 4 * 4) + 1;
  73. const uint32_t ENDPOINT_PRED_REPEAT_LAST_SYMBOL = ENDPOINT_PRED_TOTAL_SYMBOLS - 1;
  74. const uint32_t ENDPOINT_PRED_MIN_REPEAT_COUNT = 3;
  75. const uint32_t ENDPOINT_PRED_COUNT_VLC_BITS = 4;
  76. const uint32_t NUM_ENDPOINT_PREDS = 3;// BASISU_ARRAY_SIZE(g_endpoint_preds);
  77. const uint32_t CR_ENDPOINT_PRED_INDEX = NUM_ENDPOINT_PREDS - 1;
  78. const uint32_t NO_ENDPOINT_PRED_INDEX = 3;//NUM_ENDPOINT_PREDS;
  79. const uint32_t MAX_SELECTOR_HISTORY_BUF_SIZE = 64;
  80. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_THRESH = 3;
  81. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_BITS = 6;
  82. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_TOTAL = (1 << SELECTOR_HISTORY_BUF_RLE_COUNT_BITS);
  83. uint16_t crc16(const void *r, size_t size, uint16_t crc);
  84. class huffman_decoding_table
  85. {
  86. friend class bitwise_decoder;
  87. public:
  88. huffman_decoding_table()
  89. {
  90. }
  91. void clear()
  92. {
  93. basisu::clear_vector(m_code_sizes);
  94. basisu::clear_vector(m_lookup);
  95. basisu::clear_vector(m_tree);
  96. }
  97. bool init(uint32_t total_syms, const uint8_t *pCode_sizes)
  98. {
  99. if (!total_syms)
  100. {
  101. clear();
  102. return true;
  103. }
  104. m_code_sizes.resize(total_syms);
  105. memcpy(&m_code_sizes[0], pCode_sizes, total_syms);
  106. m_lookup.resize(0);
  107. m_lookup.resize(basisu::cHuffmanFastLookupSize);
  108. m_tree.resize(0);
  109. m_tree.resize(total_syms * 2);
  110. uint32_t syms_using_codesize[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  111. basisu::clear_obj(syms_using_codesize);
  112. for (uint32_t i = 0; i < total_syms; i++)
  113. {
  114. if (pCode_sizes[i] > basisu::cHuffmanMaxSupportedInternalCodeSize)
  115. return false;
  116. syms_using_codesize[pCode_sizes[i]]++;
  117. }
  118. uint32_t next_code[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  119. next_code[0] = next_code[1] = 0;
  120. uint32_t used_syms = 0, total = 0;
  121. for (uint32_t i = 1; i < basisu::cHuffmanMaxSupportedInternalCodeSize; i++)
  122. {
  123. used_syms += syms_using_codesize[i];
  124. next_code[i + 1] = (total = ((total + syms_using_codesize[i]) << 1));
  125. }
  126. if (((1U << basisu::cHuffmanMaxSupportedInternalCodeSize) != total) && (used_syms > 1U))
  127. return false;
  128. for (int tree_next = -1, sym_index = 0; sym_index < (int)total_syms; ++sym_index)
  129. {
  130. uint32_t rev_code = 0, l, cur_code, code_size = pCode_sizes[sym_index];
  131. if (!code_size)
  132. continue;
  133. cur_code = next_code[code_size]++;
  134. for (l = code_size; l > 0; l--, cur_code >>= 1)
  135. rev_code = (rev_code << 1) | (cur_code & 1);
  136. if (code_size <= basisu::cHuffmanFastLookupBits)
  137. {
  138. uint32_t k = (code_size << 16) | sym_index;
  139. while (rev_code < basisu::cHuffmanFastLookupSize)
  140. {
  141. if (m_lookup[rev_code] != 0)
  142. {
  143. // Supplied codesizes can't create a valid prefix code.
  144. return false;
  145. }
  146. m_lookup[rev_code] = k;
  147. rev_code += (1 << code_size);
  148. }
  149. continue;
  150. }
  151. int tree_cur;
  152. if (0 == (tree_cur = m_lookup[rev_code & (basisu::cHuffmanFastLookupSize - 1)]))
  153. {
  154. const uint32_t idx = rev_code & (basisu::cHuffmanFastLookupSize - 1);
  155. if (m_lookup[idx] != 0)
  156. {
  157. // Supplied codesizes can't create a valid prefix code.
  158. return false;
  159. }
  160. m_lookup[idx] = tree_next;
  161. tree_cur = tree_next;
  162. tree_next -= 2;
  163. }
  164. if (tree_cur >= 0)
  165. {
  166. // Supplied codesizes can't create a valid prefix code.
  167. return false;
  168. }
  169. rev_code >>= (basisu::cHuffmanFastLookupBits - 1);
  170. for (int j = code_size; j > (basisu::cHuffmanFastLookupBits + 1); j--)
  171. {
  172. tree_cur -= ((rev_code >>= 1) & 1);
  173. const int idx = -tree_cur - 1;
  174. if (idx < 0)
  175. return false;
  176. else if (idx >= (int)m_tree.size())
  177. m_tree.resize(idx + 1);
  178. if (!m_tree[idx])
  179. {
  180. m_tree[idx] = (int16_t)tree_next;
  181. tree_cur = tree_next;
  182. tree_next -= 2;
  183. }
  184. else
  185. {
  186. tree_cur = m_tree[idx];
  187. if (tree_cur >= 0)
  188. {
  189. // Supplied codesizes can't create a valid prefix code.
  190. return false;
  191. }
  192. }
  193. }
  194. tree_cur -= ((rev_code >>= 1) & 1);
  195. const int idx = -tree_cur - 1;
  196. if (idx < 0)
  197. return false;
  198. else if (idx >= (int)m_tree.size())
  199. m_tree.resize(idx + 1);
  200. if (m_tree[idx] != 0)
  201. {
  202. // Supplied codesizes can't create a valid prefix code.
  203. return false;
  204. }
  205. m_tree[idx] = (int16_t)sym_index;
  206. }
  207. return true;
  208. }
  209. const basisu::uint8_vec &get_code_sizes() const { return m_code_sizes; }
  210. bool is_valid() const { return m_code_sizes.size() > 0; }
  211. private:
  212. basisu::uint8_vec m_code_sizes;
  213. basisu::int_vec m_lookup;
  214. basisu::int16_vec m_tree;
  215. };
  216. class bitwise_decoder
  217. {
  218. public:
  219. bitwise_decoder() :
  220. m_buf_size(0),
  221. m_pBuf(nullptr),
  222. m_pBuf_start(nullptr),
  223. m_pBuf_end(nullptr),
  224. m_bit_buf(0),
  225. m_bit_buf_size(0)
  226. {
  227. }
  228. void clear()
  229. {
  230. m_buf_size = 0;
  231. m_pBuf = nullptr;
  232. m_pBuf_start = nullptr;
  233. m_pBuf_end = nullptr;
  234. m_bit_buf = 0;
  235. m_bit_buf_size = 0;
  236. }
  237. bool init(const uint8_t *pBuf, uint32_t buf_size)
  238. {
  239. if ((!pBuf) && (buf_size))
  240. return false;
  241. m_buf_size = buf_size;
  242. m_pBuf = pBuf;
  243. m_pBuf_start = pBuf;
  244. m_pBuf_end = pBuf + buf_size;
  245. m_bit_buf = 0;
  246. m_bit_buf_size = 0;
  247. return true;
  248. }
  249. void stop()
  250. {
  251. }
  252. inline uint32_t peek_bits(uint32_t num_bits)
  253. {
  254. if (!num_bits)
  255. return 0;
  256. assert(num_bits <= 25);
  257. while (m_bit_buf_size < num_bits)
  258. {
  259. uint32_t c = 0;
  260. if (m_pBuf < m_pBuf_end)
  261. c = *m_pBuf++;
  262. m_bit_buf |= (c << m_bit_buf_size);
  263. m_bit_buf_size += 8;
  264. assert(m_bit_buf_size <= 32);
  265. }
  266. return m_bit_buf & ((1 << num_bits) - 1);
  267. }
  268. void remove_bits(uint32_t num_bits)
  269. {
  270. assert(m_bit_buf_size >= num_bits);
  271. m_bit_buf >>= num_bits;
  272. m_bit_buf_size -= num_bits;
  273. }
  274. uint32_t get_bits(uint32_t num_bits)
  275. {
  276. if (num_bits > 25)
  277. {
  278. assert(num_bits <= 32);
  279. const uint32_t bits0 = peek_bits(25);
  280. m_bit_buf >>= 25;
  281. m_bit_buf_size -= 25;
  282. num_bits -= 25;
  283. const uint32_t bits = peek_bits(num_bits);
  284. m_bit_buf >>= num_bits;
  285. m_bit_buf_size -= num_bits;
  286. return bits0 | (bits << 25);
  287. }
  288. const uint32_t bits = peek_bits(num_bits);
  289. m_bit_buf >>= num_bits;
  290. m_bit_buf_size -= num_bits;
  291. return bits;
  292. }
  293. uint32_t decode_truncated_binary(uint32_t n)
  294. {
  295. assert(n >= 2);
  296. const uint32_t k = basisu::floor_log2i(n);
  297. const uint32_t u = (1 << (k + 1)) - n;
  298. uint32_t result = get_bits(k);
  299. if (result >= u)
  300. result = ((result << 1) | get_bits(1)) - u;
  301. return result;
  302. }
  303. uint32_t decode_rice(uint32_t m)
  304. {
  305. assert(m);
  306. uint32_t q = 0;
  307. for (;;)
  308. {
  309. uint32_t k = peek_bits(16);
  310. uint32_t l = 0;
  311. while (k & 1)
  312. {
  313. l++;
  314. k >>= 1;
  315. }
  316. q += l;
  317. remove_bits(l);
  318. if (l < 16)
  319. break;
  320. }
  321. return (q << m) + (get_bits(m + 1) >> 1);
  322. }
  323. inline uint32_t decode_vlc(uint32_t chunk_bits)
  324. {
  325. assert(chunk_bits);
  326. const uint32_t chunk_size = 1 << chunk_bits;
  327. const uint32_t chunk_mask = chunk_size - 1;
  328. uint32_t v = 0;
  329. uint32_t ofs = 0;
  330. for ( ; ; )
  331. {
  332. uint32_t s = get_bits(chunk_bits + 1);
  333. v |= ((s & chunk_mask) << ofs);
  334. ofs += chunk_bits;
  335. if ((s & chunk_size) == 0)
  336. break;
  337. if (ofs >= 32)
  338. {
  339. assert(0);
  340. break;
  341. }
  342. }
  343. return v;
  344. }
  345. inline uint32_t decode_huffman(const huffman_decoding_table &ct)
  346. {
  347. assert(ct.m_code_sizes.size());
  348. while (m_bit_buf_size < 16)
  349. {
  350. uint32_t c = 0;
  351. if (m_pBuf < m_pBuf_end)
  352. c = *m_pBuf++;
  353. m_bit_buf |= (c << m_bit_buf_size);
  354. m_bit_buf_size += 8;
  355. assert(m_bit_buf_size <= 32);
  356. }
  357. int code_len;
  358. int sym;
  359. if ((sym = ct.m_lookup[m_bit_buf & (basisu::cHuffmanFastLookupSize - 1)]) >= 0)
  360. {
  361. code_len = sym >> 16;
  362. sym &= 0xFFFF;
  363. }
  364. else
  365. {
  366. code_len = basisu::cHuffmanFastLookupBits;
  367. do
  368. {
  369. sym = ct.m_tree[~sym + ((m_bit_buf >> code_len++) & 1)]; // ~sym = -sym - 1
  370. } while (sym < 0);
  371. }
  372. m_bit_buf >>= code_len;
  373. m_bit_buf_size -= code_len;
  374. return sym;
  375. }
  376. bool read_huffman_table(huffman_decoding_table &ct)
  377. {
  378. ct.clear();
  379. const uint32_t total_used_syms = get_bits(basisu::cHuffmanMaxSymsLog2);
  380. if (!total_used_syms)
  381. return true;
  382. if (total_used_syms > basisu::cHuffmanMaxSyms)
  383. return false;
  384. uint8_t code_length_code_sizes[basisu::cHuffmanTotalCodelengthCodes];
  385. basisu::clear_obj(code_length_code_sizes);
  386. const uint32_t num_codelength_codes = get_bits(5);
  387. if ((num_codelength_codes < 1) || (num_codelength_codes > basisu::cHuffmanTotalCodelengthCodes))
  388. return false;
  389. for (uint32_t i = 0; i < num_codelength_codes; i++)
  390. code_length_code_sizes[basisu::g_huffman_sorted_codelength_codes[i]] = static_cast<uint8_t>(get_bits(3));
  391. huffman_decoding_table code_length_table;
  392. if (!code_length_table.init(basisu::cHuffmanTotalCodelengthCodes, code_length_code_sizes))
  393. return false;
  394. if (!code_length_table.is_valid())
  395. return false;
  396. basisu::uint8_vec code_sizes(total_used_syms);
  397. uint32_t cur = 0;
  398. while (cur < total_used_syms)
  399. {
  400. int c = decode_huffman(code_length_table);
  401. if (c <= 16)
  402. code_sizes[cur++] = static_cast<uint8_t>(c);
  403. else if (c == basisu::cHuffmanSmallZeroRunCode)
  404. cur += get_bits(basisu::cHuffmanSmallZeroRunExtraBits) + basisu::cHuffmanSmallZeroRunSizeMin;
  405. else if (c == basisu::cHuffmanBigZeroRunCode)
  406. cur += get_bits(basisu::cHuffmanBigZeroRunExtraBits) + basisu::cHuffmanBigZeroRunSizeMin;
  407. else
  408. {
  409. if (!cur)
  410. return false;
  411. uint32_t l;
  412. if (c == basisu::cHuffmanSmallRepeatCode)
  413. l = get_bits(basisu::cHuffmanSmallRepeatExtraBits) + basisu::cHuffmanSmallRepeatSizeMin;
  414. else
  415. l = get_bits(basisu::cHuffmanBigRepeatExtraBits) + basisu::cHuffmanBigRepeatSizeMin;
  416. const uint8_t prev = code_sizes[cur - 1];
  417. if (prev == 0)
  418. return false;
  419. do
  420. {
  421. if (cur >= total_used_syms)
  422. return false;
  423. code_sizes[cur++] = prev;
  424. } while (--l > 0);
  425. }
  426. }
  427. if (cur != total_used_syms)
  428. return false;
  429. return ct.init(total_used_syms, &code_sizes[0]);
  430. }
  431. private:
  432. uint32_t m_buf_size;
  433. const uint8_t *m_pBuf;
  434. const uint8_t *m_pBuf_start;
  435. const uint8_t *m_pBuf_end;
  436. uint32_t m_bit_buf;
  437. uint32_t m_bit_buf_size;
  438. };
  439. inline uint32_t basisd_rand(uint32_t seed)
  440. {
  441. if (!seed)
  442. seed++;
  443. uint32_t z = seed;
  444. BASISD_znew;
  445. return z;
  446. }
  447. // Returns random number in [0,limit). Max limit is 0xFFFF.
  448. inline uint32_t basisd_urand(uint32_t& seed, uint32_t limit)
  449. {
  450. seed = basisd_rand(seed);
  451. return (((seed ^ (seed >> 16)) & 0xFFFF) * limit) >> 16;
  452. }
  453. class approx_move_to_front
  454. {
  455. public:
  456. approx_move_to_front(uint32_t n)
  457. {
  458. init(n);
  459. }
  460. void init(uint32_t n)
  461. {
  462. m_values.resize(n);
  463. m_rover = n / 2;
  464. }
  465. const basisu::int_vec& get_values() const { return m_values; }
  466. basisu::int_vec& get_values() { return m_values; }
  467. uint32_t size() const { return (uint32_t)m_values.size(); }
  468. const int& operator[] (uint32_t index) const { return m_values[index]; }
  469. int operator[] (uint32_t index) { return m_values[index]; }
  470. void add(int new_value)
  471. {
  472. m_values[m_rover++] = new_value;
  473. if (m_rover == m_values.size())
  474. m_rover = (uint32_t)m_values.size() / 2;
  475. }
  476. void use(uint32_t index)
  477. {
  478. if (index)
  479. {
  480. //std::swap(m_values[index / 2], m_values[index]);
  481. int x = m_values[index / 2];
  482. int y = m_values[index];
  483. m_values[index / 2] = y;
  484. m_values[index] = x;
  485. }
  486. }
  487. // returns -1 if not found
  488. int find(int value) const
  489. {
  490. for (uint32_t i = 0; i < m_values.size(); i++)
  491. if (m_values[i] == value)
  492. return i;
  493. return -1;
  494. }
  495. void reset()
  496. {
  497. const uint32_t n = (uint32_t)m_values.size();
  498. m_values.clear();
  499. init(n);
  500. }
  501. private:
  502. basisu::int_vec m_values;
  503. uint32_t m_rover;
  504. };
  505. struct decoder_etc_block;
  506. inline uint8_t clamp255(int32_t i)
  507. {
  508. return (uint8_t)((i & 0xFFFFFF00U) ? (~(i >> 31)) : i);
  509. }
  510. struct color32
  511. {
  512. union
  513. {
  514. struct
  515. {
  516. uint8_t r;
  517. uint8_t g;
  518. uint8_t b;
  519. uint8_t a;
  520. };
  521. uint8_t c[4];
  522. uint32_t m;
  523. };
  524. color32() { }
  525. color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
  526. void set(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { c[0] = static_cast<uint8_t>(vr); c[1] = static_cast<uint8_t>(vg); c[2] = static_cast<uint8_t>(vb); c[3] = static_cast<uint8_t>(va); }
  527. void set_clamped(int vr, int vg, int vb, int va) { c[0] = clamp255(vr); c[1] = clamp255(vg); c[2] = clamp255(vb); c[3] = clamp255(va); }
  528. uint8_t operator[] (uint32_t idx) const { assert(idx < 4); return c[idx]; }
  529. uint8_t &operator[] (uint32_t idx) { assert(idx < 4); return c[idx]; }
  530. bool operator== (const color32&rhs) const { return m == rhs.m; }
  531. };
  532. struct endpoint
  533. {
  534. color32 m_color5;
  535. uint8_t m_inten5;
  536. };
  537. struct selector
  538. {
  539. // Plain selectors (2-bits per value)
  540. uint8_t m_selectors[4];
  541. // ETC1 selectors
  542. uint8_t m_bytes[4];
  543. uint8_t m_lo_selector, m_hi_selector;
  544. uint8_t m_num_unique_selectors;
  545. void init_flags()
  546. {
  547. uint32_t hist[4] = { 0, 0, 0, 0 };
  548. for (uint32_t y = 0; y < 4; y++)
  549. {
  550. for (uint32_t x = 0; x < 4; x++)
  551. {
  552. uint32_t s = get_selector(x, y);
  553. hist[s]++;
  554. }
  555. }
  556. m_lo_selector = 3;
  557. m_hi_selector = 0;
  558. m_num_unique_selectors = 0;
  559. for (uint32_t i = 0; i < 4; i++)
  560. {
  561. if (hist[i])
  562. {
  563. m_num_unique_selectors++;
  564. if (i < m_lo_selector) m_lo_selector = static_cast<uint8_t>(i);
  565. if (i > m_hi_selector) m_hi_selector = static_cast<uint8_t>(i);
  566. }
  567. }
  568. }
  569. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  570. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  571. {
  572. assert((x < 4) && (y < 4));
  573. return (m_selectors[y] >> (x * 2)) & 3;
  574. }
  575. void set_selector(uint32_t x, uint32_t y, uint32_t val)
  576. {
  577. static const uint8_t s_selector_index_to_etc1[4] = { 3, 2, 0, 1 };
  578. assert((x | y | val) < 4);
  579. m_selectors[y] &= ~(3 << (x * 2));
  580. m_selectors[y] |= (val << (x * 2));
  581. const uint32_t etc1_bit_index = x * 4 + y;
  582. uint8_t *p = &m_bytes[3 - (etc1_bit_index >> 3)];
  583. const uint32_t byte_bit_ofs = etc1_bit_index & 7;
  584. const uint32_t mask = 1 << byte_bit_ofs;
  585. const uint32_t etc1_val = s_selector_index_to_etc1[val];
  586. const uint32_t lsb = etc1_val & 1;
  587. const uint32_t msb = etc1_val >> 1;
  588. p[0] &= ~mask;
  589. p[0] |= (lsb << byte_bit_ofs);
  590. p[-2] &= ~mask;
  591. p[-2] |= (msb << byte_bit_ofs);
  592. }
  593. };
  594. bool basis_block_format_is_uncompressed(block_format tex_type);
  595. } // namespace basist