Path: blob/master/thirdparty/basis_universal/encoder/3rdparty/android_astc_decomp.cpp
9906 views
// File: android_astc_decomp.cpp12/*-------------------------------------------------------------------------3* drawElements Quality Program Tester Core4* ----------------------------------------5*6* Copyright 2016 The Android Open Source Project7*8* Licensed under the Apache License, Version 2.0 (the "License");9* you may not use this file except in compliance with the License.10* You may obtain a copy of the License at11*12* http://www.apache.org/licenses/LICENSE-2.013*14* Unless required by applicable law or agreed to in writing, software15* distributed under the License is distributed on an "AS IS" BASIS,16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.17* See the License for the specific language governing permissions and18* limitations under the License.19*20* rg: Removed external dependencies, minor fix to decompress() so it converts non-sRGB21* output to 8-bits correctly. I've compared this decoder's output22* vs. astc-codec with random inputs.23*24*//*!25* \file26* \brief ASTC Utilities.27*//*--------------------------------------------------------------------*/28#include "android_astc_decomp.h"29#include <assert.h>30#include <algorithm>31#include <fenv.h>32#include <math.h>3334#define DE_LENGTH_OF_ARRAY(x) (sizeof(x)/sizeof(x[0]))35#define DE_UNREF(x) (void)x3637typedef uint8_t deUint8;38typedef int8_t deInt8;39typedef uint32_t deUint32;40typedef int32_t deInt32;41typedef uint16_t deUint16;42typedef int16_t deInt16;43typedef int64_t deInt64;44typedef uint64_t deUint64;4546#define DE_ASSERT assert4748#ifdef _MSC_VER49#pragma warning (disable:4505) // unreferenced local function has been removed50#elif defined(__GNUC__)51#pragma GCC diagnostic push52#pragma GCC diagnostic ignored "-Wunused-function"53#endif5455namespace basisu_astc56{57template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }58template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }59template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }6061static bool inBounds(int v, int l, int h)62{63return (v >= l) && (v < h);64}6566static bool inRange(int v, int l, int h)67{68return (v >= l) && (v <= h);69}7071template<typename T>72static inline T max(T a, T b)73{74return (a > b) ? a : b;75}7677template<typename T>78static inline T min(T a, T b)79{80return (a < b) ? a : b;81}8283template<typename T>84static inline T clamp(T a, T l, T h)85{86if (a < l)87return l;88else if (a > h)89return h;90return a;91}9293struct UVec494{95uint32_t m_c[4];9697UVec4()98{99m_c[0] = 0;100m_c[1] = 0;101m_c[2] = 0;102m_c[3] = 0;103}104105UVec4(uint32_t x, uint32_t y, uint32_t z, uint32_t w)106{107m_c[0] = x;108m_c[1] = y;109m_c[2] = z;110m_c[3] = w;111}112113uint32_t x() const { return m_c[0]; }114uint32_t y() const { return m_c[1]; }115uint32_t z() const { return m_c[2]; }116uint32_t w() const { return m_c[3]; }117118uint32_t& x() { return m_c[0]; }119uint32_t& y() { return m_c[1]; }120uint32_t& z() { return m_c[2]; }121uint32_t& w() { return m_c[3]; }122123uint32_t operator[] (uint32_t idx) const { assert(idx < 4); return m_c[idx]; }124uint32_t& operator[] (uint32_t idx) { assert(idx < 4); return m_c[idx]; }125};126127struct IVec4128{129int32_t m_c[4];130131IVec4()132{133m_c[0] = 0;134m_c[1] = 0;135m_c[2] = 0;136m_c[3] = 0;137}138139IVec4(int32_t x, int32_t y, int32_t z, int32_t w)140{141m_c[0] = x;142m_c[1] = y;143m_c[2] = z;144m_c[3] = w;145}146147int32_t x() const { return m_c[0]; }148int32_t y() const { return m_c[1]; }149int32_t z() const { return m_c[2]; }150int32_t w() const { return m_c[3]; }151152int32_t& x() { return m_c[0]; }153int32_t& y() { return m_c[1]; }154int32_t& z() { return m_c[2]; }155int32_t& w() { return m_c[3]; }156157UVec4 asUint() const158{159return UVec4(maximum(0, m_c[0]), maximum(0, m_c[1]), maximum(0, m_c[2]), maximum(0, m_c[3]));160}161162int32_t operator[] (uint32_t idx) const { assert(idx < 4); return m_c[idx]; }163int32_t& operator[] (uint32_t idx) { assert(idx < 4); return m_c[idx]; }164};165166struct IVec3167{168int32_t m_c[3];169170IVec3()171{172m_c[0] = 0;173m_c[1] = 0;174m_c[2] = 0;175}176177IVec3(int32_t x, int32_t y, int32_t z)178{179m_c[0] = x;180m_c[1] = y;181m_c[2] = z;182}183184int32_t x() const { return m_c[0]; }185int32_t y() const { return m_c[1]; }186int32_t z() const { return m_c[2]; }187188int32_t& x() { return m_c[0]; }189int32_t& y() { return m_c[1]; }190int32_t& z() { return m_c[2]; }191192int32_t operator[] (uint32_t idx) const { assert(idx < 3); return m_c[idx]; }193int32_t& operator[] (uint32_t idx) { assert(idx < 3); return m_c[idx]; }194};195196static uint32_t deDivRoundUp32(uint32_t a, uint32_t b)197{198return (a + b - 1) / b;199}200201static bool deInBounds32(uint32_t v, uint32_t l, uint32_t h)202{203return (v >= l) && (v < h);204}205206namespace astc207{208209using std::vector;210211namespace212{213214// Common utilities215enum216{217MAX_BLOCK_WIDTH = 12,218MAX_BLOCK_HEIGHT = 12219};220221inline deUint32 getBit (deUint32 src, int ndx)222{223DE_ASSERT(basisu_astc::inBounds(ndx, 0, 32));224return (src >> ndx) & 1;225}226227inline deUint32 getBits (deUint32 src, int low, int high)228{229const int numBits = (high-low) + 1;230DE_ASSERT(basisu_astc::inRange(numBits, 1, 32));231232if (numBits < 32)233return (deUint32)((src >> low) & ((1u<<numBits)-1));234else235return (deUint32)((src >> low) & 0xFFFFFFFFu);236}237238inline bool isBitSet (deUint32 src, int ndx)239{240return getBit(src, ndx) != 0;241}242243inline deUint32 reverseBits (deUint32 src, int numBits)244{245DE_ASSERT(basisu_astc::inRange(numBits, 0, 32));246247deUint32 result = 0;248for (int i = 0; i < numBits; i++)249result |= ((src >> i) & 1) << (numBits-1-i);250251return result;252}253254inline deUint32 bitReplicationScale (deUint32 src, int numSrcBits, int numDstBits)255{256DE_ASSERT(numSrcBits <= numDstBits);257DE_ASSERT((src & ((1<<numSrcBits)-1)) == src);258259deUint32 dst = 0;260for (int shift = numDstBits-numSrcBits; shift > -numSrcBits; shift -= numSrcBits)261dst |= (shift >= 0) ? (src << shift) : (src >> -shift);262263return dst;264}265266inline deInt32 signExtend (deInt32 src, int numSrcBits)267{268DE_ASSERT(basisu_astc::inRange(numSrcBits, 2, 31));269270const bool negative = (src & (1 << (numSrcBits-1))) != 0;271return src | (negative ? ~((1 << numSrcBits) - 1) : 0);272}273274typedef uint16_t deFloat16;275276inline bool isFloat16InfOrNan (deFloat16 v)277{278return getBits(v, 10, 14) == 31;279}280281float deFloat16To32(deFloat16 val16)282{283deUint32 sign;284deUint32 expotent;285deUint32 mantissa;286287union288{289float f;290deUint32 u;291} x;292293x.u = 0u;294295sign = ((deUint32)val16 >> 15u) & 0x00000001u;296expotent = ((deUint32)val16 >> 10u) & 0x0000001fu;297mantissa = (deUint32)val16 & 0x000003ffu;298299if (expotent == 0u)300{301if (mantissa == 0u)302{303/* +/- 0 */304x.u = sign << 31u;305return x.f;306}307else308{309/* Denormalized, normalize it. */310311while (!(mantissa & 0x00000400u))312{313mantissa <<= 1u;314expotent -= 1u;315}316317expotent += 1u;318mantissa &= ~0x00000400u;319}320}321else if (expotent == 31u)322{323if (mantissa == 0u)324{325/* +/- InF */326x.u = (sign << 31u) | 0x7f800000u;327return x.f;328}329else330{331/* +/- NaN */332x.u = (sign << 31u) | 0x7f800000u | (mantissa << 13u);333return x.f;334}335}336337expotent = expotent + (127u - 15u);338mantissa = mantissa << 13u;339340x.u = (sign << 31u) | (expotent << 23u) | mantissa;341return x.f;342}343344enum ISEMode345{346ISEMODE_TRIT = 0,347ISEMODE_QUINT,348ISEMODE_PLAIN_BIT,349ISEMODE_LAST350};351352struct ISEParams353{354ISEMode mode;355int numBits;356ISEParams (ISEMode mode_, int numBits_) : mode(mode_), numBits(numBits_) {}357};358359inline int computeNumRequiredBits (const ISEParams& iseParams, int numValues)360{361switch (iseParams.mode)362{363case ISEMODE_TRIT: return deDivRoundUp32(numValues*8, 5) + numValues*iseParams.numBits;364case ISEMODE_QUINT: return deDivRoundUp32(numValues*7, 3) + numValues*iseParams.numBits;365case ISEMODE_PLAIN_BIT: return numValues*iseParams.numBits;366default:367DE_ASSERT(false);368return -1;369}370}371372ISEParams computeMaximumRangeISEParams (int numAvailableBits, int numValuesInSequence)373{374int curBitsForTritMode = 6;375int curBitsForQuintMode = 5;376int curBitsForPlainBitMode = 8;377378while (true)379{380DE_ASSERT(curBitsForTritMode > 0 || curBitsForQuintMode > 0 || curBitsForPlainBitMode > 0);381const int tritRange = (curBitsForTritMode > 0) ? (3 << curBitsForTritMode) - 1 : -1;382const int quintRange = (curBitsForQuintMode > 0) ? (5 << curBitsForQuintMode) - 1 : -1;383const int plainBitRange = (curBitsForPlainBitMode > 0) ? (1 << curBitsForPlainBitMode) - 1 : -1;384const int maxRange = basisu_astc::max(basisu_astc::max(tritRange, quintRange), plainBitRange);385386if (maxRange == tritRange)387{388const ISEParams params(ISEMODE_TRIT, curBitsForTritMode);389390if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)391return ISEParams(ISEMODE_TRIT, curBitsForTritMode);392393curBitsForTritMode--;394}395else if (maxRange == quintRange)396{397const ISEParams params(ISEMODE_QUINT, curBitsForQuintMode);398399if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)400return ISEParams(ISEMODE_QUINT, curBitsForQuintMode);401402curBitsForQuintMode--;403}404else405{406const ISEParams params(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);407DE_ASSERT(maxRange == plainBitRange);408409if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)410return ISEParams(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);411412curBitsForPlainBitMode--;413}414}415}416417inline int computeNumColorEndpointValues (deUint32 endpointMode)418{419DE_ASSERT(endpointMode < 16);420return (endpointMode/4 + 1) * 2;421}422423// Decompression utilities424enum DecompressResult425{426DECOMPRESS_RESULT_VALID_BLOCK = 0, //!< Decompressed valid block427DECOMPRESS_RESULT_ERROR, //!< Encountered error while decompressing, error color written428DECOMPRESS_RESULT_LAST429};430431// A helper for getting bits from a 128-bit block.432class Block128433{434private:435typedef deUint64 Word;436437enum438{439WORD_BYTES = sizeof(Word),440WORD_BITS = 8*WORD_BYTES,441NUM_WORDS = 128 / WORD_BITS442};443//DE_STATIC_ASSERT(128 % WORD_BITS == 0);444445public:446Block128 (const deUint8* src)447{448for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)449{450m_words[wordNdx] = 0;451for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)452m_words[wordNdx] |= (Word)src[wordNdx*WORD_BYTES + byteNdx] << (8*byteNdx);453}454}455456deUint32 getBit (int ndx) const457{458DE_ASSERT(basisu_astc::inBounds(ndx, 0, 128));459return (m_words[ndx / WORD_BITS] >> (ndx % WORD_BITS)) & 1;460}461462deUint32 getBits (int low, int high) const463{464DE_ASSERT(basisu_astc::inBounds(low, 0, 128));465DE_ASSERT(basisu_astc::inBounds(high, 0, 128));466DE_ASSERT(basisu_astc::inRange(high-low+1, 0, 32));467468if (high-low+1 == 0)469return 0;470471const int word0Ndx = low / WORD_BITS;472const int word1Ndx = high / WORD_BITS;473// \note "foo << bar << 1" done instead of "foo << (bar+1)" to avoid overflow, i.e. shift amount being too big.474if (word0Ndx == word1Ndx)475return (deUint32)((m_words[word0Ndx] & ((((Word)1 << high%WORD_BITS << 1) - 1))) >> ((Word)low % WORD_BITS));476else477{478DE_ASSERT(word1Ndx == word0Ndx + 1);479return (deUint32)(m_words[word0Ndx] >> (low%WORD_BITS)) |480(deUint32)((m_words[word1Ndx] & (((Word)1 << high%WORD_BITS << 1) - 1)) << (high-low - high%WORD_BITS));481}482}483484bool isBitSet (int ndx) const485{486DE_ASSERT(basisu_astc::inBounds(ndx, 0, 128));487return getBit(ndx) != 0;488}489490private:491Word m_words[NUM_WORDS];492};493494// A helper for sequential access into a Block128.495class BitAccessStream496{497public:498BitAccessStream (const Block128& src, int startNdxInSrc, int length, bool forward)499: m_src (src)500, m_startNdxInSrc (startNdxInSrc)501, m_length (length)502, m_forward (forward)503, m_ndx (0)504{505}506507// Get the next num bits. Bits at positions greater than or equal to m_length are zeros.508deUint32 getNext (int num)509{510if (num == 0 || m_ndx >= m_length)511return 0;512const int end = m_ndx + num;513const int numBitsFromSrc = basisu_astc::max(0, basisu_astc::min(m_length, end) - m_ndx);514const int low = m_ndx;515const int high = m_ndx + numBitsFromSrc - 1;516517m_ndx += num;518519return m_forward ? m_src.getBits(m_startNdxInSrc + low, m_startNdxInSrc + high)520: reverseBits(m_src.getBits(m_startNdxInSrc - high, m_startNdxInSrc - low), numBitsFromSrc);521}522523private:524const Block128& m_src;525const int m_startNdxInSrc;526const int m_length;527const bool m_forward;528int m_ndx;529};530531struct ISEDecodedResult532{533deUint32 m;534deUint32 tq; //!< Trit or quint value, depending on ISE mode.535deUint32 v;536};537538// Data from an ASTC block's "block mode" part (i.e. bits [0,10]).539struct ASTCBlockMode540{541bool isError;542// \note Following fields only relevant if !isError.543bool isVoidExtent;544// \note Following fields only relevant if !isVoidExtent.545bool isDualPlane;546int weightGridWidth;547int weightGridHeight;548ISEParams weightISEParams;549550ASTCBlockMode (void)551: isError (true)552, isVoidExtent (true)553, isDualPlane (true)554, weightGridWidth (-1)555, weightGridHeight (-1)556, weightISEParams (ISEMODE_LAST, -1)557{558}559};560561inline int computeNumWeights (const ASTCBlockMode& mode)562{563return mode.weightGridWidth * mode.weightGridHeight * (mode.isDualPlane ? 2 : 1);564}565566struct ColorEndpointPair567{568UVec4 e0;569UVec4 e1;570};571572struct TexelWeightPair573{574deUint32 w[2];575};576577ASTCBlockMode getASTCBlockMode (deUint32 blockModeData)578{579ASTCBlockMode blockMode;580blockMode.isError = true; // \note Set to false later, if not error.581blockMode.isVoidExtent = getBits(blockModeData, 0, 8) == 0x1fc;582if (!blockMode.isVoidExtent)583{584if ((getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 6, 8) == 7) || getBits(blockModeData, 0, 3) == 0)585return blockMode; // Invalid ("reserved").586587deUint32 r = (deUint32)-1; // \note Set in the following branches.588589if (getBits(blockModeData, 0, 1) == 0)590{591const deUint32 r0 = getBit(blockModeData, 4);592const deUint32 r1 = getBit(blockModeData, 2);593const deUint32 r2 = getBit(blockModeData, 3);594const deUint32 i78 = getBits(blockModeData, 7, 8);595596r = (r2 << 2) | (r1 << 1) | (r0 << 0);597598if (i78 == 3)599{600const bool i5 = isBitSet(blockModeData, 5);601blockMode.weightGridWidth = i5 ? 10 : 6;602blockMode.weightGridHeight = i5 ? 6 : 10;603}604else605{606const deUint32 a = getBits(blockModeData, 5, 6);607608switch (i78)609{610case 0: blockMode.weightGridWidth = 12; blockMode.weightGridHeight = a + 2; break;611case 1: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = 12; break;612case 2: blockMode.weightGridWidth = a + 6; blockMode.weightGridHeight = getBits(blockModeData, 9, 10) + 6; break;613default: DE_ASSERT(false);614}615}616}617else618{619const deUint32 r0 = getBit(blockModeData, 4);620const deUint32 r1 = getBit(blockModeData, 0);621const deUint32 r2 = getBit(blockModeData, 1);622const deUint32 i23 = getBits(blockModeData, 2, 3);623const deUint32 a = getBits(blockModeData, 5, 6);624625r = (r2 << 2) | (r1 << 1) | (r0 << 0);626if (i23 == 3)627{628const deUint32 b = getBit(blockModeData, 7);629const bool i8 = isBitSet(blockModeData, 8);630blockMode.weightGridWidth = i8 ? b+2 : a+2;631blockMode.weightGridHeight = i8 ? a+2 : b+6;632}633else634{635const deUint32 b = getBits(blockModeData, 7, 8);636switch (i23)637{638case 0: blockMode.weightGridWidth = b + 4; blockMode.weightGridHeight = a + 2; break;639case 1: blockMode.weightGridWidth = b + 8; blockMode.weightGridHeight = a + 2; break;640case 2: blockMode.weightGridWidth = a + 2; blockMode.weightGridHeight = b + 8; break;641default: DE_ASSERT(false);642}643}644}645646const bool zeroDH = getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 7, 8) == 2;647const bool h = zeroDH ? 0 : isBitSet(blockModeData, 9);648blockMode.isDualPlane = zeroDH ? 0 : isBitSet(blockModeData, 10);649650{651ISEMode& m = blockMode.weightISEParams.mode;652int& b = blockMode.weightISEParams.numBits;653m = ISEMODE_PLAIN_BIT;654b = 0;655if (h)656{657switch (r)658{659case 2: m = ISEMODE_QUINT; b = 1; break;660case 3: m = ISEMODE_TRIT; b = 2; break;661case 4: b = 4; break;662case 5: m = ISEMODE_QUINT; b = 2; break;663case 6: m = ISEMODE_TRIT; b = 3; break;664case 7: b = 5; break;665default: DE_ASSERT(false);666}667}668else669{670switch (r)671{672case 2: b = 1; break;673case 3: m = ISEMODE_TRIT; break;674case 4: b = 2; break;675case 5: m = ISEMODE_QUINT; break;676case 6: m = ISEMODE_TRIT; b = 1; break;677case 7: b = 3; break;678default: DE_ASSERT(false);679}680}681}682}683684blockMode.isError = false;685return blockMode;686}687688inline void setASTCErrorColorBlock (void* dst, int blockWidth, int blockHeight, bool isSRGB)689{690if (isSRGB)691{692deUint8* const dstU = (deUint8*)dst;693for (int i = 0; i < blockWidth*blockHeight; i++)694{695dstU[4*i + 0] = 0xff;696dstU[4*i + 1] = 0;697dstU[4*i + 2] = 0xff;698dstU[4*i + 3] = 0xff;699}700}701else702{703float* const dstF = (float*)dst;704for (int i = 0; i < blockWidth*blockHeight; i++)705{706dstF[4*i + 0] = 1.0f;707dstF[4*i + 1] = 0.0f;708dstF[4*i + 2] = 1.0f;709dstF[4*i + 3] = 1.0f;710}711}712}713714DecompressResult decodeVoidExtentBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode)715{716const deUint32 minSExtent = blockData.getBits(12, 24);717const deUint32 maxSExtent = blockData.getBits(25, 37);718const deUint32 minTExtent = blockData.getBits(38, 50);719const deUint32 maxTExtent = blockData.getBits(51, 63);720const bool allExtentsAllOnes = (minSExtent == 0x1fff) && (maxSExtent == 0x1fff) && (minTExtent == 0x1fff) && (maxTExtent == 0x1fff);721const bool isHDRBlock = blockData.isBitSet(9);722723if ((isLDRMode && isHDRBlock) || (!allExtentsAllOnes && (minSExtent >= maxSExtent || minTExtent >= maxTExtent)))724{725setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);726return DECOMPRESS_RESULT_ERROR;727}728729const deUint32 rgba[4] =730{731blockData.getBits(64, 79),732blockData.getBits(80, 95),733blockData.getBits(96, 111),734blockData.getBits(112, 127)735};736737if (isSRGB)738{739deUint8* const dstU = (deUint8*)dst;740for (int i = 0; i < blockWidth * blockHeight; i++)741{742for (int c = 0; c < 4; c++)743dstU[i * 4 + c] = (deUint8)((rgba[c] & 0xff00) >> 8);744}745}746else747{748float* const dstF = (float*)dst;749750if (isHDRBlock)751{752for (int c = 0; c < 4; c++)753{754if (isFloat16InfOrNan((deFloat16)rgba[c]))755{756//throw InternalError("Infinity or NaN color component in HDR void extent block in ASTC texture (behavior undefined by ASTC specification)");757setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);758return DECOMPRESS_RESULT_ERROR;759}760}761762for (int i = 0; i < blockWidth * blockHeight; i++)763{764for (int c = 0; c < 4; c++)765dstF[i * 4 + c] = deFloat16To32((deFloat16)rgba[c]);766}767}768else769{770for (int i = 0; i < blockWidth * blockHeight; i++)771{772for (int c = 0; c < 4; c++)773dstF[i * 4 + c] = (rgba[c] == 65535) ? 1.0f : ((float)rgba[c] / 65536.0f);774}775}776}777778return DECOMPRESS_RESULT_VALID_BLOCK;779}780781void decodeColorEndpointModes (deUint32* endpointModesDst, const Block128& blockData, int numPartitions, int extraCemBitsStart)782{783if (numPartitions == 1)784endpointModesDst[0] = blockData.getBits(13, 16);785else786{787const deUint32 highLevelSelector = blockData.getBits(23, 24);788789if (highLevelSelector == 0)790{791const deUint32 mode = blockData.getBits(25, 28);792793for (int i = 0; i < numPartitions; i++)794endpointModesDst[i] = mode;795}796else797{798for (int partNdx = 0; partNdx < numPartitions; partNdx++)799{800const deUint32 cemClass = highLevelSelector - (blockData.isBitSet(25 + partNdx) ? 0 : 1);801const deUint32 lowBit0Ndx = numPartitions + 2*partNdx;802const deUint32 lowBit1Ndx = numPartitions + 2*partNdx + 1;803const deUint32 lowBit0 = blockData.getBit(lowBit0Ndx < 4 ? 25+lowBit0Ndx : extraCemBitsStart+lowBit0Ndx-4);804const deUint32 lowBit1 = blockData.getBit(lowBit1Ndx < 4 ? 25+lowBit1Ndx : extraCemBitsStart+lowBit1Ndx-4);805806endpointModesDst[partNdx] = (cemClass << 2) | (lowBit1 << 1) | lowBit0;807}808}809}810}811812int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions)813{814int result = 0;815816for (int i = 0; i < numPartitions; i++)817result += computeNumColorEndpointValues(endpointModes[i]);818819return result;820}821822void decodeISETritBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)823{824DE_ASSERT(basisu_astc::inRange(numValues, 1, 5));825826deUint32 m[5];827m[0] = data.getNext(numBits);828deUint32 T01 = data.getNext(2);829m[1] = data.getNext(numBits);830deUint32 T23 = data.getNext(2);831m[2] = data.getNext(numBits);832deUint32 T4 = data.getNext(1);833m[3] = data.getNext(numBits);834deUint32 T56 = data.getNext(2);835m[4] = data.getNext(numBits);836deUint32 T7 = data.getNext(1);837838#ifndef __EMSCRIPTEN__839#ifdef __GNUC__840#pragma GCC diagnostic push841#pragma GCC diagnostic ignored "-Wimplicit-fallthrough="842#endif843#endif844switch (numValues)845{846// \note Fall-throughs.847case 1: T23 = 0;848case 2: T4 = 0;849case 3: T56 = 0;850case 4: T7 = 0;851case 5: break;852default:853DE_ASSERT(false);854}855#ifndef __EMSCRIPTEN__856#ifdef __GNUC__857#pragma GCC diagnostic pop858#endif859#endif860861const deUint32 T = (T7 << 7) | (T56 << 5) | (T4 << 4) | (T23 << 2) | (T01 << 0);862863static const deUint32 tritsFromT[256][5] =864{865{ 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 },866{ 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 },867{ 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 },868{ 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 },869{ 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 },870{ 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 },871{ 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 },872{ 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 },873{ 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 },874{ 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 },875{ 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 },876{ 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 },877{ 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 },878{ 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 },879{ 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 },880{ 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 }881};882883const deUint32 (& trits)[5] = tritsFromT[T];884for (int i = 0; i < numValues; i++)885{886dst[i].m = m[i];887dst[i].tq = trits[i];888dst[i].v = (trits[i] << numBits) + m[i];889}890}891892void decodeISEQuintBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)893{894DE_ASSERT(basisu_astc::inRange(numValues, 1, 3));895896deUint32 m[3];897m[0] = data.getNext(numBits);898deUint32 Q012 = data.getNext(3);899m[1] = data.getNext(numBits);900deUint32 Q34 = data.getNext(2);901m[2] = data.getNext(numBits);902deUint32 Q56 = data.getNext(2);903904#ifndef __EMSCRIPTEN__905#ifdef __GNUC__906#pragma GCC diagnostic push907#pragma GCC diagnostic ignored "-Wimplicit-fallthrough="908#endif909#endif910switch (numValues)911{912// \note Fall-throughs.913case 1: Q34 = 0;914case 2: Q56 = 0;915case 3: break;916default:917DE_ASSERT(false);918}919#ifndef __EMSCRIPTEN__920#ifdef __GNUC__921#pragma GCC diagnostic pop922#endif923#endif924925const deUint32 Q = (Q56 << 5) | (Q34 << 3) | (Q012 << 0);926927static const deUint32 quintsFromQ[256][3] =928{929{ 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 },930{ 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 },931{ 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 },932{ 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 },933{ 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 },934{ 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 },935{ 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 },936{ 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 }937};938939const deUint32 (& quints)[3] = quintsFromQ[Q];940for (int i = 0; i < numValues; i++)941{942dst[i].m = m[i];943dst[i].tq = quints[i];944dst[i].v = (quints[i] << numBits) + m[i];945}946}947948inline void decodeISEBitBlock (ISEDecodedResult* dst, BitAccessStream& data, int numBits)949{950dst[0].m = data.getNext(numBits);951dst[0].v = dst[0].m;952}953954void decodeISE (ISEDecodedResult* dst, int numValues, BitAccessStream& data, const ISEParams& params)955{956if (params.mode == ISEMODE_TRIT)957{958const int numBlocks = deDivRoundUp32(numValues, 5);959for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)960{961const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;962decodeISETritBlock(&dst[5*blockNdx], numValuesInBlock, data, params.numBits);963}964}965else if (params.mode == ISEMODE_QUINT)966{967const int numBlocks = deDivRoundUp32(numValues, 3);968for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)969{970const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;971decodeISEQuintBlock(&dst[3*blockNdx], numValuesInBlock, data, params.numBits);972}973}974else975{976DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);977for (int i = 0; i < numValues; i++)978decodeISEBitBlock(&dst[i], data, params.numBits);979}980}981982void unquantizeColorEndpoints (deUint32* dst, const ISEDecodedResult* iseResults, int numEndpoints, const ISEParams& iseParams)983{984if ((iseParams.mode == ISEMODE_TRIT) || (iseParams.mode == ISEMODE_QUINT))985{986const int rangeCase = iseParams.numBits*2 - (iseParams.mode == ISEMODE_TRIT ? 2 : 1);987DE_ASSERT(basisu_astc::inRange(rangeCase, 0, 10));988989static const deUint32 Ca[11] = { 204, 113, 93, 54, 44, 26, 22, 13, 11, 6, 5 };990const deUint32 C = Ca[rangeCase];991992for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)993{994const deUint32 a = getBit(iseResults[endpointNdx].m, 0);995const deUint32 b = getBit(iseResults[endpointNdx].m, 1);996const deUint32 c = getBit(iseResults[endpointNdx].m, 2);997const deUint32 d = getBit(iseResults[endpointNdx].m, 3);998const deUint32 e = getBit(iseResults[endpointNdx].m, 4);999const deUint32 f = getBit(iseResults[endpointNdx].m, 5);1000const deUint32 A = (a == 0) ? 0 : (1<<9)-1;10011002const deUint32 B = (rangeCase == 0) ? 01003: (rangeCase == 1) ? 01004: (rangeCase == 2) ? ((b << 8) | (b << 4) | (b << 2) | (b << 1))1005: (rangeCase == 3) ? ((b << 8) | (b << 3) | (b << 2))1006: (rangeCase == 4) ? ((c << 8) | (b << 7) | (c << 3) | (b << 2) | (c << 1) | (b << 0))1007: (rangeCase == 5) ? ((c << 8) | (b << 7) | (c << 2) | (b << 1) | (c << 0))1008: (rangeCase == 6) ? ((d << 8) | (c << 7) | (b << 6) | (d << 2) | (c << 1) | (b << 0))1009: (rangeCase == 7) ? ((d << 8) | (c << 7) | (b << 6) | (d << 1) | (c << 0))1010: (rangeCase == 8) ? ((e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 1) | (d << 0))1011: (rangeCase == 9) ? ((e << 8) | (d << 7) | (c << 6) | (b << 5) | (e << 0))1012: (rangeCase == 10) ? ((f << 8) | (e << 7) | (d << 6) | (c << 5) | (b << 4) | (f << 0))1013: (deUint32)-1;10141015DE_ASSERT(B != (deUint32)-1);1016dst[endpointNdx] = (((iseResults[endpointNdx].tq*C + B) ^ A) >> 2) | (A & 0x80);1017}1018}1019else1020{1021DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);1022for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)1023dst[endpointNdx] = bitReplicationScale(iseResults[endpointNdx].v, iseParams.numBits, 8);1024}1025}10261027inline void bitTransferSigned (deInt32& a, deInt32& b)1028{1029b >>= 1;1030b |= a & 0x80;1031a >>= 1;1032a &= 0x3f;1033if (isBitSet(a, 5))1034a -= 0x40;1035}10361037inline UVec4 clampedRGBA (const IVec4& rgba)1038{1039return UVec4(basisu_astc::clamp(rgba.x(), 0, 0xff),1040basisu_astc::clamp(rgba.y(), 0, 0xff),1041basisu_astc::clamp(rgba.z(), 0, 0xff),1042basisu_astc::clamp(rgba.w(), 0, 0xff));1043}10441045inline IVec4 blueContract (int r, int g, int b, int a)1046{1047return IVec4((r+b)>>1, (g+b)>>1, b, a);1048}10491050inline bool isColorEndpointModeHDR (deUint32 mode)1051{1052return (mode == 2) ||1053(mode == 3) ||1054(mode == 7) ||1055(mode == 11) ||1056(mode == 14) ||1057(mode == 15);1058}10591060void decodeHDREndpointMode7 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3)1061{1062const deUint32 m10 = getBit(v1, 7) | (getBit(v2, 7) << 1);1063const deUint32 m23 = getBits(v0, 6, 7);10641065const deUint32 majComp = (m10 != 3) ? m101066: (m23 != 3) ? m231067: 0;10681069const deUint32 mode = (m10 != 3) ? m231070: (m23 != 3) ? 41071: 5;10721073deInt32 red = (deInt32)getBits(v0, 0, 5);1074deInt32 green = (deInt32)getBits(v1, 0, 4);1075deInt32 blue = (deInt32)getBits(v2, 0, 4);1076deInt32 scale = (deInt32)getBits(v3, 0, 4);10771078{1079#define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)1080#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)10811082const deUint32 x0 = getBit(v1, 6);1083const deUint32 x1 = getBit(v1, 5);1084const deUint32 x2 = getBit(v2, 6);1085const deUint32 x3 = getBit(v2, 5);1086const deUint32 x4 = getBit(v3, 7);1087const deUint32 x5 = getBit(v3, 6);1088const deUint32 x6 = getBit(v3, 5);10891090deInt32& R = red;1091deInt32& G = green;1092deInt32& B = blue;1093deInt32& S = scale;10941095switch (mode)1096{1097case 0: ASSIGN_X_BITS(R,9, R,8, R,7, R,10, R,6, S,6, S,5); break;1098case 1: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, R,10, R,9); break;1099case 2: ASSIGN_X_BITS(R,9, R,8, R,7, R,6, S,7, S,6, S,5); break;1100case 3: ASSIGN_X_BITS(R,8, G,5, R,7, B,5, R,6, S,6, S,5); break;1101case 4: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, R,7, S,5); break;1102case 5: ASSIGN_X_BITS(G,6, G,5, B,6, B,5, R,6, S,6, S,5); break;1103default:1104DE_ASSERT(false);1105}1106#undef ASSIGN_X_BITS1107#undef SHOR1108}11091110static const int shiftAmounts[] = { 1, 1, 2, 3, 4, 5 };1111DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(shiftAmounts));11121113red <<= shiftAmounts[mode];1114green <<= shiftAmounts[mode];1115blue <<= shiftAmounts[mode];1116scale <<= shiftAmounts[mode];11171118if (mode != 5)1119{1120green = red - green;1121blue = red - blue;1122}11231124if (majComp == 1)1125std::swap(red, green);1126else if (majComp == 2)1127std::swap(red, blue);11281129e0 = UVec4(basisu_astc::clamp(red - scale, 0, 0xfff),1130basisu_astc::clamp(green - scale, 0, 0xfff),1131basisu_astc::clamp(blue - scale, 0, 0xfff),11320x780);11331134e1 = UVec4(basisu_astc::clamp(red, 0, 0xfff),1135basisu_astc::clamp(green, 0, 0xfff),1136basisu_astc::clamp(blue, 0, 0xfff),11370x780);1138}11391140void decodeHDREndpointMode11 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5)1141{1142const deUint32 major = (getBit(v5, 7) << 1) | getBit(v4, 7);11431144if (major == 3)1145{1146e0 = UVec4(v0<<4, v2<<4, getBits(v4,0,6)<<5, 0x780);1147e1 = UVec4(v1<<4, v3<<4, getBits(v5,0,6)<<5, 0x780);1148}1149else1150{1151const deUint32 mode = (getBit(v3, 7) << 2) | (getBit(v2, 7) << 1) | getBit(v1, 7);11521153deInt32 a = (deInt32)((getBit(v1, 6) << 8) | v0);1154deInt32 c = (deInt32)(getBits(v1, 0, 5));1155deInt32 b0 = (deInt32)(getBits(v2, 0, 5));1156deInt32 b1 = (deInt32)(getBits(v3, 0, 5));1157deInt32 d0 = (deInt32)(getBits(v4, 0, 4));1158deInt32 d1 = (deInt32)(getBits(v5, 0, 4));11591160{1161#define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)1162#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)1163const deUint32 x0 = getBit(v2, 6);1164const deUint32 x1 = getBit(v3, 6);1165const deUint32 x2 = getBit(v4, 6);1166const deUint32 x3 = getBit(v5, 6);1167const deUint32 x4 = getBit(v4, 5);1168const deUint32 x5 = getBit(v5, 5);11691170switch (mode)1171{1172case 0: ASSIGN_X_BITS(b0,6, b1,6, d0,6, d1,6, d0,5, d1,5); break;1173case 1: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, d0,5, d1,5); break;1174case 2: ASSIGN_X_BITS(a,9, c,6, d0,6, d1,6, d0,5, d1,5); break;1175case 3: ASSIGN_X_BITS(b0,6, b1,6, a,9, c,6, d0,5, d1,5); break;1176case 4: ASSIGN_X_BITS(b0,6, b1,6, b0,7, b1,7, a,9, a,10); break;1177case 5: ASSIGN_X_BITS(a,9, a,10, c,7, c,6, d0,5, d1,5); break;1178case 6: ASSIGN_X_BITS(b0,6, b1,6, a,11, c,6, a,9, a,10); break;1179case 7: ASSIGN_X_BITS(a,9, a,10, a,11, c,6, d0,5, d1,5); break;1180default:1181DE_ASSERT(false);1182}1183#undef ASSIGN_X_BITS1184#undef SHOR1185}11861187static const int numDBits[] = { 7, 6, 7, 6, 5, 6, 5, 6 };1188DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(numDBits));1189d0 = signExtend(d0, numDBits[mode]);1190d1 = signExtend(d1, numDBits[mode]);11911192const int shiftAmount = (mode >> 1) ^ 3;1193a = (uint32_t)a << shiftAmount;1194c = (uint32_t)c << shiftAmount;1195b0 = (uint32_t)b0 << shiftAmount;1196b1 = (uint32_t)b1 << shiftAmount;1197d0 = (uint32_t)d0 << shiftAmount;1198d1 = (uint32_t)d1 << shiftAmount;11991200e0 = 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);1201e1 = UVec4(basisu_astc::clamp(a, 0, 0xfff), basisu_astc::clamp(a-b0, 0, 0xfff), basisu_astc::clamp(a-b1, 0, 0xfff), 0x780);12021203if (major == 1)1204{1205std::swap(e0.x(), e0.y());1206std::swap(e1.x(), e1.y());1207}1208else if (major == 2)1209{1210std::swap(e0.x(), e0.z());1211std::swap(e1.x(), e1.z());1212}1213}1214}12151216void decodeHDREndpointMode15(UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5, deUint32 v6In, deUint32 v7In)1217{1218decodeHDREndpointMode11(e0, e1, v0, v1, v2, v3, v4, v5);12191220const deUint32 mode = (getBit(v7In, 7) << 1) | getBit(v6In, 7);1221deInt32 v6 = (deInt32)getBits(v6In, 0, 6);1222deInt32 v7 = (deInt32)getBits(v7In, 0, 6);12231224if (mode == 3)1225{1226e0.w() = v6 << 5;1227e1.w() = v7 << 5;1228}1229else1230{1231v6 |= (v7 << (mode+1)) & 0x780;1232v7 &= (0x3f >> mode);1233v7 ^= 0x20 >> mode;1234v7 -= 0x20 >> mode;1235v6 <<= 4-mode;1236v7 <<= 4-mode;1237v7 += v6;1238v7 = basisu_astc::clamp(v7, 0, 0xfff);1239e0.w() = v6;1240e1.w() = v7;1241}1242}12431244void decodeColorEndpoints (ColorEndpointPair* dst, const deUint32* unquantizedEndpoints, const deUint32* endpointModes, int numPartitions)1245{1246int unquantizedNdx = 0;12471248for (int partitionNdx = 0; partitionNdx < numPartitions; partitionNdx++)1249{1250const deUint32 endpointMode = endpointModes[partitionNdx];1251const deUint32* v = &unquantizedEndpoints[unquantizedNdx];12521253UVec4& e0 = dst[partitionNdx].e0;1254UVec4& e1 = dst[partitionNdx].e1;1255unquantizedNdx += computeNumColorEndpointValues(endpointMode);12561257switch (endpointMode)1258{1259case 0:1260{1261e0 = UVec4(v[0], v[0], v[0], 0xff);1262e1 = UVec4(v[1], v[1], v[1], 0xff);1263break;1264}1265case 1:1266{1267const deUint32 L0 = (v[0] >> 2) | (getBits(v[1], 6, 7) << 6);1268const deUint32 L1 = basisu_astc::min(0xffu, L0 + getBits(v[1], 0, 5));1269e0 = UVec4(L0, L0, L0, 0xff);1270e1 = UVec4(L1, L1, L1, 0xff);1271break;1272}1273case 2:1274{1275const deUint32 v1Gr = v[1] >= v[0];1276const deUint32 y0 = v1Gr ? v[0]<<4 : (v[1]<<4) + 8;1277const deUint32 y1 = v1Gr ? v[1]<<4 : (v[0]<<4) - 8;1278e0 = UVec4(y0, y0, y0, 0x780);1279e1 = UVec4(y1, y1, y1, 0x780);1280break;1281}1282case 3:1283{1284const bool m = isBitSet(v[0], 7);1285const deUint32 y0 = m ? (getBits(v[1], 5, 7) << 9) | (getBits(v[0], 0, 6) << 2)1286: (getBits(v[1], 4, 7) << 8) | (getBits(v[0], 0, 6) << 1);1287const deUint32 d = m ? getBits(v[1], 0, 4) << 21288: getBits(v[1], 0, 3) << 1;1289const deUint32 y1 = basisu_astc::min(0xfffu, y0+d);1290e0 = UVec4(y0, y0, y0, 0x780);1291e1 = UVec4(y1, y1, y1, 0x780);1292break;1293}1294case 4:1295{1296e0 = UVec4(v[0], v[0], v[0], v[2]);1297e1 = UVec4(v[1], v[1], v[1], v[3]);1298break;1299}1300case 5:1301{1302deInt32 v0 = (deInt32)v[0];1303deInt32 v1 = (deInt32)v[1];1304deInt32 v2 = (deInt32)v[2];1305deInt32 v3 = (deInt32)v[3];1306bitTransferSigned(v1, v0);1307bitTransferSigned(v3, v2);1308e0 = clampedRGBA(IVec4(v0, v0, v0, v2));1309e1 = clampedRGBA(IVec4(v0+v1, v0+v1, v0+v1, v2+v3));1310break;1311}1312case 6:1313e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, 0xff);1314e1 = UVec4(v[0], v[1], v[2], 0xff);1315break;1316case 7:1317decodeHDREndpointMode7(e0, e1, v[0], v[1], v[2], v[3]);1318break;1319case 8:1320{1321if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])1322{1323e0 = UVec4(v[0], v[2], v[4], 0xff);1324e1 = UVec4(v[1], v[3], v[5], 0xff);1325}1326else1327{1328e0 = blueContract(v[1], v[3], v[5], 0xff).asUint();1329e1 = blueContract(v[0], v[2], v[4], 0xff).asUint();1330}1331break;1332}1333case 9:1334{1335deInt32 v0 = (deInt32)v[0];1336deInt32 v1 = (deInt32)v[1];1337deInt32 v2 = (deInt32)v[2];1338deInt32 v3 = (deInt32)v[3];1339deInt32 v4 = (deInt32)v[4];1340deInt32 v5 = (deInt32)v[5];1341bitTransferSigned(v1, v0);1342bitTransferSigned(v3, v2);1343bitTransferSigned(v5, v4);1344if (v1+v3+v5 >= 0)1345{1346e0 = clampedRGBA(IVec4(v0, v2, v4, 0xff));1347e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, 0xff));1348}1349else1350{1351e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, 0xff));1352e1 = clampedRGBA(blueContract(v0, v2, v4, 0xff));1353}1354break;1355}1356case 10:1357{1358e0 = UVec4((v[0]*v[3]) >> 8, (v[1]*v[3]) >> 8, (v[2]*v[3]) >> 8, v[4]);1359e1 = UVec4(v[0], v[1], v[2], v[5]);1360break;1361}1362case 11:1363{1364decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);1365break;1366}1367case 12:1368{1369if (v[1] + v[3] + v[5] >= v[0] + v[2] + v[4])1370{1371e0 = UVec4(v[0], v[2], v[4], v[6]);1372e1 = UVec4(v[1], v[3], v[5], v[7]);1373}1374else1375{1376e0 = clampedRGBA(blueContract(v[1], v[3], v[5], v[7]));1377e1 = clampedRGBA(blueContract(v[0], v[2], v[4], v[6]));1378}1379break;1380}1381case 13:1382{1383deInt32 v0 = (deInt32)v[0];1384deInt32 v1 = (deInt32)v[1];1385deInt32 v2 = (deInt32)v[2];1386deInt32 v3 = (deInt32)v[3];1387deInt32 v4 = (deInt32)v[4];1388deInt32 v5 = (deInt32)v[5];1389deInt32 v6 = (deInt32)v[6];1390deInt32 v7 = (deInt32)v[7];1391bitTransferSigned(v1, v0);1392bitTransferSigned(v3, v2);1393bitTransferSigned(v5, v4);1394bitTransferSigned(v7, v6);1395if (v1+v3+v5 >= 0)1396{1397e0 = clampedRGBA(IVec4(v0, v2, v4, v6));1398e1 = clampedRGBA(IVec4(v0+v1, v2+v3, v4+v5, v6+v7));1399}1400else1401{1402e0 = clampedRGBA(blueContract(v0+v1, v2+v3, v4+v5, v6+v7));1403e1 = clampedRGBA(blueContract(v0, v2, v4, v6));1404}1405break;1406}1407case 14:1408decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);1409e0.w() = v[6];1410e1.w() = v[7];1411break;1412case 15:1413{1414decodeHDREndpointMode15(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);1415break;1416}1417default:1418DE_ASSERT(false);1419}1420}1421}14221423void computeColorEndpoints (ColorEndpointPair* dst, const Block128& blockData, const deUint32* endpointModes, int numPartitions, int numColorEndpointValues, const ISEParams& iseParams, int numBitsAvailable)1424{1425const int colorEndpointDataStart = (numPartitions == 1) ? 17 : 29;1426ISEDecodedResult colorEndpointData[18];14271428{1429BitAccessStream dataStream(blockData, colorEndpointDataStart, numBitsAvailable, true);1430decodeISE(&colorEndpointData[0], numColorEndpointValues, dataStream, iseParams);1431}14321433{1434deUint32 unquantizedEndpoints[18];1435unquantizeColorEndpoints(&unquantizedEndpoints[0], &colorEndpointData[0], numColorEndpointValues, iseParams);1436decodeColorEndpoints(dst, &unquantizedEndpoints[0], &endpointModes[0], numPartitions);1437}1438}14391440void unquantizeWeights (deUint32 dst[64], const ISEDecodedResult* weightGrid, const ASTCBlockMode& blockMode)1441{1442const int numWeights = computeNumWeights(blockMode);1443const ISEParams& iseParams = blockMode.weightISEParams;14441445if ((iseParams.mode == ISEMODE_TRIT) || (iseParams.mode == ISEMODE_QUINT))1446{1447const int rangeCase = iseParams.numBits*2 + (iseParams.mode == ISEMODE_QUINT ? 1 : 0);14481449if ((rangeCase == 0) || (rangeCase == 1))1450{1451static const deUint32 map0[3] = { 0, 32, 63 };1452static const deUint32 map1[5] = { 0, 16, 32, 47, 63 };1453const deUint32* const map = (rangeCase == 0) ? &map0[0] : &map1[0];14541455for (int i = 0; i < numWeights; i++)1456{1457DE_ASSERT(weightGrid[i].v < (rangeCase == 0 ? 3u : 5u));1458dst[i] = map[weightGrid[i].v];1459}1460}1461else1462{1463DE_ASSERT(rangeCase <= 6);1464static const deUint32 Ca[5] = { 50, 28, 23, 13, 11 };1465const deUint32 C = Ca[rangeCase-2];14661467for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)1468{1469const deUint32 a = getBit(weightGrid[weightNdx].m, 0);1470const deUint32 b = getBit(weightGrid[weightNdx].m, 1);1471const deUint32 c = getBit(weightGrid[weightNdx].m, 2);14721473const deUint32 A = (a == 0) ? 0 : (1<<7)-1;1474const deUint32 B = (rangeCase == 2) ? 01475: (rangeCase == 3) ? 01476: (rangeCase == 4) ? (b << 6) | (b << 2) | (b << 0)1477: (rangeCase == 5) ? (b << 6) | (b << 1)1478: (rangeCase == 6) ? (c << 6) | (b << 5) | (c << 1) | (b << 0)1479: (deUint32)-1;14801481dst[weightNdx] = (((weightGrid[weightNdx].tq*C + B) ^ A) >> 2) | (A & 0x20);1482}1483}1484}1485else1486{1487DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);1488for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)1489dst[weightNdx] = bitReplicationScale(weightGrid[weightNdx].v, iseParams.numBits, 6);1490}14911492for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)1493dst[weightNdx] += dst[weightNdx] > 32 ? 1 : 0;14941495// Initialize nonexistent weights to poison values1496for (int weightNdx = numWeights; weightNdx < 64; weightNdx++)1497dst[weightNdx] = ~0u;1498}14991500void interpolateWeights (TexelWeightPair* dst, const deUint32 (&unquantizedWeights) [64], int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)1501{1502const int numWeightsPerTexel = blockMode.isDualPlane ? 2 : 1;1503const deUint32 scaleX = (1024 + blockWidth/2) / (blockWidth-1);1504const deUint32 scaleY = (1024 + blockHeight/2) / (blockHeight-1);1505DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= (int)DE_LENGTH_OF_ARRAY(unquantizedWeights));15061507for (int texelY = 0; texelY < blockHeight; texelY++)1508{1509for (int texelX = 0; texelX < blockWidth; texelX++)1510{1511const deUint32 gX = (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;1512const deUint32 gY = (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;1513const deUint32 jX = gX >> 4;1514const deUint32 jY = gY >> 4;1515const deUint32 fX = gX & 0xf;1516const deUint32 fY = gY & 0xf;1517const deUint32 w11 = (fX*fY + 8) >> 4;1518const deUint32 w10 = fY - w11;1519const deUint32 w01 = fX - w11;1520const deUint32 w00 = 16 - fX - fY + w11;1521const deUint32 i00 = jY*blockMode.weightGridWidth + jX;1522const deUint32 i01 = i00 + 1;1523const deUint32 i10 = i00 + blockMode.weightGridWidth;1524const deUint32 i11 = i00 + blockMode.weightGridWidth + 1;15251526// These addresses can be out of bounds, but respective weights will be 0 then.1527DE_ASSERT(deInBounds32(i00, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w00 == 0);1528DE_ASSERT(deInBounds32(i01, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w01 == 0);1529DE_ASSERT(deInBounds32(i10, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w10 == 0);1530DE_ASSERT(deInBounds32(i11, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w11 == 0);15311532for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)1533{1534// & 0x3f clamps address to bounds of unquantizedWeights1535const deUint32 p00 = unquantizedWeights[(i00 * numWeightsPerTexel + texelWeightNdx) & 0x3f];1536const deUint32 p01 = unquantizedWeights[(i01 * numWeightsPerTexel + texelWeightNdx) & 0x3f];1537const deUint32 p10 = unquantizedWeights[(i10 * numWeightsPerTexel + texelWeightNdx) & 0x3f];1538const deUint32 p11 = unquantizedWeights[(i11 * numWeightsPerTexel + texelWeightNdx) & 0x3f];15391540dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;1541}1542}1543}1544}15451546void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)1547{1548ISEDecodedResult weightGrid[64];15491550{1551BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);1552decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);1553}15541555{1556deUint32 unquantizedWeights[64];1557unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);15581559interpolateWeights(dst, unquantizedWeights, blockWidth, blockHeight, blockMode);1560}1561}15621563inline deUint32 hash52 (deUint32 v)1564{1565deUint32 p = v;1566p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;1567p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;1568p ^= p << 6; p ^= p >> 17;1569return p;1570}15711572int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)1573{1574DE_ASSERT(zIn == 0);15751576const deUint32 x = smallBlock ? xIn << 1 : xIn;1577const deUint32 y = smallBlock ? yIn << 1 : yIn;1578const deUint32 z = smallBlock ? zIn << 1 : zIn;1579const deUint32 seed = seedIn + 1024*(numPartitions-1);1580const deUint32 rnum = hash52(seed);15811582deUint8 seed1 = (deUint8)( rnum & 0xf);1583deUint8 seed2 = (deUint8)((rnum >> 4) & 0xf);1584deUint8 seed3 = (deUint8)((rnum >> 8) & 0xf);1585deUint8 seed4 = (deUint8)((rnum >> 12) & 0xf);1586deUint8 seed5 = (deUint8)((rnum >> 16) & 0xf);1587deUint8 seed6 = (deUint8)((rnum >> 20) & 0xf);1588deUint8 seed7 = (deUint8)((rnum >> 24) & 0xf);1589deUint8 seed8 = (deUint8)((rnum >> 28) & 0xf);1590deUint8 seed9 = (deUint8)((rnum >> 18) & 0xf);1591deUint8 seed10 = (deUint8)((rnum >> 22) & 0xf);1592deUint8 seed11 = (deUint8)((rnum >> 26) & 0xf);1593deUint8 seed12 = (deUint8)(((rnum >> 30) | (rnum << 2)) & 0xf);15941595seed1 = (deUint8)(seed1 * seed1 );1596seed2 = (deUint8)(seed2 * seed2 );1597seed3 = (deUint8)(seed3 * seed3 );1598seed4 = (deUint8)(seed4 * seed4 );1599seed5 = (deUint8)(seed5 * seed5 );1600seed6 = (deUint8)(seed6 * seed6 );1601seed7 = (deUint8)(seed7 * seed7 );1602seed8 = (deUint8)(seed8 * seed8 );1603seed9 = (deUint8)(seed9 * seed9 );1604seed10 = (deUint8)(seed10 * seed10);1605seed11 = (deUint8)(seed11 * seed11);1606seed12 = (deUint8)(seed12 * seed12);16071608const int shA = (seed & 2) != 0 ? 4 : 5;1609const int shB = numPartitions == 3 ? 6 : 5;1610const int sh1 = (seed & 1) != 0 ? shA : shB;1611const int sh2 = (seed & 1) != 0 ? shB : shA;1612const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;16131614seed1 = (deUint8)(seed1 >> sh1);1615seed2 = (deUint8)(seed2 >> sh2);1616seed3 = (deUint8)(seed3 >> sh1);1617seed4 = (deUint8)(seed4 >> sh2);1618seed5 = (deUint8)(seed5 >> sh1);1619seed6 = (deUint8)(seed6 >> sh2);1620seed7 = (deUint8)(seed7 >> sh1);1621seed8 = (deUint8)(seed8 >> sh2);1622seed9 = (deUint8)(seed9 >> sh3);1623seed10 = (deUint8)(seed10 >> sh3);1624seed11 = (deUint8)(seed11 >> sh3);1625seed12 = (deUint8)(seed12 >> sh3);16261627const int a = 0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));1628const int b = 0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));1629const int c = (numPartitions >= 3) ? 0x3f & (seed5*x + seed6*y + seed9*z + (rnum >> 6)) : 0;1630const int d = (numPartitions >= 4) ? 0x3f & (seed7*x + seed8*y + seed10*z + (rnum >> 2)) : 0;16311632return (a >= b && a >= c && a >= d) ? 01633: (b >= c && b >= d) ? 11634: (c >= d) ? 21635: 3;1636}16371638DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,1639int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)1640{1641const bool smallBlock = blockWidth*blockHeight < 31;1642DecompressResult result = DECOMPRESS_RESULT_VALID_BLOCK;1643bool isHDREndpoint[4];16441645for (int i = 0; i < numPartitions; i++)1646{1647isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);1648}16491650for (int texelY = 0; texelY < blockHeight; texelY++)1651{1652for (int texelX = 0; texelX < blockWidth; texelX++)1653{1654const int texelNdx = texelY * blockWidth + texelX;1655const int colorEndpointNdx = (numPartitions == 1) ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);16561657DE_ASSERT(colorEndpointNdx < numPartitions);1658const UVec4& e0 = colorEndpoints[colorEndpointNdx].e0;1659const UVec4& e1 = colorEndpoints[colorEndpointNdx].e1;1660const TexelWeightPair& weight = texelWeights[texelNdx];16611662if (isLDRMode && isHDREndpoint[colorEndpointNdx])1663{1664if (isSRGB)1665{1666((deUint8*)dst)[texelNdx * 4 + 0] = 0xff;1667((deUint8*)dst)[texelNdx * 4 + 1] = 0;1668((deUint8*)dst)[texelNdx * 4 + 2] = 0xff;1669((deUint8*)dst)[texelNdx * 4 + 3] = 0xff;1670}1671else1672{1673((float*)dst)[texelNdx * 4 + 0] = 1.0f;1674((float*)dst)[texelNdx * 4 + 1] = 0;1675((float*)dst)[texelNdx * 4 + 2] = 1.0f;1676((float*)dst)[texelNdx * 4 + 3] = 1.0f;1677}1678result = DECOMPRESS_RESULT_ERROR;1679}1680else1681{1682for (int channelNdx = 0; channelNdx < 4; channelNdx++)1683{1684if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.1685{1686const deUint32 c0 = (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);1687const deUint32 c1 = (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);1688const deUint32 w = weight.w[ccs == channelNdx ? 1 : 0];1689const deUint32 c = (c0 * (64 - w) + c1 * w + 32) / 64;16901691if (isSRGB)1692((deUint8*)dst)[texelNdx * 4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);1693else1694((float*)dst)[texelNdx * 4 + channelNdx] = (c == 65535) ? 1.0f : (float)c / 65536.0f;1695}1696else1697{1698DE_ASSERT(!isSRGB);1699//DE_STATIC_ASSERT((basisu_astc::meta::TypesSame<deFloat16, deUint16>::Value));17001701const deUint32 c0 = e0[channelNdx] << 4;1702const deUint32 c1 = e1[channelNdx] << 4;1703const deUint32 w = weight.w[(ccs == channelNdx) ? 1 : 0];1704const deUint32 c = (c0 * (64 - w) + c1 * w + 32) / 64;1705const deUint32 e = getBits(c, 11, 15);1706const deUint32 m = getBits(c, 0, 10);1707const deUint32 mt = (m < 512) ? (3 * m)1708: (m >= 1536) ? (5 * m - 2048)1709: (4 * m - 512);17101711const deFloat16 cf = (deFloat16)((e << 10) + (mt >> 3));17121713((float*)dst)[texelNdx * 4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);1714}17151716} // channelNdx1717}1718} // texelX1719} // texelY17201721return result;1722}17231724DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)1725{1726DE_ASSERT(isLDR || !isSRGB);17271728// Decode block mode.1729const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));17301731// Check for block mode errors.1732if (blockMode.isError)1733{1734setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);1735return DECOMPRESS_RESULT_ERROR;1736}17371738// Separate path for void-extent.1739if (blockMode.isVoidExtent)1740return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);17411742// Compute weight grid values.1743const int numWeights = computeNumWeights(blockMode);1744const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);1745const int numPartitions = (int)blockData.getBits(11, 12) + 1;17461747// Check for errors in weight grid, partition and dual-plane parameters.1748if ((numWeights > 64) ||1749(numWeightDataBits > 96) ||1750(numWeightDataBits < 24) ||1751(blockMode.weightGridWidth > blockWidth) ||1752(blockMode.weightGridHeight > blockHeight) ||1753((numPartitions == 4) && blockMode.isDualPlane))1754{1755setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);1756return DECOMPRESS_RESULT_ERROR;1757}17581759// Compute number of bits available for color endpoint data.1760const bool isSingleUniqueCem = (numPartitions == 1) || (blockData.getBits(23, 24) == 0);17611762const int numConfigDataBits = ((numPartitions == 1) ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +1763(blockMode.isDualPlane ? 2 : 0);17641765const int numBitsForColorEndpoints = 128 - numWeightDataBits - numConfigDataBits;17661767const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -11768: (numPartitions == 4) ? 71769: (numPartitions == 3) ? 41770: (numPartitions == 2) ? 11771: 0);17721773// Decode color endpoint modes.1774deUint32 colorEndpointModes[4];1775decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);1776const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);17771778// Check for errors in color endpoint value count.1779if ((numColorEndpointValues > 18) || (numBitsForColorEndpoints < (int)deDivRoundUp32(13*numColorEndpointValues, 5)))1780{1781setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);1782return DECOMPRESS_RESULT_ERROR;1783}17841785// Compute color endpoints.1786ColorEndpointPair colorEndpoints[4];1787computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,1788computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);17891790// Compute texel weights.1791TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];1792computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);17931794// Set texel colors.1795const int ccs = blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;1796const deUint32 partitionIndexSeed = (numPartitions > 1) ? blockData.getBits(13, 22) : (deUint32)-1;17971798return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);1799}18001801// Returns -1 on error, 0 if LDR, 1 if HDR1802int isHDR(const Block128& blockData, int blockWidth, int blockHeight)1803{1804// Decode block mode.1805const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));18061807// Check for block mode errors.1808if (blockMode.isError)1809return -1;18101811// Separate path for void-extent.1812if (blockMode.isVoidExtent)1813{1814const bool isHDRBlock = blockData.isBitSet(9);1815return isHDRBlock ? 1 : 0;1816}18171818// Compute weight grid values.1819const int numWeights = computeNumWeights(blockMode);1820const int numWeightDataBits = computeNumRequiredBits(blockMode.weightISEParams, numWeights);1821const int numPartitions = (int)blockData.getBits(11, 12) + 1;18221823// Check for errors in weight grid, partition and dual-plane parameters.1824if ((numWeights > 64) ||1825(numWeightDataBits > 96) ||1826(numWeightDataBits < 24) ||1827(blockMode.weightGridWidth > blockWidth) ||1828(blockMode.weightGridHeight > blockHeight) ||1829((numPartitions == 4) && blockMode.isDualPlane))1830{1831return -1;1832}18331834// Compute number of bits available for color endpoint data.1835const bool isSingleUniqueCem = (numPartitions == 1) || (blockData.getBits(23, 24) == 0);18361837const int extraCemBitsStart = 127 - numWeightDataBits - (isSingleUniqueCem ? -11838: (numPartitions == 4) ? 71839: (numPartitions == 3) ? 41840: (numPartitions == 2) ? 11841: 0);18421843// Decode color endpoint modes.1844deUint32 colorEndpointModes[4];1845decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);18461847for (int i = 0; i < numPartitions; i++)1848{1849if (isColorEndpointModeHDR(colorEndpointModes[i]))1850return 1;1851}18521853return 0;1854}18551856typedef uint16_t half_float;18571858half_float float_to_half(float val, bool toward_zero)1859{1860union { float f; int32_t i; uint32_t u; } fi = { val };1861const int flt_m = fi.i & 0x7FFFFF, flt_e = (fi.i >> 23) & 0xFF, flt_s = (fi.i >> 31) & 0x1;1862int s = flt_s, e = 0, m = 0;18631864// inf/NaN1865if (flt_e == 0xff)1866{1867e = 31;1868if (flt_m != 0) // NaN1869m = 1;1870}1871// not zero or denormal1872else if (flt_e != 0)1873{1874int new_exp = flt_e - 127;1875if (new_exp > 15)1876e = 31;1877else if (new_exp < -14)1878{1879if (toward_zero)1880m = (int)truncf((1 << 24) * fabsf(fi.f));1881else1882m = lrintf((1 << 24) * fabsf(fi.f));1883}1884else1885{1886e = new_exp + 15;1887if (toward_zero)1888m = (int)truncf((float)flt_m * (1.0f / (float)(1 << 13)));1889else1890m = lrintf((float)flt_m * (1.0f / (float)(1 << 13)));1891}1892}18931894assert((0 <= m) && (m <= 1024));1895if (m == 1024)1896{1897e++;1898m = 0;1899}19001901assert((s >= 0) && (s <= 1));1902assert((e >= 0) && (e <= 31));1903assert((m >= 0) && (m <= 1023));19041905half_float result = (half_float)((s << 15) | (e << 10) | m);1906return result;1907}19081909float half_to_float(half_float hval)1910{1911union { float f; uint32_t u; } x = { 0 };19121913uint32_t s = ((uint32_t)hval >> 15) & 1;1914uint32_t e = ((uint32_t)hval >> 10) & 0x1F;1915uint32_t m = (uint32_t)hval & 0x3FF;19161917if (!e)1918{1919if (!m)1920{1921// +- 01922x.u = s << 31;1923return x.f;1924}1925else1926{1927// denormalized1928while (!(m & 0x00000400))1929{1930m <<= 1;1931--e;1932}19331934++e;1935m &= ~0x00000400;1936}1937}1938else if (e == 31)1939{1940if (m == 0)1941{1942// +/- INF1943x.u = (s << 31) | 0x7f800000;1944return x.f;1945}1946else1947{1948// +/- NaN1949x.u = (s << 31) | 0x7f800000 | (m << 13);1950return x.f;1951}1952}19531954e = e + (127 - 15);1955m = m << 13;19561957assert(s <= 1);1958assert(m <= 0x7FFFFF);1959assert(e <= 255);19601961x.u = m | (e << 23) | (s << 31);1962return x.f;1963}19641965} // anonymous19661967// See https://registry.khronos.org/DataFormat/specs/1.3/dataformat.1.3.inline.html#_hdr_endpoint_decoding1968static void convert_to_half_prec(uint32_t n, float* pVals)1969{1970#if 01971const int prev_dir = fesetround(FE_TOWARDZERO);19721973for (uint32_t i = 0; i < n; i++)1974pVals[i] = half_to_float(float_to_half(pVals[i]));19751976fesetround(prev_dir);19771978for (uint32_t i = 0; i < n; i++)1979{1980assert(pVals[i] == half_to_float(float_to_half(pVals[i], true)));1981}1982#else1983// This ensures the values are rounded towards zero as half floats.1984for (uint32_t i = 0; i < n; i++)1985{1986pVals[i] = half_to_float(float_to_half(pVals[i], true));1987}1988#endif1989}19901991bool decompress_ldr(uint8_t *pDst, const uint8_t * data, bool isSRGB, int blockWidth, int blockHeight)1992{1993float linear[MAX_BLOCK_WIDTH * MAX_BLOCK_HEIGHT * 4];19941995const Block128 blockData(data);19961997// isSRGB is true, this writes uint8_t's. Otherwise it writes floats.1998if (decompressBlock(isSRGB ? (void*)pDst : (void*)&linear[0], blockData, blockWidth, blockHeight, isSRGB, true) != DECOMPRESS_RESULT_VALID_BLOCK)1999{2000return false;2001}20022003if (!isSRGB)2004{2005// Convert the floats to 8-bits with rounding.2006int pix = 0;2007for (int i = 0; i < blockHeight; i++)2008{2009for (int j = 0; j < blockWidth; j++, pix++)2010{2011pDst[4 * pix + 0] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 0] * 65536.0f + .5f), 0, 65535) >> 8);2012pDst[4 * pix + 1] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 1] * 65536.0f + .5f), 0, 65535) >> 8);2013pDst[4 * pix + 2] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 2] * 65536.0f + .5f), 0, 65535) >> 8);2014pDst[4 * pix + 3] = (uint8_t)(basisu_astc::clamp<int>((int)(linear[pix * 4 + 3] * 65536.0f + .5f), 0, 65535) >> 8);2015}2016}2017}20182019return true;2020}20212022bool decompress_hdr(float* pDstRGBA, const uint8_t* data, int blockWidth, int blockHeight)2023{2024const Block128 blockData(data);20252026if (decompressBlock(pDstRGBA, blockData, blockWidth, blockHeight, false, false) != DECOMPRESS_RESULT_VALID_BLOCK)2027{2028return false;2029}20302031convert_to_half_prec(blockWidth * blockHeight * 4, pDstRGBA);20322033return true;2034}20352036bool is_hdr(const uint8_t* data, int blockWidth, int blockHeight, bool &is_hdr)2037{2038is_hdr = false;20392040const Block128 blockData(data);20412042int status = isHDR(blockData, blockWidth, blockHeight);2043if (status < 0)2044{2045return false;2046}20472048is_hdr = (status == 1);20492050return true;2051}20522053} // astc20542055} // basisu_astc20562057#if defined(__GNUC__)2058#pragma GCC diagnostic pop2059#endif206020612062