More block tweaks

This commit is contained in:
Andrew Cassidy 2021-04-01 22:37:25 -07:00
parent 0caa4fbc4c
commit 8cd870ed26
9 changed files with 107 additions and 135 deletions

View File

@ -18,7 +18,8 @@
*/
#include "Color.h"
#include <algorithm> // for max, Min
#include <algorithm>
#include <stdexcept>
#include "Vector4.h"
#include "Vector4Int.h"
@ -26,7 +27,17 @@
namespace quicktex {
Color::Color(Vector4Int v) { SetRGBA((uint8_t)v[0], (uint8_t)v[1], (uint8_t)v[2], (uint8_t)v[3]); }
Color::Color(Vector4Int v) {
if (v.MaxAbs() > 0xFF) throw std::invalid_argument("Vector members out of range");
for (int i = 0; i < 4; i++) {
if (v[i] < 0) throw std::range_error("Color members cannot be negative");
}
r = static_cast<uint8_t>(v[0]);
g = static_cast<uint8_t>(v[1]);
b = static_cast<uint8_t>(v[2]);
a = static_cast<uint8_t>(v[3]);
}
uint16_t Color::Pack565Unscaled(uint8_t r, uint8_t g, uint8_t b) {
assert5bit(r);
@ -75,13 +86,6 @@ Color Color::PreciseRound565(Vector4 &v) {
return Color(r, g, b);
}
void Color::SetRGBA(uint8_t vr, uint8_t vg, uint8_t vb, uint8_t va = 0xFF) {
r = vr;
g = vg;
b = vb;
a = va;
}
void Color::SetRGB(uint8_t vr, uint8_t vg, uint8_t vb) {
r = vr;
g = vg;

View File

@ -66,9 +66,6 @@ class Color {
operator Vector4Int() const;
friend Vector4Int operator-(const Color &lhs, const Color &rhs);
void SetRGBA(uint8_t vr, uint8_t vg, uint8_t vb, uint8_t va);
void SetRGBA(const Color &other) { SetRGBA(other.r, other.g, other.b, other.a); }
void SetRGB(uint8_t vr, uint8_t vg, uint8_t vb);
void SetRGB(const Color &other) { SetRGB(other.r, other.g, other.b); }

View File

@ -35,39 +35,40 @@ class alignas(8) BC1Block {
static constexpr int Height = 4;
using SelectorArray = std::array<std::array<uint8_t, Width>, Height>;
using ColorPair = std::tuple<Color, Color>;
using ColorPair = std::pair<Color, Color>;
constexpr BC1Block() {
static_assert(sizeof(BC1Block) == 8);
static_assert(sizeof(std::array<BC1Block, 10>) == 8 * 10);
SetColor0Raw(0);
SetColor1Raw(0);
SetSelectorsSolid(0);
static_assert(alignof(BC1Block) >= 8);
_color0 = _color1 = {0, 0};
_selectors = {0, 0, 0, 0};
}
constexpr BC1Block(Color color0, Color color1, const SelectorArray& selectors) {
BC1Block(Color color0, Color color1, const SelectorArray& selectors) {
SetColor0(color0);
SetColor1(color1);
SetSelectors(selectors);
}
constexpr BC1Block(Color color0, Color color1, uint8_t solid_mask) {
SetColor0(color0);
SetColor1(color1);
SetSelectorsSolid(solid_mask);
BC1Block(uint16_t ep0, uint16_t ep1, const SelectorArray& selectors) {
SetColor0Raw(ep0);
SetColor1Raw(ep1);
SetSelectors(selectors);
}
uint16_t GetColor0Raw() const { return static_cast<uint16_t>(_color_0[0] | (_color_0[1] << 8U)); }
uint16_t GetColor1Raw() const { return static_cast<uint16_t>(_color_1[0] | (_color_1[1] << 8U)); }
void SetColor0Raw(uint16_t c) {
_color_0[0] = c & 0xFF;
_color_0[1] = (c >> 8) & 0xFF;
}
void SetColor1Raw(uint16_t c) {
_color_1[0] = c & 0xFF;
_color_1[1] = (c >> 8) & 0xFF;
BC1Block(uint16_t ep0, uint16_t ep1, uint8_t solid_mask) {
SetColor0Raw(ep0);
SetColor1Raw(ep1);
_selectors.fill(solid_mask);
}
constexpr uint16_t GetColor0Raw() const { return Pack<uint8_t, uint16_t, 8, EndpointSize>(_color0); }
constexpr uint16_t GetColor1Raw() const { return Pack<uint8_t, uint16_t, 8, EndpointSize>(_color1); }
void SetColor0Raw(uint16_t c) { _color0 = Unpack<uint16_t, uint8_t, 8, EndpointSize>(c); }
void SetColor1Raw(uint16_t c) { _color1 = Unpack<uint16_t, uint8_t, 8, EndpointSize>(c); }
Color GetColor0() const { return Color::Unpack565(GetColor0Raw()); }
Color GetColor1() const { return Color::Unpack565(GetColor1Raw()); }
ColorPair GetColors() const { return {GetColor0(), GetColor1()}; }
@ -75,37 +76,23 @@ class alignas(8) BC1Block {
void SetColor0(Color c) { SetColor0Raw(c.Pack565()); }
void SetColor1(Color c) { SetColor1Raw(c.Pack565()); }
void SetColors(ColorPair cs) {
SetColor0(std::get<0>(cs));
SetColor1(std::get<1>(cs));
SetColor0(cs.first);
SetColor1(cs.second);
}
bool Is3Color() const { return GetColor0Raw() <= GetColor1Raw(); }
constexpr SelectorArray GetSelectors() const { return MapArray(_selectors, Unpack<uint8_t, uint8_t, SelectorBits, Width>); }
SelectorArray GetSelectors() const {
SelectorArray unpacked;
for (int i = 0; i < Height; i++) { unpacked[i] = Unpack<uint8_t, uint8_t, SelectorBits, Width>(_selectors[i]); }
return unpacked;
}
void SetSelectors(const SelectorArray& unpacked) { _selectors = MapArray(unpacked, Pack<uint8_t, uint8_t, SelectorBits, Width>); }
void SetSelectors(const SelectorArray& unpacked) {
for (int i = 0; i < Height; i++) { _selectors[i] = Pack<uint8_t, uint8_t, SelectorBits, Width>(unpacked[i]); }
}
constexpr bool Is3Color() const { return GetColor0Raw() <= GetColor1Raw(); }
/**
* Set every row of selectors to the same 8-bit mask. useful for solid-color blocks
* @param mask the 8-bit mask to use for each row
*/
void SetSelectorsSolid(uint8_t mask) {
for (int i = 0; i < Height; i++) _selectors[i] = mask;
}
constexpr static inline size_t EndpointSize = 2; // in bytes
constexpr static inline size_t SelectorSize = 4; // in bytes
constexpr static inline uint8_t SelectorBits = 2; // in bits
constexpr static inline size_t EndpointSize = 2; // in bytes
constexpr static inline size_t SelectorSize = 4; // in bytes
constexpr static inline uint8_t SelectorBits = 2; // in bits
private:
std::array<uint8_t, EndpointSize> _color_0;
std::array<uint8_t, EndpointSize> _color_1;
std::array<uint8_t, EndpointSize> _color0;
std::array<uint8_t, EndpointSize> _color1;
std::array<uint8_t, SelectorSize> _selectors;
};
} // namespace quicktex::s3tc

View File

@ -43,9 +43,6 @@
namespace quicktex::s3tc {
using CBlock = ColorBlock<4, 4>;
using BlockMetrics = CBlock::Metrics;
// constructors
BC1Encoder::BC1Encoder(unsigned int level, ColorMode color_mode, InterpolatorPtr interpolator) : _interpolator(interpolator), _color_mode(color_mode) {
@ -347,7 +344,6 @@ BC1Block BC1Encoder::EncodeBlock(const ColorBlock<4, 4> &pixels) const {
// Private methods
BC1Block BC1Encoder::WriteBlockSolid(Color color) const {
BC1Block block;
uint8_t mask = 0xAA; // 2222
uint16_t min16, max16;
@ -394,17 +390,13 @@ BC1Block BC1Encoder::WriteBlockSolid(Color color) const {
}
}
block.SetColor0Raw(max16);
block.SetColor1Raw(min16);
block.SetSelectorsSolid(mask);
return block;
return BC1Block(max16, min16, mask);
}
BC1Block BC1Encoder::WriteBlock(EncodeResults &result) const {
BC1Block block;
BC1Block::SelectorArray selectors;
uint16_t color1 = result.low.Pack565Unscaled();
uint16_t color0 = result.high.Pack565Unscaled();
uint16_t ep1 = result.low.Pack565Unscaled();
uint16_t ep0 = result.high.Pack565Unscaled();
std::array<uint8_t, 4> lut;
assert(result.color_mode != ColorMode::Incomplete);
@ -412,31 +404,31 @@ BC1Block BC1Encoder::WriteBlock(EncodeResults &result) const {
if ((bool)(result.color_mode & ColorMode::FourColor)) {
lut = {1, 3, 2, 0};
if (color1 > color0) {
std::swap(color1, color0);
if (ep1 > ep0) {
std::swap(ep1, ep0);
lut = {0, 2, 3, 1};
} else if (color1 == color0) {
if (color1 > 0) {
color1--;
} else if (ep1 == ep0) {
if (ep1 > 0) {
ep1--;
lut = {0, 0, 0, 0};
} else {
assert(color1 == 0 && color0 == 0);
color0 = 1;
color1 = 0;
assert(ep1 == 0 && ep0 == 0);
ep0 = 1;
ep1 = 0;
lut = {1, 1, 1, 1};
}
}
assert(color0 > color1);
assert(ep0 > ep1);
} else {
lut = {1, 2, 0, 3};
if (color1 < color0) {
std::swap(color1, color0);
if (ep1 < ep0) {
std::swap(ep1, ep0);
lut = {0, 2, 1, 3};
}
assert(color0 <= color1);
assert(ep0 <= ep1);
}
for (unsigned i = 0; i < 16; i++) {
@ -446,10 +438,7 @@ BC1Block BC1Encoder::WriteBlock(EncodeResults &result) const {
if (result.color_mode == ColorMode::ThreeColor) { assert(selectors[y][x] != 3); }
}
block.SetColor0Raw(color0);
block.SetColor1Raw(color1);
block.SetSelectors(selectors);
return block;
return BC1Block(ep0, ep1, selectors);
}
void BC1Encoder::FindEndpointsSingleColor(EncodeResults &result, Color color, bool is_3color) const {
@ -474,7 +463,7 @@ void BC1Encoder::FindEndpointsSingleColor(EncodeResults &result, const CBlock &p
FindEndpointsSingleColor(result, color, is_3color);
result.error = 0;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
Vector4Int pixel_vector = (Vector4Int)pixels.Get(i);
auto diff = pixel_vector - result_vector;
result.error += diff.SqrMag();
@ -521,7 +510,7 @@ void BC1Encoder::FindEndpoints(EncodeResults &result, const CBlock &pixels, cons
std::array<unsigned, 3> sums_xy;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
auto val = pixels.Get(i);
for (unsigned c = 0; c < 3; c++) { sums_xy[c] += val[chan0] * val[c]; }
}
@ -579,7 +568,7 @@ void BC1Encoder::FindEndpoints(EncodeResults &result, const CBlock &pixels, cons
// Select the correct diagonal across the bounding box
int icov_xz = 0, icov_yz = 0;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
int b = (int)pixels.Get(i).b - metrics.avg.b;
icov_xz += b * (int)pixels.Get(i).r - metrics.avg.r;
icov_yz += b * (int)pixels.Get(i).g - metrics.avg.g;
@ -604,7 +593,7 @@ void BC1Encoder::FindEndpoints(EncodeResults &result, const CBlock &pixels, cons
}
int icov_xz = 0, icov_yz = 0;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
int b = (int)pixels.Get(i).b - metrics.avg.b;
icov_xz += b * (int)pixels.Get(i).r - metrics.avg.r;
icov_yz += b * (int)pixels.Get(i).g - metrics.avg.g;
@ -625,7 +614,7 @@ void BC1Encoder::FindEndpoints(EncodeResults &result, const CBlock &pixels, cons
Vector4 axis = {306, 601, 117}; // Luma vector
Matrix4x4 covariance = Matrix4x4::Identity();
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
auto val = pixels.Get(i);
if (ignore_black && val.IsBlack()) continue;
@ -661,9 +650,9 @@ void BC1Encoder::FindEndpoints(EncodeResults &result, const CBlock &pixels, cons
float min_dot = INFINITY;
float max_dot = -INFINITY;
unsigned min_index = 0, max_index = 0;
int min_index = 0, max_index = 0;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
auto val = pixels.Get(i);
if (ignore_black && val.IsBlack()) continue;
@ -709,11 +698,11 @@ template <BC1Encoder::ColorMode M> void BC1Encoder::FindSelectors(EncodeResults
if (error_mode == ErrorMode::None || error_mode == ErrorMode::Faster) {
Vector4Int axis = color_vectors[3] - color_vectors[0];
std::array<int, 4> dots;
for (unsigned i = 0; i < 4; i++) { dots[i] = axis.Dot(color_vectors[i]); }
for (int i = 0; i < 4; i++) { dots[i] = axis.Dot(color_vectors[i]); }
int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
axis *= 2;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
Vector4Int pixel_vector = Vector4Int::FromColorRGB(pixels.Get(i));
int dot = axis.Dot(pixel_vector);
uint8_t level = (dot <= t0) + (dot < t1) + (dot < t2);
@ -734,7 +723,7 @@ template <BC1Encoder::ColorMode M> void BC1Encoder::FindSelectors(EncodeResults
Vector4Int axis = color_vectors[3] - color_vectors[0];
const float f = 4.0f / ((float)axis.SqrMag() + .00000125f);
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
Vector4Int pixel_vector = Vector4Int::FromColorRGB(pixels.Get(i));
auto diff = pixel_vector - color_vectors[0];
float sel_f = (float)diff.Dot(axis) * f + 0.5f;
@ -762,7 +751,7 @@ template <BC1Encoder::ColorMode M> void BC1Encoder::FindSelectors(EncodeResults
} else if (error_mode == ErrorMode::Full) {
unsigned max_sel = (bool)(M == ColorMode::ThreeColor) ? 3 : 4;
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
unsigned best_error = UINT_MAX;
uint8_t best_sel = 0;
Vector4Int pixel_vector = Vector4Int::FromColorRGB(pixels.Get(i));
@ -800,7 +789,7 @@ template <BC1Encoder::ColorMode M> bool BC1Encoder::RefineEndpointsLS(EncodeResu
Vector4 q00 = {0, 0, 0};
Vector4 matrix = Vector4(0);
for (unsigned i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
const Color color = pixels.Get(i);
const uint8_t sel = result.selectors[i];

View File

@ -32,11 +32,12 @@ class alignas(8) BC3Block {
constexpr BC3Block() {
static_assert(sizeof(BC3Block) == 16);
static_assert(sizeof(std::array<BC3Block, 10>) == 16 * 10);
static_assert(alignof(BC3Block) >= 8);
alpha_block = BC4Block();
color_block = BC1Block();
}
constexpr BC3Block(const BC4Block &alpha, const BC1Block &color) {
BC3Block(const BC4Block &alpha, const BC1Block &color) {
alpha_block = alpha;
color_block = color;
}

View File

@ -24,9 +24,7 @@
#include <cstdint>
#include <cstdlib>
#include "../../Color.h"
#include "../../util.h"
#include "../bc1/BC1Block.h"
namespace quicktex::s3tc {
@ -41,55 +39,50 @@ class alignas(8) BC4Block {
constexpr BC4Block() {
static_assert(sizeof(BC4Block) == 8);
static_assert(sizeof(std::array<BC4Block, 10>) == 8 * 10);
static_assert(alignof(BC4Block) >= 8);
alpha0 = alpha1 = 0;
SetSelectorBits(0);
_selectors = {0, 0, 0, 0, 0, 0};
}
constexpr BC4Block(uint8_t valpha0, uint8_t valpha1, const SelectorArray& selectors) {
BC4Block(uint8_t valpha0, uint8_t valpha1, const SelectorArray& selectors) {
alpha0 = valpha0;
alpha1 = valpha1;
SetSelectors(selectors);
}
inline bool Is6Value() const { return alpha0 <= alpha1; }
AlphaPair GetAlphas() const { return AlphaPair(alpha0, alpha1); }
constexpr AlphaPair GetAlphas() const { return AlphaPair(alpha0, alpha1); }
void SetAlphas(AlphaPair as) {
alpha0 = as.first;
alpha1 = as.second;
}
inline uint64_t GetSelectorBits() const {
auto packed = Pack<uint8_t, uint64_t, 8, SelectorSize>(selectors);
assert(packed <= SelectorBitsMax);
constexpr uint64_t GetSelectorBits() const {
auto packed = Pack<uint8_t, uint64_t, 8, SelectorSize>(_selectors);
assert(packed <= SelectorsPackedMax);
return packed;
}
void SetSelectorBits(uint64_t packed) {
assert(packed <= SelectorBitsMax);
selectors = Unpack<uint64_t, uint8_t, 8, SelectorSize>(packed);
assert(packed <= SelectorsPackedMax);
_selectors = Unpack<uint64_t, uint8_t, 8, SelectorSize>(packed);
}
SelectorArray GetSelectors() const {
SelectorArray unpacked;
auto rows = Unpack<uint64_t, uint16_t, 12, Width>(GetSelectorBits());
for (unsigned i = 0; i < Height; i++) {
auto row = Unpack<uint16_t, uint8_t, SelectorBits, Width>(rows[i]);
unpacked[i] = row;
}
constexpr SelectorArray GetSelectors() const {
auto rows = Unpack<uint64_t, uint16_t, SelectorBits * Width, Height>(GetSelectorBits());
auto unpacked = MapArray(rows, Unpack<uint16_t, uint8_t, SelectorBits, Width>);
return unpacked;
}
void SetSelectors(const SelectorArray& unpacked) {
std::array<uint16_t, Height> rows;
for (int i = 0; i < Height; i++) { rows[i] = Pack<uint8_t, uint16_t, SelectorBits, Width>(unpacked[i]); }
auto packed = Pack<uint16_t, uint64_t, 12, Height>(rows);
auto rows = MapArray(unpacked, Pack<uint8_t, uint16_t, SelectorBits, Width>);
auto packed = Pack<uint16_t, uint64_t, SelectorBits * Width, Height>(rows);
SetSelectorBits(packed);
}
static inline std::array<uint8_t, 8> GetValues6(uint32_t l, uint32_t h) {
constexpr bool Is6Value() const { return alpha0 <= alpha1; }
static constexpr std::array<uint8_t, 8> GetValues6(unsigned l, unsigned h) {
return {static_cast<uint8_t>(l),
static_cast<uint8_t>(h),
static_cast<uint8_t>((l * 4 + h) / 5),
@ -100,7 +93,7 @@ class alignas(8) BC4Block {
255};
}
static inline std::array<uint8_t, 8> GetValues8(uint32_t l, uint32_t h) {
static constexpr std::array<uint8_t, 8> GetValues8(unsigned l, unsigned h) {
return {static_cast<uint8_t>(l),
static_cast<uint8_t>(h),
static_cast<uint8_t>((l * 6 + h) / 7),
@ -111,24 +104,16 @@ class alignas(8) BC4Block {
static_cast<uint8_t>((l + h * 6) / 7)};
}
static inline std::array<uint8_t, 8> GetValues(uint32_t l, uint32_t h) {
if (l > h)
return GetValues8(l, h);
else
return GetValues6(l, h);
}
static constexpr std::array<uint8_t, 8> GetValues(unsigned l, unsigned h) { return l > h ? GetValues8(l, h) : GetValues6(l, h); }
constexpr static inline size_t EndpointSize = 1;
constexpr static inline size_t SelectorSize = 6;
constexpr static inline uint8_t SelectorBits = 3;
constexpr static inline uint8_t SelectorValues = 1 << SelectorBits;
constexpr static inline uint8_t SelectorMask = SelectorValues - 1;
constexpr static inline uint64_t SelectorBitsMax = (1ULL << (8U * SelectorSize)) - 1U;
static constexpr size_t SelectorSize = 6;
static constexpr uint8_t SelectorBits = 3;
static constexpr uint64_t SelectorsPackedMax = (1ULL << (8U * SelectorSize)) - 1U;
uint8_t alpha0;
uint8_t alpha1;
private:
std::array<uint8_t, SelectorSize> selectors;
std::array<uint8_t, SelectorSize> _selectors;
};
} // namespace quicktex::s3tc

View File

@ -47,7 +47,7 @@ void InitBC4(py::module_ &s3tc) {
bc4_block.doc() = "A single BC4 block.";
bc4_block.def(py::init<>());
bc4_block.def(py::init<uint8_t, uint8_t, BC1Block::SelectorArray>(), "endpoint0"_a, "endpoint1"_a, "selectors"_a, R"doc(
bc4_block.def(py::init<uint8_t, uint8_t, BC4Block::SelectorArray>(), "endpoint0"_a, "endpoint1"_a, "selectors"_a, R"doc(
__init__(self, endpoint0: int, endpoint1: int, selectors: List[List[int]]) -> None
Create a new BC4Block with the specified endpoints and selectors.

View File

@ -31,10 +31,11 @@ class alignas(8) BC5Block {
constexpr BC5Block() {
static_assert(sizeof(BC5Block) == 16);
static_assert(sizeof(std::array<BC5Block, 10>) == 16 * 10);
static_assert(alignof(BC5Block) >= 8);
chan0_block = chan1_block = BC4Block();
}
constexpr BC5Block(const BC4Block &chan0, const BC4Block &chan1) {
BC5Block(const BC4Block &chan0, const BC4Block &chan1) {
chan0_block = chan0;
chan1_block = chan1;
}

View File

@ -100,6 +100,14 @@ template <size_t Size, int Op(int)> constexpr std::array<uint8_t, Size> ExpandAr
return res;
}
template <typename I, typename Fn, size_t N> constexpr auto MapArray(const std::array<I, N> &input, Fn&& op) {
std::array<std::invoke_result_t<Fn, I>, N> output;
for (unsigned i = 0; i < N; i++) {
output[i] = op(input[i]);
}
return output;
}
template <typename S> constexpr S scale8To5(S v) {
auto v2 = v * 31 + 128;
return static_cast<S>((v2 + (v2 >> 8)) >> 8);
@ -159,7 +167,7 @@ template <typename... Args> std::string Format(const char *str, const Args &...a
for (unsigned i = 0; i < values.size(); i++) {
auto key = "{" + std::to_string(i) + "}";
auto value = values[i];
while(true) {
while (true) {
size_t where = output.find(key);
if (where == output.npos) break;
output.replace(where, key.length(), value);