upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/root_signature.cpp
diff options
context:
space:
mode:
authorSuarezPM <SuarezPM@users.noreply.github.com>2026-11-21 17:04:28 -0300
committerSuarezPM <SuarezPM@users.noreply.github.com>2026-11-21 17:04:28 -0300
commit612f1edebdcf5add10d90c259a1a457c86313a93 (patch)
tree91fa65e6fb55dfc131d6ea88ef4a94ba92c032be /runtime/src/root_signature.cpp
parent4c75ce537add406b426d0a1dc84d9a01fb4c86c9 (diff)
Root signature packer + reconciler + hard-fail (fix the least verifiable part of the layer)
AI-assisted (Muse Spark + explorer fleet). Implements docs/d3d12_vulkan.md section 3.5 as code: a portable D3D12 root-signature model (1.0/1.1, no d3d12.h) plus a packer that lays root parameters into the 32 user-SGPR dwords in root-parameter order (constants take N, views/tables take 2, static samplers take none). Anything that does not fit - more than 32 user SGPRs (spill layout unknown) or more than the 64-dword D3D12 cost limit - is refused loudly instead of truncated. tools/root_sig_contract.py reconciles a candidate signature against every shader's user_sgpr_reads from spirv/index.json (const32 vs constant dwords, 64-bit VAs vs view/table pairs, unpaired-hi detection) and exits nonzero on the first contradiction; --cluster shows which read-set shapes the game actually needs. The RTS0 chunk reader is marked hypothesis until the new Windows oracle test (synthetic descs serialized by the real d3d12.dll) confirms the layout; Tier A (model/packer/canonical codec/RTS0 self-consistency/reconciler) runs everywhere with no game data and no GPU. Tested: python -m pytest tests/test_root_sig_contract.py (12 passed); full suite 651 passed, 20 skipped, 1 xfailed; compileall + make_game --help; portable C++ test 17/17 PASS under g++ -Wall -Wextra -Wpedantic -Werror with ASan+UBSan; turing atlas on RTX 2060 SUPER (subgroup 32, mesh/bary/m8/heap true): compute32/ballot64/f16_input/maintenance8 valid, broken_negative correctly invalid. Windows-only Tier B (oracle + MSVC build) runs in CI.
Diffstat (limited to 'runtime/src/root_signature.cpp')
-rw-r--r--runtime/src/root_signature.cpp317
1 files changed, 317 insertions, 0 deletions
diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp
new file mode 100644
index 000000000..eda380198
--- /dev/null
+++ b/runtime/src/root_signature.cpp
@@ -0,0 +1,317 @@
1// Portable root-signature packer, see root_signature.h.
2#include "root_signature.h"
3
4namespace rt::rootsig {
5
6PackResult pack(const RootSignature& sig) {
7 PackResult out;
8 if (sig.params.size() > 64) {
9 out.error = "root signature has more than 64 root parameters";
10 return out;
11 }
12 uint32_t cost = 0;
13 for (const RootParameter& p : sig.params) {
14 if (p.type == ParamType::Constants && p.num_values == 0) {
15 out.error = "root constant parameter needs at least 1 dword";
16 return out;
17 }
18 const uint32_t dwords = p.type == ParamType::Table ? 2
19 : p.type == ParamType::Descriptor ? 2
20 : p.num_values;
21 cost += param_cost(p.type, p.num_values);
22 if (cost > kMaxD3d12Cost) {
23 out.error = "root signature exceeds the 64-dword D3D12 cost limit";
24 return out;
25 }
26 if (out.dword_count + dwords > kMaxUserSgprs) {
27 // Spill layout is unknown (see __XB_GetRootSignatureSpillPointer):
28 // refuse loudly instead of silently truncating every draw.
29 out.error = "root signature needs more than 32 user SGPRs (spill unsupported)";
30 return out;
31 }
32 const uint32_t param_index =
33 static_cast<uint32_t>(&p - sig.params.data());
34 for (uint32_t i = 0; i < dwords; ++i) {
35 out.dwords[out.dword_count] = 0; // filled at bind time
36 out.provenance.push_back({param_index, i});
37 ++out.dword_count;
38 }
39 }
40 out.d3d12_cost = cost;
41 out.ok = true;
42 return out;
43}
44
45namespace {
46
47void put_u32(std::vector<uint8_t>& out, uint32_t v) {
48 out.push_back(static_cast<uint8_t>(v));
49 out.push_back(static_cast<uint8_t>(v >> 8));
50 out.push_back(static_cast<uint8_t>(v >> 16));
51 out.push_back(static_cast<uint8_t>(v >> 24));
52}
53
54void put_f32(std::vector<uint8_t>& out, float v) {
55 uint32_t bits = 0;
56 static_assert(sizeof(bits) == sizeof(v));
57 __builtin_memcpy(&bits, &v, sizeof(bits));
58 put_u32(out, bits);
59}
60
61class Reader {
62public:
63 Reader(const uint8_t* data, size_t size) : data_(data), size_(size) {}
64
65 [[nodiscard]] bool u32(uint32_t& v) {
66 if (pos_ + 4 > size_) return false;
67 v = static_cast<uint32_t>(data_[pos_]) |
68 (static_cast<uint32_t>(data_[pos_ + 1]) << 8) |
69 (static_cast<uint32_t>(data_[pos_ + 2]) << 16) |
70 (static_cast<uint32_t>(data_[pos_ + 3]) << 24);
71 pos_ += 4;
72 return true;
73 }
74
75 [[nodiscard]] bool f32(float& v) {
76 uint32_t bits = 0;
77 if (!u32(bits)) return false;
78 __builtin_memcpy(&v, &bits, sizeof(v));
79 return true;
80 }
81
82 [[nodiscard]] bool at_end() const { return pos_ == size_; }
83
84private:
85 const uint8_t* data_ = nullptr;
86 size_t size_ = 0;
87 size_t pos_ = 0;
88};
89
90constexpr uint32_t kCanonicalMagic = 0x3153524Cu; // "LRS1" little-endian
91constexpr uint32_t kCanonicalVersion = 1;
92
93} // namespace
94
95std::vector<uint8_t> encode_canonical(const RootSignature& sig) {
96 std::vector<uint8_t> out;
97 put_u32(out, kCanonicalMagic);
98 put_u32(out, kCanonicalVersion);
99 put_u32(out, static_cast<uint32_t>(sig.version));
100 put_u32(out, static_cast<uint32_t>(sig.params.size()));
101 put_u32(out, static_cast<uint32_t>(sig.static_samplers.size()));
102 put_u32(out, sig.flags);
103 for (const RootParameter& p : sig.params) {
104 put_u32(out, static_cast<uint32_t>(p.type));
105 put_u32(out, static_cast<uint32_t>(p.visibility));
106 put_u32(out, p.num_values);
107 put_u32(out, p.shader_register);
108 put_u32(out, p.register_space);
109 put_u32(out, p.descriptor_register);
110 put_u32(out, p.descriptor_space);
111 put_u32(out, static_cast<uint32_t>(p.ranges.size()));
112 for (const DescriptorRange& r : p.ranges) {
113 put_u32(out, r.range_type);
114 put_u32(out, r.num_descriptors);
115 }
116 }
117 for (const StaticSampler& s : sig.static_samplers) {
118 put_u32(out, s.filter);
119 put_u32(out, s.address_u);
120 put_u32(out, s.address_v);
121 put_u32(out, s.address_w);
122 put_f32(out, s.mip_lod_bias);
123 put_u32(out, s.max_anisotropy);
124 put_u32(out, s.comparison_func);
125 put_u32(out, s.border_color);
126 put_f32(out, s.min_lod);
127 put_f32(out, s.max_lod);
128 put_u32(out, s.shader_register);
129 put_u32(out, s.register_space);
130 put_u32(out, static_cast<uint32_t>(s.visibility));
131 }
132 return out;
133}
134
135DecodeResult decode_canonical(const uint8_t* data, size_t size) {
136 DecodeResult out;
137 if (data == nullptr && size != 0) {
138 out.error = "null data with nonzero size";
139 return out;
140 }
141 Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size);
142 uint32_t magic = 0, version = 0, nparams = 0, nsamplers = 0;
143 if (!r.u32(magic) || magic != kCanonicalMagic) {
144 out.error = "bad canonical magic";
145 return out;
146 }
147 if (!r.u32(version) || version != kCanonicalVersion) {
148 out.error = "unsupported canonical version";
149 return out;
150 }
151 uint32_t ver = 0;
152 if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(out.sig.flags)) {
153 out.error = "truncated canonical header";
154 return out;
155 }
156 if (ver != 1 && ver != 2) {
157 out.error = "unknown root-signature version";
158 return out;
159 }
160 if (nparams > 64) {
161 out.error = "more than 64 root parameters";
162 return out;
163 }
164 out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1;
165 out.sig.params.reserve(nparams);
166 for (uint32_t i = 0; i < nparams; ++i) {
167 RootParameter p;
168 uint32_t type = 0, vis = 0, nranges = 0;
169 if (!r.u32(type) || !r.u32(vis) || !r.u32(p.num_values) || !r.u32(p.shader_register) ||
170 !r.u32(p.register_space) || !r.u32(p.descriptor_register) ||
171 !r.u32(p.descriptor_space) || !r.u32(nranges)) {
172 out.error = "truncated canonical parameter";
173 return out;
174 }
175 if (type > 2) {
176 out.error = "unknown parameter type";
177 return out;
178 }
179 p.type = static_cast<ParamType>(type);
180 p.visibility = static_cast<Visibility>(vis);
181 if (nranges > 1024) {
182 out.error = "absurd range count";
183 return out;
184 }
185 p.ranges.reserve(nranges);
186 for (uint32_t j = 0; j < nranges; ++j) {
187 DescriptorRange range;
188 if (!r.u32(range.range_type) || !r.u32(range.num_descriptors)) {
189 out.error = "truncated canonical range";
190 return out;
191 }
192 p.ranges.push_back(range);
193 }
194 out.sig.params.push_back(std::move(p));
195 }
196 out.sig.static_samplers.reserve(nsamplers);
197 for (uint32_t i = 0; i < nsamplers; ++i) {
198 StaticSampler s;
199 uint32_t vis = 0;
200 if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) ||
201 !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) ||
202 !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) ||
203 !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) {
204 out.error = "truncated canonical sampler";
205 return out;
206 }
207 s.visibility = static_cast<Visibility>(vis);
208 out.sig.static_samplers.push_back(s);
209 }
210 if (!r.at_end()) {
211 out.error = "trailing bytes after canonical root signature";
212 return out;
213 }
214 out.ok = true;
215 return out;
216}
217
218// RTS0 payload hypothesis: u32 version (1 = 1.0, 2 = 1.1), u32 num_params,
219// u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h
220// order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC.
221// Anything else (v1.2 blobs, extra dwords, truncation) is a hard error.
222DecodeResult parse_rts0(const uint8_t* data, size_t size) {
223 DecodeResult out;
224 Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size);
225 uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0;
226 if (data == nullptr && size != 0) {
227 out.error = "null data with nonzero size";
228 return out;
229 }
230 if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(flags)) {
231 out.error = "truncated RTS0 header";
232 return out;
233 }
234 if (ver != 1 && ver != 2) {
235 out.error = "unknown RTS0 version (not 1.0/1.1)";
236 return out;
237 }
238 if (nparams > 64) {
239 out.error = "RTS0 with more than 64 root parameters";
240 return out;
241 }
242 out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1;
243 out.sig.flags = flags;
244 out.sig.params.reserve(nparams);
245 for (uint32_t i = 0; i < nparams; ++i) {
246 RootParameter p;
247 uint32_t type = 0, vis = 0;
248 if (!r.u32(type) || !r.u32(vis)) {
249 out.error = "truncated RTS0 parameter";
250 return out;
251 }
252 if (type > 2) {
253 out.error = "unknown RTS0 parameter type";
254 return out;
255 }
256 p.type = static_cast<ParamType>(type);
257 p.visibility = static_cast<Visibility>(vis);
258 if (p.type == ParamType::Constants) {
259 if (!r.u32(p.shader_register) || !r.u32(p.register_space) || !r.u32(p.num_values)) {
260 out.error = "truncated RTS0 constants";
261 return out;
262 }
263 } else if (p.type == ParamType::Descriptor) {
264 if (!r.u32(p.descriptor_register) || !r.u32(p.descriptor_space)) {
265 out.error = "truncated RTS0 descriptor";
266 return out;
267 }
268 } else {
269 uint32_t nranges = 0;
270 if (!r.u32(nranges)) {
271 out.error = "truncated RTS0 table";
272 return out;
273 }
274 if (nranges > 1024) {
275 out.error = "absurd RTS0 range count";
276 return out;
277 }
278 p.ranges.reserve(nranges);
279 for (uint32_t j = 0; j < nranges; ++j) {
280 DescriptorRange range;
281 uint32_t base_reg = 0, space = 0, offset = 0;
282 if (!r.u32(range.range_type) || !r.u32(range.num_descriptors) || !r.u32(base_reg) ||
283 !r.u32(space) || !r.u32(offset)) {
284 out.error = "truncated RTS0 range";
285 return out;
286 }
287 (void)base_reg;
288 (void)space;
289 (void)offset;
290 p.ranges.push_back(range);
291 }
292 }
293 out.sig.params.push_back(std::move(p));
294 }
295 out.sig.static_samplers.reserve(nsamplers);
296 for (uint32_t i = 0; i < nsamplers; ++i) {
297 StaticSampler s;
298 uint32_t vis = 0;
299 if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) ||
300 !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) ||
301 !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) ||
302 !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) {
303 out.error = "truncated RTS0 sampler";
304 return out;
305 }
306 s.visibility = static_cast<Visibility>(vis);
307 out.sig.static_samplers.push_back(s);
308 }
309 if (!r.at_end()) {
310 out.error = "trailing bytes after RTS0 root signature";
311 return out;
312 }
313 out.ok = true;
314 return out;
315}
316
317} // namespace rt::rootsig