upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/shadow_keep_test.cpp
diff options
context:
space:
mode:
authorDadaCap <167002618+DadaCap@users.noreply.github.com>2026-11-26 00:46:03 +0100
committerDadaCap <167002618+DadaCap@users.noreply.github.com>2026-11-26 01:22:14 +0100
commit0ebd4286ea80b5de20dbf7adf7bf6b970c7492df (patch)
tree351ecd034823d3274e316d056cd570da3e9c3518 /runtime/test/shadow_keep_test.cpp
parent33bd07cf86bb42236635c27341359b3fe841d1d5 (diff)
d3d12: a slot-133 shadow keeps the GPU's writes where the title's bytes did not change (LEONIDA_SHADOW_KEEP)
ShadowPages copies pages that carry no sign of a title write (aliased, unguarded, reopened with their 64 KiB block, write-watched or pinned) at every refresh, and that copy overwrote what a shader had stored in the shadow with the title's unchanged bytes. A value a compute program writes for the next frame then read back as the title's old bytes every other frame. Each tracker now keeps a 64-bit sum per 256-byte piece of the bytes it copied last (runtime/src/shadow_keep.h) and copies a piece only when the title's bytes differ. Zeroed pages forget their sums. LEONIDA_SHADOW_KEEP=0 copies every job whole, as before. New CPU-only test shadow_keep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
Diffstat (limited to 'runtime/test/shadow_keep_test.cpp')
-rw-r--r--runtime/test/shadow_keep_test.cpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/runtime/test/shadow_keep_test.cpp b/runtime/test/shadow_keep_test.cpp
new file mode 100644
index 000000000..87960c0a2
--- /dev/null
+++ b/runtime/test/shadow_keep_test.cpp
@@ -0,0 +1,141 @@
1// src/shadow_keep.h: a shadow copy keeps a GPU write until the title's bytes under it change. No Windows headers, no
2// GPU, no game data: the "title" and its "copy" are arrays here, and the values are made up.
3#include "../src/shadow_keep.h"
4
5#include <cstdint>
6#include <cstdio>
7#include <cstring>
8#include <utility>
9#include <vector>
10
11namespace {
12
13int failures = 0;
14#define EXPECT(condition) \
15 do { \
16 if (!(condition)) { \
17 std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \
18 ++failures; \
19 } \
20 } while (0)
21
22using leonida::shadow_keep::kPiece;
23
24// A title range and its copy, as ShadowPages has them: the copy of the title byte at address a is copy[a - base].
25struct Pages {
26 static constexpr std::uintptr_t base = 0x10000; // kPiece-aligned, like a tracker's first page
27 std::vector<std::uint8_t> title, copy;
28 std::vector<std::uint64_t> sums;
29 std::vector<std::pair<std::uintptr_t, std::uintptr_t>> copied; // the ranges the last refresh copied
30
31 explicit Pages(std::size_t bytes) : title(bytes), copy(bytes), sums(bytes / kPiece) {
32 for (std::size_t i = 0; i < bytes; ++i) title[i] = std::uint8_t(i * 7 + 3);
33 }
34 // A refresh of [va, va + n): what ShadowPages::copy_changed does for one page.
35 std::size_t refresh(std::uintptr_t va, std::size_t n) {
36 copied.clear();
37 std::size_t bytes = 0;
38 leonida::shadow_keep::copy_changed(
39 title.data() + (va - base), va, n, sums.data(), base,
40 [&](std::uintptr_t from, std::uintptr_t to, const std::uint8_t* source) {
41 std::memcpy(copy.data() + (from - base), source, to - from);
42 copied.emplace_back(from, to);
43 bytes += to - from;
44 });
45 return bytes;
46 }
47 std::size_t refresh() { return refresh(base, title.size()); }
48 bool same(std::size_t from, std::size_t to) const {
49 return !std::memcmp(title.data() + from, copy.data() + from, to - from);
50 }
51};
52
53void unchanged_title_keeps_gpu_write() {
54 Pages p(4096);
55 EXPECT(p.refresh() == 4096); // nothing known yet: all of it
56 EXPECT(p.same(0, 4096));
57 EXPECT(p.refresh() == 0); // nothing changed
58 // A shader stores 16 bytes in the copy; the title's bytes there stay as they were.
59 const std::uint8_t gpu[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
60 std::memcpy(p.copy.data() + 0x340, gpu, sizeof(gpu));
61 EXPECT(p.refresh() == 0);
62 EXPECT(!std::memcmp(p.copy.data() + 0x340, gpu, sizeof(gpu))); // kept: the old code overwrote it here
63 EXPECT(p.same(0, 0x340) && p.same(0x350, 4096));
64}
65
66void changed_title_piece_is_copied() {
67 Pages p(4096);
68 p.refresh();
69 const std::uint8_t gpu[16] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
70 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
71 std::memcpy(p.copy.data() + 0x340, gpu, sizeof(gpu)); // GPU write in piece 3
72 std::memcpy(p.copy.data() + 0x800, gpu, sizeof(gpu)); // and in piece 8
73 p.title[0x810] ^= 0x5A; // the title writes piece 8
74 EXPECT(p.refresh() == kPiece);
75 EXPECT(p.copied.size() == 1 && p.copied[0].first == Pages::base + 0x800 &&
76 p.copied[0].second == Pages::base + 0x900);
77 EXPECT(p.same(0x800, 0x900)); // the title's new bytes win there
78 EXPECT(!std::memcmp(p.copy.data() + 0x340, gpu, sizeof(gpu))); // the other GPU write stays
79 EXPECT(p.refresh() == 0);
80}
81
82void partial_first_and_last_pieces() {
83 Pages p(4096);
84 // A target that starts and ends inside pieces: [base + 100, base + 1000).
85 const std::uintptr_t va = Pages::base + 100;
86 const std::size_t n = 900;
87 EXPECT(p.refresh(va, n) == n);
88 EXPECT(p.copied.size() == 4);
89 EXPECT(p.copied.front().first == va && p.copied.front().second == Pages::base + kPiece);
90 EXPECT(p.copied.back().first == Pages::base + 3 * kPiece && p.copied.back().second == va + n);
91 EXPECT(p.same(100, 1000));
92 EXPECT(p.copy[99] == 0 && p.copy[1000] == 0); // nothing outside the range
93 EXPECT(p.refresh(va, n) == 0);
94 p.title[999] ^= 1; // last byte of the last, partial piece
95 EXPECT(p.refresh(va, n) == 1000 - 3 * kPiece);
96 p.title[100] ^= 1; // first byte of the first, partial piece
97 EXPECT(p.refresh(va, n) == kPiece - 100);
98 EXPECT(p.same(100, 1000));
99}
100
101void forgotten_sums_copy_once() {
102 Pages p(4096);
103 p.refresh();
104 // The pages were not committed for a refresh: the copies were zeroed and their sums forgotten.
105 std::memset(p.copy.data() + 0x400, 0, 0x200);
106 leonida::shadow_keep::forget(p.sums.data(), Pages::base, Pages::base + 0x400, Pages::base + 0x600);
107 EXPECT(p.refresh() == 0x200); // the same title bytes back: copied once all the same
108 EXPECT(p.same(0, 4096));
109 EXPECT(p.refresh() == 0);
110 // A partial range forgets every piece it touches.
111 leonida::shadow_keep::forget(p.sums.data(), Pages::base, Pages::base + 0x410, Pages::base + 0x501);
112 EXPECT(p.refresh() == 0x200);
113}
114
115void sums_tell_bytes_apart() {
116 std::uint8_t a[kPiece] = {}, b[kPiece] = {};
117 EXPECT(leonida::shadow_keep::sum_of(a, kPiece) != 0);
118 EXPECT(leonida::shadow_keep::sum_of(a, kPiece) == leonida::shadow_keep::sum_of(b, kPiece));
119 for (std::size_t i = 0; i < kPiece; ++i) {
120 b[i] = 1;
121 EXPECT(leonida::shadow_keep::sum_of(a, kPiece) != leonida::shadow_keep::sum_of(b, kPiece));
122 b[i] = 0;
123 }
124 EXPECT(leonida::shadow_keep::sum_of(a, 16) != leonida::shadow_keep::sum_of(a, 17)); // the length counts
125}
126
127} // namespace
128
129int main() {
130 unchanged_title_keeps_gpu_write();
131 changed_title_piece_is_copied();
132 partial_first_and_last_pieces();
133 forgotten_sums_copy_once();
134 sums_tell_bytes_apart();
135 if (failures) {
136 std::fprintf(stderr, "shadow_keep: %d check(s) failed\n", failures);
137 return 1;
138 }
139 std::puts("shadow_keep: OK");
140 return 0;
141}