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