android_astc_decomp.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. // File: android_astc_decomp.cpp
  2. /*-------------------------------------------------------------------------
  3. * drawElements Quality Program Tester Core
  4. * ----------------------------------------
  5. *
  6. * Copyright 2016 The Android Open Source Project
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * rg: Removed external dependencies, minor fix to decompress() so it converts non-sRGB
  21. * output to 8-bits correctly. I've compared this decoder's output
  22. * vs. astc-codec with random inputs.
  23. *
  24. *//*!
  25. * \file
  26. * \brief ASTC Utilities.
  27. *//*--------------------------------------------------------------------*/
  28. #include "android_astc_decomp.h"
  29. #include <assert.h>
  30. #include <algorithm>
  31. #include <fenv.h>
  32. #include <math.h>
  33. #define DE_LENGTH_OF_ARRAY(x) (sizeof(x)/sizeof(x[0]))
  34. #define DE_UNREF(x) (void)x
  35. typedef uint8_t deUint8;
  36. typedef int8_t deInt8;
  37. typedef uint32_t deUint32;
  38. typedef int32_t deInt32;
  39. typedef uint16_t deUint16;
  40. typedef int16_t deInt16;
  41. typedef int64_t deInt64;
  42. typedef uint64_t deUint64;
  43. #define DE_ASSERT assert
  44. #ifdef _MSC_VER
  45. #pragma warning (disable:4505) // unreferenced local function has been removed
  46. #elif defined(__GNUC__)
  47. #pragma GCC diagnostic push
  48. #pragma GCC diagnostic ignored "-Wunused-function"
  49. #endif
  50. namespace basisu_astc
  51. {
  52. template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
  53. template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
  54. template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }
  55. static bool inBounds(int v, int l, int h)
  56. {
  57. return (v >= l) && (v < h);
  58. }
  59. static bool inRange(int v, int l, int h)
  60. {
  61. return (v >= l) && (v <= h);
  62. }
  63. template<typename T>
  64. static inline T max(T a, T b)
  65. {
  66. return (a > b) ? a : b;
  67. }
  68. template<typename T>
  69. static inline T min(T a, T b)
  70. {
  71. return (a < b) ? a : b;
  72. }
  73. template<typename T>
  74. static inline T clamp(T a, T l, T h)
  75. {
  76. if (a < l)
  77. return l;
  78. else if (a > h)
  79. return h;
  80. return a;
  81. }
  82. struct UVec4
  83. {
  84. uint32_t m_c[4];
  85. UVec4()
  86. {
  87. m_c[0] = 0;
  88. m_c[1] = 0;
  89. m_c[2] = 0;
  90. m_c[3] = 0;
  91. }
  92. UVec4(uint32_t x, uint32_t y, uint32_t z, uint32_t w)
  93. {
  94. m_c[0] = x;
  95. m_c[1] = y;
  96. m_c[2] = z;
  97. m_c[3] = w;
  98. }
  99. uint32_t x() const { return m_c[0]; }
  100. uint32_t y() const { return m_c[1]; }
  101. uint32_t z() const { return m_c[2]; }
  102. uint32_t w() const { return m_c[3]; }
  103. uint32_t& x() { return m_c[0]; }
  104. uint32_t& y() { return m_c[1]; }
  105. uint32_t& z() { return m_c[2]; }
  106. uint32_t& w() { return m_c[3]; }
  107. uint32_t operator[] (uint32_t idx) const { assert(idx < 4); return m_c[idx]; }
  108. uint32_t& operator[] (uint32_t idx) { assert(idx < 4); return m_c[idx]; }
  109. };
  110. struct IVec4
  111. {
  112. int32_t m_c[4];
  113. IVec4()
  114. {
  115. m_c[0] = 0;
  116. m_c[1] = 0;
  117. m_c[2] = 0;
  118. m_c[3] = 0;
  119. }
  120. IVec4(int32_t x, int32_t y, int32_t z, int32_t w)
  121. {
  122. m_c[0] = x;
  123. m_c[1] = y;
  124. m_c[2] = z;
  125. m_c[3] = w;
  126. }
  127. int32_t x() const { return m_c[0]; }
  128. int32_t y() const { return m_c[1]; }
  129. int32_t z() const { return m_c[2]; }
  130. int32_t w() const { return m_c[3]; }
  131. int32_t& x() { return m_c[0]; }
  132. int32_t& y() { return m_c[1]; }
  133. int32_t& z() { return m_c[2]; }
  134. int32_t& w() { return m_c[3]; }
  135. UVec4 asUint() const
  136. {
  137. return UVec4(maximum(0, m_c[0]), maximum(0, m_c[1]), maximum(0, m_c[2]), maximum(0, m_c[3]));
  138. }
  139. int32_t operator[] (uint32_t idx) const { assert(idx < 4); return m_c[idx]; }
  140. int32_t& operator[] (uint32_t idx) { assert(idx < 4); return m_c[idx]; }
  141. };
  142. struct IVec3
  143. {
  144. int32_t m_c[3];
  145. IVec3()
  146. {
  147. m_c[0] = 0;
  148. m_c[1] = 0;
  149. m_c[2] = 0;
  150. }
  151. IVec3(int32_t x, int32_t y, int32_t z)
  152. {
  153. m_c[0] = x;
  154. m_c[1] = y;
  155. m_c[2] = z;
  156. }
  157. int32_t x() const { return m_c[0]; }
  158. int32_t y() const { return m_c[1]; }
  159. int32_t z() const { return m_c[2]; }
  160. int32_t& x() { return m_c[0]; }
  161. int32_t& y() { return m_c[1]; }
  162. int32_t& z() { return m_c[2]; }
  163. int32_t operator[] (uint32_t idx) const { assert(idx < 3); return m_c[idx]; }
  164. int32_t& operator[] (uint32_t idx) { assert(idx < 3); return m_c[idx]; }
  165. };
  166. static uint32_t deDivRoundUp32(uint32_t a, uint32_t b)
  167. {
  168. return (a + b - 1) / b;
  169. }
  170. static bool deInBounds32(uint32_t v, uint32_t l, uint32_t h)
  171. {
  172. return (v >= l) && (v < h);
  173. }
  174. namespace astc
  175. {
  176. using std::vector;
  177. namespace
  178. {
  179. // Common utilities
  180. enum
  181. {
  182. MAX_BLOCK_WIDTH = 12,
  183. MAX_BLOCK_HEIGHT = 12
  184. };
  185. inline deUint32 getBit (deUint32 src, int ndx)
  186. {
  187. DE_ASSERT(basisu_astc::inBounds(ndx, 0, 32));
  188. return (src >> ndx) & 1;
  189. }
  190. inline deUint32 getBits (deUint32 src, int low, int high)
  191. {
  192. const int numBits = (high-low) + 1;
  193. DE_ASSERT(basisu_astc::inRange(numBits, 1, 32));
  194. if (numBits < 32)
  195. return (deUint32)((src >> low) & ((1u<<numBits)-1));
  196. else
  197. return (deUint32)((src >> low) & 0xFFFFFFFFu);
  198. }
  199. inline bool isBitSet (deUint32 src, int ndx)
  200. {
  201. return getBit(src, ndx) != 0;
  202. }
  203. inline deUint32 reverseBits (deUint32 src, int numBits)
  204. {
  205. DE_ASSERT(basisu_astc::inRange(numBits, 0, 32));
  206. deUint32 result = 0;
  207. for (int i = 0; i < numBits; i++)
  208. result |= ((src >> i) & 1) << (numBits-1-i);
  209. return result;
  210. }
  211. inline deUint32 bitReplicationScale (deUint32 src, int numSrcBits, int numDstBits)
  212. {
  213. DE_ASSERT(numSrcBits <= numDstBits);
  214. DE_ASSERT((src & ((1<<numSrcBits)-1)) == src);
  215. deUint32 dst = 0;
  216. for (int shift = numDstBits-numSrcBits; shift > -numSrcBits; shift -= numSrcBits)
  217. dst |= (shift >= 0) ? (src << shift) : (src >> -shift);
  218. return dst;
  219. }
  220. inline deInt32 signExtend (deInt32 src, int numSrcBits)
  221. {
  222. DE_ASSERT(basisu_astc::inRange(numSrcBits, 2, 31));
  223. const bool negative = (src & (1 << (numSrcBits-1))) != 0;
  224. return src | (negative ? ~((1 << numSrcBits) - 1) : 0);
  225. }
  226. typedef uint16_t deFloat16;
  227. inline bool isFloat16InfOrNan (deFloat16 v)
  228. {
  229. return getBits(v, 10, 14) == 31;
  230. }
  231. float deFloat16To32(deFloat16 val16)
  232. {
  233. deUint32 sign;
  234. deUint32 expotent;
  235. deUint32 mantissa;
  236. union
  237. {
  238. float f;
  239. deUint32 u;
  240. } x;
  241. x.u = 0u;
  242. sign = ((deUint32)val16 >> 15u) & 0x00000001u;
  243. expotent = ((deUint32)val16 >> 10u) & 0x0000001fu;
  244. mantissa = (deUint32)val16 & 0x000003ffu;
  245. if (expotent == 0u)
  246. {
  247. if (mantissa == 0u)
  248. {
  249. /* +/- 0 */
  250. x.u = sign << 31u;
  251. return x.f;
  252. }
  253. else
  254. {
  255. /* Denormalized, normalize it. */
  256. while (!(mantissa & 0x00000400u))
  257. {
  258. mantissa <<= 1u;
  259. expotent -= 1u;
  260. }
  261. expotent += 1u;
  262. mantissa &= ~0x00000400u;
  263. }
  264. }
  265. else if (expotent == 31u)
  266. {
  267. if (mantissa == 0u)
  268. {
  269. /* +/- InF */
  270. x.u = (sign << 31u) | 0x7f800000u;
  271. return x.f;
  272. }
  273. else
  274. {
  275. /* +/- NaN */
  276. x.u = (sign << 31u) | 0x7f800000u | (mantissa << 13u);
  277. return x.f;
  278. }
  279. }
  280. expotent = expotent + (127u - 15u);
  281. mantissa = mantissa << 13u;
  282. x.u = (sign << 31u) | (expotent << 23u) | mantissa;
  283. return x.f;
  284. }
  285. enum ISEMode
  286. {
  287. ISEMODE_TRIT = 0,
  288. ISEMODE_QUINT,
  289. ISEMODE_PLAIN_BIT,
  290. ISEMODE_LAST
  291. };
  292. struct ISEParams
  293. {
  294. ISEMode mode;
  295. int numBits;
  296. ISEParams (ISEMode mode_, int numBits_) : mode(mode_), numBits(numBits_) {}
  297. };
  298. inline int computeNumRequiredBits (const ISEParams& iseParams, int numValues)
  299. {
  300. switch (iseParams.mode)
  301. {
  302. case ISEMODE_TRIT: return deDivRoundUp32(numValues*8, 5) + numValues*iseParams.numBits;
  303. case ISEMODE_QUINT: return deDivRoundUp32(numValues*7, 3) + numValues*iseParams.numBits;
  304. case ISEMODE_PLAIN_BIT: return numValues*iseParams.numBits;
  305. default:
  306. DE_ASSERT(false);
  307. return -1;
  308. }
  309. }
  310. ISEParams computeMaximumRangeISEParams (int numAvailableBits, int numValuesInSequence)
  311. {
  312. int curBitsForTritMode = 6;
  313. int curBitsForQuintMode = 5;
  314. int curBitsForPlainBitMode = 8;
  315. while (true)
  316. {
  317. DE_ASSERT(curBitsForTritMode > 0 || curBitsForQuintMode > 0 || curBitsForPlainBitMode > 0);
  318. const int tritRange = (curBitsForTritMode > 0) ? (3 << curBitsForTritMode) - 1 : -1;
  319. const int quintRange = (curBitsForQuintMode > 0) ? (5 << curBitsForQuintMode) - 1 : -1;
  320. const int plainBitRange = (curBitsForPlainBitMode > 0) ? (1 << curBitsForPlainBitMode) - 1 : -1;
  321. const int maxRange = basisu_astc::max(basisu_astc::max(tritRange, quintRange), plainBitRange);
  322. if (maxRange == tritRange)
  323. {
  324. const ISEParams params(ISEMODE_TRIT, curBitsForTritMode);
  325. if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
  326. return ISEParams(ISEMODE_TRIT, curBitsForTritMode);
  327. curBitsForTritMode--;
  328. }
  329. else if (maxRange == quintRange)
  330. {
  331. const ISEParams params(ISEMODE_QUINT, curBitsForQuintMode);
  332. if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
  333. return ISEParams(ISEMODE_QUINT, curBitsForQuintMode);
  334. curBitsForQuintMode--;
  335. }
  336. else
  337. {
  338. const ISEParams params(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
  339. DE_ASSERT(maxRange == plainBitRange);
  340. if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
  341. return ISEParams(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
  342. curBitsForPlainBitMode--;
  343. }
  344. }
  345. }
  346. inline int computeNumColorEndpointValues (deUint32 endpointMode)
  347. {
  348. DE_ASSERT(endpointMode < 16);
  349. return (endpointMode/4 + 1) * 2;
  350. }
  351. // Decompression utilities
  352. enum DecompressResult
  353. {
  354. DECOMPRESS_RESULT_VALID_BLOCK = 0, //!< Decompressed valid block
  355. DECOMPRESS_RESULT_ERROR, //!< Encountered error while decompressing, error color written
  356. DECOMPRESS_RESULT_LAST
  357. };
  358. // A helper for getting bits from a 128-bit block.
  359. class Block128
  360. {
  361. private:
  362. typedef deUint64 Word;
  363. enum
  364. {
  365. WORD_BYTES = sizeof(Word),
  366. WORD_BITS = 8*WORD_BYTES,
  367. NUM_WORDS = 128 / WORD_BITS
  368. };
  369. //DE_STATIC_ASSERT(128 % WORD_BITS == 0);
  370. public:
  371. Block128 (const deUint8* src)
  372. {
  373. for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
  374. {
  375. m_words[wordNdx] = 0;
  376. for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
  377. m_words[wordNdx] |= (Word)src[wordNdx*WORD_BYTES + byteNdx] << (8*byteNdx);
  378. }
  379. }
  380. deUint32 getBit (int ndx) const
  381. {
  382. DE_ASSERT(basisu_astc::inBounds(ndx, 0, 128));
  383. return (m_words[ndx / WORD_BITS] >> (ndx % WORD_BITS)) & 1;
  384. }
  385. deUint32 getBits (int low, int high) const
  386. {
  387. DE_ASSERT(basisu_astc::inBounds(low, 0, 128));
  388. DE_ASSERT(basisu_astc::inBounds(high, 0, 128));
  389. DE_ASSERT(basisu_astc::inRange(high-low+1, 0, 32));
  390. if (high-low+1 == 0)
  391. return 0;
  392. const int word0Ndx = low / WORD_BITS;
  393. const int word1Ndx = high / WORD_BITS;
  394. // \note "foo << bar << 1" done instead of "foo << (bar+1)" to avoid overflow, i.e. shift amount being too big.
  395. if (word0Ndx == word1Ndx)
  396. return (deUint32)((m_words[word0Ndx] & ((((Word)1 << high%WORD_BITS << 1) - 1))) >> ((Word)low % WORD_BITS));
  397. else
  398. {
  399. DE_ASSERT(word1Ndx == word0Ndx + 1);
  400. return (deUint32)(m_words[word0Ndx] >> (low%WORD_BITS)) |
  401. (deUint32)((m_words[word1Ndx] & (((Word)1 << high%WORD_BITS << 1) - 1)) << (high-low - high%WORD_BITS));
  402. }
  403. }
  404. bool isBitSet (int ndx) const
  405. {
  406. DE_ASSERT(basisu_astc::inBounds(ndx, 0, 128));
  407. return getBit(ndx) != 0;
  408. }
  409. private:
  410. Word m_words[NUM_WORDS];
  411. };
  412. // A helper for sequential access into a Block128.
  413. class BitAccessStream
  414. {
  415. public:
  416. BitAccessStream (const Block128& src, int startNdxInSrc, int length, bool forward)
  417. : m_src (src)
  418. , m_startNdxInSrc (startNdxInSrc)
  419. , m_length (length)
  420. , m_forward (forward)
  421. , m_ndx (0)
  422. {
  423. }
  424. // Get the next num bits. Bits at positions greater than or equal to m_length are zeros.
  425. deUint32 getNext (int num)
  426. {
  427. if (num == 0 || m_ndx >= m_length)
  428. return 0;
  429. const int end = m_ndx + num;
  430. const int numBitsFromSrc = basisu_astc::max(0, basisu_astc::min(m_length, end) - m_ndx);
  431. const int low = m_ndx;
  432. const int high = m_ndx + numBitsFromSrc - 1;
  433. m_ndx += num;
  434. return m_forward ? m_src.getBits(m_startNdxInSrc + low, m_startNdxInSrc + high)
  435. : reverseBits(m_src.getBits(m_startNdxInSrc - high, m_startNdxInSrc - low), numBitsFromSrc);
  436. }
  437. private:
  438. const Block128& m_src;
  439. const int m_startNdxInSrc;
  440. const int m_length;
  441. const bool m_forward;
  442. int m_ndx;
  443. };
  444. struct ISEDecodedResult
  445. {
  446. deUint32 m;
  447. deUint32 tq; //!< Trit or quint value, depending on ISE mode.
  448. deUint32 v;
  449. };
  450. // Data from an ASTC block's "block mode" part (i.e. bits [0,10]).
  451. struct ASTCBlockMode
  452. {
  453. bool isError;
  454. // \note Following fields only relevant if !isError.
  455. bool isVoidExtent;
  456. // \note Following fields only relevant if !isVoidExtent.
  457. bool isDualPlane;
  458. int weightGridWidth;
  459. int weightGridHeight;
  460. ISEParams weightISEParams;
  461. ASTCBlockMode (void)
  462. : isError (true)
  463. , isVoidExtent (true)
  464. , isDualPlane (true)
  465. , weightGridWidth (-1)
  466. , weightGridHeight (-1)
  467. , weightISEParams (ISEMODE_LAST, -1)
  468. {
  469. }
  470. };
  471. inline int computeNumWeights (const ASTCBlockMode& mode)
  472. {
  473. return mode.weightGridWidth * mode.weightGridHeight * (mode.isDualPlane ? 2 : 1);
  474. }
  475. struct ColorEndpointPair
  476. {
  477. UVec4 e0;
  478. UVec4 e1;
  479. };
  480. struct TexelWeightPair
  481. {
  482. deUint32 w[2];
  483. };
  484. ASTCBlockMode getASTCBlockMode (deUint32 blockModeData)
  485. {
  486. ASTCBlockMode blockMode;
  487. blockMode.isError = true; // \note Set to false later, if not error.
  488. blockMode.isVoidExtent = getBits(blockModeData, 0, 8) == 0x1fc;
  489. if (!blockMode.isVoidExtent)
  490. {
  491. if ((getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 6, 8) == 7) || getBits(blockModeData, 0, 3) == 0)
  492. return blockMode; // Invalid ("reserved").
  493. deUint32 r = (deUint32)-1; // \note Set in the following branches.
  494. if (getBits(blockModeData, 0, 1) == 0)
  495. {
  496. const deUint32 r0 = getBit(blockModeData, 4);
  497. const deUint32 r1 = getBit(blockModeData, 2);
  498. const deUint32 r2 = getBit(blockModeData, 3);
  499. const deUint32 i78 = getBits(blockModeData, 7, 8);
  500. r = (r2 << 2) | (r1 << 1) | (r0 << 0);
  501. if (i78 == 3)
  502. {
  503. const bool i5 = isBitSet(blockModeData, 5);
  504. blockMode.weightGridWidth = i5 ? 10 : 6;
  505. blockMode.weightGridHeight = i5 ? 6 : 10;
  506. }
  507. else
  508. {
  509. const deUint32 a = getBits(blockModeData, 5, 6);
  510. switch (i78)
  511. {
  512. case 0: blockMode.weightGridWidth = 12; blockMode.weightGridHeight = a + 2; break;
  513. case 1: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = 12; break;
  514. case 2: blockMode.weightGridWidth = a + 6; blockMode.weightGridHeight = getBits(blockModeData, 9, 10) + 6; break;
  515. default: DE_ASSERT(false);
  516. }
  517. }
  518. }
  519. else
  520. {
  521. const deUint32 r0 = getBit(blockModeData, 4);
  522. const deUint32 r1 = getBit(blockModeData, 0);
  523. const deUint32 r2 = getBit(blockModeData, 1);
  524. const deUint32 i23 = getBits(blockModeData, 2, 3);
  525. const deUint32 a = getBits(blockModeData, 5, 6);
  526. r = (r2 << 2) | (r1 << 1) | (r0 << 0);
  527. if (i23 == 3)
  528. {
  529. const deUint32 b = getBit(blockModeData, 7);
  530. const bool i8 = isBitSet(blockModeData, 8);
  531. blockMode.weightGridWidth = i8 ? b+2 : a+2;
  532. blockMode.weightGridHeight = i8 ? a+2 : b+6;
  533. }
  534. else
  535. {
  536. const deUint32 b = getBits(blockModeData, 7, 8);
  537. switch (i23)
  538. {
  539. case 0: blockMode.weightGridWidth = b + 4; blockMode.weightGridHeight = a + 2; break;
  540. case 1: blockMode.weightGridWidth = b + 8; blockMode.weightGridHeight = a + 2; break;
  541. case 2: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = b + 8; break;
  542. default: DE_ASSERT(false);
  543. }
  544. }
  545. }
  546. const bool zeroDH = getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 7, 8) == 2;
  547. const bool h = zeroDH ? 0 : isBitSet(blockModeData, 9);
  548. blockMode.isDualPlane = zeroDH ? 0 : isBitSet(blockModeData, 10);
  549. {
  550. ISEMode& m = blockMode.weightISEParams.mode;
  551. int& b = blockMode.weightISEParams.numBits;
  552. m = ISEMODE_PLAIN_BIT;
  553. b = 0;
  554. if (h)
  555. {
  556. switch (r)
  557. {
  558. case 2: m = ISEMODE_QUINT; b = 1; break;
  559. case 3: m = ISEMODE_TRIT; b = 2; break;
  560. case 4: b = 4; break;
  561. case 5: m = ISEMODE_QUINT; b = 2; break;
  562. case 6: m = ISEMODE_TRIT; b = 3; break;
  563. case 7: b = 5; break;
  564. default: DE_ASSERT(false);
  565. }
  566. }
  567. else
  568. {
  569. switch (r)
  570. {
  571. case 2: b = 1; break;
  572. case 3: m = ISEMODE_TRIT; break;
  573. case 4: b = 2; break;
  574. case 5: m = ISEMODE_QUINT; break;
  575. case 6: m = ISEMODE_TRIT; b = 1; break;
  576. case 7: b = 3; break;
  577. default: DE_ASSERT(false);
  578. }
  579. }
  580. }
  581. }
  582. blockMode.isError = false;
  583. return blockMode;
  584. }
  585. inline void setASTCErrorColorBlock (void* dst, int blockWidth, int blockHeight, bool isSRGB)
  586. {
  587. if (isSRGB)
  588. {
  589. deUint8* const dstU = (deUint8*)dst;
  590. for (int i = 0; i < blockWidth*blockHeight; i++)
  591. {
  592. dstU[4*i + 0] = 0xff;
  593. dstU[4*i + 1] = 0;
  594. dstU[4*i + 2] = 0xff;
  595. dstU[4*i + 3] = 0xff;
  596. }
  597. }
  598. else
  599. {
  600. float* const dstF = (float*)dst;
  601. for (int i = 0; i < blockWidth*blockHeight; i++)
  602. {
  603. dstF[4*i + 0] = 1.0f;
  604. dstF[4*i + 1] = 0.0f;
  605. dstF[4*i + 2] = 1.0f;
  606. dstF[4*i + 3] = 1.0f;
  607. }
  608. }
  609. }
  610. DecompressResult decodeVoidExtentBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode)
  611. {
  612. const deUint32 minSExtent = blockData.getBits(12, 24);
  613. const deUint32 maxSExtent = blockData.getBits(25, 37);
  614. const deUint32 minTExtent = blockData.getBits(38, 50);
  615. const deUint32 maxTExtent = blockData.getBits(51, 63);
  616. const bool allExtentsAllOnes = (minSExtent == 0x1fff) && (maxSExtent == 0x1fff) && (minTExtent == 0x1fff) && (maxTExtent == 0x1fff);
  617. const bool isHDRBlock = blockData.isBitSet(9);
  618. if ((isLDRMode && isHDRBlock) || (!allExtentsAllOnes && (minSExtent >= maxSExtent || minTExtent >= maxTExtent)))
  619. {
  620. setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
  621. return DECOMPRESS_RESULT_ERROR;
  622. }
  623. const deUint32 rgba[4] =
  624. {
  625. blockData.getBits(64, 79),
  626. blockData.getBits(80, 95),
  627. blockData.getBits(96, 111),
  628. blockData.getBits(112, 127)
  629. };
  630. if (isSRGB)
  631. {
  632. deUint8* const dstU = (deUint8*)dst;
  633. for (int i = 0; i < blockWidth * blockHeight; i++)
  634. {
  635. for (int c = 0; c < 4; c++)
  636. dstU[i * 4 + c] = (deUint8)((rgba[c] & 0xff00) >> 8);
  637. }
  638. }
  639. else
  640. {
  641. float* const dstF = (float*)dst;
  642. if (isHDRBlock)
  643. {
  644. for (int c = 0; c < 4; c++)
  645. {
  646. if (isFloat16InfOrNan((deFloat16)rgba[c]))
  647. {
  648. //throw InternalError("Infinity or NaN color component in HDR void extent block in ASTC texture (behavior undefined by ASTC specification)");
  649. setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
  650. return DECOMPRESS_RESULT_ERROR;
  651. }
  652. }
  653. for (int i = 0; i < blockWidth * blockHeight; i++)
  654. {
  655. for (int c = 0; c < 4; c++)
  656. dstF[i * 4 + c] = deFloat16To32((deFloat16)rgba[c]);
  657. }
  658. }
  659. else
  660. {
  661. for (int i = 0; i < blockWidth * blockHeight; i++)
  662. {
  663. for (int c = 0; c < 4; c++)
  664. dstF[i * 4 + c] = (rgba[c] == 65535) ? 1.0f : ((float)rgba[c] / 65536.0f);
  665. }
  666. }
  667. }
  668. return DECOMPRESS_RESULT_VALID_BLOCK;
  669. }
  670. void decodeColorEndpointModes (deUint32* endpointModesDst, const Block128& blockData, int numPartitions, int extraCemBitsStart)
  671. {
  672. if (numPartitions == 1)
  673. endpointModesDst[0] = blockData.getBits(13, 16);
  674. else
  675. {
  676. const deUint32 highLevelSelector = blockData.getBits(23, 24);
  677. if (highLevelSelector == 0)
  678. {
  679. const deUint32 mode = blockData.getBits(25, 28);
  680. for (int i = 0; i < numPartitions; i++)
  681. endpointModesDst[i] = mode;
  682. }
  683. else
  684. {
  685. for (int partNdx = 0; partNdx < numPartitions; partNdx++)
  686. {
  687. const deUint32 cemClass = highLevelSelector - (blockData.isBitSet(25 + partNdx) ? 0 : 1);
  688. const deUint32 lowBit0Ndx = numPartitions + 2*partNdx;
  689. const deUint32 lowBit1Ndx = numPartitions + 2*partNdx + 1;
  690. const deUint32 lowBit0 = blockData.getBit(lowBit0Ndx < 4 ? 25+lowBit0Ndx : extraCemBitsStart+lowBit0Ndx-4);
  691. const deUint32 lowBit1 = blockData.getBit(lowBit1Ndx < 4 ? 25+lowBit1Ndx : extraCemBitsStart+lowBit1Ndx-4);
  692. endpointModesDst[partNdx] = (cemClass << 2) | (lowBit1 << 1) | lowBit0;
  693. }
  694. }
  695. }
  696. }
  697. int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions)
  698. {
  699. int result = 0;
  700. for (int i = 0; i < numPartitions; i++)
  701. result += computeNumColorEndpointValues(endpointModes[i]);
  702. return result;
  703. }
  704. void decodeISETritBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
  705. {
  706. DE_ASSERT(basisu_astc::inRange(numValues, 1, 5));
  707. deUint32 m[5];
  708. m[0] = data.getNext(numBits);
  709. deUint32 T01 = data.getNext(2);
  710. m[1] = data.getNext(numBits);
  711. deUint32 T23 = data.getNext(2);
  712. m[2] = data.getNext(numBits);
  713. deUint32 T4 = data.getNext(1);
  714. m[3] = data.getNext(numBits);
  715. deUint32 T56 = data.getNext(2);
  716. m[4] = data.getNext(numBits);
  717. deUint32 T7 = data.getNext(1);
  718. #ifdef __GNUC__
  719. #pragma GCC diagnostic push
  720. #pragma GCC diagnostic ignored "-Wimplicit-fallthrough="
  721. #endif
  722. switch (numValues)
  723. {
  724. // \note Fall-throughs.
  725. case 1: T23 = 0;
  726. case 2: T4 = 0;
  727. case 3: T56 = 0;
  728. case 4: T7 = 0;
  729. case 5: break;
  730. default:
  731. DE_ASSERT(false);
  732. }
  733. #ifdef __GNUC__
  734. #pragma GCC diagnostic pop
  735. #endif
  736. const deUint32 T = (T7 << 7) | (T56 << 5) | (T4 << 4) | (T23 << 2) | (T01 << 0);
  737. static const deUint32 tritsFromT[256][5] =
  738. {
  739. { 0,0,0,0,0 }, { 1,0,0,0,0 }, { 2,0,0,0,0 }, { 0,0,2,0,0 }, { 0,1,0,0,0 }, { 1,1,0,0,0 }, { 2,1,0,0,0 }, { 1,0,2,0,0 }, { 0,2,0,0,0 }, { 1,2,0,0,0 }, { 2,2,0,0,0 }, { 2,0,2,0,0 }, { 0,2,2,0,0 }, { 1,2,2,0,0 }, { 2,2,2,0,0 }, { 2,0,2,0,0 },
  740. { 0,0,1,0,0 }, { 1,0,1,0,0 }, { 2,0,1,0,0 }, { 0,1,2,0,0 }, { 0,1,1,0,0 }, { 1,1,1,0,0 }, { 2,1,1,0,0 }, { 1,1,2,0,0 }, { 0,2,1,0,0 }, { 1,2,1,0,0 }, { 2,2,1,0,0 }, { 2,1,2,0,0 }, { 0,0,0,2,2 }, { 1,0,0,2,2 }, { 2,0,0,2,2 }, { 0,0,2,2,2 },
  741. { 0,0,0,1,0 }, { 1,0,0,1,0 }, { 2,0,0,1,0 }, { 0,0,2,1,0 }, { 0,1,0,1,0 }, { 1,1,0,1,0 }, { 2,1,0,1,0 }, { 1,0,2,1,0 }, { 0,2,0,1,0 }, { 1,2,0,1,0 }, { 2,2,0,1,0 }, { 2,0,2,1,0 }, { 0,2,2,1,0 }, { 1,2,2,1,0 }, { 2,2,2,1,0 }, { 2,0,2,1,0 },
  742. { 0,0,1,1,0 }, { 1,0,1,1,0 }, { 2,0,1,1,0 }, { 0,1,2,1,0 }, { 0,1,1,1,0 }, { 1,1,1,1,0 }, { 2,1,1,1,0 }, { 1,1,2,1,0 }, { 0,2,1,1,0 }, { 1,2,1,1,0 }, { 2,2,1,1,0 }, { 2,1,2,1,0 }, { 0,1,0,2,2 }, { 1,1,0,2,2 }, { 2,1,0,2,2 }, { 1,0,2,2,2 },
  743. { 0,0,0,2,0 }, { 1,0,0,2,0 }, { 2,0,0,2,0 }, { 0,0,2,2,0 }, { 0,1,0,2,0 }, { 1,1,0,2,0 }, { 2,1,0,2,0 }, { 1,0,2,2,0 }, { 0,2,0,2,0 }, { 1,2,0,2,0 }, { 2,2,0,2,0 }, { 2,0,2,2,0 }, { 0,2,2,2,0 }, { 1,2,2,2,0 }, { 2,2,2,2,0 }, { 2,0,2,2,0 },
  744. { 0,0,1,2,0 }, { 1,0,1,2,0 }, { 2,0,1,2,0 }, { 0,1,2,2,0 }, { 0,1,1,2,0 }, { 1,1,1,2,0 }, { 2,1,1,2,0 }, { 1,1,2,2,0 }, { 0,2,1,2,0 }, { 1,2,1,2,0 }, { 2,2,1,2,0 }, { 2,1,2,2,0 }, { 0,2,0,2,2 }, { 1,2,0,2,2 }, { 2,2,0,2,2 }, { 2,0,2,2,2 },
  745. { 0,0,0,0,2 }, { 1,0,0,0,2 }, { 2,0,0,0,2 }, { 0,0,2,0,2 }, { 0,1,0,0,2 }, { 1,1,0,0,2 }, { 2,1,0,0,2 }, { 1,0,2,0,2 }, { 0,2,0,0,2 }, { 1,2,0,0,2 }, { 2,2,0,0,2 }, { 2,0,2,0,2 }, { 0,2,2,0,2 }, { 1,2,2,0,2 }, { 2,2,2,0,2 }, { 2,0,2,0,2 },
  746. { 0,0,1,0,2 }, { 1,0,1,0,2 }, { 2,0,1,0,2 }, { 0,1,2,0,2 }, { 0,1,1,0,2 }, { 1,1,1,0,2 }, { 2,1,1,0,2 }, { 1,1,2,0,2 }, { 0,2,1,0,2 }, { 1,2,1,0,2 }, { 2,2,1,0,2 }, { 2,1,2,0,2 }, { 0,2,2,2,2 }, { 1,2,2,2,2 }, { 2,2,2,2,2 }, { 2,0,2,2,2 },
  747. { 0,0,0,0,1 }, { 1,0,0,0,1 }, { 2,0,0,0,1 }, { 0,0,2,0,1 }, { 0,1,0,0,1 }, { 1,1,0,0,1 }, { 2,1,0,0,1 }, { 1,0,2,0,1 }, { 0,2,0,0,1 }, { 1,2,0,0,1 }, { 2,2,0,0,1 }, { 2,0,2,0,1 }, { 0,2,2,0,1 }, { 1,2,2,0,1 }, { 2,2,2,0,1 }, { 2,0,2,0,1 },
  748. { 0,0,1,0,1 }, { 1,0,1,0,1 }, { 2,0,1,0,1 }, { 0,1,2,0,1 }, { 0,1,1,0,1 }, { 1,1,1,0,1 }, { 2,1,1,0,1 }, { 1,1,2,0,1 }, { 0,2,1,0,1 }, { 1,2,1,0,1 }, { 2,2,1,0,1 }, { 2,1,2,0,1 }, { 0,0,1,2,2 }, { 1,0,1,2,2 }, { 2,0,1,2,2 }, { 0,1,2,2,2 },
  749. { 0,0,0,1,1 }, { 1,0,0,1,1 }, { 2,0,0,1,1 }, { 0,0,2,1,1 }, { 0,1,0,1,1 }, { 1,1,0,1,1 }, { 2,1,0,1,1 }, { 1,0,2,1,1 }, { 0,2,0,1,1 }, { 1,2,0,1,1 }, { 2,2,0,1,1 }, { 2,0,2,1,1 }, { 0,2,2,1,1 }, { 1,2,2,1,1 }, { 2,2,2,1,1 }, { 2,0,2,1,1 },
  750. { 0,0,1,1,1 }, { 1,0,1,1,1 }, { 2,0,1,1,1 }, { 0,1,2,1,1 }, { 0,1,1,1,1 }, { 1,1,1,1,1 }, { 2,1,1,1,1 }, { 1,1,2,1,1 }, { 0,2,1,1,1 }, { 1,2,1,1,1 }, { 2,2,1,1,1 }, { 2,1,2,1,1 }, { 0,1,1,2,2 }, { 1,1,1,2,2 }, { 2,1,1,2,2 }, { 1,1,2,2,2 },
  751. { 0,0,0,2,1 }, { 1,0,0,2,1 }, { 2,0,0,2,1 }, { 0,0,2,2,1 }, { 0,1,0,2,1 }, { 1,1,0,2,1 }, { 2,1,0,2,1 }, { 1,0,2,2,1 }, { 0,2,0,2,1 }, { 1,2,0,2,1 }, { 2,2,0,2,1 }, { 2,0,2,2,1 }, { 0,2,2,2,1 }, { 1,2,2,2,1 }, { 2,2,2,2,1 }, { 2,0,2,2,1 },
  752. { 0,0,1,2,1 }, { 1,0,1,2,1 }, { 2,0,1,2,1 }, { 0,1,2,2,1 }, { 0,1,1,2,1 }, { 1,1,1,2,1 }, { 2,1,1,2,1 }, { 1,1,2,2,1 }, { 0,2,1,2,1 }, { 1,2,1,2,1 }, { 2,2,1,2,1 }, { 2,1,2,2,1 }, { 0,2,1,2,2 }, { 1,2,1,2,2 }, { 2,2,1,2,2 }, { 2,1,2,2,2 },
  753. { 0,0,0,1,2 }, { 1,0,0,1,2 }, { 2,0,0,1,2 }, { 0,0,2,1,2 }, { 0,1,0,1,2 }, { 1,1,0,1,2 }, { 2,1,0,1,2 }, { 1,0,2,1,2 }, { 0,2,0,1,2 }, { 1,2,0,1,2 }, { 2,2,0,1,2 }, { 2,0,2,1,2 }, { 0,2,2,1,2 }, { 1,2,2,1,2 }, { 2,2,2,1,2 }, { 2,0,2,1,2 },
  754. { 0,0,1,1,2 }, { 1,0,1,1,2 }, { 2,0,1,1,2 }, { 0,1,2,1,2 }, { 0,1,1,1,2 }, { 1,1,1,1,2 }, { 2,1,1,1,2 }, { 1,1,2,1,2 }, { 0,2,1,1,2 }, { 1,2,1,1,2 }, { 2,2,1,1,2 }, { 2,1,2,1,2 }, { 0,2,2,2,2 }, { 1,2,2,2,2 }, { 2,2,2,2,2 }, { 2,1,2,2,2 }
  755. };
  756. const deUint32 (& trits)[5] = tritsFromT[T];
  757. for (int i = 0; i < numValues; i++)
  758. {
  759. dst[i].m = m[i];
  760. dst[i].tq = trits[i];
  761. dst[i].v = (trits[i] << numBits) + m[i];
  762. }
  763. }
  764. void decodeISEQuintBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
  765. {
  766. DE_ASSERT(basisu_astc::inRange(numValues, 1, 3));
  767. deUint32 m[3];
  768. m[0] = data.getNext(numBits);
  769. deUint32 Q012 = data.getNext(3);
  770. m[1] = data.getNext(numBits);
  771. deUint32 Q34 = data.getNext(2);
  772. m[2] = data.getNext(numBits);
  773. deUint32 Q56 = data.getNext(2);
  774. #ifdef __GNUC__
  775. #pragma GCC diagnostic push
  776. #pragma GCC diagnostic ignored "-Wimplicit-fallthrough="
  777. #endif
  778. switch (numValues)
  779. {
  780. // \note Fall-throughs.
  781. case 1: Q34 = 0;
  782. case 2: Q56 = 0;
  783. case 3: break;
  784. default:
  785. DE_ASSERT(false);
  786. }
  787. #ifdef __GNUC__
  788. #pragma GCC diagnostic pop
  789. #endif
  790. const deUint32 Q = (Q56 << 5) | (Q34 << 3) | (Q012 << 0);
  791. static const deUint32 quintsFromQ[256][3] =
  792. {
  793. { 0,0,0 }, { 1,0,0 }, { 2,0,0 }, { 3,0,0 }, { 4,0,0 }, { 0,4,0 }, { 4,4,0 }, { 4,4,4 }, { 0,1,0 }, { 1,1,0 }, { 2,1,0 }, { 3,1,0 }, { 4,1,0 }, { 1,4,0 }, { 4,4,1 }, { 4,4,4 },
  794. { 0,2,0 }, { 1,2,0 }, { 2,2,0 }, { 3,2,0 }, { 4,2,0 }, { 2,4,0 }, { 4,4,2 }, { 4,4,4 }, { 0,3,0 }, { 1,3,0 }, { 2,3,0 }, { 3,3,0 }, { 4,3,0 }, { 3,4,0 }, { 4,4,3 }, { 4,4,4 },
  795. { 0,0,1 }, { 1,0,1 }, { 2,0,1 }, { 3,0,1 }, { 4,0,1 }, { 0,4,1 }, { 4,0,4 }, { 0,4,4 }, { 0,1,1 }, { 1,1,1 }, { 2,1,1 }, { 3,1,1 }, { 4,1,1 }, { 1,4,1 }, { 4,1,4 }, { 1,4,4 },
  796. { 0,2,1 }, { 1,2,1 }, { 2,2,1 }, { 3,2,1 }, { 4,2,1 }, { 2,4,1 }, { 4,2,4 }, { 2,4,4 }, { 0,3,1 }, { 1,3,1 }, { 2,3,1 }, { 3,3,1 }, { 4,3,1 }, { 3,4,1 }, { 4,3,4 }, { 3,4,4 },
  797. { 0,0,2 }, { 1,0,2 }, { 2,0,2 }, { 3,0,2 }, { 4,0,2 }, { 0,4,2 }, { 2,0,4 }, { 3,0,4 }, { 0,1,2 }, { 1,1,2 }, { 2,1,2 }, { 3,1,2 }, { 4,1,2 }, { 1,4,2 }, { 2,1,4 }, { 3,1,4 },
  798. { 0,2,2 }, { 1,2,2 }, { 2,2,2 }, { 3,2,2 }, { 4,2,2 }, { 2,4,2 }, { 2,2,4 }, { 3,2,4 }, { 0,3,2 }, { 1,3,2 }, { 2,3,2 }, { 3,3,2 }, { 4,3,2 }, { 3,4,2 }, { 2,3,4 }, { 3,3,4 },
  799. { 0,0,3 }, { 1,0,3 }, { 2,0,3 }, { 3,0,3 }, { 4,0,3 }, { 0,4,3 }, { 0,0,4 }, { 1,0,4 }, { 0,1,3 }, { 1,1,3 }, { 2,1,3 }, { 3,1,3 }, { 4,1,3 }, { 1,4,3 }, { 0,1,4 }, { 1,1,4 },
  800. { 0,2,3 }, { 1,2,3 }, { 2,2,3 }, { 3,2,3 }, { 4,2,3 }, { 2,4,3 }, { 0,2,4 }, { 1,2,4 }, { 0,3,3 }, { 1,3,3 }, { 2,3,3 }, { 3,3,3 }, { 4,3,3 }, { 3,4,3 }, { 0,3,4 }, { 1,3,4 }
  801. };
  802. const deUint32 (& quints)[3] = quintsFromQ[Q];
  803. for (int i = 0; i < numValues; i++)
  804. {
  805. dst[i].m = m[i];
  806. dst[i].tq = quints[i];
  807. dst[i].v = (quints[i] << numBits) + m[i];
  808. }
  809. }
  810. inline void decodeISEBitBlock (ISEDecodedResult* dst, BitAccessStream& data, int numBits)
  811. {
  812. dst[0].m = data.getNext(numBits);
  813. dst[0].v = dst[0].m;
  814. }
  815. void decodeISE (ISEDecodedResult* dst, int numValues, BitAccessStream& data, const ISEParams& params)
  816. {
  817. if (params.mode == ISEMODE_TRIT)
  818. {
  819. const int numBlocks = deDivRoundUp32(numValues, 5);
  820. for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
  821. {
  822. const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
  823. decodeISETritBlock(&dst[5*blockNdx], numValuesInBlock, data, params.numBits);
  824. }
  825. }
  826. else if (params.mode == ISEMODE_QUINT)
  827. {
  828. const int numBlocks = deDivRoundUp32(numValues, 3);
  829. for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
  830. {
  831. const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
  832. decodeISEQuintBlock(&dst[3*blockNdx], numValuesInBlock, data, params.numBits);
  833. }
  834. }
  835. else
  836. {
  837. DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
  838. for (int i = 0; i < numValues; i++)
  839. decodeISEBitBlock(&dst[i], data, params.numBits);
  840. }
  841. }
  842. void unquantizeColorEndpoints (deUint32* dst, const ISEDecodedResult* iseResults, int numEndpoints, const ISEParams& iseParams)
  843. {
  844. if ((iseParams.mode == ISEMODE_TRIT) || (iseParams.mode == ISEMODE_QUINT))
  845. {
  846. const int rangeCase = iseParams.numBits*2 - (iseParams.mode == ISEMODE_TRIT ? 2 : 1);
  847. DE_ASSERT(basisu_astc::inRange(rangeCase, 0, 10));
  848. static const deUint32 Ca[11] = { 204, 113, 93, 54, 44, 26, 22, 13, 11, 6, 5 };
  849. const deUint32 C = Ca[rangeCase];
  850. for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
  851. {
  852. const deUint32 a = getBit(iseResults[endpointNdx].m, 0);
  853. const deUint32 b = getBit(iseResults[endpointNdx].m, 1);
  854. const deUint32 c = getBit(iseResults[endpointNdx].m, 2);
  855. const deUint32 d = getBit(iseResults[endpointNdx].m, 3);
  856. const deUint32 e = getBit(iseResults[endpointNdx].m, 4);
  857. const deUint32 f = getBit(iseResults[endpointNdx].m, 5);
  858. const deUint32 A = (a == 0) ? 0 : (1<<9)-1;
  859. const deUint32 B = (rangeCase == 0) ? 0
  860. : (rangeCase == 1) ? 0
  861. : (rangeCase == 2) ? ((b << 8) | (b << 4) | (b << 2) | (b << 1))
  862. : (rangeCase == 3) ? ((b << 8) | (b << 3) | (b << 2))
  863. : (rangeCase == 4) ? ((c << 8) | (b << 7) | (c << 3) | (b << 2) | (c << 1) | (b << 0))
  864. : (rangeCase == 5) ? ((c << 8) | (b << 7) | (c << 2) | (b << 1) | (c << 0))
  865. : (rangeCase == 6) ? ((d << 8) | (c << 7) | (b << 6) | (d << 2) | (c << 1) | (b << 0))
  866. : (rangeCase == 7) ? ((d << 8) | (c << 7) | (b << 6) | (d << 1) | (c << 0))
  867. : (rangeCase == 8) ? ((e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 1) | (d << 0))
  868. : (rangeCase == 9) ? ((e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 0))
  869. : (rangeCase == 10) ? ((f << 8) | (e << 7) | (d << 6) | (c << 5) | (b << 4) | (f << 0))
  870. : (deUint32)-1;
  871. DE_ASSERT(B != (deUint32)-1);
  872. dst[endpointNdx] = (((iseResults[endpointNdx].tq*C + B) ^ A) >> 2) | (A & 0x80);
  873. }
  874. }
  875. else
  876. {
  877. DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
  878. for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
  879. dst[endpointNdx] = bitReplicationScale(iseResults[endpointNdx].v, iseParams.numBits, 8);
  880. }
  881. }
  882. inline void bitTransferSigned (deInt32& a, deInt32& b)
  883. {
  884. b >>= 1;
  885. b |= a & 0x80;
  886. a >>= 1;
  887. a &= 0x3f;
  888. if (isBitSet(a, 5))
  889. a -= 0x40;
  890. }
  891. inline UVec4 clampedRGBA (const IVec4& rgba)
  892. {
  893. return UVec4(basisu_astc::clamp(rgba.x(), 0, 0xff),
  894. basisu_astc::clamp(rgba.y(), 0, 0xff),
  895. basisu_astc::clamp(rgba.z(), 0, 0xff),
  896. basisu_astc::clamp(rgba.w(), 0, 0xff));
  897. }
  898. inline IVec4 blueContract (int r, int g, int b, int a)
  899. {
  900. return IVec4((r+b)>>1, (g+b)>>1, b, a);
  901. }
  902. inline bool isColorEndpointModeHDR (deUint32 mode)
  903. {
  904. return (mode == 2) ||
  905. (mode == 3) ||
  906. (mode == 7) ||
  907. (mode == 11) ||
  908. (mode == 14) ||
  909. (mode == 15);
  910. }
  911. void decodeHDREndpointMode7 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3)
  912. {
  913. const deUint32 m10 = getBit(v1, 7) | (getBit(v2, 7) << 1);
  914. const deUint32 m23 = getBits(v0, 6, 7);
  915. const deUint32 majComp = (m10 != 3) ? m10
  916. : (m23 != 3) ? m23
  917. : 0;
  918. const deUint32 mode = (m10 != 3) ? m23
  919. : (m23 != 3) ? 4
  920. : 5;
  921. deInt32 red = (deInt32)getBits(v0, 0, 5);
  922. deInt32 green = (deInt32)getBits(v1, 0, 4);
  923. deInt32 blue = (deInt32)getBits(v2, 0, 4);
  924. deInt32 scale = (deInt32)getBits(v3, 0, 4);
  925. {
  926. #define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
  927. #define ASSIGN_X_BITS(V0,S0, V1,S1, V2,S2, V3,S3, V4,S4, V5,S5, V6,S6) do { SHOR(V0,S0,x0); SHOR(V1,S1,x1); SHOR(V2,S2,x2); SHOR(V3,S3,x3); SHOR(V4,S4,x4); SHOR(V5,S5,x5); SHOR(V6,S6,x6); } while (false)
  928. const deUint32 x0 = getBit(v1, 6);
  929. const deUint32 x1 = getBit(v1, 5);
  930. const deUint32 x2 = getBit(v2, 6);
  931. const deUint32 x3 = getBit(v2, 5);
  932. const deUint32 x4 = getBit(v3, 7);
  933. const deUint32 x5 = getBit(v3, 6);
  934. const deUint32 x6 = getBit(v3, 5);
  935. deInt32& R = red;
  936. deInt32& G = green;
  937. deInt32& B = blue;
  938. deInt32& S = scale;
  939. switch (mode)
  940. {
  941. case 0: ASSIGN_X_BITS(R,9, R,8, R,7, R,10, R,6, S,6, S,5); break;
  942. case 1: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, R,10, R,9); break;
  943. case 2: ASSIGN_X_BITS(R,9, R,8, R,7, R,6, S,7, S,6, S,5); break;
  944. case 3: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, S,6, S,5); break;
  945. case 4: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, R,7, S,5); break;
  946. case 5: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, S,6, S,5); break;
  947. default:
  948. DE_ASSERT(false);
  949. }
  950. #undef ASSIGN_X_BITS
  951. #undef SHOR
  952. }
  953. static const int shiftAmounts[] = { 1, 1, 2, 3, 4, 5 };
  954. DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(shiftAmounts));
  955. red <<= shiftAmounts[mode];
  956. green <<= shiftAmounts[mode];
  957. blue <<= shiftAmounts[mode];
  958. scale <<= shiftAmounts[mode];
  959. if (mode != 5)
  960. {
  961. green = red - green;
  962. blue = red - blue;
  963. }
  964. if (majComp == 1)
  965. std::swap(red, green);
  966. else if (majComp == 2)
  967. std::swap(red, blue);
  968. e0 = UVec4(basisu_astc::clamp(red - scale, 0, 0xfff),
  969. basisu_astc::clamp(green - scale, 0, 0xfff),
  970. basisu_astc::clamp(blue - scale, 0, 0xfff),
  971. 0x780);
  972. e1 = UVec4(basisu_astc::clamp(red, 0, 0xfff),
  973. basisu_astc::clamp(green, 0, 0xfff),
  974. basisu_astc::clamp(blue, 0, 0xfff),
  975. 0x780);
  976. }
  977. void decodeHDREndpointMode11 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5)
  978. {
  979. const deUint32 major = (getBit(v5, 7) << 1) | getBit(v4, 7);
  980. if (major == 3)
  981. {
  982. e0 = UVec4(v0<<4, v2<<4, getBits(v4,0,6)<<5, 0x780);
  983. e1 = UVec4(v1<<4, v3<<4, getBits(v5,0,6)<<5, 0x780);
  984. }
  985. else
  986. {
  987. const deUint32 mode = (getBit(v3, 7) << 2) | (getBit(v2, 7) << 1) | getBit(v1, 7);
  988. deInt32 a = (deInt32)((getBit(v1, 6) << 8) | v0);
  989. deInt32 c = (deInt32)(getBits(v1, 0, 5));
  990. deInt32 b0 = (deInt32)(getBits(v2, 0, 5));
  991. deInt32 b1 = (deInt32)(getBits(v3, 0, 5));
  992. deInt32 d0 = (deInt32)(getBits(v4, 0, 4));
  993. deInt32 d1 = (deInt32)(getBits(v5, 0, 4));
  994. {
  995. #define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
  996. #define ASSIGN_X_BITS(V0,S0, V1,S1, V2,S2, V3,S3, V4,S4, V5,S5) do { SHOR(V0,S0,x0); SHOR(V1,S1,x1); SHOR(V2,S2,x2); SHOR(V3,S3,x3); SHOR(V4,S4,x4); SHOR(V5,S5,x5); } while (false)
  997. const deUint32 x0 = getBit(v2, 6);
  998. const deUint32 x1 = getBit(v3, 6);
  999. const deUint32 x2 = getBit(v4, 6);
  1000. const deUint32 x3 = getBit(v5, 6);
  1001. const deUint32 x4 = getBit(v4, 5);
  1002. const deUint32 x5 = getBit(v5, 5);
  1003. switch (mode)
  1004. {
  1005. case 0: ASSIGN_X_BITS(b0,6, b1,6, d0,6, d1,6, d0,5, d1,5); break;
  1006. case 1: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, d0,5, d1,5); break;
  1007. case 2: ASSIGN_X_BITS(a,9, c,6, d0,6, d1,6, d0,5, d1,5); break;
  1008. case 3: ASSIGN_X_BITS(b0,6, b1,6, a,9, c,6, d0,5, d1,5); break;
  1009. case 4: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, a,9, a,10); break;
  1010. case 5: ASSIGN_X_BITS(a,9, a,10, c,7, c,6, d0,5, d1,5); break;
  1011. case 6: ASSIGN_X_BITS(b0,6, b1,6, a,11, c,6, a,9, a,10); break;
  1012. case 7: ASSIGN_X_BITS(a,9, a,10, a,11, c,6, d0,5, d1,5); break;
  1013. default:
  1014. DE_ASSERT(false);
  1015. }
  1016. #undef ASSIGN_X_BITS
  1017. #undef SHOR
  1018. }
  1019. static const int numDBits[] = { 7, 6, 7, 6, 5, 6, 5, 6 };
  1020. DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(numDBits));
  1021. d0 = signExtend(d0, numDBits[mode]);
  1022. d1 = signExtend(d1, numDBits[mode]);
  1023. const int shiftAmount = (mode >> 1) ^ 3;
  1024. a = (uint32_t)a << shiftAmount;
  1025. c = (uint32_t)c << shiftAmount;
  1026. b0 = (uint32_t)b0 << shiftAmount;
  1027. b1 = (uint32_t)b1 << shiftAmount;
  1028. d0 = (uint32_t)d0 << shiftAmount;
  1029. d1 = (uint32_t)d1 << shiftAmount;
  1030. e0 = UVec4(basisu_astc::clamp(a-c, 0, 0xfff), basisu_astc::clamp(a-b0-c-d0, 0, 0xfff), basisu_astc::clamp(a-b1-c-d1, 0, 0xfff), 0x780);
  1031. e1 = UVec4(basisu_astc::clamp(a, 0, 0xfff), basisu_astc::clamp(a-b0, 0, 0xfff), basisu_astc::clamp(a-b1, 0, 0xfff), 0x780);
  1032. if (major == 1)
  1033. {
  1034. std::swap(e0.x(), e0.y());
  1035. std::swap(e1.x(), e1.y());
  1036. }
  1037. else if (major == 2)
  1038. {
  1039. std::swap(e0.x(), e0.z());
  1040. std::swap(e1.x(), e1.z());
  1041. }
  1042. }
  1043. }
  1044. void decodeHDREndpointMode15(UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5, deUint32 v6In, deUint32 v7In)
  1045. {
  1046. decodeHDREndpointMode11(e0, e1, v0, v1, v2, v3, v4, v5);
  1047. const deUint32 mode = (getBit(v7In, 7) << 1) | getBit(v6In, 7);
  1048. deInt32 v6 = (deInt32)getBits(v6In, 0, 6);
  1049. deInt32 v7 = (deInt32)getBits(v7In, 0, 6);
  1050. if (mode == 3)
  1051. {
  1052. e0.w() = v6 << 5;
  1053. e1.w() = v7 << 5;
  1054. }
  1055. else
  1056. {
  1057. v6 |= (v7 << (mode+1)) & 0x780;
  1058. v7 &= (0x3f >> mode);
  1059. v7 ^= 0x20 >> mode;
  1060. v7 -= 0x20 >> mode;
  1061. v6 <<= 4-mode;
  1062. v7 <<= 4-mode;
  1063. v7 += v6;
  1064. v7 = basisu_astc::clamp(v7, 0, 0xfff);
  1065. e0.w() = v6;
  1066. e1.w() = v7;
  1067. }
  1068. }
  1069. void decodeColorEndpoints (ColorEndpointPair* dst, const deUint32* unquantizedEndpoints, const deUint32* endpointModes, int numPartitions)
  1070. {
  1071. int unquantizedNdx = 0;
  1072. for (int partitionNdx = 0; partitionNdx < numPartitions; partitionNdx++)
  1073. {
  1074. const deUint32 endpointMode = endpointModes[partitionNdx];
  1075. const deUint32* v = &unquantizedEndpoints[unquantizedNdx];
  1076. UVec4& e0 = dst[partitionNdx].e0;
  1077. UVec4& e1 = dst[partitionNdx].e1;
  1078. unquantizedNdx += computeNumColorEndpointValues(endpointMode);
  1079. switch (endpointMode)
  1080. {
  1081. case 0:
  1082. {
  1083. e0 = UVec4(v[0], v[0], v[0], 0xff);
  1084. e1 = UVec4(v[1], v[1], v[1], 0xff);
  1085. break;
  1086. }
  1087. case 1:
  1088. {
  1089. const deUint32 L0 = (v[0] >> 2) | (getBits(v[1], 6, 7) << 6);
  1090. const deUint32 L1 = basisu_astc::min(0xffu, L0 + getBits(v[1], 0, 5));
  1091. e0 = UVec4(L0, L0, L0, 0xff);
  1092. e1 = UVec4(L1, L1, L1, 0xff);
  1093. break;
  1094. }
  1095. case 2:
  1096. {
  1097. const deUint32 v1Gr = v[1] >= v[0];
  1098. const deUint32 y0 = v1Gr ? v[0]<<4 : (v[1]<<4) + 8;
  1099. const deUint32 y1 = v1Gr ? v[1]<<4 : (v[0]<<4) - 8;
  1100. e0 = UVec4(y0, y0, y0, 0x780);
  1101. e1 = UVec4(y1, y1, y1, 0x780);
  1102. break;
  1103. }
  1104. case 3:
  1105. {
  1106. const bool m = isBitSet(v[0], 7);
  1107. const deUint32 y0 = m ? (getBits(v[1], 5, 7) << 9) | (getBits(v[0], 0, 6) << 2)
  1108. : (getBits(v[1], 4, 7) << 8) | (getBits(v[0], 0, 6) << 1);
  1109. const deUint32 d = m ? getBits(v[1], 0, 4) << 2
  1110. : getBits(v[1], 0, 3) << 1;
  1111. const deUint32 y1 = basisu_astc::min(0xfffu, y0+d);
  1112. e0 = UVec4(y0, y0, y0, 0x780);
  1113. e1 = UVec4(y1, y1, y1, 0x780);
  1114. break;
  1115. }
  1116. case 4:
  1117. {
  1118. e0 = UVec4(v[0], v[0], v[0], v[2]);
  1119. e1 = UVec4(v[1], v[1], v[1], v[3]);
  1120. break;
  1121. }
  1122. case 5:
  1123. {
  1124. deInt32 v0 = (deInt32)v[0];
  1125. deInt32 v1 = (deInt32)v[1];
  1126. deInt32 v2 = (deInt32)v[2];
  1127. deInt32 v3 = (deInt32)v[3];
  1128. bitTransferSigned(v1, v0);
  1129. bitTransferSigned(v3, v2);
  1130. e0 = clampedRGBA(IVec4(v0, v0, v0, v2));
  1131. e1 = clampedRGBA(IVec4(v0+v1, v0+v1, v0+v1, v2+v3));
  1132. break;
  1133. }
  1134. case 6:
  1135. e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, 0xff);
  1136. e1 = UVec4(v[0], v[1], v[2], 0xff);
  1137. break;
  1138. case 7:
  1139. decodeHDREndpointMode7(e0, e1, v[0], v[1], v[2], v[3]);
  1140. break;
  1141. case 8:
  1142. {
  1143. if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
  1144. {
  1145. e0 = UVec4(v[0], v[2], v[4], 0xff);
  1146. e1 = UVec4(v[1], v[3], v[5], 0xff);
  1147. }
  1148. else
  1149. {
  1150. e0 = blueContract(v[1], v[3], v[5], 0xff).asUint();
  1151. e1 = blueContract(v[0], v[2], v[4], 0xff).asUint();
  1152. }
  1153. break;
  1154. }
  1155. case 9:
  1156. {
  1157. deInt32 v0 = (deInt32)v[0];
  1158. deInt32 v1 = (deInt32)v[1];
  1159. deInt32 v2 = (deInt32)v[2];
  1160. deInt32 v3 = (deInt32)v[3];
  1161. deInt32 v4 = (deInt32)v[4];
  1162. deInt32 v5 = (deInt32)v[5];
  1163. bitTransferSigned(v1, v0);
  1164. bitTransferSigned(v3, v2);
  1165. bitTransferSigned(v5, v4);
  1166. if (v1+v3+v5 >= 0)
  1167. {
  1168. e0 = clampedRGBA(IVec4(v0, v2, v4, 0xff));
  1169. e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, 0xff));
  1170. }
  1171. else
  1172. {
  1173. e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, 0xff));
  1174. e1 = clampedRGBA(blueContract(v0, v2, v4, 0xff));
  1175. }
  1176. break;
  1177. }
  1178. case 10:
  1179. {
  1180. e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, v[4]);
  1181. e1 = UVec4(v[0], v[1], v[2], v[5]);
  1182. break;
  1183. }
  1184. case 11:
  1185. {
  1186. decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
  1187. break;
  1188. }
  1189. case 12:
  1190. {
  1191. if (v[1] + v[3] + v[5] >= v[0] + v[2] + v[4])
  1192. {
  1193. e0 = UVec4(v[0], v[2], v[4], v[6]);
  1194. e1 = UVec4(v[1], v[3], v[5], v[7]);
  1195. }
  1196. else
  1197. {
  1198. e0 = clampedRGBA(blueContract(v[1], v[3], v[5], v[7]));
  1199. e1 = clampedRGBA(blueContract(v[0], v[2], v[4], v[6]));
  1200. }
  1201. break;
  1202. }
  1203. case 13:
  1204. {
  1205. deInt32 v0 = (deInt32)v[0];
  1206. deInt32 v1 = (deInt32)v[1];
  1207. deInt32 v2 = (deInt32)v[2];
  1208. deInt32 v3 = (deInt32)v[3];
  1209. deInt32 v4 = (deInt32)v[4];
  1210. deInt32 v5 = (deInt32)v[5];
  1211. deInt32 v6 = (deInt32)v[6];
  1212. deInt32 v7 = (deInt32)v[7];
  1213. bitTransferSigned(v1, v0);
  1214. bitTransferSigned(v3, v2);
  1215. bitTransferSigned(v5, v4);
  1216. bitTransferSigned(v7, v6);
  1217. if (v1+v3+v5 >= 0)
  1218. {
  1219. e0 = clampedRGBA(IVec4(v0, v2, v4, v6));
  1220. e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, v6+v7));
  1221. }
  1222. else
  1223. {
  1224. e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, v6+v7));
  1225. e1 = clampedRGBA(blueContract(v0, v2, v4, v6));
  1226. }
  1227. break;
  1228. }
  1229. case 14:
  1230. decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
  1231. e0.w() = v[6];
  1232. e1.w() = v[7];
  1233. break;
  1234. case 15:
  1235. {
  1236. decodeHDREndpointMode15(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  1237. break;
  1238. }
  1239. default:
  1240. DE_ASSERT(false);
  1241. }
  1242. }
  1243. }
  1244. void computeColorEndpoints (ColorEndpointPair* dst, const Block128& blockData, const deUint32* endpointModes, int numPartitions, int numColorEndpointValues, const ISEParams& iseParams, int numBitsAvailable)
  1245. {
  1246. const int colorEndpointDataStart = (numPartitions == 1) ? 17 : 29;
  1247. ISEDecodedResult colorEndpointData[18];
  1248. {
  1249. BitAccessStream dataStream(blockData, colorEndpointDataStart, numBitsAvailable, true);
  1250. decodeISE(&colorEndpointData[0], numColorEndpointValues, dataStream, iseParams);
  1251. }
  1252. {
  1253. deUint32 unquantizedEndpoints[18];
  1254. unquantizeColorEndpoints(&unquantizedEndpoints[0], &colorEndpointData[0], numColorEndpointValues, iseParams);
  1255. decodeColorEndpoints(dst, &unquantizedEndpoints[0], &endpointModes[0], numPartitions);
  1256. }
  1257. }
  1258. void unquantizeWeights (deUint32 dst[64], const ISEDecodedResult* weightGrid, const ASTCBlockMode& blockMode)
  1259. {
  1260. const int numWeights = computeNumWeights(blockMode);
  1261. const ISEParams& iseParams = blockMode.weightISEParams;
  1262. if ((iseParams.mode == ISEMODE_TRIT) || (iseParams.mode == ISEMODE_QUINT))
  1263. {
  1264. const int rangeCase = iseParams.numBits*2 + (iseParams.mode == ISEMODE_QUINT ? 1 : 0);
  1265. if ((rangeCase == 0) || (rangeCase == 1))
  1266. {
  1267. static const deUint32 map0[3] = { 0, 32, 63 };
  1268. static const deUint32 map1[5] = { 0, 16, 32, 47, 63 };
  1269. const deUint32* const map = (rangeCase == 0) ? &map0[0] : &map1[0];
  1270. for (int i = 0; i < numWeights; i++)
  1271. {
  1272. DE_ASSERT(weightGrid[i].v < (rangeCase == 0 ? 3u : 5u));
  1273. dst[i] = map[weightGrid[i].v];
  1274. }
  1275. }
  1276. else
  1277. {
  1278. DE_ASSERT(rangeCase <= 6);
  1279. static const deUint32 Ca[5] = { 50, 28, 23, 13, 11 };
  1280. const deUint32 C = Ca[rangeCase-2];
  1281. for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
  1282. {
  1283. const deUint32 a = getBit(weightGrid[weightNdx].m, 0);
  1284. const deUint32 b = getBit(weightGrid[weightNdx].m, 1);
  1285. const deUint32 c = getBit(weightGrid[weightNdx].m, 2);
  1286. const deUint32 A = (a == 0) ? 0 : (1<<7)-1;
  1287. const deUint32 B = (rangeCase == 2) ? 0
  1288. : (rangeCase == 3) ? 0
  1289. : (rangeCase == 4) ? (b << 6) | (b << 2) | (b << 0)
  1290. : (rangeCase == 5) ? (b << 6) | (b << 1)
  1291. : (rangeCase == 6) ? (c << 6) | (b << 5) | (c << 1) | (b << 0)
  1292. : (deUint32)-1;
  1293. dst[weightNdx] = (((weightGrid[weightNdx].tq*C + B) ^ A) >> 2) | (A & 0x20);
  1294. }
  1295. }
  1296. }
  1297. else
  1298. {
  1299. DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
  1300. for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
  1301. dst[weightNdx] = bitReplicationScale(weightGrid[weightNdx].v, iseParams.numBits, 6);
  1302. }
  1303. for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
  1304. dst[weightNdx] += dst[weightNdx] > 32 ? 1 : 0;
  1305. // Initialize nonexistent weights to poison values
  1306. for (int weightNdx = numWeights; weightNdx < 64; weightNdx++)
  1307. dst[weightNdx] = ~0u;
  1308. }
  1309. void interpolateWeights (TexelWeightPair* dst, const deUint32 (&unquantizedWeights) [64], int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
  1310. {
  1311. const int numWeightsPerTexel = blockMode.isDualPlane ? 2 : 1;
  1312. const deUint32 scaleX = (1024 + blockWidth/2) / (blockWidth-1);
  1313. const deUint32 scaleY = (1024 + blockHeight/2) / (blockHeight-1);
  1314. DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= (int)DE_LENGTH_OF_ARRAY(unquantizedWeights));
  1315. for (int texelY = 0; texelY < blockHeight; texelY++)
  1316. {
  1317. for (int texelX = 0; texelX < blockWidth; texelX++)
  1318. {
  1319. const deUint32 gX = (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;
  1320. const deUint32 gY = (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;
  1321. const deUint32 jX = gX >> 4;
  1322. const deUint32 jY = gY >> 4;
  1323. const deUint32 fX = gX & 0xf;
  1324. const deUint32 fY = gY & 0xf;
  1325. const deUint32 w11 = (fX*fY + 8) >> 4;
  1326. const deUint32 w10 = fY - w11;
  1327. const deUint32 w01 = fX - w11;
  1328. const deUint32 w00 = 16 - fX - fY + w11;
  1329. const deUint32 i00 = jY*blockMode.weightGridWidth + jX;
  1330. const deUint32 i01 = i00 + 1;
  1331. const deUint32 i10 = i00 + blockMode.weightGridWidth;
  1332. const deUint32 i11 = i00 + blockMode.weightGridWidth + 1;
  1333. // These addresses can be out of bounds, but respective weights will be 0 then.
  1334. DE_ASSERT(deInBounds32(i00, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w00 == 0);
  1335. DE_ASSERT(deInBounds32(i01, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w01 == 0);
  1336. DE_ASSERT(deInBounds32(i10, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w10 == 0);
  1337. DE_ASSERT(deInBounds32(i11, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w11 == 0);
  1338. for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)
  1339. {
  1340. // & 0x3f clamps address to bounds of unquantizedWeights
  1341. const deUint32 p00 = unquantizedWeights[(i00 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
  1342. const deUint32 p01 = unquantizedWeights[(i01 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
  1343. const deUint32 p10 = unquantizedWeights[(i10 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
  1344. const deUint32 p11 = unquantizedWeights[(i11 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
  1345. dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
  1351. {
  1352. ISEDecodedResult weightGrid[64];
  1353. {
  1354. BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);
  1355. decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);
  1356. }
  1357. {
  1358. deUint32 unquantizedWeights[64];
  1359. unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);
  1360. interpolateWeights(dst, unquantizedWeights, blockWidth, blockHeight, blockMode);
  1361. }
  1362. }
  1363. inline deUint32 hash52 (deUint32 v)
  1364. {
  1365. deUint32 p = v;
  1366. p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
  1367. p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
  1368. p ^= p << 6; p ^= p >> 17;
  1369. return p;
  1370. }
  1371. int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)
  1372. {
  1373. DE_ASSERT(zIn == 0);
  1374. const deUint32 x = smallBlock ? xIn << 1 : xIn;
  1375. const deUint32 y = smallBlock ? yIn << 1 : yIn;
  1376. const deUint32 z = smallBlock ? zIn << 1 : zIn;
  1377. const deUint32 seed = seedIn + 1024*(numPartitions-1);
  1378. const deUint32 rnum = hash52(seed);
  1379. deUint8 seed1 = (deUint8)( rnum & 0xf);
  1380. deUint8 seed2 = (deUint8)((rnum >> 4) & 0xf);
  1381. deUint8 seed3 = (deUint8)((rnum >> 8) & 0xf);
  1382. deUint8 seed4 = (deUint8)((rnum >> 12) & 0xf);
  1383. deUint8 seed5 = (deUint8)((rnum >> 16) & 0xf);
  1384. deUint8 seed6 = (deUint8)((rnum >> 20) & 0xf);
  1385. deUint8 seed7 = (deUint8)((rnum >> 24) & 0xf);
  1386. deUint8 seed8 = (deUint8)((rnum >> 28) & 0xf);
  1387. deUint8 seed9 = (deUint8)((rnum >> 18) & 0xf);
  1388. deUint8 seed10 = (deUint8)((rnum >> 22) & 0xf);
  1389. deUint8 seed11 = (deUint8)((rnum >> 26) & 0xf);
  1390. deUint8 seed12 = (deUint8)(((rnum >> 30) | (rnum << 2)) & 0xf);
  1391. seed1 = (deUint8)(seed1 * seed1 );
  1392. seed2 = (deUint8)(seed2 * seed2 );
  1393. seed3 = (deUint8)(seed3 * seed3 );
  1394. seed4 = (deUint8)(seed4 * seed4 );
  1395. seed5 = (deUint8)(seed5 * seed5 );
  1396. seed6 = (deUint8)(seed6 * seed6 );
  1397. seed7 = (deUint8)(seed7 * seed7 );
  1398. seed8 = (deUint8)(seed8 * seed8 );
  1399. seed9 = (deUint8)(seed9 * seed9 );
  1400. seed10 = (deUint8)(seed10 * seed10);
  1401. seed11 = (deUint8)(seed11 * seed11);
  1402. seed12 = (deUint8)(seed12 * seed12);
  1403. const int shA = (seed & 2) != 0 ? 4 : 5;
  1404. const int shB = numPartitions == 3 ? 6 : 5;
  1405. const int sh1 = (seed & 1) != 0 ? shA : shB;
  1406. const int sh2 = (seed & 1) != 0 ? shB : shA;
  1407. const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;
  1408. seed1 = (deUint8)(seed1 >> sh1);
  1409. seed2 = (deUint8)(seed2 >> sh2);
  1410. seed3 = (deUint8)(seed3 >> sh1);
  1411. seed4 = (deUint8)(seed4 >> sh2);
  1412. seed5 = (deUint8)(seed5 >> sh1);
  1413. seed6 = (deUint8)(seed6 >> sh2);
  1414. seed7 = (deUint8)(seed7 >> sh1);
  1415. seed8 = (deUint8)(seed8 >> sh2);
  1416. seed9 = (deUint8)(seed9 >> sh3);
  1417. seed10 = (deUint8)(seed10 >> sh3);
  1418. seed11 = (deUint8)(seed11 >> sh3);
  1419. seed12 = (deUint8)(seed12 >> sh3);
  1420. const int a = 0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));
  1421. const int b = 0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));
  1422. const int c = (numPartitions >= 3) ? 0x3f & (seed5*x + seed6*y + seed9*z + (rnum >> 6)) : 0;
  1423. const int d = (numPartitions >= 4) ? 0x3f & (seed7*x + seed8*y + seed10*z + (rnum >> 2)) : 0;
  1424. return (a >= b && a >= c && a >= d) ? 0
  1425. : (b >= c && b >= d) ? 1
  1426. : (c >= d) ? 2
  1427. : 3;
  1428. }
  1429. DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,
  1430. int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)
  1431. {
  1432. const bool smallBlock = blockWidth*blockHeight < 31;
  1433. DecompressResult result = DECOMPRESS_RESULT_VALID_BLOCK;
  1434. bool isHDREndpoint[4];
  1435. for (int i = 0; i < numPartitions; i++)
  1436. {
  1437. isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);
  1438. }
  1439. for (int texelY = 0; texelY < blockHeight; texelY++)
  1440. {
  1441. for (int texelX = 0; texelX < blockWidth; texelX++)
  1442. {
  1443. const int texelNdx = texelY * blockWidth + texelX;
  1444. const int colorEndpointNdx = (numPartitions == 1) ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);
  1445. DE_ASSERT(colorEndpointNdx < numPartitions);
  1446. const UVec4& e0 = colorEndpoints[colorEndpointNdx].e0;
  1447. const UVec4& e1 = colorEndpoints[colorEndpointNdx].e1;
  1448. const TexelWeightPair& weight = texelWeights[texelNdx];
  1449. if (isLDRMode && isHDREndpoint[colorEndpointNdx])
  1450. {
  1451. if (isSRGB)
  1452. {
  1453. ((deUint8*)dst)[texelNdx * 4 + 0] = 0xff;
  1454. ((deUint8*)dst)[texelNdx * 4 + 1] = 0;
  1455. ((deUint8*)dst)[texelNdx * 4 + 2] = 0xff;
  1456. ((deUint8*)dst)[texelNdx * 4 + 3] = 0xff;
  1457. }
  1458. else
  1459. {
  1460. ((float*)dst)[texelNdx * 4 + 0] = 1.0f;
  1461. ((float*)dst)[texelNdx * 4 + 1] = 0;
  1462. ((float*)dst)[texelNdx * 4 + 2] = 1.0f;
  1463. ((float*)dst)[texelNdx * 4 + 3] = 1.0f;
  1464. }
  1465. result = DECOMPRESS_RESULT_ERROR;
  1466. }
  1467. else
  1468. {
  1469. for (int channelNdx = 0; channelNdx < 4; channelNdx++)
  1470. {
  1471. if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.
  1472. {
  1473. const deUint32 c0 = (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);
  1474. const deUint32 c1 = (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);
  1475. const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
  1476. const deUint32 c = (c0 * (64 - w) + c1 * w + 32) / 64;
  1477. if (isSRGB)
  1478. ((deUint8*)dst)[texelNdx * 4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);
  1479. else
  1480. ((float*)dst)[texelNdx * 4 + channelNdx] = (c == 65535) ? 1.0f : (float)c / 65536.0f;
  1481. }
  1482. else
  1483. {
  1484. DE_ASSERT(!isSRGB);
  1485. //DE_STATIC_ASSERT((basisu_astc::meta::TypesSame<deFloat16, deUint16>::Value));
  1486. const deUint32 c0 = e0[channelNdx] << 4;
  1487. const deUint32 c1 = e1[channelNdx] << 4;
  1488. const deUint32 w = weight.w[(ccs == channelNdx) ? 1 : 0];
  1489. const deUint32 c = (c0 * (64 - w) + c1 * w + 32) / 64;
  1490. const deUint32 e = getBits(c, 11, 15);
  1491. const deUint32 m = getBits(c, 0, 10);
  1492. const deUint32 mt = (m < 512) ? (3 * m)
  1493. : (m >= 1536) ? (5 * m - 2048)
  1494. : (4 * m - 512);
  1495. const deFloat16 cf = (deFloat16)((e << 10) + (mt >> 3));
  1496. ((float*)dst)[texelNdx * 4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);
  1497. }
  1498. } // channelNdx
  1499. }
  1500. } // texelX
  1501. } // texelY
  1502. return result;
  1503. }
  1504. DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)
  1505. {
  1506. DE_ASSERT(isLDR || !isSRGB);
  1507. // Decode block mode.
  1508. const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));
  1509. // Check for block mode errors.
  1510. if (blockMode.isError)
  1511. {
  1512. setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
  1513. return DECOMPRESS_RESULT_ERROR;
  1514. }
  1515. // Separate path for void-extent.
  1516. if (blockMode.isVoidExtent)
  1517. return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);
  1518. // Compute weight grid values.
  1519. const int numWeights = computeNumWeights(blockMode);
  1520. const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);
  1521. const int numPartitions = (int)blockData.getBits(11, 12) + 1;
  1522. // Check for errors in weight grid, partition and dual-plane parameters.
  1523. if ((numWeights > 64) ||
  1524. (numWeightDataBits > 96) ||
  1525. (numWeightDataBits < 24) ||
  1526. (blockMode.weightGridWidth > blockWidth) ||
  1527. (blockMode.weightGridHeight > blockHeight) ||
  1528. ((numPartitions == 4) && blockMode.isDualPlane))
  1529. {
  1530. setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
  1531. return DECOMPRESS_RESULT_ERROR;
  1532. }
  1533. // Compute number of bits available for color endpoint data.
  1534. const bool isSingleUniqueCem = (numPartitions == 1) || (blockData.getBits(23, 24) == 0);
  1535. const int numConfigDataBits = ((numPartitions == 1) ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +
  1536. (blockMode.isDualPlane ? 2 : 0);
  1537. const int numBitsForColorEndpoints = 128 - numWeightDataBits - numConfigDataBits;
  1538. const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -1
  1539. : (numPartitions == 4) ? 7
  1540. : (numPartitions == 3) ? 4
  1541. : (numPartitions == 2) ? 1
  1542. : 0);
  1543. // Decode color endpoint modes.
  1544. deUint32 colorEndpointModes[4];
  1545. decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);
  1546. const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);
  1547. // Check for errors in color endpoint value count.
  1548. if ((numColorEndpointValues > 18) || (numBitsForColorEndpoints < (int)deDivRoundUp32(13*numColorEndpointValues, 5)))
  1549. {
  1550. setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
  1551. return DECOMPRESS_RESULT_ERROR;
  1552. }
  1553. // Compute color endpoints.
  1554. ColorEndpointPair colorEndpoints[4];
  1555. computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,
  1556. computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);
  1557. // Compute texel weights.
  1558. TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];
  1559. computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);
  1560. // Set texel colors.
  1561. const int ccs = blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;
  1562. const deUint32 partitionIndexSeed = (numPartitions > 1) ? blockData.getBits(13, 22) : (deUint32)-1;
  1563. return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);
  1564. }
  1565. // Returns -1 on error, 0 if LDR, 1 if HDR
  1566. int isHDR(const Block128& blockData, int blockWidth, int blockHeight)
  1567. {
  1568. // Decode block mode.
  1569. const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));
  1570. // Check for block mode errors.
  1571. if (blockMode.isError)
  1572. return -1;
  1573. // Separate path for void-extent.
  1574. if (blockMode.isVoidExtent)
  1575. {
  1576. const bool isHDRBlock = blockData.isBitSet(9);
  1577. return isHDRBlock ? 1 : 0;
  1578. }
  1579. // Compute weight grid values.
  1580. const int numWeights = computeNumWeights(blockMode);
  1581. const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);
  1582. const int numPartitions = (int)blockData.getBits(11, 12) + 1;
  1583. // Check for errors in weight grid, partition and dual-plane parameters.
  1584. if ((numWeights > 64) ||
  1585. (numWeightDataBits > 96) ||
  1586. (numWeightDataBits < 24) ||
  1587. (blockMode.weightGridWidth > blockWidth) ||
  1588. (blockMode.weightGridHeight > blockHeight) ||
  1589. ((numPartitions == 4) && blockMode.isDualPlane))
  1590. {
  1591. return -1;
  1592. }
  1593. // Compute number of bits available for color endpoint data.
  1594. const bool isSingleUniqueCem = (numPartitions == 1) || (blockData.getBits(23, 24) == 0);
  1595. const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -1
  1596. : (numPartitions == 4) ? 7
  1597. : (numPartitions == 3) ? 4
  1598. : (numPartitions == 2) ? 1
  1599. : 0);
  1600. // Decode color endpoint modes.
  1601. deUint32 colorEndpointModes[4];
  1602. decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);
  1603. for (int i = 0; i < numPartitions; i++)
  1604. {
  1605. if (isColorEndpointModeHDR(colorEndpointModes[i]))
  1606. return 1;
  1607. }
  1608. return 0;
  1609. }
  1610. typedef uint16_t half_float;
  1611. half_float float_to_half(float val, bool toward_zero)
  1612. {
  1613. union { float f; int32_t i; uint32_t u; } fi = { val };
  1614. const int flt_m = fi.i & 0x7FFFFF, flt_e = (fi.i >> 23) & 0xFF, flt_s = (fi.i >> 31) & 0x1;
  1615. int s = flt_s, e = 0, m = 0;
  1616. // inf/NaN
  1617. if (flt_e == 0xff)
  1618. {
  1619. e = 31;
  1620. if (flt_m != 0) // NaN
  1621. m = 1;
  1622. }
  1623. // not zero or denormal
  1624. else if (flt_e != 0)
  1625. {
  1626. int new_exp = flt_e - 127;
  1627. if (new_exp > 15)
  1628. e = 31;
  1629. else if (new_exp < -14)
  1630. {
  1631. if (toward_zero)
  1632. m = (int)truncf((1 << 24) * fabsf(fi.f));
  1633. else
  1634. m = lrintf((1 << 24) * fabsf(fi.f));
  1635. }
  1636. else
  1637. {
  1638. e = new_exp + 15;
  1639. if (toward_zero)
  1640. m = (int)truncf((float)flt_m * (1.0f / (float)(1 << 13)));
  1641. else
  1642. m = lrintf((float)flt_m * (1.0f / (float)(1 << 13)));
  1643. }
  1644. }
  1645. assert((0 <= m) && (m <= 1024));
  1646. if (m == 1024)
  1647. {
  1648. e++;
  1649. m = 0;
  1650. }
  1651. assert((s >= 0) && (s <= 1));
  1652. assert((e >= 0) && (e <= 31));
  1653. assert((m >= 0) && (m <= 1023));
  1654. half_float result = (half_float)((s << 15) | (e << 10) | m);
  1655. return result;
  1656. }
  1657. float half_to_float(half_float hval)
  1658. {
  1659. union { float f; uint32_t u; } x = { 0 };
  1660. uint32_t s = ((uint32_t)hval >> 15) & 1;
  1661. uint32_t e = ((uint32_t)hval >> 10) & 0x1F;
  1662. uint32_t m = (uint32_t)hval & 0x3FF;
  1663. if (!e)
  1664. {
  1665. if (!m)
  1666. {
  1667. // +- 0
  1668. x.u = s << 31;
  1669. return x.f;
  1670. }
  1671. else
  1672. {
  1673. // denormalized
  1674. while (!(m & 0x00000400))
  1675. {
  1676. m <<= 1;
  1677. --e;
  1678. }
  1679. ++e;
  1680. m &= ~0x00000400;
  1681. }
  1682. }
  1683. else if (e == 31)
  1684. {
  1685. if (m == 0)
  1686. {
  1687. // +/- INF
  1688. x.u = (s << 31) | 0x7f800000;
  1689. return x.f;
  1690. }
  1691. else
  1692. {
  1693. // +/- NaN
  1694. x.u = (s << 31) | 0x7f800000 | (m << 13);
  1695. return x.f;
  1696. }
  1697. }
  1698. e = e + (127 - 15);
  1699. m = m << 13;
  1700. assert(s <= 1);
  1701. assert(m <= 0x7FFFFF);
  1702. assert(e <= 255);
  1703. x.u = m | (e << 23) | (s << 31);
  1704. return x.f;
  1705. }
  1706. } // anonymous
  1707. // See https://registry.khronos.org/DataFormat/specs/1.3/dataformat.1.3.inline.html#_hdr_endpoint_decoding
  1708. static void convert_to_half_prec(uint32_t n, float* pVals)
  1709. {
  1710. #if 0
  1711. const int prev_dir = fesetround(FE_TOWARDZERO);
  1712. for (uint32_t i = 0; i < n; i++)
  1713. pVals[i] = half_to_float(float_to_half(pVals[i]));
  1714. fesetround(prev_dir);
  1715. for (uint32_t i = 0; i < n; i++)
  1716. {
  1717. assert(pVals[i] == half_to_float(float_to_half(pVals[i], true)));
  1718. }
  1719. #else
  1720. // This ensures the values are rounded towards zero as half floats.
  1721. for (uint32_t i = 0; i < n; i++)
  1722. {
  1723. pVals[i] = half_to_float(float_to_half(pVals[i], true));
  1724. }
  1725. #endif
  1726. }
  1727. bool decompress_ldr(uint8_t *pDst, const uint8_t * data, bool isSRGB, int blockWidth, int blockHeight)
  1728. {
  1729. float linear[MAX_BLOCK_WIDTH * MAX_BLOCK_HEIGHT * 4];
  1730. const Block128 blockData(data);
  1731. // isSRGB is true, this writes uint8_t's. Otherwise it writes floats.
  1732. if (decompressBlock(isSRGB ? (void*)pDst : (void*)&linear[0], blockData, blockWidth, blockHeight, isSRGB, true) != DECOMPRESS_RESULT_VALID_BLOCK)
  1733. {
  1734. return false;
  1735. }
  1736. if (!isSRGB)
  1737. {
  1738. // Convert the floats to 8-bits with rounding.
  1739. int pix = 0;
  1740. for (int i = 0; i < blockHeight; i++)
  1741. {
  1742. for (int j = 0; j < blockWidth; j++, pix++)
  1743. {
  1744. pDst[4 * pix + 0] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 0] * 65536.0f + .5f), 0, 65535) >> 8);
  1745. pDst[4 * pix + 1] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 1] * 65536.0f + .5f), 0, 65535) >> 8);
  1746. pDst[4 * pix + 2] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 2] * 65536.0f + .5f), 0, 65535) >> 8);
  1747. pDst[4 * pix + 3] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 3] * 65536.0f + .5f), 0, 65535) >> 8);
  1748. }
  1749. }
  1750. }
  1751. return true;
  1752. }
  1753. bool decompress_hdr(float* pDstRGBA, const uint8_t* data, int blockWidth, int blockHeight)
  1754. {
  1755. const Block128 blockData(data);
  1756. if (decompressBlock(pDstRGBA, blockData, blockWidth, blockHeight, false, false) != DECOMPRESS_RESULT_VALID_BLOCK)
  1757. {
  1758. return false;
  1759. }
  1760. convert_to_half_prec(blockWidth * blockHeight * 4, pDstRGBA);
  1761. return true;
  1762. }
  1763. bool is_hdr(const uint8_t* data, int blockWidth, int blockHeight, bool &is_hdr)
  1764. {
  1765. is_hdr = false;
  1766. const Block128 blockData(data);
  1767. int status = isHDR(blockData, blockWidth, blockHeight);
  1768. if (status < 0)
  1769. {
  1770. return false;
  1771. }
  1772. is_hdr = (status == 1);
  1773. return true;
  1774. }
  1775. } // astc
  1776. } // basisu_astc
  1777. #if defined(__GNUC__)
  1778. #pragma GCC diagnostic pop
  1779. #endif