mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-29 04:18:49 -04:00
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
498 lines
28 KiB
Python
498 lines
28 KiB
Python
"""The bake write-back's scan walks a list of the textures it can take, not the whole texture cache (gpu/vk/bakescan.h,
|
|
vk_texture.cpp bake_writeback_step, texture_written, forget_gpu; no game, no GPU).
|
|
|
|
(Triforce, 11-26, the GPU audit's C36.) Every fourth present bake_writeback_step walked every texture of the cache for the
|
|
few the GPU drew in one frame and whose texels are not in the game's memory yet (the terrain's normal maps): 1.1% of the
|
|
Presentation Thread at Lake Hylia for a walk that finds nothing where the recordings write them back themselves. Now the
|
|
textures that can be taken are listed where they become so, and the step walks the list.
|
|
|
|
This compiles bakescan.h over textures of the test's own and a script of events (drawn, drawn again, sampled, released,
|
|
only cleared, a copy lost, destroyed, the image's contents dropped), with the writes of texture_written and the step's
|
|
predicate as the source had them:
|
|
- at every step the list gives exactly the textures the walk of the whole cache gives (as a set), and every texture the
|
|
rule calls armed is listed, at every present;
|
|
- a copy lost (wb_written back to 0) is taken again; one not sampled for long is taken once it is sampled again; one
|
|
destroyed while listed is not looked at again; one drawn every frame does not stay listed;
|
|
- under the step's caps the list takes what is left at the next steps: all of them, each once, as the walk did; one no
|
|
step can take (more than 8 MB of storage) does not stop the newer ones in the list, and stops the walk of the whole
|
|
cache as before;
|
|
- the step looks at the listed ones, not at the cache; the switch's values;
|
|
- with RX_VK_BAKE_WRITEBACK=0 or RX_VK_BAKE_SCAN=all (bakescan::kept false) nothing is listed: targets drawn every frame
|
|
and a tile leave the list empty, and the walk of the whole cache takes the tile.
|
|
|
|
And reads the source: the fields the rule reads can make a texture armed only where the list is told (texture_written, the
|
|
two places a copy is lost), with nothing that leaves texture_written before it is told, and a texture leaves the cache only
|
|
through forget_gpu, which tells it; every place that tells the list calls bake_list_note, which asks bake_list_kept()
|
|
first, and texture_written calls it without a branch of its own (its control flow, and so its entry in the renderer's play
|
|
profile, as before); the step walks the list unless RX_VK_BAKE_SCAN=all / check or the lab knob nobakescan, and passes
|
|
over in the list one no step can take; check mode lists what it counts; the scan's line is printed on its own, its listed
|
|
count the settle's; the switch is in the switches table.
|
|
|
|
python -I -B tests/test_bakescan.py [--cxx clang++]
|
|
"""
|
|
import argparse
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
VK = ROOT / "gpu" / "vk"
|
|
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
|
|
|
|
|
|
def check(ok, why):
|
|
if not ok:
|
|
raise SystemExit("FAIL: " + why)
|
|
|
|
|
|
def block(text, head):
|
|
at = text.index(head)
|
|
return text[at:text.index("\n}\n", at) + 3]
|
|
|
|
|
|
def flat(code):
|
|
"""code without its comments, its whitespace runs one space"""
|
|
return re.sub(r"\s+", " ", re.sub(r"//[^\n]*", "", code)).strip()
|
|
|
|
|
|
FIXTURE = r"""
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <vector>
|
|
#include "bakescan.h"
|
|
static int fails = 0;
|
|
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
|
|
struct Texture {
|
|
uint32_t id = 0;
|
|
bool gpu_written = false, released = false, only_cleared = false, image = true, wb_listed = false;
|
|
uint64_t first_written = 0, last_written = 0, wb_written = 0, last_frame = 0;
|
|
uint64_t bytes = 64 << 10, guest_size = 64 << 10, gpu = 0x1000;
|
|
};
|
|
static std::vector<std::unique_ptr<Texture>> textures; // (the cache)
|
|
static bakescan::List<Texture> bake_list;
|
|
static bool list_kept = true; // (bake_list_kept(): bakescan::kept of RX_VK_BAKE_WRITEBACK and RX_VK_BAKE_SCAN)
|
|
static void note(Texture* t) { // (bake_list_note, which every place that tells the list calls: the list kept, or nothing)
|
|
if (list_kept) bake_list.note(t);
|
|
}
|
|
static uint64_t frame_no = 8;
|
|
static uint32_t next_id = 1;
|
|
static Texture* make(uint64_t bytes = 64 << 10) {
|
|
textures.push_back(std::make_unique<Texture>());
|
|
Texture* t = textures.back().get();
|
|
t->id = next_id++, t->bytes = t->guest_size = bytes, t->gpu = 0x100000ull * t->id, t->last_frame = frame_no;
|
|
return t;
|
|
}
|
|
// (texture_written's writes of the fields the rule reads, in its order, and its hook last)
|
|
static void written(Texture* tex) {
|
|
tex->only_cleared = false;
|
|
if (!tex->gpu_written || tex->released) tex->first_written = frame_no;
|
|
tex->last_written = frame_no;
|
|
tex->gpu_written = true;
|
|
tex->released = false;
|
|
note(tex);
|
|
}
|
|
static void destroy(Texture* t) { // (texture_sweep: forget_gpu, then out of the cache)
|
|
bake_list.forget(t);
|
|
for (auto it = textures.begin(); it != textures.end(); ++it)
|
|
if (it->get() == t) { textures.erase(it); return; }
|
|
}
|
|
// (bake_writeback_step's predicate, as the walk of the whole cache had it before the list)
|
|
static bool old_takes(const Texture* t) {
|
|
if (!t->gpu_written || t->released || t->only_cleared || !t->first_written || t->first_written != t->last_written ||
|
|
t->wb_written == t->last_written || frame_no < t->last_written + 4 || frame_no - t->last_frame > 4 ||
|
|
t->bytes > (4ull << 20) || t->gpu >= (1ull << 39) || !t->image)
|
|
return false;
|
|
return true;
|
|
}
|
|
static bool new_takes(const Texture* t) {
|
|
return bakescan::armed(t->gpu_written, t->first_written, t->last_written, t->wb_written) &&
|
|
bakescan::ripe(t->released, t->only_cleared, t->last_written, t->last_frame, t->bytes, t->gpu, frame_no) && t->image;
|
|
}
|
|
static uint64_t looked = 0;
|
|
// (a step: what the list gives against what the whole walk gives; then the copies, under the caps, over the list or, with
|
|
// whole (RX_VK_BAKE_SCAN=all / check, the knob nobakescan), over the cache as the walk did; the ids taken)
|
|
static std::set<uint32_t> step(bool take = true, bool whole = false) {
|
|
std::set<uint32_t> by_walk, by_list, taken;
|
|
for (const auto& t : textures)
|
|
if (old_takes(t.get())) by_walk.insert(t->id);
|
|
static std::vector<Texture*> cands;
|
|
bake_list.settle(cands);
|
|
for (Texture* t : cands)
|
|
if (new_takes(t)) by_list.insert(t->id);
|
|
CHECK(list_kept ? by_walk == by_list : cands.empty());
|
|
std::vector<Texture*> order = cands;
|
|
if (whole) {
|
|
order.clear();
|
|
for (const auto& t : textures) order.push_back(t.get());
|
|
}
|
|
size_t items = 0;
|
|
uint64_t bytes = 0;
|
|
for (Texture* t : order) {
|
|
looked += !whole;
|
|
if (!new_takes(t) || !take) continue;
|
|
if (!whole && !bakescan::room(0, 0, t->guest_size)) continue; // (no step can take it: not the newer ones' stop)
|
|
if (!bakescan::room(items, bytes, t->guest_size)) break;
|
|
t->wb_written = t->last_written, items++, bytes += t->guest_size;
|
|
taken.insert(t->id);
|
|
}
|
|
return taken;
|
|
}
|
|
static void invariant() { // (what RX_VK_BAKE_SCAN=check counts: armed and not listed; and no list where it is not kept)
|
|
for (const auto& t : textures)
|
|
CHECK(list_kept ? !bakescan::armed(t->gpu_written, t->first_written, t->last_written, t->wb_written) || t->wb_listed
|
|
: !t->wb_listed);
|
|
}
|
|
static void present(int n = 1) {
|
|
for (int i = 0; i < n; i++) frame_no++, invariant();
|
|
}
|
|
static uint32_t rnd_state = 12345;
|
|
static uint32_t rnd(size_t n) {
|
|
rnd_state = rnd_state * 1664525u + 1013904223u;
|
|
return (rnd_state >> 8) % (uint32_t)n;
|
|
}
|
|
|
|
int main() {
|
|
using namespace bakescan;
|
|
// the switch
|
|
CHECK(mode_of(nullptr) == kList && mode_of("") == kList && mode_of("list") == kList && mode_of("1") == kList);
|
|
CHECK(mode_of("all") == kAll && mode_of("check") == kCheck && mode_of("0") == kAll);
|
|
// the rule's two halves are the walk's predicate
|
|
CHECK(armed(true, 5, 5, 0) && !armed(false, 5, 5, 0) && !armed(true, 0, 0, 7) && !armed(true, 4, 5, 0) && !armed(true, 5, 5, 5));
|
|
CHECK(ripe(false, false, 8, 12, 4ull << 20, (1ull << 39) - 1, 12));
|
|
CHECK(!ripe(true, false, 8, 12, 1, 1, 12) && !ripe(false, true, 8, 12, 1, 1, 12) && !ripe(false, false, 9, 12, 1, 1, 12));
|
|
CHECK(!ripe(false, false, 8, 7, 1, 1, 12) && ripe(false, false, 8, 8, 1, 1, 12) && !ripe(false, false, 8, 13, 1, 1, 12));
|
|
CHECK(!ripe(false, false, 8, 12, (4ull << 20) + 1, 1, 12) && !ripe(false, false, 8, 12, 1, 1ull << 39, 12));
|
|
CHECK(room(23, 0, 1) && !room(24, 0, 1) && room(0, (8ull << 20) - 4, 4) && !room(0, (8ull << 20) - 4, 5));
|
|
CHECK(room(0, 0, 8ull << 20) && !room(0, 0, (8ull << 20) + 1)); // (no step can take more than 8 MB of storage)
|
|
// the list kept only where something reads it: not with the write-back off, nor with the walk of the whole cache
|
|
CHECK(kept(true, kList) && kept(true, kCheck) && !kept(true, kAll) && !kept(false, kList) && !kept(false, kCheck));
|
|
|
|
// a cache of many textures the GPU never drew, some targets drawn every frame, and one tile baked once
|
|
for (int i = 0; i < 500; i++) make();
|
|
std::vector<Texture*> targets;
|
|
for (int i = 0; i < 6; i++) targets.push_back(make());
|
|
auto frame = [&](int n = 1) {
|
|
for (int i = 0; i < n; i++) {
|
|
for (Texture* t : targets) written(t), written(t), t->last_frame = frame_no;
|
|
present();
|
|
}
|
|
};
|
|
auto to_step = [&] { // (on to the next fourth present, at least four on)
|
|
frame(4);
|
|
while (frame_no % 4) frame();
|
|
};
|
|
frame(2);
|
|
Texture* tile = make();
|
|
written(tile);
|
|
CHECK(tile->wb_listed && bake_list.size() <= 1 + targets.size());
|
|
// sampled: taken at the first step four presents on, by both
|
|
looked = 0;
|
|
to_step(), tile->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{tile->id});
|
|
CHECK(looked <= 1 + targets.size()); // (the listed ones, not the 507 of the cache)
|
|
to_step(), tile->last_frame = frame_no;
|
|
CHECK(step().empty() && !tile->wb_listed);
|
|
CHECK(bake_list.size() == 0); // (a target drawn every frame left the list at the first step, and is not listed again)
|
|
for (Texture* t : targets) CHECK(!t->wb_listed);
|
|
|
|
// the copy lost: wb_written back to 0 and the list told (bake_writeback_step's two places): taken again
|
|
tile->wb_written = 0, note(tile);
|
|
to_step(), tile->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{tile->id});
|
|
|
|
// not sampled for long, then sampled again: it stays listed meanwhile, and is taken when it is sampled
|
|
Texture* late = make();
|
|
written(late), late->last_frame = frame_no - 6; // (a render target not sampled yet)
|
|
for (int i = 0; i < 60; i++) {
|
|
frame();
|
|
if (frame_no % 4 == 0) CHECK(step().empty());
|
|
}
|
|
CHECK(late->wb_listed);
|
|
while (frame_no % 4) frame();
|
|
late->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{late->id});
|
|
|
|
// released while listed: not taken; made again and drawn (its contents begin anew): taken
|
|
Texture* rel = make();
|
|
written(rel);
|
|
rel->released = true;
|
|
to_step(), rel->last_frame = frame_no;
|
|
CHECK(step().empty());
|
|
written(rel);
|
|
CHECK(rel->first_written == rel->last_written);
|
|
to_step(), rel->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{rel->id});
|
|
|
|
// destroyed while listed: gone from the list, never looked at again
|
|
Texture* dead = make();
|
|
written(dead);
|
|
const uint32_t dead_id = dead->id;
|
|
CHECK(dead->wb_listed);
|
|
const size_t before = bake_list.size();
|
|
destroy(dead);
|
|
CHECK(bake_list.size() == before - 1);
|
|
to_step();
|
|
CHECK(!step().count(dead_id));
|
|
|
|
// the image's contents dropped (gpu_written false) while listed, then drawn again
|
|
Texture* drop = make();
|
|
written(drop);
|
|
drop->gpu_written = false;
|
|
to_step(), drop->last_frame = frame_no;
|
|
CHECK(step().empty() && !drop->wb_listed);
|
|
written(drop);
|
|
CHECK(drop->wb_listed);
|
|
to_step(), drop->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{drop->id});
|
|
|
|
// drawn in a second frame: no bake, off the list at the next step; too large: listed, never taken
|
|
Texture* twice = make();
|
|
written(twice), frame(), written(twice);
|
|
Texture* big = make(5ull << 20);
|
|
written(big);
|
|
to_step(), twice->last_frame = big->last_frame = frame_no;
|
|
CHECK(step().empty() && !twice->wb_listed && big->wb_listed);
|
|
|
|
// the caps: 60 tiles baked in one frame, all sampled: 24 a step, all of them in three steps, each once
|
|
std::vector<Texture*> tiles;
|
|
for (int i = 0; i < 60; i++) tiles.push_back(make()), written(tiles.back());
|
|
std::set<uint32_t> all;
|
|
size_t steps = 0, total = 0;
|
|
for (int i = 0; i < 40 && all.size() < 60; i++) {
|
|
frame();
|
|
if (frame_no % 4) continue;
|
|
for (Texture* t : tiles) t->last_frame = frame_no;
|
|
const std::set<uint32_t> s = step();
|
|
CHECK(s.size() <= 24);
|
|
steps += !s.empty(), total += s.size();
|
|
all.insert(s.begin(), s.end());
|
|
}
|
|
CHECK(all.size() == 60 && total == 60 && steps == 3);
|
|
// and by bytes: three of 3 MB, 8 MB a step
|
|
std::vector<Texture*> wide;
|
|
for (int i = 0; i < 3; i++) wide.push_back(make(3ull << 20)), written(wide.back());
|
|
to_step();
|
|
for (Texture* t : wide) t->last_frame = frame_no;
|
|
CHECK(step().size() == 2);
|
|
to_step();
|
|
for (Texture* t : wide) t->last_frame = frame_no;
|
|
CHECK(step().size() == 1);
|
|
|
|
// one no step can take (9 MB of storage over 3 MB of texels: ripe, armed, never written back) listed before a tile: the
|
|
// list passes over it and takes the tile; it stays listed. The walk of the whole cache (=all, =check, the knob) stops
|
|
// at it, as before: a tile after it in the cache waits
|
|
Texture* huge = make(3ull << 20);
|
|
huge->guest_size = 9ull << 20;
|
|
written(huge);
|
|
Texture* after = make();
|
|
written(after);
|
|
to_step(), huge->last_frame = after->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{after->id} && huge->wb_listed);
|
|
Texture* after2 = make();
|
|
written(after2);
|
|
to_step(), huge->last_frame = after2->last_frame = frame_no;
|
|
CHECK(step(true, true).empty() && after2->wb_listed);
|
|
to_step(), huge->last_frame = after2->last_frame = frame_no;
|
|
CHECK(step() == std::set<uint32_t>{after2->id} && huge->wb_listed);
|
|
destroy(huge);
|
|
|
|
// a script of random events over 40 textures, 4000 presents: the list's step and the whole walk's agree throughout
|
|
std::vector<Texture*> pool;
|
|
for (int i = 0; i < 40; i++) pool.push_back(make(rnd(8) ? 64 << 10 : 5ull << 20));
|
|
uint64_t taken_n = 0;
|
|
for (int f = 0; f < 4000; f++) {
|
|
for (int e = 0; e < 6; e++) {
|
|
Texture*& t = pool[rnd(pool.size())];
|
|
switch (rnd(12)) {
|
|
case 0: case 1: written(t); break;
|
|
case 2: case 3: case 4: case 5: t->last_frame = frame_no; break;
|
|
case 6: if (!rnd(3)) t->released = true; break;
|
|
case 7: if (!t->gpu_written || t->released) t->only_cleared = true; break; // (vk_xfer.cpp: a clear of a fresh one)
|
|
case 8: if (t->wb_written) t->wb_written = 0, note(t); break; // (the copy lost)
|
|
case 9: if (!rnd(4)) t->gpu_written = false; break;
|
|
case 10: if (!rnd(6)) destroy(t), t = make(); break;
|
|
default: if (!rnd(3)) written(t), present(), written(t); break; // (drawn in two frames)
|
|
}
|
|
}
|
|
for (Texture* t : targets) written(t);
|
|
present();
|
|
if (frame_no % 4 == 0) taken_n += step(rnd(5) != 0).size();
|
|
}
|
|
CHECK(taken_n > 100); // (the script does reach the step's copies)
|
|
CHECK(bake_list.size() <= textures.size());
|
|
const size_t listed_end = bake_list.size(), cache_end = textures.size();
|
|
|
|
// RX_VK_BAKE_WRITEBACK=0 or RX_VK_BAKE_SCAN=all: no list (nothing reads it). A hundred targets drawn every frame and a
|
|
// tile leave it empty, and the walk of the whole cache still takes the tile
|
|
while (!textures.empty()) destroy(textures.back().get());
|
|
CHECK(bake_list.size() == 0);
|
|
const uint64_t added = bake_list.added.load();
|
|
for (bool writeback : {false, true}) {
|
|
list_kept = kept(writeback, writeback ? kAll : kList);
|
|
targets.clear();
|
|
for (int i = 0; i < 100; i++) targets.push_back(make());
|
|
Texture* lone = make();
|
|
written(lone);
|
|
to_step(), lone->last_frame = frame_no;
|
|
CHECK(bake_list.size() == 0 && !lone->wb_listed && bake_list.added.load() == added);
|
|
if (writeback) CHECK(step(true, true) == std::set<uint32_t>{lone->id});
|
|
while (!textures.empty()) destroy(textures.back().get());
|
|
}
|
|
if (!fails) std::printf("ok: %llu taken in the script, %zu listed of %zu at its end\n", (unsigned long long)taken_n,
|
|
listed_end, cache_end);
|
|
return fails != 0;
|
|
}
|
|
"""
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--cxx", default="clang++")
|
|
args = parser.parse_args()
|
|
head = VK / "bakescan.h"
|
|
check(head.exists(), "gpu/vk/bakescan.h is missing: the bake write-back's scan has no list (it walks the whole texture cache)")
|
|
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
|
|
check('#include "bakescan.h"' in tex, "vk_texture.cpp does not use the rule")
|
|
|
|
# the list is told wherever a texture can become one the step takes, where the list is kept
|
|
written = block(tex, "void texture_written(Texture* tex, const char* why) {")
|
|
at = 0
|
|
first = written.find("tex->last_written = frame_no;")
|
|
for part in ("tex->last_written = frame_no;", "tex->gpu_written = true;", "tex->released = false;",
|
|
"bake_list_note(tex);"):
|
|
at = written.find(part, at)
|
|
check(at >= 0, "texture_written does not tell the bake list after its writes (missing, or out of order: " + part + ")")
|
|
at += len(part)
|
|
seg = re.sub(r"//[^\n]*", "", written[first:at])
|
|
check(not re.search(r"\b(?:return|goto|throw)\b", seg),
|
|
"texture_written can leave between the writes the bake list's rule reads and the list's note: a texture armed and "
|
|
"not listed")
|
|
check(flat(written[at:]) == "}", "texture_written: the bake list's note is not its last statement")
|
|
# (and without a branch of its own there: the gate is in bake_list_note, so that texture_written's control flow, and
|
|
# with it its entry in the renderer's play profile, stays v0.8.4's. An if at the call put it at plain -O2)
|
|
check(re.search(r"\n bake_note_written\(tex\);[^\n]*\n bake_list_note\(tex\);[^\n]*\n\}\n$", written)
|
|
and "bake_list_kept" not in written,
|
|
"texture_written: the bake list's note is not an unconditional bake_list_note(tex) after bake_note_written: a "
|
|
"branch there changes the function's control flow, and the play profile no longer matches it")
|
|
# (every place that tells the list goes through bake_list_note, which asks first whether it is kept: not with
|
|
# RX_VK_BAKE_WRITEBACK=0, where nothing walks it, nor with RX_VK_BAKE_SCAN=all, the walk of the whole cache as before)
|
|
notes = [m.start() for m in re.finditer(r"\bbake_list\.note\(", tex)]
|
|
helper = tex.find("void bake_list_note(Texture* tex) {")
|
|
check(helper >= 0, "vk_texture.cpp has no bake_list_note, where the bake list is told")
|
|
helper_line = tex[helper:tex.index("\n", helper)]
|
|
check(len(notes) == 1 and helper < notes[0] < helper + len(helper_line),
|
|
"the bake list is told somewhere else than in bake_list_note (%d places)" % len(notes))
|
|
check(flat(helper_line) == "void bake_list_note(Texture* tex) { if (bake_list_kept()) bake_list.note(tex); }",
|
|
"bake_list_note does not ask bake_list_kept() before it tells the bake list: fed where nothing reads it")
|
|
calls = re.findall(r"\bbake_list_note\((?!Texture\*)", tex)
|
|
check(len(calls) == 4, "the places that tell the bake list are not texture_written's, the two lost copies' and the "
|
|
"check's (%d)" % len(calls))
|
|
check(flat(block(tex, "bool bake_list_kept() {")) ==
|
|
"bool bake_list_kept() { static const bool kept = bakescan::kept(bake_writeback_on(), bake_scan_mode()); "
|
|
"return kept; }",
|
|
"bake_list_kept() is not bakescan::kept of RX_VK_BAKE_WRITEBACK and RX_VK_BAKE_SCAN, read once")
|
|
check(re.fullmatch(r'bakescan::Mode bake_scan_mode\(\) \{ static const bakescan::Mode scan = \[\] \{ '
|
|
r'const bakescan::Mode m = bakescan::mode_of\(getenv\("RX_VK_BAKE_SCAN"\)\); '
|
|
r'if \(m != bakescan::kList && bake_writeback_on\(\)\) logf\([^;]*\); return m; \}\(\); return scan; \}',
|
|
flat(block(tex, "bakescan::Mode bake_scan_mode() {"))),
|
|
"bake_scan_mode() does not read RX_VK_BAKE_SCAN once and give that")
|
|
check(tex.find("bool bake_list_kept();") < tex.find("void bake_list_note(Texture* tex) {") <
|
|
tex.find("void texture_written(Texture* tex, const char* why) {"),
|
|
"bake_list_kept() and bake_list_note are not declared before texture_written")
|
|
forget = block(tex, "void forget_gpu(Texture* tex) {")
|
|
check("bake_forget(tex);" in forget, "forget_gpu does not take a destroyed texture off the bake list: a pointer to a freed one")
|
|
check(len(re.findall(r"\bforget_gpu\(", tex)) == 2 and tex.count("textures.erase(") == 1,
|
|
"a texture leaves the cache somewhere else than texture_sweep's forget_gpu: the bake list must be told there too")
|
|
# (the fields the rule's armed() reads: written in texture_written alone, but gpu_written = false (not armed any more:
|
|
# the step drops it); wb_written set to last_written or, back to 0, with the list told in the next statement. The
|
|
# headers too)
|
|
n_wb0 = 0
|
|
for path in sorted(VK.glob("*.cpp")) + sorted(VK.glob("*.h")):
|
|
src = path.read_text(encoding="utf-8", errors="replace").replace("\r\n", "\n")
|
|
inside = written if path.name == "vk_texture.cpp" else ""
|
|
for m in re.finditer(r"(?:->|\.)(last_written|first_written|wb_written|gpu_written)\s*=(?!=)\s*([^,;]+)", src):
|
|
field, value = m.group(1), m.group(2).strip()
|
|
line = src[src.rfind("\n", 0, m.start()) + 1:src.find("\n", m.end())]
|
|
where = "%s: %s" % (path.name, line.strip())
|
|
lhs = src[max(0, m.start() - 16):m.end(1)]
|
|
if field == "gpu_written":
|
|
if value == "false":
|
|
continue # (not armed any more: the step drops it)
|
|
if path.name == "vk_memory.cpp" and re.search(r"(?<![\w.>])(?:p->|p\.|o->second\.)gpu_written$", lhs):
|
|
continue # (a pool's field of that name)
|
|
if path.name == "vk_texture.cpp" and re.search(r"(?<![\w.>])s\.gpu_written$", lhs) and value == "t.gpu_written":
|
|
continue # (a snapshot of another type)
|
|
if field == "wb_written":
|
|
if value == "0":
|
|
n_wb0 += 1
|
|
check(re.match(r"[^;]*;\s*bake_list_note\(t\);", src[m.end():]),
|
|
"a copy lost puts wb_written back to 0 without telling the bake list next: " + where)
|
|
else:
|
|
check(value.endswith("last_written"), "wb_written is given something else than last_written: " + where)
|
|
else:
|
|
check(line in inside, "a field the bake list's rule reads is written outside texture_written: " + where)
|
|
check(n_wb0 == 2, "the places a copy is lost (wb_written = 0) are not the two of bake_writeback_step (%d)" % n_wb0)
|
|
|
|
# the step: the list unless the switch or the lab knob says the whole cache; check mode counts; the status line
|
|
step = block(tex, "void bake_writeback_step(uint64_t completed) {")
|
|
for part in ("const bakescan::Mode scan = bake_scan_mode();",
|
|
'const bool whole = scan != bakescan::kList || lab_ab::on("nobakescan");', "bake_list.settle(listed);",
|
|
"bakescan::armed(t->gpu_written, t->first_written, t->last_written, t->wb_written)",
|
|
"bakescan::ripe(t->released, t->only_cleared, t->last_written, t->last_frame, t->bytes, t->gpu, frame_no)",
|
|
"st_scan_missed"):
|
|
check(part in step, "bake_writeback_step: missing " + part)
|
|
check(re.search(r"if \(whole\) \{\s*st_scan_all\.fetch_add\(1[^;]*;\s*for \(auto& kv : textures\)\s*"
|
|
r"if \(!take\(kv\.second\.get\(\)\)\) break;\s*\} else \{", step),
|
|
"bake_writeback_step: RX_VK_BAKE_SCAN=all / check and the knob nobakescan do not walk the whole cache as before")
|
|
check(re.search(r"for \(Texture\* t : listed\) \{\s*looked\+\+;\s*if \(!take\(t\)\) break;\s*\}\s*"
|
|
r"st_scan_looked\.fetch_add\(looked", step),
|
|
"bake_writeback_step: the list's walk, or its count of what it looked at, is not the one checked here")
|
|
# (in the list, one no step can take is passed over before the caps' stop; the walk of the whole cache stops at it)
|
|
skip = "if (!whole && !bakescan::room(0, 0, t->guest_size)) return true;"
|
|
cap = "if (!bakescan::room(nb.items.size(), bytes, t->guest_size)) return false;"
|
|
check(skip in step and cap in step and step.index(skip) < step.index(cap),
|
|
"bake_writeback_step: a texture no step can take stops every newer one in the list (or the walk of the whole "
|
|
"cache passes over it, unlike before)")
|
|
# (check mode lists what it counted: each miss counted and logged once)
|
|
chk = step[step.index("if (scan == bakescan::kCheck)"):step.index("BakeBatch nb;")]
|
|
check(chk.index("st_scan_missed.fetch_add(") < chk.index("bake_list_note(t);"),
|
|
"RX_VK_BAKE_SCAN=check counts the same missed texture again at every step")
|
|
# (the scan's line on its own, every 600 presents, after the step's settle: not only when something was written back)
|
|
at = step.index('"vk: the bake write-back\'s scan: ')
|
|
check(step[:at].rstrip().endswith("if (frame_no % 600 == 0)\n logf(") and at > step.index("bake_list.settle(listed);"),
|
|
"bake_writeback_step: the scan's line is not printed on its own every 600 presents after the settle")
|
|
# (its listed count is the settle's copy: the list's size after the step's copies still held the textures it had just
|
|
# taken, which are armed no more and which the next settle drops)
|
|
scan_line = step[at:step.index(");\n", at)]
|
|
check("%zu listed at this step" in scan_line and "listed.size()" in scan_line and "bake_list.size()" not in scan_line,
|
|
"bake_writeback_step: the scan's line counts the list after the step's copies (those it just took included), "
|
|
"not what the step's settle kept")
|
|
check("bool wb_listed = false;" in (VK / "vk_texture.h").read_text(encoding="utf-8"), "vk_texture.h: Texture has no wb_listed")
|
|
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
|
|
check("`RX_VK_BAKE_SCAN=all`" in dev and "nobakescan" in dev, "docs/development.md: RX_VK_BAKE_SCAN is not in the switches table")
|
|
row = next((l for l in dev.splitlines() if l.startswith("| `RX_VK_BAKE_SCAN=")), "")
|
|
for part in ("`RX_VK_BAKE_SCAN=all` (or `0`)", "`all` or `0`: the walk of the whole cache, and no list, as before",
|
|
"`RX_VK_BAKE_WRITEBACK=0` there is no list", "counted and logged, each once", "not released nor only cleared",
|
|
"one no step can take", "\"the bake write-back's scan: by the list at N steps",
|
|
"so the same textures are taken, but for those the walk does not reach behind such a one"):
|
|
check(part in row, "docs/development.md: the RX_VK_BAKE_SCAN row does not say " + part)
|
|
check("The same textures are taken;" not in row, "docs/development.md: the RX_VK_BAKE_SCAN row says the same textures "
|
|
"are taken, where the list also takes those the walk does not reach behind one no step can take")
|
|
|
|
with tempfile.TemporaryDirectory(prefix="bakescan-") as tmp:
|
|
c, exe = Path(tmp) / "scan.cpp", Path(tmp) / "scan.exe"
|
|
c.write_text(FIXTURE, encoding="utf-8")
|
|
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Wno-unused-function", "-Wno-unused-variable", "-Werror",
|
|
"-I" + str(VK), str(c), "-o", str(exe)], check=True)
|
|
r = subprocess.run([str(exe)], capture_output=True, text=True)
|
|
check(r.returncode == 0, "the list against the walk of the whole cache:\n" + r.stdout)
|
|
print("PASS: the bake write-back's scan walks its list, and takes what the walk of the whole cache took")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|