basisu_transcoder_internal.h 20 KB

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