2018-06-18 06:50:44 +03:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
#include <bit>
|
|
|
|
#include "common/common_types.h"
|
2018-06-18 06:50:44 +03:00
|
|
|
|
|
|
|
namespace Tegra::Texture::ASTC {
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
enum class IntegerEncoding { JustBits, Quint, Trit };
|
2021-02-13 23:50:12 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
// Returns the number of bits required to encode num_vals values.
|
|
|
|
u32 GetBitLength(u32 num_vals) const {
|
|
|
|
u32 total_bits = num_bits * num_vals;
|
2021-02-13 23:50:12 +03:00
|
|
|
if (encoding == IntegerEncoding::Trit) {
|
2021-03-25 23:53:51 +03:00
|
|
|
total_bits += (num_vals * 8 + 4) / 5;
|
|
|
|
} else if (encoding == IntegerEncoding::Quint) {
|
|
|
|
total_bits += (num_vals * 7 + 2) / 3;
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
2021-03-25 23:53:51 +03:00
|
|
|
return total_bits;
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IntegerEncoding encoding{};
|
|
|
|
u32 num_bits = 0;
|
|
|
|
u32 bit_value = 0;
|
|
|
|
union {
|
2021-03-25 23:53:51 +03:00
|
|
|
u32 quint_value = 0;
|
2021-02-13 23:50:12 +03:00
|
|
|
u32 trit_value;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns a new instance of this struct that corresponds to the
|
2021-03-25 23:53:51 +03:00
|
|
|
// can take no more than mav_value values
|
|
|
|
constexpr IntegerEncodedValue CreateEncoding(u32 mav_value) {
|
|
|
|
while (mav_value > 0) {
|
|
|
|
u32 check = mav_value + 1;
|
2021-02-13 23:50:12 +03:00
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
// Is mav_value a power of two?
|
2021-02-13 23:50:12 +03:00
|
|
|
if (!(check & (check - 1))) {
|
2021-03-25 23:53:51 +03:00
|
|
|
return IntegerEncodedValue(IntegerEncoding::JustBits, std::popcount(mav_value));
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
// Is mav_value of the type 3*2^n - 1?
|
2021-02-13 23:50:12 +03:00
|
|
|
if ((check % 3 == 0) && !((check / 3) & ((check / 3) - 1))) {
|
2021-03-25 23:53:51 +03:00
|
|
|
return IntegerEncodedValue(IntegerEncoding::Trit, std::popcount(check / 3 - 1));
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
// Is mav_value of the type 5*2^n - 1?
|
2021-02-13 23:50:12 +03:00
|
|
|
if ((check % 5 == 0) && !((check / 5) & ((check / 5) - 1))) {
|
2021-03-25 23:53:51 +03:00
|
|
|
return IntegerEncodedValue(IntegerEncoding::Quint, std::popcount(check / 5 - 1));
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apparently it can't be represented with a bounded integer sequence...
|
|
|
|
// just iterate.
|
2021-03-25 23:53:51 +03:00
|
|
|
mav_value--;
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
return IntegerEncodedValue(IntegerEncoding::JustBits, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
constexpr std::array<IntegerEncodedValue, 256> MakeEncodedValues() {
|
2021-02-13 23:50:12 +03:00
|
|
|
std::array<IntegerEncodedValue, 256> encodings{};
|
|
|
|
for (std::size_t i = 0; i < encodings.size(); ++i) {
|
|
|
|
encodings[i] = CreateEncoding(static_cast<u32>(i));
|
|
|
|
}
|
|
|
|
return encodings;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
constexpr std::array<IntegerEncodedValue, 256> EncodingsValues = MakeEncodedValues();
|
2021-02-13 23:50:12 +03:00
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
// 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.
|
2021-02-13 23:50:12 +03:00
|
|
|
template <typename IntType>
|
2021-03-25 23:53:51 +03:00
|
|
|
constexpr IntType Replicate(IntType val, u32 num_bits, u32 to_bit) {
|
|
|
|
if (num_bits == 0 || to_bit == 0) {
|
2021-02-13 23:50:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2021-03-25 23:53:51 +03:00
|
|
|
const IntType v = val & static_cast<IntType>((1 << num_bits) - 1);
|
2021-02-13 23:50:12 +03:00
|
|
|
IntType res = v;
|
2021-03-25 23:53:51 +03:00
|
|
|
u32 reslen = num_bits;
|
|
|
|
while (reslen < to_bit) {
|
2021-02-13 23:50:12 +03:00
|
|
|
u32 comp = 0;
|
2021-03-25 23:53:51 +03:00
|
|
|
if (num_bits > to_bit - reslen) {
|
|
|
|
u32 newshift = to_bit - reslen;
|
|
|
|
comp = num_bits - newshift;
|
|
|
|
num_bits = newshift;
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
2021-03-25 23:53:51 +03:00
|
|
|
res = static_cast<IntType>(res << num_bits);
|
2021-02-13 23:50:12 +03:00
|
|
|
res = static_cast<IntType>(res | (v >> comp));
|
2021-03-25 23:53:51 +03:00
|
|
|
reslen += num_bits;
|
2021-02-13 23:50:12 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
constexpr std::size_t NumReplicateEntries(u32 num_bits) {
|
2021-02-13 23:50:12 +03:00
|
|
|
return std::size_t(1) << num_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType, u32 num_bits, u32 to_bit>
|
2021-03-25 23:53:51 +03:00
|
|
|
constexpr auto MakeReplicateTable() {
|
2021-02-13 23:50:12 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:53:51 +03:00
|
|
|
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;
|
2018-06-18 06:50:44 +03:00
|
|
|
|
|
|
|
} // namespace Tegra::Texture::ASTC
|