early-access version 1786

This commit is contained in:
pineappleEA
2021-06-14 16:57:42 +02:00
parent 53c119c6c4
commit 0155a19876
27 changed files with 473 additions and 216 deletions

View File

@@ -24,22 +24,8 @@
#include <boost/container/static_vector.hpp>
#include "common/common_types.h"
#include "video_core/textures/astc.h"
namespace {
/// Count the number of bits set in a number.
constexpr u32 Popcnt(u32 n) {
u32 c = 0;
for (; n; c++) {
n &= n - 1;
}
return c;
}
} // Anonymous namespace
class InputBitStream {
public:
constexpr explicit InputBitStream(std::span<const u8> data, size_t start_offset = 0)
@@ -165,37 +151,7 @@ private:
const IntType& m_Bits;
};
enum class IntegerEncoding { JustBits, Qus32, Trit };
struct IntegerEncodedValue {
constexpr IntegerEncodedValue() = default;
constexpr IntegerEncodedValue(IntegerEncoding encoding_, u32 num_bits_)
: encoding{encoding_}, num_bits{num_bits_} {}
constexpr bool MatchesEncoding(const IntegerEncodedValue& other) const {
return encoding == other.encoding && num_bits == other.num_bits;
}
// Returns the number of bits required to encode nVals values.
u32 GetBitLength(u32 nVals) const {
u32 totalBits = num_bits * nVals;
if (encoding == IntegerEncoding::Trit) {
totalBits += (nVals * 8 + 4) / 5;
} else if (encoding == IntegerEncoding::Qus32) {
totalBits += (nVals * 7 + 2) / 3;
}
return totalBits;
}
IntegerEncoding encoding{};
u32 num_bits = 0;
u32 bit_value = 0;
union {
u32 qus32_value = 0;
u32 trit_value;
};
};
namespace Tegra::Texture::ASTC {
using IntegerEncodedVector = boost::container::static_vector<
IntegerEncodedValue, 256,
boost::container::static_vector_options<
@@ -260,7 +216,7 @@ static void DecodeTritBlock(InputBitStream& bits, IntegerEncodedVector& result,
}
}
static void DecodeQus32Block(InputBitStream& bits, IntegerEncodedVector& result,
static void DecodeQuintBlock(InputBitStream& bits, IntegerEncodedVector& result,
u32 nBitsPerValue) {
// Implement the algorithm in section C.2.12
u32 m[3];
@@ -301,50 +257,12 @@ static void DecodeQus32Block(InputBitStream& bits, IntegerEncodedVector& result,
}
for (std::size_t i = 0; i < 3; ++i) {
IntegerEncodedValue& val = result.emplace_back(IntegerEncoding::Qus32, nBitsPerValue);
IntegerEncodedValue& val = result.emplace_back(IntegerEncoding::Quint, nBitsPerValue);
val.bit_value = m[i];
val.qus32_value = q[i];
val.quint_value = q[i];
}
}
// Returns a new instance of this struct that corresponds to the
// can take no more than maxval values
static constexpr IntegerEncodedValue CreateEncoding(u32 maxVal) {
while (maxVal > 0) {
u32 check = maxVal + 1;
// Is maxVal a power of two?
if (!(check & (check - 1))) {
return IntegerEncodedValue(IntegerEncoding::JustBits, Popcnt(maxVal));
}
// Is maxVal of the type 3*2^n - 1?
if ((check % 3 == 0) && !((check / 3) & ((check / 3) - 1))) {
return IntegerEncodedValue(IntegerEncoding::Trit, Popcnt(check / 3 - 1));
}
// Is maxVal of the type 5*2^n - 1?
if ((check % 5 == 0) && !((check / 5) & ((check / 5) - 1))) {
return IntegerEncodedValue(IntegerEncoding::Qus32, Popcnt(check / 5 - 1));
}
// Apparently it can't be represented with a bounded integer sequence...
// just iterate.
maxVal--;
}
return IntegerEncodedValue(IntegerEncoding::JustBits, 0);
}
static constexpr std::array<IntegerEncodedValue, 256> MakeEncodedValues() {
std::array<IntegerEncodedValue, 256> encodings{};
for (std::size_t i = 0; i < encodings.size(); ++i) {
encodings[i] = CreateEncoding(static_cast<u32>(i));
}
return encodings;
}
static constexpr std::array EncodingsValues = MakeEncodedValues();
// Fills result with the values that are encoded in the given
// bitstream. We must know beforehand what the maximum possible
// value is, and how many values we're decoding.
@@ -357,8 +275,8 @@ static void DecodeIntegerSequence(IntegerEncodedVector& result, InputBitStream&
u32 nValsDecoded = 0;
while (nValsDecoded < nValues) {
switch (val.encoding) {
case IntegerEncoding::Qus32:
DecodeQus32Block(bits, result, val.num_bits);
case IntegerEncoding::Quint:
DecodeQuintBlock(bits, result, val.num_bits);
nValsDecoded += 3;
break;
@@ -376,8 +294,6 @@ static void DecodeIntegerSequence(IntegerEncodedVector& result, InputBitStream&
}
}
namespace ASTCC {
struct TexelWeightParams {
u32 m_Width = 0;
u32 m_Height = 0;
@@ -635,48 +551,6 @@ static void FillError(std::span<u32> outBuf, u32 blockWidth, u32 blockHeight) {
}
}
}
// Replicates low numBits such that [(toBit - 1):(toBit - 1 - fromBit)]
// is the same as [(numBits - 1):0] and repeats all the way down.
template <typename IntType>
static constexpr IntType Replicate(IntType val, u32 numBits, u32 toBit) {
if (numBits == 0) {
return 0;
}
if (toBit == 0) {
return 0;
}
const IntType v = val & static_cast<IntType>((1 << numBits) - 1);
IntType res = v;
u32 reslen = numBits;
while (reslen < toBit) {
u32 comp = 0;
if (numBits > toBit - reslen) {
u32 newshift = toBit - reslen;
comp = numBits - newshift;
numBits = newshift;
}
res = static_cast<IntType>(res << numBits);
res = static_cast<IntType>(res | (v >> comp));
reslen += numBits;
}
return res;
}
static constexpr std::size_t NumReplicateEntries(u32 num_bits) {
return std::size_t(1) << num_bits;
}
template <typename IntType, u32 num_bits, u32 to_bit>
static constexpr auto MakeReplicateTable() {
std::array<IntType, NumReplicateEntries(num_bits)> table{};
for (IntType value = 0; value < static_cast<IntType>(std::size(table)); ++value) {
table[value] = Replicate(value, num_bits, to_bit);
}
return table;
}
static constexpr auto REPLICATE_BYTE_TO_16_TABLE = MakeReplicateTable<u32, 8, 16>();
static constexpr u32 ReplicateByteTo16(std::size_t value) {
return REPLICATE_BYTE_TO_16_TABLE[value];
}
@@ -696,9 +570,6 @@ static constexpr auto REPLICATE_2_BIT_TO_8_TABLE = MakeReplicateTable<u32, 2, 8>
static constexpr auto REPLICATE_3_BIT_TO_8_TABLE = MakeReplicateTable<u32, 3, 8>();
static constexpr auto REPLICATE_4_BIT_TO_8_TABLE = MakeReplicateTable<u32, 4, 8>();
static constexpr auto REPLICATE_5_BIT_TO_8_TABLE = MakeReplicateTable<u32, 5, 8>();
static constexpr auto REPLICATE_6_BIT_TO_8_TABLE = MakeReplicateTable<u32, 6, 8>();
static constexpr auto REPLICATE_7_BIT_TO_8_TABLE = MakeReplicateTable<u32, 7, 8>();
static constexpr auto REPLICATE_8_BIT_TO_8_TABLE = MakeReplicateTable<u32, 8, 8>();
/// Use a precompiled table with the most common usages, if it's not in the expected range, fallback
/// to the runtime implementation
static constexpr u32 FastReplicateTo8(u32 value, u32 num_bits) {
@@ -982,9 +853,9 @@ static void DecodeColorValues(u32* out, std::span<u8> data, const u32* modes, co
} // case IntegerEncoding::Trit
break;
case IntegerEncoding::Qus32: {
case IntegerEncoding::Quint: {
D = val.qus32_value;
D = val.quint_value;
switch (bitlen) {
case 1: {
@@ -1023,7 +894,7 @@ static void DecodeColorValues(u32* out, std::span<u8> data, const u32* modes, co
assert(false && "Unsupported quint encoding for color values!");
break;
} // switch(bitlen)
} // case IntegerEncoding::Qus32
} // case IntegerEncoding::Quint
break;
} // switch(val.encoding)
@@ -1086,8 +957,8 @@ static u32 UnquantizeTexelWeight(const IntegerEncodedValue& val) {
}
} break;
case IntegerEncoding::Qus32: {
D = val.qus32_value;
case IntegerEncoding::Quint: {
D = val.quint_value;
assert(D < 5);
switch (bitlen) {
@@ -1675,10 +1546,6 @@ static void DecompressBlock(std::span<const u8, 16> inBuf, const u32 blockWidth,
}
}
} // namespace ASTCC
namespace Tegra::Texture::ASTC {
void Decompress(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
uint32_t block_width, uint32_t block_height, std::span<uint8_t> output) {
u32 block_index = 0;
@@ -1690,7 +1557,7 @@ void Decompress(std::span<const uint8_t> data, uint32_t width, uint32_t height,
// Blocks can be at most 12x12
std::array<u32, 12 * 12> uncompData;
ASTCC::DecompressBlock(blockPtr, block_width, block_height, uncompData);
DecompressBlock(blockPtr, block_width, block_height, uncompData);
u32 decompWidth = std::min(block_width, width - x);
u32 decompHeight = std::min(block_height, height - y);

View File

@@ -129,4 +129,7 @@ struct AstcBufferData {
decltype(REPLICATE_BYTE_TO_16_TABLE) replicate_byte_to_16 = REPLICATE_BYTE_TO_16_TABLE;
} constexpr ASTC_BUFFER_DATA;
void Decompress(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
uint32_t block_width, uint32_t block_height, std::span<uint8_t> output);
} // namespace Tegra::Texture::ASTC