upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 11:40:37 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 11:40:37 -0500
commitfc2ee62c982b165816fe6fe377e736c2870549da (patch)
tree698c599fe05ed329357e9fbc0a66c46ea783578f /runtime/src
parent82cfa38023ec9ae7377cac8ce96fcd569e952a53 (diff)
parent9cc95bff0d706331efdefe727a4f6c8782672eca (diff)
Merge pull request #1683 from requiemsnkrp
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/cached_pso.h110
1 files changed, 110 insertions, 0 deletions
diff --git a/runtime/src/cached_pso.h b/runtime/src/cached_pso.h
new file mode 100644
index 000000000..32710244e
--- /dev/null
+++ b/runtime/src/cached_pso.h
@@ -0,0 +1,110 @@
1// The blob ID3D12PipelineState::GetCachedBlob hands out and Create*PipelineState checks in CachedPSO.
2//
3// The Vulkan layer keeps no driver-compiled pipeline in it (D3D-34 seeds a VkPipelineCache from the build instead);
4// the blob only records what the PC runtime checks before accepting a cached PSO, measured with the host runtime on
5// an RTX 3060 (driver 32.0.16.1714) and WARP:
6// - the same description: accepted;
7// - another shader, or a root signature with other content: E_INVALIDARG (an equal root signature object is fine;
8// D3D12_PIPELINE_STATE_DESC::Flags is not part of the match);
9// - any byte changed, a truncated blob, or a pointer with CachedBlobSizeInBytes 0: E_INVALIDARG;
10// - a null pointer with a size: ignored, the PSO is created;
11// - a blob from another adapter: D3D12_ERROR_ADAPTER_NOT_FOUND.
12// A blob from the same adapter and another driver gives D3D12_ERROR_DRIVER_VERSION_MISMATCH (DirectX docs; not
13// measured, it needs a driver change).
14#pragma once
15
16#include <cstddef>
17#include <cstdint>
18#include <cstring>
19#include <span>
20#include <string>
21#include <vector>
22
23namespace leonida::cached_pso {
24
25// The physical device the blob was made on: VkPhysicalDeviceProperties' IDs, driverVersion and pipelineCacheUUID.
26struct Identity {
27 std::uint32_t vendor = 0;
28 std::uint32_t device = 0;
29 std::uint32_t driver = 0;
30 std::uint8_t cache_uuid[16] = {};
31};
32
33// What a blob is checked against: the SHA-256 (hex) of the parts of the description the PC runtime matches.
34using Key = std::string;
35
36enum class Check { accepted, invalid, other_adapter, other_driver, other_description };
37
38inline constexpr std::uint32_t magic = 0x4f53504c; // "LPSO"
39inline constexpr std::uint32_t version = 1;
40inline constexpr std::size_t key_bytes = 64;
41inline constexpr std::size_t blob_bytes = 4 + 4 + 12 + 16 + key_bytes + 4;
42
43inline std::uint32_t fnv1a(const std::uint8_t* data, std::size_t size) {
44 std::uint32_t h = 0x811c9dc5u;
45 for (std::size_t i = 0; i < size; ++i) h = (h ^ data[i]) * 0x01000193u;
46 return h;
47}
48
49inline void put32(std::vector<std::uint8_t>& out, std::uint32_t v) {
50 for (int i = 0; i < 4; ++i) out.push_back(std::uint8_t(v >> (8 * i)));
51}
52
53inline std::uint32_t get32(const std::uint8_t* p) {
54 return std::uint32_t(p[0]) | (std::uint32_t(p[1]) << 8) | (std::uint32_t(p[2]) << 16) | (std::uint32_t(p[3]) << 24);
55}
56
57// magic, version, vendor, device, driver, pipelineCacheUUID, key, then FNV-1a of everything before it: a changed
58// byte anywhere makes the blob invalid, as with the PC runtime.
59inline std::vector<std::uint8_t> encode(const Identity& identity, const Key& key) {
60 std::vector<std::uint8_t> out;
61 out.reserve(blob_bytes);
62 put32(out, magic);
63 put32(out, version);
64 put32(out, identity.vendor);
65 put32(out, identity.device);
66 put32(out, identity.driver);
67 out.insert(out.end(), std::begin(identity.cache_uuid), std::end(identity.cache_uuid));
68 std::string padded = key;
69 padded.resize(key_bytes, '\0');
70 out.insert(out.end(), padded.begin(), padded.end());
71 put32(out, fnv1a(out.data(), out.size()));
72 return out;
73}
74
75inline Check check(const Identity& identity, const Key& key, std::span<const std::uint8_t> blob) {
76 if (blob.size() != blob_bytes) return Check::invalid;
77 const std::uint8_t* p = blob.data();
78 if (get32(p + blob_bytes - 4) != fnv1a(p, blob_bytes - 4)) return Check::invalid;
79 if (get32(p) != magic || get32(p + 4) != version) return Check::invalid;
80 if (get32(p + 8) != identity.vendor || get32(p + 12) != identity.device) return Check::other_adapter;
81 if (get32(p + 16) != identity.driver || std::memcmp(p + 20, identity.cache_uuid, 16) != 0)
82 return Check::other_driver;
83 std::string padded = key;
84 padded.resize(key_bytes, '\0');
85 if (std::memcmp(p + 36, padded.data(), key_bytes) != 0) return Check::other_description;
86 return Check::accepted;
87}
88
89// Builds the bytes a key is hashed from: every field the PC runtime matches, in a fixed order with lengths, so two
90// descriptions give the same bytes only when those fields are equal.
91class KeyBuilder {
92public:
93 KeyBuilder& tag(const char* name) { return bytes(name, std::strlen(name)); }
94 KeyBuilder& u32(std::uint32_t v) {
95 put32(data_, v);
96 return *this;
97 }
98 KeyBuilder& bytes(const void* p, std::size_t n) {
99 put32(data_, std::uint32_t(n));
100 if (n) data_.insert(data_.end(), static_cast<const std::uint8_t*>(p), static_cast<const std::uint8_t*>(p) + n);
101 return *this;
102 }
103 KeyBuilder& text(const char* s) { return s ? bytes(s, std::strlen(s)) : u32(0xffffffffu); }
104 const std::vector<std::uint8_t>& data() const { return data_; }
105
106private:
107 std::vector<std::uint8_t> data_;
108};
109
110} // namespace leonida::cached_pso