diff options
author | Rodrigo Locatti <reinuseslisp@airmail.cc> | 2021-03-31 00:31:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-31 00:31:52 +0200 |
commit | 5ee669466fcebd2258229ed6bfe6b5e5529e0200 (patch) | |
tree | 6dbf84fb5c2c9656f1d1ef6c46b2527ea1a205ff /src/video_core/textures/astc.h | |
parent | Merge pull request #6116 from german77/userArgument (diff) | |
parent | astc_decoder: Refactor for style and more efficient memory use (diff) | |
download | yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar.gz yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar.bz2 yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar.lz yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar.xz yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.tar.zst yuzu-5ee669466fcebd2258229ed6bfe6b5e5529e0200.zip |
Diffstat (limited to 'src/video_core/textures/astc.h')
-rw-r--r-- | src/video_core/textures/astc.h | 124 |
1 files changed, 121 insertions, 3 deletions
diff --git a/src/video_core/textures/astc.h b/src/video_core/textures/astc.h index 9105119bc..c1c73fda5 100644 --- a/src/video_core/textures/astc.h +++ b/src/video_core/textures/astc.h @@ -4,11 +4,129 @@ #pragma once -#include <cstdint> +#include <bit> +#include "common/common_types.h" 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); +enum class IntegerEncoding { JustBits, Quint, 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 num_vals values. + u32 GetBitLength(u32 num_vals) const { + u32 total_bits = num_bits * num_vals; + if (encoding == IntegerEncoding::Trit) { + total_bits += (num_vals * 8 + 4) / 5; + } else if (encoding == IntegerEncoding::Quint) { + total_bits += (num_vals * 7 + 2) / 3; + } + return total_bits; + } + + IntegerEncoding encoding{}; + u32 num_bits = 0; + u32 bit_value = 0; + union { + u32 quint_value = 0; + u32 trit_value; + }; +}; + +// Returns a new instance of this struct that corresponds to the +// can take no more than mav_value values +constexpr IntegerEncodedValue CreateEncoding(u32 mav_value) { + while (mav_value > 0) { + u32 check = mav_value + 1; + + // Is mav_value a power of two? + if (!(check & (check - 1))) { + return IntegerEncodedValue(IntegerEncoding::JustBits, std::popcount(mav_value)); + } + + // Is mav_value of the type 3*2^n - 1? + if ((check % 3 == 0) && !((check / 3) & ((check / 3) - 1))) { + return IntegerEncodedValue(IntegerEncoding::Trit, std::popcount(check / 3 - 1)); + } + + // Is mav_value of the type 5*2^n - 1? + if ((check % 5 == 0) && !((check / 5) & ((check / 5) - 1))) { + return IntegerEncodedValue(IntegerEncoding::Quint, std::popcount(check / 5 - 1)); + } + + // Apparently it can't be represented with a bounded integer sequence... + // just iterate. + mav_value--; + } + return IntegerEncodedValue(IntegerEncoding::JustBits, 0); +} + +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; +} + +constexpr std::array<IntegerEncodedValue, 256> EncodingsValues = MakeEncodedValues(); + +// Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)] +// is the same as [(num_bits - 1):0] and repeats all the way down. +template <typename IntType> +constexpr IntType Replicate(IntType val, u32 num_bits, u32 to_bit) { + if (num_bits == 0 || to_bit == 0) { + return 0; + } + const IntType v = val & static_cast<IntType>((1 << num_bits) - 1); + IntType res = v; + u32 reslen = num_bits; + while (reslen < to_bit) { + u32 comp = 0; + if (num_bits > to_bit - reslen) { + u32 newshift = to_bit - reslen; + comp = num_bits - newshift; + num_bits = newshift; + } + res = static_cast<IntType>(res << num_bits); + res = static_cast<IntType>(res | (v >> comp)); + reslen += num_bits; + } + return res; +} + +constexpr std::size_t NumReplicateEntries(u32 num_bits) { + return std::size_t(1) << num_bits; +} + +template <typename IntType, u32 num_bits, u32 to_bit> +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; +} + +constexpr auto REPLICATE_BYTE_TO_16_TABLE = MakeReplicateTable<u32, 8, 16>(); +constexpr auto REPLICATE_6_BIT_TO_8_TABLE = MakeReplicateTable<u32, 6, 8>(); +constexpr auto REPLICATE_7_BIT_TO_8_TABLE = MakeReplicateTable<u32, 7, 8>(); +constexpr auto REPLICATE_8_BIT_TO_8_TABLE = MakeReplicateTable<u32, 8, 8>(); + +struct AstcBufferData { + decltype(EncodingsValues) encoding_values = EncodingsValues; + decltype(REPLICATE_6_BIT_TO_8_TABLE) replicate_6_to_8 = REPLICATE_6_BIT_TO_8_TABLE; + decltype(REPLICATE_7_BIT_TO_8_TABLE) replicate_7_to_8 = REPLICATE_7_BIT_TO_8_TABLE; + decltype(REPLICATE_8_BIT_TO_8_TABLE) replicate_8_to_8 = REPLICATE_8_BIT_TO_8_TABLE; + decltype(REPLICATE_BYTE_TO_16_TABLE) replicate_byte_to_16 = REPLICATE_BYTE_TO_16_TABLE; +} constexpr ASTC_BUFFER_DATA; } // namespace Tegra::Texture::ASTC |