1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2016 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief ASTC Utilities.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuAstcUtil.hpp"
25 #include "deFloat16.h"
26 #include "deRandom.hpp"
49 inline deUint32 getBit (deUint32 src, int ndx)
51 DE_ASSERT(de::inBounds(ndx, 0, 32));
52 return (src >> ndx) & 1;
55 inline deUint32 getBits (deUint32 src, int low, int high)
57 const int numBits = (high-low) + 1;
59 DE_ASSERT(de::inRange(numBits, 1, 32));
62 return (deUint32)((src >> low) & ((1u<<numBits)-1));
64 return (deUint32)((src >> low) & 0xFFFFFFFFu);
67 inline bool isBitSet (deUint32 src, int ndx)
69 return getBit(src, ndx) != 0;
72 inline deUint32 reverseBits (deUint32 src, int numBits)
74 DE_ASSERT(de::inRange(numBits, 0, 32));
76 for (int i = 0; i < numBits; i++)
77 result |= ((src >> i) & 1) << (numBits-1-i);
81 inline deUint32 bitReplicationScale (deUint32 src, int numSrcBits, int numDstBits)
83 DE_ASSERT(numSrcBits <= numDstBits);
84 DE_ASSERT((src & ((1<<numSrcBits)-1)) == src);
86 for (int shift = numDstBits-numSrcBits; shift > -numSrcBits; shift -= numSrcBits)
87 dst |= shift >= 0 ? src << shift : src >> -shift;
91 inline deInt32 signExtend (deInt32 src, int numSrcBits)
93 DE_ASSERT(de::inRange(numSrcBits, 2, 31));
94 const bool negative = (src & (1 << (numSrcBits-1))) != 0;
95 return src | (negative ? ~((1 << numSrcBits) - 1) : 0);
98 inline bool isFloat16InfOrNan (deFloat16 v)
100 return getBits(v, 10, 14) == 31;
117 ISEParams (ISEMode mode_, int numBits_) : mode(mode_), numBits(numBits_) {}
120 inline int computeNumRequiredBits (const ISEParams& iseParams, int numValues)
122 switch (iseParams.mode)
124 case ISEMODE_TRIT: return deDivRoundUp32(numValues*8, 5) + numValues*iseParams.numBits;
125 case ISEMODE_QUINT: return deDivRoundUp32(numValues*7, 3) + numValues*iseParams.numBits;
126 case ISEMODE_PLAIN_BIT: return numValues*iseParams.numBits;
133 ISEParams computeMaximumRangeISEParams (int numAvailableBits, int numValuesInSequence)
135 int curBitsForTritMode = 6;
136 int curBitsForQuintMode = 5;
137 int curBitsForPlainBitMode = 8;
141 DE_ASSERT(curBitsForTritMode > 0 || curBitsForQuintMode > 0 || curBitsForPlainBitMode > 0);
143 const int tritRange = curBitsForTritMode > 0 ? (3 << curBitsForTritMode) - 1 : -1;
144 const int quintRange = curBitsForQuintMode > 0 ? (5 << curBitsForQuintMode) - 1 : -1;
145 const int plainBitRange = curBitsForPlainBitMode > 0 ? (1 << curBitsForPlainBitMode) - 1 : -1;
146 const int maxRange = de::max(de::max(tritRange, quintRange), plainBitRange);
148 if (maxRange == tritRange)
150 const ISEParams params(ISEMODE_TRIT, curBitsForTritMode);
151 if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
152 return ISEParams(ISEMODE_TRIT, curBitsForTritMode);
153 curBitsForTritMode--;
155 else if (maxRange == quintRange)
157 const ISEParams params(ISEMODE_QUINT, curBitsForQuintMode);
158 if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
159 return ISEParams(ISEMODE_QUINT, curBitsForQuintMode);
160 curBitsForQuintMode--;
164 const ISEParams params(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
165 DE_ASSERT(maxRange == plainBitRange);
166 if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
167 return ISEParams(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
168 curBitsForPlainBitMode--;
173 inline int computeNumColorEndpointValues (deUint32 endpointMode)
175 DE_ASSERT(endpointMode < 16);
176 return (endpointMode/4 + 1) * 2;
179 // Decompression utilities
181 enum DecompressResult
183 DECOMPRESS_RESULT_VALID_BLOCK = 0, //!< Decompressed valid block
184 DECOMPRESS_RESULT_ERROR, //!< Encountered error while decompressing, error color written
186 DECOMPRESS_RESULT_LAST
189 // A helper for getting bits from a 128-bit block.
193 typedef deUint64 Word;
197 WORD_BYTES = sizeof(Word),
198 WORD_BITS = 8*WORD_BYTES,
199 NUM_WORDS = 128 / WORD_BITS
202 DE_STATIC_ASSERT(128 % WORD_BITS == 0);
205 Block128 (const deUint8* src)
207 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
209 m_words[wordNdx] = 0;
210 for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
211 m_words[wordNdx] |= (Word)src[wordNdx*WORD_BYTES + byteNdx] << (8*byteNdx);
215 deUint32 getBit (int ndx) const
217 DE_ASSERT(de::inBounds(ndx, 0, 128));
218 return (m_words[ndx / WORD_BITS] >> (ndx % WORD_BITS)) & 1;
221 deUint32 getBits (int low, int high) const
223 DE_ASSERT(de::inBounds(low, 0, 128));
224 DE_ASSERT(de::inBounds(high, 0, 128));
225 DE_ASSERT(de::inRange(high-low+1, 0, 32));
230 const int word0Ndx = low / WORD_BITS;
231 const int word1Ndx = high / WORD_BITS;
233 // \note "foo << bar << 1" done instead of "foo << (bar+1)" to avoid overflow, i.e. shift amount being too big.
235 if (word0Ndx == word1Ndx)
236 return (deUint32)((m_words[word0Ndx] & ((((Word)1 << high%WORD_BITS << 1) - 1))) >> ((Word)low % WORD_BITS));
239 DE_ASSERT(word1Ndx == word0Ndx + 1);
241 return (deUint32)(m_words[word0Ndx] >> (low%WORD_BITS)) |
242 (deUint32)((m_words[word1Ndx] & (((Word)1 << high%WORD_BITS << 1) - 1)) << (high-low - high%WORD_BITS));
246 bool isBitSet (int ndx) const
248 DE_ASSERT(de::inBounds(ndx, 0, 128));
249 return getBit(ndx) != 0;
253 Word m_words[NUM_WORDS];
256 // A helper for sequential access into a Block128.
257 class BitAccessStream
260 BitAccessStream (const Block128& src, int startNdxInSrc, int length, bool forward)
262 , m_startNdxInSrc (startNdxInSrc)
264 , m_forward (forward)
269 // Get the next num bits. Bits at positions greater than or equal to m_length are zeros.
270 deUint32 getNext (int num)
272 if (num == 0 || m_ndx >= m_length)
275 const int end = m_ndx + num;
276 const int numBitsFromSrc = de::max(0, de::min(m_length, end) - m_ndx);
277 const int low = m_ndx;
278 const int high = m_ndx + numBitsFromSrc - 1;
282 return m_forward ? m_src.getBits(m_startNdxInSrc + low, m_startNdxInSrc + high)
283 : reverseBits(m_src.getBits(m_startNdxInSrc - high, m_startNdxInSrc - low), numBitsFromSrc);
287 const Block128& m_src;
288 const int m_startNdxInSrc;
290 const bool m_forward;
295 struct ISEDecodedResult
298 deUint32 tq; //!< Trit or quint value, depending on ISE mode.
302 // Data from an ASTC block's "block mode" part (i.e. bits [0,10]).
306 // \note Following fields only relevant if !isError.
308 // \note Following fields only relevant if !isVoidExtent.
311 int weightGridHeight;
312 ISEParams weightISEParams;
316 , isVoidExtent (true)
318 , weightGridWidth (-1)
319 , weightGridHeight (-1)
320 , weightISEParams (ISEMODE_LAST, -1)
325 inline int computeNumWeights (const ASTCBlockMode& mode)
327 return mode.weightGridWidth * mode.weightGridHeight * (mode.isDualPlane ? 2 : 1);
330 struct ColorEndpointPair
336 struct TexelWeightPair
341 ASTCBlockMode getASTCBlockMode (deUint32 blockModeData)
343 ASTCBlockMode blockMode;
344 blockMode.isError = true; // \note Set to false later, if not error.
346 blockMode.isVoidExtent = getBits(blockModeData, 0, 8) == 0x1fc;
348 if (!blockMode.isVoidExtent)
350 if ((getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 6, 8) == 7) || getBits(blockModeData, 0, 3) == 0)
351 return blockMode; // Invalid ("reserved").
353 deUint32 r = (deUint32)-1; // \note Set in the following branches.
355 if (getBits(blockModeData, 0, 1) == 0)
357 const deUint32 r0 = getBit(blockModeData, 4);
358 const deUint32 r1 = getBit(blockModeData, 2);
359 const deUint32 r2 = getBit(blockModeData, 3);
360 const deUint32 i78 = getBits(blockModeData, 7, 8);
362 r = (r2 << 2) | (r1 << 1) | (r0 << 0);
366 const bool i5 = isBitSet(blockModeData, 5);
367 blockMode.weightGridWidth = i5 ? 10 : 6;
368 blockMode.weightGridHeight = i5 ? 6 : 10;
372 const deUint32 a = getBits(blockModeData, 5, 6);
375 case 0: blockMode.weightGridWidth = 12; blockMode.weightGridHeight = a + 2; break;
376 case 1: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = 12; break;
377 case 2: blockMode.weightGridWidth = a + 6; blockMode.weightGridHeight = getBits(blockModeData, 9, 10) + 6; break;
378 default: DE_ASSERT(false);
384 const deUint32 r0 = getBit(blockModeData, 4);
385 const deUint32 r1 = getBit(blockModeData, 0);
386 const deUint32 r2 = getBit(blockModeData, 1);
387 const deUint32 i23 = getBits(blockModeData, 2, 3);
388 const deUint32 a = getBits(blockModeData, 5, 6);
390 r = (r2 << 2) | (r1 << 1) | (r0 << 0);
394 const deUint32 b = getBit(blockModeData, 7);
395 const bool i8 = isBitSet(blockModeData, 8);
396 blockMode.weightGridWidth = i8 ? b+2 : a+2;
397 blockMode.weightGridHeight = i8 ? a+2 : b+6;
401 const deUint32 b = getBits(blockModeData, 7, 8);
405 case 0: blockMode.weightGridWidth = b + 4; blockMode.weightGridHeight = a + 2; break;
406 case 1: blockMode.weightGridWidth = b + 8; blockMode.weightGridHeight = a + 2; break;
407 case 2: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = b + 8; break;
408 default: DE_ASSERT(false);
413 const bool zeroDH = getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 7, 8) == 2;
414 const bool h = zeroDH ? 0 : isBitSet(blockModeData, 9);
415 blockMode.isDualPlane = zeroDH ? 0 : isBitSet(blockModeData, 10);
418 ISEMode& m = blockMode.weightISEParams.mode;
419 int& b = blockMode.weightISEParams.numBits;
420 m = ISEMODE_PLAIN_BIT;
427 case 2: m = ISEMODE_QUINT; b = 1; break;
428 case 3: m = ISEMODE_TRIT; b = 2; break;
429 case 4: b = 4; break;
430 case 5: m = ISEMODE_QUINT; b = 2; break;
431 case 6: m = ISEMODE_TRIT; b = 3; break;
432 case 7: b = 5; break;
433 default: DE_ASSERT(false);
440 case 2: b = 1; break;
441 case 3: m = ISEMODE_TRIT; break;
442 case 4: b = 2; break;
443 case 5: m = ISEMODE_QUINT; break;
444 case 6: m = ISEMODE_TRIT; b = 1; break;
445 case 7: b = 3; break;
446 default: DE_ASSERT(false);
452 blockMode.isError = false;
456 inline void setASTCErrorColorBlock (void* dst, int blockWidth, int blockHeight, bool isSRGB)
460 deUint8* const dstU = (deUint8*)dst;
462 for (int i = 0; i < blockWidth*blockHeight; i++)
464 dstU[4*i + 0] = 0xff;
466 dstU[4*i + 2] = 0xff;
467 dstU[4*i + 3] = 0xff;
472 float* const dstF = (float*)dst;
474 for (int i = 0; i < blockWidth*blockHeight; i++)
476 dstF[4*i + 0] = 1.0f;
477 dstF[4*i + 1] = 0.0f;
478 dstF[4*i + 2] = 1.0f;
479 dstF[4*i + 3] = 1.0f;
484 DecompressResult decodeVoidExtentBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode)
486 const deUint32 minSExtent = blockData.getBits(12, 24);
487 const deUint32 maxSExtent = blockData.getBits(25, 37);
488 const deUint32 minTExtent = blockData.getBits(38, 50);
489 const deUint32 maxTExtent = blockData.getBits(51, 63);
490 const bool allExtentsAllOnes = minSExtent == 0x1fff && maxSExtent == 0x1fff && minTExtent == 0x1fff && maxTExtent == 0x1fff;
491 const bool isHDRBlock = blockData.isBitSet(9);
493 if ((isLDRMode && isHDRBlock) || (!allExtentsAllOnes && (minSExtent >= maxSExtent || minTExtent >= maxTExtent)))
495 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
496 return DECOMPRESS_RESULT_ERROR;
499 const deUint32 rgba[4] =
501 blockData.getBits(64, 79),
502 blockData.getBits(80, 95),
503 blockData.getBits(96, 111),
504 blockData.getBits(112, 127)
509 deUint8* const dstU = (deUint8*)dst;
510 for (int i = 0; i < blockWidth*blockHeight; i++)
511 for (int c = 0; c < 4; c++)
512 dstU[i*4 + c] = (deUint8)((rgba[c] & 0xff00) >> 8);
516 float* const dstF = (float*)dst;
520 for (int c = 0; c < 4; c++)
522 if (isFloat16InfOrNan((deFloat16)rgba[c]))
523 throw InternalError("Infinity or NaN color component in HDR void extent block in ASTC texture (behavior undefined by ASTC specification)");
526 for (int i = 0; i < blockWidth*blockHeight; i++)
527 for (int c = 0; c < 4; c++)
528 dstF[i*4 + c] = deFloat16To32((deFloat16)rgba[c]);
532 for (int i = 0; i < blockWidth*blockHeight; i++)
533 for (int c = 0; c < 4; c++)
534 dstF[i*4 + c] = rgba[c] == 65535 ? 1.0f : (float)rgba[c] / 65536.0f;
538 return DECOMPRESS_RESULT_VALID_BLOCK;
541 void decodeColorEndpointModes (deUint32* endpointModesDst, const Block128& blockData, int numPartitions, int extraCemBitsStart)
543 if (numPartitions == 1)
544 endpointModesDst[0] = blockData.getBits(13, 16);
547 const deUint32 highLevelSelector = blockData.getBits(23, 24);
549 if (highLevelSelector == 0)
551 const deUint32 mode = blockData.getBits(25, 28);
552 for (int i = 0; i < numPartitions; i++)
553 endpointModesDst[i] = mode;
557 for (int partNdx = 0; partNdx < numPartitions; partNdx++)
559 const deUint32 cemClass = highLevelSelector - (blockData.isBitSet(25 + partNdx) ? 0 : 1);
560 const deUint32 lowBit0Ndx = numPartitions + 2*partNdx;
561 const deUint32 lowBit1Ndx = numPartitions + 2*partNdx + 1;
562 const deUint32 lowBit0 = blockData.getBit(lowBit0Ndx < 4 ? 25+lowBit0Ndx : extraCemBitsStart+lowBit0Ndx-4);
563 const deUint32 lowBit1 = blockData.getBit(lowBit1Ndx < 4 ? 25+lowBit1Ndx : extraCemBitsStart+lowBit1Ndx-4);
565 endpointModesDst[partNdx] = (cemClass << 2) | (lowBit1 << 1) | lowBit0;
571 int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions)
574 for (int i = 0; i < numPartitions; i++)
575 result += computeNumColorEndpointValues(endpointModes[i]);
579 void decodeISETritBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
581 DE_ASSERT(de::inRange(numValues, 1, 5));
585 m[0] = data.getNext(numBits);
586 deUint32 T01 = data.getNext(2);
587 m[1] = data.getNext(numBits);
588 deUint32 T23 = data.getNext(2);
589 m[2] = data.getNext(numBits);
590 deUint32 T4 = data.getNext(1);
591 m[3] = data.getNext(numBits);
592 deUint32 T56 = data.getNext(2);
593 m[4] = data.getNext(numBits);
594 deUint32 T7 = data.getNext(1);
598 // \note Fall-throughs.
608 const deUint32 T = (T7 << 7) | (T56 << 5) | (T4 << 4) | (T23 << 2) | (T01 << 0);
610 static const deUint32 tritsFromT[256][5] =
612 { 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 },
613 { 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 },
614 { 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 },
615 { 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 },
616 { 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 },
617 { 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 },
618 { 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 },
619 { 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 },
620 { 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 },
621 { 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 },
622 { 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 },
623 { 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 },
624 { 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 },
625 { 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 },
626 { 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 },
627 { 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 }
630 const deUint32 (& trits)[5] = tritsFromT[T];
632 for (int i = 0; i < numValues; i++)
635 dst[i].tq = trits[i];
636 dst[i].v = (trits[i] << numBits) + m[i];
640 void decodeISEQuintBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
642 DE_ASSERT(de::inRange(numValues, 1, 3));
646 m[0] = data.getNext(numBits);
647 deUint32 Q012 = data.getNext(3);
648 m[1] = data.getNext(numBits);
649 deUint32 Q34 = data.getNext(2);
650 m[2] = data.getNext(numBits);
651 deUint32 Q56 = data.getNext(2);
655 // \note Fall-throughs.
663 const deUint32 Q = (Q56 << 5) | (Q34 << 3) | (Q012 << 0);
665 static const deUint32 quintsFromQ[256][3] =
667 { 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 },
668 { 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 },
669 { 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 },
670 { 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 },
671 { 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 },
672 { 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 },
673 { 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 },
674 { 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 }
677 const deUint32 (& quints)[3] = quintsFromQ[Q];
679 for (int i = 0; i < numValues; i++)
682 dst[i].tq = quints[i];
683 dst[i].v = (quints[i] << numBits) + m[i];
687 inline void decodeISEBitBlock (ISEDecodedResult* dst, BitAccessStream& data, int numBits)
689 dst[0].m = data.getNext(numBits);
693 void decodeISE (ISEDecodedResult* dst, int numValues, BitAccessStream& data, const ISEParams& params)
695 if (params.mode == ISEMODE_TRIT)
697 const int numBlocks = deDivRoundUp32(numValues, 5);
698 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
700 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
701 decodeISETritBlock(&dst[5*blockNdx], numValuesInBlock, data, params.numBits);
704 else if (params.mode == ISEMODE_QUINT)
706 const int numBlocks = deDivRoundUp32(numValues, 3);
707 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
709 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
710 decodeISEQuintBlock(&dst[3*blockNdx], numValuesInBlock, data, params.numBits);
715 DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
716 for (int i = 0; i < numValues; i++)
717 decodeISEBitBlock(&dst[i], data, params.numBits);
721 void unquantizeColorEndpoints (deUint32* dst, const ISEDecodedResult* iseResults, int numEndpoints, const ISEParams& iseParams)
723 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
725 const int rangeCase = iseParams.numBits*2 - (iseParams.mode == ISEMODE_TRIT ? 2 : 1);
726 DE_ASSERT(de::inRange(rangeCase, 0, 10));
727 static const deUint32 Ca[11] = { 204, 113, 93, 54, 44, 26, 22, 13, 11, 6, 5 };
728 const deUint32 C = Ca[rangeCase];
730 for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
732 const deUint32 a = getBit(iseResults[endpointNdx].m, 0);
733 const deUint32 b = getBit(iseResults[endpointNdx].m, 1);
734 const deUint32 c = getBit(iseResults[endpointNdx].m, 2);
735 const deUint32 d = getBit(iseResults[endpointNdx].m, 3);
736 const deUint32 e = getBit(iseResults[endpointNdx].m, 4);
737 const deUint32 f = getBit(iseResults[endpointNdx].m, 5);
739 const deUint32 A = a == 0 ? 0 : (1<<9)-1;
740 const deUint32 B = rangeCase == 0 ? 0
742 : rangeCase == 2 ? (b << 8) | (b << 4) | (b << 2) | (b << 1)
743 : rangeCase == 3 ? (b << 8) | (b << 3) | (b << 2)
744 : rangeCase == 4 ? (c << 8) | (b << 7) | (c << 3) | (b << 2) | (c << 1) | (b << 0)
745 : rangeCase == 5 ? (c << 8) | (b << 7) | (c << 2) | (b << 1) | (c << 0)
746 : rangeCase == 6 ? (d << 8) | (c << 7) | (b << 6) | (d << 2) | (c << 1) | (b << 0)
747 : rangeCase == 7 ? (d << 8) | (c << 7) | (b << 6) | (d << 1) | (c << 0)
748 : rangeCase == 8 ? (e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 1) | (d << 0)
749 : rangeCase == 9 ? (e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 0)
750 : rangeCase == 10 ? (f << 8) | (e << 7) | (d << 6) | (c << 5) | (b << 4) | (f << 0)
752 DE_ASSERT(B != (deUint32)-1);
754 dst[endpointNdx] = (((iseResults[endpointNdx].tq*C + B) ^ A) >> 2) | (A & 0x80);
759 DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
761 for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
762 dst[endpointNdx] = bitReplicationScale(iseResults[endpointNdx].v, iseParams.numBits, 8);
766 inline void bitTransferSigned (deInt32& a, deInt32& b)
776 inline UVec4 clampedRGBA (const IVec4& rgba)
778 return UVec4(de::clamp(rgba.x(), 0, 0xff),
779 de::clamp(rgba.y(), 0, 0xff),
780 de::clamp(rgba.z(), 0, 0xff),
781 de::clamp(rgba.w(), 0, 0xff));
784 inline IVec4 blueContract (int r, int g, int b, int a)
786 return IVec4((r+b)>>1, (g+b)>>1, b, a);
789 inline bool isColorEndpointModeHDR (deUint32 mode)
799 void decodeHDREndpointMode7 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3)
801 const deUint32 m10 = getBit(v1, 7) | (getBit(v2, 7) << 1);
802 const deUint32 m23 = getBits(v0, 6, 7);
803 const deUint32 majComp = m10 != 3 ? m10
806 const deUint32 mode = m10 != 3 ? m23
810 deInt32 red = (deInt32)getBits(v0, 0, 5);
811 deInt32 green = (deInt32)getBits(v1, 0, 4);
812 deInt32 blue = (deInt32)getBits(v2, 0, 4);
813 deInt32 scale = (deInt32)getBits(v3, 0, 4);
816 #define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
817 #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)
819 const deUint32 x0 = getBit(v1, 6);
820 const deUint32 x1 = getBit(v1, 5);
821 const deUint32 x2 = getBit(v2, 6);
822 const deUint32 x3 = getBit(v2, 5);
823 const deUint32 x4 = getBit(v3, 7);
824 const deUint32 x5 = getBit(v3, 6);
825 const deUint32 x6 = getBit(v3, 5);
834 case 0: ASSIGN_X_BITS(R,9, R,8, R,7, R,10, R,6, S,6, S,5); break;
835 case 1: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, R,10, R,9); break;
836 case 2: ASSIGN_X_BITS(R,9, R,8, R,7, R,6, S,7, S,6, S,5); break;
837 case 3: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, S,6, S,5); break;
838 case 4: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, R,7, S,5); break;
839 case 5: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, S,6, S,5); break;
848 static const int shiftAmounts[] = { 1, 1, 2, 3, 4, 5 };
849 DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(shiftAmounts));
851 red <<= shiftAmounts[mode];
852 green <<= shiftAmounts[mode];
853 blue <<= shiftAmounts[mode];
854 scale <<= shiftAmounts[mode];
863 std::swap(red, green);
864 else if (majComp == 2)
865 std::swap(red, blue);
867 e0 = UVec4(de::clamp(red - scale, 0, 0xfff),
868 de::clamp(green - scale, 0, 0xfff),
869 de::clamp(blue - scale, 0, 0xfff),
872 e1 = UVec4(de::clamp(red, 0, 0xfff),
873 de::clamp(green, 0, 0xfff),
874 de::clamp(blue, 0, 0xfff),
878 void decodeHDREndpointMode11 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5)
880 const deUint32 major = (getBit(v5, 7) << 1) | getBit(v4, 7);
884 e0 = UVec4(v0<<4, v2<<4, getBits(v4,0,6)<<5, 0x780);
885 e1 = UVec4(v1<<4, v3<<4, getBits(v5,0,6)<<5, 0x780);
889 const deUint32 mode = (getBit(v3, 7) << 2) | (getBit(v2, 7) << 1) | getBit(v1, 7);
891 deInt32 a = (deInt32)((getBit(v1, 6) << 8) | v0);
892 deInt32 c = (deInt32)(getBits(v1, 0, 5));
893 deInt32 b0 = (deInt32)(getBits(v2, 0, 5));
894 deInt32 b1 = (deInt32)(getBits(v3, 0, 5));
895 deInt32 d0 = (deInt32)(getBits(v4, 0, 4));
896 deInt32 d1 = (deInt32)(getBits(v5, 0, 4));
899 #define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
900 #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)
902 const deUint32 x0 = getBit(v2, 6);
903 const deUint32 x1 = getBit(v3, 6);
904 const deUint32 x2 = getBit(v4, 6);
905 const deUint32 x3 = getBit(v5, 6);
906 const deUint32 x4 = getBit(v4, 5);
907 const deUint32 x5 = getBit(v5, 5);
911 case 0: ASSIGN_X_BITS(b0,6, b1,6, d0,6, d1,6, d0,5, d1,5); break;
912 case 1: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, d0,5, d1,5); break;
913 case 2: ASSIGN_X_BITS(a,9, c,6, d0,6, d1,6, d0,5, d1,5); break;
914 case 3: ASSIGN_X_BITS(b0,6, b1,6, a,9, c,6, d0,5, d1,5); break;
915 case 4: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, a,9, a,10); break;
916 case 5: ASSIGN_X_BITS(a,9, a,10, c,7, c,6, d0,5, d1,5); break;
917 case 6: ASSIGN_X_BITS(b0,6, b1,6, a,11, c,6, a,9, a,10); break;
918 case 7: ASSIGN_X_BITS(a,9, a,10, a,11, c,6, d0,5, d1,5); break;
927 static const int numDBits[] = { 7, 6, 7, 6, 5, 6, 5, 6 };
928 DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(numDBits));
930 d0 = signExtend(d0, numDBits[mode]);
931 d1 = signExtend(d1, numDBits[mode]);
933 const int shiftAmount = (mode >> 1) ^ 3;
941 e0 = UVec4(de::clamp(a-c, 0, 0xfff),
942 de::clamp(a-b0-c-d0, 0, 0xfff),
943 de::clamp(a-b1-c-d1, 0, 0xfff),
946 e1 = UVec4(de::clamp(a, 0, 0xfff),
947 de::clamp(a-b0, 0, 0xfff),
948 de::clamp(a-b1, 0, 0xfff),
953 std::swap(e0.x(), e0.y());
954 std::swap(e1.x(), e1.y());
958 std::swap(e0.x(), e0.z());
959 std::swap(e1.x(), e1.z());
964 void decodeHDREndpointMode15(UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5, deUint32 v6In, deUint32 v7In)
966 decodeHDREndpointMode11(e0, e1, v0, v1, v2, v3, v4, v5);
968 const deUint32 mode = (getBit(v7In, 7) << 1) | getBit(v6In, 7);
969 deInt32 v6 = (deInt32)getBits(v6In, 0, 6);
970 deInt32 v7 = (deInt32)getBits(v7In, 0, 6);
979 v6 |= (v7 << (mode+1)) & 0x780;
980 v7 &= (0x3f >> mode);
987 v7 = de::clamp(v7, 0, 0xfff);
993 void decodeColorEndpoints (ColorEndpointPair* dst, const deUint32* unquantizedEndpoints, const deUint32* endpointModes, int numPartitions)
995 int unquantizedNdx = 0;
997 for (int partitionNdx = 0; partitionNdx < numPartitions; partitionNdx++)
999 const deUint32 endpointMode = endpointModes[partitionNdx];
1000 const deUint32* v = &unquantizedEndpoints[unquantizedNdx];
1001 UVec4& e0 = dst[partitionNdx].e0;
1002 UVec4& e1 = dst[partitionNdx].e1;
1004 unquantizedNdx += computeNumColorEndpointValues(endpointMode);
1006 switch (endpointMode)
1009 e0 = UVec4(v[0], v[0], v[0], 0xff);
1010 e1 = UVec4(v[1], v[1], v[1], 0xff);
1015 const deUint32 L0 = (v[0] >> 2) | (getBits(v[1], 6, 7) << 6);
1016 const deUint32 L1 = de::min(0xffu, L0 + getBits(v[1], 0, 5));
1017 e0 = UVec4(L0, L0, L0, 0xff);
1018 e1 = UVec4(L1, L1, L1, 0xff);
1024 const deUint32 v1Gr = v[1] >= v[0];
1025 const deUint32 y0 = v1Gr ? v[0]<<4 : (v[1]<<4) + 8;
1026 const deUint32 y1 = v1Gr ? v[1]<<4 : (v[0]<<4) - 8;
1028 e0 = UVec4(y0, y0, y0, 0x780);
1029 e1 = UVec4(y1, y1, y1, 0x780);
1035 const bool m = isBitSet(v[0], 7);
1036 const deUint32 y0 = m ? (getBits(v[1], 5, 7) << 9) | (getBits(v[0], 0, 6) << 2)
1037 : (getBits(v[1], 4, 7) << 8) | (getBits(v[0], 0, 6) << 1);
1038 const deUint32 d = m ? getBits(v[1], 0, 4) << 2
1039 : getBits(v[1], 0, 3) << 1;
1040 const deUint32 y1 = de::min(0xfffu, y0+d);
1042 e0 = UVec4(y0, y0, y0, 0x780);
1043 e1 = UVec4(y1, y1, y1, 0x780);
1048 e0 = UVec4(v[0], v[0], v[0], v[2]);
1049 e1 = UVec4(v[1], v[1], v[1], v[3]);
1054 deInt32 v0 = (deInt32)v[0];
1055 deInt32 v1 = (deInt32)v[1];
1056 deInt32 v2 = (deInt32)v[2];
1057 deInt32 v3 = (deInt32)v[3];
1058 bitTransferSigned(v1, v0);
1059 bitTransferSigned(v3, v2);
1061 e0 = clampedRGBA(IVec4(v0, v0, v0, v2));
1062 e1 = clampedRGBA(IVec4(v0+v1, v0+v1, v0+v1, v2+v3));
1067 e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, 0xff);
1068 e1 = UVec4(v[0], v[1], v[2], 0xff);
1072 decodeHDREndpointMode7(e0, e1, v[0], v[1], v[2], v[3]);
1076 if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
1078 e0 = UVec4(v[0], v[2], v[4], 0xff);
1079 e1 = UVec4(v[1], v[3], v[5], 0xff);
1083 e0 = blueContract(v[1], v[3], v[5], 0xff).asUint();
1084 e1 = blueContract(v[0], v[2], v[4], 0xff).asUint();
1090 deInt32 v0 = (deInt32)v[0];
1091 deInt32 v1 = (deInt32)v[1];
1092 deInt32 v2 = (deInt32)v[2];
1093 deInt32 v3 = (deInt32)v[3];
1094 deInt32 v4 = (deInt32)v[4];
1095 deInt32 v5 = (deInt32)v[5];
1096 bitTransferSigned(v1, v0);
1097 bitTransferSigned(v3, v2);
1098 bitTransferSigned(v5, v4);
1102 e0 = clampedRGBA(IVec4(v0, v2, v4, 0xff));
1103 e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, 0xff));
1107 e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, 0xff));
1108 e1 = clampedRGBA(blueContract(v0, v2, v4, 0xff));
1114 e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, v[4]);
1115 e1 = UVec4(v[0], v[1], v[2], v[5]);
1119 decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
1123 if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
1125 e0 = UVec4(v[0], v[2], v[4], v[6]);
1126 e1 = UVec4(v[1], v[3], v[5], v[7]);
1130 e0 = clampedRGBA(blueContract(v[1], v[3], v[5], v[7]));
1131 e1 = clampedRGBA(blueContract(v[0], v[2], v[4], v[6]));
1137 deInt32 v0 = (deInt32)v[0];
1138 deInt32 v1 = (deInt32)v[1];
1139 deInt32 v2 = (deInt32)v[2];
1140 deInt32 v3 = (deInt32)v[3];
1141 deInt32 v4 = (deInt32)v[4];
1142 deInt32 v5 = (deInt32)v[5];
1143 deInt32 v6 = (deInt32)v[6];
1144 deInt32 v7 = (deInt32)v[7];
1145 bitTransferSigned(v1, v0);
1146 bitTransferSigned(v3, v2);
1147 bitTransferSigned(v5, v4);
1148 bitTransferSigned(v7, v6);
1152 e0 = clampedRGBA(IVec4(v0, v2, v4, v6));
1153 e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, v6+v7));
1157 e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, v6+v7));
1158 e1 = clampedRGBA(blueContract(v0, v2, v4, v6));
1165 decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
1171 decodeHDREndpointMode15(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
1180 void computeColorEndpoints (ColorEndpointPair* dst, const Block128& blockData, const deUint32* endpointModes, int numPartitions, int numColorEndpointValues, const ISEParams& iseParams, int numBitsAvailable)
1182 const int colorEndpointDataStart = numPartitions == 1 ? 17 : 29;
1183 ISEDecodedResult colorEndpointData[18];
1186 BitAccessStream dataStream(blockData, colorEndpointDataStart, numBitsAvailable, true);
1187 decodeISE(&colorEndpointData[0], numColorEndpointValues, dataStream, iseParams);
1191 deUint32 unquantizedEndpoints[18];
1192 unquantizeColorEndpoints(&unquantizedEndpoints[0], &colorEndpointData[0], numColorEndpointValues, iseParams);
1193 decodeColorEndpoints(dst, &unquantizedEndpoints[0], &endpointModes[0], numPartitions);
1197 void unquantizeWeights (deUint32 dst[64], const ISEDecodedResult* weightGrid, const ASTCBlockMode& blockMode)
1199 const int numWeights = computeNumWeights(blockMode);
1200 const ISEParams& iseParams = blockMode.weightISEParams;
1202 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
1204 const int rangeCase = iseParams.numBits*2 + (iseParams.mode == ISEMODE_QUINT ? 1 : 0);
1206 if (rangeCase == 0 || rangeCase == 1)
1208 static const deUint32 map0[3] = { 0, 32, 63 };
1209 static const deUint32 map1[5] = { 0, 16, 32, 47, 63 };
1210 const deUint32* const map = rangeCase == 0 ? &map0[0] : &map1[0];
1211 for (int i = 0; i < numWeights; i++)
1213 DE_ASSERT(weightGrid[i].v < (rangeCase == 0 ? 3u : 5u));
1214 dst[i] = map[weightGrid[i].v];
1219 DE_ASSERT(rangeCase <= 6);
1220 static const deUint32 Ca[5] = { 50, 28, 23, 13, 11 };
1221 const deUint32 C = Ca[rangeCase-2];
1223 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1225 const deUint32 a = getBit(weightGrid[weightNdx].m, 0);
1226 const deUint32 b = getBit(weightGrid[weightNdx].m, 1);
1227 const deUint32 c = getBit(weightGrid[weightNdx].m, 2);
1229 const deUint32 A = a == 0 ? 0 : (1<<7)-1;
1230 const deUint32 B = rangeCase == 2 ? 0
1231 : rangeCase == 3 ? 0
1232 : rangeCase == 4 ? (b << 6) | (b << 2) | (b << 0)
1233 : rangeCase == 5 ? (b << 6) | (b << 1)
1234 : rangeCase == 6 ? (c << 6) | (b << 5) | (c << 1) | (b << 0)
1237 dst[weightNdx] = (((weightGrid[weightNdx].tq*C + B) ^ A) >> 2) | (A & 0x20);
1243 DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
1245 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1246 dst[weightNdx] = bitReplicationScale(weightGrid[weightNdx].v, iseParams.numBits, 6);
1249 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1250 dst[weightNdx] += dst[weightNdx] > 32 ? 1 : 0;
1252 // Initialize nonexistent weights to poison values
1253 for (int weightNdx = numWeights; weightNdx < 64; weightNdx++)
1254 dst[weightNdx] = ~0u;
1258 void interpolateWeights (TexelWeightPair* dst, const deUint32* unquantizedWeights, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
1260 const int numWeightsPerTexel = blockMode.isDualPlane ? 2 : 1;
1261 const deUint32 scaleX = (1024 + blockWidth/2) / (blockWidth-1);
1262 const deUint32 scaleY = (1024 + blockHeight/2) / (blockHeight-1);
1264 for (int texelY = 0; texelY < blockHeight; texelY++)
1266 for (int texelX = 0; texelX < blockWidth; texelX++)
1268 const deUint32 gX = (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;
1269 const deUint32 gY = (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;
1270 const deUint32 jX = gX >> 4;
1271 const deUint32 jY = gY >> 4;
1272 const deUint32 fX = gX & 0xf;
1273 const deUint32 fY = gY & 0xf;
1274 const deUint32 w11 = (fX*fY + 8) >> 4;
1275 const deUint32 w10 = fY - w11;
1276 const deUint32 w01 = fX - w11;
1277 const deUint32 w00 = 16 - fX - fY + w11;
1278 const deUint32 v0 = jY*blockMode.weightGridWidth + jX;
1280 for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)
1282 const deUint32 p00 = unquantizedWeights[(v0) * numWeightsPerTexel + texelWeightNdx];
1283 const deUint32 p01 = unquantizedWeights[(v0 + 1) * numWeightsPerTexel + texelWeightNdx];
1284 const deUint32 p10 = unquantizedWeights[(v0 + blockMode.weightGridWidth) * numWeightsPerTexel + texelWeightNdx];
1285 const deUint32 p11 = unquantizedWeights[(v0 + blockMode.weightGridWidth + 1) * numWeightsPerTexel + texelWeightNdx];
1287 dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;
1293 void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
1295 ISEDecodedResult weightGrid[64];
1298 BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);
1299 decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);
1303 deUint32 unquantizedWeights[64];
1304 unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);
1305 interpolateWeights(dst, &unquantizedWeights[0], blockWidth, blockHeight, blockMode);
1309 inline deUint32 hash52 (deUint32 v)
1312 p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
1313 p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
1314 p ^= p << 6; p ^= p >> 17;
1318 int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)
1320 DE_ASSERT(zIn == 0);
1321 const deUint32 x = smallBlock ? xIn << 1 : xIn;
1322 const deUint32 y = smallBlock ? yIn << 1 : yIn;
1323 const deUint32 z = smallBlock ? zIn << 1 : zIn;
1324 const deUint32 seed = seedIn + 1024*(numPartitions-1);
1325 const deUint32 rnum = hash52(seed);
1326 deUint8 seed1 = (deUint8)( rnum & 0xf);
1327 deUint8 seed2 = (deUint8)((rnum >> 4) & 0xf);
1328 deUint8 seed3 = (deUint8)((rnum >> 8) & 0xf);
1329 deUint8 seed4 = (deUint8)((rnum >> 12) & 0xf);
1330 deUint8 seed5 = (deUint8)((rnum >> 16) & 0xf);
1331 deUint8 seed6 = (deUint8)((rnum >> 20) & 0xf);
1332 deUint8 seed7 = (deUint8)((rnum >> 24) & 0xf);
1333 deUint8 seed8 = (deUint8)((rnum >> 28) & 0xf);
1334 deUint8 seed9 = (deUint8)((rnum >> 18) & 0xf);
1335 deUint8 seed10 = (deUint8)((rnum >> 22) & 0xf);
1336 deUint8 seed11 = (deUint8)((rnum >> 26) & 0xf);
1337 deUint8 seed12 = (deUint8)(((rnum >> 30) | (rnum << 2)) & 0xf);
1339 seed1 = (deUint8)(seed1 * seed1 );
1340 seed2 = (deUint8)(seed2 * seed2 );
1341 seed3 = (deUint8)(seed3 * seed3 );
1342 seed4 = (deUint8)(seed4 * seed4 );
1343 seed5 = (deUint8)(seed5 * seed5 );
1344 seed6 = (deUint8)(seed6 * seed6 );
1345 seed7 = (deUint8)(seed7 * seed7 );
1346 seed8 = (deUint8)(seed8 * seed8 );
1347 seed9 = (deUint8)(seed9 * seed9 );
1348 seed10 = (deUint8)(seed10 * seed10);
1349 seed11 = (deUint8)(seed11 * seed11);
1350 seed12 = (deUint8)(seed12 * seed12);
1352 const int shA = (seed & 2) != 0 ? 4 : 5;
1353 const int shB = numPartitions == 3 ? 6 : 5;
1354 const int sh1 = (seed & 1) != 0 ? shA : shB;
1355 const int sh2 = (seed & 1) != 0 ? shB : shA;
1356 const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;
1358 seed1 = (deUint8)(seed1 >> sh1);
1359 seed2 = (deUint8)(seed2 >> sh2);
1360 seed3 = (deUint8)(seed3 >> sh1);
1361 seed4 = (deUint8)(seed4 >> sh2);
1362 seed5 = (deUint8)(seed5 >> sh1);
1363 seed6 = (deUint8)(seed6 >> sh2);
1364 seed7 = (deUint8)(seed7 >> sh1);
1365 seed8 = (deUint8)(seed8 >> sh2);
1366 seed9 = (deUint8)(seed9 >> sh3);
1367 seed10 = (deUint8)(seed10 >> sh3);
1368 seed11 = (deUint8)(seed11 >> sh3);
1369 seed12 = (deUint8)(seed12 >> sh3);
1371 const int a = 0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));
1372 const int b = 0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));
1373 const int c = numPartitions >= 3 ? 0x3f & (seed5*x + seed6*y + seed9*z + (rnum >> 6)) : 0;
1374 const int d = numPartitions >= 4 ? 0x3f & (seed7*x + seed8*y + seed10*z + (rnum >> 2)) : 0;
1376 return a >= b && a >= c && a >= d ? 0
1377 : b >= c && b >= d ? 1
1382 DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,
1383 int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)
1385 const bool smallBlock = blockWidth*blockHeight < 31;
1386 DecompressResult result = DECOMPRESS_RESULT_VALID_BLOCK;
1387 bool isHDREndpoint[4];
1389 for (int i = 0; i < numPartitions; i++)
1390 isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);
1392 for (int texelY = 0; texelY < blockHeight; texelY++)
1393 for (int texelX = 0; texelX < blockWidth; texelX++)
1395 const int texelNdx = texelY*blockWidth + texelX;
1396 const int colorEndpointNdx = numPartitions == 1 ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);
1397 DE_ASSERT(colorEndpointNdx < numPartitions);
1398 const UVec4& e0 = colorEndpoints[colorEndpointNdx].e0;
1399 const UVec4& e1 = colorEndpoints[colorEndpointNdx].e1;
1400 const TexelWeightPair& weight = texelWeights[texelNdx];
1402 if (isLDRMode && isHDREndpoint[colorEndpointNdx])
1406 ((deUint8*)dst)[texelNdx*4 + 0] = 0xff;
1407 ((deUint8*)dst)[texelNdx*4 + 1] = 0;
1408 ((deUint8*)dst)[texelNdx*4 + 2] = 0xff;
1409 ((deUint8*)dst)[texelNdx*4 + 3] = 0xff;
1413 ((float*)dst)[texelNdx*4 + 0] = 1.0f;
1414 ((float*)dst)[texelNdx*4 + 1] = 0;
1415 ((float*)dst)[texelNdx*4 + 2] = 1.0f;
1416 ((float*)dst)[texelNdx*4 + 3] = 1.0f;
1419 result = DECOMPRESS_RESULT_ERROR;
1423 for (int channelNdx = 0; channelNdx < 4; channelNdx++)
1425 if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.
1427 const deUint32 c0 = (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);
1428 const deUint32 c1 = (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);
1429 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1430 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1433 ((deUint8*)dst)[texelNdx*4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);
1435 ((float*)dst)[texelNdx*4 + channelNdx] = c == 65535 ? 1.0f : (float)c / 65536.0f;
1439 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1440 const deUint32 c0 = e0[channelNdx] << 4;
1441 const deUint32 c1 = e1[channelNdx] << 4;
1442 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1443 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1444 const deUint32 e = getBits(c, 11, 15);
1445 const deUint32 m = getBits(c, 0, 10);
1446 const deUint32 mt = m < 512 ? 3*m
1447 : m >= 1536 ? 5*m - 2048
1449 const deFloat16 cf = (deFloat16)((e << 10) + (mt >> 3));
1451 ((float*)dst)[texelNdx*4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);
1460 DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)
1462 DE_ASSERT(isLDR || !isSRGB);
1464 // Decode block mode.
1466 const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));
1468 // Check for block mode errors.
1470 if (blockMode.isError)
1472 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1473 return DECOMPRESS_RESULT_ERROR;
1476 // Separate path for void-extent.
1478 if (blockMode.isVoidExtent)
1479 return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);
1481 // Compute weight grid values.
1483 const int numWeights = computeNumWeights(blockMode);
1484 const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);
1485 const int numPartitions = (int)blockData.getBits(11, 12) + 1;
1487 // Check for errors in weight grid, partition and dual-plane parameters.
1489 if (numWeights > 64 ||
1490 numWeightDataBits > 96 ||
1491 numWeightDataBits < 24 ||
1492 blockMode.weightGridWidth > blockWidth ||
1493 blockMode.weightGridHeight > blockHeight ||
1494 (numPartitions == 4 && blockMode.isDualPlane))
1496 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1497 return DECOMPRESS_RESULT_ERROR;
1500 // Compute number of bits available for color endpoint data.
1502 const bool isSingleUniqueCem = numPartitions == 1 || blockData.getBits(23, 24) == 0;
1503 const int numConfigDataBits = (numPartitions == 1 ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +
1504 (blockMode.isDualPlane ? 2 : 0);
1505 const int numBitsForColorEndpoints = 128 - numWeightDataBits - numConfigDataBits;
1506 const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -1
1507 : numPartitions == 4 ? 7
1508 : numPartitions == 3 ? 4
1509 : numPartitions == 2 ? 1
1511 // Decode color endpoint modes.
1513 deUint32 colorEndpointModes[4];
1514 decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);
1516 const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);
1518 // Check for errors in color endpoint value count.
1520 if (numColorEndpointValues > 18 || numBitsForColorEndpoints < deDivRoundUp32(13*numColorEndpointValues, 5))
1522 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1523 return DECOMPRESS_RESULT_ERROR;
1526 // Compute color endpoints.
1528 ColorEndpointPair colorEndpoints[4];
1529 computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,
1530 computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);
1532 // Compute texel weights.
1534 TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];
1535 computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);
1537 // Set texel colors.
1539 const int ccs = blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;
1540 const deUint32 partitionIndexSeed = numPartitions > 1 ? blockData.getBits(13, 22) : (deUint32)-1;
1542 return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);
1545 void decompress (const PixelBufferAccess& dst, const deUint8* data, bool isSRGB, bool isLDR)
1547 DE_ASSERT(isLDR || !isSRGB);
1549 const int blockWidth = dst.getWidth();
1550 const int blockHeight = dst.getHeight();
1554 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1555 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1556 } decompressedBuffer;
1558 const Block128 blockData(data);
1559 decompressBlock(isSRGB ? (void*)&decompressedBuffer.sRGB[0] : (void*)&decompressedBuffer.linear[0],
1560 blockData, dst.getWidth(), dst.getHeight(), isSRGB, isLDR);
1564 for (int i = 0; i < blockHeight; i++)
1565 for (int j = 0; j < blockWidth; j++)
1567 dst.setPixel(IVec4(decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 0],
1568 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 1],
1569 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 2],
1570 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 3]), j, i);
1575 for (int i = 0; i < blockHeight; i++)
1576 for (int j = 0; j < blockWidth; j++)
1578 dst.setPixel(Vec4(decompressedBuffer.linear[(i*blockWidth + j) * 4 + 0],
1579 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 1],
1580 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 2],
1581 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 3]), j, i);
1586 // Helper class for setting bits in a 128-bit block.
1587 class AssignBlock128
1590 typedef deUint64 Word;
1594 WORD_BYTES = sizeof(Word),
1595 WORD_BITS = 8*WORD_BYTES,
1596 NUM_WORDS = 128 / WORD_BITS
1599 DE_STATIC_ASSERT(128 % WORD_BITS == 0);
1602 AssignBlock128 (void)
1604 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1605 m_words[wordNdx] = 0;
1608 void setBit (int ndx, deUint32 val)
1610 DE_ASSERT(de::inBounds(ndx, 0, 128));
1611 DE_ASSERT((val & 1) == val);
1612 const int wordNdx = ndx / WORD_BITS;
1613 const int bitNdx = ndx % WORD_BITS;
1614 m_words[wordNdx] = (m_words[wordNdx] & ~((Word)1 << bitNdx)) | ((Word)val << bitNdx);
1617 void setBits (int low, int high, deUint32 bits)
1619 DE_ASSERT(de::inBounds(low, 0, 128));
1620 DE_ASSERT(de::inBounds(high, 0, 128));
1621 DE_ASSERT(de::inRange(high-low+1, 0, 32));
1622 DE_ASSERT((bits & (((Word)1 << (high-low+1)) - 1)) == bits);
1624 if (high-low+1 == 0)
1627 const int word0Ndx = low / WORD_BITS;
1628 const int word1Ndx = high / WORD_BITS;
1629 const int lowNdxInW0 = low % WORD_BITS;
1631 if (word0Ndx == word1Ndx)
1632 m_words[word0Ndx] = (m_words[word0Ndx] & ~((((Word)1 << (high-low+1)) - 1) << lowNdxInW0)) | ((Word)bits << lowNdxInW0);
1635 DE_ASSERT(word1Ndx == word0Ndx + 1);
1637 const int highNdxInW1 = high % WORD_BITS;
1638 const int numBitsToSetInW0 = WORD_BITS - lowNdxInW0;
1639 const Word bitsLowMask = ((Word)1 << numBitsToSetInW0) - 1;
1641 m_words[word0Ndx] = (m_words[word0Ndx] & (((Word)1 << lowNdxInW0) - 1)) | (((Word)bits & bitsLowMask) << lowNdxInW0);
1642 m_words[word1Ndx] = (m_words[word1Ndx] & ~(((Word)1 << (highNdxInW1+1)) - 1)) | (((Word)bits & ~bitsLowMask) >> numBitsToSetInW0);
1646 void assignToMemory (deUint8* dst) const
1648 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1650 for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
1651 dst[wordNdx*WORD_BYTES + byteNdx] = (deUint8)((m_words[wordNdx] >> (8*byteNdx)) & 0xff);
1655 void pushBytesToVector (vector<deUint8>& dst) const
1657 const int assignStartIndex = (int)dst.size();
1658 dst.resize(dst.size() + BLOCK_SIZE_BYTES);
1659 assignToMemory(&dst[assignStartIndex]);
1663 Word m_words[NUM_WORDS];
1666 // A helper for sequential access into a AssignBlock128.
1667 class BitAssignAccessStream
1670 BitAssignAccessStream (AssignBlock128& dst, int startNdxInSrc, int length, bool forward)
1672 , m_startNdxInSrc (startNdxInSrc)
1674 , m_forward (forward)
1679 // Set the next num bits. Bits at positions greater than or equal to m_length are not touched.
1680 void setNext (int num, deUint32 bits)
1682 DE_ASSERT((bits & (((deUint64)1 << num) - 1)) == bits);
1684 if (num == 0 || m_ndx >= m_length)
1687 const int end = m_ndx + num;
1688 const int numBitsToDst = de::max(0, de::min(m_length, end) - m_ndx);
1689 const int low = m_ndx;
1690 const int high = m_ndx + numBitsToDst - 1;
1691 const deUint32 actualBits = getBits(bits, 0, numBitsToDst-1);
1695 return m_forward ? m_dst.setBits(m_startNdxInSrc + low, m_startNdxInSrc + high, actualBits)
1696 : m_dst.setBits(m_startNdxInSrc - high, m_startNdxInSrc - low, reverseBits(actualBits, numBitsToDst));
1700 AssignBlock128& m_dst;
1701 const int m_startNdxInSrc;
1703 const bool m_forward;
1708 struct VoidExtentParams
1710 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1716 // \note Currently extent coordinates are all set to all-ones.
1718 VoidExtentParams (bool isHDR_, deUint16 r_, deUint16 g_, deUint16 b_, deUint16 a_) : isHDR(isHDR_), r(r_), g(g_), b(b_), a(a_) {}
1721 static AssignBlock128 generateVoidExtentBlock (const VoidExtentParams& params)
1723 AssignBlock128 block;
1725 block.setBits(0, 8, 0x1fc); // \note Marks void-extent block.
1726 block.setBit(9, params.isHDR);
1727 block.setBits(10, 11, 3); // \note Spec shows that these bits are both set, although they serve no purpose.
1729 // Extent coordinates - currently all-ones.
1730 block.setBits(12, 24, 0x1fff);
1731 block.setBits(25, 37, 0x1fff);
1732 block.setBits(38, 50, 0x1fff);
1733 block.setBits(51, 63, 0x1fff);
1735 DE_ASSERT(!params.isHDR || (!isFloat16InfOrNan(params.r) &&
1736 !isFloat16InfOrNan(params.g) &&
1737 !isFloat16InfOrNan(params.b) &&
1738 !isFloat16InfOrNan(params.a)));
1740 block.setBits(64, 79, params.r);
1741 block.setBits(80, 95, params.g);
1742 block.setBits(96, 111, params.b);
1743 block.setBits(112, 127, params.a);
1748 // An input array of ISE inputs for an entire ASTC block. Can be given as either single values in the
1749 // range [0, maximumValueOfISERange] or as explicit block value specifications. The latter is needed
1750 // so we can test all possible values of T and Q in a block, since multiple T or Q values may map
1751 // to the same set of decoded values.
1756 deUint32 tOrQValue; //!< The 8-bit T or 7-bit Q in a trit or quint ISE block.
1757 deUint32 bitValues[5];
1760 bool isGivenInBlockForm;
1763 //!< \note 64 comes from the maximum number of weight values in an ASTC block.
1769 : isGivenInBlockForm (false)
1774 static inline deUint32 computeISERangeMax (const ISEParams& iseParams)
1776 switch (iseParams.mode)
1778 case ISEMODE_TRIT: return (1u << iseParams.numBits) * 3 - 1;
1779 case ISEMODE_QUINT: return (1u << iseParams.numBits) * 5 - 1;
1780 case ISEMODE_PLAIN_BIT: return (1u << iseParams.numBits) - 1;
1787 struct NormalBlockParams
1789 int weightGridWidth;
1790 int weightGridHeight;
1791 ISEParams weightISEParams;
1793 deUint32 ccs; //! \note Irrelevant if !isDualPlane.
1795 deUint32 colorEndpointModes[4];
1796 // \note Below members are irrelevant if numPartitions == 1.
1797 bool isMultiPartSingleCemMode; //! \note If true, the single CEM is at colorEndpointModes[0].
1798 deUint32 partitionSeed;
1800 NormalBlockParams (void)
1801 : weightGridWidth (-1)
1802 , weightGridHeight (-1)
1803 , weightISEParams (ISEMODE_LAST, -1)
1804 , isDualPlane (true)
1805 , ccs ((deUint32)-1)
1806 , numPartitions (-1)
1807 , isMultiPartSingleCemMode (false)
1808 , partitionSeed ((deUint32)-1)
1810 colorEndpointModes[0] = 0;
1811 colorEndpointModes[1] = 0;
1812 colorEndpointModes[2] = 0;
1813 colorEndpointModes[3] = 0;
1817 struct NormalBlockISEInputs
1822 NormalBlockISEInputs (void)
1829 static inline int computeNumWeights (const NormalBlockParams& params)
1831 return params.weightGridWidth * params.weightGridHeight * (params.isDualPlane ? 2 : 1);
1834 static inline int computeNumBitsForColorEndpoints (const NormalBlockParams& params)
1836 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, computeNumWeights(params));
1837 const int numConfigDataBits = (params.numPartitions == 1 ? 17 : params.isMultiPartSingleCemMode ? 29 : 25 + 3*params.numPartitions) +
1838 (params.isDualPlane ? 2 : 0);
1840 return 128 - numWeightBits - numConfigDataBits;
1843 static inline int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions, bool isMultiPartSingleCemMode)
1845 if (isMultiPartSingleCemMode)
1846 return numPartitions * computeNumColorEndpointValues(endpointModes[0]);
1850 for (int i = 0; i < numPartitions; i++)
1851 result += computeNumColorEndpointValues(endpointModes[i]);
1856 static inline bool isValidBlockParams (const NormalBlockParams& params, int blockWidth, int blockHeight)
1858 const int numWeights = computeNumWeights(params);
1859 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, numWeights);
1860 const int numColorEndpointValues = computeNumColorEndpointValues(¶ms.colorEndpointModes[0], params.numPartitions, params.isMultiPartSingleCemMode);
1861 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(params);
1863 return numWeights <= 64 &&
1864 de::inRange(numWeightBits, 24, 96) &&
1865 params.weightGridWidth <= blockWidth &&
1866 params.weightGridHeight <= blockHeight &&
1867 !(params.numPartitions == 4 && params.isDualPlane) &&
1868 numColorEndpointValues <= 18 &&
1869 numBitsForColorEndpoints >= deDivRoundUp32(13*numColorEndpointValues, 5);
1872 // Write bits 0 to 10 of an ASTC block.
1873 static void writeBlockMode (AssignBlock128& dst, const NormalBlockParams& blockParams)
1875 const deUint32 d = blockParams.isDualPlane != 0;
1876 // r and h initialized in switch below.
1879 // a, b and blockModeLayoutNdx initialized in block mode layout index detecting loop below.
1880 deUint32 a = (deUint32)-1;
1881 deUint32 b = (deUint32)-1;
1882 int blockModeLayoutNdx;
1884 // Find the values of r and h (ISE range).
1885 switch (computeISERangeMax(blockParams.weightISEParams))
1887 case 1: r = 2; h = 0; break;
1888 case 2: r = 3; h = 0; break;
1889 case 3: r = 4; h = 0; break;
1890 case 4: r = 5; h = 0; break;
1891 case 5: r = 6; h = 0; break;
1892 case 7: r = 7; h = 0; break;
1894 case 9: r = 2; h = 1; break;
1895 case 11: r = 3; h = 1; break;
1896 case 15: r = 4; h = 1; break;
1897 case 19: r = 5; h = 1; break;
1898 case 23: r = 6; h = 1; break;
1899 case 31: r = 7; h = 1; break;
1907 // Find block mode layout index, i.e. appropriate row in the "2d block mode layout" table in ASTC spec.
1910 enum BlockModeLayoutABVariable { Z=0, A=1, B=2 };
1912 static const struct BlockModeLayout
1916 BlockModeLayoutABVariable gridWidthVariableTerm;
1917 int gridWidthConstantTerm;
1918 BlockModeLayoutABVariable gridHeightVariableTerm;
1919 int gridHeightConstantTerm;
1920 } blockModeLayouts[] =
1922 { 2, 2, B, 4, A, 2},
1923 { 2, 2, B, 8, A, 2},
1924 { 2, 2, A, 2, B, 8},
1925 { 2, 1, A, 2, B, 6},
1926 { 2, 1, B, 2, A, 2},
1927 { 2, 0, Z, 12, A, 2},
1928 { 2, 0, A, 2, Z, 12},
1929 { 0, 0, Z, 6, Z, 10},
1930 { 0, 0, Z, 10, Z, 6},
1934 for (blockModeLayoutNdx = 0; blockModeLayoutNdx < DE_LENGTH_OF_ARRAY(blockModeLayouts); blockModeLayoutNdx++)
1936 const BlockModeLayout& layout = blockModeLayouts[blockModeLayoutNdx];
1937 const int aMax = (1 << layout.aNumBits) - 1;
1938 const int bMax = (1 << layout.bNumBits) - 1;
1939 const int variableOffsetsMax[3] = { 0, aMax, bMax };
1940 const int widthMin = layout.gridWidthConstantTerm;
1941 const int heightMin = layout.gridHeightConstantTerm;
1942 const int widthMax = widthMin + variableOffsetsMax[layout.gridWidthVariableTerm];
1943 const int heightMax = heightMin + variableOffsetsMax[layout.gridHeightVariableTerm];
1945 DE_ASSERT(layout.gridWidthVariableTerm != layout.gridHeightVariableTerm || layout.gridWidthVariableTerm == Z);
1947 if (de::inRange(blockParams.weightGridWidth, widthMin, widthMax) &&
1948 de::inRange(blockParams.weightGridHeight, heightMin, heightMax))
1951 deUint32& widthVariable = layout.gridWidthVariableTerm == A ? a : layout.gridWidthVariableTerm == B ? b : dummy;
1952 deUint32& heightVariable = layout.gridHeightVariableTerm == A ? a : layout.gridHeightVariableTerm == B ? b : dummy;
1954 widthVariable = blockParams.weightGridWidth - layout.gridWidthConstantTerm;
1955 heightVariable = blockParams.weightGridHeight - layout.gridHeightConstantTerm;
1962 // Set block mode bits.
1964 const deUint32 a0 = getBit(a, 0);
1965 const deUint32 a1 = getBit(a, 1);
1966 const deUint32 b0 = getBit(b, 0);
1967 const deUint32 b1 = getBit(b, 1);
1968 const deUint32 r0 = getBit(r, 0);
1969 const deUint32 r1 = getBit(r, 1);
1970 const deUint32 r2 = getBit(r, 2);
1972 #define SB(NDX, VAL) dst.setBit((NDX), (VAL))
1973 #define ASSIGN_BITS(B10, B9, B8, B7, B6, B5, B4, B3, B2, B1, B0) do { SB(10,(B10)); SB(9,(B9)); SB(8,(B8)); SB(7,(B7)); SB(6,(B6)); SB(5,(B5)); SB(4,(B4)); SB(3,(B3)); SB(2,(B2)); SB(1,(B1)); SB(0,(B0)); } while (false)
1975 switch (blockModeLayoutNdx)
1977 case 0: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 0, r2, r1); break;
1978 case 1: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 1, r2, r1); break;
1979 case 2: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 1, 0, r2, r1); break;
1980 case 3: ASSIGN_BITS(d, h, 0, b, a1, a0, r0, 1, 1, r2, r1); break;
1981 case 4: ASSIGN_BITS(d, h, 1, b, a1, a0, r0, 1, 1, r2, r1); break;
1982 case 5: ASSIGN_BITS(d, h, 0, 0, a1, a0, r0, r2, r1, 0, 0); break;
1983 case 6: ASSIGN_BITS(d, h, 0, 1, a1, a0, r0, r2, r1, 0, 0); break;
1984 case 7: ASSIGN_BITS(d, h, 1, 1, 0, 0, r0, r2, r1, 0, 0); break;
1985 case 8: ASSIGN_BITS(d, h, 1, 1, 0, 1, r0, r2, r1, 0, 0); break;
1986 case 9: ASSIGN_BITS(b1, b0, 1, 0, a1, a0, r0, r2, r1, 0, 0); DE_ASSERT(d == 0 && h == 0); break;
1995 // Write color endpoint mode data of an ASTC block.
1996 static void writeColorEndpointModes (AssignBlock128& dst, const deUint32* colorEndpointModes, bool isMultiPartSingleCemMode, int numPartitions, int extraCemBitsStart)
1998 if (numPartitions == 1)
1999 dst.setBits(13, 16, colorEndpointModes[0]);
2002 if (isMultiPartSingleCemMode)
2004 dst.setBits(23, 24, 0);
2005 dst.setBits(25, 28, colorEndpointModes[0]);
2009 DE_ASSERT(numPartitions > 0);
2010 const deUint32 minCem = *std::min_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2011 const deUint32 maxCem = *std::max_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2012 const deUint32 minCemClass = minCem/4;
2013 const deUint32 maxCemClass = maxCem/4;
2014 DE_ASSERT(maxCemClass - minCemClass <= 1);
2015 DE_UNREF(minCemClass); // \note For non-debug builds.
2016 const deUint32 highLevelSelector = de::max(1u, maxCemClass);
2018 dst.setBits(23, 24, highLevelSelector);
2020 for (int partNdx = 0; partNdx < numPartitions; partNdx++)
2022 const deUint32 c = colorEndpointModes[partNdx] / 4 == highLevelSelector ? 1 : 0;
2023 const deUint32 m = colorEndpointModes[partNdx] % 4;
2024 const deUint32 lowMBit0Ndx = numPartitions + 2*partNdx;
2025 const deUint32 lowMBit1Ndx = numPartitions + 2*partNdx + 1;
2026 dst.setBit(25 + partNdx, c);
2027 dst.setBit(lowMBit0Ndx < 4 ? 25+lowMBit0Ndx : extraCemBitsStart+lowMBit0Ndx-4, getBit(m, 0));
2028 dst.setBit(lowMBit1Ndx < 4 ? 25+lowMBit1Ndx : extraCemBitsStart+lowMBit1Ndx-4, getBit(m, 1));
2034 static void encodeISETritBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2036 // tritBlockTValue[t0][t1][t2][t3][t4] is a value of T (not necessarily the only one) that will yield the given trits when decoded.
2037 static const deUint32 tritBlockTValue[3][3][3][3][3] =
2040 {{{0, 128, 96}, {32, 160, 224}, {64, 192, 28}}, {{16, 144, 112}, {48, 176, 240}, {80, 208, 156}}, {{3, 131, 99}, {35, 163, 227}, {67, 195, 31}}},
2041 {{{4, 132, 100}, {36, 164, 228}, {68, 196, 60}}, {{20, 148, 116}, {52, 180, 244}, {84, 212, 188}}, {{19, 147, 115}, {51, 179, 243}, {83, 211, 159}}},
2042 {{{8, 136, 104}, {40, 168, 232}, {72, 200, 92}}, {{24, 152, 120}, {56, 184, 248}, {88, 216, 220}}, {{12, 140, 108}, {44, 172, 236}, {76, 204, 124}}}
2045 {{{1, 129, 97}, {33, 161, 225}, {65, 193, 29}}, {{17, 145, 113}, {49, 177, 241}, {81, 209, 157}}, {{7, 135, 103}, {39, 167, 231}, {71, 199, 63}}},
2046 {{{5, 133, 101}, {37, 165, 229}, {69, 197, 61}}, {{21, 149, 117}, {53, 181, 245}, {85, 213, 189}}, {{23, 151, 119}, {55, 183, 247}, {87, 215, 191}}},
2047 {{{9, 137, 105}, {41, 169, 233}, {73, 201, 93}}, {{25, 153, 121}, {57, 185, 249}, {89, 217, 221}}, {{13, 141, 109}, {45, 173, 237}, {77, 205, 125}}}
2050 {{{2, 130, 98}, {34, 162, 226}, {66, 194, 30}}, {{18, 146, 114}, {50, 178, 242}, {82, 210, 158}}, {{11, 139, 107}, {43, 171, 235}, {75, 203, 95}}},
2051 {{{6, 134, 102}, {38, 166, 230}, {70, 198, 62}}, {{22, 150, 118}, {54, 182, 246}, {86, 214, 190}}, {{27, 155, 123}, {59, 187, 251}, {91, 219, 223}}},
2052 {{{10, 138, 106}, {42, 170, 234}, {74, 202, 94}}, {{26, 154, 122}, {58, 186, 250}, {90, 218, 222}}, {{14, 142, 110}, {46, 174, 238}, {78, 206, 126}}}
2056 DE_ASSERT(de::inRange(numValues, 1, 5));
2058 deUint32 tritParts[5];
2059 deUint32 bitParts[5];
2061 for (int i = 0; i < 5; i++)
2065 if (fromExplicitInputBlock)
2067 bitParts[i] = blockInput.bitValues[i];
2068 tritParts[i] = -1; // \note Won't be used, but silences warning.
2072 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2073 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2074 tritParts[i] = nonBlockInput[i] >> numBits;
2084 const deUint32 T = fromExplicitInputBlock ? blockInput.tOrQValue : tritBlockTValue[tritParts[0]]
2090 dst.setNext(numBits, bitParts[0]);
2091 dst.setNext(2, getBits(T, 0, 1));
2092 dst.setNext(numBits, bitParts[1]);
2093 dst.setNext(2, getBits(T, 2, 3));
2094 dst.setNext(numBits, bitParts[2]);
2095 dst.setNext(1, getBit(T, 4));
2096 dst.setNext(numBits, bitParts[3]);
2097 dst.setNext(2, getBits(T, 5, 6));
2098 dst.setNext(numBits, bitParts[4]);
2099 dst.setNext(1, getBit(T, 7));
2102 static void encodeISEQuintBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2104 // quintBlockQValue[q0][q1][q2] is a value of Q (not necessarily the only one) that will yield the given quints when decoded.
2105 static const deUint32 quintBlockQValue[5][5][5] =
2107 {{0, 32, 64, 96, 102}, {8, 40, 72, 104, 110}, {16, 48, 80, 112, 118}, {24, 56, 88, 120, 126}, {5, 37, 69, 101, 39}},
2108 {{1, 33, 65, 97, 103}, {9, 41, 73, 105, 111}, {17, 49, 81, 113, 119}, {25, 57, 89, 121, 127}, {13, 45, 77, 109, 47}},
2109 {{2, 34, 66, 98, 70}, {10, 42, 74, 106, 78}, {18, 50, 82, 114, 86}, {26, 58, 90, 122, 94}, {21, 53, 85, 117, 55}},
2110 {{3, 35, 67, 99, 71}, {11, 43, 75, 107, 79}, {19, 51, 83, 115, 87}, {27, 59, 91, 123, 95}, {29, 61, 93, 125, 63}},
2111 {{4, 36, 68, 100, 38}, {12, 44, 76, 108, 46}, {20, 52, 84, 116, 54}, {28, 60, 92, 124, 62}, {6, 14, 22, 30, 7}}
2114 DE_ASSERT(de::inRange(numValues, 1, 3));
2116 deUint32 quintParts[3];
2117 deUint32 bitParts[3];
2119 for (int i = 0; i < 3; i++)
2123 if (fromExplicitInputBlock)
2125 bitParts[i] = blockInput.bitValues[i];
2126 quintParts[i] = -1; // \note Won't be used, but silences warning.
2130 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2131 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2132 quintParts[i] = nonBlockInput[i] >> numBits;
2142 const deUint32 Q = fromExplicitInputBlock ? blockInput.tOrQValue : quintBlockQValue[quintParts[0]]
2146 dst.setNext(numBits, bitParts[0]);
2147 dst.setNext(3, getBits(Q, 0, 2));
2148 dst.setNext(numBits, bitParts[1]);
2149 dst.setNext(2, getBits(Q, 3, 4));
2150 dst.setNext(numBits, bitParts[2]);
2151 dst.setNext(2, getBits(Q, 5, 6));
2154 static void encodeISEBitBlock (BitAssignAccessStream& dst, int numBits, deUint32 value)
2156 DE_ASSERT(de::inRange(value, 0u, (1u<<numBits)-1));
2157 dst.setNext(numBits, value);
2160 static void encodeISE (BitAssignAccessStream& dst, const ISEParams& params, const ISEInput& input, int numValues)
2162 if (params.mode == ISEMODE_TRIT)
2164 const int numBlocks = deDivRoundUp32(numValues, 5);
2165 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2167 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
2168 encodeISETritBlock(dst, params.numBits, input.isGivenInBlockForm,
2169 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2170 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[5*blockNdx],
2174 else if (params.mode == ISEMODE_QUINT)
2176 const int numBlocks = deDivRoundUp32(numValues, 3);
2177 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2179 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
2180 encodeISEQuintBlock(dst, params.numBits, input.isGivenInBlockForm,
2181 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2182 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[3*blockNdx],
2188 DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
2189 for (int i = 0; i < numValues; i++)
2190 encodeISEBitBlock(dst, params.numBits, input.isGivenInBlockForm ? input.value.block[i].bitValues[0] : input.value.plain[i]);
2194 static void writeWeightData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numWeights)
2196 const int numWeightBits = computeNumRequiredBits(iseParams, numWeights);
2197 BitAssignAccessStream access (dst, 127, numWeightBits, false);
2198 encodeISE(access, iseParams, input, numWeights);
2201 static void writeColorEndpointData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numEndpoints, int numBitsForColorEndpoints, int colorEndpointDataStartNdx)
2203 BitAssignAccessStream access(dst, colorEndpointDataStartNdx, numBitsForColorEndpoints, true);
2204 encodeISE(access, iseParams, input, numEndpoints);
2207 static AssignBlock128 generateNormalBlock (const NormalBlockParams& blockParams, int blockWidth, int blockHeight, const NormalBlockISEInputs& iseInputs)
2209 DE_ASSERT(isValidBlockParams(blockParams, blockWidth, blockHeight));
2210 DE_UNREF(blockWidth); // \note For non-debug builds.
2211 DE_UNREF(blockHeight); // \note For non-debug builds.
2213 AssignBlock128 block;
2214 const int numWeights = computeNumWeights(blockParams);
2215 const int numWeightBits = computeNumRequiredBits(blockParams.weightISEParams, numWeights);
2217 writeBlockMode(block, blockParams);
2219 block.setBits(11, 12, blockParams.numPartitions - 1);
2220 if (blockParams.numPartitions > 1)
2221 block.setBits(13, 22, blockParams.partitionSeed);
2224 const int extraCemBitsStart = 127 - numWeightBits - (blockParams.numPartitions == 1 || blockParams.isMultiPartSingleCemMode ? -1
2225 : blockParams.numPartitions == 4 ? 7
2226 : blockParams.numPartitions == 3 ? 4
2227 : blockParams.numPartitions == 2 ? 1
2230 writeColorEndpointModes(block, &blockParams.colorEndpointModes[0], blockParams.isMultiPartSingleCemMode, blockParams.numPartitions, extraCemBitsStart);
2232 if (blockParams.isDualPlane)
2233 block.setBits(extraCemBitsStart-2, extraCemBitsStart-1, blockParams.ccs);
2236 writeWeightData(block, blockParams.weightISEParams, iseInputs.weight, numWeights);
2239 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2240 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2241 const int colorEndpointDataStartNdx = blockParams.numPartitions == 1 ? 17 : 29;
2242 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2244 writeColorEndpointData(block, colorEndpointISEParams, iseInputs.endpoint, numColorEndpointValues, numBitsForColorEndpoints, colorEndpointDataStartNdx);
2250 // Generate default ISE inputs for weight and endpoint data - gradient-ish values.
2251 static NormalBlockISEInputs generateDefaultISEInputs (const NormalBlockParams& blockParams)
2253 NormalBlockISEInputs result;
2256 result.weight.isGivenInBlockForm = false;
2258 const int numWeights = computeNumWeights(blockParams);
2259 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2261 if (blockParams.isDualPlane)
2263 for (int i = 0; i < numWeights; i += 2)
2264 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2266 for (int i = 1; i < numWeights; i += 2)
2267 result.weight.value.plain[i] = weightRangeMax - (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2271 for (int i = 0; i < numWeights; i++)
2272 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2277 result.endpoint.isGivenInBlockForm = false;
2279 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2280 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2281 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2282 const int colorEndpointRangeMax = computeISERangeMax(colorEndpointISEParams);
2284 for (int i = 0; i < numColorEndpointValues; i++)
2285 result.endpoint.value.plain[i] = (i*colorEndpointRangeMax + (numColorEndpointValues-1)/2) / (numColorEndpointValues-1);
2291 static const ISEParams s_weightISEParamsCandidates[] =
2293 ISEParams(ISEMODE_PLAIN_BIT, 1),
2294 ISEParams(ISEMODE_TRIT, 0),
2295 ISEParams(ISEMODE_PLAIN_BIT, 2),
2296 ISEParams(ISEMODE_QUINT, 0),
2297 ISEParams(ISEMODE_TRIT, 1),
2298 ISEParams(ISEMODE_PLAIN_BIT, 3),
2299 ISEParams(ISEMODE_QUINT, 1),
2300 ISEParams(ISEMODE_TRIT, 2),
2301 ISEParams(ISEMODE_PLAIN_BIT, 4),
2302 ISEParams(ISEMODE_QUINT, 2),
2303 ISEParams(ISEMODE_TRIT, 3),
2304 ISEParams(ISEMODE_PLAIN_BIT, 5)
2307 void generateRandomBlock (deUint8* dst, const IVec3& blockSize, de::Random& rnd)
2309 DE_ASSERT(blockSize.z() == 1);
2311 if (rnd.getFloat() < 0.1f)
2313 // Void extent block.
2314 const bool isVoidExtentHDR = rnd.getBool();
2315 const deUint16 r = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2316 const deUint16 g = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2317 const deUint16 b = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2318 const deUint16 a = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2319 generateVoidExtentBlock(VoidExtentParams(isVoidExtentHDR, r, g, b, a)).assignToMemory(dst);
2323 // Not void extent block.
2325 // Generate block params.
2327 NormalBlockParams blockParams;
2331 blockParams.weightGridWidth = rnd.getInt(2, blockSize.x());
2332 blockParams.weightGridHeight = rnd.getInt(2, blockSize.y());
2333 blockParams.weightISEParams = s_weightISEParamsCandidates[rnd.getInt(0, DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates)-1)];
2334 blockParams.numPartitions = rnd.getInt(1, 4);
2335 blockParams.isMultiPartSingleCemMode = rnd.getFloat() < 0.25f;
2336 blockParams.isDualPlane = blockParams.numPartitions != 4 && rnd.getBool();
2337 blockParams.ccs = rnd.getInt(0, 3);
2338 blockParams.partitionSeed = rnd.getInt(0, 1023);
2340 blockParams.colorEndpointModes[0] = rnd.getInt(0, 15);
2343 const int cemDiff = blockParams.isMultiPartSingleCemMode ? 0
2344 : blockParams.colorEndpointModes[0] == 0 ? 1
2345 : blockParams.colorEndpointModes[0] == 15 ? -1
2346 : rnd.getBool() ? 1 : -1;
2348 for (int i = 1; i < blockParams.numPartitions; i++)
2349 blockParams.colorEndpointModes[i] = blockParams.colorEndpointModes[0] + (cemDiff == -1 ? rnd.getInt(-1, 0) : cemDiff == 1 ? rnd.getInt(0, 1) : 0);
2351 } while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()));
2353 // Generate ISE inputs for both weight and endpoint data.
2355 NormalBlockISEInputs iseInputs;
2357 for (int weightOrEndpoints = 0; weightOrEndpoints <= 1; weightOrEndpoints++)
2359 const bool setWeights = weightOrEndpoints == 0;
2360 const int numValues = setWeights ? computeNumWeights(blockParams) :
2361 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2362 const ISEParams iseParams = setWeights ? blockParams.weightISEParams : computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams), numValues);
2363 ISEInput& iseInput = setWeights ? iseInputs.weight : iseInputs.endpoint;
2365 iseInput.isGivenInBlockForm = rnd.getBool();
2367 if (iseInput.isGivenInBlockForm)
2369 const int numValuesPerISEBlock = iseParams.mode == ISEMODE_TRIT ? 5
2370 : iseParams.mode == ISEMODE_QUINT ? 3
2372 const int iseBitMax = (1 << iseParams.numBits) - 1;
2373 const int numISEBlocks = deDivRoundUp32(numValues, numValuesPerISEBlock);
2375 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocks; iseBlockNdx++)
2377 iseInput.value.block[iseBlockNdx].tOrQValue = rnd.getInt(0, 255);
2378 for (int i = 0; i < numValuesPerISEBlock; i++)
2379 iseInput.value.block[iseBlockNdx].bitValues[i] = rnd.getInt(0, iseBitMax);
2384 const int rangeMax = computeISERangeMax(iseParams);
2386 for (int valueNdx = 0; valueNdx < numValues; valueNdx++)
2387 iseInput.value.plain[valueNdx] = rnd.getInt(0, rangeMax);
2391 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).assignToMemory(dst);
2397 // Generate block data for a given BlockTestType and format.
2398 void generateBlockCaseTestData (vector<deUint8>& dst, CompressedTexFormat format, BlockTestType testType)
2400 DE_ASSERT(isAstcFormat(format));
2401 DE_ASSERT(!(isAstcSRGBFormat(format) && isBlockTestTypeHDROnly(testType)));
2403 const IVec3 blockSize = getBlockPixelSize(format);
2404 DE_ASSERT(blockSize.z() == 1);
2408 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:
2409 // Generate a gradient-like set of LDR void-extent blocks.
2411 const int numBlocks = 1<<13;
2412 const deUint32 numValues = 1<<16;
2413 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2415 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2417 const deUint32 baseValue = blockNdx*(numValues-1) / (numBlocks-1);
2418 const deUint16 r = (deUint16)((baseValue + numValues*0/4) % numValues);
2419 const deUint16 g = (deUint16)((baseValue + numValues*1/4) % numValues);
2420 const deUint16 b = (deUint16)((baseValue + numValues*2/4) % numValues);
2421 const deUint16 a = (deUint16)((baseValue + numValues*3/4) % numValues);
2422 AssignBlock128 block;
2424 generateVoidExtentBlock(VoidExtentParams(false, r, g, b, a)).pushBytesToVector(dst);
2430 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:
2431 // Generate a gradient-like set of HDR void-extent blocks, with values ranging from the largest finite negative to largest finite positive of fp16.
2433 const float minValue = -65504.0f;
2434 const float maxValue = +65504.0f;
2435 const int numBlocks = 1<<13;
2436 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2438 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2440 const int rNdx = (blockNdx + numBlocks*0/4) % numBlocks;
2441 const int gNdx = (blockNdx + numBlocks*1/4) % numBlocks;
2442 const int bNdx = (blockNdx + numBlocks*2/4) % numBlocks;
2443 const int aNdx = (blockNdx + numBlocks*3/4) % numBlocks;
2444 const deFloat16 r = deFloat32To16(minValue + (float)rNdx * (maxValue - minValue) / (float)(numBlocks-1));
2445 const deFloat16 g = deFloat32To16(minValue + (float)gNdx * (maxValue - minValue) / (float)(numBlocks-1));
2446 const deFloat16 b = deFloat32To16(minValue + (float)bNdx * (maxValue - minValue) / (float)(numBlocks-1));
2447 const deFloat16 a = deFloat32To16(minValue + (float)aNdx * (maxValue - minValue) / (float)(numBlocks-1));
2449 generateVoidExtentBlock(VoidExtentParams(true, r, g, b, a)).pushBytesToVector(dst);
2455 case BLOCK_TEST_TYPE_WEIGHT_GRID:
2456 // Generate different combinations of plane count, weight ISE params, and grid size.
2458 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2459 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2460 for (int weightGridWidth = 2; weightGridWidth <= 12; weightGridWidth++)
2461 for (int weightGridHeight = 2; weightGridHeight <= 12; weightGridHeight++)
2463 NormalBlockParams blockParams;
2464 NormalBlockISEInputs iseInputs;
2466 blockParams.weightGridWidth = weightGridWidth;
2467 blockParams.weightGridHeight = weightGridHeight;
2468 blockParams.isDualPlane = isDualPlane != 0;
2469 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2470 blockParams.ccs = 0;
2471 blockParams.numPartitions = 1;
2472 blockParams.colorEndpointModes[0] = 0;
2474 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2475 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2481 case BLOCK_TEST_TYPE_WEIGHT_ISE:
2482 // For each weight ISE param set, generate blocks that cover:
2483 // - each single value of the ISE's range, at each position inside an ISE block
2484 // - for trit and quint ISEs, each single T or Q value of an ISE block
2486 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2488 const ISEParams& iseParams = s_weightISEParamsCandidates[iseParamsNdx];
2489 NormalBlockParams blockParams;
2491 blockParams.weightGridWidth = 4;
2492 blockParams.weightGridHeight = 4;
2493 blockParams.weightISEParams = iseParams;
2494 blockParams.numPartitions = 1;
2495 blockParams.isDualPlane = blockParams.weightGridWidth * blockParams.weightGridHeight < 24 ? true : false;
2496 blockParams.ccs = 0;
2497 blockParams.colorEndpointModes[0] = 0;
2499 while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2501 blockParams.weightGridWidth--;
2502 blockParams.weightGridHeight--;
2505 const int numValuesInISEBlock = iseParams.mode == ISEMODE_TRIT ? 5 : iseParams.mode == ISEMODE_QUINT ? 3 : 1;
2506 const int numWeights = computeNumWeights(blockParams);
2509 const int numWeightValues = (int)computeISERangeMax(iseParams) + 1;
2510 const int numBlocks = deDivRoundUp32(numWeightValues, numWeights);
2511 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2512 iseInputs.weight.isGivenInBlockForm = false;
2514 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2515 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2517 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2518 iseInputs.weight.value.plain[weightNdx] = (blockNdx*numWeights + weightNdx + offset) % numWeightValues;
2520 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2524 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
2526 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2527 iseInputs.weight.isGivenInBlockForm = true;
2529 const int numTQValues = 1 << (iseParams.mode == ISEMODE_TRIT ? 8 : 7);
2530 const int numISEBlocksPerBlock = deDivRoundUp32(numWeights, numValuesInISEBlock);
2531 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2533 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2534 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2536 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2538 for (int i = 0; i < numValuesInISEBlock; i++)
2539 iseInputs.weight.value.block[iseBlockNdx].bitValues[i] = 0;
2540 iseInputs.weight.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2543 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2551 case BLOCK_TEST_TYPE_CEMS:
2552 // For each plane count & partition count combination, generate all color endpoint mode combinations.
2554 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2555 for (int numPartitions = 1; numPartitions <= (isDualPlane != 0 ? 3 : 4); numPartitions++)
2557 // Multi-partition, single-CEM mode.
2558 if (numPartitions > 1)
2560 for (deUint32 singleCem = 0; singleCem < 16; singleCem++)
2562 NormalBlockParams blockParams;
2563 blockParams.weightGridWidth = 4;
2564 blockParams.weightGridHeight = 4;
2565 blockParams.isDualPlane = isDualPlane != 0;
2566 blockParams.ccs = 0;
2567 blockParams.numPartitions = numPartitions;
2568 blockParams.isMultiPartSingleCemMode = true;
2569 blockParams.colorEndpointModes[0] = singleCem;
2570 blockParams.partitionSeed = 634;
2572 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2574 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2575 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2577 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2584 // Separate-CEM mode.
2585 for (deUint32 cem0 = 0; cem0 < 16; cem0++)
2586 for (deUint32 cem1 = 0; cem1 < (numPartitions >= 2 ? 16u : 1u); cem1++)
2587 for (deUint32 cem2 = 0; cem2 < (numPartitions >= 3 ? 16u : 1u); cem2++)
2588 for (deUint32 cem3 = 0; cem3 < (numPartitions >= 4 ? 16u : 1u); cem3++)
2590 NormalBlockParams blockParams;
2591 blockParams.weightGridWidth = 4;
2592 blockParams.weightGridHeight = 4;
2593 blockParams.isDualPlane = isDualPlane != 0;
2594 blockParams.ccs = 0;
2595 blockParams.numPartitions = numPartitions;
2596 blockParams.isMultiPartSingleCemMode = false;
2597 blockParams.colorEndpointModes[0] = cem0;
2598 blockParams.colorEndpointModes[1] = cem1;
2599 blockParams.colorEndpointModes[2] = cem2;
2600 blockParams.colorEndpointModes[3] = cem3;
2601 blockParams.partitionSeed = 634;
2604 const deUint32 minCem = *std::min_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2605 const deUint32 maxCem = *std::max_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2606 const deUint32 minCemClass = minCem/4;
2607 const deUint32 maxCemClass = maxCem/4;
2609 if (maxCemClass - minCemClass > 1)
2613 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2615 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2616 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2618 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2628 case BLOCK_TEST_TYPE_PARTITION_SEED:
2629 // Test all partition seeds ("partition pattern indices").
2631 for (int numPartitions = 2; numPartitions <= 4; numPartitions++)
2632 for (deUint32 partitionSeed = 0; partitionSeed < 1<<10; partitionSeed++)
2634 NormalBlockParams blockParams;
2635 blockParams.weightGridWidth = 4;
2636 blockParams.weightGridHeight = 4;
2637 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2638 blockParams.isDualPlane = false;
2639 blockParams.numPartitions = numPartitions;
2640 blockParams.isMultiPartSingleCemMode = true;
2641 blockParams.colorEndpointModes[0] = 0;
2642 blockParams.partitionSeed = partitionSeed;
2644 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2650 // \note Fall-through.
2651 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:
2652 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:
2653 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:
2654 // For each endpoint mode, for each pair of components in the endpoint value, test 10x10 combinations of values for that pair.
2655 // \note Separate modes for HDR and mode 15 due to different color scales and biases.
2657 for (deUint32 cem = 0; cem < 16; cem++)
2659 const bool isHDRCem = cem == 2 ||
2666 if ((testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR && isHDRCem) ||
2667 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 && (!isHDRCem || cem == 15)) ||
2668 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15 && cem != 15))
2671 NormalBlockParams blockParams;
2672 blockParams.weightGridWidth = 3;
2673 blockParams.weightGridHeight = 4;
2674 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2675 blockParams.isDualPlane = false;
2676 blockParams.numPartitions = 1;
2677 blockParams.colorEndpointModes[0] = cem;
2680 const int numBitsForEndpoints = computeNumBitsForColorEndpoints(blockParams);
2681 const int numEndpointParts = computeNumColorEndpointValues(cem);
2682 const ISEParams endpointISE = computeMaximumRangeISEParams(numBitsForEndpoints, numEndpointParts);
2683 const int endpointISERangeMax = computeISERangeMax(endpointISE);
2685 for (int endpointPartNdx0 = 0; endpointPartNdx0 < numEndpointParts; endpointPartNdx0++)
2686 for (int endpointPartNdx1 = endpointPartNdx0+1; endpointPartNdx1 < numEndpointParts; endpointPartNdx1++)
2688 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2689 const int numEndpointValues = de::min(10, endpointISERangeMax+1);
2691 for (int endpointValueNdx0 = 0; endpointValueNdx0 < numEndpointValues; endpointValueNdx0++)
2692 for (int endpointValueNdx1 = 0; endpointValueNdx1 < numEndpointValues; endpointValueNdx1++)
2694 const int endpointValue0 = endpointValueNdx0 * endpointISERangeMax / (numEndpointValues-1);
2695 const int endpointValue1 = endpointValueNdx1 * endpointISERangeMax / (numEndpointValues-1);
2697 iseInputs.endpoint.value.plain[endpointPartNdx0] = endpointValue0;
2698 iseInputs.endpoint.value.plain[endpointPartNdx1] = endpointValue1;
2700 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2709 case BLOCK_TEST_TYPE_ENDPOINT_ISE:
2710 // Similar to BLOCK_TEST_TYPE_WEIGHT_ISE, see above.
2712 static const deUint32 endpointRangeMaximums[] = { 5, 9, 11, 19, 23, 39, 47, 79, 95, 159, 191 };
2714 for (int endpointRangeNdx = 0; endpointRangeNdx < DE_LENGTH_OF_ARRAY(endpointRangeMaximums); endpointRangeNdx++)
2716 bool validCaseGenerated = false;
2718 for (int numPartitions = 1; !validCaseGenerated && numPartitions <= 4; numPartitions++)
2719 for (int isDual = 0; !validCaseGenerated && isDual <= 1; isDual++)
2720 for (int weightISEParamsNdx = 0; !validCaseGenerated && weightISEParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); weightISEParamsNdx++)
2721 for (int weightGridWidth = 2; !validCaseGenerated && weightGridWidth <= 12; weightGridWidth++)
2722 for (int weightGridHeight = 2; !validCaseGenerated && weightGridHeight <= 12; weightGridHeight++)
2724 NormalBlockParams blockParams;
2725 blockParams.weightGridWidth = weightGridWidth;
2726 blockParams.weightGridHeight = weightGridHeight;
2727 blockParams.weightISEParams = s_weightISEParamsCandidates[weightISEParamsNdx];
2728 blockParams.isDualPlane = isDual != 0;
2729 blockParams.ccs = 0;
2730 blockParams.numPartitions = numPartitions;
2731 blockParams.isMultiPartSingleCemMode = true;
2732 blockParams.colorEndpointModes[0] = 12;
2733 blockParams.partitionSeed = 634;
2735 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2737 const ISEParams endpointISEParams = computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams),
2738 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, true));
2740 if (computeISERangeMax(endpointISEParams) == endpointRangeMaximums[endpointRangeNdx])
2742 validCaseGenerated = true;
2744 const int numColorEndpoints = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, blockParams.isMultiPartSingleCemMode);
2745 const int numValuesInISEBlock = endpointISEParams.mode == ISEMODE_TRIT ? 5 : endpointISEParams.mode == ISEMODE_QUINT ? 3 : 1;
2748 const int numColorEndpointValues = (int)computeISERangeMax(endpointISEParams) + 1;
2749 const int numBlocks = deDivRoundUp32(numColorEndpointValues, numColorEndpoints);
2750 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2751 iseInputs.endpoint.isGivenInBlockForm = false;
2753 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2754 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2756 for (int endpointNdx = 0; endpointNdx < numColorEndpoints; endpointNdx++)
2757 iseInputs.endpoint.value.plain[endpointNdx] = (blockNdx*numColorEndpoints + endpointNdx + offset) % numColorEndpointValues;
2759 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2763 if (endpointISEParams.mode == ISEMODE_TRIT || endpointISEParams.mode == ISEMODE_QUINT)
2765 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2766 iseInputs.endpoint.isGivenInBlockForm = true;
2768 const int numTQValues = 1 << (endpointISEParams.mode == ISEMODE_TRIT ? 8 : 7);
2769 const int numISEBlocksPerBlock = deDivRoundUp32(numColorEndpoints, numValuesInISEBlock);
2770 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2772 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2773 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2775 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2777 for (int i = 0; i < numValuesInISEBlock; i++)
2778 iseInputs.endpoint.value.block[iseBlockNdx].bitValues[i] = 0;
2779 iseInputs.endpoint.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2782 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2789 DE_ASSERT(validCaseGenerated);
2795 case BLOCK_TEST_TYPE_CCS:
2796 // For all partition counts, test all values of the CCS (color component selector).
2798 for (int numPartitions = 1; numPartitions <= 3; numPartitions++)
2799 for (deUint32 ccs = 0; ccs < 4; ccs++)
2801 NormalBlockParams blockParams;
2802 blockParams.weightGridWidth = 3;
2803 blockParams.weightGridHeight = 3;
2804 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2805 blockParams.isDualPlane = true;
2806 blockParams.ccs = ccs;
2807 blockParams.numPartitions = numPartitions;
2808 blockParams.isMultiPartSingleCemMode = true;
2809 blockParams.colorEndpointModes[0] = 8;
2810 blockParams.partitionSeed = 634;
2812 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2818 case BLOCK_TEST_TYPE_RANDOM:
2819 // Generate a number of random (including invalid) blocks.
2821 const int numBlocks = 16384;
2822 const deUint32 seed = 1;
2824 dst.resize(numBlocks*BLOCK_SIZE_BYTES);
2826 generateRandomBlocks(&dst[0], numBlocks, format, seed);
2836 void generateRandomBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, deUint32 seed)
2838 const IVec3 blockSize = getBlockPixelSize(format);
2839 de::Random rnd (seed);
2840 size_t numBlocksGenerated = 0;
2842 DE_ASSERT(isAstcFormat(format));
2843 DE_ASSERT(blockSize.z() == 1);
2845 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2847 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2849 generateRandomBlock(curBlockPtr, blockSize, rnd);
2853 void generateRandomValidBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, TexDecompressionParams::AstcMode mode, deUint32 seed)
2855 const IVec3 blockSize = getBlockPixelSize(format);
2856 de::Random rnd (seed);
2857 size_t numBlocksGenerated = 0;
2859 DE_ASSERT(isAstcFormat(format));
2860 DE_ASSERT(blockSize.z() == 1);
2862 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2864 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2868 generateRandomBlock(curBlockPtr, blockSize, rnd);
2869 } while (!isValidBlock(curBlockPtr, format, mode));
2873 // Generate a number of trivial dummy blocks to fill unneeded space in a texture.
2874 void generateDummyVoidExtentBlocks (deUint8* dst, size_t numBlocks)
2876 AssignBlock128 block = generateVoidExtentBlock(VoidExtentParams(false, 0, 0, 0, 0));
2877 for (size_t ndx = 0; ndx < numBlocks; ndx++)
2878 block.assignToMemory(&dst[ndx * BLOCK_SIZE_BYTES]);
2881 void generateDummyNormalBlocks (deUint8* dst, size_t numBlocks, int blockWidth, int blockHeight)
2883 NormalBlockParams blockParams;
2885 blockParams.weightGridWidth = 3;
2886 blockParams.weightGridHeight = 3;
2887 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 5);
2888 blockParams.isDualPlane = false;
2889 blockParams.numPartitions = 1;
2890 blockParams.colorEndpointModes[0] = 8;
2892 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2893 iseInputs.weight.isGivenInBlockForm = false;
2895 const int numWeights = computeNumWeights(blockParams);
2896 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2898 for (size_t blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2900 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2901 iseInputs.weight.value.plain[weightNdx] = (deUint32)((blockNdx*numWeights + weightNdx) * weightRangeMax / (numBlocks*numWeights-1));
2903 generateNormalBlock(blockParams, blockWidth, blockHeight, iseInputs).assignToMemory(dst + blockNdx*BLOCK_SIZE_BYTES);
2907 bool isValidBlock (const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2909 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2910 const bool isSRGB = isAstcSRGBFormat(format);
2911 const bool isLDR = isSRGB || mode == TexDecompressionParams::ASTCMODE_LDR;
2914 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2915 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2917 const Block128 blockData (data);
2918 const DecompressResult result = decompressBlock((isSRGB ? (void*)&tmpBuffer.sRGB[0] : (void*)&tmpBuffer.linear[0]),
2919 blockData, blockPixelSize.x(), blockPixelSize.y(), isSRGB, isLDR);
2921 return result == DECOMPRESS_RESULT_VALID_BLOCK;
2924 void decompress (const PixelBufferAccess& dst, const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2926 const bool isSRGBFormat = isAstcSRGBFormat(format);
2928 #if defined(DE_DEBUG)
2929 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2931 DE_ASSERT(dst.getWidth() == blockPixelSize.x() &&
2932 dst.getHeight() == blockPixelSize.y() &&
2933 dst.getDepth() == blockPixelSize.z());
2934 DE_ASSERT(mode == TexDecompressionParams::ASTCMODE_LDR || mode == TexDecompressionParams::ASTCMODE_HDR);
2937 decompress(dst, data, isSRGBFormat, isSRGBFormat || mode == TexDecompressionParams::ASTCMODE_LDR);
2940 const char* getBlockTestTypeName (BlockTestType testType)
2944 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "void_extent_ldr";
2945 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "void_extent_hdr";
2946 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "weight_grid";
2947 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "weight_ise";
2948 case BLOCK_TEST_TYPE_CEMS: return "color_endpoint_modes";
2949 case BLOCK_TEST_TYPE_PARTITION_SEED: return "partition_pattern_index";
2950 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "endpoint_value_ldr";
2951 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return "endpoint_value_hdr_cem_not_15";
2952 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "endpoint_value_hdr_cem_15";
2953 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "endpoint_ise";
2954 case BLOCK_TEST_TYPE_CCS: return "color_component_selector";
2955 case BLOCK_TEST_TYPE_RANDOM: return "random";
2962 const char* getBlockTestTypeDescription (BlockTestType testType)
2966 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "Test void extent block, LDR mode";
2967 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "Test void extent block, HDR mode";
2968 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "Test combinations of plane count, weight integer sequence encoding parameters, and weight grid size";
2969 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "Test different integer sequence encoding block values for weight grid";
2970 case BLOCK_TEST_TYPE_CEMS: return "Test different color endpoint mode combinations, combined with different plane and partition counts";
2971 case BLOCK_TEST_TYPE_PARTITION_SEED: return "Test different partition pattern indices";
2972 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "Test various combinations of each pair of color endpoint values, for each LDR color endpoint mode";
2973 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return "Test various combinations of each pair of color endpoint values, for each HDR color endpoint mode other than mode 15";
2974 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "Test various combinations of each pair of color endpoint values, HDR color endpoint mode 15";
2975 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "Test different integer sequence encoding block values for color endpoints";
2976 case BLOCK_TEST_TYPE_CCS: return "Test color component selector, for different partition counts";
2977 case BLOCK_TEST_TYPE_RANDOM: return "Random block test";
2984 bool isBlockTestTypeHDROnly (BlockTestType testType)
2986 return testType == BLOCK_TEST_TYPE_VOID_EXTENT_HDR ||
2987 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 ||
2988 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15;
2991 Vec4 getBlockTestTypeColorScale (BlockTestType testType)
2995 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f/65504.0f);
2996 case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return Vec4(1.0f/65504.0f, 1.0f/65504.0f, 1.0f/65504.0f, 1.0f);
2997 case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return Vec4(1.0f/65504.0f);
2998 default: return Vec4(1.0f);
3002 Vec4 getBlockTestTypeColorBias (BlockTestType testType)
3006 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f);
3007 default: return Vec4(0.0f);