diff options
| author | Etr0p <216035314+Etr0p@users.noreply.github.com> | 2026-11-23 20:51:08 +0100 |
|---|---|---|
| committer | Etr0p <216035314+Etr0p@users.noreply.github.com> | 2026-11-23 20:51:08 +0100 |
| commit | d2f328e560dcbbdbc0e34f5d26f5855c5ffd8209 (patch) | |
| tree | c7d02861ec4da9a94a5923e7371f33b1bad29910 /runtime/src | |
| parent | 65668002e972259722f008617068dd8ee90c82ce (diff) | |
d3d12: only the title's memory gets a copy; the table scan covers the declared slots and is timed
The xmem shims record what they hand the title (XMemAlloc, XMemAllocDefault,
XMemVirtualAlloc, the title's VirtualAlloc); an address outside those ranges
is left as it was and counted. The submit-time scan reads only the
CBV/SRV/UAV slots the bound table's root signature declares, and its cost
is accumulated per ExecuteCommandLists. Section 3.3 describes the three
places addresses are replaced and the two kinds of copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/title_va_alias.h | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/runtime/src/title_va_alias.h b/runtime/src/title_va_alias.h index 6f665f0ec..1e907ea43 100644 --- a/runtime/src/title_va_alias.h +++ b/runtime/src/title_va_alias.h | |||
| @@ -37,7 +37,7 @@ public: | |||
| 37 | // An address in the title's memory: true, with `shader_va` what a shader must be given (the address itself when | 37 | // An address in the title's memory: true, with `shader_va` what a shader must be given (the address itself when |
| 38 | // no copy could be made). False: not the title's memory (an arena VA, a buffer of the layer's). | 38 | // no copy could be made). False: not the title's memory (an arena VA, a buffer of the layer's). |
| 39 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); | 39 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); |
| 40 | using TableScanner = void (*)(void* context, std::uint64_t table_va); | 40 | using TableScanner = void (*)(void* context, std::uint64_t table_va, std::uint32_t descriptors); |
| 41 | 41 | ||
| 42 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one | 42 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one |
| 43 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. | 43 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. |
| @@ -109,9 +109,9 @@ public: | |||
| 109 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); | 109 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | // Before a submission: the device rewrites the buffer V#s of the table at `table_va` (a console VA in one of its | 112 | // Before a submission: the device rewrites the buffer V#s of the `descriptors` slots the root signature declares |
| 113 | // shader-visible CBV/SRV/UAV heaps). | 113 | // for the table at `table_va` (a console VA in one of its shader-visible CBV/SRV/UAV heaps). |
| 114 | void scan_table(std::uint64_t table_va) { | 114 | void scan_table(std::uint64_t table_va, std::uint32_t descriptors) { |
| 115 | TableScanner scanner; | 115 | TableScanner scanner; |
| 116 | void* context; | 116 | void* context; |
| 117 | { | 117 | { |
| @@ -119,7 +119,52 @@ public: | |||
| 119 | scanner = scanner_; | 119 | scanner = scanner_; |
| 120 | context = hook_context_; | 120 | context = hook_context_; |
| 121 | } | 121 | } |
| 122 | if (scanner) scanner(context, table_va); | 122 | if (scanner) scanner(context, table_va, descriptors); |
| 123 | } | ||
| 124 | |||
| 125 | // The scan's cost, per ExecuteCommandLists that had tables to scan. | ||
| 126 | void note_scan(std::uint64_t nanoseconds) { | ||
| 127 | scans_.fetch_add(1, std::memory_order_relaxed); | ||
| 128 | scan_ns_.fetch_add(nanoseconds, std::memory_order_relaxed); | ||
| 129 | std::uint64_t seen = scan_max_ns_.load(std::memory_order_relaxed); | ||
| 130 | while (nanoseconds > seen && !scan_max_ns_.compare_exchange_weak(seen, nanoseconds)) { | ||
| 131 | } | ||
| 132 | } | ||
| 133 | std::uint64_t scans() const { return scans_.load(std::memory_order_relaxed); } | ||
| 134 | std::uint64_t scan_ns() const { return scan_ns_.load(std::memory_order_relaxed); } | ||
| 135 | std::uint64_t scan_max_ns() const { return scan_max_ns_.load(std::memory_order_relaxed); } | ||
| 136 | |||
| 137 | // The title's own memory, as the xmem shims hand it out (XMemAlloc, XMemVirtualAlloc, the title's VirtualAlloc): | ||
| 138 | // only an address in one of these ranges gets a copy. A base anywhere else (the runtime's own heap, a host DLL) | ||
| 139 | // is left as it was and counted. | ||
| 140 | void add_title_memory(std::uint64_t base, std::uint64_t bytes) { | ||
| 141 | if (!base || !bytes || base > UINT64_MAX - bytes) return; | ||
| 142 | std::uint64_t begin = base, end = base + bytes; | ||
| 143 | std::unique_lock lock(memory_mutex_); | ||
| 144 | // A commit inside a reserved range, or two that overlap, are one range. | ||
| 145 | auto it = memory_.upper_bound(begin); | ||
| 146 | if (it != memory_.begin() && std::prev(it)->second > begin) --it; | ||
| 147 | while (it != memory_.end() && it->first < end) { | ||
| 148 | begin = std::min(begin, it->first); | ||
| 149 | end = std::max(end, it->second); | ||
| 150 | it = memory_.erase(it); | ||
| 151 | } | ||
| 152 | memory_[begin] = end; | ||
| 153 | } | ||
| 154 | void remove_title_memory(std::uint64_t base) { | ||
| 155 | std::unique_lock lock(memory_mutex_); | ||
| 156 | memory_.erase(base); | ||
| 157 | } | ||
| 158 | // The title range that holds `va`: [lo, hi). False when none does. | ||
| 159 | bool title_memory(std::uint64_t va, std::uint64_t& lo, std::uint64_t& hi) const { | ||
| 160 | std::shared_lock lock(memory_mutex_); | ||
| 161 | auto it = memory_.upper_bound(va); | ||
| 162 | if (it == memory_.begin()) return false; | ||
| 163 | --it; | ||
| 164 | if (va >= it->second) return false; | ||
| 165 | lo = it->first; | ||
| 166 | hi = it->second; | ||
| 167 | return true; | ||
| 123 | } | 168 | } |
| 124 | 169 | ||
| 125 | // The first device to register keeps the hooks until it clears them. | 170 | // The first device to register keeps the hooks until it clears them. |
| @@ -166,6 +211,9 @@ private: | |||
| 166 | TableScanner scanner_ = nullptr; | 211 | TableScanner scanner_ = nullptr; |
| 167 | std::array<std::atomic<std::uint64_t>, kind_count> translated_{}; | 212 | std::array<std::atomic<std::uint64_t>, kind_count> translated_{}; |
| 168 | std::array<std::atomic<std::uint64_t>, kind_count> untranslated_{}; | 213 | std::array<std::atomic<std::uint64_t>, kind_count> untranslated_{}; |
| 214 | std::atomic<std::uint64_t> scans_{0}, scan_ns_{0}, scan_max_ns_{0}; | ||
| 215 | mutable std::shared_mutex memory_mutex_; | ||
| 216 | std::map<std::uint64_t, std::uint64_t> memory_; // the title's memory: base -> end | ||
| 169 | }; | 217 | }; |
| 170 | 218 | ||
| 171 | // The process's table: the device's title ranges and hooks, read by the command lists and descriptor writers. | 219 | // The process's table: the device's title ranges and hooks, read by the command lists and descriptor writers. |