basisu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // basisu.h
  2. // Copyright (C) 2019 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. #ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL
  22. //#define _HAS_ITERATOR_DEBUGGING 0
  23. #if defined(_DEBUG) || defined(DEBUG)
  24. // 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.
  25. #ifndef _ITERATOR_DEBUG_LEVEL
  26. #define _ITERATOR_DEBUG_LEVEL 1
  27. #endif
  28. #ifndef _SECURE_SCL
  29. #define _SECURE_SCL 1
  30. #endif
  31. #else // defined(_DEBUG) || defined(DEBUG)
  32. #ifndef _SECURE_SCL
  33. #define _SECURE_SCL 0
  34. #endif
  35. #ifndef _ITERATOR_DEBUG_LEVEL
  36. #define _ITERATOR_DEBUG_LEVEL 0
  37. #endif
  38. #endif // defined(_DEBUG) || defined(DEBUG)
  39. #ifndef NOMINMAX
  40. #define NOMINMAX
  41. #endif
  42. #endif // BASISU_NO_ITERATOR_DEBUG_LEVEL
  43. #endif // _MSC_VER
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <math.h>
  47. #include <stdarg.h>
  48. #include <string.h>
  49. #include <memory.h>
  50. #include <limits.h>
  51. #include <stdint.h>
  52. #include <algorithm>
  53. #include <limits>
  54. #include <functional>
  55. #include <iterator>
  56. #include <type_traits>
  57. #include <vector>
  58. #include <assert.h>
  59. #include <random>
  60. #ifdef max
  61. #undef max
  62. #endif
  63. #ifdef min
  64. #undef min
  65. #endif
  66. #ifdef _WIN32
  67. #define strcasecmp _stricmp
  68. #endif
  69. // Set to one to enable debug printf()'s when any errors occur, for development/debugging.
  70. #ifndef BASISU_DEVEL_MESSAGES
  71. #define BASISU_DEVEL_MESSAGES 0
  72. #endif
  73. #define BASISU_NOTE_UNUSED(x) (void)(x)
  74. #define BASISU_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  75. #define BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(x) x(const x &) = delete; x& operator= (const x &) = delete;
  76. #define BASISU_ASSUME(x) static_assert(x, #x);
  77. #define BASISU_OFFSETOF(s, m) (uint32_t)(intptr_t)(&((s *)(0))->m)
  78. #define BASISU_STRINGIZE(x) #x
  79. #define BASISU_STRINGIZE2(x) BASISU_STRINGIZE(x)
  80. #if BASISU_DEVEL_MESSAGES
  81. #define BASISU_DEVEL_ERROR(...) do { basisu::debug_printf(__VA_ARGS__); } while(0)
  82. #else
  83. #define BASISU_DEVEL_ERROR(...)
  84. #endif
  85. namespace basisu
  86. {
  87. // Types/utilities
  88. #ifdef _WIN32
  89. const char BASISU_PATH_SEPERATOR_CHAR = '\\';
  90. #else
  91. const char BASISU_PATH_SEPERATOR_CHAR = '/';
  92. #endif
  93. typedef std::vector<uint8_t> uint8_vec;
  94. typedef std::vector<int16_t> int16_vec;
  95. typedef std::vector<uint16_t> uint16_vec;
  96. typedef std::vector<uint32_t> uint_vec;
  97. typedef std::vector<uint64_t> uint64_vec;
  98. typedef std::vector<int> int_vec;
  99. typedef std::vector<bool> bool_vec;
  100. void enable_debug_printf(bool enabled);
  101. void debug_printf(const char *pFmt, ...);
  102. template <typename T> inline void clear_obj(T& obj) { memset(&obj, 0, sizeof(obj)); }
  103. template <typename T0, typename T1> inline T0 lerp(T0 a, T0 b, T1 c) { return a + (b - a) * c; }
  104. template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
  105. template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
  106. template <typename S> inline S minimum(S a, S b) { return (a < b) ? a : b; }
  107. template <typename S> inline S minimum(S a, S b, S c) { return minimum(minimum(a, b), c); }
  108. template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }
  109. inline uint32_t iabs(int32_t i) { return (i < 0) ? static_cast<uint32_t>(-i) : static_cast<uint32_t>(i); }
  110. inline uint64_t iabs64(int64_t i) { return (i < 0) ? static_cast<uint64_t>(-i) : static_cast<uint64_t>(i); }
  111. template<typename T> inline void clear_vector(T &vec) { vec.erase(vec.begin(), vec.end()); }
  112. 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]; }
  113. template<typename S> inline S square(S val) { return val * val; }
  114. inline bool is_pow2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
  115. inline bool is_pow2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
  116. template<typename T> inline T open_range_check(T v, T minv, T maxv) { assert(v >= minv && v < maxv); return v; }
  117. template<typename T> inline T open_range_check(T v, T maxv) { assert(v < maxv); BASISU_NOTE_UNUSED(maxv); return v; }
  118. inline uint32_t total_bits(uint32_t v) { uint32_t l = 0; for ( ; v > 0U; ++l) v >>= 1; return l; }
  119. template<typename T> inline T saturate(T val) { return clamp(val, 0.0f, 1.0f); }
  120. template<typename T, typename R> inline void append_vector(T &vec, const R *pObjs, size_t n)
  121. {
  122. if (n)
  123. {
  124. const size_t cur_s = vec.size();
  125. vec.resize(cur_s + n);
  126. memcpy(&vec[cur_s], pObjs, sizeof(R) * n);
  127. }
  128. }
  129. template<typename T> inline void append_vector(T &vec, const T &other_vec)
  130. {
  131. if (other_vec.size())
  132. append_vector(vec, &other_vec[0], other_vec.size());
  133. }
  134. template<typename T> inline void vector_ensure_element_is_valid(T &vec, size_t idx)
  135. {
  136. if (idx >= vec.size())
  137. vec.resize(idx + 1);
  138. }
  139. template<typename T> inline void vector_sort(T &vec)
  140. {
  141. if (vec.size())
  142. std::sort(vec.begin(), vec.end());
  143. }
  144. template<typename T, typename U> inline bool unordered_set_contains(T& set, const U&obj)
  145. {
  146. return set.find(obj) != set.end();
  147. }
  148. template<typename T> int vector_find(const T &vec, const typename T::value_type &obj)
  149. {
  150. assert(vec.size() <= INT_MAX);
  151. for (size_t i = 0; i < vec.size(); i++)
  152. if (vec[i] == obj)
  153. return static_cast<int>(i);
  154. return -1;
  155. }
  156. template<typename T> void vector_set_all(T &vec, const typename T::value_type &obj)
  157. {
  158. for (size_t i = 0; i < vec.size(); i++)
  159. vec[i] = obj;
  160. }
  161. inline uint64_t read_be64(const void *p)
  162. {
  163. uint64_t val = 0;
  164. for (uint32_t i = 0; i < 8; i++)
  165. val |= (static_cast<uint64_t>(static_cast<const uint8_t *>(p)[7 - i]) << (i * 8));
  166. return val;
  167. }
  168. inline void write_be64(void *p, uint64_t x)
  169. {
  170. for (uint32_t i = 0; i < 8; i++)
  171. static_cast<uint8_t *>(p)[7 - i] = static_cast<uint8_t>(x >> (i * 8));
  172. }
  173. static inline uint16_t byteswap16(uint16_t x) { return static_cast<uint16_t>((x << 8) | (x >> 8)); }
  174. static inline uint32_t byteswap32(uint32_t x) { return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24)); }
  175. inline uint32_t floor_log2i(uint32_t v)
  176. {
  177. uint32_t b = 0;
  178. for (; v > 1U; ++b)
  179. v >>= 1;
  180. return b;
  181. }
  182. inline uint32_t ceil_log2i(uint32_t v)
  183. {
  184. uint32_t b = floor_log2i(v);
  185. if ((b != 32) && (v > (1U << b)))
  186. ++b;
  187. return b;
  188. }
  189. inline int posmod(int x, int y)
  190. {
  191. if (x >= 0)
  192. return (x < y) ? x : (x % y);
  193. int m = (-x) % y;
  194. return (m != 0) ? (y - m) : m;
  195. }
  196. inline bool do_excl_ranges_overlap(int la, int ha, int lb, int hb)
  197. {
  198. assert(la < ha && lb < hb);
  199. if ((ha <= lb) || (la >= hb)) return false;
  200. return true;
  201. }
  202. // Always little endian 2-4 byte unsigned int
  203. template<uint32_t NumBytes>
  204. struct packed_uint
  205. {
  206. uint8_t m_bytes[NumBytes];
  207. inline packed_uint() { static_assert(NumBytes <= 4, "NumBytes <= 4"); }
  208. inline packed_uint(uint32_t v) { *this = v; }
  209. inline packed_uint(const packed_uint& other) { *this = other; }
  210. inline packed_uint& operator= (uint32_t v) { for (uint32_t i = 0; i < NumBytes; i++) m_bytes[i] = static_cast<uint8_t>(v >> (i * 8)); return *this; }
  211. inline operator uint32_t() const
  212. {
  213. switch (NumBytes)
  214. {
  215. case 1: return m_bytes[0];
  216. case 2: return (m_bytes[1] << 8U) | m_bytes[0];
  217. case 3: return (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | (m_bytes[0]);
  218. default: return (m_bytes[3] << 24U) | (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | (m_bytes[0]);
  219. }
  220. }
  221. };
  222. enum eZero { cZero };
  223. enum eNoClamp { cNoClamp };
  224. // Rice/Huffman entropy coding
  225. // This is basically Deflate-style canonical Huffman, except we allow for a lot more symbols.
  226. enum
  227. {
  228. cHuffmanMaxSupportedCodeSize = 16, cHuffmanMaxSupportedInternalCodeSize = 31,
  229. cHuffmanFastLookupBits = 10, cHuffmanFastLookupSize = 1 << cHuffmanFastLookupBits,
  230. cHuffmanMaxSymsLog2 = 14, cHuffmanMaxSyms = 1 << cHuffmanMaxSymsLog2,
  231. // Small zero runs
  232. cHuffmanSmallZeroRunSizeMin = 3, cHuffmanSmallZeroRunSizeMax = 10, cHuffmanSmallZeroRunExtraBits = 3,
  233. // Big zero run
  234. cHuffmanBigZeroRunSizeMin = 11, cHuffmanBigZeroRunSizeMax = 138, cHuffmanBigZeroRunExtraBits = 7,
  235. // Small non-zero run
  236. cHuffmanSmallRepeatSizeMin = 3, cHuffmanSmallRepeatSizeMax = 6, cHuffmanSmallRepeatExtraBits = 2,
  237. // Big non-zero run
  238. cHuffmanBigRepeatSizeMin = 7, cHuffmanBigRepeatSizeMax = 134, cHuffmanBigRepeatExtraBits = 7,
  239. cHuffmanTotalCodelengthCodes = 21, cHuffmanSmallZeroRunCode = 17, cHuffmanBigZeroRunCode = 18, cHuffmanSmallRepeatCode = 19, cHuffmanBigRepeatCode = 20
  240. };
  241. 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 };
  242. const uint32_t cHuffmanTotalSortedCodelengthCodes = sizeof(g_huffman_sorted_codelength_codes) / sizeof(g_huffman_sorted_codelength_codes[0]);
  243. // GPU texture formats
  244. enum class texture_format
  245. {
  246. cInvalidTextureFormat = -1,
  247. // Block-based formats
  248. cETC1, // ETC1
  249. cETC1S, // ETC1 (subset: diff colors only, no subblocks)
  250. cETC2_RGB, // ETC2 color block
  251. cETC2_RGBA, // ETC2 alpha block followed by ETC2 color block
  252. cETC2_ALPHA, // ETC2 EAC alpha block
  253. cBC1, // DXT1
  254. cBC3, // DXT5 (DXT5A block followed by a DXT1 block)
  255. cBC4, // DXT5A
  256. cBC5, // 3DC/DXN (two DXT5A blocks)
  257. cBC7,
  258. cASTC4x4,
  259. cPVRTC1_4_RGB,
  260. cPVRTC1_4_RGBA,
  261. cATC_RGB,
  262. cATC_RGBA_INTERPOLATED_ALPHA,
  263. cFXT1_RGB,
  264. cPVRTC2_4_RGBA,
  265. cETC2_R11_EAC,
  266. cETC2_RG11_EAC,
  267. // Uncompressed/raw pixels
  268. cRGBA32,
  269. cRGB565,
  270. cBGR565,
  271. cRGBA4444,
  272. cABGR4444
  273. };
  274. inline uint32_t get_bytes_per_block(texture_format fmt)
  275. {
  276. switch (fmt)
  277. {
  278. case texture_format::cETC1:
  279. case texture_format::cETC1S:
  280. case texture_format::cETC2_RGB:
  281. case texture_format::cETC2_ALPHA:
  282. case texture_format::cBC1:
  283. case texture_format::cBC4:
  284. case texture_format::cPVRTC1_4_RGB:
  285. case texture_format::cPVRTC1_4_RGBA:
  286. case texture_format::cATC_RGB:
  287. case texture_format::cPVRTC2_4_RGBA:
  288. case texture_format::cETC2_R11_EAC:
  289. return 8;
  290. case texture_format::cRGBA32:
  291. return sizeof(uint32_t) * 16;
  292. default:
  293. break;
  294. }
  295. return 16;
  296. }
  297. inline uint32_t get_qwords_per_block(texture_format fmt)
  298. {
  299. return get_bytes_per_block(fmt) >> 3;
  300. }
  301. inline uint32_t get_block_width(texture_format fmt)
  302. {
  303. BASISU_NOTE_UNUSED(fmt);
  304. switch (fmt)
  305. {
  306. case texture_format::cFXT1_RGB:
  307. return 8;
  308. default:
  309. break;
  310. }
  311. return 4;
  312. }
  313. inline uint32_t get_block_height(texture_format fmt)
  314. {
  315. BASISU_NOTE_UNUSED(fmt);
  316. return 4;
  317. }
  318. } // namespace basisu