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) [64], 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 DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= DE_LENGTH_OF_ARRAY(unquantizedWeights));
1266 for (int texelY = 0; texelY < blockHeight; texelY++)
1268 for (int texelX = 0; texelX < blockWidth; texelX++)
1270 const deUint32 gX = (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;
1271 const deUint32 gY = (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;
1272 const deUint32 jX = gX >> 4;
1273 const deUint32 jY = gY >> 4;
1274 const deUint32 fX = gX & 0xf;
1275 const deUint32 fY = gY & 0xf;
1277 const deUint32 w11 = (fX*fY + 8) >> 4;
1278 const deUint32 w10 = fY - w11;
1279 const deUint32 w01 = fX - w11;
1280 const deUint32 w00 = 16 - fX - fY + w11;
1282 const deUint32 i00 = jY*blockMode.weightGridWidth + jX;
1283 const deUint32 i01 = i00 + 1;
1284 const deUint32 i10 = i00 + blockMode.weightGridWidth;
1285 const deUint32 i11 = i00 + blockMode.weightGridWidth + 1;
1287 // These addresses can be out of bounds, but respective weights will be 0 then.
1288 DE_ASSERT(deInBounds32(i00, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w00 == 0);
1289 DE_ASSERT(deInBounds32(i01, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w01 == 0);
1290 DE_ASSERT(deInBounds32(i10, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w10 == 0);
1291 DE_ASSERT(deInBounds32(i11, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w11 == 0);
1293 for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)
1295 // & 0x3f clamps address to bounds of unquantizedWeights
1296 const deUint32 p00 = unquantizedWeights[(i00 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
1297 const deUint32 p01 = unquantizedWeights[(i01 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
1298 const deUint32 p10 = unquantizedWeights[(i10 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
1299 const deUint32 p11 = unquantizedWeights[(i11 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
1301 dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;
1307 void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
1309 ISEDecodedResult weightGrid[64];
1312 BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);
1313 decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);
1317 deUint32 unquantizedWeights[64];
1318 unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);
1319 interpolateWeights(dst, unquantizedWeights, blockWidth, blockHeight, blockMode);
1323 inline deUint32 hash52 (deUint32 v)
1326 p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
1327 p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
1328 p ^= p << 6; p ^= p >> 17;
1332 int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)
1334 DE_ASSERT(zIn == 0);
1335 const deUint32 x = smallBlock ? xIn << 1 : xIn;
1336 const deUint32 y = smallBlock ? yIn << 1 : yIn;
1337 const deUint32 z = smallBlock ? zIn << 1 : zIn;
1338 const deUint32 seed = seedIn + 1024*(numPartitions-1);
1339 const deUint32 rnum = hash52(seed);
1340 deUint8 seed1 = (deUint8)( rnum & 0xf);
1341 deUint8 seed2 = (deUint8)((rnum >> 4) & 0xf);
1342 deUint8 seed3 = (deUint8)((rnum >> 8) & 0xf);
1343 deUint8 seed4 = (deUint8)((rnum >> 12) & 0xf);
1344 deUint8 seed5 = (deUint8)((rnum >> 16) & 0xf);
1345 deUint8 seed6 = (deUint8)((rnum >> 20) & 0xf);
1346 deUint8 seed7 = (deUint8)((rnum >> 24) & 0xf);
1347 deUint8 seed8 = (deUint8)((rnum >> 28) & 0xf);
1348 deUint8 seed9 = (deUint8)((rnum >> 18) & 0xf);
1349 deUint8 seed10 = (deUint8)((rnum >> 22) & 0xf);
1350 deUint8 seed11 = (deUint8)((rnum >> 26) & 0xf);
1351 deUint8 seed12 = (deUint8)(((rnum >> 30) | (rnum << 2)) & 0xf);
1353 seed1 = (deUint8)(seed1 * seed1 );
1354 seed2 = (deUint8)(seed2 * seed2 );
1355 seed3 = (deUint8)(seed3 * seed3 );
1356 seed4 = (deUint8)(seed4 * seed4 );
1357 seed5 = (deUint8)(seed5 * seed5 );
1358 seed6 = (deUint8)(seed6 * seed6 );
1359 seed7 = (deUint8)(seed7 * seed7 );
1360 seed8 = (deUint8)(seed8 * seed8 );
1361 seed9 = (deUint8)(seed9 * seed9 );
1362 seed10 = (deUint8)(seed10 * seed10);
1363 seed11 = (deUint8)(seed11 * seed11);
1364 seed12 = (deUint8)(seed12 * seed12);
1366 const int shA = (seed & 2) != 0 ? 4 : 5;
1367 const int shB = numPartitions == 3 ? 6 : 5;
1368 const int sh1 = (seed & 1) != 0 ? shA : shB;
1369 const int sh2 = (seed & 1) != 0 ? shB : shA;
1370 const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;
1372 seed1 = (deUint8)(seed1 >> sh1);
1373 seed2 = (deUint8)(seed2 >> sh2);
1374 seed3 = (deUint8)(seed3 >> sh1);
1375 seed4 = (deUint8)(seed4 >> sh2);
1376 seed5 = (deUint8)(seed5 >> sh1);
1377 seed6 = (deUint8)(seed6 >> sh2);
1378 seed7 = (deUint8)(seed7 >> sh1);
1379 seed8 = (deUint8)(seed8 >> sh2);
1380 seed9 = (deUint8)(seed9 >> sh3);
1381 seed10 = (deUint8)(seed10 >> sh3);
1382 seed11 = (deUint8)(seed11 >> sh3);
1383 seed12 = (deUint8)(seed12 >> sh3);
1385 const int a = 0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));
1386 const int b = 0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));
1387 const int c = numPartitions >= 3 ? 0x3f & (seed5*x + seed6*y + seed9*z + (rnum >> 6)) : 0;
1388 const int d = numPartitions >= 4 ? 0x3f & (seed7*x + seed8*y + seed10*z + (rnum >> 2)) : 0;
1390 return a >= b && a >= c && a >= d ? 0
1391 : b >= c && b >= d ? 1
1396 DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,
1397 int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)
1399 const bool smallBlock = blockWidth*blockHeight < 31;
1400 DecompressResult result = DECOMPRESS_RESULT_VALID_BLOCK;
1401 bool isHDREndpoint[4];
1403 for (int i = 0; i < numPartitions; i++)
1404 isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);
1406 for (int texelY = 0; texelY < blockHeight; texelY++)
1407 for (int texelX = 0; texelX < blockWidth; texelX++)
1409 const int texelNdx = texelY*blockWidth + texelX;
1410 const int colorEndpointNdx = numPartitions == 1 ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);
1411 DE_ASSERT(colorEndpointNdx < numPartitions);
1412 const UVec4& e0 = colorEndpoints[colorEndpointNdx].e0;
1413 const UVec4& e1 = colorEndpoints[colorEndpointNdx].e1;
1414 const TexelWeightPair& weight = texelWeights[texelNdx];
1416 if (isLDRMode && isHDREndpoint[colorEndpointNdx])
1420 ((deUint8*)dst)[texelNdx*4 + 0] = 0xff;
1421 ((deUint8*)dst)[texelNdx*4 + 1] = 0;
1422 ((deUint8*)dst)[texelNdx*4 + 2] = 0xff;
1423 ((deUint8*)dst)[texelNdx*4 + 3] = 0xff;
1427 ((float*)dst)[texelNdx*4 + 0] = 1.0f;
1428 ((float*)dst)[texelNdx*4 + 1] = 0;
1429 ((float*)dst)[texelNdx*4 + 2] = 1.0f;
1430 ((float*)dst)[texelNdx*4 + 3] = 1.0f;
1433 result = DECOMPRESS_RESULT_ERROR;
1437 for (int channelNdx = 0; channelNdx < 4; channelNdx++)
1439 if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.
1441 const deUint32 c0 = (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);
1442 const deUint32 c1 = (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);
1443 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1444 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1447 ((deUint8*)dst)[texelNdx*4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);
1449 ((float*)dst)[texelNdx*4 + channelNdx] = c == 65535 ? 1.0f : (float)c / 65536.0f;
1453 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1454 const deUint32 c0 = e0[channelNdx] << 4;
1455 const deUint32 c1 = e1[channelNdx] << 4;
1456 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1457 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1458 const deUint32 e = getBits(c, 11, 15);
1459 const deUint32 m = getBits(c, 0, 10);
1460 const deUint32 mt = m < 512 ? 3*m
1461 : m >= 1536 ? 5*m - 2048
1463 const deFloat16 cf = (deFloat16)((e << 10) + (mt >> 3));
1465 ((float*)dst)[texelNdx*4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);
1474 DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)
1476 DE_ASSERT(isLDR || !isSRGB);
1478 // Decode block mode.
1480 const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));
1482 // Check for block mode errors.
1484 if (blockMode.isError)
1486 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1487 return DECOMPRESS_RESULT_ERROR;
1490 // Separate path for void-extent.
1492 if (blockMode.isVoidExtent)
1493 return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);
1495 // Compute weight grid values.
1497 const int numWeights = computeNumWeights(blockMode);
1498 const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);
1499 const int numPartitions = (int)blockData.getBits(11, 12) + 1;
1501 // Check for errors in weight grid, partition and dual-plane parameters.
1503 if (numWeights > 64 ||
1504 numWeightDataBits > 96 ||
1505 numWeightDataBits < 24 ||
1506 blockMode.weightGridWidth > blockWidth ||
1507 blockMode.weightGridHeight > blockHeight ||
1508 (numPartitions == 4 && blockMode.isDualPlane))
1510 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1511 return DECOMPRESS_RESULT_ERROR;
1514 // Compute number of bits available for color endpoint data.
1516 const bool isSingleUniqueCem = numPartitions == 1 || blockData.getBits(23, 24) == 0;
1517 const int numConfigDataBits = (numPartitions == 1 ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +
1518 (blockMode.isDualPlane ? 2 : 0);
1519 const int numBitsForColorEndpoints = 128 - numWeightDataBits - numConfigDataBits;
1520 const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -1
1521 : numPartitions == 4 ? 7
1522 : numPartitions == 3 ? 4
1523 : numPartitions == 2 ? 1
1525 // Decode color endpoint modes.
1527 deUint32 colorEndpointModes[4];
1528 decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);
1530 const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);
1532 // Check for errors in color endpoint value count.
1534 if (numColorEndpointValues > 18 || numBitsForColorEndpoints < deDivRoundUp32(13*numColorEndpointValues, 5))
1536 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1537 return DECOMPRESS_RESULT_ERROR;
1540 // Compute color endpoints.
1542 ColorEndpointPair colorEndpoints[4];
1543 computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,
1544 computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);
1546 // Compute texel weights.
1548 TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];
1549 computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);
1551 // Set texel colors.
1553 const int ccs = blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;
1554 const deUint32 partitionIndexSeed = numPartitions > 1 ? blockData.getBits(13, 22) : (deUint32)-1;
1556 return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);
1559 void decompress (const PixelBufferAccess& dst, const deUint8* data, bool isSRGB, bool isLDR)
1561 DE_ASSERT(isLDR || !isSRGB);
1563 const int blockWidth = dst.getWidth();
1564 const int blockHeight = dst.getHeight();
1568 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1569 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1570 } decompressedBuffer;
1572 const Block128 blockData(data);
1573 decompressBlock(isSRGB ? (void*)&decompressedBuffer.sRGB[0] : (void*)&decompressedBuffer.linear[0],
1574 blockData, dst.getWidth(), dst.getHeight(), isSRGB, isLDR);
1578 for (int i = 0; i < blockHeight; i++)
1579 for (int j = 0; j < blockWidth; j++)
1581 dst.setPixel(IVec4(decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 0],
1582 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 1],
1583 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 2],
1584 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 3]), j, i);
1589 for (int i = 0; i < blockHeight; i++)
1590 for (int j = 0; j < blockWidth; j++)
1592 dst.setPixel(Vec4(decompressedBuffer.linear[(i*blockWidth + j) * 4 + 0],
1593 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 1],
1594 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 2],
1595 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 3]), j, i);
1600 // Helper class for setting bits in a 128-bit block.
1601 class AssignBlock128
1604 typedef deUint64 Word;
1608 WORD_BYTES = sizeof(Word),
1609 WORD_BITS = 8*WORD_BYTES,
1610 NUM_WORDS = 128 / WORD_BITS
1613 DE_STATIC_ASSERT(128 % WORD_BITS == 0);
1616 AssignBlock128 (void)
1618 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1619 m_words[wordNdx] = 0;
1622 void setBit (int ndx, deUint32 val)
1624 DE_ASSERT(de::inBounds(ndx, 0, 128));
1625 DE_ASSERT((val & 1) == val);
1626 const int wordNdx = ndx / WORD_BITS;
1627 const int bitNdx = ndx % WORD_BITS;
1628 m_words[wordNdx] = (m_words[wordNdx] & ~((Word)1 << bitNdx)) | ((Word)val << bitNdx);
1631 void setBits (int low, int high, deUint32 bits)
1633 DE_ASSERT(de::inBounds(low, 0, 128));
1634 DE_ASSERT(de::inBounds(high, 0, 128));
1635 DE_ASSERT(de::inRange(high-low+1, 0, 32));
1636 DE_ASSERT((bits & (((Word)1 << (high-low+1)) - 1)) == bits);
1638 if (high-low+1 == 0)
1641 const int word0Ndx = low / WORD_BITS;
1642 const int word1Ndx = high / WORD_BITS;
1643 const int lowNdxInW0 = low % WORD_BITS;
1645 if (word0Ndx == word1Ndx)
1646 m_words[word0Ndx] = (m_words[word0Ndx] & ~((((Word)1 << (high-low+1)) - 1) << lowNdxInW0)) | ((Word)bits << lowNdxInW0);
1649 DE_ASSERT(word1Ndx == word0Ndx + 1);
1651 const int highNdxInW1 = high % WORD_BITS;
1652 const int numBitsToSetInW0 = WORD_BITS - lowNdxInW0;
1653 const Word bitsLowMask = ((Word)1 << numBitsToSetInW0) - 1;
1655 m_words[word0Ndx] = (m_words[word0Ndx] & (((Word)1 << lowNdxInW0) - 1)) | (((Word)bits & bitsLowMask) << lowNdxInW0);
1656 m_words[word1Ndx] = (m_words[word1Ndx] & ~(((Word)1 << (highNdxInW1+1)) - 1)) | (((Word)bits & ~bitsLowMask) >> numBitsToSetInW0);
1660 void assignToMemory (deUint8* dst) const
1662 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1664 for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
1665 dst[wordNdx*WORD_BYTES + byteNdx] = (deUint8)((m_words[wordNdx] >> (8*byteNdx)) & 0xff);
1669 void pushBytesToVector (vector<deUint8>& dst) const
1671 const int assignStartIndex = (int)dst.size();
1672 dst.resize(dst.size() + BLOCK_SIZE_BYTES);
1673 assignToMemory(&dst[assignStartIndex]);
1677 Word m_words[NUM_WORDS];
1680 // A helper for sequential access into a AssignBlock128.
1681 class BitAssignAccessStream
1684 BitAssignAccessStream (AssignBlock128& dst, int startNdxInSrc, int length, bool forward)
1686 , m_startNdxInSrc (startNdxInSrc)
1688 , m_forward (forward)
1693 // Set the next num bits. Bits at positions greater than or equal to m_length are not touched.
1694 void setNext (int num, deUint32 bits)
1696 DE_ASSERT((bits & (((deUint64)1 << num) - 1)) == bits);
1698 if (num == 0 || m_ndx >= m_length)
1701 const int end = m_ndx + num;
1702 const int numBitsToDst = de::max(0, de::min(m_length, end) - m_ndx);
1703 const int low = m_ndx;
1704 const int high = m_ndx + numBitsToDst - 1;
1705 const deUint32 actualBits = getBits(bits, 0, numBitsToDst-1);
1709 return m_forward ? m_dst.setBits(m_startNdxInSrc + low, m_startNdxInSrc + high, actualBits)
1710 : m_dst.setBits(m_startNdxInSrc - high, m_startNdxInSrc - low, reverseBits(actualBits, numBitsToDst));
1714 AssignBlock128& m_dst;
1715 const int m_startNdxInSrc;
1717 const bool m_forward;
1722 struct VoidExtentParams
1724 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1730 // \note Currently extent coordinates are all set to all-ones.
1732 VoidExtentParams (bool isHDR_, deUint16 r_, deUint16 g_, deUint16 b_, deUint16 a_) : isHDR(isHDR_), r(r_), g(g_), b(b_), a(a_) {}
1735 static AssignBlock128 generateVoidExtentBlock (const VoidExtentParams& params)
1737 AssignBlock128 block;
1739 block.setBits(0, 8, 0x1fc); // \note Marks void-extent block.
1740 block.setBit(9, params.isHDR);
1741 block.setBits(10, 11, 3); // \note Spec shows that these bits are both set, although they serve no purpose.
1743 // Extent coordinates - currently all-ones.
1744 block.setBits(12, 24, 0x1fff);
1745 block.setBits(25, 37, 0x1fff);
1746 block.setBits(38, 50, 0x1fff);
1747 block.setBits(51, 63, 0x1fff);
1749 DE_ASSERT(!params.isHDR || (!isFloat16InfOrNan(params.r) &&
1750 !isFloat16InfOrNan(params.g) &&
1751 !isFloat16InfOrNan(params.b) &&
1752 !isFloat16InfOrNan(params.a)));
1754 block.setBits(64, 79, params.r);
1755 block.setBits(80, 95, params.g);
1756 block.setBits(96, 111, params.b);
1757 block.setBits(112, 127, params.a);
1762 // An input array of ISE inputs for an entire ASTC block. Can be given as either single values in the
1763 // range [0, maximumValueOfISERange] or as explicit block value specifications. The latter is needed
1764 // so we can test all possible values of T and Q in a block, since multiple T or Q values may map
1765 // to the same set of decoded values.
1770 deUint32 tOrQValue; //!< The 8-bit T or 7-bit Q in a trit or quint ISE block.
1771 deUint32 bitValues[5];
1774 bool isGivenInBlockForm;
1777 //!< \note 64 comes from the maximum number of weight values in an ASTC block.
1783 : isGivenInBlockForm (false)
1788 static inline deUint32 computeISERangeMax (const ISEParams& iseParams)
1790 switch (iseParams.mode)
1792 case ISEMODE_TRIT: return (1u << iseParams.numBits) * 3 - 1;
1793 case ISEMODE_QUINT: return (1u << iseParams.numBits) * 5 - 1;
1794 case ISEMODE_PLAIN_BIT: return (1u << iseParams.numBits) - 1;
1801 struct NormalBlockParams
1803 int weightGridWidth;
1804 int weightGridHeight;
1805 ISEParams weightISEParams;
1807 deUint32 ccs; //! \note Irrelevant if !isDualPlane.
1809 deUint32 colorEndpointModes[4];
1810 // \note Below members are irrelevant if numPartitions == 1.
1811 bool isMultiPartSingleCemMode; //! \note If true, the single CEM is at colorEndpointModes[0].
1812 deUint32 partitionSeed;
1814 NormalBlockParams (void)
1815 : weightGridWidth (-1)
1816 , weightGridHeight (-1)
1817 , weightISEParams (ISEMODE_LAST, -1)
1818 , isDualPlane (true)
1819 , ccs ((deUint32)-1)
1820 , numPartitions (-1)
1821 , isMultiPartSingleCemMode (false)
1822 , partitionSeed ((deUint32)-1)
1824 colorEndpointModes[0] = 0;
1825 colorEndpointModes[1] = 0;
1826 colorEndpointModes[2] = 0;
1827 colorEndpointModes[3] = 0;
1831 struct NormalBlockISEInputs
1836 NormalBlockISEInputs (void)
1843 static inline int computeNumWeights (const NormalBlockParams& params)
1845 return params.weightGridWidth * params.weightGridHeight * (params.isDualPlane ? 2 : 1);
1848 static inline int computeNumBitsForColorEndpoints (const NormalBlockParams& params)
1850 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, computeNumWeights(params));
1851 const int numConfigDataBits = (params.numPartitions == 1 ? 17 : params.isMultiPartSingleCemMode ? 29 : 25 + 3*params.numPartitions) +
1852 (params.isDualPlane ? 2 : 0);
1854 return 128 - numWeightBits - numConfigDataBits;
1857 static inline int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions, bool isMultiPartSingleCemMode)
1859 if (isMultiPartSingleCemMode)
1860 return numPartitions * computeNumColorEndpointValues(endpointModes[0]);
1864 for (int i = 0; i < numPartitions; i++)
1865 result += computeNumColorEndpointValues(endpointModes[i]);
1870 static inline bool isValidBlockParams (const NormalBlockParams& params, int blockWidth, int blockHeight)
1872 const int numWeights = computeNumWeights(params);
1873 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, numWeights);
1874 const int numColorEndpointValues = computeNumColorEndpointValues(¶ms.colorEndpointModes[0], params.numPartitions, params.isMultiPartSingleCemMode);
1875 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(params);
1877 return numWeights <= 64 &&
1878 de::inRange(numWeightBits, 24, 96) &&
1879 params.weightGridWidth <= blockWidth &&
1880 params.weightGridHeight <= blockHeight &&
1881 !(params.numPartitions == 4 && params.isDualPlane) &&
1882 numColorEndpointValues <= 18 &&
1883 numBitsForColorEndpoints >= deDivRoundUp32(13*numColorEndpointValues, 5);
1886 // Write bits 0 to 10 of an ASTC block.
1887 static void writeBlockMode (AssignBlock128& dst, const NormalBlockParams& blockParams)
1889 const deUint32 d = blockParams.isDualPlane != 0;
1890 // r and h initialized in switch below.
1893 // a, b and blockModeLayoutNdx initialized in block mode layout index detecting loop below.
1894 deUint32 a = (deUint32)-1;
1895 deUint32 b = (deUint32)-1;
1896 int blockModeLayoutNdx;
1898 // Find the values of r and h (ISE range).
1899 switch (computeISERangeMax(blockParams.weightISEParams))
1901 case 1: r = 2; h = 0; break;
1902 case 2: r = 3; h = 0; break;
1903 case 3: r = 4; h = 0; break;
1904 case 4: r = 5; h = 0; break;
1905 case 5: r = 6; h = 0; break;
1906 case 7: r = 7; h = 0; break;
1908 case 9: r = 2; h = 1; break;
1909 case 11: r = 3; h = 1; break;
1910 case 15: r = 4; h = 1; break;
1911 case 19: r = 5; h = 1; break;
1912 case 23: r = 6; h = 1; break;
1913 case 31: r = 7; h = 1; break;
1921 // Find block mode layout index, i.e. appropriate row in the "2d block mode layout" table in ASTC spec.
1924 enum BlockModeLayoutABVariable { Z=0, A=1, B=2 };
1926 static const struct BlockModeLayout
1930 BlockModeLayoutABVariable gridWidthVariableTerm;
1931 int gridWidthConstantTerm;
1932 BlockModeLayoutABVariable gridHeightVariableTerm;
1933 int gridHeightConstantTerm;
1934 } blockModeLayouts[] =
1936 { 2, 2, B, 4, A, 2},
1937 { 2, 2, B, 8, A, 2},
1938 { 2, 2, A, 2, B, 8},
1939 { 2, 1, A, 2, B, 6},
1940 { 2, 1, B, 2, A, 2},
1941 { 2, 0, Z, 12, A, 2},
1942 { 2, 0, A, 2, Z, 12},
1943 { 0, 0, Z, 6, Z, 10},
1944 { 0, 0, Z, 10, Z, 6},
1948 for (blockModeLayoutNdx = 0; blockModeLayoutNdx < DE_LENGTH_OF_ARRAY(blockModeLayouts); blockModeLayoutNdx++)
1950 const BlockModeLayout& layout = blockModeLayouts[blockModeLayoutNdx];
1951 const int aMax = (1 << layout.aNumBits) - 1;
1952 const int bMax = (1 << layout.bNumBits) - 1;
1953 const int variableOffsetsMax[3] = { 0, aMax, bMax };
1954 const int widthMin = layout.gridWidthConstantTerm;
1955 const int heightMin = layout.gridHeightConstantTerm;
1956 const int widthMax = widthMin + variableOffsetsMax[layout.gridWidthVariableTerm];
1957 const int heightMax = heightMin + variableOffsetsMax[layout.gridHeightVariableTerm];
1959 DE_ASSERT(layout.gridWidthVariableTerm != layout.gridHeightVariableTerm || layout.gridWidthVariableTerm == Z);
1961 if (de::inRange(blockParams.weightGridWidth, widthMin, widthMax) &&
1962 de::inRange(blockParams.weightGridHeight, heightMin, heightMax))
1965 deUint32& widthVariable = layout.gridWidthVariableTerm == A ? a : layout.gridWidthVariableTerm == B ? b : dummy;
1966 deUint32& heightVariable = layout.gridHeightVariableTerm == A ? a : layout.gridHeightVariableTerm == B ? b : dummy;
1968 widthVariable = blockParams.weightGridWidth - layout.gridWidthConstantTerm;
1969 heightVariable = blockParams.weightGridHeight - layout.gridHeightConstantTerm;
1976 // Set block mode bits.
1978 const deUint32 a0 = getBit(a, 0);
1979 const deUint32 a1 = getBit(a, 1);
1980 const deUint32 b0 = getBit(b, 0);
1981 const deUint32 b1 = getBit(b, 1);
1982 const deUint32 r0 = getBit(r, 0);
1983 const deUint32 r1 = getBit(r, 1);
1984 const deUint32 r2 = getBit(r, 2);
1986 #define SB(NDX, VAL) dst.setBit((NDX), (VAL))
1987 #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)
1989 switch (blockModeLayoutNdx)
1991 case 0: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 0, r2, r1); break;
1992 case 1: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 1, r2, r1); break;
1993 case 2: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 1, 0, r2, r1); break;
1994 case 3: ASSIGN_BITS(d, h, 0, b, a1, a0, r0, 1, 1, r2, r1); break;
1995 case 4: ASSIGN_BITS(d, h, 1, b, a1, a0, r0, 1, 1, r2, r1); break;
1996 case 5: ASSIGN_BITS(d, h, 0, 0, a1, a0, r0, r2, r1, 0, 0); break;
1997 case 6: ASSIGN_BITS(d, h, 0, 1, a1, a0, r0, r2, r1, 0, 0); break;
1998 case 7: ASSIGN_BITS(d, h, 1, 1, 0, 0, r0, r2, r1, 0, 0); break;
1999 case 8: ASSIGN_BITS(d, h, 1, 1, 0, 1, r0, r2, r1, 0, 0); break;
2000 case 9: ASSIGN_BITS(b1, b0, 1, 0, a1, a0, r0, r2, r1, 0, 0); DE_ASSERT(d == 0 && h == 0); break;
2009 // Write color endpoint mode data of an ASTC block.
2010 static void writeColorEndpointModes (AssignBlock128& dst, const deUint32* colorEndpointModes, bool isMultiPartSingleCemMode, int numPartitions, int extraCemBitsStart)
2012 if (numPartitions == 1)
2013 dst.setBits(13, 16, colorEndpointModes[0]);
2016 if (isMultiPartSingleCemMode)
2018 dst.setBits(23, 24, 0);
2019 dst.setBits(25, 28, colorEndpointModes[0]);
2023 DE_ASSERT(numPartitions > 0);
2024 const deUint32 minCem = *std::min_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2025 const deUint32 maxCem = *std::max_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2026 const deUint32 minCemClass = minCem/4;
2027 const deUint32 maxCemClass = maxCem/4;
2028 DE_ASSERT(maxCemClass - minCemClass <= 1);
2029 DE_UNREF(minCemClass); // \note For non-debug builds.
2030 const deUint32 highLevelSelector = de::max(1u, maxCemClass);
2032 dst.setBits(23, 24, highLevelSelector);
2034 for (int partNdx = 0; partNdx < numPartitions; partNdx++)
2036 const deUint32 c = colorEndpointModes[partNdx] / 4 == highLevelSelector ? 1 : 0;
2037 const deUint32 m = colorEndpointModes[partNdx] % 4;
2038 const deUint32 lowMBit0Ndx = numPartitions + 2*partNdx;
2039 const deUint32 lowMBit1Ndx = numPartitions + 2*partNdx + 1;
2040 dst.setBit(25 + partNdx, c);
2041 dst.setBit(lowMBit0Ndx < 4 ? 25+lowMBit0Ndx : extraCemBitsStart+lowMBit0Ndx-4, getBit(m, 0));
2042 dst.setBit(lowMBit1Ndx < 4 ? 25+lowMBit1Ndx : extraCemBitsStart+lowMBit1Ndx-4, getBit(m, 1));
2048 static void encodeISETritBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2050 // tritBlockTValue[t0][t1][t2][t3][t4] is a value of T (not necessarily the only one) that will yield the given trits when decoded.
2051 static const deUint32 tritBlockTValue[3][3][3][3][3] =
2054 {{{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}}},
2055 {{{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}}},
2056 {{{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}}}
2059 {{{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}}},
2060 {{{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}}},
2061 {{{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}}}
2064 {{{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}}},
2065 {{{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}}},
2066 {{{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}}}
2070 DE_ASSERT(de::inRange(numValues, 1, 5));
2072 deUint32 tritParts[5];
2073 deUint32 bitParts[5];
2075 for (int i = 0; i < 5; i++)
2079 if (fromExplicitInputBlock)
2081 bitParts[i] = blockInput.bitValues[i];
2082 tritParts[i] = -1; // \note Won't be used, but silences warning.
2086 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2087 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2088 tritParts[i] = nonBlockInput[i] >> numBits;
2098 const deUint32 T = fromExplicitInputBlock ? blockInput.tOrQValue : tritBlockTValue[tritParts[0]]
2104 dst.setNext(numBits, bitParts[0]);
2105 dst.setNext(2, getBits(T, 0, 1));
2106 dst.setNext(numBits, bitParts[1]);
2107 dst.setNext(2, getBits(T, 2, 3));
2108 dst.setNext(numBits, bitParts[2]);
2109 dst.setNext(1, getBit(T, 4));
2110 dst.setNext(numBits, bitParts[3]);
2111 dst.setNext(2, getBits(T, 5, 6));
2112 dst.setNext(numBits, bitParts[4]);
2113 dst.setNext(1, getBit(T, 7));
2116 static void encodeISEQuintBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2118 // quintBlockQValue[q0][q1][q2] is a value of Q (not necessarily the only one) that will yield the given quints when decoded.
2119 static const deUint32 quintBlockQValue[5][5][5] =
2121 {{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}},
2122 {{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}},
2123 {{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}},
2124 {{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}},
2125 {{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}}
2128 DE_ASSERT(de::inRange(numValues, 1, 3));
2130 deUint32 quintParts[3];
2131 deUint32 bitParts[3];
2133 for (int i = 0; i < 3; i++)
2137 if (fromExplicitInputBlock)
2139 bitParts[i] = blockInput.bitValues[i];
2140 quintParts[i] = -1; // \note Won't be used, but silences warning.
2144 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2145 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2146 quintParts[i] = nonBlockInput[i] >> numBits;
2156 const deUint32 Q = fromExplicitInputBlock ? blockInput.tOrQValue : quintBlockQValue[quintParts[0]]
2160 dst.setNext(numBits, bitParts[0]);
2161 dst.setNext(3, getBits(Q, 0, 2));
2162 dst.setNext(numBits, bitParts[1]);
2163 dst.setNext(2, getBits(Q, 3, 4));
2164 dst.setNext(numBits, bitParts[2]);
2165 dst.setNext(2, getBits(Q, 5, 6));
2168 static void encodeISEBitBlock (BitAssignAccessStream& dst, int numBits, deUint32 value)
2170 DE_ASSERT(de::inRange(value, 0u, (1u<<numBits)-1));
2171 dst.setNext(numBits, value);
2174 static void encodeISE (BitAssignAccessStream& dst, const ISEParams& params, const ISEInput& input, int numValues)
2176 if (params.mode == ISEMODE_TRIT)
2178 const int numBlocks = deDivRoundUp32(numValues, 5);
2179 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2181 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
2182 encodeISETritBlock(dst, params.numBits, input.isGivenInBlockForm,
2183 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2184 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[5*blockNdx],
2188 else if (params.mode == ISEMODE_QUINT)
2190 const int numBlocks = deDivRoundUp32(numValues, 3);
2191 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2193 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
2194 encodeISEQuintBlock(dst, params.numBits, input.isGivenInBlockForm,
2195 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2196 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[3*blockNdx],
2202 DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
2203 for (int i = 0; i < numValues; i++)
2204 encodeISEBitBlock(dst, params.numBits, input.isGivenInBlockForm ? input.value.block[i].bitValues[0] : input.value.plain[i]);
2208 static void writeWeightData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numWeights)
2210 const int numWeightBits = computeNumRequiredBits(iseParams, numWeights);
2211 BitAssignAccessStream access (dst, 127, numWeightBits, false);
2212 encodeISE(access, iseParams, input, numWeights);
2215 static void writeColorEndpointData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numEndpoints, int numBitsForColorEndpoints, int colorEndpointDataStartNdx)
2217 BitAssignAccessStream access(dst, colorEndpointDataStartNdx, numBitsForColorEndpoints, true);
2218 encodeISE(access, iseParams, input, numEndpoints);
2221 static AssignBlock128 generateNormalBlock (const NormalBlockParams& blockParams, int blockWidth, int blockHeight, const NormalBlockISEInputs& iseInputs)
2223 DE_ASSERT(isValidBlockParams(blockParams, blockWidth, blockHeight));
2224 DE_UNREF(blockWidth); // \note For non-debug builds.
2225 DE_UNREF(blockHeight); // \note For non-debug builds.
2227 AssignBlock128 block;
2228 const int numWeights = computeNumWeights(blockParams);
2229 const int numWeightBits = computeNumRequiredBits(blockParams.weightISEParams, numWeights);
2231 writeBlockMode(block, blockParams);
2233 block.setBits(11, 12, blockParams.numPartitions - 1);
2234 if (blockParams.numPartitions > 1)
2235 block.setBits(13, 22, blockParams.partitionSeed);
2238 const int extraCemBitsStart = 127 - numWeightBits - (blockParams.numPartitions == 1 || blockParams.isMultiPartSingleCemMode ? -1
2239 : blockParams.numPartitions == 4 ? 7
2240 : blockParams.numPartitions == 3 ? 4
2241 : blockParams.numPartitions == 2 ? 1
2244 writeColorEndpointModes(block, &blockParams.colorEndpointModes[0], blockParams.isMultiPartSingleCemMode, blockParams.numPartitions, extraCemBitsStart);
2246 if (blockParams.isDualPlane)
2247 block.setBits(extraCemBitsStart-2, extraCemBitsStart-1, blockParams.ccs);
2250 writeWeightData(block, blockParams.weightISEParams, iseInputs.weight, numWeights);
2253 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2254 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2255 const int colorEndpointDataStartNdx = blockParams.numPartitions == 1 ? 17 : 29;
2256 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2258 writeColorEndpointData(block, colorEndpointISEParams, iseInputs.endpoint, numColorEndpointValues, numBitsForColorEndpoints, colorEndpointDataStartNdx);
2264 // Generate default ISE inputs for weight and endpoint data - gradient-ish values.
2265 static NormalBlockISEInputs generateDefaultISEInputs (const NormalBlockParams& blockParams)
2267 NormalBlockISEInputs result;
2270 result.weight.isGivenInBlockForm = false;
2272 const int numWeights = computeNumWeights(blockParams);
2273 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2275 if (blockParams.isDualPlane)
2277 for (int i = 0; i < numWeights; i += 2)
2278 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2280 for (int i = 1; i < numWeights; i += 2)
2281 result.weight.value.plain[i] = weightRangeMax - (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2285 for (int i = 0; i < numWeights; i++)
2286 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2291 result.endpoint.isGivenInBlockForm = false;
2293 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2294 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2295 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2296 const int colorEndpointRangeMax = computeISERangeMax(colorEndpointISEParams);
2298 for (int i = 0; i < numColorEndpointValues; i++)
2299 result.endpoint.value.plain[i] = (i*colorEndpointRangeMax + (numColorEndpointValues-1)/2) / (numColorEndpointValues-1);
2305 static const ISEParams s_weightISEParamsCandidates[] =
2307 ISEParams(ISEMODE_PLAIN_BIT, 1),
2308 ISEParams(ISEMODE_TRIT, 0),
2309 ISEParams(ISEMODE_PLAIN_BIT, 2),
2310 ISEParams(ISEMODE_QUINT, 0),
2311 ISEParams(ISEMODE_TRIT, 1),
2312 ISEParams(ISEMODE_PLAIN_BIT, 3),
2313 ISEParams(ISEMODE_QUINT, 1),
2314 ISEParams(ISEMODE_TRIT, 2),
2315 ISEParams(ISEMODE_PLAIN_BIT, 4),
2316 ISEParams(ISEMODE_QUINT, 2),
2317 ISEParams(ISEMODE_TRIT, 3),
2318 ISEParams(ISEMODE_PLAIN_BIT, 5)
2321 void generateRandomBlock (deUint8* dst, const IVec3& blockSize, de::Random& rnd)
2323 DE_ASSERT(blockSize.z() == 1);
2325 if (rnd.getFloat() < 0.1f)
2327 // Void extent block.
2328 const bool isVoidExtentHDR = rnd.getBool();
2329 const deUint16 r = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2330 const deUint16 g = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2331 const deUint16 b = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2332 const deUint16 a = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2333 generateVoidExtentBlock(VoidExtentParams(isVoidExtentHDR, r, g, b, a)).assignToMemory(dst);
2337 // Not void extent block.
2339 // Generate block params.
2341 NormalBlockParams blockParams;
2345 blockParams.weightGridWidth = rnd.getInt(2, blockSize.x());
2346 blockParams.weightGridHeight = rnd.getInt(2, blockSize.y());
2347 blockParams.weightISEParams = s_weightISEParamsCandidates[rnd.getInt(0, DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates)-1)];
2348 blockParams.numPartitions = rnd.getInt(1, 4);
2349 blockParams.isMultiPartSingleCemMode = rnd.getFloat() < 0.25f;
2350 blockParams.isDualPlane = blockParams.numPartitions != 4 && rnd.getBool();
2351 blockParams.ccs = rnd.getInt(0, 3);
2352 blockParams.partitionSeed = rnd.getInt(0, 1023);
2354 blockParams.colorEndpointModes[0] = rnd.getInt(0, 15);
2357 const int cemDiff = blockParams.isMultiPartSingleCemMode ? 0
2358 : blockParams.colorEndpointModes[0] == 0 ? 1
2359 : blockParams.colorEndpointModes[0] == 15 ? -1
2360 : rnd.getBool() ? 1 : -1;
2362 for (int i = 1; i < blockParams.numPartitions; i++)
2363 blockParams.colorEndpointModes[i] = blockParams.colorEndpointModes[0] + (cemDiff == -1 ? rnd.getInt(-1, 0) : cemDiff == 1 ? rnd.getInt(0, 1) : 0);
2365 } while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()));
2367 // Generate ISE inputs for both weight and endpoint data.
2369 NormalBlockISEInputs iseInputs;
2371 for (int weightOrEndpoints = 0; weightOrEndpoints <= 1; weightOrEndpoints++)
2373 const bool setWeights = weightOrEndpoints == 0;
2374 const int numValues = setWeights ? computeNumWeights(blockParams) :
2375 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2376 const ISEParams iseParams = setWeights ? blockParams.weightISEParams : computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams), numValues);
2377 ISEInput& iseInput = setWeights ? iseInputs.weight : iseInputs.endpoint;
2379 iseInput.isGivenInBlockForm = rnd.getBool();
2381 if (iseInput.isGivenInBlockForm)
2383 const int numValuesPerISEBlock = iseParams.mode == ISEMODE_TRIT ? 5
2384 : iseParams.mode == ISEMODE_QUINT ? 3
2386 const int iseBitMax = (1 << iseParams.numBits) - 1;
2387 const int numISEBlocks = deDivRoundUp32(numValues, numValuesPerISEBlock);
2389 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocks; iseBlockNdx++)
2391 iseInput.value.block[iseBlockNdx].tOrQValue = rnd.getInt(0, 255);
2392 for (int i = 0; i < numValuesPerISEBlock; i++)
2393 iseInput.value.block[iseBlockNdx].bitValues[i] = rnd.getInt(0, iseBitMax);
2398 const int rangeMax = computeISERangeMax(iseParams);
2400 for (int valueNdx = 0; valueNdx < numValues; valueNdx++)
2401 iseInput.value.plain[valueNdx] = rnd.getInt(0, rangeMax);
2405 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).assignToMemory(dst);
2411 // Generate block data for a given BlockTestType and format.
2412 void generateBlockCaseTestData (vector<deUint8>& dst, CompressedTexFormat format, BlockTestType testType)
2414 DE_ASSERT(isAstcFormat(format));
2415 DE_ASSERT(!(isAstcSRGBFormat(format) && isBlockTestTypeHDROnly(testType)));
2417 const IVec3 blockSize = getBlockPixelSize(format);
2418 DE_ASSERT(blockSize.z() == 1);
2422 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:
2423 // Generate a gradient-like set of LDR void-extent blocks.
2425 const int numBlocks = 1<<13;
2426 const deUint32 numValues = 1<<16;
2427 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2429 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2431 const deUint32 baseValue = blockNdx*(numValues-1) / (numBlocks-1);
2432 const deUint16 r = (deUint16)((baseValue + numValues*0/4) % numValues);
2433 const deUint16 g = (deUint16)((baseValue + numValues*1/4) % numValues);
2434 const deUint16 b = (deUint16)((baseValue + numValues*2/4) % numValues);
2435 const deUint16 a = (deUint16)((baseValue + numValues*3/4) % numValues);
2436 AssignBlock128 block;
2438 generateVoidExtentBlock(VoidExtentParams(false, r, g, b, a)).pushBytesToVector(dst);
2444 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:
2445 // Generate a gradient-like set of HDR void-extent blocks, with values ranging from the largest finite negative to largest finite positive of fp16.
2447 const float minValue = -65504.0f;
2448 const float maxValue = +65504.0f;
2449 const int numBlocks = 1<<13;
2450 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2452 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2454 const int rNdx = (blockNdx + numBlocks*0/4) % numBlocks;
2455 const int gNdx = (blockNdx + numBlocks*1/4) % numBlocks;
2456 const int bNdx = (blockNdx + numBlocks*2/4) % numBlocks;
2457 const int aNdx = (blockNdx + numBlocks*3/4) % numBlocks;
2458 const deFloat16 r = deFloat32To16(minValue + (float)rNdx * (maxValue - minValue) / (float)(numBlocks-1));
2459 const deFloat16 g = deFloat32To16(minValue + (float)gNdx * (maxValue - minValue) / (float)(numBlocks-1));
2460 const deFloat16 b = deFloat32To16(minValue + (float)bNdx * (maxValue - minValue) / (float)(numBlocks-1));
2461 const deFloat16 a = deFloat32To16(minValue + (float)aNdx * (maxValue - minValue) / (float)(numBlocks-1));
2463 generateVoidExtentBlock(VoidExtentParams(true, r, g, b, a)).pushBytesToVector(dst);
2469 case BLOCK_TEST_TYPE_WEIGHT_GRID:
2470 // Generate different combinations of plane count, weight ISE params, and grid size.
2472 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2473 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2474 for (int weightGridWidth = 2; weightGridWidth <= 12; weightGridWidth++)
2475 for (int weightGridHeight = 2; weightGridHeight <= 12; weightGridHeight++)
2477 NormalBlockParams blockParams;
2478 NormalBlockISEInputs iseInputs;
2480 blockParams.weightGridWidth = weightGridWidth;
2481 blockParams.weightGridHeight = weightGridHeight;
2482 blockParams.isDualPlane = isDualPlane != 0;
2483 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2484 blockParams.ccs = 0;
2485 blockParams.numPartitions = 1;
2486 blockParams.colorEndpointModes[0] = 0;
2488 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2489 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2495 case BLOCK_TEST_TYPE_WEIGHT_ISE:
2496 // For each weight ISE param set, generate blocks that cover:
2497 // - each single value of the ISE's range, at each position inside an ISE block
2498 // - for trit and quint ISEs, each single T or Q value of an ISE block
2500 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2502 const ISEParams& iseParams = s_weightISEParamsCandidates[iseParamsNdx];
2503 NormalBlockParams blockParams;
2505 blockParams.weightGridWidth = 4;
2506 blockParams.weightGridHeight = 4;
2507 blockParams.weightISEParams = iseParams;
2508 blockParams.numPartitions = 1;
2509 blockParams.isDualPlane = blockParams.weightGridWidth * blockParams.weightGridHeight < 24 ? true : false;
2510 blockParams.ccs = 0;
2511 blockParams.colorEndpointModes[0] = 0;
2513 while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2515 blockParams.weightGridWidth--;
2516 blockParams.weightGridHeight--;
2519 const int numValuesInISEBlock = iseParams.mode == ISEMODE_TRIT ? 5 : iseParams.mode == ISEMODE_QUINT ? 3 : 1;
2520 const int numWeights = computeNumWeights(blockParams);
2523 const int numWeightValues = (int)computeISERangeMax(iseParams) + 1;
2524 const int numBlocks = deDivRoundUp32(numWeightValues, numWeights);
2525 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2526 iseInputs.weight.isGivenInBlockForm = false;
2528 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2529 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2531 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2532 iseInputs.weight.value.plain[weightNdx] = (blockNdx*numWeights + weightNdx + offset) % numWeightValues;
2534 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2538 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
2540 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2541 iseInputs.weight.isGivenInBlockForm = true;
2543 const int numTQValues = 1 << (iseParams.mode == ISEMODE_TRIT ? 8 : 7);
2544 const int numISEBlocksPerBlock = deDivRoundUp32(numWeights, numValuesInISEBlock);
2545 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2547 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2548 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2550 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2552 for (int i = 0; i < numValuesInISEBlock; i++)
2553 iseInputs.weight.value.block[iseBlockNdx].bitValues[i] = 0;
2554 iseInputs.weight.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2557 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2565 case BLOCK_TEST_TYPE_CEMS:
2566 // For each plane count & partition count combination, generate all color endpoint mode combinations.
2568 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2569 for (int numPartitions = 1; numPartitions <= (isDualPlane != 0 ? 3 : 4); numPartitions++)
2571 // Multi-partition, single-CEM mode.
2572 if (numPartitions > 1)
2574 for (deUint32 singleCem = 0; singleCem < 16; singleCem++)
2576 NormalBlockParams blockParams;
2577 blockParams.weightGridWidth = 4;
2578 blockParams.weightGridHeight = 4;
2579 blockParams.isDualPlane = isDualPlane != 0;
2580 blockParams.ccs = 0;
2581 blockParams.numPartitions = numPartitions;
2582 blockParams.isMultiPartSingleCemMode = true;
2583 blockParams.colorEndpointModes[0] = singleCem;
2584 blockParams.partitionSeed = 634;
2586 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2588 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2589 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2591 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2598 // Separate-CEM mode.
2599 for (deUint32 cem0 = 0; cem0 < 16; cem0++)
2600 for (deUint32 cem1 = 0; cem1 < (numPartitions >= 2 ? 16u : 1u); cem1++)
2601 for (deUint32 cem2 = 0; cem2 < (numPartitions >= 3 ? 16u : 1u); cem2++)
2602 for (deUint32 cem3 = 0; cem3 < (numPartitions >= 4 ? 16u : 1u); cem3++)
2604 NormalBlockParams blockParams;
2605 blockParams.weightGridWidth = 4;
2606 blockParams.weightGridHeight = 4;
2607 blockParams.isDualPlane = isDualPlane != 0;
2608 blockParams.ccs = 0;
2609 blockParams.numPartitions = numPartitions;
2610 blockParams.isMultiPartSingleCemMode = false;
2611 blockParams.colorEndpointModes[0] = cem0;
2612 blockParams.colorEndpointModes[1] = cem1;
2613 blockParams.colorEndpointModes[2] = cem2;
2614 blockParams.colorEndpointModes[3] = cem3;
2615 blockParams.partitionSeed = 634;
2618 const deUint32 minCem = *std::min_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2619 const deUint32 maxCem = *std::max_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2620 const deUint32 minCemClass = minCem/4;
2621 const deUint32 maxCemClass = maxCem/4;
2623 if (maxCemClass - minCemClass > 1)
2627 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2629 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2630 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2632 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2642 case BLOCK_TEST_TYPE_PARTITION_SEED:
2643 // Test all partition seeds ("partition pattern indices").
2645 for (int numPartitions = 2; numPartitions <= 4; numPartitions++)
2646 for (deUint32 partitionSeed = 0; partitionSeed < 1<<10; partitionSeed++)
2648 NormalBlockParams blockParams;
2649 blockParams.weightGridWidth = 4;
2650 blockParams.weightGridHeight = 4;
2651 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2652 blockParams.isDualPlane = false;
2653 blockParams.numPartitions = numPartitions;
2654 blockParams.isMultiPartSingleCemMode = true;
2655 blockParams.colorEndpointModes[0] = 0;
2656 blockParams.partitionSeed = partitionSeed;
2658 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2664 // \note Fall-through.
2665 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:
2666 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:
2667 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:
2668 // For each endpoint mode, for each pair of components in the endpoint value, test 10x10 combinations of values for that pair.
2669 // \note Separate modes for HDR and mode 15 due to different color scales and biases.
2671 for (deUint32 cem = 0; cem < 16; cem++)
2673 const bool isHDRCem = cem == 2 ||
2680 if ((testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR && isHDRCem) ||
2681 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 && (!isHDRCem || cem == 15)) ||
2682 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15 && cem != 15))
2685 NormalBlockParams blockParams;
2686 blockParams.weightGridWidth = 3;
2687 blockParams.weightGridHeight = 4;
2688 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2689 blockParams.isDualPlane = false;
2690 blockParams.numPartitions = 1;
2691 blockParams.colorEndpointModes[0] = cem;
2694 const int numBitsForEndpoints = computeNumBitsForColorEndpoints(blockParams);
2695 const int numEndpointParts = computeNumColorEndpointValues(cem);
2696 const ISEParams endpointISE = computeMaximumRangeISEParams(numBitsForEndpoints, numEndpointParts);
2697 const int endpointISERangeMax = computeISERangeMax(endpointISE);
2699 for (int endpointPartNdx0 = 0; endpointPartNdx0 < numEndpointParts; endpointPartNdx0++)
2700 for (int endpointPartNdx1 = endpointPartNdx0+1; endpointPartNdx1 < numEndpointParts; endpointPartNdx1++)
2702 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2703 const int numEndpointValues = de::min(10, endpointISERangeMax+1);
2705 for (int endpointValueNdx0 = 0; endpointValueNdx0 < numEndpointValues; endpointValueNdx0++)
2706 for (int endpointValueNdx1 = 0; endpointValueNdx1 < numEndpointValues; endpointValueNdx1++)
2708 const int endpointValue0 = endpointValueNdx0 * endpointISERangeMax / (numEndpointValues-1);
2709 const int endpointValue1 = endpointValueNdx1 * endpointISERangeMax / (numEndpointValues-1);
2711 iseInputs.endpoint.value.plain[endpointPartNdx0] = endpointValue0;
2712 iseInputs.endpoint.value.plain[endpointPartNdx1] = endpointValue1;
2714 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2723 case BLOCK_TEST_TYPE_ENDPOINT_ISE:
2724 // Similar to BLOCK_TEST_TYPE_WEIGHT_ISE, see above.
2726 static const deUint32 endpointRangeMaximums[] = { 5, 9, 11, 19, 23, 39, 47, 79, 95, 159, 191 };
2728 for (int endpointRangeNdx = 0; endpointRangeNdx < DE_LENGTH_OF_ARRAY(endpointRangeMaximums); endpointRangeNdx++)
2730 bool validCaseGenerated = false;
2732 for (int numPartitions = 1; !validCaseGenerated && numPartitions <= 4; numPartitions++)
2733 for (int isDual = 0; !validCaseGenerated && isDual <= 1; isDual++)
2734 for (int weightISEParamsNdx = 0; !validCaseGenerated && weightISEParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); weightISEParamsNdx++)
2735 for (int weightGridWidth = 2; !validCaseGenerated && weightGridWidth <= 12; weightGridWidth++)
2736 for (int weightGridHeight = 2; !validCaseGenerated && weightGridHeight <= 12; weightGridHeight++)
2738 NormalBlockParams blockParams;
2739 blockParams.weightGridWidth = weightGridWidth;
2740 blockParams.weightGridHeight = weightGridHeight;
2741 blockParams.weightISEParams = s_weightISEParamsCandidates[weightISEParamsNdx];
2742 blockParams.isDualPlane = isDual != 0;
2743 blockParams.ccs = 0;
2744 blockParams.numPartitions = numPartitions;
2745 blockParams.isMultiPartSingleCemMode = true;
2746 blockParams.colorEndpointModes[0] = 12;
2747 blockParams.partitionSeed = 634;
2749 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2751 const ISEParams endpointISEParams = computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams),
2752 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, true));
2754 if (computeISERangeMax(endpointISEParams) == endpointRangeMaximums[endpointRangeNdx])
2756 validCaseGenerated = true;
2758 const int numColorEndpoints = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, blockParams.isMultiPartSingleCemMode);
2759 const int numValuesInISEBlock = endpointISEParams.mode == ISEMODE_TRIT ? 5 : endpointISEParams.mode == ISEMODE_QUINT ? 3 : 1;
2762 const int numColorEndpointValues = (int)computeISERangeMax(endpointISEParams) + 1;
2763 const int numBlocks = deDivRoundUp32(numColorEndpointValues, numColorEndpoints);
2764 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2765 iseInputs.endpoint.isGivenInBlockForm = false;
2767 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2768 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2770 for (int endpointNdx = 0; endpointNdx < numColorEndpoints; endpointNdx++)
2771 iseInputs.endpoint.value.plain[endpointNdx] = (blockNdx*numColorEndpoints + endpointNdx + offset) % numColorEndpointValues;
2773 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2777 if (endpointISEParams.mode == ISEMODE_TRIT || endpointISEParams.mode == ISEMODE_QUINT)
2779 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2780 iseInputs.endpoint.isGivenInBlockForm = true;
2782 const int numTQValues = 1 << (endpointISEParams.mode == ISEMODE_TRIT ? 8 : 7);
2783 const int numISEBlocksPerBlock = deDivRoundUp32(numColorEndpoints, numValuesInISEBlock);
2784 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2786 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2787 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2789 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2791 for (int i = 0; i < numValuesInISEBlock; i++)
2792 iseInputs.endpoint.value.block[iseBlockNdx].bitValues[i] = 0;
2793 iseInputs.endpoint.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2796 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2803 DE_ASSERT(validCaseGenerated);
2809 case BLOCK_TEST_TYPE_CCS:
2810 // For all partition counts, test all values of the CCS (color component selector).
2812 for (int numPartitions = 1; numPartitions <= 3; numPartitions++)
2813 for (deUint32 ccs = 0; ccs < 4; ccs++)
2815 NormalBlockParams blockParams;
2816 blockParams.weightGridWidth = 3;
2817 blockParams.weightGridHeight = 3;
2818 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2819 blockParams.isDualPlane = true;
2820 blockParams.ccs = ccs;
2821 blockParams.numPartitions = numPartitions;
2822 blockParams.isMultiPartSingleCemMode = true;
2823 blockParams.colorEndpointModes[0] = 8;
2824 blockParams.partitionSeed = 634;
2826 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2832 case BLOCK_TEST_TYPE_RANDOM:
2833 // Generate a number of random (including invalid) blocks.
2835 const int numBlocks = 16384;
2836 const deUint32 seed = 1;
2838 dst.resize(numBlocks*BLOCK_SIZE_BYTES);
2840 generateRandomBlocks(&dst[0], numBlocks, format, seed);
2850 void generateRandomBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, deUint32 seed)
2852 const IVec3 blockSize = getBlockPixelSize(format);
2853 de::Random rnd (seed);
2854 size_t numBlocksGenerated = 0;
2856 DE_ASSERT(isAstcFormat(format));
2857 DE_ASSERT(blockSize.z() == 1);
2859 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2861 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2863 generateRandomBlock(curBlockPtr, blockSize, rnd);
2867 void generateRandomValidBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, TexDecompressionParams::AstcMode mode, deUint32 seed)
2869 const IVec3 blockSize = getBlockPixelSize(format);
2870 de::Random rnd (seed);
2871 size_t numBlocksGenerated = 0;
2873 DE_ASSERT(isAstcFormat(format));
2874 DE_ASSERT(blockSize.z() == 1);
2876 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2878 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2882 generateRandomBlock(curBlockPtr, blockSize, rnd);
2883 } while (!isValidBlock(curBlockPtr, format, mode));
2887 // Generate a number of trivial dummy blocks to fill unneeded space in a texture.
2888 void generateDummyVoidExtentBlocks (deUint8* dst, size_t numBlocks)
2890 AssignBlock128 block = generateVoidExtentBlock(VoidExtentParams(false, 0, 0, 0, 0));
2891 for (size_t ndx = 0; ndx < numBlocks; ndx++)
2892 block.assignToMemory(&dst[ndx * BLOCK_SIZE_BYTES]);
2895 void generateDummyNormalBlocks (deUint8* dst, size_t numBlocks, int blockWidth, int blockHeight)
2897 NormalBlockParams blockParams;
2899 blockParams.weightGridWidth = 3;
2900 blockParams.weightGridHeight = 3;
2901 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 5);
2902 blockParams.isDualPlane = false;
2903 blockParams.numPartitions = 1;
2904 blockParams.colorEndpointModes[0] = 8;
2906 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2907 iseInputs.weight.isGivenInBlockForm = false;
2909 const int numWeights = computeNumWeights(blockParams);
2910 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2912 for (size_t blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2914 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2915 iseInputs.weight.value.plain[weightNdx] = (deUint32)((blockNdx*numWeights + weightNdx) * weightRangeMax / (numBlocks*numWeights-1));
2917 generateNormalBlock(blockParams, blockWidth, blockHeight, iseInputs).assignToMemory(dst + blockNdx*BLOCK_SIZE_BYTES);
2921 bool isValidBlock (const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2923 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2924 const bool isSRGB = isAstcSRGBFormat(format);
2925 const bool isLDR = isSRGB || mode == TexDecompressionParams::ASTCMODE_LDR;
2927 // sRGB is not supported in HDR mode
2928 DE_ASSERT(!(mode == TexDecompressionParams::ASTCMODE_HDR && isSRGB));
2932 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2933 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2935 const Block128 blockData (data);
2936 const DecompressResult result = decompressBlock((isSRGB ? (void*)&tmpBuffer.sRGB[0] : (void*)&tmpBuffer.linear[0]),
2937 blockData, blockPixelSize.x(), blockPixelSize.y(), isSRGB, isLDR);
2939 return result == DECOMPRESS_RESULT_VALID_BLOCK;
2942 void decompress (const PixelBufferAccess& dst, const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2944 const bool isSRGBFormat = isAstcSRGBFormat(format);
2946 #if defined(DE_DEBUG)
2947 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2949 DE_ASSERT(dst.getWidth() == blockPixelSize.x() &&
2950 dst.getHeight() == blockPixelSize.y() &&
2951 dst.getDepth() == blockPixelSize.z());
2952 DE_ASSERT(mode == TexDecompressionParams::ASTCMODE_LDR || mode == TexDecompressionParams::ASTCMODE_HDR);
2955 // sRGB is not supported in HDR mode
2956 DE_ASSERT(!(mode == TexDecompressionParams::ASTCMODE_HDR && isSRGBFormat));
2958 decompress(dst, data, isSRGBFormat, isSRGBFormat || mode == TexDecompressionParams::ASTCMODE_LDR);
2961 const char* getBlockTestTypeName (BlockTestType testType)
2965 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "void_extent_ldr";
2966 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "void_extent_hdr";
2967 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "weight_grid";
2968 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "weight_ise";
2969 case BLOCK_TEST_TYPE_CEMS: return "color_endpoint_modes";
2970 case BLOCK_TEST_TYPE_PARTITION_SEED: return "partition_pattern_index";
2971 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "endpoint_value_ldr";
2972 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return "endpoint_value_hdr_cem_not_15";
2973 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "endpoint_value_hdr_cem_15";
2974 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "endpoint_ise";
2975 case BLOCK_TEST_TYPE_CCS: return "color_component_selector";
2976 case BLOCK_TEST_TYPE_RANDOM: return "random";
2983 const char* getBlockTestTypeDescription (BlockTestType testType)
2987 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "Test void extent block, LDR mode";
2988 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "Test void extent block, HDR mode";
2989 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "Test combinations of plane count, weight integer sequence encoding parameters, and weight grid size";
2990 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "Test different integer sequence encoding block values for weight grid";
2991 case BLOCK_TEST_TYPE_CEMS: return "Test different color endpoint mode combinations, combined with different plane and partition counts";
2992 case BLOCK_TEST_TYPE_PARTITION_SEED: return "Test different partition pattern indices";
2993 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "Test various combinations of each pair of color endpoint values, for each LDR color endpoint mode";
2994 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";
2995 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "Test various combinations of each pair of color endpoint values, HDR color endpoint mode 15";
2996 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "Test different integer sequence encoding block values for color endpoints";
2997 case BLOCK_TEST_TYPE_CCS: return "Test color component selector, for different partition counts";
2998 case BLOCK_TEST_TYPE_RANDOM: return "Random block test";
3005 bool isBlockTestTypeHDROnly (BlockTestType testType)
3007 return testType == BLOCK_TEST_TYPE_VOID_EXTENT_HDR ||
3008 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 ||
3009 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15;
3012 Vec4 getBlockTestTypeColorScale (BlockTestType testType)
3016 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f/65504.0f);
3017 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);
3018 case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return Vec4(1.0f/65504.0f);
3019 default: return Vec4(1.0f);
3023 Vec4 getBlockTestTypeColorBias (BlockTestType testType)
3027 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f);
3028 default: return Vec4(0.0f);