basisu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // basisu.h
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. // Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #pragma once
  17. #ifdef _MSC_VER
  18. #pragma warning (disable : 4201)
  19. #pragma warning (disable : 4127) // warning C4127: conditional expression is constant
  20. #pragma warning (disable : 4530) // C++ exception handler used, but unwind semantics are not enabled.
  21. // Slamming this off always for v1.16 because we've gotten rid of most std containers.
  22. #ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL
  23. #define BASISU_NO_ITERATOR_DEBUG_LEVEL (1)
  24. #endif
  25. #ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL
  26. //#define _HAS_ITERATOR_DEBUGGING 0
  27. #if defined(_DEBUG) || defined(DEBUG)
  28. // This is madness, but we need to disable iterator debugging in debug builds or the encoder is unsable because MSVC's iterator debugging implementation is totally broken.
  29. #ifndef _ITERATOR_DEBUG_LEVEL
  30. #define _ITERATOR_DEBUG_LEVEL 1
  31. #endif
  32. #ifndef _SECURE_SCL
  33. #define _SECURE_SCL 1
  34. #endif
  35. #else // defined(_DEBUG) || defined(DEBUG)
  36. #ifndef _SECURE_SCL
  37. #define _SECURE_SCL 0
  38. #endif
  39. #ifndef _ITERATOR_DEBUG_LEVEL
  40. #define _ITERATOR_DEBUG_LEVEL 0
  41. #endif
  42. #endif // defined(_DEBUG) || defined(DEBUG)
  43. #endif // BASISU_NO_ITERATOR_DEBUG_LEVEL
  44. #endif // _MSC_VER
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <math.h>
  48. #include <stdarg.h>
  49. #include <string.h>
  50. #include <memory.h>
  51. #include <limits.h>
  52. #include <stdint.h>
  53. #include <algorithm>
  54. #include <limits>
  55. #include <functional>
  56. #include <iterator>
  57. #include <type_traits>
  58. #include <assert.h>
  59. #include <random>
  60. #include "basisu_containers.h"
  61. #ifdef max
  62. #undef max
  63. #endif
  64. #ifdef min
  65. #undef min
  66. #endif
  67. #ifdef _WIN32
  68. #define strcasecmp _stricmp
  69. #endif
  70. // Set to one to enable debug printf()'s when any errors occur, for development/debugging. Especially useful for WebGL development.
  71. #ifndef BASISU_FORCE_DEVEL_MESSAGES
  72. #define BASISU_FORCE_DEVEL_MESSAGES 0
  73. #endif
  74. #define BASISU_NOTE_UNUSED(x) (void)(x)
  75. #define BASISU_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  76. #define BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(x) x(const x &) = delete; x& operator= (const x &) = delete;
  77. #define BASISU_ASSUME(x) static_assert(x, #x);
  78. #define BASISU_OFFSETOF(s, m) offsetof(s, m)
  79. #define BASISU_STRINGIZE(x) #x
  80. #define BASISU_STRINGIZE2(x) BASISU_STRINGIZE(x)
  81. #if BASISU_FORCE_DEVEL_MESSAGES
  82. #define BASISU_DEVEL_ERROR(...) do { basisu::debug_printf(__VA_ARGS__); } while(0)
  83. #else
  84. #define BASISU_DEVEL_ERROR(...)
  85. #endif
  86. namespace basisu
  87. {
  88. // Types/utilities
  89. #ifdef _WIN32
  90. const char BASISU_PATH_SEPERATOR_CHAR = '\\';
  91. #else
  92. const char BASISU_PATH_SEPERATOR_CHAR = '/';
  93. #endif
  94. typedef basisu::vector<uint8_t> uint8_vec;
  95. typedef basisu::vector<int16_t> int16_vec;
  96. typedef basisu::vector<uint16_t> uint16_vec;
  97. typedef basisu::vector<uint32_t> uint_vec;
  98. typedef basisu::vector<uint64_t> uint64_vec;
  99. typedef basisu::vector<int> int_vec;
  100. typedef basisu::vector<bool> bool_vec;
  101. void enable_debug_printf(bool enabled);
  102. void debug_printf(const char *pFmt, ...);
  103. template <typename T> inline void clear_obj(T& obj) { memset(&obj, 0, sizeof(obj)); }
  104. template <typename T0, typename T1> inline T0 lerp(T0 a, T0 b, T1 c) { return a + (b - a) * c; }
  105. template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
  106. template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
  107. template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }
  108. template <typename S> inline S minimum(S a, S b) { return (a < b) ? a : b; }
  109. template <typename S> inline S minimum(S a, S b, S c) { return minimum(minimum(a, b), c); }
  110. template <typename S> inline S minimum(S a, S b, S c, S d) { return minimum(minimum(minimum(a, b), c), d); }
  111. inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }
  112. inline float saturate(float value) { return clampf(value, 0, 1.0f); }
  113. inline uint8_t minimumub(uint8_t a, uint8_t b) { return (a < b) ? a : b; }
  114. inline uint32_t minimumu(uint32_t a, uint32_t b) { return (a < b) ? a : b; }
  115. inline int32_t minimumi(int32_t a, int32_t b) { return (a < b) ? a : b; }
  116. inline float minimumf(float a, float b) { return (a < b) ? a : b; }
  117. inline uint8_t maximumub(uint8_t a, uint8_t b) { return (a > b) ? a : b; }
  118. inline uint32_t maximumu(uint32_t a, uint32_t b) { return (a > b) ? a : b; }
  119. inline int32_t maximumi(int32_t a, int32_t b) { return (a > b) ? a : b; }
  120. inline float maximumf(float a, float b) { return (a > b) ? a : b; }
  121. inline int squarei(int i) { return i * i; }
  122. inline float squaref(float i) { return i * i; }
  123. template<typename T> inline T square(T a) { return a * a; }
  124. template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }
  125. inline uint32_t iabs(int32_t i) { return (i < 0) ? static_cast<uint32_t>(-i) : static_cast<uint32_t>(i); }
  126. inline uint64_t iabs64(int64_t i) { return (i < 0) ? static_cast<uint64_t>(-i) : static_cast<uint64_t>(i); }
  127. template<typename T> inline void clear_vector(T &vec) { vec.erase(vec.begin(), vec.end()); }
  128. template<typename T> inline typename T::value_type *enlarge_vector(T &vec, size_t n) { size_t cs = vec.size(); vec.resize(cs + n); return &vec[cs]; }
  129. inline bool is_pow2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
  130. inline bool is_pow2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
  131. template<typename T> inline T open_range_check(T v, T minv, T maxv) { assert(v >= minv && v < maxv); BASISU_NOTE_UNUSED(minv); BASISU_NOTE_UNUSED(maxv); return v; }
  132. template<typename T> inline T open_range_check(T v, T maxv) { assert(v < maxv); BASISU_NOTE_UNUSED(maxv); return v; }
  133. inline uint32_t total_bits(uint32_t v) { uint32_t l = 0; for ( ; v > 0U; ++l) v >>= 1; return l; }
  134. template<typename T> inline T saturate(T val) { return clamp(val, 0.0f, 1.0f); }
  135. template<typename T, typename R> inline void append_vector(T &vec, const R *pObjs, size_t n)
  136. {
  137. if (n)
  138. {
  139. if (vec.size())
  140. {
  141. assert((pObjs + n) <= vec.begin() || (pObjs >= vec.end()));
  142. }
  143. const size_t cur_s = vec.size();
  144. vec.resize(cur_s + n);
  145. memcpy(&vec[cur_s], pObjs, sizeof(R) * n);
  146. }
  147. }
  148. template<typename T> inline void append_vector(T &vec, const T &other_vec)
  149. {
  150. assert(&vec != &other_vec);
  151. if (other_vec.size())
  152. append_vector(vec, &other_vec[0], other_vec.size());
  153. }
  154. template<typename T> inline void vector_ensure_element_is_valid(T &vec, size_t idx)
  155. {
  156. if (idx >= vec.size())
  157. vec.resize(idx + 1);
  158. }
  159. template<typename T> inline void vector_sort(T &vec)
  160. {
  161. if (vec.size())
  162. std::sort(vec.begin(), vec.end());
  163. }
  164. template<typename T, typename U> inline bool unordered_set_contains(T& set, const U&obj)
  165. {
  166. return set.find(obj) != set.end();
  167. }
  168. template<typename T> int vector_find(const T &vec, const typename T::value_type &obj)
  169. {
  170. assert(vec.size() <= INT_MAX);
  171. for (size_t i = 0; i < vec.size(); i++)
  172. if (vec[i] == obj)
  173. return static_cast<int>(i);
  174. return -1;
  175. }
  176. template<typename T> void vector_set_all(T &vec, const typename T::value_type &obj)
  177. {
  178. for (size_t i = 0; i < vec.size(); i++)
  179. vec[i] = obj;
  180. }
  181. inline uint64_t read_be64(const void *p)
  182. {
  183. uint64_t val = 0;
  184. for (uint32_t i = 0; i < 8; i++)
  185. val |= (static_cast<uint64_t>(static_cast<const uint8_t *>(p)[7 - i]) << (i * 8));
  186. return val;
  187. }
  188. inline void write_be64(void *p, uint64_t x)
  189. {
  190. for (uint32_t i = 0; i < 8; i++)
  191. static_cast<uint8_t *>(p)[7 - i] = static_cast<uint8_t>(x >> (i * 8));
  192. }
  193. static inline uint16_t byteswap16(uint16_t x) { return static_cast<uint16_t>((x << 8) | (x >> 8)); }
  194. static inline uint32_t byteswap32(uint32_t x) { return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24)); }
  195. inline uint32_t floor_log2i(uint32_t v)
  196. {
  197. uint32_t b = 0;
  198. for (; v > 1U; ++b)
  199. v >>= 1;
  200. return b;
  201. }
  202. inline uint32_t ceil_log2i(uint32_t v)
  203. {
  204. uint32_t b = floor_log2i(v);
  205. if ((b != 32) && (v > (1U << b)))
  206. ++b;
  207. return b;
  208. }
  209. inline int posmod(int x, int y)
  210. {
  211. if (x >= 0)
  212. return (x < y) ? x : (x % y);
  213. int m = (-x) % y;
  214. return (m != 0) ? (y - m) : m;
  215. }
  216. inline bool do_excl_ranges_overlap(int la, int ha, int lb, int hb)
  217. {
  218. assert(la < ha && lb < hb);
  219. if ((ha <= lb) || (la >= hb)) return false;
  220. return true;
  221. }
  222. static inline uint32_t read_le_dword(const uint8_t *pBytes)
  223. {
  224. return (pBytes[3] << 24U) | (pBytes[2] << 16U) | (pBytes[1] << 8U) | (pBytes[0]);
  225. }
  226. static inline void write_le_dword(uint8_t* pBytes, uint32_t val)
  227. {
  228. pBytes[0] = (uint8_t)val;
  229. pBytes[1] = (uint8_t)(val >> 8U);
  230. pBytes[2] = (uint8_t)(val >> 16U);
  231. pBytes[3] = (uint8_t)(val >> 24U);
  232. }
  233. // Always little endian 1-8 byte unsigned int
  234. template<uint32_t NumBytes>
  235. struct packed_uint
  236. {
  237. uint8_t m_bytes[NumBytes];
  238. inline packed_uint() { static_assert(NumBytes <= sizeof(uint64_t), "Invalid NumBytes"); }
  239. inline packed_uint(uint64_t v) { *this = v; }
  240. inline packed_uint(const packed_uint& other) { *this = other; }
  241. inline packed_uint& operator= (uint64_t v)
  242. {
  243. for (uint32_t i = 0; i < NumBytes; i++)
  244. m_bytes[i] = static_cast<uint8_t>(v >> (i * 8));
  245. return *this;
  246. }
  247. inline packed_uint& operator= (const packed_uint& rhs)
  248. {
  249. memcpy(m_bytes, rhs.m_bytes, sizeof(m_bytes));
  250. return *this;
  251. }
  252. inline operator uint32_t() const
  253. {
  254. switch (NumBytes)
  255. {
  256. case 1:
  257. {
  258. return m_bytes[0];
  259. }
  260. case 2:
  261. {
  262. return (m_bytes[1] << 8U) | m_bytes[0];
  263. }
  264. case 3:
  265. {
  266. return (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | m_bytes[0];
  267. }
  268. case 4:
  269. {
  270. return read_le_dword(m_bytes);
  271. }
  272. case 5:
  273. {
  274. uint32_t l = read_le_dword(m_bytes);
  275. uint32_t h = m_bytes[4];
  276. return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);
  277. }
  278. case 6:
  279. {
  280. uint32_t l = read_le_dword(m_bytes);
  281. uint32_t h = (m_bytes[5] << 8U) | m_bytes[4];
  282. return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);
  283. }
  284. case 7:
  285. {
  286. uint32_t l = read_le_dword(m_bytes);
  287. uint32_t h = (m_bytes[6] << 16U) | (m_bytes[5] << 8U) | m_bytes[4];
  288. return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);
  289. }
  290. case 8:
  291. {
  292. uint32_t l = read_le_dword(m_bytes);
  293. uint32_t h = read_le_dword(m_bytes + 4);
  294. return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);
  295. }
  296. default:
  297. {
  298. assert(0);
  299. return 0;
  300. }
  301. }
  302. }
  303. };
  304. enum eZero { cZero };
  305. enum eNoClamp { cNoClamp };
  306. // Rice/Huffman entropy coding
  307. // This is basically Deflate-style canonical Huffman, except we allow for a lot more symbols.
  308. enum
  309. {
  310. cHuffmanMaxSupportedCodeSize = 16, cHuffmanMaxSupportedInternalCodeSize = 31,
  311. cHuffmanFastLookupBits = 10,
  312. cHuffmanMaxSymsLog2 = 14, cHuffmanMaxSyms = 1 << cHuffmanMaxSymsLog2,
  313. // Small zero runs
  314. cHuffmanSmallZeroRunSizeMin = 3, cHuffmanSmallZeroRunSizeMax = 10, cHuffmanSmallZeroRunExtraBits = 3,
  315. // Big zero run
  316. cHuffmanBigZeroRunSizeMin = 11, cHuffmanBigZeroRunSizeMax = 138, cHuffmanBigZeroRunExtraBits = 7,
  317. // Small non-zero run
  318. cHuffmanSmallRepeatSizeMin = 3, cHuffmanSmallRepeatSizeMax = 6, cHuffmanSmallRepeatExtraBits = 2,
  319. // Big non-zero run
  320. cHuffmanBigRepeatSizeMin = 7, cHuffmanBigRepeatSizeMax = 134, cHuffmanBigRepeatExtraBits = 7,
  321. cHuffmanTotalCodelengthCodes = 21, cHuffmanSmallZeroRunCode = 17, cHuffmanBigZeroRunCode = 18, cHuffmanSmallRepeatCode = 19, cHuffmanBigRepeatCode = 20
  322. };
  323. static const uint8_t g_huffman_sorted_codelength_codes[] = { cHuffmanSmallZeroRunCode, cHuffmanBigZeroRunCode, cHuffmanSmallRepeatCode, cHuffmanBigRepeatCode, 0, 8, 7, 9, 6, 0xA, 5, 0xB, 4, 0xC, 3, 0xD, 2, 0xE, 1, 0xF, 0x10 };
  324. const uint32_t cHuffmanTotalSortedCodelengthCodes = sizeof(g_huffman_sorted_codelength_codes) / sizeof(g_huffman_sorted_codelength_codes[0]);
  325. // GPU texture formats
  326. enum class texture_format
  327. {
  328. cInvalidTextureFormat = -1,
  329. // Block-based formats
  330. cETC1, // ETC1
  331. cETC1S, // ETC1 (subset: diff colors only, no subblocks)
  332. cETC2_RGB, // ETC2 color block (basisu doesn't support ETC2 planar/T/H modes - just basic ETC1)
  333. cETC2_RGBA, // ETC2 EAC alpha block followed by ETC2 color block
  334. cETC2_ALPHA, // ETC2 EAC alpha block
  335. cBC1, // DXT1
  336. cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block)
  337. cBC4, // DXT5A
  338. cBC5, // 3DC/DXN (two BC4/DXT5A blocks)
  339. cBC7,
  340. cASTC4x4, // LDR only
  341. cPVRTC1_4_RGB,
  342. cPVRTC1_4_RGBA,
  343. cATC_RGB,
  344. cATC_RGBA_INTERPOLATED_ALPHA,
  345. cFXT1_RGB,
  346. cPVRTC2_4_RGBA,
  347. cETC2_R11_EAC,
  348. cETC2_RG11_EAC,
  349. cUASTC4x4,
  350. cBC1_NV,
  351. cBC1_AMD,
  352. // Uncompressed/raw pixels
  353. cRGBA32,
  354. cRGB565,
  355. cBGR565,
  356. cRGBA4444,
  357. cABGR4444
  358. };
  359. inline uint32_t get_bytes_per_block(texture_format fmt)
  360. {
  361. switch (fmt)
  362. {
  363. case texture_format::cETC1:
  364. case texture_format::cETC1S:
  365. case texture_format::cETC2_RGB:
  366. case texture_format::cETC2_ALPHA:
  367. case texture_format::cBC1:
  368. case texture_format::cBC1_NV:
  369. case texture_format::cBC1_AMD:
  370. case texture_format::cBC4:
  371. case texture_format::cPVRTC1_4_RGB:
  372. case texture_format::cPVRTC1_4_RGBA:
  373. case texture_format::cATC_RGB:
  374. case texture_format::cPVRTC2_4_RGBA:
  375. case texture_format::cETC2_R11_EAC:
  376. return 8;
  377. case texture_format::cRGBA32:
  378. return sizeof(uint32_t) * 16;
  379. default:
  380. break;
  381. }
  382. return 16;
  383. }
  384. inline uint32_t get_qwords_per_block(texture_format fmt)
  385. {
  386. return get_bytes_per_block(fmt) >> 3;
  387. }
  388. inline uint32_t get_block_width(texture_format fmt)
  389. {
  390. BASISU_NOTE_UNUSED(fmt);
  391. switch (fmt)
  392. {
  393. case texture_format::cFXT1_RGB:
  394. return 8;
  395. default:
  396. break;
  397. }
  398. return 4;
  399. }
  400. inline uint32_t get_block_height(texture_format fmt)
  401. {
  402. BASISU_NOTE_UNUSED(fmt);
  403. return 4;
  404. }
  405. } // namespace basisu