upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/console_heap_fix.comp
diff options
context:
space:
mode:
authorTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
committerTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
commitbeffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch)
tree32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/src/console_heap_fix.comp
parentf29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff)
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/src/console_heap_fix.comp')
-rw-r--r--runtime/src/console_heap_fix.comp292
1 files changed, 292 insertions, 0 deletions
diff --git a/runtime/src/console_heap_fix.comp b/runtime/src/console_heap_fix.comp
new file mode 100644
index 000000000..85b5d1787
--- /dev/null
+++ b/runtime/src/console_heap_fix.comp
@@ -0,0 +1,292 @@
1// Console descriptor heap translation on the GPU (LEONIDA_CONSOLE_HEAP_GPU=1, shims/d3d12_console_heap_gpu.h).
2// The title writes 32-byte RDNA2 records into its console descriptor heap; shaders read a copy in which every buffer
3// V# whose base is the title's own memory names the GPU copy of that memory instead (DeviceState::fix_vsharp). This
4// pass makes that copy: for each listed slot it reads the title's bytes (a host mirror of the heap's pages), rewrites
5// the V# halves, and writes the record where shaders read it. T#s and other words are copied as they are (T#s are
6// found by their exact words in the bindless table; the CPU registers them).
7// console_heap_fix_spv.h is this file compiled with `glslc --target-env=vulkan1.2 -O -mfmt=c` (see the header);
8// keep them together. runtime/test/vk_console_heap_gpu_test.cpp checks it against the CPU reference
9// (console_heap_gpu_reference in d3d12_console_heap_gpu.h).
10//
11// A V# half (dwords 0-3, and 4-7 when dword 3 says the slot is a buffer pair) is translated like fix_vsharp:
12// - skipped when its TYPE bits (dword 3, bits 30-31) are set or it has no records (dword 2 zero);
13// - base 0: written with no records;
14// - base inside a range of the range table (the alias table: slot-133 buffers, title windows, pool extents), sorted
15// by title VA: rebased to the range's shader VA when the range was checked backed on the CPU and holds the V#'s
16// whole span; written with no records when it does not hold it (vsharp_address answers 0 there too). An
17// unchecked range falls through;
18// - the base as a shader VA (sign-extended from bit 47 with FLAG_SIGNED) with its whole span inside a span of the
19// backed table (live allocations, geometry blocks): left as it is, as vsharp_address's backed() leaves it;
20// - otherwise its exact 16 bytes are looked up in the key table (an open-addressed table the CPU fills with what
21// vsharp_address answered for those words: a shader VA, or "no backing"); found: rebased, or written with no
22// records;
23// - not found: written with no records; when the base is in the ask table (the title's memory and GPU windows, with
24// FLAG_ASK), or there is no ask table, the half is recorded in the miss list, which the CPU reads once the
25// submission is done, resolves (making the copy if it can) and enters in the key table.
26// "No records" is what fix_vsharp writes: dwords 0, 1 and 2 zero, so loads return 0 and stores are dropped, and no
27// shader adds heap_base to a raw title address.
28//
29// MODE_UPDATE writes the key table entries the CPU changed (one thread each) before MODE_TRANSLATE runs, so the
30// table only ever changes in GPU order, between passes.
31//
32// Addresses are 64-bit buffer device addresses passed as uvec2 (GL_EXT_buffer_reference_uvec2): only the
33// bufferDeviceAddress feature is needed, no shaderInt64.
34#version 460
35#extension GL_EXT_buffer_reference : require
36#extension GL_EXT_buffer_reference_uvec2 : require
37
38layout(local_size_x = 64) in;
39
40layout(buffer_reference, std430, buffer_reference_align = 16) buffer Slots {
41 uvec4 v[];
42};
43layout(buffer_reference, std430, buffer_reference_align = 4) buffer Words {
44 uint w[];
45};
46
47const uint MODE_TRANSLATE = 0u;
48const uint MODE_UPDATE = 1u;
49
50const uint RANGE_WORDS = 8u; // title_va (2), end (2), shader_va (2), flags, pad
51const uint RANGE_CHECKED = 1u; // the CPU found the whole range backed
52const uint KEY_WORDS = 8u; // key (4), to.x, to.y | flags << 16, pad (2)
53const uint KEY_USED = 1u;
54const uint KEY_NONE = 2u; // vsharp_address answered 0: no records
55const uint KEY_PROBES = 16u;
56const uint UPDATE_WORDS = 12u; // index, pad (3), entry (8)
57const uint MISS_HEADER = 4u; // count, capacity, overflowed, pad
58const uint SPAN_WORDS = 4u; // start (2), end (2)
59const uint FLAG_SIGNED = 1u;
60const uint FLAG_ASK = 2u;
61const uint FLAG_RANGE = 4u; // MODE_TRANSLATE over slots pc.work.x + i, not a list: a rescan, V# pairs only
62const uint FLAG_FILTER = 8u; // with FLAG_RANGE: only slots whose metadata meets a change span (below)
63
64layout(push_constant) uniform Params {
65 uvec2 input_slots; // the title's bytes of the heap (its host mirror)
66 uvec2 copy_slots; // the heap where shaders read it
67 uvec2 work; // slot indices (MODE_TRANSLATE)
68 uvec2 ranges; // range table
69 uvec2 keys; // key table
70 uvec2 misses; // miss list
71 uvec2 backed; // backed spans
72 uvec2 ask; // ask spans
73 uvec2 meta; // per slot and half, device-local: what the pass last made of it (meta_of); 0: none
74 uvec2 cshader; // a filtered rescan's change spans in shader VAs (backings and alias copies that went)
75 uvec2 ctitle; // and in title VAs (alias ranges that went, words answered since)
76 uint count; // work items or updates
77 uint range_count;
78 uint key_mask; // key table entries - 1 (a power of two)
79 uint mode;
80 uint heap; // the heap's index (< 8), kept in each miss
81 uint backed_count;
82 uint ask_count;
83 uint flags;
84 uint cshader_count;
85 uint ctitle_count;
86} pc;
87// MODE_UPDATE reads its records at pc.work (the push constants hold 128 bytes).
88
89uvec2 add64(uvec2 a, uvec2 b) {
90 uint carry;
91 uint lo = uaddCarry(a.x, b.x, carry);
92 return uvec2(lo, a.y + b.y + carry);
93}
94uvec2 sub64(uvec2 a, uvec2 b) {
95 uint borrow;
96 uint lo = usubBorrow(a.x, b.x, borrow);
97 return uvec2(lo, a.y - b.y - borrow);
98}
99bool less64(uvec2 a, uvec2 b) { return a.y < b.y || (a.y == b.y && a.x < b.x); }
100uvec2 offset(uvec2 base, uint word) {
101 uint carry;
102 uint lo = uaddCarry(base.x, word * 4u, carry);
103 return uvec2(lo, base.y + carry);
104}
105uint load(uvec2 base, uint word) { return Words(offset(base, word)).w[0]; }
106uvec2 load2(uvec2 base, uint word) { return uvec2(load(base, word), load(base, word + 1u)); }
107
108// FNV-1a over the four dwords, then MurmurHash3's fmix32: without the finalizer the low bits (the table index) depend
109// only on the low bits of each dword, and V#s whose bases differ only above bit 20 all fell into one probe chain.
110uint key_hash(uvec4 k) {
111 uint h = 2166136261u;
112 h = (h ^ k.x) * 16777619u;
113 h = (h ^ k.y) * 16777619u;
114 h = (h ^ k.z) * 16777619u;
115 h = (h ^ k.w) * 16777619u;
116 h ^= h >> 16;
117 h *= 0x85EBCA6Bu;
118 h ^= h >> 13;
119 h *= 0xC2B2AE35u;
120 h ^= h >> 16;
121 return h;
122}
123
124// [v, v + n) inside one of `count` sorted, disjoint spans at `spans`.
125bool spans_hold(uvec2 spans, uint count, uvec2 v, uvec2 n) {
126 uint lo = 0u, hi = count;
127 while (lo < hi) {
128 const uint mid = (lo + hi) >> 1u;
129 if (less64(v, load2(spans, SPAN_WORDS * mid))) hi = mid;
130 else lo = mid + 1u;
131 }
132 if (lo == 0u) return false;
133 const uvec2 end = load2(spans, SPAN_WORDS * (lo - 1u) + 2u);
134 return less64(v, end) && !less64(sub64(end, v), n);
135}
136
137// Some span of `count` sorted, disjoint spans at `spans` overlaps [v, v + n).
138bool spans_meet(uvec2 spans, uint count, uvec2 v, uvec2 n) {
139 const uvec2 last = add64(v, n); // spans starting below it
140 uint lo = 0u, hi = count;
141 while (lo < hi) {
142 const uint mid = (lo + hi) >> 1u;
143 if (less64(load2(spans, SPAN_WORDS * mid), last)) lo = mid + 1u;
144 else hi = mid;
145 }
146 if (lo == 0u) return false;
147 return less64(v, load2(spans, SPAN_WORDS * (lo - 1u) + 2u));
148}
149
150// A 48-bit V# base as the shader VA it names: sign-extended from bit 47 with FLAG_SIGNED.
151uvec2 shader_va(uvec2 base48) {
152 uvec2 v = uvec2(base48.x, base48.y & 0xffffu);
153 if ((pc.flags & FLAG_SIGNED) != 0u && (v.y & 0x8000u) != 0u) v.y |= 0xffff0000u;
154 return v;
155}
156
157// Metadata of one V# half: its title base and span before the pass, and the base it got (0: no records).
158// x = title base low, y = title base bits 32-47 | result bits 32-47 << 16, z = result low, w = span (saturated).
159uvec4 meta_of(uvec4 before, uvec4 after) {
160 if ((before.w >> 30) != 0u || before.z == 0u) return uvec4(0u);
161 const uint stride = (before.y >> 16) & 0x3fffu;
162 uvec2 span;
163 umulExtended(before.z, stride == 0u ? 1u : stride, span.y, span.x);
164 const uvec2 to = after.z == 0u ? uvec2(0u) : uvec2(after.x, after.y & 0xffffu);
165 return uvec4(before.x, (before.y & 0xffffu) | (to.y << 16), to.x, span.y != 0u ? 0xffffffffu : span.x);
166}
167
168// The half's metadata meets a change span: its title base in a title span, or its base or result (with its span) in
169// a shader span. What DeviceState's CPU release filter asks of a slot (LEONIDA_CONSOLE_RELEASE_FILTER).
170bool affected(uvec4 m) {
171 if (m.x == 0u && m.y == 0u && m.z == 0u) return false;
172 const uvec2 base = uvec2(m.x, m.y & 0xffffu);
173 const uvec2 span = uvec2(m.w, 0u);
174 if (pc.ctitle_count != 0u && spans_meet(pc.ctitle, pc.ctitle_count, base, uvec2(1u, 0u))) return true;
175 if (pc.cshader_count == 0u) return false;
176 if (spans_meet(pc.cshader, pc.cshader_count, shader_va(base), span)) return true;
177 const uvec2 to = uvec2(m.z, m.y >> 16);
178 return !(to.x == 0u && to.y == 0u) && spans_meet(pc.cshader, pc.cshader_count, shader_va(to), span);
179}
180
181void rebase(inout uvec4 k, uvec2 to) {
182 k.x = to.x;
183 k.y = (k.y & 0xffff0000u) | (to.y & 0xffffu);
184}
185void no_records(inout uvec4 k) {
186 k.x = 0u;
187 k.y = 0u;
188 k.z = 0u;
189}
190
191// 0: left as it is, 1: rebased or written with no records by a table, 2: missed (written with no records).
192uint translate(inout uvec4 k) {
193 if ((k.w >> 30) != 0u || k.z == 0u) return 0u;
194 const uvec2 base = uvec2(k.x, k.y & 0xffffu);
195 if (base.x == 0u && base.y == 0u) {
196 no_records(k);
197 return 1u;
198 }
199 const uint stride = (k.y >> 16) & 0x3fffu;
200 uvec2 span;
201 umulExtended(k.z, stride == 0u ? 1u : stride, span.y, span.x);
202 // The last range that starts at or below base.
203 uint lo = 0u, hi = pc.range_count;
204 while (lo < hi) {
205 const uint mid = (lo + hi) >> 1u;
206 if (less64(base, load2(pc.ranges, RANGE_WORDS * mid))) hi = mid;
207 else lo = mid + 1u;
208 }
209 if (lo > 0u) {
210 const uint e = RANGE_WORDS * (lo - 1u);
211 const uvec2 end = load2(pc.ranges, e + 2u);
212 if (less64(base, end) && (load(pc.ranges, e + 6u) & RANGE_CHECKED) != 0u) {
213 if (less64(sub64(end, base), span)) {
214 no_records(k);
215 } else {
216 const uvec2 start = load2(pc.ranges, e);
217 rebase(k, add64(load2(pc.ranges, e + 4u), sub64(base, start)));
218 }
219 return 1u;
220 }
221 }
222 uvec2 shader = base;
223 if ((pc.flags & FLAG_SIGNED) != 0u && (base.y & 0x8000u) != 0u) shader.y |= 0xffff0000u;
224 if (pc.backed_count != 0u && spans_hold(pc.backed, pc.backed_count, shader, span.x == 0u && span.y == 0u ? uvec2(1u, 0u) : span))
225 return 1u;
226 const uint h = key_hash(k);
227 for (uint p = 0u; p < KEY_PROBES; ++p) {
228 const uint at = KEY_WORDS * ((h + p) & pc.key_mask);
229 const uint flags = load(pc.keys, at + 5u) >> 16;
230 if ((flags & KEY_USED) == 0u) continue;
231 if (load(pc.keys, at) != k.x || load(pc.keys, at + 1u) != k.y || load(pc.keys, at + 2u) != k.z ||
232 load(pc.keys, at + 3u) != k.w)
233 continue;
234 if ((flags & KEY_NONE) != 0u) no_records(k);
235 else rebase(k, uvec2(load(pc.keys, at + 4u), load(pc.keys, at + 5u) & 0xffffu));
236 return 1u;
237 }
238 no_records(k);
239 if ((pc.flags & FLAG_ASK) != 0u && !spans_hold(pc.ask, pc.ask_count, base, uvec2(1u, 0u))) return 1u;
240 return 2u;
241}
242
243void miss(uint code) {
244 Words m = Words(pc.misses);
245 const uint at = atomicAdd(m.w[0], 1u);
246 if (at < m.w[1]) m.w[MISS_HEADER + at] = code;
247 else m.w[2] = 1u;
248}
249
250void main() {
251 const uint i = gl_GlobalInvocationID.x;
252 if (i >= pc.count) return;
253 if (pc.mode == MODE_UPDATE) {
254 const uvec2 record = offset(pc.work, UPDATE_WORDS * i);
255 const uint index = Words(record).w[0];
256 Words table = Words(offset(pc.keys, KEY_WORDS * index));
257 for (uint j = 0u; j < KEY_WORDS; ++j) table.w[j] = Words(record).w[4u + j];
258 return;
259 }
260 // A work item is a slot index; bit 31: the CPU found the slot neither a buffer pair nor a T# it can decode, and
261 // dwords 0-3 are written as zeros (fix_console_slot's rule: such words must not reach a shader as a descriptor).
262 const bool range = (pc.flags & FLAG_RANGE) != 0u;
263 const uint item = range ? pc.work.x + i : Words(pc.work).w[i];
264 const uint slot = item & 0x7fffffffu;
265 const bool has_meta = !(pc.meta.x == 0u && pc.meta.y == 0u);
266 // A filtered rescan reads only the device-local metadata of a slot it leaves alone, not the title's bytes.
267 if (range && (pc.flags & FLAG_FILTER) != 0u && has_meta) {
268 Slots m = Slots(pc.meta);
269 if (!affected(m.v[2u * slot]) && !affected(m.v[2u * slot + 1u])) return;
270 }
271 Slots source = Slots(pc.input_slots);
272 uvec4 a = source.v[2u * slot];
273 uvec4 b = source.v[2u * slot + 1u];
274 // A rescan rewrites buffer pairs only: every other slot keeps what its last listed pass wrote.
275 if (range && (a.w >> 30) != 0u) return;
276 if ((item >> 31) != 0u && (a.w >> 30) != 0u) a = uvec4(0u);
277 const uvec4 a0 = a, b0 = b;
278 if ((a.w >> 30) == 0u) {
279 // A miss is (heap << 29) | (slot << 1) | half: up to eight heaps of 2^28 slots.
280 if (translate(a) == 2u) miss((pc.heap << 29) | (slot << 1u));
281 if (translate(b) == 2u) miss((pc.heap << 29) | (slot << 1u) | 1u);
282 }
283 Slots target = Slots(pc.copy_slots);
284 target.v[2u * slot] = a;
285 target.v[2u * slot + 1u] = b;
286 if (has_meta) {
287 Slots m = Slots(pc.meta);
288 const bool pair = (a0.w >> 30) == 0u;
289 m.v[2u * slot] = pair ? meta_of(a0, a) : uvec4(0u);
290 m.v[2u * slot + 1u] = pair ? meta_of(b0, b) : uvec4(0u);
291 }
292}