mirror of
https://git.triforceproject.com/triforce/source
synced 2026-12-01 05:18:50 -04:00
Triforce v0.9.0
Character tab, faster renderer defaults, HD rumble shipped, and the 11-30 bug pass: ending softlock below 60 fps, adult first-night lock, Intel Arc robust buffers, adult map, randomizer load order.
This commit is contained in:
268
gpu/vk/tex_pack_bc7.h
Normal file
268
gpu/vk/tex_pack_bc7.h
Normal file
@@ -0,0 +1,268 @@
|
||||
// ((Triforce, 11-28) Evil HD's texture pack as BC7, prepared once (vk_texture.cpp texpack). No Vulkan here: tests/test_tex_pack_bc7.py
|
||||
// builds it alone.
|
||||
//
|
||||
// The owner's "A texture pack should just reduce fps and have all the textures showing instead of popping in random textures as
|
||||
// hd ... its not even constant". The pack's PNGs held as RGBA8 at twice the game's size were ~19 GB for the 2,011 pack textures
|
||||
// one Hyrule Field visit sees: more than any card holds beside the game, so a budget picked a changing few. As BC7 (8 bits a texel,
|
||||
// a quarter) they are 4.7 GB, 2.7 GB without the 231 that are 1024 or more already (tier). Decoding a PNG and encoding it took
|
||||
// 66 + 162 ms for a 2048 x 2048 on one thread of a Ryzen with 24, so it is done once for the whole pack (prepare: every core but
|
||||
// a few, a minute or two there), into triforce_texpack/bc7/<key>.bc7, and a texture's file is then read as it is and uploaded.
|
||||
//
|
||||
// The encoder is the WebGPU back end's (gpu/render/rx_bc7.cpp: BC7 mode 6, the colours' main axis, one least-squares refit).
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mkwvk::tex_pack_bc7 {
|
||||
|
||||
// ---- the encoder (rx_bc7.cpp's, unchanged) ----
|
||||
namespace enc {
|
||||
constexpr int kWeights[16] = {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64};
|
||||
struct Fit {
|
||||
int q[2][4];
|
||||
int p[2];
|
||||
uint8_t index[16];
|
||||
int64_t error;
|
||||
};
|
||||
inline void quantize(const float e[4], int q[4], int& p) {
|
||||
int64_t best = INT64_MAX;
|
||||
for (int pb = 0; pb < 2; pb++) {
|
||||
int64_t err = 0;
|
||||
int t[4];
|
||||
for (int c = 0; c < 4; c++) {
|
||||
t[c] = std::clamp((int)((e[c] - pb) * 0.5f + 0.5f), 0, 127);
|
||||
const float d = e[c] - (float)(t[c] * 2 + pb);
|
||||
err += (int64_t)(d * d * 16.0f);
|
||||
}
|
||||
if (err < best) {
|
||||
best = err;
|
||||
p = pb;
|
||||
std::memcpy(q, t, sizeof t);
|
||||
}
|
||||
}
|
||||
}
|
||||
inline void choose_indices(const uint8_t px[16][4], Fit& f) {
|
||||
int pal[16][4];
|
||||
for (int k = 0; k < 16; k++)
|
||||
for (int c = 0; c < 4; c++) {
|
||||
const int a = f.q[0][c] * 2 + f.p[0], b = f.q[1][c] * 2 + f.p[1];
|
||||
pal[k][c] = ((64 - kWeights[k]) * a + kWeights[k] * b + 32) >> 6;
|
||||
}
|
||||
f.error = 0;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int best = 0;
|
||||
int64_t best_err = INT64_MAX;
|
||||
for (int k = 0; k < 16; k++) {
|
||||
int64_t e = 0;
|
||||
for (int c = 0; c < 4; c++) {
|
||||
const int d = px[i][c] - pal[k][c];
|
||||
e += d * d;
|
||||
}
|
||||
if (e < best_err) best_err = e, best = k;
|
||||
}
|
||||
f.index[i] = (uint8_t)best;
|
||||
f.error += best_err;
|
||||
}
|
||||
}
|
||||
inline Fit fit_endpoints(const uint8_t px[16][4], const float e0[4], const float e1[4]) {
|
||||
Fit f;
|
||||
quantize(e0, f.q[0], f.p[0]);
|
||||
quantize(e1, f.q[1], f.p[1]);
|
||||
choose_indices(px, f);
|
||||
return f;
|
||||
}
|
||||
inline void put(uint64_t (&w)[2], int& pos, uint64_t v, int n) {
|
||||
if (pos < 64) {
|
||||
w[0] |= v << pos;
|
||||
if (pos + n > 64) w[1] |= v >> (64 - pos);
|
||||
} else {
|
||||
w[1] |= v << (pos - 64);
|
||||
}
|
||||
pos += n;
|
||||
}
|
||||
inline void encode_block(const uint8_t px[16][4], uint8_t out[16]) {
|
||||
float mean[4] = {0, 0, 0, 0};
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int c = 0; c < 4; c++) mean[c] += px[i][c];
|
||||
for (float& m : mean) m *= 1.0f / 16.0f;
|
||||
float cov[4][4] = {};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
float d[4];
|
||||
for (int c = 0; c < 4; c++) d[c] = px[i][c] - mean[c];
|
||||
for (int a = 0; a < 4; a++)
|
||||
for (int b = 0; b < 4; b++) cov[a][b] += d[a] * d[b];
|
||||
}
|
||||
float axis[4] = {cov[0][0], cov[1][1], cov[2][2], cov[3][3]};
|
||||
for (int it = 0; it < 8; it++) {
|
||||
float n[4] = {0, 0, 0, 0};
|
||||
for (int a = 0; a < 4; a++)
|
||||
for (int b = 0; b < 4; b++) n[a] += cov[a][b] * axis[b];
|
||||
float m = 0;
|
||||
for (float v : n) m = std::max(m, std::fabs(v));
|
||||
if (m < 1e-6f) break;
|
||||
for (int a = 0; a < 4; a++) axis[a] = n[a] / m;
|
||||
}
|
||||
float len = 0;
|
||||
for (float v : axis) len += v * v;
|
||||
len = std::sqrt(len);
|
||||
float tmin = 0, tmax = 0;
|
||||
if (len > 1e-6f) {
|
||||
for (float& v : axis) v /= len;
|
||||
tmin = 1e30f, tmax = -1e30f;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
float t = 0;
|
||||
for (int c = 0; c < 4; c++) t += (px[i][c] - mean[c]) * axis[c];
|
||||
tmin = std::min(tmin, t), tmax = std::max(tmax, t);
|
||||
}
|
||||
}
|
||||
float e0[4], e1[4];
|
||||
for (int c = 0; c < 4; c++) {
|
||||
e0[c] = std::clamp(mean[c] + axis[c] * tmin, 0.0f, 255.0f);
|
||||
e1[c] = std::clamp(mean[c] + axis[c] * tmax, 0.0f, 255.0f);
|
||||
}
|
||||
Fit best = fit_endpoints(px, e0, e1);
|
||||
if (best.error > 64) {
|
||||
float aa = 0, ab = 0, bb = 0, ax[4] = {0, 0, 0, 0}, bx[4] = {0, 0, 0, 0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
const float b = kWeights[best.index[i]] / 64.0f, a = 1.0f - b;
|
||||
aa += a * a, ab += a * b, bb += b * b;
|
||||
for (int c = 0; c < 4; c++) ax[c] += a * px[i][c], bx[c] += b * px[i][c];
|
||||
}
|
||||
const float det = aa * bb - ab * ab;
|
||||
if (std::fabs(det) > 1e-6f) {
|
||||
float r0[4], r1[4];
|
||||
for (int c = 0; c < 4; c++) {
|
||||
r0[c] = std::clamp((bb * ax[c] - ab * bx[c]) / det, 0.0f, 255.0f);
|
||||
r1[c] = std::clamp((aa * bx[c] - ab * ax[c]) / det, 0.0f, 255.0f);
|
||||
}
|
||||
const Fit refit = fit_endpoints(px, r0, r1);
|
||||
if (refit.error < best.error) best = refit;
|
||||
}
|
||||
}
|
||||
if (best.index[0] & 8) {
|
||||
std::swap(best.q[0], best.q[1]);
|
||||
std::swap(best.p[0], best.p[1]);
|
||||
for (uint8_t& k : best.index) k = (uint8_t)(15 - k);
|
||||
}
|
||||
uint64_t w[2] = {0, 0};
|
||||
int pos = 0;
|
||||
put(w, pos, 1u << 6, 7);
|
||||
for (int c = 0; c < 4; c++) {
|
||||
put(w, pos, (uint64_t)best.q[0][c], 7);
|
||||
put(w, pos, (uint64_t)best.q[1][c], 7);
|
||||
}
|
||||
put(w, pos, (uint64_t)best.p[0], 1);
|
||||
put(w, pos, (uint64_t)best.p[1], 1);
|
||||
put(w, pos, best.index[0], 3);
|
||||
for (int i = 1; i < 16; i++) put(w, pos, best.index[i], 4);
|
||||
std::memcpy(out, w, 16);
|
||||
}
|
||||
} // namespace enc
|
||||
|
||||
/// The bytes of a w x h level as BC7: 16 a block of 4 x 4, partial blocks whole
|
||||
inline uint64_t level_bytes(uint32_t w, uint32_t h) { return (uint64_t)((w + 3) / 4) * ((h + 3) / 4) * 16; }
|
||||
/// The levels of a w x h chain down to 1 x 1
|
||||
inline uint32_t full_levels(uint32_t w, uint32_t h) {
|
||||
uint32_t n = 1;
|
||||
while ((std::max(w, h) >> n) > 0) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
/// A tightly packed RGBA8 w x h picture as BC7 blocks (out: level_bytes(w, h))
|
||||
inline void encode(const uint8_t* rgba, uint32_t w, uint32_t h, uint8_t* out) {
|
||||
const uint32_t bw = (w + 3) / 4, bh = (h + 3) / 4;
|
||||
uint8_t px[16][4];
|
||||
for (uint32_t by = 0; by < bh; by++)
|
||||
for (uint32_t bx = 0; bx < bw; bx++) {
|
||||
for (uint32_t y = 0; y < 4; y++)
|
||||
for (uint32_t x = 0; x < 4; x++) {
|
||||
const uint32_t sx = std::min(bx * 4 + x, w - 1), sy = std::min(by * 4 + y, h - 1);
|
||||
std::memcpy(px[y * 4 + x], rgba + ((size_t)sy * w + sx) * 4, 4);
|
||||
}
|
||||
enc::encode_block(px, out + ((size_t)by * bw + bx) * 16);
|
||||
}
|
||||
}
|
||||
|
||||
/// The next level of an RGBA8 picture: each texel the mean of the 2 x 2 above (an odd edge's last texel repeated), the
|
||||
/// renderer's blit did the same with a linear filter
|
||||
inline std::vector<uint8_t> half(const std::vector<uint8_t>& rgba, uint32_t w, uint32_t h, uint32_t* ow, uint32_t* oh) {
|
||||
const uint32_t nw = std::max(1u, w / 2), nh = std::max(1u, h / 2);
|
||||
std::vector<uint8_t> o((size_t)nw * nh * 4);
|
||||
for (uint32_t y = 0; y < nh; y++)
|
||||
for (uint32_t x = 0; x < nw; x++) {
|
||||
const uint32_t x0 = std::min(x * 2, w - 1), x1 = std::min(x * 2 + 1, w - 1);
|
||||
const uint32_t y0 = std::min(y * 2, h - 1), y1 = std::min(y * 2 + 1, h - 1);
|
||||
for (int c = 0; c < 4; c++) {
|
||||
const uint32_t s = rgba[((size_t)y0 * w + x0) * 4 + c] + rgba[((size_t)y0 * w + x1) * 4 + c] +
|
||||
rgba[((size_t)y1 * w + x0) * 4 + c] + rgba[((size_t)y1 * w + x1) * 4 + c];
|
||||
o[((size_t)y * nw + x) * 4 + c] = (uint8_t)((s + 2) / 4);
|
||||
}
|
||||
}
|
||||
*ow = nw, *oh = nh;
|
||||
return o;
|
||||
}
|
||||
|
||||
// ---- the prepared file: triforce_texpack/bc7/<key>.bc7 ----
|
||||
// "TFB7", version, width, height, levels (each uint32), the source PNG's size (uint64: the pack's file changed, the file is
|
||||
// made again), then each level's blocks in order, level 0 first
|
||||
constexpr uint32_t kMagic = 0x37424654; // ("TFB7" little-endian)
|
||||
constexpr uint32_t kVersion = 1;
|
||||
constexpr size_t kHeader = 4 * 5 + 8;
|
||||
struct Header {
|
||||
uint32_t w = 0, h = 0, levels = 0;
|
||||
uint64_t png_size = 0;
|
||||
};
|
||||
inline uint64_t file_bytes(uint32_t w, uint32_t h, uint32_t levels) {
|
||||
uint64_t n = kHeader;
|
||||
for (uint32_t l = 0; l < levels; l++) n += level_bytes(std::max(1u, w >> l), std::max(1u, h >> l));
|
||||
return n;
|
||||
}
|
||||
inline void write_header(uint8_t* p, const Header& hd) {
|
||||
const uint32_t v[5] = {kMagic, kVersion, hd.w, hd.h, hd.levels};
|
||||
std::memcpy(p, v, sizeof v);
|
||||
std::memcpy(p + sizeof v, &hd.png_size, 8);
|
||||
}
|
||||
/// A file's header, checked against its length (false: not one, or cut short)
|
||||
inline bool read_header(const uint8_t* p, size_t n, Header* hd) {
|
||||
if (n < kHeader) return false;
|
||||
uint32_t v[5];
|
||||
std::memcpy(v, p, sizeof v);
|
||||
if (v[0] != kMagic || v[1] != kVersion || !v[2] || !v[3] || !v[4] || v[4] > full_levels(v[2], v[3])) return false;
|
||||
hd->w = v[2], hd->h = v[3], hd->levels = v[4];
|
||||
std::memcpy(&hd->png_size, p + sizeof v, 8);
|
||||
return file_bytes(hd->w, hd->h, hd->levels) == n;
|
||||
}
|
||||
|
||||
// ---- which textures, by the card's video memory (its device-local heap: the same on every run, where what the driver allows
|
||||
// moves with the other programs open, 9.4 to 11.3 GB in one run on the owner's 12 GB card; no budget choosing a changing few) ----
|
||||
// kAll: every pack texture. kNoBig: those of 1024 or more a side stay the game's (at Hyrule Field 4.7 -> 2.7 GB). kSmall: only
|
||||
// those under 512. kNone: the pack is not used (the card cannot hold it beside the game)
|
||||
enum Tier { kNone = 0, kSmall = 1, kNoBig = 2, kAll = 3 };
|
||||
// Measured 11-28 on the owner's 12 GB card, every pack texture (kAll): at most 822 MB of them drawn at once (the Market, Hyrule
|
||||
// Field 775 MB), never near the emergency line. The 4.7 GB above is every texture one visit sees, not those held at once
|
||||
inline Tier tier_for(uint64_t heap_mb) {
|
||||
if (heap_mb >= 11000) return kAll; // (12 GB cards and up: the owner's RTX 4070 SUPER)
|
||||
if (heap_mb >= 7500) return kNoBig; // (8 and 10 GB: ~0.5 GB of them)
|
||||
if (heap_mb >= 5500) return kSmall; // (6 GB)
|
||||
return kNone;
|
||||
}
|
||||
inline const char* tier_name(Tier t) {
|
||||
return t == kAll ? "every pack texture" : t == kNoBig ? "those under 1024 a side (the bigger ones stay the game's)"
|
||||
: t == kSmall ? "those under 512 a side" : "none (too little video memory beside the game)";
|
||||
}
|
||||
/// Whether a game texture of w x h takes its pack file at this tier
|
||||
inline bool tier_wants(Tier t, uint32_t w, uint32_t h) {
|
||||
const uint32_t side = std::max(w, h);
|
||||
return t == kAll || (t == kNoBig && side < 1024) || (t == kSmall && side < 512);
|
||||
}
|
||||
/// RX_VK_TEX_PACK_TIER=<0-3>: another tier (the lab's); unset or another word: by the card
|
||||
inline int tier_env(const char* e) {
|
||||
if (!e || !*e || e[1] || e[0] < '0' || e[0] > '3') return -1;
|
||||
return e[0] - '0';
|
||||
}
|
||||
|
||||
} // namespace mkwvk::tex_pack_bc7
|
||||
Reference in New Issue
Block a user