basisu_astc_decomp.cpp 56 KB

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