summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/video_core/textures/astc.cpp138
1 files changed, 60 insertions, 78 deletions
diff --git a/src/video_core/textures/astc.cpp b/src/video_core/textures/astc.cpp
index 3c4ad1c9d..b1feacae9 100644
--- a/src/video_core/textures/astc.cpp
+++ b/src/video_core/textures/astc.cpp
@@ -25,16 +25,15 @@
25 25
26class BitStream { 26class BitStream {
27public: 27public:
28 BitStream(unsigned char* ptr, int nBits = 0, int start_offset = 0) 28 explicit BitStream(unsigned char* ptr, int nBits = 0, int start_offset = 0)
29 : m_BitsWritten(0), m_BitsRead(0), m_NumBits(nBits), m_CurByte(ptr), 29 : m_NumBits(nBits), m_CurByte(ptr), m_NextBit(start_offset % 8) {}
30 m_NextBit(start_offset % 8), done(false) {} 30
31 ~BitStream() = default;
31 32
32 int GetBitsWritten() const { 33 int GetBitsWritten() const {
33 return m_BitsWritten; 34 return m_BitsWritten;
34 } 35 }
35 36
36 ~BitStream() {}
37
38 void WriteBitsR(unsigned int val, unsigned int nBits) { 37 void WriteBitsR(unsigned int val, unsigned int nBits) {
39 for (unsigned int i = 0; i < nBits; i++) { 38 for (unsigned int i = 0; i < nBits; i++) {
40 WriteBit((val >> (nBits - i - 1)) & 1); 39 WriteBit((val >> (nBits - i - 1)) & 1);
@@ -95,33 +94,28 @@ private:
95 done = done || ++m_BitsWritten >= m_NumBits; 94 done = done || ++m_BitsWritten >= m_NumBits;
96 } 95 }
97 96
98 int m_BitsWritten; 97 int m_BitsWritten = 0;
99 const int m_NumBits; 98 const int m_NumBits;
100 unsigned char* m_CurByte; 99 unsigned char* m_CurByte;
101 int m_NextBit; 100 int m_NextBit = 0;
102 int m_BitsRead; 101 int m_BitsRead = 0;
103 102
104 bool done; 103 bool done = false;
105}; 104};
106 105
107template <typename IntType> 106template <typename IntType>
108class Bits { 107class Bits {
109private:
110 const IntType& m_Bits;
111
112 // Don't copy
113 Bits() {}
114 Bits(const Bits&) {}
115 Bits& operator=(const Bits&) {}
116
117public: 108public:
118 explicit Bits(IntType& v) : m_Bits(v) {} 109 explicit Bits(const IntType& v) : m_Bits(v) {}
119 110
120 uint8_t operator[](uint32_t bitPos) { 111 Bits(const Bits&) = delete;
112 Bits& operator=(const Bits&) = delete;
113
114 uint8_t operator[](uint32_t bitPos) const {
121 return static_cast<uint8_t>((m_Bits >> bitPos) & 1); 115 return static_cast<uint8_t>((m_Bits >> bitPos) & 1);
122 } 116 }
123 117
124 IntType operator()(uint32_t start, uint32_t end) { 118 IntType operator()(uint32_t start, uint32_t end) const {
125 if (start == end) { 119 if (start == end) {
126 return (*this)[start]; 120 return (*this)[start];
127 } else if (start > end) { 121 } else if (start > end) {
@@ -133,6 +127,9 @@ public:
133 uint64_t mask = (1 << (end - start + 1)) - 1; 127 uint64_t mask = (1 << (end - start + 1)) - 1;
134 return (m_Bits >> start) & mask; 128 return (m_Bits >> start) & mask;
135 } 129 }
130
131private:
132 const IntType& m_Bits;
136}; 133};
137 134
138enum EIntegerEncoding { eIntegerEncoding_JustBits, eIntegerEncoding_Quint, eIntegerEncoding_Trit }; 135enum EIntegerEncoding { eIntegerEncoding_JustBits, eIntegerEncoding_Quint, eIntegerEncoding_Trit };
@@ -186,12 +183,12 @@ public:
186 m_QuintValue = val; 183 m_QuintValue = val;
187 } 184 }
188 185
189 bool MatchesEncoding(const IntegerEncodedValue& other) { 186 bool MatchesEncoding(const IntegerEncodedValue& other) const {
190 return m_Encoding == other.m_Encoding && m_NumBits == other.m_NumBits; 187 return m_Encoding == other.m_Encoding && m_NumBits == other.m_NumBits;
191 } 188 }
192 189
193 // Returns the number of bits required to encode nVals values. 190 // Returns the number of bits required to encode nVals values.
194 uint32_t GetBitLength(uint32_t nVals) { 191 uint32_t GetBitLength(uint32_t nVals) const {
195 uint32_t totalBits = m_NumBits * nVals; 192 uint32_t totalBits = m_NumBits * nVals;
196 if (m_Encoding == eIntegerEncoding_Trit) { 193 if (m_Encoding == eIntegerEncoding_Trit) {
197 totalBits += (nVals * 8 + 4) / 5; 194 totalBits += (nVals * 8 + 4) / 5;
@@ -382,19 +379,15 @@ private:
382namespace ASTCC { 379namespace ASTCC {
383 380
384struct TexelWeightParams { 381struct TexelWeightParams {
385 uint32_t m_Width; 382 uint32_t m_Width = 0;
386 uint32_t m_Height; 383 uint32_t m_Height = 0;
387 bool m_bDualPlane; 384 bool m_bDualPlane = false;
388 uint32_t m_MaxWeight; 385 uint32_t m_MaxWeight = 0;
389 bool m_bError; 386 bool m_bError = false;
390 bool m_bVoidExtentLDR; 387 bool m_bVoidExtentLDR = false;
391 bool m_bVoidExtentHDR; 388 bool m_bVoidExtentHDR = false;
392 389
393 TexelWeightParams() { 390 uint32_t GetPackedBitSize() const {
394 memset(this, 0, sizeof(*this));
395 }
396
397 uint32_t GetPackedBitSize() {
398 // How many indices do we have? 391 // How many indices do we have?
399 uint32_t nIdxs = m_Height * m_Width; 392 uint32_t nIdxs = m_Height * m_Width;
400 if (m_bDualPlane) { 393 if (m_bDualPlane) {
@@ -413,7 +406,7 @@ struct TexelWeightParams {
413 } 406 }
414}; 407};
415 408
416TexelWeightParams DecodeBlockInfo(BitStream& strm) { 409static TexelWeightParams DecodeBlockInfo(BitStream& strm) {
417 TexelWeightParams params; 410 TexelWeightParams params;
418 411
419 // Read the entire block mode all at once 412 // Read the entire block mode all at once
@@ -612,8 +605,8 @@ TexelWeightParams DecodeBlockInfo(BitStream& strm) {
612 return params; 605 return params;
613} 606}
614 607
615void FillVoidExtentLDR(BitStream& strm, uint32_t* const outBuf, uint32_t blockWidth, 608static void FillVoidExtentLDR(BitStream& strm, uint32_t* const outBuf, uint32_t blockWidth,
616 uint32_t blockHeight) { 609 uint32_t blockHeight) {
617 // Don't actually care about the void extent, just read the bits... 610 // Don't actually care about the void extent, just read the bits...
618 for (int i = 0; i < 4; ++i) { 611 for (int i = 0; i < 4; ++i) {
619 strm.ReadBits(13); 612 strm.ReadBits(13);
@@ -628,23 +621,25 @@ void FillVoidExtentLDR(BitStream& strm, uint32_t* const outBuf, uint32_t blockWi
628 uint32_t rgba = (r >> 8) | (g & 0xFF00) | (static_cast<uint32_t>(b) & 0xFF00) << 8 | 621 uint32_t rgba = (r >> 8) | (g & 0xFF00) | (static_cast<uint32_t>(b) & 0xFF00) << 8 |
629 (static_cast<uint32_t>(a) & 0xFF00) << 16; 622 (static_cast<uint32_t>(a) & 0xFF00) << 16;
630 623
631 for (uint32_t j = 0; j < blockHeight; j++) 624 for (uint32_t j = 0; j < blockHeight; j++) {
632 for (uint32_t i = 0; i < blockWidth; i++) { 625 for (uint32_t i = 0; i < blockWidth; i++) {
633 outBuf[j * blockWidth + i] = rgba; 626 outBuf[j * blockWidth + i] = rgba;
634 } 627 }
628 }
635} 629}
636 630
637void FillError(uint32_t* outBuf, uint32_t blockWidth, uint32_t blockHeight) { 631static void FillError(uint32_t* outBuf, uint32_t blockWidth, uint32_t blockHeight) {
638 for (uint32_t j = 0; j < blockHeight; j++) 632 for (uint32_t j = 0; j < blockHeight; j++) {
639 for (uint32_t i = 0; i < blockWidth; i++) { 633 for (uint32_t i = 0; i < blockWidth; i++) {
640 outBuf[j * blockWidth + i] = 0xFFFF00FF; 634 outBuf[j * blockWidth + i] = 0xFFFF00FF;
641 } 635 }
636 }
642} 637}
643 638
644// Replicates low numBits such that [(toBit - 1):(toBit - 1 - fromBit)] 639// Replicates low numBits such that [(toBit - 1):(toBit - 1 - fromBit)]
645// is the same as [(numBits - 1):0] and repeats all the way down. 640// is the same as [(numBits - 1):0] and repeats all the way down.
646template <typename IntType> 641template <typename IntType>
647IntType Replicate(const IntType& val, uint32_t numBits, uint32_t toBit) { 642static IntType Replicate(const IntType& val, uint32_t numBits, uint32_t toBit) {
648 if (numBits == 0) 643 if (numBits == 0)
649 return 0; 644 return 0;
650 if (toBit == 0) 645 if (toBit == 0)
@@ -668,27 +663,15 @@ IntType Replicate(const IntType& val, uint32_t numBits, uint32_t toBit) {
668 663
669class Pixel { 664class Pixel {
670protected: 665protected:
671 typedef int16_t ChannelType; 666 using ChannelType = int16_t;
672 uint8_t m_BitDepth[4]; 667 uint8_t m_BitDepth[4] = {8, 8, 8, 8};
673 int16_t color[4]; 668 int16_t color[4] = {};
674 669
675public: 670public:
676 Pixel() { 671 Pixel() = default;
677 for (int i = 0; i < 4; i++) { 672 Pixel(ChannelType a, ChannelType r, ChannelType g, ChannelType b, unsigned bitDepth = 8)
678 m_BitDepth[i] = 8; 673 : m_BitDepth{uint8_t(bitDepth), uint8_t(bitDepth), uint8_t(bitDepth), uint8_t(bitDepth)},
679 color[i] = 0; 674 color{a, r, g, b} {}
680 }
681 }
682
683 Pixel(ChannelType a, ChannelType r, ChannelType g, ChannelType b, unsigned bitDepth = 8) {
684 for (int i = 0; i < 4; i++)
685 m_BitDepth[i] = bitDepth;
686
687 color[0] = a;
688 color[1] = r;
689 color[2] = g;
690 color[3] = b;
691 }
692 675
693 // Changes the depth of each pixel. This scales the values to 676 // Changes the depth of each pixel. This scales the values to
694 // the appropriate bit depth by either truncating the least 677 // the appropriate bit depth by either truncating the least
@@ -807,8 +790,8 @@ public:
807 } 790 }
808}; 791};
809 792
810void DecodeColorValues(uint32_t* out, uint8_t* data, uint32_t* modes, const uint32_t nPartitions, 793static void DecodeColorValues(uint32_t* out, uint8_t* data, const uint32_t* modes,
811 const uint32_t nBitsForColorData) { 794 const uint32_t nPartitions, const uint32_t nBitsForColorData) {
812 // First figure out how many color values we have 795 // First figure out how many color values we have
813 uint32_t nValues = 0; 796 uint32_t nValues = 0;
814 for (uint32_t i = 0; i < nPartitions; i++) { 797 for (uint32_t i = 0; i < nPartitions; i++) {
@@ -844,8 +827,7 @@ void DecodeColorValues(uint32_t* out, uint8_t* data, uint32_t* modes, const uint
844 // Once we have the decoded values, we need to dequantize them to the 0-255 range 827 // Once we have the decoded values, we need to dequantize them to the 0-255 range
845 // This procedure is outlined in ASTC spec C.2.13 828 // This procedure is outlined in ASTC spec C.2.13
846 uint32_t outIdx = 0; 829 uint32_t outIdx = 0;
847 std::vector<IntegerEncodedValue>::const_iterator itr; 830 for (auto itr = decodedColorValues.begin(); itr != decodedColorValues.end(); ++itr) {
848 for (itr = decodedColorValues.begin(); itr != decodedColorValues.end(); itr++) {
849 // Have we already decoded all that we need? 831 // Have we already decoded all that we need?
850 if (outIdx >= nValues) { 832 if (outIdx >= nValues) {
851 break; 833 break;
@@ -978,7 +960,7 @@ void DecodeColorValues(uint32_t* out, uint8_t* data, uint32_t* modes, const uint
978 } 960 }
979} 961}
980 962
981uint32_t UnquantizeTexelWeight(const IntegerEncodedValue& val) { 963static uint32_t UnquantizeTexelWeight(const IntegerEncodedValue& val) {
982 uint32_t bitval = val.GetBitValue(); 964 uint32_t bitval = val.GetBitValue();
983 uint32_t bitlen = val.BaseBitLength(); 965 uint32_t bitlen = val.BaseBitLength();
984 966
@@ -1067,17 +1049,18 @@ uint32_t UnquantizeTexelWeight(const IntegerEncodedValue& val) {
1067 return result; 1049 return result;
1068} 1050}
1069 1051
1070void UnquantizeTexelWeights(uint32_t out[2][144], std::vector<IntegerEncodedValue>& weights, 1052static void UnquantizeTexelWeights(uint32_t out[2][144],
1071 const TexelWeightParams& params, const uint32_t blockWidth, 1053 const std::vector<IntegerEncodedValue>& weights,
1072 const uint32_t blockHeight) { 1054 const TexelWeightParams& params, const uint32_t blockWidth,
1055 const uint32_t blockHeight) {
1073 uint32_t weightIdx = 0; 1056 uint32_t weightIdx = 0;
1074 uint32_t unquantized[2][144]; 1057 uint32_t unquantized[2][144];
1075 std::vector<IntegerEncodedValue>::const_iterator itr; 1058
1076 for (itr = weights.begin(); itr != weights.end(); itr++) { 1059 for (auto itr = weights.begin(); itr != weights.end(); ++itr) {
1077 unquantized[0][weightIdx] = UnquantizeTexelWeight(*itr); 1060 unquantized[0][weightIdx] = UnquantizeTexelWeight(*itr);
1078 1061
1079 if (params.m_bDualPlane) { 1062 if (params.m_bDualPlane) {
1080 itr++; 1063 ++itr;
1081 unquantized[1][weightIdx] = UnquantizeTexelWeight(*itr); 1064 unquantized[1][weightIdx] = UnquantizeTexelWeight(*itr);
1082 if (itr == weights.end()) { 1065 if (itr == weights.end()) {
1083 break; 1066 break;
@@ -1261,8 +1244,8 @@ static inline uint32_t Select2DPartition(int32_t seed, int32_t x, int32_t y, int
1261} 1244}
1262 1245
1263// Section C.2.14 1246// Section C.2.14
1264void ComputeEndpoints(Pixel& ep1, Pixel& ep2, const uint32_t*& colorValues, 1247static void ComputeEndpoints(Pixel& ep1, Pixel& ep2, const uint32_t*& colorValues,
1265 uint32_t colorEndpointMode) { 1248 uint32_t colorEndpointMode) {
1266#define READ_UINT_VALUES(N) \ 1249#define READ_UINT_VALUES(N) \
1267 uint32_t v[N]; \ 1250 uint32_t v[N]; \
1268 for (uint32_t i = 0; i < N; i++) { \ 1251 for (uint32_t i = 0; i < N; i++) { \
@@ -1382,8 +1365,8 @@ void ComputeEndpoints(Pixel& ep1, Pixel& ep2, const uint32_t*& colorValues,
1382#undef READ_INT_VALUES 1365#undef READ_INT_VALUES
1383} 1366}
1384 1367
1385void DecompressBlock(uint8_t inBuf[16], const uint32_t blockWidth, const uint32_t blockHeight, 1368static void DecompressBlock(uint8_t inBuf[16], const uint32_t blockWidth,
1386 uint32_t* outBuf) { 1369 const uint32_t blockHeight, uint32_t* outBuf) {
1387 BitStream strm(inBuf); 1370 BitStream strm(inBuf);
1388 TexelWeightParams weightParams = DecodeBlockInfo(strm); 1371 TexelWeightParams weightParams = DecodeBlockInfo(strm);
1389 1372
@@ -1617,8 +1600,7 @@ namespace Tegra::Texture::ASTC {
1617std::vector<uint8_t> Decompress(std::vector<uint8_t>& data, uint32_t width, uint32_t height, 1600std::vector<uint8_t> Decompress(std::vector<uint8_t>& data, uint32_t width, uint32_t height,
1618 uint32_t block_width, uint32_t block_height) { 1601 uint32_t block_width, uint32_t block_height) {
1619 uint32_t blockIdx = 0; 1602 uint32_t blockIdx = 0;
1620 std::vector<uint8_t> outData; 1603 std::vector<uint8_t> outData(height * width * 4);
1621 outData.resize(height * width * 4);
1622 for (uint32_t j = 0; j < height; j += block_height) { 1604 for (uint32_t j = 0; j < height; j += block_height) {
1623 for (uint32_t i = 0; i < width; i += block_width) { 1605 for (uint32_t i = 0; i < width; i += block_width) {
1624 1606