basisu_transcoder_internal.h 20 KB

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