1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2016 The Android Open Source Project
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief ASTC Utilities.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuAstcUtil.hpp"
25 #include "deFloat16.h"
26 #include "deRandom.hpp"
27 #include "deMeta.hpp"
28
29 #include <algorithm>
30
31 namespace tcu
32 {
33 namespace astc
34 {
35
36 using std::vector;
37
38 namespace
39 {
40
41 // Common utilities
42
43 enum
44 {
45 MAX_BLOCK_WIDTH = 12,
46 MAX_BLOCK_HEIGHT = 12
47 };
48
getBit(deUint32 src,int ndx)49 inline deUint32 getBit (deUint32 src, int ndx)
50 {
51 DE_ASSERT(de::inBounds(ndx, 0, 32));
52 return (src >> ndx) & 1;
53 }
54
getBits(deUint32 src,int low,int high)55 inline deUint32 getBits (deUint32 src, int low, int high)
56 {
57 const int numBits = (high-low) + 1;
58
59 DE_ASSERT(de::inRange(numBits, 1, 32));
60
61 if (numBits < 32)
62 return (deUint32)((src >> low) & ((1u<<numBits)-1));
63 else
64 return (deUint32)((src >> low) & 0xFFFFFFFFu);
65 }
66
isBitSet(deUint32 src,int ndx)67 inline bool isBitSet (deUint32 src, int ndx)
68 {
69 return getBit(src, ndx) != 0;
70 }
71
reverseBits(deUint32 src,int numBits)72 inline deUint32 reverseBits (deUint32 src, int numBits)
73 {
74 DE_ASSERT(de::inRange(numBits, 0, 32));
75 deUint32 result = 0;
76 for (int i = 0; i < numBits; i++)
77 result |= ((src >> i) & 1) << (numBits-1-i);
78 return result;
79 }
80
bitReplicationScale(deUint32 src,int numSrcBits,int numDstBits)81 inline deUint32 bitReplicationScale (deUint32 src, int numSrcBits, int numDstBits)
82 {
83 DE_ASSERT(numSrcBits <= numDstBits);
84 DE_ASSERT((src & ((1<<numSrcBits)-1)) == src);
85 deUint32 dst = 0;
86 for (int shift = numDstBits-numSrcBits; shift > -numSrcBits; shift -= numSrcBits)
87 dst |= shift >= 0 ? src << shift : src >> -shift;
88 return dst;
89 }
90
signExtend(deInt32 src,int numSrcBits)91 inline deInt32 signExtend (deInt32 src, int numSrcBits)
92 {
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);
96 }
97
isFloat16InfOrNan(deFloat16 v)98 inline bool isFloat16InfOrNan (deFloat16 v)
99 {
100 return getBits(v, 10, 14) == 31;
101 }
102
103 enum ISEMode
104 {
105 ISEMODE_TRIT = 0,
106 ISEMODE_QUINT,
107 ISEMODE_PLAIN_BIT,
108
109 ISEMODE_LAST
110 };
111
112 struct ISEParams
113 {
114 ISEMode mode;
115 int numBits;
116
ISEParamstcu::astc::__anonba8e8f060111::ISEParams117 ISEParams (ISEMode mode_, int numBits_) : mode(mode_), numBits(numBits_) {}
118 };
119
computeNumRequiredBits(const ISEParams & iseParams,int numValues)120 inline int computeNumRequiredBits (const ISEParams& iseParams, int numValues)
121 {
122 switch (iseParams.mode)
123 {
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;
127 default:
128 DE_ASSERT(false);
129 return -1;
130 }
131 }
132
computeMaximumRangeISEParams(int numAvailableBits,int numValuesInSequence)133 ISEParams computeMaximumRangeISEParams (int numAvailableBits, int numValuesInSequence)
134 {
135 int curBitsForTritMode = 6;
136 int curBitsForQuintMode = 5;
137 int curBitsForPlainBitMode = 8;
138
139 while (true)
140 {
141 DE_ASSERT(curBitsForTritMode > 0 || curBitsForQuintMode > 0 || curBitsForPlainBitMode > 0);
142
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);
147
148 if (maxRange == tritRange)
149 {
150 const ISEParams params(ISEMODE_TRIT, curBitsForTritMode);
151 if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
152 return ISEParams(ISEMODE_TRIT, curBitsForTritMode);
153 curBitsForTritMode--;
154 }
155 else if (maxRange == quintRange)
156 {
157 const ISEParams params(ISEMODE_QUINT, curBitsForQuintMode);
158 if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
159 return ISEParams(ISEMODE_QUINT, curBitsForQuintMode);
160 curBitsForQuintMode--;
161 }
162 else
163 {
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--;
169 }
170 }
171 }
172
computeNumColorEndpointValues(deUint32 endpointMode)173 inline int computeNumColorEndpointValues (deUint32 endpointMode)
174 {
175 DE_ASSERT(endpointMode < 16);
176 return (endpointMode/4 + 1) * 2;
177 }
178
179 // Decompression utilities
180
181 enum DecompressResult
182 {
183 DECOMPRESS_RESULT_VALID_BLOCK = 0, //!< Decompressed valid block
184 DECOMPRESS_RESULT_ERROR, //!< Encountered error while decompressing, error color written
185
186 DECOMPRESS_RESULT_LAST
187 };
188
189 // A helper for getting bits from a 128-bit block.
190 class Block128
191 {
192 private:
193 typedef deUint64 Word;
194
195 enum
196 {
197 WORD_BYTES = sizeof(Word),
198 WORD_BITS = 8*WORD_BYTES,
199 NUM_WORDS = 128 / WORD_BITS
200 };
201
202 DE_STATIC_ASSERT(128 % WORD_BITS == 0);
203
204 public:
Block128(const deUint8 * src)205 Block128 (const deUint8* src)
206 {
207 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
208 {
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);
212 }
213 }
214
getBit(int ndx) const215 deUint32 getBit (int ndx) const
216 {
217 DE_ASSERT(de::inBounds(ndx, 0, 128));
218 return (m_words[ndx / WORD_BITS] >> (ndx % WORD_BITS)) & 1;
219 }
220
getBits(int low,int high) const221 deUint32 getBits (int low, int high) const
222 {
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));
226
227 if (high-low+1 == 0)
228 return 0;
229
230 const int word0Ndx = low / WORD_BITS;
231 const int word1Ndx = high / WORD_BITS;
232
233 // \note "foo << bar << 1" done instead of "foo << (bar+1)" to avoid overflow, i.e. shift amount being too big.
234
235 if (word0Ndx == word1Ndx)
236 return (deUint32)((m_words[word0Ndx] & ((((Word)1 << high%WORD_BITS << 1) - 1))) >> ((Word)low % WORD_BITS));
237 else
238 {
239 DE_ASSERT(word1Ndx == word0Ndx + 1);
240
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));
243 }
244 }
245
isBitSet(int ndx) const246 bool isBitSet (int ndx) const
247 {
248 DE_ASSERT(de::inBounds(ndx, 0, 128));
249 return getBit(ndx) != 0;
250 }
251
252 private:
253 Word m_words[NUM_WORDS];
254 };
255
256 // A helper for sequential access into a Block128.
257 class BitAccessStream
258 {
259 public:
BitAccessStream(const Block128 & src,int startNdxInSrc,int length,bool forward)260 BitAccessStream (const Block128& src, int startNdxInSrc, int length, bool forward)
261 : m_src (src)
262 , m_startNdxInSrc (startNdxInSrc)
263 , m_length (length)
264 , m_forward (forward)
265 , m_ndx (0)
266 {
267 }
268
269 // Get the next num bits. Bits at positions greater than or equal to m_length are zeros.
getNext(int num)270 deUint32 getNext (int num)
271 {
272 if (num == 0 || m_ndx >= m_length)
273 return 0;
274
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;
279
280 m_ndx += num;
281
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);
284 }
285
286 private:
287 const Block128& m_src;
288 const int m_startNdxInSrc;
289 const int m_length;
290 const bool m_forward;
291
292 int m_ndx;
293 };
294
295 struct ISEDecodedResult
296 {
297 deUint32 m;
298 deUint32 tq; //!< Trit or quint value, depending on ISE mode.
299 deUint32 v;
300 };
301
302 // Data from an ASTC block's "block mode" part (i.e. bits [0,10]).
303 struct ASTCBlockMode
304 {
305 bool isError;
306 // \note Following fields only relevant if !isError.
307 bool isVoidExtent;
308 // \note Following fields only relevant if !isVoidExtent.
309 bool isDualPlane;
310 int weightGridWidth;
311 int weightGridHeight;
312 ISEParams weightISEParams;
313
ASTCBlockModetcu::astc::__anonba8e8f060111::ASTCBlockMode314 ASTCBlockMode (void)
315 : isError (true)
316 , isVoidExtent (true)
317 , isDualPlane (true)
318 , weightGridWidth (-1)
319 , weightGridHeight (-1)
320 , weightISEParams (ISEMODE_LAST, -1)
321 {
322 }
323 };
324
computeNumWeights(const ASTCBlockMode & mode)325 inline int computeNumWeights (const ASTCBlockMode& mode)
326 {
327 return mode.weightGridWidth * mode.weightGridHeight * (mode.isDualPlane ? 2 : 1);
328 }
329
330 struct ColorEndpointPair
331 {
332 UVec4 e0;
333 UVec4 e1;
334 };
335
336 struct TexelWeightPair
337 {
338 deUint32 w[2];
339 };
340
getASTCBlockMode(deUint32 blockModeData)341 ASTCBlockMode getASTCBlockMode (deUint32 blockModeData)
342 {
343 ASTCBlockMode blockMode;
344 blockMode.isError = true; // \note Set to false later, if not error.
345
346 blockMode.isVoidExtent = getBits(blockModeData, 0, 8) == 0x1fc;
347
348 if (!blockMode.isVoidExtent)
349 {
350 if ((getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 6, 8) == 7) || getBits(blockModeData, 0, 3) == 0)
351 return blockMode; // Invalid ("reserved").
352
353 deUint32 r = (deUint32)-1; // \note Set in the following branches.
354
355 if (getBits(blockModeData, 0, 1) == 0)
356 {
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);
361
362 r = (r2 << 2) | (r1 << 1) | (r0 << 0);
363
364 if (i78 == 3)
365 {
366 const bool i5 = isBitSet(blockModeData, 5);
367 blockMode.weightGridWidth = i5 ? 10 : 6;
368 blockMode.weightGridHeight = i5 ? 6 : 10;
369 }
370 else
371 {
372 const deUint32 a = getBits(blockModeData, 5, 6);
373 switch (i78)
374 {
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);
379 }
380 }
381 }
382 else
383 {
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);
389
390 r = (r2 << 2) | (r1 << 1) | (r0 << 0);
391
392 if (i23 == 3)
393 {
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;
398 }
399 else
400 {
401 const deUint32 b = getBits(blockModeData, 7, 8);
402
403 switch (i23)
404 {
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);
409 }
410 }
411 }
412
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);
416
417 {
418 ISEMode& m = blockMode.weightISEParams.mode;
419 int& b = blockMode.weightISEParams.numBits;
420 m = ISEMODE_PLAIN_BIT;
421 b = 0;
422
423 if (h)
424 {
425 switch (r)
426 {
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);
434 }
435 }
436 else
437 {
438 switch (r)
439 {
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);
447 }
448 }
449 }
450 }
451
452 blockMode.isError = false;
453 return blockMode;
454 }
455
setASTCErrorColorBlock(void * dst,int blockWidth,int blockHeight,bool isSRGB)456 inline void setASTCErrorColorBlock (void* dst, int blockWidth, int blockHeight, bool isSRGB)
457 {
458 if (isSRGB)
459 {
460 deUint8* const dstU = (deUint8*)dst;
461
462 for (int i = 0; i < blockWidth*blockHeight; i++)
463 {
464 dstU[4*i + 0] = 0xff;
465 dstU[4*i + 1] = 0;
466 dstU[4*i + 2] = 0xff;
467 dstU[4*i + 3] = 0xff;
468 }
469 }
470 else
471 {
472 float* const dstF = (float*)dst;
473
474 for (int i = 0; i < blockWidth*blockHeight; i++)
475 {
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;
480 }
481 }
482 }
483
decodeVoidExtentBlock(void * dst,const Block128 & blockData,int blockWidth,int blockHeight,bool isSRGB,bool isLDRMode)484 DecompressResult decodeVoidExtentBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode)
485 {
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);
492
493 if ((isLDRMode && isHDRBlock) || (!allExtentsAllOnes && (minSExtent >= maxSExtent || minTExtent >= maxTExtent)))
494 {
495 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
496 return DECOMPRESS_RESULT_ERROR;
497 }
498
499 const deUint32 rgba[4] =
500 {
501 blockData.getBits(64, 79),
502 blockData.getBits(80, 95),
503 blockData.getBits(96, 111),
504 blockData.getBits(112, 127)
505 };
506
507 if (isSRGB)
508 {
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);
513 }
514 else
515 {
516 float* const dstF = (float*)dst;
517
518 if (isHDRBlock)
519 {
520 for (int c = 0; c < 4; c++)
521 {
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)");
524 }
525
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]);
529 }
530 else
531 {
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;
535 }
536 }
537
538 return DECOMPRESS_RESULT_VALID_BLOCK;
539 }
540
decodeColorEndpointModes(deUint32 * endpointModesDst,const Block128 & blockData,int numPartitions,int extraCemBitsStart)541 void decodeColorEndpointModes (deUint32* endpointModesDst, const Block128& blockData, int numPartitions, int extraCemBitsStart)
542 {
543 if (numPartitions == 1)
544 endpointModesDst[0] = blockData.getBits(13, 16);
545 else
546 {
547 const deUint32 highLevelSelector = blockData.getBits(23, 24);
548
549 if (highLevelSelector == 0)
550 {
551 const deUint32 mode = blockData.getBits(25, 28);
552 for (int i = 0; i < numPartitions; i++)
553 endpointModesDst[i] = mode;
554 }
555 else
556 {
557 for (int partNdx = 0; partNdx < numPartitions; partNdx++)
558 {
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);
564
565 endpointModesDst[partNdx] = (cemClass << 2) | (lowBit1 << 1) | lowBit0;
566 }
567 }
568 }
569 }
570
computeNumColorEndpointValues(const deUint32 * endpointModes,int numPartitions)571 int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions)
572 {
573 int result = 0;
574 for (int i = 0; i < numPartitions; i++)
575 result += computeNumColorEndpointValues(endpointModes[i]);
576 return result;
577 }
578
decodeISETritBlock(ISEDecodedResult * dst,int numValues,BitAccessStream & data,int numBits)579 void decodeISETritBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
580 {
581 DE_ASSERT(de::inRange(numValues, 1, 5));
582
583 deUint32 m[5];
584
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);
595
596 switch (numValues)
597 {
598 // \note Fall-throughs.
599 case 1: T23 = 0;
600 case 2: T4 = 0;
601 case 3: T56 = 0;
602 case 4: T7 = 0;
603 case 5: break;
604 default:
605 DE_ASSERT(false);
606 }
607
608 const deUint32 T = (T7 << 7) | (T56 << 5) | (T4 << 4) | (T23 << 2) | (T01 << 0);
609
610 static const deUint32 tritsFromT[256][5] =
611 {
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 }
628 };
629
630 const deUint32 (& trits)[5] = tritsFromT[T];
631
632 for (int i = 0; i < numValues; i++)
633 {
634 dst[i].m = m[i];
635 dst[i].tq = trits[i];
636 dst[i].v = (trits[i] << numBits) + m[i];
637 }
638 }
639
decodeISEQuintBlock(ISEDecodedResult * dst,int numValues,BitAccessStream & data,int numBits)640 void decodeISEQuintBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
641 {
642 DE_ASSERT(de::inRange(numValues, 1, 3));
643
644 deUint32 m[3];
645
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);
652
653 switch (numValues)
654 {
655 // \note Fall-throughs.
656 case 1: Q34 = 0;
657 case 2: Q56 = 0;
658 case 3: break;
659 default:
660 DE_ASSERT(false);
661 }
662
663 const deUint32 Q = (Q56 << 5) | (Q34 << 3) | (Q012 << 0);
664
665 static const deUint32 quintsFromQ[256][3] =
666 {
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 }
675 };
676
677 const deUint32 (& quints)[3] = quintsFromQ[Q];
678
679 for (int i = 0; i < numValues; i++)
680 {
681 dst[i].m = m[i];
682 dst[i].tq = quints[i];
683 dst[i].v = (quints[i] << numBits) + m[i];
684 }
685 }
686
decodeISEBitBlock(ISEDecodedResult * dst,BitAccessStream & data,int numBits)687 inline void decodeISEBitBlock (ISEDecodedResult* dst, BitAccessStream& data, int numBits)
688 {
689 dst[0].m = data.getNext(numBits);
690 dst[0].v = dst[0].m;
691 }
692
decodeISE(ISEDecodedResult * dst,int numValues,BitAccessStream & data,const ISEParams & params)693 void decodeISE (ISEDecodedResult* dst, int numValues, BitAccessStream& data, const ISEParams& params)
694 {
695 if (params.mode == ISEMODE_TRIT)
696 {
697 const int numBlocks = deDivRoundUp32(numValues, 5);
698 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
699 {
700 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
701 decodeISETritBlock(&dst[5*blockNdx], numValuesInBlock, data, params.numBits);
702 }
703 }
704 else if (params.mode == ISEMODE_QUINT)
705 {
706 const int numBlocks = deDivRoundUp32(numValues, 3);
707 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
708 {
709 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
710 decodeISEQuintBlock(&dst[3*blockNdx], numValuesInBlock, data, params.numBits);
711 }
712 }
713 else
714 {
715 DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
716 for (int i = 0; i < numValues; i++)
717 decodeISEBitBlock(&dst[i], data, params.numBits);
718 }
719 }
720
unquantizeColorEndpoints(deUint32 * dst,const ISEDecodedResult * iseResults,int numEndpoints,const ISEParams & iseParams)721 void unquantizeColorEndpoints (deUint32* dst, const ISEDecodedResult* iseResults, int numEndpoints, const ISEParams& iseParams)
722 {
723 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
724 {
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];
729
730 for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
731 {
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);
738
739 const deUint32 A = a == 0 ? 0 : (1<<9)-1;
740 const deUint32 B = rangeCase == 0 ? 0
741 : rangeCase == 1 ? 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)
751 : (deUint32)-1;
752 DE_ASSERT(B != (deUint32)-1);
753
754 dst[endpointNdx] = (((iseResults[endpointNdx].tq*C + B) ^ A) >> 2) | (A & 0x80);
755 }
756 }
757 else
758 {
759 DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
760
761 for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
762 dst[endpointNdx] = bitReplicationScale(iseResults[endpointNdx].v, iseParams.numBits, 8);
763 }
764 }
765
bitTransferSigned(deInt32 & a,deInt32 & b)766 inline void bitTransferSigned (deInt32& a, deInt32& b)
767 {
768 b >>= 1;
769 b |= a & 0x80;
770 a >>= 1;
771 a &= 0x3f;
772 if (isBitSet(a, 5))
773 a -= 0x40;
774 }
775
clampedRGBA(const IVec4 & rgba)776 inline UVec4 clampedRGBA (const IVec4& rgba)
777 {
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));
782 }
783
blueContract(int r,int g,int b,int a)784 inline IVec4 blueContract (int r, int g, int b, int a)
785 {
786 return IVec4((r+b)>>1, (g+b)>>1, b, a);
787 }
788
isColorEndpointModeHDR(deUint32 mode)789 inline bool isColorEndpointModeHDR (deUint32 mode)
790 {
791 return mode == 2 ||
792 mode == 3 ||
793 mode == 7 ||
794 mode == 11 ||
795 mode == 14 ||
796 mode == 15;
797 }
798
decodeHDREndpointMode7(UVec4 & e0,UVec4 & e1,deUint32 v0,deUint32 v1,deUint32 v2,deUint32 v3)799 void decodeHDREndpointMode7 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3)
800 {
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
804 : m23 != 3 ? m23
805 : 0;
806 const deUint32 mode = m10 != 3 ? m23
807 : m23 != 3 ? 4
808 : 5;
809
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);
814
815 {
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)
818
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);
826
827 deInt32& R = red;
828 deInt32& G = green;
829 deInt32& B = blue;
830 deInt32& S = scale;
831
832 switch (mode)
833 {
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;
840 default:
841 DE_ASSERT(false);
842 }
843
844 #undef ASSIGN_X_BITS
845 #undef SHOR
846 }
847
848 static const int shiftAmounts[] = { 1, 1, 2, 3, 4, 5 };
849 DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(shiftAmounts));
850
851 red <<= shiftAmounts[mode];
852 green <<= shiftAmounts[mode];
853 blue <<= shiftAmounts[mode];
854 scale <<= shiftAmounts[mode];
855
856 if (mode != 5)
857 {
858 green = red - green;
859 blue = red - blue;
860 }
861
862 if (majComp == 1)
863 std::swap(red, green);
864 else if (majComp == 2)
865 std::swap(red, blue);
866
867 e0 = UVec4(de::clamp(red - scale, 0, 0xfff),
868 de::clamp(green - scale, 0, 0xfff),
869 de::clamp(blue - scale, 0, 0xfff),
870 0x780);
871
872 e1 = UVec4(de::clamp(red, 0, 0xfff),
873 de::clamp(green, 0, 0xfff),
874 de::clamp(blue, 0, 0xfff),
875 0x780);
876 }
877
decodeHDREndpointMode11(UVec4 & e0,UVec4 & e1,deUint32 v0,deUint32 v1,deUint32 v2,deUint32 v3,deUint32 v4,deUint32 v5)878 void decodeHDREndpointMode11 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5)
879 {
880 const deUint32 major = (getBit(v5, 7) << 1) | getBit(v4, 7);
881
882 if (major == 3)
883 {
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);
886 }
887 else
888 {
889 const deUint32 mode = (getBit(v3, 7) << 2) | (getBit(v2, 7) << 1) | getBit(v1, 7);
890
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));
897
898 {
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)
901
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);
908
909 switch (mode)
910 {
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;
919 default:
920 DE_ASSERT(false);
921 }
922
923 #undef ASSIGN_X_BITS
924 #undef SHOR
925 }
926
927 static const int numDBits[] = { 7, 6, 7, 6, 5, 6, 5, 6 };
928 DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(numDBits));
929
930 d0 = signExtend(d0, numDBits[mode]);
931 d1 = signExtend(d1, numDBits[mode]);
932
933 const int shiftAmount = (mode >> 1) ^ 3;
934 a <<= shiftAmount;
935 c <<= shiftAmount;
936 b0 <<= shiftAmount;
937 b1 <<= shiftAmount;
938 d0 <<= shiftAmount;
939 d1 <<= shiftAmount;
940
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),
944 0x780);
945
946 e1 = UVec4(de::clamp(a, 0, 0xfff),
947 de::clamp(a-b0, 0, 0xfff),
948 de::clamp(a-b1, 0, 0xfff),
949 0x780);
950
951 if (major == 1)
952 {
953 std::swap(e0.x(), e0.y());
954 std::swap(e1.x(), e1.y());
955 }
956 else if (major == 2)
957 {
958 std::swap(e0.x(), e0.z());
959 std::swap(e1.x(), e1.z());
960 }
961 }
962 }
963
decodeHDREndpointMode15(UVec4 & e0,UVec4 & e1,deUint32 v0,deUint32 v1,deUint32 v2,deUint32 v3,deUint32 v4,deUint32 v5,deUint32 v6In,deUint32 v7In)964 void decodeHDREndpointMode15(UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5, deUint32 v6In, deUint32 v7In)
965 {
966 decodeHDREndpointMode11(e0, e1, v0, v1, v2, v3, v4, v5);
967
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);
971
972 if (mode == 3)
973 {
974 e0.w() = v6 << 5;
975 e1.w() = v7 << 5;
976 }
977 else
978 {
979 v6 |= (v7 << (mode+1)) & 0x780;
980 v7 &= (0x3f >> mode);
981 v7 ^= 0x20 >> mode;
982 v7 -= 0x20 >> mode;
983 v6 <<= 4-mode;
984 v7 <<= 4-mode;
985
986 v7 += v6;
987 v7 = de::clamp(v7, 0, 0xfff);
988 e0.w() = v6;
989 e1.w() = v7;
990 }
991 }
992
decodeColorEndpoints(ColorEndpointPair * dst,const deUint32 * unquantizedEndpoints,const deUint32 * endpointModes,int numPartitions)993 void decodeColorEndpoints (ColorEndpointPair* dst, const deUint32* unquantizedEndpoints, const deUint32* endpointModes, int numPartitions)
994 {
995 int unquantizedNdx = 0;
996
997 for (int partitionNdx = 0; partitionNdx < numPartitions; partitionNdx++)
998 {
999 const deUint32 endpointMode = endpointModes[partitionNdx];
1000 const deUint32* v = &unquantizedEndpoints[unquantizedNdx];
1001 UVec4& e0 = dst[partitionNdx].e0;
1002 UVec4& e1 = dst[partitionNdx].e1;
1003
1004 unquantizedNdx += computeNumColorEndpointValues(endpointMode);
1005
1006 switch (endpointMode)
1007 {
1008 case 0:
1009 e0 = UVec4(v[0], v[0], v[0], 0xff);
1010 e1 = UVec4(v[1], v[1], v[1], 0xff);
1011 break;
1012
1013 case 1:
1014 {
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);
1019 break;
1020 }
1021
1022 case 2:
1023 {
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;
1027
1028 e0 = UVec4(y0, y0, y0, 0x780);
1029 e1 = UVec4(y1, y1, y1, 0x780);
1030 break;
1031 }
1032
1033 case 3:
1034 {
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);
1041
1042 e0 = UVec4(y0, y0, y0, 0x780);
1043 e1 = UVec4(y1, y1, y1, 0x780);
1044 break;
1045 }
1046
1047 case 4:
1048 e0 = UVec4(v[0], v[0], v[0], v[2]);
1049 e1 = UVec4(v[1], v[1], v[1], v[3]);
1050 break;
1051
1052 case 5:
1053 {
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);
1060
1061 e0 = clampedRGBA(IVec4(v0, v0, v0, v2));
1062 e1 = clampedRGBA(IVec4(v0+v1, v0+v1, v0+v1, v2+v3));
1063 break;
1064 }
1065
1066 case 6:
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);
1069 break;
1070
1071 case 7:
1072 decodeHDREndpointMode7(e0, e1, v[0], v[1], v[2], v[3]);
1073 break;
1074
1075 case 8:
1076 if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
1077 {
1078 e0 = UVec4(v[0], v[2], v[4], 0xff);
1079 e1 = UVec4(v[1], v[3], v[5], 0xff);
1080 }
1081 else
1082 {
1083 e0 = blueContract(v[1], v[3], v[5], 0xff).asUint();
1084 e1 = blueContract(v[0], v[2], v[4], 0xff).asUint();
1085 }
1086 break;
1087
1088 case 9:
1089 {
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);
1099
1100 if (v1+v3+v5 >= 0)
1101 {
1102 e0 = clampedRGBA(IVec4(v0, v2, v4, 0xff));
1103 e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, 0xff));
1104 }
1105 else
1106 {
1107 e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, 0xff));
1108 e1 = clampedRGBA(blueContract(v0, v2, v4, 0xff));
1109 }
1110 break;
1111 }
1112
1113 case 10:
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]);
1116 break;
1117
1118 case 11:
1119 decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
1120 break;
1121
1122 case 12:
1123 if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
1124 {
1125 e0 = UVec4(v[0], v[2], v[4], v[6]);
1126 e1 = UVec4(v[1], v[3], v[5], v[7]);
1127 }
1128 else
1129 {
1130 e0 = clampedRGBA(blueContract(v[1], v[3], v[5], v[7]));
1131 e1 = clampedRGBA(blueContract(v[0], v[2], v[4], v[6]));
1132 }
1133 break;
1134
1135 case 13:
1136 {
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);
1149
1150 if (v1+v3+v5 >= 0)
1151 {
1152 e0 = clampedRGBA(IVec4(v0, v2, v4, v6));
1153 e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, v6+v7));
1154 }
1155 else
1156 {
1157 e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, v6+v7));
1158 e1 = clampedRGBA(blueContract(v0, v2, v4, v6));
1159 }
1160
1161 break;
1162 }
1163
1164 case 14:
1165 decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
1166 e0.w() = v[6];
1167 e1.w() = v[7];
1168 break;
1169
1170 case 15:
1171 decodeHDREndpointMode15(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
1172 break;
1173
1174 default:
1175 DE_ASSERT(false);
1176 }
1177 }
1178 }
1179
computeColorEndpoints(ColorEndpointPair * dst,const Block128 & blockData,const deUint32 * endpointModes,int numPartitions,int numColorEndpointValues,const ISEParams & iseParams,int numBitsAvailable)1180 void computeColorEndpoints (ColorEndpointPair* dst, const Block128& blockData, const deUint32* endpointModes, int numPartitions, int numColorEndpointValues, const ISEParams& iseParams, int numBitsAvailable)
1181 {
1182 const int colorEndpointDataStart = numPartitions == 1 ? 17 : 29;
1183 ISEDecodedResult colorEndpointData[18];
1184
1185 {
1186 BitAccessStream dataStream(blockData, colorEndpointDataStart, numBitsAvailable, true);
1187 decodeISE(&colorEndpointData[0], numColorEndpointValues, dataStream, iseParams);
1188 }
1189
1190 {
1191 deUint32 unquantizedEndpoints[18];
1192 unquantizeColorEndpoints(&unquantizedEndpoints[0], &colorEndpointData[0], numColorEndpointValues, iseParams);
1193 decodeColorEndpoints(dst, &unquantizedEndpoints[0], &endpointModes[0], numPartitions);
1194 }
1195 }
1196
unquantizeWeights(deUint32 dst[64],const ISEDecodedResult * weightGrid,const ASTCBlockMode & blockMode)1197 void unquantizeWeights (deUint32 dst[64], const ISEDecodedResult* weightGrid, const ASTCBlockMode& blockMode)
1198 {
1199 const int numWeights = computeNumWeights(blockMode);
1200 const ISEParams& iseParams = blockMode.weightISEParams;
1201
1202 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
1203 {
1204 const int rangeCase = iseParams.numBits*2 + (iseParams.mode == ISEMODE_QUINT ? 1 : 0);
1205
1206 if (rangeCase == 0 || rangeCase == 1)
1207 {
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++)
1212 {
1213 DE_ASSERT(weightGrid[i].v < (rangeCase == 0 ? 3u : 5u));
1214 dst[i] = map[weightGrid[i].v];
1215 }
1216 }
1217 else
1218 {
1219 DE_ASSERT(rangeCase <= 6);
1220 static const deUint32 Ca[5] = { 50, 28, 23, 13, 11 };
1221 const deUint32 C = Ca[rangeCase-2];
1222
1223 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1224 {
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);
1228
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)
1235 : (deUint32)-1;
1236
1237 dst[weightNdx] = (((weightGrid[weightNdx].tq*C + B) ^ A) >> 2) | (A & 0x20);
1238 }
1239 }
1240 }
1241 else
1242 {
1243 DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);
1244
1245 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1246 dst[weightNdx] = bitReplicationScale(weightGrid[weightNdx].v, iseParams.numBits, 6);
1247 }
1248
1249 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
1250 dst[weightNdx] += dst[weightNdx] > 32 ? 1 : 0;
1251
1252 // Initialize nonexistent weights to poison values
1253 for (int weightNdx = numWeights; weightNdx < 64; weightNdx++)
1254 dst[weightNdx] = ~0u;
1255
1256 }
1257
interpolateWeights(TexelWeightPair * dst,const deUint32 * unquantizedWeights,int blockWidth,int blockHeight,const ASTCBlockMode & blockMode)1258 void interpolateWeights (TexelWeightPair* dst, const deUint32* unquantizedWeights, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
1259 {
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);
1263
1264 for (int texelY = 0; texelY < blockHeight; texelY++)
1265 {
1266 for (int texelX = 0; texelX < blockWidth; texelX++)
1267 {
1268 const deUint32 gX = (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;
1269 const deUint32 gY = (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;
1270 const deUint32 jX = gX >> 4;
1271 const deUint32 jY = gY >> 4;
1272 const deUint32 fX = gX & 0xf;
1273 const deUint32 fY = gY & 0xf;
1274 const deUint32 w11 = (fX*fY + 8) >> 4;
1275 const deUint32 w10 = fY - w11;
1276 const deUint32 w01 = fX - w11;
1277 const deUint32 w00 = 16 - fX - fY + w11;
1278 const deUint32 v0 = jY*blockMode.weightGridWidth + jX;
1279
1280 for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)
1281 {
1282 const deUint32 p00 = unquantizedWeights[(v0) * numWeightsPerTexel + texelWeightNdx];
1283 const deUint32 p01 = unquantizedWeights[(v0 + 1) * numWeightsPerTexel + texelWeightNdx];
1284 const deUint32 p10 = unquantizedWeights[(v0 + blockMode.weightGridWidth) * numWeightsPerTexel + texelWeightNdx];
1285 const deUint32 p11 = unquantizedWeights[(v0 + blockMode.weightGridWidth + 1) * numWeightsPerTexel + texelWeightNdx];
1286
1287 dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;
1288 }
1289 }
1290 }
1291 }
1292
computeTexelWeights(TexelWeightPair * dst,const Block128 & blockData,int blockWidth,int blockHeight,const ASTCBlockMode & blockMode)1293 void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
1294 {
1295 ISEDecodedResult weightGrid[64];
1296
1297 {
1298 BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);
1299 decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);
1300 }
1301
1302 {
1303 deUint32 unquantizedWeights[64];
1304 unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);
1305 interpolateWeights(dst, &unquantizedWeights[0], blockWidth, blockHeight, blockMode);
1306 }
1307 }
1308
hash52(deUint32 v)1309 inline deUint32 hash52 (deUint32 v)
1310 {
1311 deUint32 p = v;
1312 p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
1313 p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
1314 p ^= p << 6; p ^= p >> 17;
1315 return p;
1316 }
1317
computeTexelPartition(deUint32 seedIn,deUint32 xIn,deUint32 yIn,deUint32 zIn,int numPartitions,bool smallBlock)1318 int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)
1319 {
1320 DE_ASSERT(zIn == 0);
1321 const deUint32 x = smallBlock ? xIn << 1 : xIn;
1322 const deUint32 y = smallBlock ? yIn << 1 : yIn;
1323 const deUint32 z = smallBlock ? zIn << 1 : zIn;
1324 const deUint32 seed = seedIn + 1024*(numPartitions-1);
1325 const deUint32 rnum = hash52(seed);
1326 deUint8 seed1 = (deUint8)( rnum & 0xf);
1327 deUint8 seed2 = (deUint8)((rnum >> 4) & 0xf);
1328 deUint8 seed3 = (deUint8)((rnum >> 8) & 0xf);
1329 deUint8 seed4 = (deUint8)((rnum >> 12) & 0xf);
1330 deUint8 seed5 = (deUint8)((rnum >> 16) & 0xf);
1331 deUint8 seed6 = (deUint8)((rnum >> 20) & 0xf);
1332 deUint8 seed7 = (deUint8)((rnum >> 24) & 0xf);
1333 deUint8 seed8 = (deUint8)((rnum >> 28) & 0xf);
1334 deUint8 seed9 = (deUint8)((rnum >> 18) & 0xf);
1335 deUint8 seed10 = (deUint8)((rnum >> 22) & 0xf);
1336 deUint8 seed11 = (deUint8)((rnum >> 26) & 0xf);
1337 deUint8 seed12 = (deUint8)(((rnum >> 30) | (rnum << 2)) & 0xf);
1338
1339 seed1 = (deUint8)(seed1 * seed1 );
1340 seed2 = (deUint8)(seed2 * seed2 );
1341 seed3 = (deUint8)(seed3 * seed3 );
1342 seed4 = (deUint8)(seed4 * seed4 );
1343 seed5 = (deUint8)(seed5 * seed5 );
1344 seed6 = (deUint8)(seed6 * seed6 );
1345 seed7 = (deUint8)(seed7 * seed7 );
1346 seed8 = (deUint8)(seed8 * seed8 );
1347 seed9 = (deUint8)(seed9 * seed9 );
1348 seed10 = (deUint8)(seed10 * seed10);
1349 seed11 = (deUint8)(seed11 * seed11);
1350 seed12 = (deUint8)(seed12 * seed12);
1351
1352 const int shA = (seed & 2) != 0 ? 4 : 5;
1353 const int shB = numPartitions == 3 ? 6 : 5;
1354 const int sh1 = (seed & 1) != 0 ? shA : shB;
1355 const int sh2 = (seed & 1) != 0 ? shB : shA;
1356 const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;
1357
1358 seed1 = (deUint8)(seed1 >> sh1);
1359 seed2 = (deUint8)(seed2 >> sh2);
1360 seed3 = (deUint8)(seed3 >> sh1);
1361 seed4 = (deUint8)(seed4 >> sh2);
1362 seed5 = (deUint8)(seed5 >> sh1);
1363 seed6 = (deUint8)(seed6 >> sh2);
1364 seed7 = (deUint8)(seed7 >> sh1);
1365 seed8 = (deUint8)(seed8 >> sh2);
1366 seed9 = (deUint8)(seed9 >> sh3);
1367 seed10 = (deUint8)(seed10 >> sh3);
1368 seed11 = (deUint8)(seed11 >> sh3);
1369 seed12 = (deUint8)(seed12 >> sh3);
1370
1371 const int a = 0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));
1372 const int b = 0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));
1373 const int c = numPartitions >= 3 ? 0x3f & (seed5*x + seed6*y + seed9*z + (rnum >> 6)) : 0;
1374 const int d = numPartitions >= 4 ? 0x3f & (seed7*x + seed8*y + seed10*z + (rnum >> 2)) : 0;
1375
1376 return a >= b && a >= c && a >= d ? 0
1377 : b >= c && b >= d ? 1
1378 : c >= d ? 2
1379 : 3;
1380 }
1381
setTexelColors(void * dst,ColorEndpointPair * colorEndpoints,TexelWeightPair * texelWeights,int ccs,deUint32 partitionIndexSeed,int numPartitions,int blockWidth,int blockHeight,bool isSRGB,bool isLDRMode,const deUint32 * colorEndpointModes)1382 DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,
1383 int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)
1384 {
1385 const bool smallBlock = blockWidth*blockHeight < 31;
1386 DecompressResult result = DECOMPRESS_RESULT_VALID_BLOCK;
1387 bool isHDREndpoint[4];
1388
1389 for (int i = 0; i < numPartitions; i++)
1390 isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);
1391
1392 for (int texelY = 0; texelY < blockHeight; texelY++)
1393 for (int texelX = 0; texelX < blockWidth; texelX++)
1394 {
1395 const int texelNdx = texelY*blockWidth + texelX;
1396 const int colorEndpointNdx = numPartitions == 1 ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);
1397 DE_ASSERT(colorEndpointNdx < numPartitions);
1398 const UVec4& e0 = colorEndpoints[colorEndpointNdx].e0;
1399 const UVec4& e1 = colorEndpoints[colorEndpointNdx].e1;
1400 const TexelWeightPair& weight = texelWeights[texelNdx];
1401
1402 if (isLDRMode && isHDREndpoint[colorEndpointNdx])
1403 {
1404 if (isSRGB)
1405 {
1406 ((deUint8*)dst)[texelNdx*4 + 0] = 0xff;
1407 ((deUint8*)dst)[texelNdx*4 + 1] = 0;
1408 ((deUint8*)dst)[texelNdx*4 + 2] = 0xff;
1409 ((deUint8*)dst)[texelNdx*4 + 3] = 0xff;
1410 }
1411 else
1412 {
1413 ((float*)dst)[texelNdx*4 + 0] = 1.0f;
1414 ((float*)dst)[texelNdx*4 + 1] = 0;
1415 ((float*)dst)[texelNdx*4 + 2] = 1.0f;
1416 ((float*)dst)[texelNdx*4 + 3] = 1.0f;
1417 }
1418
1419 result = DECOMPRESS_RESULT_ERROR;
1420 }
1421 else
1422 {
1423 for (int channelNdx = 0; channelNdx < 4; channelNdx++)
1424 {
1425 if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.
1426 {
1427 const deUint32 c0 = (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);
1428 const deUint32 c1 = (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);
1429 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1430 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1431
1432 if (isSRGB)
1433 ((deUint8*)dst)[texelNdx*4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);
1434 else
1435 ((float*)dst)[texelNdx*4 + channelNdx] = c == 65535 ? 1.0f : (float)c / 65536.0f;
1436 }
1437 else
1438 {
1439 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1440 const deUint32 c0 = e0[channelNdx] << 4;
1441 const deUint32 c1 = e1[channelNdx] << 4;
1442 const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];
1443 const deUint32 c = (c0*(64-w) + c1*w + 32) / 64;
1444 const deUint32 e = getBits(c, 11, 15);
1445 const deUint32 m = getBits(c, 0, 10);
1446 const deUint32 mt = m < 512 ? 3*m
1447 : m >= 1536 ? 5*m - 2048
1448 : 4*m - 512;
1449 const deFloat16 cf = (deFloat16)((e << 10) + (mt >> 3));
1450
1451 ((float*)dst)[texelNdx*4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);
1452 }
1453 }
1454 }
1455 }
1456
1457 return result;
1458 }
1459
decompressBlock(void * dst,const Block128 & blockData,int blockWidth,int blockHeight,bool isSRGB,bool isLDR)1460 DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)
1461 {
1462 DE_ASSERT(isLDR || !isSRGB);
1463
1464 // Decode block mode.
1465
1466 const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));
1467
1468 // Check for block mode errors.
1469
1470 if (blockMode.isError)
1471 {
1472 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1473 return DECOMPRESS_RESULT_ERROR;
1474 }
1475
1476 // Separate path for void-extent.
1477
1478 if (blockMode.isVoidExtent)
1479 return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);
1480
1481 // Compute weight grid values.
1482
1483 const int numWeights = computeNumWeights(blockMode);
1484 const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);
1485 const int numPartitions = (int)blockData.getBits(11, 12) + 1;
1486
1487 // Check for errors in weight grid, partition and dual-plane parameters.
1488
1489 if (numWeights > 64 ||
1490 numWeightDataBits > 96 ||
1491 numWeightDataBits < 24 ||
1492 blockMode.weightGridWidth > blockWidth ||
1493 blockMode.weightGridHeight > blockHeight ||
1494 (numPartitions == 4 && blockMode.isDualPlane))
1495 {
1496 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1497 return DECOMPRESS_RESULT_ERROR;
1498 }
1499
1500 // Compute number of bits available for color endpoint data.
1501
1502 const bool isSingleUniqueCem = numPartitions == 1 || blockData.getBits(23, 24) == 0;
1503 const int numConfigDataBits = (numPartitions == 1 ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +
1504 (blockMode.isDualPlane ? 2 : 0);
1505 const int numBitsForColorEndpoints = 128 - numWeightDataBits - numConfigDataBits;
1506 const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -1
1507 : numPartitions == 4 ? 7
1508 : numPartitions == 3 ? 4
1509 : numPartitions == 2 ? 1
1510 : 0);
1511 // Decode color endpoint modes.
1512
1513 deUint32 colorEndpointModes[4];
1514 decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);
1515
1516 const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);
1517
1518 // Check for errors in color endpoint value count.
1519
1520 if (numColorEndpointValues > 18 || numBitsForColorEndpoints < deDivRoundUp32(13*numColorEndpointValues, 5))
1521 {
1522 setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
1523 return DECOMPRESS_RESULT_ERROR;
1524 }
1525
1526 // Compute color endpoints.
1527
1528 ColorEndpointPair colorEndpoints[4];
1529 computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,
1530 computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);
1531
1532 // Compute texel weights.
1533
1534 TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];
1535 computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);
1536
1537 // Set texel colors.
1538
1539 const int ccs = blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;
1540 const deUint32 partitionIndexSeed = numPartitions > 1 ? blockData.getBits(13, 22) : (deUint32)-1;
1541
1542 return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);
1543 }
1544
decompress(const PixelBufferAccess & dst,const deUint8 * data,bool isSRGB,bool isLDR)1545 void decompress (const PixelBufferAccess& dst, const deUint8* data, bool isSRGB, bool isLDR)
1546 {
1547 DE_ASSERT(isLDR || !isSRGB);
1548
1549 const int blockWidth = dst.getWidth();
1550 const int blockHeight = dst.getHeight();
1551
1552 union
1553 {
1554 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1555 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
1556 } decompressedBuffer;
1557
1558 const Block128 blockData(data);
1559 decompressBlock(isSRGB ? (void*)&decompressedBuffer.sRGB[0] : (void*)&decompressedBuffer.linear[0],
1560 blockData, dst.getWidth(), dst.getHeight(), isSRGB, isLDR);
1561
1562 if (isSRGB)
1563 {
1564 for (int i = 0; i < blockHeight; i++)
1565 for (int j = 0; j < blockWidth; j++)
1566 {
1567 dst.setPixel(IVec4(decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 0],
1568 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 1],
1569 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 2],
1570 decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 3]), j, i);
1571 }
1572 }
1573 else
1574 {
1575 for (int i = 0; i < blockHeight; i++)
1576 for (int j = 0; j < blockWidth; j++)
1577 {
1578 dst.setPixel(Vec4(decompressedBuffer.linear[(i*blockWidth + j) * 4 + 0],
1579 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 1],
1580 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 2],
1581 decompressedBuffer.linear[(i*blockWidth + j) * 4 + 3]), j, i);
1582 }
1583 }
1584 }
1585
1586 // Helper class for setting bits in a 128-bit block.
1587 class AssignBlock128
1588 {
1589 private:
1590 typedef deUint64 Word;
1591
1592 enum
1593 {
1594 WORD_BYTES = sizeof(Word),
1595 WORD_BITS = 8*WORD_BYTES,
1596 NUM_WORDS = 128 / WORD_BITS
1597 };
1598
1599 DE_STATIC_ASSERT(128 % WORD_BITS == 0);
1600
1601 public:
AssignBlock128(void)1602 AssignBlock128 (void)
1603 {
1604 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1605 m_words[wordNdx] = 0;
1606 }
1607
setBit(int ndx,deUint32 val)1608 void setBit (int ndx, deUint32 val)
1609 {
1610 DE_ASSERT(de::inBounds(ndx, 0, 128));
1611 DE_ASSERT((val & 1) == val);
1612 const int wordNdx = ndx / WORD_BITS;
1613 const int bitNdx = ndx % WORD_BITS;
1614 m_words[wordNdx] = (m_words[wordNdx] & ~((Word)1 << bitNdx)) | ((Word)val << bitNdx);
1615 }
1616
setBits(int low,int high,deUint32 bits)1617 void setBits (int low, int high, deUint32 bits)
1618 {
1619 DE_ASSERT(de::inBounds(low, 0, 128));
1620 DE_ASSERT(de::inBounds(high, 0, 128));
1621 DE_ASSERT(de::inRange(high-low+1, 0, 32));
1622 DE_ASSERT((bits & (((Word)1 << (high-low+1)) - 1)) == bits);
1623
1624 if (high-low+1 == 0)
1625 return;
1626
1627 const int word0Ndx = low / WORD_BITS;
1628 const int word1Ndx = high / WORD_BITS;
1629 const int lowNdxInW0 = low % WORD_BITS;
1630
1631 if (word0Ndx == word1Ndx)
1632 m_words[word0Ndx] = (m_words[word0Ndx] & ~((((Word)1 << (high-low+1)) - 1) << lowNdxInW0)) | ((Word)bits << lowNdxInW0);
1633 else
1634 {
1635 DE_ASSERT(word1Ndx == word0Ndx + 1);
1636
1637 const int highNdxInW1 = high % WORD_BITS;
1638 const int numBitsToSetInW0 = WORD_BITS - lowNdxInW0;
1639 const Word bitsLowMask = ((Word)1 << numBitsToSetInW0) - 1;
1640
1641 m_words[word0Ndx] = (m_words[word0Ndx] & (((Word)1 << lowNdxInW0) - 1)) | (((Word)bits & bitsLowMask) << lowNdxInW0);
1642 m_words[word1Ndx] = (m_words[word1Ndx] & ~(((Word)1 << (highNdxInW1+1)) - 1)) | (((Word)bits & ~bitsLowMask) >> numBitsToSetInW0);
1643 }
1644 }
1645
assignToMemory(deUint8 * dst) const1646 void assignToMemory (deUint8* dst) const
1647 {
1648 for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
1649 {
1650 for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
1651 dst[wordNdx*WORD_BYTES + byteNdx] = (deUint8)((m_words[wordNdx] >> (8*byteNdx)) & 0xff);
1652 }
1653 }
1654
pushBytesToVector(vector<deUint8> & dst) const1655 void pushBytesToVector (vector<deUint8>& dst) const
1656 {
1657 const int assignStartIndex = (int)dst.size();
1658 dst.resize(dst.size() + BLOCK_SIZE_BYTES);
1659 assignToMemory(&dst[assignStartIndex]);
1660 }
1661
1662 private:
1663 Word m_words[NUM_WORDS];
1664 };
1665
1666 // A helper for sequential access into a AssignBlock128.
1667 class BitAssignAccessStream
1668 {
1669 public:
BitAssignAccessStream(AssignBlock128 & dst,int startNdxInSrc,int length,bool forward)1670 BitAssignAccessStream (AssignBlock128& dst, int startNdxInSrc, int length, bool forward)
1671 : m_dst (dst)
1672 , m_startNdxInSrc (startNdxInSrc)
1673 , m_length (length)
1674 , m_forward (forward)
1675 , m_ndx (0)
1676 {
1677 }
1678
1679 // Set the next num bits. Bits at positions greater than or equal to m_length are not touched.
setNext(int num,deUint32 bits)1680 void setNext (int num, deUint32 bits)
1681 {
1682 DE_ASSERT((bits & (((deUint64)1 << num) - 1)) == bits);
1683
1684 if (num == 0 || m_ndx >= m_length)
1685 return;
1686
1687 const int end = m_ndx + num;
1688 const int numBitsToDst = de::max(0, de::min(m_length, end) - m_ndx);
1689 const int low = m_ndx;
1690 const int high = m_ndx + numBitsToDst - 1;
1691 const deUint32 actualBits = getBits(bits, 0, numBitsToDst-1);
1692
1693 m_ndx += num;
1694
1695 return m_forward ? m_dst.setBits(m_startNdxInSrc + low, m_startNdxInSrc + high, actualBits)
1696 : m_dst.setBits(m_startNdxInSrc - high, m_startNdxInSrc - low, reverseBits(actualBits, numBitsToDst));
1697 }
1698
1699 private:
1700 AssignBlock128& m_dst;
1701 const int m_startNdxInSrc;
1702 const int m_length;
1703 const bool m_forward;
1704
1705 int m_ndx;
1706 };
1707
1708 struct VoidExtentParams
1709 {
1710 DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
1711 bool isHDR;
1712 deUint16 r;
1713 deUint16 g;
1714 deUint16 b;
1715 deUint16 a;
1716 // \note Currently extent coordinates are all set to all-ones.
1717
VoidExtentParamstcu::astc::__anonba8e8f060111::VoidExtentParams1718 VoidExtentParams (bool isHDR_, deUint16 r_, deUint16 g_, deUint16 b_, deUint16 a_) : isHDR(isHDR_), r(r_), g(g_), b(b_), a(a_) {}
1719 };
1720
generateVoidExtentBlock(const VoidExtentParams & params)1721 static AssignBlock128 generateVoidExtentBlock (const VoidExtentParams& params)
1722 {
1723 AssignBlock128 block;
1724
1725 block.setBits(0, 8, 0x1fc); // \note Marks void-extent block.
1726 block.setBit(9, params.isHDR);
1727 block.setBits(10, 11, 3); // \note Spec shows that these bits are both set, although they serve no purpose.
1728
1729 // Extent coordinates - currently all-ones.
1730 block.setBits(12, 24, 0x1fff);
1731 block.setBits(25, 37, 0x1fff);
1732 block.setBits(38, 50, 0x1fff);
1733 block.setBits(51, 63, 0x1fff);
1734
1735 DE_ASSERT(!params.isHDR || (!isFloat16InfOrNan(params.r) &&
1736 !isFloat16InfOrNan(params.g) &&
1737 !isFloat16InfOrNan(params.b) &&
1738 !isFloat16InfOrNan(params.a)));
1739
1740 block.setBits(64, 79, params.r);
1741 block.setBits(80, 95, params.g);
1742 block.setBits(96, 111, params.b);
1743 block.setBits(112, 127, params.a);
1744
1745 return block;
1746 }
1747
1748 // An input array of ISE inputs for an entire ASTC block. Can be given as either single values in the
1749 // range [0, maximumValueOfISERange] or as explicit block value specifications. The latter is needed
1750 // so we can test all possible values of T and Q in a block, since multiple T or Q values may map
1751 // to the same set of decoded values.
1752 struct ISEInput
1753 {
1754 struct Block
1755 {
1756 deUint32 tOrQValue; //!< The 8-bit T or 7-bit Q in a trit or quint ISE block.
1757 deUint32 bitValues[5];
1758 };
1759
1760 bool isGivenInBlockForm;
1761 union
1762 {
1763 //!< \note 64 comes from the maximum number of weight values in an ASTC block.
1764 deUint32 plain[64];
1765 Block block[64];
1766 } value;
1767
ISEInputtcu::astc::__anonba8e8f060111::ISEInput1768 ISEInput (void)
1769 : isGivenInBlockForm (false)
1770 {
1771 }
1772 };
1773
computeISERangeMax(const ISEParams & iseParams)1774 static inline deUint32 computeISERangeMax (const ISEParams& iseParams)
1775 {
1776 switch (iseParams.mode)
1777 {
1778 case ISEMODE_TRIT: return (1u << iseParams.numBits) * 3 - 1;
1779 case ISEMODE_QUINT: return (1u << iseParams.numBits) * 5 - 1;
1780 case ISEMODE_PLAIN_BIT: return (1u << iseParams.numBits) - 1;
1781 default:
1782 DE_ASSERT(false);
1783 return -1;
1784 }
1785 }
1786
1787 struct NormalBlockParams
1788 {
1789 int weightGridWidth;
1790 int weightGridHeight;
1791 ISEParams weightISEParams;
1792 bool isDualPlane;
1793 deUint32 ccs; //! \note Irrelevant if !isDualPlane.
1794 int numPartitions;
1795 deUint32 colorEndpointModes[4];
1796 // \note Below members are irrelevant if numPartitions == 1.
1797 bool isMultiPartSingleCemMode; //! \note If true, the single CEM is at colorEndpointModes[0].
1798 deUint32 partitionSeed;
1799
NormalBlockParamstcu::astc::__anonba8e8f060111::NormalBlockParams1800 NormalBlockParams (void)
1801 : weightGridWidth (-1)
1802 , weightGridHeight (-1)
1803 , weightISEParams (ISEMODE_LAST, -1)
1804 , isDualPlane (true)
1805 , ccs ((deUint32)-1)
1806 , numPartitions (-1)
1807 , isMultiPartSingleCemMode (false)
1808 , partitionSeed ((deUint32)-1)
1809 {
1810 colorEndpointModes[0] = 0;
1811 colorEndpointModes[1] = 0;
1812 colorEndpointModes[2] = 0;
1813 colorEndpointModes[3] = 0;
1814 }
1815 };
1816
1817 struct NormalBlockISEInputs
1818 {
1819 ISEInput weight;
1820 ISEInput endpoint;
1821
NormalBlockISEInputstcu::astc::__anonba8e8f060111::NormalBlockISEInputs1822 NormalBlockISEInputs (void)
1823 : weight ()
1824 , endpoint ()
1825 {
1826 }
1827 };
1828
computeNumWeights(const NormalBlockParams & params)1829 static inline int computeNumWeights (const NormalBlockParams& params)
1830 {
1831 return params.weightGridWidth * params.weightGridHeight * (params.isDualPlane ? 2 : 1);
1832 }
1833
computeNumBitsForColorEndpoints(const NormalBlockParams & params)1834 static inline int computeNumBitsForColorEndpoints (const NormalBlockParams& params)
1835 {
1836 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, computeNumWeights(params));
1837 const int numConfigDataBits = (params.numPartitions == 1 ? 17 : params.isMultiPartSingleCemMode ? 29 : 25 + 3*params.numPartitions) +
1838 (params.isDualPlane ? 2 : 0);
1839
1840 return 128 - numWeightBits - numConfigDataBits;
1841 }
1842
computeNumColorEndpointValues(const deUint32 * endpointModes,int numPartitions,bool isMultiPartSingleCemMode)1843 static inline int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions, bool isMultiPartSingleCemMode)
1844 {
1845 if (isMultiPartSingleCemMode)
1846 return numPartitions * computeNumColorEndpointValues(endpointModes[0]);
1847 else
1848 {
1849 int result = 0;
1850 for (int i = 0; i < numPartitions; i++)
1851 result += computeNumColorEndpointValues(endpointModes[i]);
1852 return result;
1853 }
1854 }
1855
isValidBlockParams(const NormalBlockParams & params,int blockWidth,int blockHeight)1856 static inline bool isValidBlockParams (const NormalBlockParams& params, int blockWidth, int blockHeight)
1857 {
1858 const int numWeights = computeNumWeights(params);
1859 const int numWeightBits = computeNumRequiredBits(params.weightISEParams, numWeights);
1860 const int numColorEndpointValues = computeNumColorEndpointValues(¶ms.colorEndpointModes[0], params.numPartitions, params.isMultiPartSingleCemMode);
1861 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(params);
1862
1863 return numWeights <= 64 &&
1864 de::inRange(numWeightBits, 24, 96) &&
1865 params.weightGridWidth <= blockWidth &&
1866 params.weightGridHeight <= blockHeight &&
1867 !(params.numPartitions == 4 && params.isDualPlane) &&
1868 numColorEndpointValues <= 18 &&
1869 numBitsForColorEndpoints >= deDivRoundUp32(13*numColorEndpointValues, 5);
1870 }
1871
1872 // Write bits 0 to 10 of an ASTC block.
writeBlockMode(AssignBlock128 & dst,const NormalBlockParams & blockParams)1873 static void writeBlockMode (AssignBlock128& dst, const NormalBlockParams& blockParams)
1874 {
1875 const deUint32 d = blockParams.isDualPlane != 0;
1876 // r and h initialized in switch below.
1877 deUint32 r;
1878 deUint32 h;
1879 // a, b and blockModeLayoutNdx initialized in block mode layout index detecting loop below.
1880 deUint32 a = (deUint32)-1;
1881 deUint32 b = (deUint32)-1;
1882 int blockModeLayoutNdx;
1883
1884 // Find the values of r and h (ISE range).
1885 switch (computeISERangeMax(blockParams.weightISEParams))
1886 {
1887 case 1: r = 2; h = 0; break;
1888 case 2: r = 3; h = 0; break;
1889 case 3: r = 4; h = 0; break;
1890 case 4: r = 5; h = 0; break;
1891 case 5: r = 6; h = 0; break;
1892 case 7: r = 7; h = 0; break;
1893
1894 case 9: r = 2; h = 1; break;
1895 case 11: r = 3; h = 1; break;
1896 case 15: r = 4; h = 1; break;
1897 case 19: r = 5; h = 1; break;
1898 case 23: r = 6; h = 1; break;
1899 case 31: r = 7; h = 1; break;
1900
1901 default:
1902 DE_ASSERT(false);
1903 r = (deUint32)-1;
1904 h = (deUint32)-1;
1905 }
1906
1907 // Find block mode layout index, i.e. appropriate row in the "2d block mode layout" table in ASTC spec.
1908
1909 {
1910 enum BlockModeLayoutABVariable { Z=0, A=1, B=2 };
1911
1912 static const struct BlockModeLayout
1913 {
1914 int aNumBits;
1915 int bNumBits;
1916 BlockModeLayoutABVariable gridWidthVariableTerm;
1917 int gridWidthConstantTerm;
1918 BlockModeLayoutABVariable gridHeightVariableTerm;
1919 int gridHeightConstantTerm;
1920 } blockModeLayouts[] =
1921 {
1922 { 2, 2, B, 4, A, 2},
1923 { 2, 2, B, 8, A, 2},
1924 { 2, 2, A, 2, B, 8},
1925 { 2, 1, A, 2, B, 6},
1926 { 2, 1, B, 2, A, 2},
1927 { 2, 0, Z, 12, A, 2},
1928 { 2, 0, A, 2, Z, 12},
1929 { 0, 0, Z, 6, Z, 10},
1930 { 0, 0, Z, 10, Z, 6},
1931 { 2, 2, A, 6, B, 6}
1932 };
1933
1934 for (blockModeLayoutNdx = 0; blockModeLayoutNdx < DE_LENGTH_OF_ARRAY(blockModeLayouts); blockModeLayoutNdx++)
1935 {
1936 const BlockModeLayout& layout = blockModeLayouts[blockModeLayoutNdx];
1937 const int aMax = (1 << layout.aNumBits) - 1;
1938 const int bMax = (1 << layout.bNumBits) - 1;
1939 const int variableOffsetsMax[3] = { 0, aMax, bMax };
1940 const int widthMin = layout.gridWidthConstantTerm;
1941 const int heightMin = layout.gridHeightConstantTerm;
1942 const int widthMax = widthMin + variableOffsetsMax[layout.gridWidthVariableTerm];
1943 const int heightMax = heightMin + variableOffsetsMax[layout.gridHeightVariableTerm];
1944
1945 DE_ASSERT(layout.gridWidthVariableTerm != layout.gridHeightVariableTerm || layout.gridWidthVariableTerm == Z);
1946
1947 if (de::inRange(blockParams.weightGridWidth, widthMin, widthMax) &&
1948 de::inRange(blockParams.weightGridHeight, heightMin, heightMax))
1949 {
1950 deUint32 dummy = 0;
1951 deUint32& widthVariable = layout.gridWidthVariableTerm == A ? a : layout.gridWidthVariableTerm == B ? b : dummy;
1952 deUint32& heightVariable = layout.gridHeightVariableTerm == A ? a : layout.gridHeightVariableTerm == B ? b : dummy;
1953
1954 widthVariable = blockParams.weightGridWidth - layout.gridWidthConstantTerm;
1955 heightVariable = blockParams.weightGridHeight - layout.gridHeightConstantTerm;
1956
1957 break;
1958 }
1959 }
1960 }
1961
1962 // Set block mode bits.
1963
1964 const deUint32 a0 = getBit(a, 0);
1965 const deUint32 a1 = getBit(a, 1);
1966 const deUint32 b0 = getBit(b, 0);
1967 const deUint32 b1 = getBit(b, 1);
1968 const deUint32 r0 = getBit(r, 0);
1969 const deUint32 r1 = getBit(r, 1);
1970 const deUint32 r2 = getBit(r, 2);
1971
1972 #define SB(NDX, VAL) dst.setBit((NDX), (VAL))
1973 #define ASSIGN_BITS(B10, B9, B8, B7, B6, B5, B4, B3, B2, B1, B0) do { SB(10,(B10)); SB(9,(B9)); SB(8,(B8)); SB(7,(B7)); SB(6,(B6)); SB(5,(B5)); SB(4,(B4)); SB(3,(B3)); SB(2,(B2)); SB(1,(B1)); SB(0,(B0)); } while (false)
1974
1975 switch (blockModeLayoutNdx)
1976 {
1977 case 0: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 0, r2, r1); break;
1978 case 1: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 0, 1, r2, r1); break;
1979 case 2: ASSIGN_BITS(d, h, b1, b0, a1, a0, r0, 1, 0, r2, r1); break;
1980 case 3: ASSIGN_BITS(d, h, 0, b, a1, a0, r0, 1, 1, r2, r1); break;
1981 case 4: ASSIGN_BITS(d, h, 1, b, a1, a0, r0, 1, 1, r2, r1); break;
1982 case 5: ASSIGN_BITS(d, h, 0, 0, a1, a0, r0, r2, r1, 0, 0); break;
1983 case 6: ASSIGN_BITS(d, h, 0, 1, a1, a0, r0, r2, r1, 0, 0); break;
1984 case 7: ASSIGN_BITS(d, h, 1, 1, 0, 0, r0, r2, r1, 0, 0); break;
1985 case 8: ASSIGN_BITS(d, h, 1, 1, 0, 1, r0, r2, r1, 0, 0); break;
1986 case 9: ASSIGN_BITS(b1, b0, 1, 0, a1, a0, r0, r2, r1, 0, 0); DE_ASSERT(d == 0 && h == 0); break;
1987 default:
1988 DE_ASSERT(false);
1989 }
1990
1991 #undef ASSIGN_BITS
1992 #undef SB
1993 }
1994
1995 // Write color endpoint mode data of an ASTC block.
writeColorEndpointModes(AssignBlock128 & dst,const deUint32 * colorEndpointModes,bool isMultiPartSingleCemMode,int numPartitions,int extraCemBitsStart)1996 static void writeColorEndpointModes (AssignBlock128& dst, const deUint32* colorEndpointModes, bool isMultiPartSingleCemMode, int numPartitions, int extraCemBitsStart)
1997 {
1998 if (numPartitions == 1)
1999 dst.setBits(13, 16, colorEndpointModes[0]);
2000 else
2001 {
2002 if (isMultiPartSingleCemMode)
2003 {
2004 dst.setBits(23, 24, 0);
2005 dst.setBits(25, 28, colorEndpointModes[0]);
2006 }
2007 else
2008 {
2009 DE_ASSERT(numPartitions > 0);
2010 const deUint32 minCem = *std::min_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2011 const deUint32 maxCem = *std::max_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
2012 const deUint32 minCemClass = minCem/4;
2013 const deUint32 maxCemClass = maxCem/4;
2014 DE_ASSERT(maxCemClass - minCemClass <= 1);
2015 DE_UNREF(minCemClass); // \note For non-debug builds.
2016 const deUint32 highLevelSelector = de::max(1u, maxCemClass);
2017
2018 dst.setBits(23, 24, highLevelSelector);
2019
2020 for (int partNdx = 0; partNdx < numPartitions; partNdx++)
2021 {
2022 const deUint32 c = colorEndpointModes[partNdx] / 4 == highLevelSelector ? 1 : 0;
2023 const deUint32 m = colorEndpointModes[partNdx] % 4;
2024 const deUint32 lowMBit0Ndx = numPartitions + 2*partNdx;
2025 const deUint32 lowMBit1Ndx = numPartitions + 2*partNdx + 1;
2026 dst.setBit(25 + partNdx, c);
2027 dst.setBit(lowMBit0Ndx < 4 ? 25+lowMBit0Ndx : extraCemBitsStart+lowMBit0Ndx-4, getBit(m, 0));
2028 dst.setBit(lowMBit1Ndx < 4 ? 25+lowMBit1Ndx : extraCemBitsStart+lowMBit1Ndx-4, getBit(m, 1));
2029 }
2030 }
2031 }
2032 }
2033
encodeISETritBlock(BitAssignAccessStream & dst,int numBits,bool fromExplicitInputBlock,const ISEInput::Block & blockInput,const deUint32 * nonBlockInput,int numValues)2034 static void encodeISETritBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2035 {
2036 // tritBlockTValue[t0][t1][t2][t3][t4] is a value of T (not necessarily the only one) that will yield the given trits when decoded.
2037 static const deUint32 tritBlockTValue[3][3][3][3][3] =
2038 {
2039 {
2040 {{{0, 128, 96}, {32, 160, 224}, {64, 192, 28}}, {{16, 144, 112}, {48, 176, 240}, {80, 208, 156}}, {{3, 131, 99}, {35, 163, 227}, {67, 195, 31}}},
2041 {{{4, 132, 100}, {36, 164, 228}, {68, 196, 60}}, {{20, 148, 116}, {52, 180, 244}, {84, 212, 188}}, {{19, 147, 115}, {51, 179, 243}, {83, 211, 159}}},
2042 {{{8, 136, 104}, {40, 168, 232}, {72, 200, 92}}, {{24, 152, 120}, {56, 184, 248}, {88, 216, 220}}, {{12, 140, 108}, {44, 172, 236}, {76, 204, 124}}}
2043 },
2044 {
2045 {{{1, 129, 97}, {33, 161, 225}, {65, 193, 29}}, {{17, 145, 113}, {49, 177, 241}, {81, 209, 157}}, {{7, 135, 103}, {39, 167, 231}, {71, 199, 63}}},
2046 {{{5, 133, 101}, {37, 165, 229}, {69, 197, 61}}, {{21, 149, 117}, {53, 181, 245}, {85, 213, 189}}, {{23, 151, 119}, {55, 183, 247}, {87, 215, 191}}},
2047 {{{9, 137, 105}, {41, 169, 233}, {73, 201, 93}}, {{25, 153, 121}, {57, 185, 249}, {89, 217, 221}}, {{13, 141, 109}, {45, 173, 237}, {77, 205, 125}}}
2048 },
2049 {
2050 {{{2, 130, 98}, {34, 162, 226}, {66, 194, 30}}, {{18, 146, 114}, {50, 178, 242}, {82, 210, 158}}, {{11, 139, 107}, {43, 171, 235}, {75, 203, 95}}},
2051 {{{6, 134, 102}, {38, 166, 230}, {70, 198, 62}}, {{22, 150, 118}, {54, 182, 246}, {86, 214, 190}}, {{27, 155, 123}, {59, 187, 251}, {91, 219, 223}}},
2052 {{{10, 138, 106}, {42, 170, 234}, {74, 202, 94}}, {{26, 154, 122}, {58, 186, 250}, {90, 218, 222}}, {{14, 142, 110}, {46, 174, 238}, {78, 206, 126}}}
2053 }
2054 };
2055
2056 DE_ASSERT(de::inRange(numValues, 1, 5));
2057
2058 deUint32 tritParts[5];
2059 deUint32 bitParts[5];
2060
2061 for (int i = 0; i < 5; i++)
2062 {
2063 if (i < numValues)
2064 {
2065 if (fromExplicitInputBlock)
2066 {
2067 bitParts[i] = blockInput.bitValues[i];
2068 tritParts[i] = -1; // \note Won't be used, but silences warning.
2069 }
2070 else
2071 {
2072 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2073 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2074 tritParts[i] = nonBlockInput[i] >> numBits;
2075 }
2076 }
2077 else
2078 {
2079 bitParts[i] = 0;
2080 tritParts[i] = 0;
2081 }
2082 }
2083
2084 const deUint32 T = fromExplicitInputBlock ? blockInput.tOrQValue : tritBlockTValue[tritParts[0]]
2085 [tritParts[1]]
2086 [tritParts[2]]
2087 [tritParts[3]]
2088 [tritParts[4]];
2089
2090 dst.setNext(numBits, bitParts[0]);
2091 dst.setNext(2, getBits(T, 0, 1));
2092 dst.setNext(numBits, bitParts[1]);
2093 dst.setNext(2, getBits(T, 2, 3));
2094 dst.setNext(numBits, bitParts[2]);
2095 dst.setNext(1, getBit(T, 4));
2096 dst.setNext(numBits, bitParts[3]);
2097 dst.setNext(2, getBits(T, 5, 6));
2098 dst.setNext(numBits, bitParts[4]);
2099 dst.setNext(1, getBit(T, 7));
2100 }
2101
encodeISEQuintBlock(BitAssignAccessStream & dst,int numBits,bool fromExplicitInputBlock,const ISEInput::Block & blockInput,const deUint32 * nonBlockInput,int numValues)2102 static void encodeISEQuintBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
2103 {
2104 // quintBlockQValue[q0][q1][q2] is a value of Q (not necessarily the only one) that will yield the given quints when decoded.
2105 static const deUint32 quintBlockQValue[5][5][5] =
2106 {
2107 {{0, 32, 64, 96, 102}, {8, 40, 72, 104, 110}, {16, 48, 80, 112, 118}, {24, 56, 88, 120, 126}, {5, 37, 69, 101, 39}},
2108 {{1, 33, 65, 97, 103}, {9, 41, 73, 105, 111}, {17, 49, 81, 113, 119}, {25, 57, 89, 121, 127}, {13, 45, 77, 109, 47}},
2109 {{2, 34, 66, 98, 70}, {10, 42, 74, 106, 78}, {18, 50, 82, 114, 86}, {26, 58, 90, 122, 94}, {21, 53, 85, 117, 55}},
2110 {{3, 35, 67, 99, 71}, {11, 43, 75, 107, 79}, {19, 51, 83, 115, 87}, {27, 59, 91, 123, 95}, {29, 61, 93, 125, 63}},
2111 {{4, 36, 68, 100, 38}, {12, 44, 76, 108, 46}, {20, 52, 84, 116, 54}, {28, 60, 92, 124, 62}, {6, 14, 22, 30, 7}}
2112 };
2113
2114 DE_ASSERT(de::inRange(numValues, 1, 3));
2115
2116 deUint32 quintParts[3];
2117 deUint32 bitParts[3];
2118
2119 for (int i = 0; i < 3; i++)
2120 {
2121 if (i < numValues)
2122 {
2123 if (fromExplicitInputBlock)
2124 {
2125 bitParts[i] = blockInput.bitValues[i];
2126 quintParts[i] = -1; // \note Won't be used, but silences warning.
2127 }
2128 else
2129 {
2130 // \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
2131 bitParts[i] = numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
2132 quintParts[i] = nonBlockInput[i] >> numBits;
2133 }
2134 }
2135 else
2136 {
2137 bitParts[i] = 0;
2138 quintParts[i] = 0;
2139 }
2140 }
2141
2142 const deUint32 Q = fromExplicitInputBlock ? blockInput.tOrQValue : quintBlockQValue[quintParts[0]]
2143 [quintParts[1]]
2144 [quintParts[2]];
2145
2146 dst.setNext(numBits, bitParts[0]);
2147 dst.setNext(3, getBits(Q, 0, 2));
2148 dst.setNext(numBits, bitParts[1]);
2149 dst.setNext(2, getBits(Q, 3, 4));
2150 dst.setNext(numBits, bitParts[2]);
2151 dst.setNext(2, getBits(Q, 5, 6));
2152 }
2153
encodeISEBitBlock(BitAssignAccessStream & dst,int numBits,deUint32 value)2154 static void encodeISEBitBlock (BitAssignAccessStream& dst, int numBits, deUint32 value)
2155 {
2156 DE_ASSERT(de::inRange(value, 0u, (1u<<numBits)-1));
2157 dst.setNext(numBits, value);
2158 }
2159
encodeISE(BitAssignAccessStream & dst,const ISEParams & params,const ISEInput & input,int numValues)2160 static void encodeISE (BitAssignAccessStream& dst, const ISEParams& params, const ISEInput& input, int numValues)
2161 {
2162 if (params.mode == ISEMODE_TRIT)
2163 {
2164 const int numBlocks = deDivRoundUp32(numValues, 5);
2165 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2166 {
2167 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
2168 encodeISETritBlock(dst, params.numBits, input.isGivenInBlockForm,
2169 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2170 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[5*blockNdx],
2171 numValuesInBlock);
2172 }
2173 }
2174 else if (params.mode == ISEMODE_QUINT)
2175 {
2176 const int numBlocks = deDivRoundUp32(numValues, 3);
2177 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2178 {
2179 const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
2180 encodeISEQuintBlock(dst, params.numBits, input.isGivenInBlockForm,
2181 input.isGivenInBlockForm ? input.value.block[blockNdx] : ISEInput::Block(),
2182 input.isGivenInBlockForm ? DE_NULL : &input.value.plain[3*blockNdx],
2183 numValuesInBlock);
2184 }
2185 }
2186 else
2187 {
2188 DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
2189 for (int i = 0; i < numValues; i++)
2190 encodeISEBitBlock(dst, params.numBits, input.isGivenInBlockForm ? input.value.block[i].bitValues[0] : input.value.plain[i]);
2191 }
2192 }
2193
writeWeightData(AssignBlock128 & dst,const ISEParams & iseParams,const ISEInput & input,int numWeights)2194 static void writeWeightData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numWeights)
2195 {
2196 const int numWeightBits = computeNumRequiredBits(iseParams, numWeights);
2197 BitAssignAccessStream access (dst, 127, numWeightBits, false);
2198 encodeISE(access, iseParams, input, numWeights);
2199 }
2200
writeColorEndpointData(AssignBlock128 & dst,const ISEParams & iseParams,const ISEInput & input,int numEndpoints,int numBitsForColorEndpoints,int colorEndpointDataStartNdx)2201 static void writeColorEndpointData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numEndpoints, int numBitsForColorEndpoints, int colorEndpointDataStartNdx)
2202 {
2203 BitAssignAccessStream access(dst, colorEndpointDataStartNdx, numBitsForColorEndpoints, true);
2204 encodeISE(access, iseParams, input, numEndpoints);
2205 }
2206
generateNormalBlock(const NormalBlockParams & blockParams,int blockWidth,int blockHeight,const NormalBlockISEInputs & iseInputs)2207 static AssignBlock128 generateNormalBlock (const NormalBlockParams& blockParams, int blockWidth, int blockHeight, const NormalBlockISEInputs& iseInputs)
2208 {
2209 DE_ASSERT(isValidBlockParams(blockParams, blockWidth, blockHeight));
2210 DE_UNREF(blockWidth); // \note For non-debug builds.
2211 DE_UNREF(blockHeight); // \note For non-debug builds.
2212
2213 AssignBlock128 block;
2214 const int numWeights = computeNumWeights(blockParams);
2215 const int numWeightBits = computeNumRequiredBits(blockParams.weightISEParams, numWeights);
2216
2217 writeBlockMode(block, blockParams);
2218
2219 block.setBits(11, 12, blockParams.numPartitions - 1);
2220 if (blockParams.numPartitions > 1)
2221 block.setBits(13, 22, blockParams.partitionSeed);
2222
2223 {
2224 const int extraCemBitsStart = 127 - numWeightBits - (blockParams.numPartitions == 1 || blockParams.isMultiPartSingleCemMode ? -1
2225 : blockParams.numPartitions == 4 ? 7
2226 : blockParams.numPartitions == 3 ? 4
2227 : blockParams.numPartitions == 2 ? 1
2228 : 0);
2229
2230 writeColorEndpointModes(block, &blockParams.colorEndpointModes[0], blockParams.isMultiPartSingleCemMode, blockParams.numPartitions, extraCemBitsStart);
2231
2232 if (blockParams.isDualPlane)
2233 block.setBits(extraCemBitsStart-2, extraCemBitsStart-1, blockParams.ccs);
2234 }
2235
2236 writeWeightData(block, blockParams.weightISEParams, iseInputs.weight, numWeights);
2237
2238 {
2239 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2240 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2241 const int colorEndpointDataStartNdx = blockParams.numPartitions == 1 ? 17 : 29;
2242 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2243
2244 writeColorEndpointData(block, colorEndpointISEParams, iseInputs.endpoint, numColorEndpointValues, numBitsForColorEndpoints, colorEndpointDataStartNdx);
2245 }
2246
2247 return block;
2248 }
2249
2250 // Generate default ISE inputs for weight and endpoint data - gradient-ish values.
generateDefaultISEInputs(const NormalBlockParams & blockParams)2251 static NormalBlockISEInputs generateDefaultISEInputs (const NormalBlockParams& blockParams)
2252 {
2253 NormalBlockISEInputs result;
2254
2255 {
2256 result.weight.isGivenInBlockForm = false;
2257
2258 const int numWeights = computeNumWeights(blockParams);
2259 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2260
2261 if (blockParams.isDualPlane)
2262 {
2263 for (int i = 0; i < numWeights; i += 2)
2264 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2265
2266 for (int i = 1; i < numWeights; i += 2)
2267 result.weight.value.plain[i] = weightRangeMax - (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2268 }
2269 else
2270 {
2271 for (int i = 0; i < numWeights; i++)
2272 result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
2273 }
2274 }
2275
2276 {
2277 result.endpoint.isGivenInBlockForm = false;
2278
2279 const int numColorEndpointValues = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2280 const int numBitsForColorEndpoints = computeNumBitsForColorEndpoints(blockParams);
2281 const ISEParams& colorEndpointISEParams = computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
2282 const int colorEndpointRangeMax = computeISERangeMax(colorEndpointISEParams);
2283
2284 for (int i = 0; i < numColorEndpointValues; i++)
2285 result.endpoint.value.plain[i] = (i*colorEndpointRangeMax + (numColorEndpointValues-1)/2) / (numColorEndpointValues-1);
2286 }
2287
2288 return result;
2289 }
2290
2291 static const ISEParams s_weightISEParamsCandidates[] =
2292 {
2293 ISEParams(ISEMODE_PLAIN_BIT, 1),
2294 ISEParams(ISEMODE_TRIT, 0),
2295 ISEParams(ISEMODE_PLAIN_BIT, 2),
2296 ISEParams(ISEMODE_QUINT, 0),
2297 ISEParams(ISEMODE_TRIT, 1),
2298 ISEParams(ISEMODE_PLAIN_BIT, 3),
2299 ISEParams(ISEMODE_QUINT, 1),
2300 ISEParams(ISEMODE_TRIT, 2),
2301 ISEParams(ISEMODE_PLAIN_BIT, 4),
2302 ISEParams(ISEMODE_QUINT, 2),
2303 ISEParams(ISEMODE_TRIT, 3),
2304 ISEParams(ISEMODE_PLAIN_BIT, 5)
2305 };
2306
generateRandomBlock(deUint8 * dst,const IVec3 & blockSize,de::Random & rnd)2307 void generateRandomBlock (deUint8* dst, const IVec3& blockSize, de::Random& rnd)
2308 {
2309 DE_ASSERT(blockSize.z() == 1);
2310
2311 if (rnd.getFloat() < 0.1f)
2312 {
2313 // Void extent block.
2314 const bool isVoidExtentHDR = rnd.getBool();
2315 const deUint16 r = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2316 const deUint16 g = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2317 const deUint16 b = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2318 const deUint16 a = isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
2319 generateVoidExtentBlock(VoidExtentParams(isVoidExtentHDR, r, g, b, a)).assignToMemory(dst);
2320 }
2321 else
2322 {
2323 // Not void extent block.
2324
2325 // Generate block params.
2326
2327 NormalBlockParams blockParams;
2328
2329 do
2330 {
2331 blockParams.weightGridWidth = rnd.getInt(2, blockSize.x());
2332 blockParams.weightGridHeight = rnd.getInt(2, blockSize.y());
2333 blockParams.weightISEParams = s_weightISEParamsCandidates[rnd.getInt(0, DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates)-1)];
2334 blockParams.numPartitions = rnd.getInt(1, 4);
2335 blockParams.isMultiPartSingleCemMode = rnd.getFloat() < 0.25f;
2336 blockParams.isDualPlane = blockParams.numPartitions != 4 && rnd.getBool();
2337 blockParams.ccs = rnd.getInt(0, 3);
2338 blockParams.partitionSeed = rnd.getInt(0, 1023);
2339
2340 blockParams.colorEndpointModes[0] = rnd.getInt(0, 15);
2341
2342 {
2343 const int cemDiff = blockParams.isMultiPartSingleCemMode ? 0
2344 : blockParams.colorEndpointModes[0] == 0 ? 1
2345 : blockParams.colorEndpointModes[0] == 15 ? -1
2346 : rnd.getBool() ? 1 : -1;
2347
2348 for (int i = 1; i < blockParams.numPartitions; i++)
2349 blockParams.colorEndpointModes[i] = blockParams.colorEndpointModes[0] + (cemDiff == -1 ? rnd.getInt(-1, 0) : cemDiff == 1 ? rnd.getInt(0, 1) : 0);
2350 }
2351 } while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()));
2352
2353 // Generate ISE inputs for both weight and endpoint data.
2354
2355 NormalBlockISEInputs iseInputs;
2356
2357 for (int weightOrEndpoints = 0; weightOrEndpoints <= 1; weightOrEndpoints++)
2358 {
2359 const bool setWeights = weightOrEndpoints == 0;
2360 const int numValues = setWeights ? computeNumWeights(blockParams) :
2361 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
2362 const ISEParams iseParams = setWeights ? blockParams.weightISEParams : computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams), numValues);
2363 ISEInput& iseInput = setWeights ? iseInputs.weight : iseInputs.endpoint;
2364
2365 iseInput.isGivenInBlockForm = rnd.getBool();
2366
2367 if (iseInput.isGivenInBlockForm)
2368 {
2369 const int numValuesPerISEBlock = iseParams.mode == ISEMODE_TRIT ? 5
2370 : iseParams.mode == ISEMODE_QUINT ? 3
2371 : 1;
2372 const int iseBitMax = (1 << iseParams.numBits) - 1;
2373 const int numISEBlocks = deDivRoundUp32(numValues, numValuesPerISEBlock);
2374
2375 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocks; iseBlockNdx++)
2376 {
2377 iseInput.value.block[iseBlockNdx].tOrQValue = rnd.getInt(0, 255);
2378 for (int i = 0; i < numValuesPerISEBlock; i++)
2379 iseInput.value.block[iseBlockNdx].bitValues[i] = rnd.getInt(0, iseBitMax);
2380 }
2381 }
2382 else
2383 {
2384 const int rangeMax = computeISERangeMax(iseParams);
2385
2386 for (int valueNdx = 0; valueNdx < numValues; valueNdx++)
2387 iseInput.value.plain[valueNdx] = rnd.getInt(0, rangeMax);
2388 }
2389 }
2390
2391 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).assignToMemory(dst);
2392 }
2393 }
2394
2395 } // anonymous
2396
2397 // Generate block data for a given BlockTestType and format.
generateBlockCaseTestData(vector<deUint8> & dst,CompressedTexFormat format,BlockTestType testType)2398 void generateBlockCaseTestData (vector<deUint8>& dst, CompressedTexFormat format, BlockTestType testType)
2399 {
2400 DE_ASSERT(isAstcFormat(format));
2401 DE_ASSERT(!(isAstcSRGBFormat(format) && isBlockTestTypeHDROnly(testType)));
2402
2403 const IVec3 blockSize = getBlockPixelSize(format);
2404 DE_ASSERT(blockSize.z() == 1);
2405
2406 switch (testType)
2407 {
2408 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:
2409 // Generate a gradient-like set of LDR void-extent blocks.
2410 {
2411 const int numBlocks = 1<<13;
2412 const deUint32 numValues = 1<<16;
2413 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2414
2415 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2416 {
2417 const deUint32 baseValue = blockNdx*(numValues-1) / (numBlocks-1);
2418 const deUint16 r = (deUint16)((baseValue + numValues*0/4) % numValues);
2419 const deUint16 g = (deUint16)((baseValue + numValues*1/4) % numValues);
2420 const deUint16 b = (deUint16)((baseValue + numValues*2/4) % numValues);
2421 const deUint16 a = (deUint16)((baseValue + numValues*3/4) % numValues);
2422 AssignBlock128 block;
2423
2424 generateVoidExtentBlock(VoidExtentParams(false, r, g, b, a)).pushBytesToVector(dst);
2425 }
2426
2427 break;
2428 }
2429
2430 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:
2431 // Generate a gradient-like set of HDR void-extent blocks, with values ranging from the largest finite negative to largest finite positive of fp16.
2432 {
2433 const float minValue = -65504.0f;
2434 const float maxValue = +65504.0f;
2435 const int numBlocks = 1<<13;
2436 dst.reserve(numBlocks*BLOCK_SIZE_BYTES);
2437
2438 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2439 {
2440 const int rNdx = (blockNdx + numBlocks*0/4) % numBlocks;
2441 const int gNdx = (blockNdx + numBlocks*1/4) % numBlocks;
2442 const int bNdx = (blockNdx + numBlocks*2/4) % numBlocks;
2443 const int aNdx = (blockNdx + numBlocks*3/4) % numBlocks;
2444 const deFloat16 r = deFloat32To16(minValue + (float)rNdx * (maxValue - minValue) / (float)(numBlocks-1));
2445 const deFloat16 g = deFloat32To16(minValue + (float)gNdx * (maxValue - minValue) / (float)(numBlocks-1));
2446 const deFloat16 b = deFloat32To16(minValue + (float)bNdx * (maxValue - minValue) / (float)(numBlocks-1));
2447 const deFloat16 a = deFloat32To16(minValue + (float)aNdx * (maxValue - minValue) / (float)(numBlocks-1));
2448
2449 generateVoidExtentBlock(VoidExtentParams(true, r, g, b, a)).pushBytesToVector(dst);
2450 }
2451
2452 break;
2453 }
2454
2455 case BLOCK_TEST_TYPE_WEIGHT_GRID:
2456 // Generate different combinations of plane count, weight ISE params, and grid size.
2457 {
2458 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2459 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2460 for (int weightGridWidth = 2; weightGridWidth <= 12; weightGridWidth++)
2461 for (int weightGridHeight = 2; weightGridHeight <= 12; weightGridHeight++)
2462 {
2463 NormalBlockParams blockParams;
2464 NormalBlockISEInputs iseInputs;
2465
2466 blockParams.weightGridWidth = weightGridWidth;
2467 blockParams.weightGridHeight = weightGridHeight;
2468 blockParams.isDualPlane = isDualPlane != 0;
2469 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2470 blockParams.ccs = 0;
2471 blockParams.numPartitions = 1;
2472 blockParams.colorEndpointModes[0] = 0;
2473
2474 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2475 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2476 }
2477
2478 break;
2479 }
2480
2481 case BLOCK_TEST_TYPE_WEIGHT_ISE:
2482 // For each weight ISE param set, generate blocks that cover:
2483 // - each single value of the ISE's range, at each position inside an ISE block
2484 // - for trit and quint ISEs, each single T or Q value of an ISE block
2485 {
2486 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2487 {
2488 const ISEParams& iseParams = s_weightISEParamsCandidates[iseParamsNdx];
2489 NormalBlockParams blockParams;
2490
2491 blockParams.weightGridWidth = 4;
2492 blockParams.weightGridHeight = 4;
2493 blockParams.weightISEParams = iseParams;
2494 blockParams.numPartitions = 1;
2495 blockParams.isDualPlane = blockParams.weightGridWidth * blockParams.weightGridHeight < 24 ? true : false;
2496 blockParams.ccs = 0;
2497 blockParams.colorEndpointModes[0] = 0;
2498
2499 while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2500 {
2501 blockParams.weightGridWidth--;
2502 blockParams.weightGridHeight--;
2503 }
2504
2505 const int numValuesInISEBlock = iseParams.mode == ISEMODE_TRIT ? 5 : iseParams.mode == ISEMODE_QUINT ? 3 : 1;
2506 const int numWeights = computeNumWeights(blockParams);
2507
2508 {
2509 const int numWeightValues = (int)computeISERangeMax(iseParams) + 1;
2510 const int numBlocks = deDivRoundUp32(numWeightValues, numWeights);
2511 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2512 iseInputs.weight.isGivenInBlockForm = false;
2513
2514 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2515 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2516 {
2517 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2518 iseInputs.weight.value.plain[weightNdx] = (blockNdx*numWeights + weightNdx + offset) % numWeightValues;
2519
2520 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2521 }
2522 }
2523
2524 if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
2525 {
2526 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2527 iseInputs.weight.isGivenInBlockForm = true;
2528
2529 const int numTQValues = 1 << (iseParams.mode == ISEMODE_TRIT ? 8 : 7);
2530 const int numISEBlocksPerBlock = deDivRoundUp32(numWeights, numValuesInISEBlock);
2531 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2532
2533 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2534 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2535 {
2536 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2537 {
2538 for (int i = 0; i < numValuesInISEBlock; i++)
2539 iseInputs.weight.value.block[iseBlockNdx].bitValues[i] = 0;
2540 iseInputs.weight.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2541 }
2542
2543 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2544 }
2545 }
2546 }
2547
2548 break;
2549 }
2550
2551 case BLOCK_TEST_TYPE_CEMS:
2552 // For each plane count & partition count combination, generate all color endpoint mode combinations.
2553 {
2554 for (int isDualPlane = 0; isDualPlane <= 1; isDualPlane++)
2555 for (int numPartitions = 1; numPartitions <= (isDualPlane != 0 ? 3 : 4); numPartitions++)
2556 {
2557 // Multi-partition, single-CEM mode.
2558 if (numPartitions > 1)
2559 {
2560 for (deUint32 singleCem = 0; singleCem < 16; singleCem++)
2561 {
2562 NormalBlockParams blockParams;
2563 blockParams.weightGridWidth = 4;
2564 blockParams.weightGridHeight = 4;
2565 blockParams.isDualPlane = isDualPlane != 0;
2566 blockParams.ccs = 0;
2567 blockParams.numPartitions = numPartitions;
2568 blockParams.isMultiPartSingleCemMode = true;
2569 blockParams.colorEndpointModes[0] = singleCem;
2570 blockParams.partitionSeed = 634;
2571
2572 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2573 {
2574 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2575 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2576 {
2577 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2578 break;
2579 }
2580 }
2581 }
2582 }
2583
2584 // Separate-CEM mode.
2585 for (deUint32 cem0 = 0; cem0 < 16; cem0++)
2586 for (deUint32 cem1 = 0; cem1 < (numPartitions >= 2 ? 16u : 1u); cem1++)
2587 for (deUint32 cem2 = 0; cem2 < (numPartitions >= 3 ? 16u : 1u); cem2++)
2588 for (deUint32 cem3 = 0; cem3 < (numPartitions >= 4 ? 16u : 1u); cem3++)
2589 {
2590 NormalBlockParams blockParams;
2591 blockParams.weightGridWidth = 4;
2592 blockParams.weightGridHeight = 4;
2593 blockParams.isDualPlane = isDualPlane != 0;
2594 blockParams.ccs = 0;
2595 blockParams.numPartitions = numPartitions;
2596 blockParams.isMultiPartSingleCemMode = false;
2597 blockParams.colorEndpointModes[0] = cem0;
2598 blockParams.colorEndpointModes[1] = cem1;
2599 blockParams.colorEndpointModes[2] = cem2;
2600 blockParams.colorEndpointModes[3] = cem3;
2601 blockParams.partitionSeed = 634;
2602
2603 {
2604 const deUint32 minCem = *std::min_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2605 const deUint32 maxCem = *std::max_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
2606 const deUint32 minCemClass = minCem/4;
2607 const deUint32 maxCemClass = maxCem/4;
2608
2609 if (maxCemClass - minCemClass > 1)
2610 continue;
2611 }
2612
2613 for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
2614 {
2615 blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
2616 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2617 {
2618 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2619 break;
2620 }
2621 }
2622 }
2623 }
2624
2625 break;
2626 }
2627
2628 case BLOCK_TEST_TYPE_PARTITION_SEED:
2629 // Test all partition seeds ("partition pattern indices").
2630 {
2631 for (int numPartitions = 2; numPartitions <= 4; numPartitions++)
2632 for (deUint32 partitionSeed = 0; partitionSeed < 1<<10; partitionSeed++)
2633 {
2634 NormalBlockParams blockParams;
2635 blockParams.weightGridWidth = 4;
2636 blockParams.weightGridHeight = 4;
2637 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2638 blockParams.isDualPlane = false;
2639 blockParams.numPartitions = numPartitions;
2640 blockParams.isMultiPartSingleCemMode = true;
2641 blockParams.colorEndpointModes[0] = 0;
2642 blockParams.partitionSeed = partitionSeed;
2643
2644 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2645 }
2646
2647 break;
2648 }
2649
2650 // \note Fall-through.
2651 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:
2652 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:
2653 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:
2654 // For each endpoint mode, for each pair of components in the endpoint value, test 10x10 combinations of values for that pair.
2655 // \note Separate modes for HDR and mode 15 due to different color scales and biases.
2656 {
2657 for (deUint32 cem = 0; cem < 16; cem++)
2658 {
2659 const bool isHDRCem = cem == 2 ||
2660 cem == 3 ||
2661 cem == 7 ||
2662 cem == 11 ||
2663 cem == 14 ||
2664 cem == 15;
2665
2666 if ((testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR && isHDRCem) ||
2667 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 && (!isHDRCem || cem == 15)) ||
2668 (testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15 && cem != 15))
2669 continue;
2670
2671 NormalBlockParams blockParams;
2672 blockParams.weightGridWidth = 3;
2673 blockParams.weightGridHeight = 4;
2674 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2675 blockParams.isDualPlane = false;
2676 blockParams.numPartitions = 1;
2677 blockParams.colorEndpointModes[0] = cem;
2678
2679 {
2680 const int numBitsForEndpoints = computeNumBitsForColorEndpoints(blockParams);
2681 const int numEndpointParts = computeNumColorEndpointValues(cem);
2682 const ISEParams endpointISE = computeMaximumRangeISEParams(numBitsForEndpoints, numEndpointParts);
2683 const int endpointISERangeMax = computeISERangeMax(endpointISE);
2684
2685 for (int endpointPartNdx0 = 0; endpointPartNdx0 < numEndpointParts; endpointPartNdx0++)
2686 for (int endpointPartNdx1 = endpointPartNdx0+1; endpointPartNdx1 < numEndpointParts; endpointPartNdx1++)
2687 {
2688 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2689 const int numEndpointValues = de::min(10, endpointISERangeMax+1);
2690
2691 for (int endpointValueNdx0 = 0; endpointValueNdx0 < numEndpointValues; endpointValueNdx0++)
2692 for (int endpointValueNdx1 = 0; endpointValueNdx1 < numEndpointValues; endpointValueNdx1++)
2693 {
2694 const int endpointValue0 = endpointValueNdx0 * endpointISERangeMax / (numEndpointValues-1);
2695 const int endpointValue1 = endpointValueNdx1 * endpointISERangeMax / (numEndpointValues-1);
2696
2697 iseInputs.endpoint.value.plain[endpointPartNdx0] = endpointValue0;
2698 iseInputs.endpoint.value.plain[endpointPartNdx1] = endpointValue1;
2699
2700 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2701 }
2702 }
2703 }
2704 }
2705
2706 break;
2707 }
2708
2709 case BLOCK_TEST_TYPE_ENDPOINT_ISE:
2710 // Similar to BLOCK_TEST_TYPE_WEIGHT_ISE, see above.
2711 {
2712 static const deUint32 endpointRangeMaximums[] = { 5, 9, 11, 19, 23, 39, 47, 79, 95, 159, 191 };
2713
2714 for (int endpointRangeNdx = 0; endpointRangeNdx < DE_LENGTH_OF_ARRAY(endpointRangeMaximums); endpointRangeNdx++)
2715 {
2716 bool validCaseGenerated = false;
2717
2718 for (int numPartitions = 1; !validCaseGenerated && numPartitions <= 4; numPartitions++)
2719 for (int isDual = 0; !validCaseGenerated && isDual <= 1; isDual++)
2720 for (int weightISEParamsNdx = 0; !validCaseGenerated && weightISEParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); weightISEParamsNdx++)
2721 for (int weightGridWidth = 2; !validCaseGenerated && weightGridWidth <= 12; weightGridWidth++)
2722 for (int weightGridHeight = 2; !validCaseGenerated && weightGridHeight <= 12; weightGridHeight++)
2723 {
2724 NormalBlockParams blockParams;
2725 blockParams.weightGridWidth = weightGridWidth;
2726 blockParams.weightGridHeight = weightGridHeight;
2727 blockParams.weightISEParams = s_weightISEParamsCandidates[weightISEParamsNdx];
2728 blockParams.isDualPlane = isDual != 0;
2729 blockParams.ccs = 0;
2730 blockParams.numPartitions = numPartitions;
2731 blockParams.isMultiPartSingleCemMode = true;
2732 blockParams.colorEndpointModes[0] = 12;
2733 blockParams.partitionSeed = 634;
2734
2735 if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
2736 {
2737 const ISEParams endpointISEParams = computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams),
2738 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, true));
2739
2740 if (computeISERangeMax(endpointISEParams) == endpointRangeMaximums[endpointRangeNdx])
2741 {
2742 validCaseGenerated = true;
2743
2744 const int numColorEndpoints = computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, blockParams.isMultiPartSingleCemMode);
2745 const int numValuesInISEBlock = endpointISEParams.mode == ISEMODE_TRIT ? 5 : endpointISEParams.mode == ISEMODE_QUINT ? 3 : 1;
2746
2747 {
2748 const int numColorEndpointValues = (int)computeISERangeMax(endpointISEParams) + 1;
2749 const int numBlocks = deDivRoundUp32(numColorEndpointValues, numColorEndpoints);
2750 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2751 iseInputs.endpoint.isGivenInBlockForm = false;
2752
2753 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2754 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2755 {
2756 for (int endpointNdx = 0; endpointNdx < numColorEndpoints; endpointNdx++)
2757 iseInputs.endpoint.value.plain[endpointNdx] = (blockNdx*numColorEndpoints + endpointNdx + offset) % numColorEndpointValues;
2758
2759 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2760 }
2761 }
2762
2763 if (endpointISEParams.mode == ISEMODE_TRIT || endpointISEParams.mode == ISEMODE_QUINT)
2764 {
2765 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2766 iseInputs.endpoint.isGivenInBlockForm = true;
2767
2768 const int numTQValues = 1 << (endpointISEParams.mode == ISEMODE_TRIT ? 8 : 7);
2769 const int numISEBlocksPerBlock = deDivRoundUp32(numColorEndpoints, numValuesInISEBlock);
2770 const int numBlocks = deDivRoundUp32(numTQValues, numISEBlocksPerBlock);
2771
2772 for (int offset = 0; offset < numValuesInISEBlock; offset++)
2773 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2774 {
2775 for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
2776 {
2777 for (int i = 0; i < numValuesInISEBlock; i++)
2778 iseInputs.endpoint.value.block[iseBlockNdx].bitValues[i] = 0;
2779 iseInputs.endpoint.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
2780 }
2781
2782 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
2783 }
2784 }
2785 }
2786 }
2787 }
2788
2789 DE_ASSERT(validCaseGenerated);
2790 }
2791
2792 break;
2793 }
2794
2795 case BLOCK_TEST_TYPE_CCS:
2796 // For all partition counts, test all values of the CCS (color component selector).
2797 {
2798 for (int numPartitions = 1; numPartitions <= 3; numPartitions++)
2799 for (deUint32 ccs = 0; ccs < 4; ccs++)
2800 {
2801 NormalBlockParams blockParams;
2802 blockParams.weightGridWidth = 3;
2803 blockParams.weightGridHeight = 3;
2804 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 2);
2805 blockParams.isDualPlane = true;
2806 blockParams.ccs = ccs;
2807 blockParams.numPartitions = numPartitions;
2808 blockParams.isMultiPartSingleCemMode = true;
2809 blockParams.colorEndpointModes[0] = 8;
2810 blockParams.partitionSeed = 634;
2811
2812 generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
2813 }
2814
2815 break;
2816 }
2817
2818 case BLOCK_TEST_TYPE_RANDOM:
2819 // Generate a number of random (including invalid) blocks.
2820 {
2821 const int numBlocks = 16384;
2822 const deUint32 seed = 1;
2823
2824 dst.resize(numBlocks*BLOCK_SIZE_BYTES);
2825
2826 generateRandomBlocks(&dst[0], numBlocks, format, seed);
2827
2828 break;
2829 }
2830
2831 default:
2832 DE_ASSERT(false);
2833 }
2834 }
2835
generateRandomBlocks(deUint8 * dst,size_t numBlocks,CompressedTexFormat format,deUint32 seed)2836 void generateRandomBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, deUint32 seed)
2837 {
2838 const IVec3 blockSize = getBlockPixelSize(format);
2839 de::Random rnd (seed);
2840 size_t numBlocksGenerated = 0;
2841
2842 DE_ASSERT(isAstcFormat(format));
2843 DE_ASSERT(blockSize.z() == 1);
2844
2845 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2846 {
2847 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2848
2849 generateRandomBlock(curBlockPtr, blockSize, rnd);
2850 }
2851 }
2852
generateRandomValidBlocks(deUint8 * dst,size_t numBlocks,CompressedTexFormat format,TexDecompressionParams::AstcMode mode,deUint32 seed)2853 void generateRandomValidBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, TexDecompressionParams::AstcMode mode, deUint32 seed)
2854 {
2855 const IVec3 blockSize = getBlockPixelSize(format);
2856 de::Random rnd (seed);
2857 size_t numBlocksGenerated = 0;
2858
2859 DE_ASSERT(isAstcFormat(format));
2860 DE_ASSERT(blockSize.z() == 1);
2861
2862 for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
2863 {
2864 deUint8* const curBlockPtr = dst + numBlocksGenerated*BLOCK_SIZE_BYTES;
2865
2866 do
2867 {
2868 generateRandomBlock(curBlockPtr, blockSize, rnd);
2869 } while (!isValidBlock(curBlockPtr, format, mode));
2870 }
2871 }
2872
2873 // Generate a number of trivial dummy blocks to fill unneeded space in a texture.
generateDummyVoidExtentBlocks(deUint8 * dst,size_t numBlocks)2874 void generateDummyVoidExtentBlocks (deUint8* dst, size_t numBlocks)
2875 {
2876 AssignBlock128 block = generateVoidExtentBlock(VoidExtentParams(false, 0, 0, 0, 0));
2877 for (size_t ndx = 0; ndx < numBlocks; ndx++)
2878 block.assignToMemory(&dst[ndx * BLOCK_SIZE_BYTES]);
2879 }
2880
generateDummyNormalBlocks(deUint8 * dst,size_t numBlocks,int blockWidth,int blockHeight)2881 void generateDummyNormalBlocks (deUint8* dst, size_t numBlocks, int blockWidth, int blockHeight)
2882 {
2883 NormalBlockParams blockParams;
2884
2885 blockParams.weightGridWidth = 3;
2886 blockParams.weightGridHeight = 3;
2887 blockParams.weightISEParams = ISEParams(ISEMODE_PLAIN_BIT, 5);
2888 blockParams.isDualPlane = false;
2889 blockParams.numPartitions = 1;
2890 blockParams.colorEndpointModes[0] = 8;
2891
2892 NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
2893 iseInputs.weight.isGivenInBlockForm = false;
2894
2895 const int numWeights = computeNumWeights(blockParams);
2896 const int weightRangeMax = computeISERangeMax(blockParams.weightISEParams);
2897
2898 for (size_t blockNdx = 0; blockNdx < numBlocks; blockNdx++)
2899 {
2900 for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
2901 iseInputs.weight.value.plain[weightNdx] = (deUint32)((blockNdx*numWeights + weightNdx) * weightRangeMax / (numBlocks*numWeights-1));
2902
2903 generateNormalBlock(blockParams, blockWidth, blockHeight, iseInputs).assignToMemory(dst + blockNdx*BLOCK_SIZE_BYTES);
2904 }
2905 }
2906
isValidBlock(const deUint8 * data,CompressedTexFormat format,TexDecompressionParams::AstcMode mode)2907 bool isValidBlock (const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2908 {
2909 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2910 const bool isSRGB = isAstcSRGBFormat(format);
2911 const bool isLDR = isSRGB || mode == TexDecompressionParams::ASTCMODE_LDR;
2912 union
2913 {
2914 deUint8 sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2915 float linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
2916 } tmpBuffer;
2917 const Block128 blockData (data);
2918 const DecompressResult result = decompressBlock((isSRGB ? (void*)&tmpBuffer.sRGB[0] : (void*)&tmpBuffer.linear[0]),
2919 blockData, blockPixelSize.x(), blockPixelSize.y(), isSRGB, isLDR);
2920
2921 return result == DECOMPRESS_RESULT_VALID_BLOCK;
2922 }
2923
decompress(const PixelBufferAccess & dst,const deUint8 * data,CompressedTexFormat format,TexDecompressionParams::AstcMode mode)2924 void decompress (const PixelBufferAccess& dst, const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
2925 {
2926 const bool isSRGBFormat = isAstcSRGBFormat(format);
2927
2928 #if defined(DE_DEBUG)
2929 const tcu::IVec3 blockPixelSize = getBlockPixelSize(format);
2930
2931 DE_ASSERT(dst.getWidth() == blockPixelSize.x() &&
2932 dst.getHeight() == blockPixelSize.y() &&
2933 dst.getDepth() == blockPixelSize.z());
2934 DE_ASSERT(mode == TexDecompressionParams::ASTCMODE_LDR || mode == TexDecompressionParams::ASTCMODE_HDR);
2935 #endif
2936
2937 decompress(dst, data, isSRGBFormat, isSRGBFormat || mode == TexDecompressionParams::ASTCMODE_LDR);
2938 }
2939
getBlockTestTypeName(BlockTestType testType)2940 const char* getBlockTestTypeName (BlockTestType testType)
2941 {
2942 switch (testType)
2943 {
2944 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "void_extent_ldr";
2945 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "void_extent_hdr";
2946 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "weight_grid";
2947 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "weight_ise";
2948 case BLOCK_TEST_TYPE_CEMS: return "color_endpoint_modes";
2949 case BLOCK_TEST_TYPE_PARTITION_SEED: return "partition_pattern_index";
2950 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "endpoint_value_ldr";
2951 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return "endpoint_value_hdr_cem_not_15";
2952 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "endpoint_value_hdr_cem_15";
2953 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "endpoint_ise";
2954 case BLOCK_TEST_TYPE_CCS: return "color_component_selector";
2955 case BLOCK_TEST_TYPE_RANDOM: return "random";
2956 default:
2957 DE_ASSERT(false);
2958 return DE_NULL;
2959 }
2960 }
2961
getBlockTestTypeDescription(BlockTestType testType)2962 const char* getBlockTestTypeDescription (BlockTestType testType)
2963 {
2964 switch (testType)
2965 {
2966 case BLOCK_TEST_TYPE_VOID_EXTENT_LDR: return "Test void extent block, LDR mode";
2967 case BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return "Test void extent block, HDR mode";
2968 case BLOCK_TEST_TYPE_WEIGHT_GRID: return "Test combinations of plane count, weight integer sequence encoding parameters, and weight grid size";
2969 case BLOCK_TEST_TYPE_WEIGHT_ISE: return "Test different integer sequence encoding block values for weight grid";
2970 case BLOCK_TEST_TYPE_CEMS: return "Test different color endpoint mode combinations, combined with different plane and partition counts";
2971 case BLOCK_TEST_TYPE_PARTITION_SEED: return "Test different partition pattern indices";
2972 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR: return "Test various combinations of each pair of color endpoint values, for each LDR color endpoint mode";
2973 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return "Test various combinations of each pair of color endpoint values, for each HDR color endpoint mode other than mode 15";
2974 case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return "Test various combinations of each pair of color endpoint values, HDR color endpoint mode 15";
2975 case BLOCK_TEST_TYPE_ENDPOINT_ISE: return "Test different integer sequence encoding block values for color endpoints";
2976 case BLOCK_TEST_TYPE_CCS: return "Test color component selector, for different partition counts";
2977 case BLOCK_TEST_TYPE_RANDOM: return "Random block test";
2978 default:
2979 DE_ASSERT(false);
2980 return DE_NULL;
2981 }
2982 }
2983
isBlockTestTypeHDROnly(BlockTestType testType)2984 bool isBlockTestTypeHDROnly (BlockTestType testType)
2985 {
2986 return testType == BLOCK_TEST_TYPE_VOID_EXTENT_HDR ||
2987 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15 ||
2988 testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15;
2989 }
2990
getBlockTestTypeColorScale(BlockTestType testType)2991 Vec4 getBlockTestTypeColorScale (BlockTestType testType)
2992 {
2993 switch (testType)
2994 {
2995 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f/65504.0f);
2996 case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15: return Vec4(1.0f/65504.0f, 1.0f/65504.0f, 1.0f/65504.0f, 1.0f);
2997 case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15: return Vec4(1.0f/65504.0f);
2998 default: return Vec4(1.0f);
2999 }
3000 }
3001
getBlockTestTypeColorBias(BlockTestType testType)3002 Vec4 getBlockTestTypeColorBias (BlockTestType testType)
3003 {
3004 switch (testType)
3005 {
3006 case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR: return Vec4(0.5f);
3007 default: return Vec4(0.0f);
3008 }
3009 }
3010
3011 } // astc
3012 } // tcu
3013