Files
Triforce-Source/tests/test_chunk_default.py
ruckus7382sfgsgdf 822ee913bc 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.
2026-11-30 20:55:14 -06:00

263 lines
20 KiB
Python

"""The size of a submission's parts: RX_VK_CHUNK, the lab's knob, the driver's default (gpu/vk/barrier_rules.h part_draws,
vk_replay.cpp part_draws and maybe_split; no game, no GPU).
(Triforce, 11-22, the performance audit's SYNC-66, after Source B's.) A long submission goes to the GPU in parts of 120 draws
and dispatches on every card, read once into a static of maybe_split, so nothing could compare sizes inside a run. The size is
now RX_VK_CHUNK when set, else the RX_VK_LAB_AB knob chunk:<n> in its phases, else the driver's default: 120 for every driver
until the lab picks one. (11-26) The lab picked NVIDIA's: 240 (measured in the benchmark's four scenes); every other driver
keeps 120. NVIDIA's driver is the device's driver id (VK_DRIVER_ID_NVIDIA_PROPRIETARY, one id on Windows and Linux), not the
card's vendor: NVK, Dozen and Venus on an NVIDIA card keep 120. (11-29) On NVIDIA's driver a part ends where the game ends its
render pass by default, Threaded rendering or not (barrier_rules.h part_pass_end_default, vk_replay.cpp pass_end_parts): the
counted 240 applies there with RX_VK_PART_PASS_END=0.
Compiles a driver against barrier_rules.h, lab_ab.h and the Vulkan headers and checks:
the default is 240 for NVIDIA's driver, and 120 for NVK, Dozen, Venus, RADV, AMD's, Intel's, any other driver id and none;
the pass-end parts are the default for NVIDIA's driver alone (by its id: not NVK, not any other, not none);
the rule's id for NVIDIA's driver is Vulkan's;
with no knob the size is what the old read gave, for 100,000 random texts of the variable (unset, 0, 120, negative, words,
any number) on random drivers, except unset on NVIDIA's driver (240): the default path splits where it split elsewhere;
RX_VK_CHUNK beats the knob and the default; 0 stays 0 (never split); the knob beats the default only in its own phases
(lab_ab.h's value_of with the phase's knob set, another knob's phase, and no phase), with NVIDIA's 240 and NVK's 120 as the
default beside the measured knob chunk:120, and the pass-end parts' test (value_of("chunk", -1) < 0) true only outside it.
And reads vk_replay.cpp: maybe_split has no static size and asks part_draws() at each call, still returns for a size of 0 or
less and still counts ++part_work against it after the tail's test, and takes Threaded rendering's pass-end parts only
outside the knob's phases; pass_end_parts turns them on by default with Threaded rendering or the rule (the device's driver
id), and its line says which; part_draws reads the variable once and the knob at each call, asks the rule with the device's
driver id (no platform test) and says when Threaded rendering's parts decide instead; barrier_rules.h's comment names NVK,
Threaded rendering's parts and both measurements; the switches table has the row, with NVK, the measured PC, where the size
applies under Threaded rendering and a lab example against 120, and no longer says the in-pass split is on with NVIDIA's
driver.
python -I -B tests/test_chunk_default.py --headers <Vulkan-Headers/include> [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
DRIVER = r'''
#include "barrier_rules.h"
#include "lab_ab.h"
#include <vulkan/vulkan_core.h>
#include <cstdio>
namespace br = mkwvk::barrier_rules;
namespace lab = mkwvk::lab_ab;
static int checks, fails;
#define CHECK(what, cond) do { checks++; if (!(cond)) fails++, printf("FAIL %s (line %d)\n", what, __LINE__); } while (0)
int main() {
// the device's driver id (dev.driver_id): NVIDIA's own driver has one on Windows and Linux; the drivers that run on NVIDIA's
// cards beside it (NVK, Dozen, Venus) report its vendor, 0x10de, and keep 120 until the lab has measured them
static const struct { const char* what; uint32_t driver; int64_t want; } kDrivers[] = {
{"NVIDIA's driver (one id on Windows and Linux): 240", VK_DRIVER_ID_NVIDIA_PROPRIETARY, 240},
{"NVK on an NVIDIA card: 120", VK_DRIVER_ID_MESA_NVK, 120},
{"Dozen on an NVIDIA card: 120", VK_DRIVER_ID_MESA_DOZEN, 120},
{"Venus on an NVIDIA card: 120", VK_DRIVER_ID_MESA_VENUS, 120},
{"RADV: 120", VK_DRIVER_ID_MESA_RADV, 120},
{"AMD's driver: 120", VK_DRIVER_ID_AMD_PROPRIETARY, 120},
{"AMDVLK: 120", VK_DRIVER_ID_AMD_OPEN_SOURCE, 120},
{"Intel's Windows driver: 120", VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS, 120},
{"Intel's Mesa driver: 120", VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA, 120},
{"llvmpipe: 120", VK_DRIVER_ID_MESA_LLVMPIPE, 120},
{"MoltenVK: 120", VK_DRIVER_ID_MOLTENVK, 120},
{"no driver id (0): 120", 0, 120},
{"an id from a later header: 120", 0x7fffffffu, 120},
};
constexpr size_t kN = sizeof kDrivers / sizeof *kDrivers;
for (const auto& d : kDrivers) CHECK(d.what, br::part_draws_default(d.driver) == d.want);
static_assert(br::kDriverNvidiaProprietary == VK_DRIVER_ID_NVIDIA_PROPRIETARY, "the rule's id for NVIDIA's driver is Vulkan's");
CHECK("the constants", br::kPartDraws == 120 && br::kPartDrawsNvidia == 240);
// ((Triforce, 11-29) the pass-end parts by default, Threaded rendering off: NVIDIA's driver alone, by its id (the drivers
// that run on NVIDIA's cards beside it keep the parts cut by count, as every other driver, until the lab has measured them)
for (const auto& d : kDrivers)
CHECK("the pass-end parts by default on NVIDIA's driver alone", br::part_pass_end_default(d.driver) == (d.driver == VK_DRIVER_ID_NVIDIA_PROPRIETARY));
CHECK("the pass-end parts by default: NVIDIA's driver", br::part_pass_end_default(VK_DRIVER_ID_NVIDIA_PROPRIETARY));
CHECK("the pass-end parts not by default: NVK", !br::part_pass_end_default(VK_DRIVER_ID_MESA_NVK));
CHECK("the pass-end parts not by default: no driver id", !br::part_pass_end_default(0));
// with no knob: the old read, whatever the variable says
uint64_t rng = 0xD1B54A32D192ED03ull;
const auto rnd = [&](uint64_t n) { rng ^= rng << 13, rng ^= rng >> 7, rng ^= rng << 17; return rng % n; };
static const char* const kTexts[] = {nullptr, "", "0", "120", "480", "4096", "-5", "junk", "12x", " 64", "1", "999999999999"};
for (int i = 0; i < 100000; i++) {
char num[32];
const char* env = kTexts[rnd(sizeof kTexts / sizeof *kTexts)];
if (rnd(3) == 0) snprintf(num, sizeof num, "%lld", (long long)rnd(5000) - 500), env = num;
const uint32_t driver = kDrivers[rnd(kN)].driver;
const int64_t now = br::part_draws(env != nullptr, env ? atoll(env) : 0, br::part_draws_default(driver));
const int64_t was = !env && driver == VK_DRIVER_ID_NVIDIA_PROPRIETARY ? 240 : br::part_draws_before(env);
CHECK("with no knob the size is the old read's (unset on NVIDIA's driver: 240)", now == was);
if (fails) break;
}
const int64_t nvidia = br::part_draws_default(VK_DRIVER_ID_NVIDIA_PROPRIETARY), nvk = br::part_draws_default(VK_DRIVER_ID_MESA_NVK);
CHECK("unset on NVIDIA's driver: 240", br::part_draws(false, 0, nvidia) == 240);
CHECK("unset on NVK: 120", br::part_draws(false, 0, nvk) == 120);
CHECK("unset on AMD's: 120", br::part_draws(false, 0, br::part_draws_default(VK_DRIVER_ID_AMD_PROPRIETARY)) == 120);
CHECK("unset on RADV: 120", br::part_draws(false, 0, br::part_draws_default(VK_DRIVER_ID_MESA_RADV)) == 120);
CHECK("unset on Intel's: 120", br::part_draws(false, 0, br::part_draws_default(VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS)) == 120);
CHECK("RX_VK_CHUNK=120 on NVIDIA's driver: 120", br::part_draws(true, 120, nvidia) == 120);
CHECK("RX_VK_CHUNK beats the knob", br::part_draws(true, 240, 480) == 240);
CHECK("RX_VK_CHUNK=0 stays 0 under a knob", br::part_draws(true, 0, 480) == 0);
CHECK("the knob beats the default", br::part_draws(false, 0, 480) == 480);
// the knob through lab_ab.h, as vk_replay.cpp's part_draws asks it (the driver's default beside it): only in its own
// phases. The measured A/B, chunk:120 against NVIDIA's 240; and maybe_split's pass-end parts step aside in its phases alone
CHECK("no RX_VK_LAB_AB: the default", (lab::n_knobs = 0, lab::value_of("chunk", nvidia)) == 240);
CHECK("no RX_VK_LAB_AB: the pass-end parts' test passes", lab::value_of("chunk", -1) < 0);
lab::n_knobs = 2;
snprintf(lab::knobs[0].name, sizeof lab::knobs[0].name, "chunk"), lab::knobs[0].value = 120;
snprintf(lab::knobs[1].name, sizeof lab::knobs[1].name, "rblate"), lab::knobs[1].value = 1;
lab::active.store(0);
CHECK("a phase with no knob on NVIDIA's driver: 240", br::part_draws(false, 0, lab::value_of("chunk", nvidia)) == 240);
CHECK("a phase with no knob on NVK: 120", br::part_draws(false, 0, lab::value_of("chunk", nvk)) == 120);
CHECK("a phase with no knob: the pass-end parts' test passes", lab::value_of("chunk", -1) < 0);
lab::active.store(1);
CHECK("the knob chunk:120's phase on NVIDIA's driver: 120", br::part_draws(false, 0, lab::value_of("chunk", nvidia)) == 120);
CHECK("the knob's phase on NVK: its number", br::part_draws(false, 0, lab::value_of("chunk", nvk)) == 120);
CHECK("the knob's phase, the variable set: the variable", br::part_draws(true, 480, lab::value_of("chunk", nvidia)) == 480);
CHECK("the knob's phase: the pass-end parts step aside", lab::value_of("chunk", -1) == 120);
lab::active.store(2);
CHECK("another knob's phase: the default", br::part_draws(false, 0, lab::value_of("chunk", nvidia)) == 240);
CHECK("another knob's phase: the pass-end parts' test passes", lab::value_of("chunk", -1) < 0);
if (fails) printf("chunk default: FAILED (%d of %d checks)\n", fails, checks);
else printf("chunk default: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check(ok, what):
if not ok:
raise SystemExit("FAIL: " + what)
def function(text, head):
at = text.index(head)
return text[at:text.index("\n}\n", at)]
def in_order(text, needles, where):
at = 0
for n in needles:
i = text.find(n, at)
check(i >= 0, "%s: %r missing or out of order" % (where, n))
at = i + len(n)
def check_source():
replay = (ROOT / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
split = function(replay, "void maybe_split(VkCommandBuffer& cmd) {")
check("static const int64_t every" not in replay, "maybe_split still reads the size once into a static")
check(split.startswith("void maybe_split(VkCommandBuffer& cmd) {\n const int64_t every = part_draws();\n"),
"maybe_split does not ask part_draws() at each call")
check('getenv("RX_VK_CHUNK")' not in split, "maybe_split reads RX_VK_CHUNK itself")
check('if (pe.on && !part_end_now && lab_ab::value_of("chunk", -1) < 0) {' in split,
"maybe_split's pass-end parts are no longer left in the knob's phases alone (the docs' Threaded rendering note)")
check("if (tail_on()) return;" in split and
"if (!rec_cur || every <= 0 || ++part_work < every || rec_parts + 1 >= kMaxParts || dump_on()) return;" in split and
split.index("if (tail_on()) return;") < split.index("++part_work < every"),
"maybe_split's test changed: a size of 0 or less must never split, and the count comes after the tail's test")
part = function(replay, "int64_t part_draws() {")
check('static const char* const env = getenv("RX_VK_CHUNK");' in part and "static const int64_t env_value = env ? atoll(env) : 0;" in part,
"part_draws does not read RX_VK_CHUNK once")
check("barrier_rules::part_draws_default((uint32_t)dev.driver_id);" in part and "#if" not in part and "vendorID" not in part,
"the default is not asked of the rule with the device's driver id alone (NVK reports NVIDIA's vendor)")
check("pass_end_parts_on()" in part and "not in force" in part,
"part_draws' line does not say when Threaded rendering's pass-end parts decide instead of the size")
# (with those parts on and RX_VK_CHUNK unset the printed default is never in force: the knob's number decides in its phases)
check("env ? \"; not in force outside the knob's phases: " in part and '"; not in force: a part ends where' in part,
"part_draws' line says the driver's default is in force in the knob's phases under Threaded rendering's parts")
check(part.rstrip().endswith('return barrier_rules::part_draws(env != nullptr, env_value, lab_ab::value_of("chunk", by_driver));'),
"part_draws does not ask the knob at each call, behind the variable")
# ((Triforce) and once by Threaded rendering's parts, only to step aside when it is set: pass_end_parts)
ends = function(replay, "PassEndParts& pass_end_parts() {")
check(replay.count('getenv("RX_VK_CHUNK")') == 2 and ends.count('getenv("RX_VK_CHUNK")') == 1, "RX_VK_CHUNK is read somewhere else")
check('v.on = e && *e ? atoi(e) != 0 : by_default && !getenv("RX_VK_CHUNK");' in ends,
"pass_end_parts' switch changed: the docs say any RX_VK_CHUNK turns Threaded rendering's parts off")
# ((Triforce, 11-29) and NVIDIA's driver has them by default, Threaded rendering or not: the rule asked with the driver id)
check("const bool by_default = threaded_on() || barrier_rules::part_pass_end_default((uint32_t)dev.driver_id);" in ends,
"pass_end_parts' default is not Threaded rendering or the rule's, by the device's driver id")
check('"NVIDIA\'s driver\'s default since 11-29"' in ends and '"Threaded rendering"' in ends,
"pass_end_parts' line does not say why the pass-end parts are on")
check("every RX_VK_CHUNK, as before" not in ends and "default 120;" not in replay,
"vk_replay.cpp still says the counted size is 120 as before")
rules = (ROOT / "gpu/vk/barrier_rules.h").read_text(encoding="utf-8")
at = rules.index("RX_VK_CHUNK: how many draws")
note = rules[at:rules.index("constexpr int64_t kPartDraws", at)]
for need in ("NVK", "Threaded rendering", "RX_VK_PART_PASS_END not 1", "RX_VK_PART_PASS_END=0", "v0.8.3", "v0.8.4"):
check(need in note, "barrier_rules.h's part-size comment does not name " + need)
check("a few more reports keep" not in note, "barrier_rules.h's part-size comment still gives the occlusion counts a direction")
dev = (ROOT / "docs/development.md").read_text(encoding="utf-8")
row = [line for line in dev.splitlines() if line.startswith("| `RX_VK_CHUNK=<n>` |")]
check(len(row) == 1 and "**240 on NVIDIA's driver**" in row[0] and "**120 on every other driver**" in row[0] and
"`chunk:<n>`" in row[0], "docs/development.md: no row for RX_VK_CHUNK with its defaults")
check("NVK" in row[0] and "measured only on" in row[0], "the RX_VK_CHUNK row does not say NVK keeps 120 or where 240 was measured")
check("Threaded rendering" in row[0] and "`RX_VK_PART_PASS_END=0`" in row[0] and "leave `RX_VK_CHUNK` unset" in row[0],
"the RX_VK_CHUNK row does not say its size and its switch back are for parts cut by count, not Threaded rendering's")
check("`RX_VK_LAB_AB=chunk:120,chunk:480`" in row[0] and "chunk:480,chunk:240" not in row[0],
"the RX_VK_CHUNK row's lab example does not compare against the old size")
there = row[0][row[0].index("leave `RX_VK_CHUNK` unset"):row[0].index("The size was read once")]
check("(the first part from 32)" in row[0] and "whole submissions" in there,
"the RX_VK_CHUNK row does not say Threaded rendering's first part is from 32 draws, or that RX_VK_CHUNK=0 there gives "
"whole submissions")
# ((Triforce, 11-29) the pass-end parts' own row: its default on NVIDIA's driver, the way back, and where it was measured)
pe = [line for line in dev.splitlines() if line.startswith("| `RX_VK_PART_PASS_END=0` or `=1`,")]
check(len(pe) == 1 and "**on by default on NVIDIA's driver since 11-29**" in pe[0] and "part_pass_end_default" in pe[0] and
"`=0`: counted parts" in pe[0] and "Not measured:" in pe[0] and "RTX 5090" in pe[0],
"docs/development.md: no row for RX_VK_PART_PASS_END with its default on NVIDIA's driver, =0 and where it was measured")
check("parts of 120 draws" not in dev, "docs/development.md still says a part is 120 draws on every card")
check("Unset: `pass` with NVIDIA's driver" not in dev, "docs/development.md still says the in-pass split is on with NVIDIA's driver")
# ((Triforce, 11-29) RX_VK_PART_IDLE / the knob partidle: a part after the first may end from the idle size once the GPU has
# done everything sent. Off unset; asked through part_min_now in place of pe.min, so maybe_split's control flow (and its
# renderer profile) is the same; the GPU looked at every 8th draw at most, below RX_VK_PART_MIN only)
check('v.idle = std::clamp<int64_t>(num("RX_VK_PART_IDLE", 0), 0, v.min);' in ends, "RX_VK_PART_IDLE is not off unset and at most PART_MIN")
split = function(replay, "void maybe_split(VkCommandBuffer& cmd) {")
check("const int64_t need = rec_parts == 0 && pe.first ? pe.first : part_min_now(pe);" in split,
"maybe_split does not ask part_min_now for a later part's size in place of pe.min")
pm = function(replay, "int64_t part_min_now(PassEndParts& pe) {")
# (and RX_VK_PART_RB / partrb: a part that writes back a page the CPU reads back, below PART_MIN only; the binding notes it
# through a call, so the binding's control flow (and its profile) is the same; cleared at each part's end)
# ((11-29, the owner's decision) 32 unset with RX_VK_RB_REC_END=3, the default with these parts; off with another mode set)
check('v.rb = std::clamp<int64_t>(num("RX_VK_PART_RB", range ? 32 : 0), 0, v.min);' in ends,
"RX_VK_PART_RB is not 32 unset with mode 3 (off with another) and at most PART_MIN")
mem = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
bind = function(mem, "BufferRef gpu_buffer_bind(uint64_t gpu, uint64_t size, bool written) {")
check("rb_write_note(p->lo + off, p->lo + wb_end);" in bind and "rb_watched(" not in bind and "part_rb_write" not in bind,
"gpu_buffer_bind does not note a read-back page's write-back through rb_write_note alone (a branch there loses its profile)")
check("part_rb_write.store(false, std::memory_order_relaxed);" in function(mem, "void memory_end_submission(VkCommandBuffer cmd) {"),
"a part's end does not clear the read-back write note")
in_order(pm, ['const int64_t rb = lab_ab::value_of("partrb", pe.rb);',
"if (rb > 0 && n >= rb && n < pe.min && memory_part_rb_write()) {", "return rb;",
'lab_ab::value_of("partidle", pe.idle);', "if (idle <= 0 || n < idle) {", "return pe.min;",
"if (part_idle_now) return idle;", "if (n >= pe.min || (n & 7) != 0 || !rec_timeline) return pe.min;",
"vkGetSemaphoreCounterValue(dev.device, rec_timeline, &done) == VK_SUCCESS && done >= rec_seq;"],
"part_min_now (off at 0, kept for the part once seen, the GPU looked at every 8th draw below PART_MIN)")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--headers", type=Path, required=True)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="chunk-default-") as tmp:
src = Path(tmp) / "driver.cpp"
src.write_text(DRIVER, encoding="utf-8")
exe = Path(tmp) / "driver.exe"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I", str(ROOT / "gpu/vk"), "-I", str(args.headers), str(src),
"-o", str(exe)],
check=True)
out = subprocess.run([str(exe)], capture_output=True, text=True)
print(out.stdout, end="")
check(out.returncode == 0, "the rule's checks failed")
print("PASS: a part's size: RX_VK_CHUNK, then the lab knob, then the driver's default, 240 on NVIDIA's and 120 for the rest (NVK too)")
if __name__ == "__main__":
main()