diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
| commit | 154e4ec034902deffa4b89b2cbca9a949f38baec (patch) | |
| tree | 1b2ef5656de42c172291bc6009efa4c510650815 /runtime/test/clzero_patch_test.cpp | |
| parent | eb6a08cd359d98b5777e70d54d0ae8a75879495a (diff) | |
| parent | 8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff) | |
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by
one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the
two solve one problem in two ways the base's is the frame and #939's plugs into it.
A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h,
d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions
ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's
proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease,
raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages),
the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's
duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in.
A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow
#939's rule that a device which ever had a handler has no queue fallback.
B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h,
pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile
and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps
the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad
lists as triangle indices.
B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list,
shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where
both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch,
executable shaders, default A8 views).
C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h,
rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp,
replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its
rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the
label fence on the base's submit.
D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test,
main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with
#939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's
answering first; --first-run-ui kept without host speech in its profile.
E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py,
scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's
v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the
base's executable-shader step with #939's cache fix; 366 registered tests.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/clzero_patch_test.cpp')
| -rw-r--r-- | runtime/test/clzero_patch_test.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/runtime/test/clzero_patch_test.cpp b/runtime/test/clzero_patch_test.cpp new file mode 100644 index 000000000..dc34de8f8 --- /dev/null +++ b/runtime/test/clzero_patch_test.cpp | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | // clzero_patch.h: the two CLZERO loop shapes the title uses are found, rewritten to jump to stubs, and the stubs | ||
| 2 | // zero exactly the lines the loops would zero and leave the registers the loops leave. The loops are hand-assembled | ||
| 3 | // here (no game code), and only the patched copies run, so the test runs on any x86-64 CPU. | ||
| 4 | #include <windows.h> | ||
| 5 | |||
| 6 | #include <cstdint> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <cstring> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "clzero_patch.h" | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | int failures = 0; | ||
| 16 | int checks = 0; | ||
| 17 | |||
| 18 | void check(bool ok, const char* what) { | ||
| 19 | ++checks; | ||
| 20 | if (!ok) ++failures; | ||
| 21 | std::printf("%s %s\n", ok ? "PASS" : "FAIL", what); | ||
| 22 | } | ||
| 23 | |||
| 24 | // uint64_t f(void* begin, void* end): shape A over [begin, end), returns rax at the loop's exit. | ||
| 25 | const uint8_t kShapeA[] = { | ||
| 26 | 0x48, 0x89, 0xC8, // mov rax, rcx | ||
| 27 | 0x49, 0x89, 0xD2, // mov r10, rdx | ||
| 28 | 0x0F, 0x01, 0xFC, // L: clzero | ||
| 29 | 0x48, 0x83, 0xC0, 0x40, // add rax, 40h | ||
| 30 | 0x4C, 0x39, 0xD0, // cmp rax, r10 | ||
| 31 | 0x72, 0xF4, // jb L | ||
| 32 | 0xC3, // ret | ||
| 33 | }; | ||
| 34 | |||
| 35 | // uint64_t f(void* begin, void* end): shape B over [begin, end), returns the counter at the loop's exit. | ||
| 36 | const uint8_t kShapeB[] = { | ||
| 37 | 0x49, 0x89, 0xCA, // mov r10, rcx | ||
| 38 | 0x49, 0x89, 0xD3, // mov r11, rdx | ||
| 39 | 0x4C, 0x89, 0xD0, // L: mov rax, r10 | ||
| 40 | 0x49, 0x83, 0xC2, 0x40, // add r10, 40h | ||
| 41 | 0x0F, 0x01, 0xFC, // clzero | ||
| 42 | 0x4D, 0x39, 0xDA, // cmp r10, r11 | ||
| 43 | 0x72, 0xF1, // jb L | ||
| 44 | 0x4C, 0x89, 0xD0, // mov rax, r10 | ||
| 45 | 0xC3, // ret | ||
| 46 | }; | ||
| 47 | |||
| 48 | // Shape A with cmp rax, rcx: rcx is the register the stub saves, so this loop is left alone. | ||
| 49 | const uint8_t kShapeAWithRcx[] = { | ||
| 50 | 0x0F, 0x01, 0xFC, // clzero | ||
| 51 | 0x48, 0x83, 0xC0, 0x40, // add rax, 40h | ||
| 52 | 0x48, 0x39, 0xC8, // cmp rax, rcx | ||
| 53 | 0x72, 0xF4, // jb | ||
| 54 | 0xC3, | ||
| 55 | }; | ||
| 56 | |||
| 57 | using Loop = uint64_t (*)(void*, void*); | ||
| 58 | |||
| 59 | // Fills a buffer, runs `loop` over [buf + first, buf + last) and checks which bytes changed. | ||
| 60 | bool zeroes_exactly(Loop loop, size_t first, size_t last, uint64_t& returned) { | ||
| 61 | alignas(64) static uint8_t buf[4096]; | ||
| 62 | std::memset(buf, 0xAB, sizeof buf); | ||
| 63 | returned = loop(buf + first, buf + last); | ||
| 64 | for (size_t i = 0; i < sizeof buf; ++i) { | ||
| 65 | const bool inside = i >= (first & ~size_t(63)) && i < last; | ||
| 66 | if (buf[i] != (inside ? 0 : 0xAB)) return false; | ||
| 67 | } | ||
| 68 | return returned == reinterpret_cast<uint64_t>(buf + last); | ||
| 69 | } | ||
| 70 | |||
| 71 | } // namespace | ||
| 72 | |||
| 73 | int main() { | ||
| 74 | namespace cz = rt::clzero_patch; | ||
| 75 | { | ||
| 76 | const auto a = cz::find(kShapeA, sizeof kShapeA); | ||
| 77 | check(a.size() == 1 && a[0].head == 6 && a[0].length == 12 && a[0].clzero == 0, "shape A is found at its CLZERO"); | ||
| 78 | const auto b = cz::find(kShapeB, sizeof kShapeB); | ||
| 79 | check(b.size() == 1 && b[0].head == 6 && b[0].length == 15 && b[0].clzero == 7, | ||
| 80 | "shape B is found at its mov rax, CUR"); | ||
| 81 | check(cz::find(kShapeAWithRcx, sizeof kShapeAWithRcx).empty(), "a loop that compares against rcx is left alone"); | ||
| 82 | const uint8_t lone[] = {0x0F, 0x01, 0xFC, 0xC3}; | ||
| 83 | check(cz::find(lone, sizeof lone).empty(), "a CLZERO outside both loop shapes is left alone"); | ||
| 84 | } | ||
| 85 | |||
| 86 | auto* code = static_cast<uint8_t*>(VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); | ||
| 87 | check(code != nullptr, "code page"); | ||
| 88 | if (!code) return 1; | ||
| 89 | std::memcpy(code, kShapeA, sizeof kShapeA); | ||
| 90 | std::memcpy(code + 0x100, kShapeB, sizeof kShapeB); | ||
| 91 | const auto r = cz::patch({{code, 0x1000}}, uint64_t(code), uint64_t(code) + 0x1000); | ||
| 92 | check(r.sites == 2 && r.stubs != nullptr, "both loops are patched"); | ||
| 93 | check(code[6] == 0xE9 && code[0x106] == 0xE9, "each loop starts with a jmp rel32 to its stub"); | ||
| 94 | const int64_t distance = int64_t(r.stubs) - int64_t(code); | ||
| 95 | check(distance > -(int64_t(1) << 31) && distance < (int64_t(1) << 31), "the stubs are within 2 GiB of the code"); | ||
| 96 | FlushInstructionCache(GetCurrentProcess(), code, 0x1000); | ||
| 97 | |||
| 98 | const auto shape_a = reinterpret_cast<Loop>(code); | ||
| 99 | const auto shape_b = reinterpret_cast<Loop>(code + 0x100); | ||
| 100 | uint64_t ret = 0; | ||
| 101 | check(zeroes_exactly(shape_a, 64, 64 + 1024, ret), "shape A zeroes its 16 lines and returns rax = end"); | ||
| 102 | check(zeroes_exactly(shape_a, 128, 192, ret), "shape A runs once for a one-line range"); | ||
| 103 | check(zeroes_exactly(shape_b, 256, 256 + 0x380, ret), "shape B zeroes its 14 lines and leaves CUR = end"); | ||
| 104 | { | ||
| 105 | // An unaligned start: CLZERO zeroes the whole line rAX points into, and the loop steps by 64 from rAX. | ||
| 106 | alignas(64) static uint8_t buf[512]; | ||
| 107 | std::memset(buf, 0xAB, sizeof buf); | ||
| 108 | shape_a(buf + 64 + 8, buf + 64 + 8 + 128); | ||
| 109 | bool ok = true; | ||
| 110 | for (size_t i = 0; i < sizeof buf; ++i) ok &= buf[i] == ((i >= 64 && i < 64 + 128) ? 0 : 0xAB); | ||
| 111 | check(ok, "an unaligned start zeroes the lines rAX points into"); | ||
| 112 | } | ||
| 113 | |||
| 114 | std::printf("clzero_patch_test: %s (%d checks)\n", failures ? "FAILED" : "OK", checks); | ||
| 115 | return failures ? 1 : 0; | ||
| 116 | } | ||