"""The pipeline cache's file has a bound: one over it is not read, data over it is not written (gpu/vk/pcache_bound.h, vk_draw.cpp pcache_init and pipeline_cache_save; no game/GPU). The header is compiled and run as it is: the bound from RX_VK_PCACHE_MAX_MB or the PC's RAM, and the read of authored files in a temporary folder: under the bound byte for byte as written, at the bound still read, over it not a byte, a file no longer than a header no cache, a missing one a first start. Then the source: pcache_init sets the bound before it reads and reads through the header, and pipeline_cache_save asks the bound before it fetches the data (the save's own behaviour is run by tests/test_pipeline_cache_save.py, case over-bound). python -I -B tests/test_pcache_bound.py --cxx clang++ """ import argparse import subprocess import tempfile from pathlib import Path RUN_ALL_ARGS = ["--cxx", "{cxx}"] ROOT = Path(__file__).resolve().parents[1] FIXTURE = r""" #include #include #include #include "pcache_bound.h" #define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0) using namespace pcache_bound; static bool put(const std::string& path, size_t n) { FILE* f = fopen(path.c_str(), "wb"); if (!f) return false; bool ok = true; for (size_t i = 0; i < n && ok; i++) ok = fputc((int)(i * 7 + 3) & 0xFF, f) != EOF; return fclose(f) == 0 && ok; } static bool same(const std::vector& d, size_t n) { if (d.size() != n) return false; for (size_t i = 0; i < n; i++) if (d[i] != (uint8_t)(i * 7 + 3)) return false; return true; } int main(int, char** argv) { const uint64_t GB = 1ull << 30, MB = 1ull << 20; // (unset: an eighth of the RAM, 2048 MB at least, 8192 MB at most; RAM not known: the least) CHECK(bytes(nullptr, 0) == 2048 * MB && bytes(nullptr, 8 * GB) == 2048 * MB && bytes(nullptr, 16 * GB) == 2048 * MB); CHECK(bytes(nullptr, 32 * GB) == 4096 * MB && bytes(nullptr, 48 * GB) == 6144 * MB); CHECK(bytes(nullptr, 64 * GB) == 8192 * MB && bytes(nullptr, 256 * GB) == 8192 * MB); // (set: that many MB, below the least too; 0 lifts it; what is no number is as unset, never "no bound") CHECK(bytes("0", 32 * GB) == 0 && bytes("512", 32 * GB) == 512 * MB && bytes("20000", 8 * GB) == 20000 * MB); CHECK(bytes("", 32 * GB) == 4096 * MB && bytes("off", 32 * GB) == 4096 * MB && bytes("-1", 32 * GB) == 4096 * MB); CHECK(bytes("12x", 32 * GB) == 4096 * MB && bytes("99999999999999999999999", 32 * GB) == 0); CHECK(within(0, 0) && within(~0ull, 0) && within(80, 80) && !within(81, 80) && within(1, 80)); const std::string dir = argv[1]; std::vector d{1, 2, 3}; uint64_t len = 99; // (no file: a first start, and nothing left in the block) CHECK(read((dir + "/none.bin").c_str(), 32, 1000, d, &len) == Read::None && d.empty() && len == 0); // (a header and no more is no cache) CHECK(put(dir + "/short.bin", 32)); CHECK(read((dir + "/short.bin").c_str(), 32, 1000, d, &len) == Read::Short && d.empty() && len == 32); // (under the bound: whole, as written, however the read is cut into pieces) const size_t big = 300000; CHECK(put(dir + "/under.bin", big)); CHECK(read((dir + "/under.bin").c_str(), 32, big + 1, d, &len) == Read::Ok && same(d, big) && len == big); // (at the bound: read. No bound: read) CHECK(read((dir + "/under.bin").c_str(), 32, big, d, &len) == Read::Ok && same(d, big)); CHECK(read((dir + "/under.bin").c_str(), 32, 0, d, &len) == Read::Ok && same(d, big)); // (a byte over: nothing of it, and its length is still told for the log) CHECK(read((dir + "/under.bin").c_str(), 32, big - 1, d, &len) == Read::OverBound && d.empty() && len == big); // (the answers the log names have words; the three that need no line have none) CHECK(*why(Read::OverBound) && *why(Read::Cut) && *why(Read::NoMemory) && *why(Read::Unsized) && *why(Read::Short)); CHECK(!*why(Read::Ok) && !*why(Read::None)); return 0; } """ def between(text, start, end): a = text.index(start) return text[a:text.index(end, a)] def check_source(): draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8") init = between(draw, "void pcache_init() {", "\n}\n") setb = 'pcache_bound_bytes = pcache_bound::bytes(getenv("RX_VK_PCACHE_MAX_MB"), ram);' readb = "pcache_bound::read(pcache_path.c_str(), 32, pcache_bound_bytes, data, &len)" if setb not in init or readb not in init or init.index(setb) > init.index(readb): raise SystemExit("pcache_init does not read the file through the bound, or reads before the bound is set") if "fread(" in init[init.index(setb):]: raise SystemExit("pcache_init reads the cache's file past the bounded read") if "ci.initialDataSize = data.size();" not in init: raise SystemExit("the cache is not made from what the bounded read gave") # (what is for other shaders is still not read at all, whatever its size) if init.index("if (!pcache_same) {", init.index(setb)) > init.index(readb): raise SystemExit("a file for other shaders is read") save = between(draw, "void pipeline_cache_save(bool force) {", "\n}\n") ask = "if (!pcache_bound::within(n, pcache_bound_bytes)) {" if ask not in save or save.index(ask) > save.index("std::vector data(n);"): raise SystemExit("pipeline_cache_save fetches the cache's data before it asks the bound") if save.index(ask) < save.index("n != pcache_saved_size"): raise SystemExit("the bound is asked before the size is known") dev_md = (ROOT / "docs/development.md").read_text(encoding="utf-8") if "`RX_VK_PCACHE_MAX_MB=`" not in dev_md: raise SystemExit("RX_VK_PCACHE_MAX_MB is not in docs/development.md's switches") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cxx", default="g++") args = parser.parse_args() check_source() with tempfile.TemporaryDirectory(prefix="pcache-bound-") as tmp: tmp = Path(tmp) (tmp / "t.cpp").write_text(FIXTURE, encoding="utf-8") exe = tmp / "pcache-bound" subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(tmp / "t.cpp"), "-o", str(exe)], check=True) data = tmp / "data" data.mkdir() r = subprocess.run([str(exe), str(data)], capture_output=True, text=True) if r.returncode: raise SystemExit(f"pcache_bound.h: {r.stdout.strip()}") print("PASS: a pipeline cache file over the bound is not read, at or under it it is read whole, and data over it is not written") if __name__ == "__main__": main()