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:
ruckus7382sfgsgdf
2026-11-30 20:55:14 -06:00
parent 8cb23131f9
commit 822ee913bc
86 changed files with 8128 additions and 438 deletions

View File

@@ -77,6 +77,7 @@
#include "linux_pressure.h" // ((Triforce, 11-22) the performance audit's MEM-141: the swap hold's valves here (arenas, malloc_trim, budget)
#include "tex_detail.h"
#include "tex_pack.h" // ((Triforce, 11-24) Evil HD's texture pack: pack_view)
#include "tex_pack_bc7.h" // ((Triforce, 11-28) the pack as BC7, prepared once: texpack)
#include "vk_mods.h" // (its files decoded as the Mods page's pictures are)
#include <condition_variable>
#include <deque>
@@ -5541,18 +5542,17 @@ VkImageView twin3d_view(Texture* tex, VkFormat f, uint32_t level0, uint32_t nlev
// ---- ((Triforce, 11-24) Evil HD's texture pack, tex_pack.h) While Evil HD is on, a colour texture the pack has a file for is
// sampled from an image of its own, twice each way, made from that file (an AI upscale of the texture, the pack's tool's): the
// game's image and memory are as they were. The file is read and decoded off the replay (a thread of its own, Windows' WIC as
// the Mods page's pictures); once it is there the texture's views go stale (views_gone, destroy_gen) and its next sampled
// view (texture_view) makes the image: RGBA8, its levels made by blits from the file's, in the uploads of that recording, as
// the 3D twin above is. A texture the GPU writes, or one of another shape, is never taken. The pack's images together stay
// under kPackBudget of video memory; past it none is made. RX_VK_TEX_PACK=0: no pack; RX_VK_TEX_PACK_LOG=1: each key looked
// game's image and memory are as they were. ((Triforce, 11-28) The pack's PNGs are prepared once as BC7 files (tex_pack_bc7.h,
// prepare_start: the first time Evil HD is on, on most of the cores, the foot of the screen saying how far), and none is used
// before that is done, so they turn HD together. A texture's file is then read off the replay (kReaders threads); once it is
// there the texture's views go stale (views_gone, destroy_gen) and its next sampled view (texture_view) makes the image: BC7 with
// the file's levels, in the uploads of that recording, as the 3D twin above is. A texture the GPU writes, or one of another
// shape, is never taken; which ones is the tier's, by the card, once a run; images go only past the emergency line
// (texpack_pressure). RX_VK_TEX_PACK=0: no pack; RX_VK_TEX_PACK_LOG=1: each key looked
// up, found or not (the lab's, to make a pack for a place). The folder: triforce_texpack in the game folder ----
namespace texpack {
constexpr uint64_t kPackBudget = 3ull << 30;
// ((Triforce, 11-27) the pack's share of the card (texpack_pressure): a fifth of what the driver allows, kPackBudget at most;
// and whether the use is past the line where the pack's images go before any of the game's textures (no new one meanwhile)
uint64_t cap = kPackBudget;
// (whether the use is past the emergency line, texpack_pressure: no new image meanwhile)
bool pressed = false;
/// A pack file's picture as RGBA8 (false: not a picture this reads)
bool decode_png(const std::string& bytes, std::vector<unsigned char>& rgba, int* w, int* h) {
@@ -5572,22 +5572,22 @@ constexpr char kSep = '\\';
#else
constexpr char kSep = '/';
#endif
// ((Triforce, 11-28) a texture's prepared file as read: its size, its levels (each BC7, level 0 first) one after another
struct Decoded {
int w = 0, h = 0;
std::vector<unsigned char> rgba;
// (the pack's own smaller levels, <key>_m1.png on, when it has them: the texture's own level 0 as is, its halves made
// sharp: what is seen from afar stays the game's. Each half the one before, or the list stops there)
std::vector<Decoded> more;
uint32_t levels = 0;
std::vector<unsigned char> data;
};
// ((Triforce, 11-25) his v0.8.0 stutter: every decoded file stayed in RAM for good (private 15.7 -> 28 GB, the PC 1.6 GB free).
// Now a decoded file is held only until its image is made (take), or kKeepFrames when no look takes it; the decoder waits
// while kRamCap of them are held; the images made in one present stay under kMakeBudget of staging; and the views of the
// textures whose file came go stale together every kFlipEvery presents, not at each file (each flip renews the descriptors))
// Now a read file is held only until its image is made (take), or kKeepFrames when no look takes it; the readers wait while
// kRamCap of them are held; the images made in one present stay under kMakeBudget of staging; and the views of the textures
// whose file came go stale together every kFlipEvery presents, not at each file (each flip renews the descriptors))
constexpr uint64_t kRamCap = 768ull << 20;
constexpr uint64_t kMakeBudget = 96ull << 20;
constexpr uint64_t kFlipEvery = 8;
constexpr uint64_t kMakeBudget = 192ull << 20; // ((Triforce, 11-28) BC7: four times the textures of the RGBA8 96 MB)
constexpr uint64_t kFlipEvery = 4;
constexpr uint64_t kKeepFrames = 240;
constexpr uint64_t kRetryFrames = 600; // (a texture the budget turned away: asked again no sooner)
constexpr uint64_t kRetryFrames = 600; // (a texture the emergency line turned away: asked again no sooner)
constexpr int kReaders = 4;
struct Held {
std::shared_ptr<Decoded> d;
uint64_t bytes = 0, frame = 0; // (its RAM; the present it came at)
@@ -5595,14 +5595,160 @@ struct Held {
std::mutex mu;
std::condition_variable cv;
std::deque<uint64_t> queue;
std::unordered_set<uint64_t> index, asked; // (asked: being decoded or held in done; out of both once taken or let go)
std::unordered_set<uint64_t> index, asked; // (asked: being read or held in done; out of both once taken or let go)
std::unordered_map<uint64_t, Held> done;
std::vector<std::pair<Texture*, uint32_t>> waiting; // (textures whose file is being decoded: their views go stale when it is)
std::vector<std::pair<Texture*, uint32_t>> waiting; // (textures whose file is being read: their views go stale when it is)
std::vector<std::pair<Texture*, uint32_t>> retry; // (textures whose image waits for a later present's kMakeBudget)
std::atomic<uint64_t> done_count{0}, done_bytes{0}, frame_no{0};
uint64_t bytes = 0; // (the pack's images' video memory; the replay thread's)
uint64_t made_now = 0; // (the staging of the images made in this present; the replay thread's)
std::string folder;
// ((Triforce, 11-28) the prepare: every pack file as BC7 in folder/bc7, once (tex_pack_bc7.h). No pack image is used before it
// is done, so the textures turn HD together. tier: the textures it stands in for, by the card (-1 until the first look)
std::atomic<bool> ready{false}, prepare_began{false};
std::atomic<uint64_t> prep_total{0}, prep_done{0}, prep_made{0}, prep_failed{0};
std::atomic<int64_t> ready_at_ns{0};
std::atomic<int> tier{-1};
std::string file_of(uint64_t k, const char* tail) {
char name[40];
std::snprintf(name, sizeof name, "%016llx%s", (unsigned long long)k, tail);
return folder + kSep + name;
}
std::string bc7_path(uint64_t k) {
char name[40];
std::snprintf(name, sizeof name, "bc7%c%016llx.bc7", kSep, (unsigned long long)k);
return folder + kSep + name;
}
bool read_all(const std::string& path, std::string& out) {
out.clear();
FILE* f = fopen(path.c_str(), "rb");
if (!f) return false;
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size > 0) {
out.resize((size_t)size);
if (fread(out.data(), 1, out.size(), f) != out.size()) out.clear();
}
fclose(f);
return !out.empty();
}
uint64_t file_size(const std::string& path) {
std::error_code ec;
const auto n = std::filesystem::file_size(path, ec);
return ec ? 0 : (uint64_t)n;
}
/// A pack file's prepared BC7 file is there, whole, and of its PNG as it is now
bool prepared(uint64_t k) {
namespace tb = tex_pack_bc7;
const std::string path = bc7_path(k);
const uint64_t n = file_size(path);
if (n < tb::kHeader) return false;
uint8_t head[tb::kHeader];
FILE* f = fopen(path.c_str(), "rb");
if (!f) return false;
const bool got = fread(head, 1, sizeof head, f) == sizeof head;
fclose(f);
if (!got) return false;
uint32_t v[5];
std::memcpy(v, head, sizeof v);
tb::Header hd;
hd.w = v[2], hd.h = v[3], hd.levels = v[4];
std::memcpy(&hd.png_size, head + sizeof v, 8);
return v[0] == tb::kMagic && v[1] == tb::kVersion && hd.w && hd.h && hd.levels &&
tb::file_bytes(hd.w, hd.h, hd.levels) == n && hd.png_size == file_size(file_of(k, ".png"));
}
/// One pack file made BC7: its PNG, its own smaller levels (<key>_m1.png on: the texture's own level 0 as is, its halves made
/// sharp, while each is half the one before), the rest halved down to 1 x 1, each level encoded; written beside, then renamed
bool prepare_one(uint64_t k) {
namespace tb = tex_pack_bc7;
std::string bytes_in;
std::vector<unsigned char> rgba;
int w = 0, h = 0;
if (!read_all(file_of(k, ".png"), bytes_in) || !decode_png(bytes_in, rgba, &w, &h) || w <= 0 || h <= 0) return false;
const uint32_t levels = tb::full_levels((uint32_t)w, (uint32_t)h);
tb::Header hd;
hd.w = (uint32_t)w, hd.h = (uint32_t)h, hd.levels = levels, hd.png_size = (uint64_t)bytes_in.size();
std::vector<uint8_t> out((size_t)tb::file_bytes(hd.w, hd.h, levels));
tb::write_header(out.data(), hd);
size_t at = tb::kHeader;
std::vector<uint8_t> cur(rgba.begin(), rgba.end());
uint32_t cw = hd.w, ch = hd.h;
bool own = true;
for (uint32_t l = 0; l < levels; l++) {
if (l) {
uint32_t nw = 0, nh = 0;
std::vector<unsigned char> m;
int mw = 0, mh = 0;
char tail[16];
std::snprintf(tail, sizeof tail, "_m%u.png", l);
if (own && read_all(file_of(k, tail), bytes_in) && decode_png(bytes_in, m, &mw, &mh) &&
(uint32_t)mw == std::max(1u, cw / 2) && (uint32_t)mh == std::max(1u, ch / 2)) {
cur.assign(m.begin(), m.end()), nw = (uint32_t)mw, nh = (uint32_t)mh;
} else {
own = false;
cur = tb::half(cur, cw, ch, &nw, &nh);
}
cw = nw, ch = nh;
}
tb::encode(cur.data(), cw, ch, out.data() + at);
at += (size_t)tb::level_bytes(cw, ch);
}
const std::string path = bc7_path(k), part = path + ".part";
FILE* f = fopen(part.c_str(), "wb");
if (!f) return false;
const bool wrote = fwrite(out.data(), 1, out.size(), f) == out.size();
fclose(f);
std::error_code ec;
if (wrote) std::filesystem::rename(part, path, ec);
if (!wrote || ec) {
std::filesystem::remove(part, ec);
return false;
}
return true;
}
/// (texpack_tick, the first time Evil HD is on) Every pack file not prepared yet, on every core but four (two at least), below
/// the game's threads; then ready
void prepare_start() {
if (prepare_began.exchange(true)) return;
auto list = std::make_shared<std::vector<uint64_t>>(index.begin(), index.end());
prep_total = list->size();
std::error_code ec;
std::filesystem::create_directories(folder + kSep + "bc7", ec);
const unsigned hw = std::max(1u, std::thread::hardware_concurrency());
const unsigned n = hw > 6 ? hw - 4 : 2u;
auto next = std::make_shared<std::atomic<size_t>>(0);
auto left = std::make_shared<std::atomic<unsigned>>(n);
const auto t0 = std::chrono::steady_clock::now();
logf("vk: texture pack: preparing its %zu textures as BC7 (once: %s%cbc7) on %u threads", list->size(), folder.c_str(), kSep, n);
for (unsigned t = 0; t < n; t++)
std::thread([next, left, list, t0, n] {
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
#endif
for (size_t i; (i = next->fetch_add(1)) < list->size();) {
const uint64_t k = (*list)[i];
if (!prepared(k)) {
if (prepare_one(k)) prep_made.fetch_add(1);
else {
prep_failed.fetch_add(1);
static std::atomic<int> told{0};
if (told.fetch_add(1) < 8)
logf("vk: texture pack: %016llx could not be prepared (its PNG unread or not a picture)", (unsigned long long)k);
}
}
prep_done.fetch_add(1);
}
if (left->fetch_sub(1) == 1) {
const double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
logf("vk: texture pack: prepared: %llu made now, %llu there already, %llu failed, in %.1f s on %u threads: the pack's "
"textures in use from here", (unsigned long long)prep_made.load(),
(unsigned long long)(prep_total.load() - prep_made.load() - prep_failed.load()), (unsigned long long)prep_failed.load(), s, n);
ready_at_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
ready = true;
}
}).detach();
}
bool on() {
static const bool v = [] {
if (const char* e = getenv("RX_VK_TEX_PACK"); e && !strcmp(e, "0")) return false;
@@ -5623,64 +5769,36 @@ bool on() {
}
logf("vk: Evil HD texture pack: %zu textures in %s", index.size(), folder.c_str());
if (index.empty()) return false;
std::thread([] {
for (;;) {
uint64_t k;
{
std::unique_lock lk(mu);
cv.wait(lk, [] { return !queue.empty() && done_bytes.load(std::memory_order_relaxed) < kRamCap; });
k = queue.front();
queue.pop_front();
}
char name[24];
tex_pack::file_name(k, name);
auto d = std::make_shared<Decoded>();
std::string bytes_in;
if (FILE* f = fopen((folder + kSep + name).c_str(), "rb")) {
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size > 0) {
bytes_in.resize((size_t)size);
if (fread(bytes_in.data(), 1, bytes_in.size(), f) != bytes_in.size()) bytes_in.clear();
for (int r = 0; r < kReaders; r++) // ((Triforce, 11-28) the prepared files, read as they are asked for)
std::thread([] {
for (;;) {
uint64_t k;
{
std::unique_lock lk(mu);
cv.wait(lk, [] { return !queue.empty() && done_bytes.load(std::memory_order_relaxed) < kRamCap; });
k = queue.front();
queue.pop_front();
}
fclose(f);
}
if (bytes_in.empty() || !decode_png(bytes_in, d->rgba, &d->w, &d->h)) d->w = d->h = 0;
for (int m = 1; d->w && m < 16; m++) {
char more[40];
std::snprintf(more, sizeof more, "%016llx_m%d.png", (unsigned long long)k, m);
std::string b;
if (FILE* f = fopen((folder + kSep + more).c_str(), "rb")) {
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size > 0) {
b.resize((size_t)size);
if (fread(b.data(), 1, b.size(), f) != b.size()) b.clear();
}
fclose(f);
auto d = std::make_shared<Decoded>();
std::string bytes_in;
tex_pack_bc7::Header hd;
if (read_all(bc7_path(k), bytes_in) &&
tex_pack_bc7::read_header((const uint8_t*)bytes_in.data(), bytes_in.size(), &hd)) {
d->w = (int)hd.w, d->h = (int)hd.h, d->levels = hd.levels;
d->data.assign(bytes_in.begin() + tex_pack_bc7::kHeader, bytes_in.end());
}
Decoded l;
const int pw = m == 1 ? d->w : d->more.back().w, ph = m == 1 ? d->h : d->more.back().h;
if (b.empty() || !decode_png(b, l.rgba, &l.w, &l.h) || l.w != std::max(1, pw / 2) ||
l.h != std::max(1, ph / 2))
break;
d->more.push_back(std::move(l));
const uint64_t held = d->data.size();
{
std::lock_guard lk(mu);
asked.insert(k); // (held: not asked again; let go after kKeepFrames if no look takes it)
Held& h = done[k];
done_bytes -= h.bytes;
h = {d, held, frame_no.load(std::memory_order_relaxed)};
done_bytes += held;
}
done_count.fetch_add(1, std::memory_order_release);
}
uint64_t held = d->rgba.size();
for (const auto& l : d->more) held += l.rgba.size();
{
std::lock_guard lk(mu);
asked.insert(k); // (held: not asked again; let go after kKeepFrames if no look takes it)
Held& h = done[k];
done_bytes -= h.bytes;
h = {d, held, frame_no.load(std::memory_order_relaxed)};
done_bytes += held;
}
done_count.fetch_add(1, std::memory_order_release);
}
}).detach();
}).detach();
return true;
}();
return v;
@@ -5785,12 +5903,17 @@ bool srgb(VkFormat f) {
/// The pack's view of a texture sampled flat (type 2D, one layer) while Evil HD is on: the game's levels from level0, nlevels
/// of them, as the image twice its size has them (its level L is the game's L - 1: one level more on top). VK_NULL_HANDLE:
/// the game's own view (no file for it, not decoded yet, another shape, the GPU writes it, no memory)
/// the game's own view (no file for it, not read yet, another shape, the GPU writes it, not its tier, the pack not prepared yet)
/// ((Triforce, 11-28) BC7 from the prepared file (tex_pack_bc7.h), its levels as the file has them: no blit, no budget share.
/// Which textures is the tier's, decided once by the card; only the emergency line (texpack_pressure) lets images go)
VkImageView pack_view(Texture* tex, VkFormat f, uint32_t level0, uint32_t nlevels, const VkComponentMapping& cm, VkFormat* format_out) {
if (tex->pack_state == 1 || !texpack::on()) return VK_NULL_HANDLE;
namespace tb = tex_pack_bc7;
if (tex->pack_state == 1 || !texpack::on() || !texpack::ready.load(std::memory_order_acquire)) return VK_NULL_HANDLE;
const int tier = texpack::tier.load(std::memory_order_relaxed);
if (tier < 0) return VK_NULL_HANDLE; // (the card not known yet)
const uint32_t fw = tex->width << tex->skip, fh = tex->height << tex->skip;
if (tex->type != VK_IMAGE_TYPE_2D || tex->layers != 1 || tex->cube || tex->sparse || tex->aspect != VK_IMAGE_ASPECT_COLOR_BIT ||
tex->samples != VK_SAMPLE_COUNT_1_BIT || !tex_pack::wanted(true, 1, true, fw, fh)) {
tex->samples != VK_SAMPLE_COUNT_1_BIT || !tex_pack::wanted(true, 1, true, fw, fh) || !tb::tier_wants((tb::Tier)tier, fw, fh)) {
tex->pack_state = 1; // (never one the pack stands in for)
return VK_NULL_HANDLE;
}
@@ -5813,69 +5936,61 @@ VkImageView pack_view(Texture* tex, VkFormat f, uint32_t level0, uint32_t nlevel
}
texpack::want(tex);
}
// ((Triforce, 11-25) 4: its image did not fit kPackBudget, its file let go: asked again once there is room)
const uint64_t want_need = (uint64_t)fw * tex_pack::kScale * fh * tex_pack::kScale * 4 * 4 / 3;
if (tex->pack_state == 4 && texpack::frame_no.load(std::memory_order_relaxed) >= tex->pack_retry_at && !texpack::pressed &&
texpack::bytes + want_need <= texpack::cap)
// (4: let go at the emergency line, its file too: asked again once the use is back under it)
const uint32_t w = fw * tex_pack::kScale, h = fh * tex_pack::kScale;
uint32_t levels = 1;
while (levels < tex->levels + tex->skip + 1 && (std::max(w, h) >> levels)) levels++;
uint64_t need = 0;
for (uint32_t l = 0; l < levels; l++) need += tb::level_bytes(std::max(1u, w >> l), std::max(1u, h >> l));
if (tex->pack_state == 4 && texpack::frame_no.load(std::memory_order_relaxed) >= tex->pack_retry_at && !texpack::pressed)
texpack::want(tex);
if (tex->pack_state != 3) return VK_NULL_HANDLE;
if (!tex->hd) {
if (texpack::bytes + want_need > texpack::cap || texpack::pressed) { // ((Triforce, 11-27) its share, and not under pressure)
static std::atomic<int> told{0};
if (told.fetch_add(1) < 4) logf("vk: texture pack: its %llu MB of video memory are used: the game's own textures from here",
(unsigned long long)(texpack::bytes >> 20));
texpack::let_go(tex->pack_key); // (not held in RAM meanwhile)
if (texpack::pressed) { // (the emergency line: no new image while the use is past it)
texpack::let_go(tex->pack_key);
tex->pack_state = 4, tex->pack_retry_at = texpack::frame_no.load(std::memory_order_relaxed) + texpack::kRetryFrames;
return VK_NULL_HANDLE;
}
if (texpack::made_now && texpack::made_now + want_need > texpack::kMakeBudget) { // (the next presents': no spike)
if (texpack::made_now && texpack::made_now + need > texpack::kMakeBudget) { // (the next presents': no spike)
texpack::later(tex);
return VK_NULL_HANDLE;
}
const auto d = texpack::take(tex->pack_key);
if (!d) { // (let go before a look took it: decoded again)
if (!d) { // (let go before a look took it: read again)
texpack::want(tex);
return VK_NULL_HANDLE;
}
if (d->w != (int)(fw * tex_pack::kScale) || d->h != (int)(fh * tex_pack::kScale)) {
if (d->w != (int)w || d->h != (int)h || d->levels < levels) {
static std::atomic<int> told{0};
if (told.fetch_add(1) < 16)
logf("vk: texture pack: %016llx is %dx%d, not twice %ux%u: the game's own", (unsigned long long)tex->pack_key,
d->w, d->h, fw, fh);
logf("vk: texture pack: %016llx is %dx%d with %u levels, not twice %ux%u with %u: the game's own (its prepared file "
"missing or another's)", (unsigned long long)tex->pack_key, d->w, d->h, d->levels, fw, fh, levels);
tex->pack_state = 1;
return VK_NULL_HANDLE;
}
const uint32_t w = (uint32_t)d->w, h = (uint32_t)d->h;
uint32_t levels = 1;
while (levels < tex->levels + tex->skip + 1 && (std::max(w, h) >> levels)) levels++;
const uint64_t need = (uint64_t)w * h * 4 * 4 / 3;
VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
ci.imageType = VK_IMAGE_TYPE_2D, ci.format = VK_FORMAT_R8G8B8A8_UNORM;
ci.imageType = VK_IMAGE_TYPE_2D, ci.format = VK_FORMAT_BC7_UNORM_BLOCK;
ci.extent = {w, h, 1}, ci.mipLevels = levels, ci.arrayLayers = 1, ci.samples = VK_SAMPLE_COUNT_1_BIT;
ci.tiling = VK_IMAGE_TILING_OPTIMAL;
ci.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
ci.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
ci.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; // (seen as sRGB where the game's view is)
uint64_t staged = (uint64_t)w * h * 4;
uint32_t own = 1; // (levels the pack gives: level 0 and its _m files, as many as the image has)
for (const auto& l : d->more)
if (own < levels) staged += ((uint64_t)l.w * l.h * 4 + 255) & ~255ull, own++;
const Staging s = staging(staged + 256, 256);
const Staging s = staging(need + 256, 256);
if (!s.buffer || !create_image(ci, tex->hd, tex->hd_alloc, false, false, false)) {
tex->hd = VK_NULL_HANDLE, tex->hd_alloc = nullptr;
return VK_NULL_HANDLE;
}
std::vector<VkBufferImageCopy> regions;
std::memcpy(s.data, d->data.data(), (size_t)need); // (the file's levels one after another, as the copies read them)
{
uint64_t at = 0;
for (uint32_t l = 0; l < own; l++) {
const texpack::Decoded& src = l == 0 ? *d : d->more[l - 1];
std::memcpy(s.data + at, src.rgba.data(), (size_t)src.w * src.h * 4);
for (uint32_t l = 0; l < levels; l++) {
const uint32_t lw = std::max(1u, w >> l), lh = std::max(1u, h >> l);
VkBufferImageCopy r{};
r.bufferOffset = s.offset + at;
r.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, l, 0, 1};
r.imageExtent = {(uint32_t)src.w, (uint32_t)src.h, 1};
r.imageExtent = {lw, lh, 1};
regions.push_back(r);
at += ((uint64_t)src.w * src.h * 4 + 255) & ~255ull;
at += tb::level_bytes(lw, lh);
}
flush_mapped_range(s.allocation, s.offset, at);
}
@@ -5894,30 +6009,21 @@ VkImageView pack_view(Texture* tex, VkFormat f, uint32_t level0, uint32_t nlevel
}
VkCommandBuffer cmd = upload_cmd();
vkCmdCopyBufferToImage(cmd, s.buffer, tex->hd, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)regions.size(), regions.data());
for (uint32_t l = own; l < levels; l++) { // (each level the pack does not give, from the one above)
full_barrier(cmd, "barrier (Evil HD's texture pack: a level made)");
VkImageBlit bl{};
bl.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, l - 1, 0, 1};
bl.srcOffsets[1] = {(int32_t)std::max(1u, w >> (l - 1)), (int32_t)std::max(1u, h >> (l - 1)), 1};
bl.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, l, 0, 1};
bl.dstOffsets[1] = {(int32_t)std::max(1u, w >> l), (int32_t)std::max(1u, h >> l), 1};
vkCmdBlitImage(cmd, tex->hd, VK_IMAGE_LAYOUT_GENERAL, tex->hd, VK_IMAGE_LAYOUT_GENERAL, 1, &bl, VK_FILTER_LINEAR);
}
full_barrier(cmd, "barrier (Evil HD's texture pack: made)");
tex->hd_levels = levels, tex->hd_bytes = need;
texpack::bytes += need;
texpack::made_now += staged;
texpack::made_now += need;
static std::atomic<int> made{0};
const int m = made.fetch_add(1) + 1;
if (m <= 8 || !(m & (m - 1)))
logf("vk: texture pack: %d made so far (%016llx, %ux%u from %ux%u; %llu MB in all; decoded files held %llu MB)", m,
logf("vk: texture pack: %d made so far (%016llx, %ux%u BC7 from %ux%u; %llu MB in all; read files held %llu MB)", m,
(unsigned long long)tex->pack_key, w, h, fw, fh, (unsigned long long)(texpack::bytes >> 20),
(unsigned long long)(texpack::done_bytes.load(std::memory_order_relaxed) >> 20));
}
// (the game's level L is the pack's L + 1: the pack's level0 = the game's image level + its skip, one level more on top)
const uint32_t l0 = std::min(level0 + tex->skip, tex->hd_levels - 1);
const uint32_t nl = std::clamp<uint32_t>(nlevels + 1, 1, tex->hd_levels - l0);
const VkFormat hf = texpack::srgb(f) ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
const VkFormat hf = texpack::srgb(f) ? VK_FORMAT_BC7_SRGB_BLOCK : VK_FORMAT_BC7_UNORM_BLOCK;
uint64_t key = mix((uint64_t)hf, (uint64_t)l0 << 32 | nl);
key = mix(key, (uint64_t)cm.r << 24 | (uint64_t)cm.g << 16 | (uint64_t)cm.b << 8 | cm.a);
if (format_out) *format_out = hf;
@@ -5941,19 +6047,32 @@ void texpack_gone(Texture& tex) {
texpack::bytes -= std::min(texpack::bytes, (uint64_t)tex.hd_bytes);
tex.hd = VK_NULL_HANDLE, tex.hd_alloc = nullptr, tex.hd_views.clear();
}
/// ((Triforce, 11-27) texture_frame's, each look with the driver's count: the pack's share and its yielding. A player's RX 6800
/// XT (16 GB, ~13.5 GB allowed) ran Castle Town at ~30 fps with "textures 3489 MB (budget 1208 MB)": the pack's images (up to
/// 3 GB) were not textures to the budget, so the live line squeezed the game's own instead, which were made again at every
/// turn (texture lookups 20 ms a frame), each taking its pack image with it to be decoded again; an RTX 3060 Ti (8 GB, 1440p)
/// lost its device at "textures 1041 MB (budget 181 MB)". Now the pack's share is a fifth of what the driver allows (3 GB at
/// most), and past 75% of it (under the textures' own lines: the live one's ~80%, the 90% rule) the pack's images go first, the least recently used, down to 2% under that line, before any of
/// the game's textures: those textures are drawn from the game's own image (pack_state 4, asked again after kRetryFrames once
/// the use is back under the line). No pack image is made while the use is past it
void texpack_pressure(bool known, uint64_t pressure, uint64_t allowed) {
/// ((Triforce, 11-27/28) texture_frame's, each look with the driver's count. 11-27: a player's RX 6800 XT (16 GB) ran Castle Town at
/// ~30 fps with the pack's RGBA8 images (up to 3 GB) outside the textures' budget, and an RTX 3060 Ti (8 GB) lost its device; so
/// the pack had a share and gave way at 75%, then 88%. The owner, 11-28: "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". So the pack is BC7 (a quarter of the
/// memory) and which textures it stands in for is decided once a run by what the driver allows (tex_pack_bc7.h tier_for: at
/// Hyrule Field 4.7 GB for all, 2.7 GB without those of 1024 or more), the same textures every time. Its images are let go only
/// at the emergency line, 95% of what the driver allows (RX_VK_TEX_PACK_LINE=<pct>, 20 to 98), the least recently used, down to 2%
/// under it, before the device runs short; none is made while the use is past it
void texpack_pressure(bool known, uint64_t pressure, uint64_t allowed, uint64_t /*live_pct*/) {
namespace tb = tex_pack_bc7;
if (!texpack::on() || !known || !allowed) return;
texpack::cap = std::min<uint64_t>(texpack::kPackBudget, allowed / 5);
// (RX_VK_TEX_PACK_LINE=<pct>, 20 to 95: another line, the lab's way to see the yielding on a card with room)
static const uint64_t pct = getenv("RX_VK_TEX_PACK_LINE") ? (uint64_t)std::clamp(atoi(getenv("RX_VK_TEX_PACK_LINE")), 20, 95) : 75;
if (texpack::tier.load(std::memory_order_relaxed) < 0) { // (once: the card's tier, or the lab's)
static const int env = tb::tier_env(getenv("RX_VK_TEX_PACK_TIER"));
VkFormatProperties fp{};
vkGetPhysicalDeviceFormatProperties(dev.physical, VK_FORMAT_BC7_UNORM_BLOCK, &fp);
const bool bc7 = dev.features.textureCompressionBC && (fp.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT);
uint64_t heap = 0; // (the card's own memory: the same every run)
for (uint32_t h = 0; h < dev.memory.memoryHeapCount; h++)
if (dev.memory.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) heap = std::max(heap, (uint64_t)dev.memory.memoryHeaps[h].size);
const tb::Tier t = !bc7 ? tb::kNone : env >= 0 ? (tb::Tier)env : tb::tier_for(heap >> 20);
texpack::tier = (int)t;
logf("vk: texture pack: a card of %llu MB (%llu MB allowed now): it stands in for %s%s (RX_VK_TEX_PACK_TIER=0-3)",
(unsigned long long)(heap >> 20), (unsigned long long)(allowed >> 20), tb::tier_name(t),
!bc7 ? "; this GPU samples no BC7" : env >= 0 ? ", by RX_VK_TEX_PACK_TIER" : "");
}
static const uint64_t pct = getenv("RX_VK_TEX_PACK_LINE") ? (uint64_t)std::clamp(atoi(getenv("RX_VK_TEX_PACK_LINE")), 20, 98) : 95;
const uint64_t line = allowed / 100 * pct;
texpack::pressed = pressure > line;
if (!texpack::pressed || !texpack::bytes) return;
@@ -5970,7 +6089,7 @@ void texpack_pressure(bool known, uint64_t pressure, uint64_t allowed) {
freed += t->hd_bytes;
views_gone.went(t->id);
texpack_gone(*t);
texpack::let_go(t->pack_key); // (its decoded file not held in RAM meanwhile)
texpack::let_go(t->pack_key); // (its read file not held in RAM meanwhile)
t->pack_state = 4, t->pack_retry_at = now + texpack::kRetryFrames;
n++;
}
@@ -5978,10 +6097,26 @@ void texpack_pressure(bool known, uint64_t pressure, uint64_t allowed) {
destroy_gen++;
static std::atomic<int> told{0};
if (told.fetch_add(1, std::memory_order_relaxed) < 8)
logf("vk: texture pack: VRAM past %llu%% of what the driver allows (%llu of %llu MB): %d of its images let go first, %llu MB "
"(the least recently used; %llu MB of them now, its share %llu MB)", (unsigned long long)pct, (unsigned long long)(pressure >> 20),
(unsigned long long)(allowed >> 20), n, (unsigned long long)(freed >> 20), (unsigned long long)(texpack::bytes >> 20),
(unsigned long long)(texpack::cap >> 20));
logf("vk: texture pack: VRAM past %llu%% of what the driver allows (%llu of %llu MB), the emergency line: %d of its images let "
"go, %llu MB (the least recently used; %llu MB of them now)", (unsigned long long)pct, (unsigned long long)(pressure >> 20),
(unsigned long long)(allowed >> 20), n, (unsigned long long)(freed >> 20), (unsigned long long)(texpack::bytes >> 20));
}
/// ((Triforce, 11-28) the foot-of-screen line while the pack is prepared (vk_menu.cpp shows it): true with the text while the
/// prepare runs with files to make, and for 4 s after it ended
bool texpack_prepare_text(char* out, size_t cap) {
if (!texpack::prepare_began.load(std::memory_order_relaxed)) return false;
const uint64_t total = texpack::prep_total.load(), done_n = texpack::prep_done.load(), made = texpack::prep_made.load();
if (!texpack::ready.load(std::memory_order_acquire)) {
if (!made && done_n < total / 20) return false; // (still checking the files there: nothing said yet)
std::snprintf(out, cap, "Evil HD: preparing HD textures (once) %llu%%",
(unsigned long long)(total ? done_n * 100 / total : 100));
return true;
}
if (!made) return false;
const int64_t now = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
if (now - texpack::ready_at_ns.load() > 4000000000LL) return false;
std::snprintf(out, cap, "Evil HD: HD textures ready");
return true;
}
/// (texture_frame) decoded files' textures, and Evil HD turned (F2, the row): the views that had the game's or the pack's
/// image go stale, so the next look takes the other
@@ -5990,6 +6125,29 @@ void texpack_tick() {
texpack::frame();
static int was = -1;
const int now = evil_hd_now();
if (now) texpack::prepare_start(); // ((Triforce, 11-28) once: the pack as BC7 (a few minutes the first time, then none))
if (now && texpack::ready.load(std::memory_order_acquire) && texpack::frame_no.load(std::memory_order_relaxed) % 1200 == 0) {
// (every 1,200 presents while it is in use, 40 lines: the textures it stands in for and those waiting, by state)
static int told = 0;
if (told < 40) {
int st[5] = {}, hd = 0;
for (auto& kv : textures)
if (kv.second) st[std::min<int>(kv.second->pack_state, 4)]++, hd += kv.second->hd != VK_NULL_HANDLE;
told++;
logf("vk: texture pack: %d textures drawn from it (%llu MB), %d being read, %d read and waiting for their image, %d let "
"go at the emergency line; %d not its, %d not looked at yet", hd, (unsigned long long)(texpack::bytes >> 20), st[2],
st[3] - hd > 0 ? st[3] - hd : 0, st[4], st[1], st[0]);
}
}
{ // (prepared: every texture looks again, so the pack's turn HD together)
static bool was_ready = false;
if (!was_ready && texpack::ready.load(std::memory_order_acquire)) {
was_ready = true;
for (auto& kv : textures)
if (kv.second && kv.second->pack_state != 1) views_gone.went(kv.second->id);
destroy_gen++;
}
}
if (was >= 0 && now != was) {
bool any = false;
const uint64_t before = texpack::bytes;
@@ -8342,7 +8500,7 @@ void texture_frame(uint64_t completed) {
if (textures_pool_usage(&pool_blocks, &pool_free)) reclaim = vram_compact::reclaimable(block_free, pool_free);
}
const uint64_t pressure = by_blocks ? used : used - std::min(used, reclaim);
texpack_pressure(known, pressure, allowed); // ((Triforce, 11-27) Evil HD's pack yields first: texpack_pressure)
texpack_pressure(known, pressure, allowed, press.live ? press_pct : 0); // ((Triforce, 11-27/28) Evil HD's pack: texpack_pressure)
pool_pressed = known && pressure > allowed / 10 * 9; // (the target pool given back meanwhile)
pool_tier = target_pool::tier(known, pressure, allowed); // ((Triforce, 11-21, TEX-29) halved above 90%, none above 97%)
compact.pressed = known && vram_compact::pressed(used, allowed); // (hookshot, RX_VK_COMPACT: by the driver's own count)