diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:58:44 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:58:44 -0500 |
| commit | a127c11a84a09588cd35cfcffa5fb2bf733c35bb (patch) | |
| tree | d544a8a3cca56a96de03dcc9de9cf022c744c517 /runtime/src | |
| parent | 0ce9f8992cedaa1e9f7668c34b1a04b15eca4770 (diff) | |
GPU labels reach a title page the D3D12 layer keeps read-only while it watches it
A completed label was stored only into a page that was read-write at that moment and dropped otherwise. The
base makes title pages read-only while a shadow or the geometry watch waits for the title's next write, so a
label for such a page was lost, and a title that waits for a completion label then waits for ever. The store
now announces the write the way a kernel write is announced (kernel_writes::begin_write): the layer opens the
pages it guards and copies them again at its next refresh. A page that is still read-only after that is the
title's own doing and the label is dropped as before; so are guard, no-access and decommitted pages and a
released allocation. The store moves out of the class as label::store_value so that it can be tested.
gpu_label_store_test (CPU only, registered as gpu_label_store) covers each of those cases with a stand-in for
the layer's opener.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_gpu_labels.h | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/runtime/src/vulkan_gpu_labels.h b/runtime/src/vulkan_gpu_labels.h index 3220d0613..32590cc39 100644 --- a/runtime/src/vulkan_gpu_labels.h +++ b/runtime/src/vulkan_gpu_labels.h | |||
| @@ -18,6 +18,7 @@ | |||
| 18 | #define NOMINMAX | 18 | #define NOMINMAX |
| 19 | #endif | 19 | #endif |
| 20 | #include <windows.h> | 20 | #include <windows.h> |
| 21 | #include "title_page_watch.h" | ||
| 21 | #endif | 22 | #endif |
| 22 | 23 | ||
| 23 | namespace leonida::label { | 24 | namespace leonida::label { |
| @@ -28,6 +29,51 @@ inline bool enabled() { | |||
| 28 | }(); | 29 | }(); |
| 29 | return active; | 30 | return active; |
| 30 | } | 31 | } |
| 32 | // Stores a completed label into the title's memory: only into a committed, private, writable page of the allocation | ||
| 33 | // the label was captured in, and only while that allocation's generation is the captured one (Memory::publish). | ||
| 34 | // | ||
| 35 | // A page the D3D12 layer watches for the title's writes is read-only while it waits (title_page_watch.h), and it is | ||
| 36 | // writable as far as the title knows. Such a page is announced and opened the way a kernel write is | ||
| 37 | // (kernel_writes::begin_write), so the label reaches it and the layer copies the page again; a page that stays | ||
| 38 | // read-only after that is the title's own doing and the label is dropped, as before. Called with no shadow lock held. | ||
| 39 | inline bool store_value(const Target &target, std::uint64_t value) { | ||
| 40 | #ifdef _WIN32 | ||
| 41 | return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) { | ||
| 42 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 43 | const auto in_allocation = [&] { | ||
| 44 | return VirtualQuery(reinterpret_cast<const void *>(address), &mbi, sizeof(mbi)) && mbi.State == MEM_COMMIT && | ||
| 45 | mbi.Type == MEM_PRIVATE && reinterpret_cast<std::uintptr_t>(mbi.AllocationBase) == allocation_base && | ||
| 46 | !(mbi.Protect & PAGE_GUARD); | ||
| 47 | }; | ||
| 48 | if (!in_allocation()) return false; | ||
| 49 | std::uint64_t pin = 0; | ||
| 50 | if ((mbi.Protect & 0xff) == PAGE_READONLY) { | ||
| 51 | pin = kernel_writes::begin_write(reinterpret_cast<const void *>(address), width, nullptr); | ||
| 52 | if (!pin || !in_allocation()) { | ||
| 53 | kernel_writes::end_write(pin, false); | ||
| 54 | return false; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | const auto begin = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress); | ||
| 58 | const bool writable = (mbi.Protect & 0xff) == PAGE_READWRITE || (mbi.Protect & 0xff) == PAGE_WRITECOPY; | ||
| 59 | const bool inside = address >= begin && address - begin <= mbi.RegionSize && | ||
| 60 | mbi.RegionSize - (address - begin) >= width; | ||
| 61 | if (writable && inside) { | ||
| 62 | if (width == 4) | ||
| 63 | InterlockedExchange(reinterpret_cast<volatile LONG *>(address), static_cast<LONG>(value)); | ||
| 64 | else | ||
| 65 | InterlockedExchange64(reinterpret_cast<volatile LONG64 *>(address), static_cast<LONG64>(value)); | ||
| 66 | } | ||
| 67 | kernel_writes::end_write(pin, false); | ||
| 68 | return writable && inside; | ||
| 69 | }); | ||
| 70 | #else | ||
| 71 | (void)target; | ||
| 72 | (void)value; | ||
| 73 | return false; | ||
| 74 | #endif | ||
| 75 | } | ||
| 76 | |||
| 31 | // Each submitted Vulkan command buffer owns a fence and its query slots until the GPU finishes it. | 77 | // Each submitted Vulkan command buffer owns a fence and its query slots until the GPU finishes it. |
| 32 | // The CPU destination is a generation-checked title data allocation. No guest pointer is a GPU command destination. | 78 | // The CPU destination is a generation-checked title data allocation. No guest pointer is a GPU command destination. |
| 33 | class Completions { | 79 | class Completions { |
| @@ -85,30 +131,7 @@ class Completions { | |||
| 85 | idle_pools_.push_back(p); | 131 | idle_pools_.push_back(p); |
| 86 | if (b.fence) idle_fences_.push_back(b.fence); | 132 | if (b.fence) idle_fences_.push_back(b.fence); |
| 87 | } | 133 | } |
| 88 | static bool store(const Target &target, std::uint64_t value) { | 134 | static bool store(const Target &target, std::uint64_t value) { return store_value(target, value); } |
| 89 | #ifdef _WIN32 | ||
| 90 | return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) { | ||
| 91 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 92 | if (!VirtualQuery(reinterpret_cast<const void *>(address), &mbi, sizeof(mbi)) || mbi.State != MEM_COMMIT || | ||
| 93 | mbi.Type != MEM_PRIVATE || reinterpret_cast<std::uintptr_t>(mbi.AllocationBase) != allocation_base || | ||
| 94 | (mbi.Protect & PAGE_GUARD) || | ||
| 95 | ((mbi.Protect & 0xff) != PAGE_READWRITE && (mbi.Protect & 0xff) != PAGE_WRITECOPY)) | ||
| 96 | return false; | ||
| 97 | const auto begin = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress); | ||
| 98 | if (address < begin || address - begin > mbi.RegionSize || mbi.RegionSize - (address - begin) < width) | ||
| 99 | return false; | ||
| 100 | if (width == 4) | ||
| 101 | InterlockedExchange(reinterpret_cast<volatile LONG *>(address), static_cast<LONG>(value)); | ||
| 102 | else | ||
| 103 | InterlockedExchange64(reinterpret_cast<volatile LONG64 *>(address), static_cast<LONG64>(value)); | ||
| 104 | return true; | ||
| 105 | }); | ||
| 106 | #else | ||
| 107 | (void)target; | ||
| 108 | (void)value; | ||
| 109 | return false; | ||
| 110 | #endif | ||
| 111 | } | ||
| 112 | void publish(const Batch &batch) { | 135 | void publish(const Batch &batch) { |
| 113 | for (std::size_t i = 0; i < batch.labels.size(); ++i) { | 136 | for (std::size_t i = 0; i < batch.labels.size(); ++i) { |
| 114 | const Write &op = batch.labels[i]; | 137 | const Write &op = batch.labels[i]; |