mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:08:37 +00:00
PSOs a session built were compiled again by the next one. The layer already read the `pipelines` stage's pipeline_cache.bin beside gta6rt.exe (#367), but never wrote anything back. Now the runtime keeps its own cache in pipeline_cache.session.bin beside it: vkGetPipelineCacheData on device teardown, then vkMergePipelineCaches into the cache at the next launch. Vulkan takes one blob per create, so the offline seed and the session file are two caches merged together rather than one file. They are deliberately separate files, so a run can never replace the multi-GB corpus cache with the handful of pipelines it compiled. The write is capped at 256 MiB (LEONIDA_PIPELINE_CACHE_BYTES=<bytes>, 0 turns it off): a cache seeded from the corpus comes back as the whole union, which is not worth rewriting at every exit; past the cap the previous session file is left alone. A foreign or truncated session file is ignored with one line, and so is a failed write (a read-only install directory). The header check moves to runtime/src/pipeline_cache_store.h, shared with the offline seed and tested without Vulkan (runtime/test/pipeline_cache_store_test.cpp: the limit parser and the 32-byte VkPipelineCacheHeaderVersionOne comparison). write_pipeline_cache_file moves from runtime/test to runtime/src so the runtime and vk_pipeline_check use the same staged write. Not run: this machine has neither MSVC nor the Windows SDK, so the shim itself is compiled by the Windows job. The portable test passes here (23/23 ctest on Linux). The end-to-end check needs a Vulkan GPU: run d3d12_vulkan_device_test twice and look for "wrote N bytes to pipeline_cache.session.bin", then "merged N bytes" on the second run.
94 lines
4.2 KiB
C++
94 lines
4.2 KiB
C++
// The two pure decisions behind D3D-704: how large a session cache may be (LEONIDA_PIPELINE_CACHE_BYTES) and
|
|
// whether a blob's VkPipelineCacheHeaderVersionOne names this device. No Vulkan, no GPU, no files.
|
|
#include "../src/pipeline_cache_store.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <string>
|
|
|
|
#define CHECK(condition) do { \
|
|
if (!(condition)) { \
|
|
std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
namespace store = leonida::pipeline_cache_store;
|
|
|
|
static void put32(std::uint8_t* p, std::uint32_t value) {
|
|
for (int i = 0; i < 4; ++i) p[i] = static_cast<std::uint8_t>(value >> (8 * i));
|
|
}
|
|
|
|
static int test_limit() {
|
|
using store::default_save_limit;
|
|
// Absent, empty and the default knob.
|
|
CHECK(store::parse_save_limit(nullptr).bytes == default_save_limit);
|
|
CHECK(store::parse_save_limit(nullptr).valid);
|
|
CHECK(store::parse_save_limit("").bytes == default_save_limit);
|
|
// 0 is the documented "off", not a typo, and leading zeros are still a number.
|
|
CHECK(store::parse_save_limit("0").bytes == 0);
|
|
CHECK(store::parse_save_limit("0").valid);
|
|
CHECK(store::parse_save_limit("007").bytes == 7);
|
|
CHECK(store::parse_save_limit("1048576").bytes == 1048576);
|
|
const std::string largest = std::to_string(std::numeric_limits<std::size_t>::max());
|
|
CHECK(store::parse_save_limit(largest.c_str()).bytes == std::numeric_limits<std::size_t>::max());
|
|
// Anything that is not a plain byte count falls back to the default and says the value was rejected.
|
|
for (const char* text : {"-1", "+1", " 1", "1 ", "12x", "0x10", "1.5", "99999999999999999999999999"}) {
|
|
const auto limit = store::parse_save_limit(text);
|
|
CHECK(!limit.valid);
|
|
CHECK(limit.bytes == default_save_limit);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int test_header() {
|
|
leonida::cached_pso::Identity identity;
|
|
identity.vendor = 0x1002;
|
|
identity.device = 0x73ff;
|
|
identity.driver = 0x02000d0b;
|
|
|
|
std::uint8_t header[store::header_bytes] = {};
|
|
put32(header, store::header_bytes);
|
|
put32(header + 4, store::header_version_one);
|
|
put32(header + 8, identity.vendor);
|
|
put32(header + 12, identity.device);
|
|
for (int i = 0; i < 16; ++i) header[16 + i] = identity.cache_uuid[i] = static_cast<std::uint8_t>(i + 1);
|
|
CHECK(store::header_matches(header, sizeof(header), identity));
|
|
// Extra payload past the header does not matter: the driver reads the header and then its own data.
|
|
std::uint8_t blob[store::header_bytes + 8] = {};
|
|
std::memcpy(blob, header, sizeof(header));
|
|
CHECK(store::header_matches(blob, sizeof(blob), identity));
|
|
|
|
// Too short and null are "no cache", never a read past the end.
|
|
CHECK(!store::header_matches(header, store::header_bytes - 1, identity));
|
|
CHECK(!store::header_matches(nullptr, store::header_bytes, identity));
|
|
|
|
// The four fields a driver checks before trusting a blob from another device, driver or header version.
|
|
auto differs = [&](int offset, std::uint32_t value) {
|
|
std::uint8_t changed[store::header_bytes];
|
|
std::memcpy(changed, header, sizeof(changed));
|
|
put32(changed + offset, value);
|
|
return !store::header_matches(changed, sizeof(changed), identity);
|
|
};
|
|
CHECK(differs(0, store::header_bytes + 4));
|
|
CHECK(differs(4, store::header_version_one + 1));
|
|
CHECK(differs(8, identity.vendor + 1));
|
|
CHECK(differs(12, identity.device + 1));
|
|
std::uint8_t uuid[store::header_bytes];
|
|
std::memcpy(uuid, header, sizeof(uuid));
|
|
uuid[16] ^= 0xff;
|
|
CHECK(!store::header_matches(uuid, sizeof(uuid), identity));
|
|
// vendorID/deviceID are u32s little-endian; the same bytes read the other way name another device.
|
|
leonida::cached_pso::Identity swapped = identity;
|
|
swapped.vendor = 0x02100000;
|
|
CHECK(!store::header_matches(header, sizeof(header), swapped));
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
if (const int failed = test_limit()) return failed;
|
|
return test_header();
|
|
}
|