basisu_transcoder_internal.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. // basisu_transcoder_internal.h - Universal texture format transcoder library.
  2. // Copyright (C) 2019-2024 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. // v1.50: Added UASTC HDR support
  22. #define BASISD_LIB_VERSION 150
  23. #define BASISD_VERSION_STRING "01.50"
  24. #ifdef _DEBUG
  25. #define BASISD_BUILD_DEBUG
  26. #else
  27. #define BASISD_BUILD_RELEASE
  28. #endif
  29. #include "basisu.h"
  30. #define BASISD_znew (z = 36969 * (z & 65535) + (z >> 16))
  31. namespace basisu
  32. {
  33. extern bool g_debug_printf;
  34. }
  35. namespace basist
  36. {
  37. // Low-level formats directly supported by the transcoder (other supported texture formats are combinations of these low-level block formats).
  38. // You probably don't care about these enum's unless you are going pretty low-level and calling the transcoder to decode individual slices.
  39. enum class block_format
  40. {
  41. cETC1, // ETC1S RGB
  42. cETC2_RGBA, // full ETC2 EAC RGBA8 block
  43. cBC1, // DXT1 RGB
  44. cBC3, // BC4 block followed by a four color BC1 block
  45. cBC4, // DXT5A (alpha block only)
  46. cBC5, // two BC4 blocks
  47. cPVRTC1_4_RGB, // opaque-only PVRTC1 4bpp
  48. cPVRTC1_4_RGBA, // PVRTC1 4bpp RGBA
  49. cBC7, // Full BC7 block, any mode
  50. cBC7_M5_COLOR, // RGB BC7 mode 5 color (writes an opaque mode 5 block)
  51. 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.)
  52. cETC2_EAC_A8, // alpha block of ETC2 EAC (first 8 bytes of the 16-bit ETC2 EAC RGBA format)
  53. cASTC_4x4, // ASTC 4x4 (either color-only or color+alpha). Note that the transcoder always currently assumes sRGB is not enabled when outputting ASTC
  54. // 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.
  55. cATC_RGB,
  56. cATC_RGBA_INTERPOLATED_ALPHA,
  57. cFXT1_RGB, // Opaque-only, has oddball 8x4 pixel block size
  58. cPVRTC2_4_RGB,
  59. cPVRTC2_4_RGBA,
  60. cETC2_EAC_R11,
  61. cETC2_EAC_RG11,
  62. cIndices, // Used internally: Write 16-bit endpoint and selector indices directly to output (output block must be at least 32-bits)
  63. cRGB32, // Writes RGB components to 32bpp output pixels
  64. cRGBA32, // Writes RGB255 components to 32bpp output pixels
  65. cA32, // Writes alpha component to 32bpp output pixels
  66. cRGB565,
  67. cBGR565,
  68. cRGBA4444_COLOR,
  69. cRGBA4444_ALPHA,
  70. cRGBA4444_COLOR_OPAQUE,
  71. cRGBA4444,
  72. cRGBA_HALF,
  73. cRGB_HALF,
  74. cRGB_9E5,
  75. cUASTC_4x4, // LDR, universal
  76. cUASTC_HDR_4x4, // HDR, transcodes only to 4x4 HDR ASTC, BC6H, or uncompressed
  77. cBC6H,
  78. cASTC_HDR_4x4,
  79. cTotalBlockFormats
  80. };
  81. const int COLOR5_PAL0_PREV_HI = 9, COLOR5_PAL0_DELTA_LO = -9, COLOR5_PAL0_DELTA_HI = 31;
  82. const int COLOR5_PAL1_PREV_HI = 21, COLOR5_PAL1_DELTA_LO = -21, COLOR5_PAL1_DELTA_HI = 21;
  83. const int COLOR5_PAL2_PREV_HI = 31, COLOR5_PAL2_DELTA_LO = -31, COLOR5_PAL2_DELTA_HI = 9;
  84. const int COLOR5_PAL_MIN_DELTA_B_RUNLEN = 3, COLOR5_PAL_DELTA_5_RUNLEN_VLC_BITS = 3;
  85. const uint32_t ENDPOINT_PRED_TOTAL_SYMBOLS = (4 * 4 * 4 * 4) + 1;
  86. const uint32_t ENDPOINT_PRED_REPEAT_LAST_SYMBOL = ENDPOINT_PRED_TOTAL_SYMBOLS - 1;
  87. const uint32_t ENDPOINT_PRED_MIN_REPEAT_COUNT = 3;
  88. const uint32_t ENDPOINT_PRED_COUNT_VLC_BITS = 4;
  89. const uint32_t NUM_ENDPOINT_PREDS = 3;// BASISU_ARRAY_SIZE(g_endpoint_preds);
  90. const uint32_t CR_ENDPOINT_PRED_INDEX = NUM_ENDPOINT_PREDS - 1;
  91. const uint32_t NO_ENDPOINT_PRED_INDEX = 3;//NUM_ENDPOINT_PREDS;
  92. const uint32_t MAX_SELECTOR_HISTORY_BUF_SIZE = 64;
  93. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_THRESH = 3;
  94. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_BITS = 6;
  95. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_TOTAL = (1 << SELECTOR_HISTORY_BUF_RLE_COUNT_BITS);
  96. uint16_t crc16(const void *r, size_t size, uint16_t crc);
  97. class huffman_decoding_table
  98. {
  99. friend class bitwise_decoder;
  100. public:
  101. huffman_decoding_table()
  102. {
  103. }
  104. void clear()
  105. {
  106. basisu::clear_vector(m_code_sizes);
  107. basisu::clear_vector(m_lookup);
  108. basisu::clear_vector(m_tree);
  109. }
  110. bool init(uint32_t total_syms, const uint8_t *pCode_sizes, uint32_t fast_lookup_bits = basisu::cHuffmanFastLookupBits)
  111. {
  112. if (!total_syms)
  113. {
  114. clear();
  115. return true;
  116. }
  117. m_code_sizes.resize(total_syms);
  118. memcpy(&m_code_sizes[0], pCode_sizes, total_syms);
  119. const uint32_t huffman_fast_lookup_size = 1 << fast_lookup_bits;
  120. m_lookup.resize(0);
  121. m_lookup.resize(huffman_fast_lookup_size);
  122. m_tree.resize(0);
  123. m_tree.resize(total_syms * 2);
  124. uint32_t syms_using_codesize[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  125. basisu::clear_obj(syms_using_codesize);
  126. for (uint32_t i = 0; i < total_syms; i++)
  127. {
  128. if (pCode_sizes[i] > basisu::cHuffmanMaxSupportedInternalCodeSize)
  129. return false;
  130. syms_using_codesize[pCode_sizes[i]]++;
  131. }
  132. uint32_t next_code[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  133. next_code[0] = next_code[1] = 0;
  134. uint32_t used_syms = 0, total = 0;
  135. for (uint32_t i = 1; i < basisu::cHuffmanMaxSupportedInternalCodeSize; i++)
  136. {
  137. used_syms += syms_using_codesize[i];
  138. next_code[i + 1] = (total = ((total + syms_using_codesize[i]) << 1));
  139. }
  140. if (((1U << basisu::cHuffmanMaxSupportedInternalCodeSize) != total) && (used_syms != 1U))
  141. return false;
  142. for (int tree_next = -1, sym_index = 0; sym_index < (int)total_syms; ++sym_index)
  143. {
  144. uint32_t rev_code = 0, l, cur_code, code_size = pCode_sizes[sym_index];
  145. if (!code_size)
  146. continue;
  147. cur_code = next_code[code_size]++;
  148. for (l = code_size; l > 0; l--, cur_code >>= 1)
  149. rev_code = (rev_code << 1) | (cur_code & 1);
  150. if (code_size <= fast_lookup_bits)
  151. {
  152. uint32_t k = (code_size << 16) | sym_index;
  153. while (rev_code < huffman_fast_lookup_size)
  154. {
  155. if (m_lookup[rev_code] != 0)
  156. {
  157. // Supplied codesizes can't create a valid prefix code.
  158. return false;
  159. }
  160. m_lookup[rev_code] = k;
  161. rev_code += (1 << code_size);
  162. }
  163. continue;
  164. }
  165. int tree_cur;
  166. if (0 == (tree_cur = m_lookup[rev_code & (huffman_fast_lookup_size - 1)]))
  167. {
  168. const uint32_t idx = rev_code & (huffman_fast_lookup_size - 1);
  169. if (m_lookup[idx] != 0)
  170. {
  171. // Supplied codesizes can't create a valid prefix code.
  172. return false;
  173. }
  174. m_lookup[idx] = tree_next;
  175. tree_cur = tree_next;
  176. tree_next -= 2;
  177. }
  178. if (tree_cur >= 0)
  179. {
  180. // Supplied codesizes can't create a valid prefix code.
  181. return false;
  182. }
  183. rev_code >>= (fast_lookup_bits - 1);
  184. for (int j = code_size; j > ((int)fast_lookup_bits + 1); j--)
  185. {
  186. tree_cur -= ((rev_code >>= 1) & 1);
  187. const int idx = -tree_cur - 1;
  188. if (idx < 0)
  189. return false;
  190. else if (idx >= (int)m_tree.size())
  191. m_tree.resize(idx + 1);
  192. if (!m_tree[idx])
  193. {
  194. m_tree[idx] = (int16_t)tree_next;
  195. tree_cur = tree_next;
  196. tree_next -= 2;
  197. }
  198. else
  199. {
  200. tree_cur = m_tree[idx];
  201. if (tree_cur >= 0)
  202. {
  203. // Supplied codesizes can't create a valid prefix code.
  204. return false;
  205. }
  206. }
  207. }
  208. tree_cur -= ((rev_code >>= 1) & 1);
  209. const int idx = -tree_cur - 1;
  210. if (idx < 0)
  211. return false;
  212. else if (idx >= (int)m_tree.size())
  213. m_tree.resize(idx + 1);
  214. if (m_tree[idx] != 0)
  215. {
  216. // Supplied codesizes can't create a valid prefix code.
  217. return false;
  218. }
  219. m_tree[idx] = (int16_t)sym_index;
  220. }
  221. return true;
  222. }
  223. const basisu::uint8_vec &get_code_sizes() const { return m_code_sizes; }
  224. const basisu::int_vec &get_lookup() const { return m_lookup; }
  225. const basisu::int16_vec &get_tree() const { return m_tree; }
  226. bool is_valid() const { return m_code_sizes.size() > 0; }
  227. private:
  228. basisu::uint8_vec m_code_sizes;
  229. basisu::int_vec m_lookup;
  230. basisu::int16_vec m_tree;
  231. };
  232. class bitwise_decoder
  233. {
  234. public:
  235. bitwise_decoder() :
  236. m_buf_size(0),
  237. m_pBuf(nullptr),
  238. m_pBuf_start(nullptr),
  239. m_pBuf_end(nullptr),
  240. m_bit_buf(0),
  241. m_bit_buf_size(0)
  242. {
  243. }
  244. void clear()
  245. {
  246. m_buf_size = 0;
  247. m_pBuf = nullptr;
  248. m_pBuf_start = nullptr;
  249. m_pBuf_end = nullptr;
  250. m_bit_buf = 0;
  251. m_bit_buf_size = 0;
  252. }
  253. bool init(const uint8_t *pBuf, uint32_t buf_size)
  254. {
  255. if ((!pBuf) && (buf_size))
  256. return false;
  257. m_buf_size = buf_size;
  258. m_pBuf = pBuf;
  259. m_pBuf_start = pBuf;
  260. m_pBuf_end = pBuf + buf_size;
  261. m_bit_buf = 0;
  262. m_bit_buf_size = 0;
  263. return true;
  264. }
  265. void stop()
  266. {
  267. }
  268. inline uint32_t peek_bits(uint32_t num_bits)
  269. {
  270. if (!num_bits)
  271. return 0;
  272. assert(num_bits <= 25);
  273. while (m_bit_buf_size < num_bits)
  274. {
  275. uint32_t c = 0;
  276. if (m_pBuf < m_pBuf_end)
  277. c = *m_pBuf++;
  278. m_bit_buf |= (c << m_bit_buf_size);
  279. m_bit_buf_size += 8;
  280. assert(m_bit_buf_size <= 32);
  281. }
  282. return m_bit_buf & ((1 << num_bits) - 1);
  283. }
  284. void remove_bits(uint32_t num_bits)
  285. {
  286. assert(m_bit_buf_size >= num_bits);
  287. m_bit_buf >>= num_bits;
  288. m_bit_buf_size -= num_bits;
  289. }
  290. uint32_t get_bits(uint32_t num_bits)
  291. {
  292. if (num_bits > 25)
  293. {
  294. assert(num_bits <= 32);
  295. const uint32_t bits0 = peek_bits(25);
  296. m_bit_buf >>= 25;
  297. m_bit_buf_size -= 25;
  298. num_bits -= 25;
  299. const uint32_t bits = peek_bits(num_bits);
  300. m_bit_buf >>= num_bits;
  301. m_bit_buf_size -= num_bits;
  302. return bits0 | (bits << 25);
  303. }
  304. const uint32_t bits = peek_bits(num_bits);
  305. m_bit_buf >>= num_bits;
  306. m_bit_buf_size -= num_bits;
  307. return bits;
  308. }
  309. uint32_t decode_truncated_binary(uint32_t n)
  310. {
  311. assert(n >= 2);
  312. const uint32_t k = basisu::floor_log2i(n);
  313. const uint32_t u = (1 << (k + 1)) - n;
  314. uint32_t result = get_bits(k);
  315. if (result >= u)
  316. result = ((result << 1) | get_bits(1)) - u;
  317. return result;
  318. }
  319. uint32_t decode_rice(uint32_t m)
  320. {
  321. assert(m);
  322. uint32_t q = 0;
  323. for (;;)
  324. {
  325. uint32_t k = peek_bits(16);
  326. uint32_t l = 0;
  327. while (k & 1)
  328. {
  329. l++;
  330. k >>= 1;
  331. }
  332. q += l;
  333. remove_bits(l);
  334. if (l < 16)
  335. break;
  336. }
  337. return (q << m) + (get_bits(m + 1) >> 1);
  338. }
  339. inline uint32_t decode_vlc(uint32_t chunk_bits)
  340. {
  341. assert(chunk_bits);
  342. const uint32_t chunk_size = 1 << chunk_bits;
  343. const uint32_t chunk_mask = chunk_size - 1;
  344. uint32_t v = 0;
  345. uint32_t ofs = 0;
  346. for ( ; ; )
  347. {
  348. uint32_t s = get_bits(chunk_bits + 1);
  349. v |= ((s & chunk_mask) << ofs);
  350. ofs += chunk_bits;
  351. if ((s & chunk_size) == 0)
  352. break;
  353. if (ofs >= 32)
  354. {
  355. assert(0);
  356. break;
  357. }
  358. }
  359. return v;
  360. }
  361. inline uint32_t decode_huffman(const huffman_decoding_table &ct, int fast_lookup_bits = basisu::cHuffmanFastLookupBits)
  362. {
  363. assert(ct.m_code_sizes.size());
  364. const uint32_t huffman_fast_lookup_size = 1 << fast_lookup_bits;
  365. while (m_bit_buf_size < 16)
  366. {
  367. uint32_t c = 0;
  368. if (m_pBuf < m_pBuf_end)
  369. c = *m_pBuf++;
  370. m_bit_buf |= (c << m_bit_buf_size);
  371. m_bit_buf_size += 8;
  372. assert(m_bit_buf_size <= 32);
  373. }
  374. int code_len;
  375. int sym;
  376. if ((sym = ct.m_lookup[m_bit_buf & (huffman_fast_lookup_size - 1)]) >= 0)
  377. {
  378. code_len = sym >> 16;
  379. sym &= 0xFFFF;
  380. }
  381. else
  382. {
  383. code_len = fast_lookup_bits;
  384. do
  385. {
  386. sym = ct.m_tree[~sym + ((m_bit_buf >> code_len++) & 1)]; // ~sym = -sym - 1
  387. } while (sym < 0);
  388. }
  389. m_bit_buf >>= code_len;
  390. m_bit_buf_size -= code_len;
  391. return sym;
  392. }
  393. bool read_huffman_table(huffman_decoding_table &ct)
  394. {
  395. ct.clear();
  396. const uint32_t total_used_syms = get_bits(basisu::cHuffmanMaxSymsLog2);
  397. if (!total_used_syms)
  398. return true;
  399. if (total_used_syms > basisu::cHuffmanMaxSyms)
  400. return false;
  401. uint8_t code_length_code_sizes[basisu::cHuffmanTotalCodelengthCodes];
  402. basisu::clear_obj(code_length_code_sizes);
  403. const uint32_t num_codelength_codes = get_bits(5);
  404. if ((num_codelength_codes < 1) || (num_codelength_codes > basisu::cHuffmanTotalCodelengthCodes))
  405. return false;
  406. for (uint32_t i = 0; i < num_codelength_codes; i++)
  407. code_length_code_sizes[basisu::g_huffman_sorted_codelength_codes[i]] = static_cast<uint8_t>(get_bits(3));
  408. huffman_decoding_table code_length_table;
  409. if (!code_length_table.init(basisu::cHuffmanTotalCodelengthCodes, code_length_code_sizes))
  410. return false;
  411. if (!code_length_table.is_valid())
  412. return false;
  413. basisu::uint8_vec code_sizes(total_used_syms);
  414. uint32_t cur = 0;
  415. while (cur < total_used_syms)
  416. {
  417. int c = decode_huffman(code_length_table);
  418. if (c <= 16)
  419. code_sizes[cur++] = static_cast<uint8_t>(c);
  420. else if (c == basisu::cHuffmanSmallZeroRunCode)
  421. cur += get_bits(basisu::cHuffmanSmallZeroRunExtraBits) + basisu::cHuffmanSmallZeroRunSizeMin;
  422. else if (c == basisu::cHuffmanBigZeroRunCode)
  423. cur += get_bits(basisu::cHuffmanBigZeroRunExtraBits) + basisu::cHuffmanBigZeroRunSizeMin;
  424. else
  425. {
  426. if (!cur)
  427. return false;
  428. uint32_t l;
  429. if (c == basisu::cHuffmanSmallRepeatCode)
  430. l = get_bits(basisu::cHuffmanSmallRepeatExtraBits) + basisu::cHuffmanSmallRepeatSizeMin;
  431. else
  432. l = get_bits(basisu::cHuffmanBigRepeatExtraBits) + basisu::cHuffmanBigRepeatSizeMin;
  433. const uint8_t prev = code_sizes[cur - 1];
  434. if (prev == 0)
  435. return false;
  436. do
  437. {
  438. if (cur >= total_used_syms)
  439. return false;
  440. code_sizes[cur++] = prev;
  441. } while (--l > 0);
  442. }
  443. }
  444. if (cur != total_used_syms)
  445. return false;
  446. return ct.init(total_used_syms, &code_sizes[0]);
  447. }
  448. private:
  449. uint32_t m_buf_size;
  450. const uint8_t *m_pBuf;
  451. const uint8_t *m_pBuf_start;
  452. const uint8_t *m_pBuf_end;
  453. uint32_t m_bit_buf;
  454. uint32_t m_bit_buf_size;
  455. };
  456. inline uint32_t basisd_rand(uint32_t seed)
  457. {
  458. if (!seed)
  459. seed++;
  460. uint32_t z = seed;
  461. BASISD_znew;
  462. return z;
  463. }
  464. // Returns random number in [0,limit). Max limit is 0xFFFF.
  465. inline uint32_t basisd_urand(uint32_t& seed, uint32_t limit)
  466. {
  467. seed = basisd_rand(seed);
  468. return (((seed ^ (seed >> 16)) & 0xFFFF) * limit) >> 16;
  469. }
  470. class approx_move_to_front
  471. {
  472. public:
  473. approx_move_to_front(uint32_t n)
  474. {
  475. init(n);
  476. }
  477. void init(uint32_t n)
  478. {
  479. m_values.resize(n);
  480. m_rover = n / 2;
  481. }
  482. const basisu::int_vec& get_values() const { return m_values; }
  483. basisu::int_vec& get_values() { return m_values; }
  484. uint32_t size() const { return (uint32_t)m_values.size(); }
  485. const int& operator[] (uint32_t index) const { return m_values[index]; }
  486. int operator[] (uint32_t index) { return m_values[index]; }
  487. void add(int new_value)
  488. {
  489. m_values[m_rover++] = new_value;
  490. if (m_rover == m_values.size())
  491. m_rover = (uint32_t)m_values.size() / 2;
  492. }
  493. void use(uint32_t index)
  494. {
  495. if (index)
  496. {
  497. //std::swap(m_values[index / 2], m_values[index]);
  498. int x = m_values[index / 2];
  499. int y = m_values[index];
  500. m_values[index / 2] = y;
  501. m_values[index] = x;
  502. }
  503. }
  504. // returns -1 if not found
  505. int find(int value) const
  506. {
  507. for (uint32_t i = 0; i < m_values.size(); i++)
  508. if (m_values[i] == value)
  509. return i;
  510. return -1;
  511. }
  512. void reset()
  513. {
  514. const uint32_t n = (uint32_t)m_values.size();
  515. m_values.clear();
  516. init(n);
  517. }
  518. private:
  519. basisu::int_vec m_values;
  520. uint32_t m_rover;
  521. };
  522. struct decoder_etc_block;
  523. inline uint8_t clamp255(int32_t i)
  524. {
  525. return (uint8_t)((i & 0xFFFFFF00U) ? (~(i >> 31)) : i);
  526. }
  527. enum eNoClamp
  528. {
  529. cNoClamp = 0
  530. };
  531. struct color32
  532. {
  533. union
  534. {
  535. struct
  536. {
  537. uint8_t r;
  538. uint8_t g;
  539. uint8_t b;
  540. uint8_t a;
  541. };
  542. uint8_t c[4];
  543. uint32_t m;
  544. };
  545. color32() { }
  546. color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
  547. color32(eNoClamp unused, uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { (void)unused; set_noclamp_rgba(vr, vg, vb, va); }
  548. 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); }
  549. 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); }
  550. void set_noclamp_rgba(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
  551. 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); }
  552. uint8_t operator[] (uint32_t idx) const { assert(idx < 4); return c[idx]; }
  553. uint8_t &operator[] (uint32_t idx) { assert(idx < 4); return c[idx]; }
  554. bool operator== (const color32&rhs) const { return m == rhs.m; }
  555. 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])); }
  556. 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])); }
  557. };
  558. struct endpoint
  559. {
  560. color32 m_color5;
  561. uint8_t m_inten5;
  562. bool operator== (const endpoint& rhs) const
  563. {
  564. 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);
  565. }
  566. bool operator!= (const endpoint& rhs) const { return !(*this == rhs); }
  567. };
  568. struct selector
  569. {
  570. // Plain selectors (2-bits per value)
  571. uint8_t m_selectors[4];
  572. // ETC1 selectors
  573. uint8_t m_bytes[4];
  574. uint8_t m_lo_selector, m_hi_selector;
  575. uint8_t m_num_unique_selectors;
  576. bool operator== (const selector& rhs) const
  577. {
  578. return (m_selectors[0] == rhs.m_selectors[0]) &&
  579. (m_selectors[1] == rhs.m_selectors[1]) &&
  580. (m_selectors[2] == rhs.m_selectors[2]) &&
  581. (m_selectors[3] == rhs.m_selectors[3]);
  582. }
  583. bool operator!= (const selector& rhs) const
  584. {
  585. return !(*this == rhs);
  586. }
  587. void init_flags()
  588. {
  589. uint32_t hist[4] = { 0, 0, 0, 0 };
  590. for (uint32_t y = 0; y < 4; y++)
  591. {
  592. for (uint32_t x = 0; x < 4; x++)
  593. {
  594. uint32_t s = get_selector(x, y);
  595. hist[s]++;
  596. }
  597. }
  598. m_lo_selector = 3;
  599. m_hi_selector = 0;
  600. m_num_unique_selectors = 0;
  601. for (uint32_t i = 0; i < 4; i++)
  602. {
  603. if (hist[i])
  604. {
  605. m_num_unique_selectors++;
  606. if (i < m_lo_selector) m_lo_selector = static_cast<uint8_t>(i);
  607. if (i > m_hi_selector) m_hi_selector = static_cast<uint8_t>(i);
  608. }
  609. }
  610. }
  611. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  612. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  613. {
  614. assert((x < 4) && (y < 4));
  615. return (m_selectors[y] >> (x * 2)) & 3;
  616. }
  617. void set_selector(uint32_t x, uint32_t y, uint32_t val)
  618. {
  619. static const uint8_t s_selector_index_to_etc1[4] = { 3, 2, 0, 1 };
  620. assert((x | y | val) < 4);
  621. m_selectors[y] &= ~(3 << (x * 2));
  622. m_selectors[y] |= (val << (x * 2));
  623. const uint32_t etc1_bit_index = x * 4 + y;
  624. uint8_t *p = &m_bytes[3 - (etc1_bit_index >> 3)];
  625. const uint32_t byte_bit_ofs = etc1_bit_index & 7;
  626. const uint32_t mask = 1 << byte_bit_ofs;
  627. const uint32_t etc1_val = s_selector_index_to_etc1[val];
  628. const uint32_t lsb = etc1_val & 1;
  629. const uint32_t msb = etc1_val >> 1;
  630. p[0] &= ~mask;
  631. p[0] |= (lsb << byte_bit_ofs);
  632. p[-2] &= ~mask;
  633. p[-2] |= (msb << byte_bit_ofs);
  634. }
  635. };
  636. bool basis_block_format_is_uncompressed(block_format tex_type);
  637. //------------------------------------
  638. typedef uint16_t half_float;
  639. const double MIN_DENORM_HALF_FLOAT = 0.000000059604645; // smallest positive subnormal number
  640. const double MIN_HALF_FLOAT = 0.00006103515625; // smallest positive normal number
  641. const double MAX_HALF_FLOAT = 65504.0; // largest normal number
  642. inline uint32_t get_bits(uint32_t val, int low, int high)
  643. {
  644. const int num_bits = (high - low) + 1;
  645. assert((num_bits >= 1) && (num_bits <= 32));
  646. val >>= low;
  647. if (num_bits != 32)
  648. val &= ((1u << num_bits) - 1);
  649. return val;
  650. }
  651. inline bool is_half_inf_or_nan(half_float v)
  652. {
  653. return get_bits(v, 10, 14) == 31;
  654. }
  655. inline bool is_half_denorm(half_float v)
  656. {
  657. int e = (v >> 10) & 31;
  658. return !e;
  659. }
  660. inline int get_half_exp(half_float v)
  661. {
  662. int e = ((v >> 10) & 31);
  663. return e ? (e - 15) : -14;
  664. }
  665. inline int get_half_mantissa(half_float v)
  666. {
  667. if (is_half_denorm(v))
  668. return v & 0x3FF;
  669. return (v & 0x3FF) | 0x400;
  670. }
  671. inline float get_half_mantissaf(half_float v)
  672. {
  673. return ((float)get_half_mantissa(v)) / 1024.0f;
  674. }
  675. inline int get_half_sign(half_float v)
  676. {
  677. return v ? ((v & 0x8000) ? -1 : 1) : 0;
  678. }
  679. inline bool half_is_signed(half_float v)
  680. {
  681. return (v & 0x8000) != 0;
  682. }
  683. #if 0
  684. int hexp = get_half_exp(Cf);
  685. float hman = get_half_mantissaf(Cf);
  686. int hsign = get_half_sign(Cf);
  687. float k = powf(2.0f, hexp) * hman * hsign;
  688. if (is_half_inf_or_nan(Cf))
  689. k = std::numeric_limits<float>::quiet_NaN();
  690. #endif
  691. half_float float_to_half(float val);
  692. inline float half_to_float(half_float hval)
  693. {
  694. union { float f; uint32_t u; } x = { 0 };
  695. uint32_t s = ((uint32_t)hval >> 15) & 1;
  696. uint32_t e = ((uint32_t)hval >> 10) & 0x1F;
  697. uint32_t m = (uint32_t)hval & 0x3FF;
  698. if (!e)
  699. {
  700. if (!m)
  701. {
  702. // +- 0
  703. x.u = s << 31;
  704. return x.f;
  705. }
  706. else
  707. {
  708. // denormalized
  709. while (!(m & 0x00000400))
  710. {
  711. m <<= 1;
  712. --e;
  713. }
  714. ++e;
  715. m &= ~0x00000400;
  716. }
  717. }
  718. else if (e == 31)
  719. {
  720. if (m == 0)
  721. {
  722. // +/- INF
  723. x.u = (s << 31) | 0x7f800000;
  724. return x.f;
  725. }
  726. else
  727. {
  728. // +/- NaN
  729. x.u = (s << 31) | 0x7f800000 | (m << 13);
  730. return x.f;
  731. }
  732. }
  733. e = e + (127 - 15);
  734. m = m << 13;
  735. assert(s <= 1);
  736. assert(m <= 0x7FFFFF);
  737. assert(e <= 255);
  738. x.u = m | (e << 23) | (s << 31);
  739. return x.f;
  740. }
  741. // Originally from bc6h_enc.h
  742. void bc6h_enc_init();
  743. const uint32_t MAX_BLOG16_VAL = 0xFFFF;
  744. // BC6H internals
  745. const uint32_t NUM_BC6H_MODES = 14;
  746. const uint32_t BC6H_LAST_MODE_INDEX = 13;
  747. const uint32_t BC6H_FIRST_1SUBSET_MODE_INDEX = 10; // in the MS docs, this is "mode 11" (where the first mode is 1), 60 bits for endpoints (10.10, 10.10, 10.10), 63 bits for weights
  748. const uint32_t TOTAL_BC6H_PARTITION_PATTERNS = 32;
  749. extern const uint8_t g_bc6h_mode_sig_bits[NUM_BC6H_MODES][4]; // base, r, g, b
  750. struct bc6h_bit_layout
  751. {
  752. int8_t m_comp; // R=0,G=1,B=2,D=3 (D=partition index)
  753. int8_t m_index; // 0-3, 0-1 Low/High subset 1, 2-3 Low/High subset 2, -1=partition index (d)
  754. int8_t m_last_bit;
  755. int8_t m_first_bit; // may be -1 if a single bit, may be >m_last_bit if reversed
  756. };
  757. const uint32_t MAX_BC6H_LAYOUT_INDEX = 25;
  758. extern const bc6h_bit_layout g_bc6h_bit_layouts[NUM_BC6H_MODES][MAX_BC6H_LAYOUT_INDEX];
  759. extern const uint8_t g_bc6h_2subset_patterns[TOTAL_BC6H_PARTITION_PATTERNS][4][4]; // [y][x]
  760. extern const uint8_t g_bc6h_weight3[8];
  761. extern const uint8_t g_bc6h_weight4[16];
  762. extern const int8_t g_bc6h_mode_lookup[32];
  763. // Converts b16 to half float
  764. inline half_float bc6h_blog16_to_half(uint32_t comp)
  765. {
  766. assert(comp <= 0xFFFF);
  767. // scale the magnitude by 31/64
  768. comp = (comp * 31u) >> 6u;
  769. return (half_float)comp;
  770. }
  771. const uint32_t MAX_BC6H_HALF_FLOAT_AS_UINT = 0x7BFF;
  772. // Inverts bc6h_blog16_to_half().
  773. // Returns the nearest blog16 given a half value.
  774. inline uint32_t bc6h_half_to_blog16(half_float h)
  775. {
  776. assert(h <= MAX_BC6H_HALF_FLOAT_AS_UINT);
  777. return (h * 64 + 30) / 31;
  778. }
  779. struct bc6h_block
  780. {
  781. uint8_t m_bytes[16];
  782. };
  783. void bc6h_enc_block_mode10(bc6h_block* pPacked_block, const half_float pEndpoints[3][2], const uint8_t* pWeights);
  784. void bc6h_enc_block_1subset_4bit_weights(bc6h_block* pPacked_block, const half_float pEndpoints[3][2], const uint8_t* pWeights);
  785. void bc6h_enc_block_1subset_mode9_3bit_weights(bc6h_block* pPacked_block, const half_float pEndpoints[3][2], const uint8_t* pWeights);
  786. void bc6h_enc_block_1subset_3bit_weights(bc6h_block* pPacked_block, const half_float pEndpoints[3][2], const uint8_t* pWeights);
  787. void bc6h_enc_block_2subset_mode9_3bit_weights(bc6h_block* pPacked_block, uint32_t common_part_index, const half_float pEndpoints[2][3][2], const uint8_t* pWeights); // pEndpoints[subset][comp][lh_index]
  788. void bc6h_enc_block_2subset_3bit_weights(bc6h_block* pPacked_block, uint32_t common_part_index, const half_float pEndpoints[2][3][2], const uint8_t* pWeights); // pEndpoints[subset][comp][lh_index]
  789. bool bc6h_enc_block_solid_color(bc6h_block* pPacked_block, const half_float pColor[3]);
  790. } // namespace basist