Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,8 @@ to be recovered from call sites (or the NDA GDK headers, if a developer has acce
| Device slot 61 | The retail title's boot under `gta6rt --run` (stop 20 of the RT-5 measurements in #6910): called as `(desc, dest handle)` once per sampler | `d3d12_xs_device.cpp` routes it to the PC `CreateSampler` (slot 22) with the description's PC `D3D12_SAMPLER_DESC` prefix, for a caller in an image the guest loader mapped; the PC slot 61 is `CreateMetaCommand`, which any other caller (a host test driving the device through the proxy) still gets. The layout of the description beyond the PC fields is **Open**. |
| Device slot 74 | The retail title's boot under `gta6rt --run`: called as `(pointer, u32, pointer)` | **Stand-in.** `d3d12_xs_device.cpp` answers `S_OK` and does nothing, for a caller in an image the guest loader mapped; the PC slot 74 is `ShaderCacheControl`, which the call does not fit and which any other caller still gets. What the console method is, and what it should do, is **Open**. |
| Command-allocator slot 9 | The retail title's boot under `gta6rt --run`: called past the 9-slot PC allocator, as `(function pointer, context, ...)`; `SetAllocatorCallbackX` is the name the commit that added it uses | **Stand-in.** The allocator proxy records the callback and its context (`allocator_proxy_callback`, `allocator_proxy_callback_context`) and answers `S_OK`. **Nothing calls the callback**: the Vulkan layer backs command buffers itself. Whether the title depends on being called back is **Open**. |
| Device slot 81, queue `PresentX`/`SuspendX`/`ResumeX` | XCL (GPL-2.0-only, third-party): `GetFrameStatisticsX` at device slot 81, `PresentX` at queue slot 30 (older runtime layout) or 32 (newer); `SuspendX`/`ResumeX` slots unmeasured | `set_presentation_handler` forwards 78-81 and the queue routes to a presenter (`attach_xbox_presentation`). Without one, slot 81 answers `S_OK` with a zero count (no statistics yet; the answer the retail boot through the story load was measured with, where the public samples' "not available yet" is `S_FALSE`; a null count, or a count above zero with no array, is `E_POINTER`); `PresentX` goes to the presentation the device proxy builds itself on the first call (`queue_present_x`: in the title's window or one of the runtime's, `LEONIDA_PRESENT_WINDOW`), which takes its frame tokens from the same source as the device's `WaitFrameEventX`; every other queue slot past the PC table answers `S_OK` and does nothing. `PresentX` is slot 30 on a queue in the older layout and slot 32 on a queue detected as the newer one (a fence reaching slot 16 or 17, or `SignalX` at 25), never both: in the newer layout slot 30 is another method. Slot 81 moves with `LEONIDA_XS_FRAME_STATISTICS_SLOT` (3..127); `LEONIDA_XS_PRESENT_X_SLOT` pins `PresentX` to one slot whatever the layout; `SuspendX` and `ResumeX` are routed only when `LEONIDA_XS_SUSPEND_X_SLOT` / `LEONIDA_XS_RESUME_X_SLOT` are set (queue slots 19..127; two routes on one slot are refused with a log). |
| Device slot 81, queue `PresentX`/`SuspendX`/`ResumeX` | XCL (GPL-2.0-only, third-party): `GetFrameStatisticsX` at device slot 81, `PresentX` at queue slot 30 (older runtime layout) or 32 (newer); `SuspendX`/`ResumeX` slots unmeasured | `set_presentation_handler` forwards 78-81 and the queue routes to a presenter (`attach_xbox_presentation`). Without one, slot 81 answers `S_OK` with a zero count (no statistics yet; the answer the retail boot through the story load was measured with, where the public samples' "not available yet" is `S_FALSE`; a null count, or a count above zero with no array, is `E_POINTER`); `PresentX` goes to the presentation the device proxy builds itself on the first call (`queue_present_x`: in the title's window or one of the runtime's, `LEONIDA_PRESENT_WINDOW`), which takes its frame tokens from the same source as the device's `WaitFrameEventX`; every other queue slot past the PC table answers `S_OK` and does nothing, except slot 31 (next row). `PresentX` is slot 30 on a queue in the older layout and slot 32 on a queue detected as the newer one (a fence reaching slot 16 or 17, or `SignalX` at 25), never both: in the newer layout slot 30 is another method. Slot 81 moves with `LEONIDA_XS_FRAME_STATISTICS_SLOT` (3..127); `LEONIDA_XS_PRESENT_X_SLOT` pins `PresentX` to one slot whatever the layout; `SuspendX` and `ResumeX` are routed only when `LEONIDA_XS_SUSPEND_X_SLOT` / `LEONIDA_XS_RESUME_X_SLOT` are set (queue slots 19..127; two routes on one slot are refused with a log). |
| Queue slot 31, `CopyPageMappingsBatchX` | Arguments `(count, batches, pool, pool pages, flags)`, a 24-byte batch `{UINT64 destination; UINT32 range count; const range* ranges}` and a 12-byte range `{UINT32 kind, start, count}`: read statically from the retail executable's call site and the code that fills the batches (#968), corroborated by the same call shape in a public project's queue (VIctory, `copy_page_mappings`, where the newer layout puts it at 29), and the pool-size argument equalled the page count of the title's own `XMemAllocatePhysicalPages` call in a boot | Every range goes to xmem's `copy_page_mappings_fn` (`d3d12_xs_device.cpp`, `queue_copy_page_mappings`): kind 0 maps pool pages `start, start + 1, ...` at the destination, 1 maps pool page `start` at every destination page, 2 unmaps, 3 leaves the destination as it is; each range moves the destination on by `count` pages. A destination page gets a second view of the physical page mapped at its source page, and the same view at the paired CPU range when the destination is a GPU-only reservation (`title_gpu_views`), where the layer reads that memory. `S_OK` when every range was applied, `E_INVALIDARG` (logged once) for a range past the pool, an unknown kind or one xmem refuses; without xmem linked (tests of the proxy alone) the slot answers `S_OK` like the other console-only slots. Tested by `xmem_test` and `xs_proxy_mock_test`. **Known gap:** the mappings change when the call is made, not when the queue reaches the batch, as on the console; whether earlier queued work still reads a destination page then is not measured. |
| VRS | Public `D3D12_SHADING_RATE` / `D3D12_AXIS_SHADING_RATE` | `VK_KHR_fragment_shading_rate`; `RSSetShadingRate`/image-based rate map to `vkCmdSetFragmentShadingRateKHR`. |
| Mesh shader tier | Public `D3D12_MESH_SHADER_TIER` | `VK_EXT_mesh_shader`; the 9,282 translated mesh shaders make this a hard requirement for any content that uses them. |
| Wave MMA | Public `D3D12_WAVE_MMA_TIER` | Not used: no shader sets `WAVE_MMA` in SFI0 and none contains `v_mfma`/`v_wmma` (all 26,503, `tools/feature_scan.py`). Answer the tier query truthfully; no cooperative-matrix mapping is needed. |
Expand Down
14 changes: 9 additions & 5 deletions docs/linux_proton.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ Each item was a stop on at least one of the machines. Tests named in the pull re

## Where it stops

- **Streamed memory that never reaches its destination.** The title calls queue slot 31 (`CopyPageMappingsBatchX`)
about 300 times per boot and this branch still ignores it; tens of thousands of streamed views wait for memory
that never becomes readable. An open pull request in this repository implements it.
- **Culling compute at world start** reads buffers named by descriptors in the directly indexed heap; with the
point above unfixed those reads leave mapped memory and the device is lost (NVIDIA Xid 13/109 on Linux).
- **Streamed memory that reaches its destination late or not at all.** The title calls queue slot 31
(`CopyPageMappingsBatchX`) about 300 times per boot. With #968 this branch maps each batch's pool pages at the
destination and at the paired CPU view the layer reads (`docs/d3d12_vulkan.md`, section 4.9), so most streamed
views find readable memory; before it, tens of thousands of them waited for memory that never became readable. Two gaps
remain: the mappings change when the call is made, not in queue order, and a run with the CPU-view fix still had
views waiting after 420 s, apparently for ranges no mapping call had reached yet.
- **Culling compute at world start** reads buffers named by descriptors in the directly indexed heap; where the
memory behind them is not mapped, those reads leave mapped memory and the device is lost (NVIDIA Xid 13/109 on
Linux).
- **Mesh stages** do not get the heap offset yet.
- **16 GB of RAM:** at the end of the load the process holds ~14.6 GB (RAM plus swap) on the Linux machine after the
cuts above. zram made this worse (compressed pages still occupy RAM); a swap file on disk is the better choice.
Expand Down
87 changes: 87 additions & 0 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "d3d12_com.h" // rt::d3d12::create_console_descriptor_heap for slot 14
#endif
#include "d3d12_trace.h"
#include "xmem.h" // rt::xmem::copy_page_mappings_fn for queue slot 31
#include "d3d12_xs_alloc_size.h"
#include "d3d12_xs_desc_clean.h"
#include "../src/frame_pacer.h"
Expand Down Expand Up @@ -894,6 +895,8 @@ bool is_console_caller(const void* address); // defined with the device proxy
constexpr unsigned kQueueExecuteCommandListsX = 35; // console ExecuteCommandLists(count, lists) variant
static_assert(kQueueExecuteCommandLists < kQueuePublicSlots && kQueueExecuteCommandListsX < kQueueSlots,
"queue execution slots must stay within their respective tables");
// CopyPageMappingsBatchX(count, batches, pool, pool pages, flags), past the public chain (queue_copy_page_mappings).
constexpr unsigned kQueueCopyPageMappingsBatchX = 31;
constexpr unsigned kFenceSignal = 10; // ID3D12Fence::Signal(UINT64), from the CPU
constexpr unsigned kFenceCompletedValueSlot = 12; // one past ID3D12Fence1: the completed value's address
static_assert(kFenceCompletedValueSlot >= kFence1PublicSlots && kFenceCompletedValueSlot < kFenceSlots,
Expand Down Expand Up @@ -1537,6 +1540,82 @@ std::uint64_t queue_execute_command_lists(ChildProxy* self, std::uint64_t count_
return 0;
}

// ---- Queue slot 31: CopyPageMappingsBatchX ----
//
// Measured on the retail executable (static): game_scarlett_master.exe+0xf2cc7f calls the queue's slot 31
// (`call [vtable + 0xf8]`) with EDX the batch count, R8 the batches, R9 the base of its physical-page pool, the pool's
// size in 64 KiB pages as the fifth argument and 0 as the sixth; the result is not tested. It fills a batch at +0xf2ca74
// as {UINT64 destination; UINT32 range count; const range* ranges} (24 bytes) and a range as {UINT32 kind, start,
// count} (12 bytes). The kinds: 0 maps pool pages start, start + 1, ... at the destination; 1 maps pool page `start` at
// every destination page; 2 leaves the destination unmapped; 3 leaves it as it is. Each range moves the destination on
// by count pages, whatever its kind. The same call shape is in VIctory's queue (copy_page_mappings, "newer" layout,
// where every slot from 14 on is two later than in older titles). In a boot the pool size matched the title's own
// XMemAllocatePhysicalPages count exactly. The console maps GPU page tables; here each range becomes xmem's
// copy_page_mappings_fn, which maps the physical page behind each pool page at its destination, the aliasing two
// XMemMapPhysicalPages calls with one page give.
//
// Without xmem (tests that compile this proxy alone) the slot is refused as before. A range outside the pool, of an
// unknown kind, or that xmem refuses is skipped and logged once; the call then returns E_INVALIDARG.
struct ConsolePageRange {
std::uint32_t kind, start, count;
};
struct ConsolePageBatch {
std::uint64_t destination;
std::uint32_t count;
const ConsolePageRange* ranges;
};
static_assert(sizeof(ConsolePageRange) == 12 && sizeof(ConsolePageBatch) == 24, "the title's page batch layout");

constexpr std::uint64_t kConsolePageSize = 0x10000;

bool copy_console_page_range(const ConsolePageRange& range, std::uint64_t destination, std::uint64_t pool,
std::uint32_t pool_pages) {
void* const at = reinterpret_cast<void*>(destination);
const void* const from = reinterpret_cast<const void*>(pool + std::uint64_t{range.start} * kConsolePageSize);
switch (range.kind) {
case 0:
return std::uint64_t{range.start} + range.count <= pool_pages &&
rt::xmem::copy_page_mappings_fn(at, from, range.count, false);
case 1:
return range.start < pool_pages && rt::xmem::copy_page_mappings_fn(at, from, range.count, true);
case 2:
return rt::xmem::copy_page_mappings_fn(at, nullptr, range.count, false);
case 3:
return true;
default:
return false;
}
}

HRESULT queue_copy_page_mappings(std::uint64_t count_word, std::uint64_t batches_word, std::uint64_t pool,
std::uint64_t pool_pages_word) {
const auto count = static_cast<std::uint32_t>(count_word);
const auto pool_pages = static_cast<std::uint32_t>(pool_pages_word);
const auto* const batches = reinterpret_cast<const ConsolePageBatch*>(batches_word);
if (count && !batches) return E_INVALIDARG;
static std::atomic<bool> first_call{true}, first_refusal{true};
if (first_call.exchange(false, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: queue slot 31 (CopyPageMappingsBatchX) first call: %u batch(es), pool "
"0x%llx of %u pages\n", count, static_cast<unsigned long long>(pool), pool_pages);
bool all = true;
for (std::uint32_t b = 0; b < count; ++b) {
if (!batches[b].ranges) continue;
std::uint64_t at = batches[b].destination;
for (std::uint32_t r = 0; r < batches[b].count; ++r) {
const ConsolePageRange& range = batches[b].ranges[r];
if (!copy_console_page_range(range, at, pool, pool_pages)) {
all = false;
if (first_refusal.exchange(false, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: queue slot 31: range kind %u, pool pages %u+%u at 0x%llx not "
"mapped (pool of %u pages, error %lu); logged once\n", range.kind, range.start,
range.count, static_cast<unsigned long long>(at), pool_pages, GetLastError());
}
at += std::uint64_t{range.count} * kConsolePageSize;
}
}
return all ? S_OK : E_INVALIDARG;
}

// PresentX is at queue slot 30 in an older runtime layout and 32 in a newer one (public corroboration, XCL; see
// docs/d3d12_vulkan.md). 30 is the default and LEONIDA_XS_PRESENT_X_SLOT sets the other. SuspendX and ResumeX have no
// default: 0 means not routed, so they answer only when their variable is set.
Expand Down Expand Up @@ -1693,6 +1772,14 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
}
return hr_to_u64(S_OK);
}
// CopyPageMappingsBatchX (queue_copy_page_mappings), ahead of the presentation routes and the
// console-only S_OK below; without xmem linked it stays with the console-only answer.
if constexpr (Slot == kQueueCopyPageMappingsBatchX) {
if (Slot >= self->slot_count && rt::xmem::copy_page_mappings_fn) {
d3d12_trace::record(child_trace_name<K>(Slot));
return hr_to_u64(queue_copy_page_mappings(a1, a2, a3, a4));
}
}
// PresentX, SuspendX and ResumeX at their routed slots (xbox_queue_slot_routes). The default PresentX
// slot is the older layout's; the newer layout has it two slots later unless the variable names it. A
// presentation attached with set_presentation_handler answers first; without one PresentX goes to the
Expand Down
56 changes: 56 additions & 0 deletions runtime/shims/xmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,62 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co
return TRUE;
}

// rt::xmem::copy_page_mappings_fn (xmem.h), for the console queue's CopyPageMappingsBatchX: each destination page gets
// a view of the physical page mapped at its source page, so both addresses are one memory, as two XMemMapPhysicalPages
// calls with the same page make them. Every source page is looked up before any destination page changes, so a source
// range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped
// at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there.
bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) {
const uintptr_t to = reinterpret_cast<uintptr_t>(destination);
const uintptr_t from = reinterpret_cast<uintptr_t>(source);
constexpr uintptr_t kMax = (std::numeric_limits<uintptr_t>::max)();
if (!aligned_page_address(to) || !aligned_page_address(from) || pages > kMax / kPhysicalPageSize ||
to > kMax - pages * kPhysicalPageSize || (!repeat_first && from > kMax - pages * kPhysicalPageSize)) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
if (!placeholder_api().available()) {
SetLastError(ERROR_NOT_SUPPORTED);
return false;
}
const std::lock_guard lock(g_phys_mutex);
// The destination is usually a GPU-only reservation, which the D3D12 layer reads at its paired CPU view
// (title_gpu_views::to_cpu); that view gets the same pages, or it would stay reserved and never readable.
const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize);
const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr;
if (!source) {
for (uint64_t i = 0; i < pages; ++i) {
if (!unmap_page(to + i * kPhysicalPageSize)) return false;
if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false;
}
return true;
}
const Window* const window = find_window(to, pages * kPhysicalPageSize);
if (!window) {
SetLastError(ERROR_INVALID_ADDRESS);
return false;
}
std::vector<uint64_t> found(static_cast<size_t>(pages), 0);
for (uint64_t i = 0; i < pages; ++i) {
const uintptr_t src_va = from + (repeat_first ? 0 : i) * kPhysicalPageSize;
auto it = g_mapped.find(src_va);
if (it == g_mapped.end()) { // a GPU-only source whose pages were mapped at its CPU view
const uintptr_t cpu_src = leonida::title_gpu_views::to_cpu(src_va, kPhysicalPageSize);
if (cpu_src != src_va) it = g_mapped.find(cpu_src);
}
if (it != g_mapped.end()) found[static_cast<size_t>(i)] = it->second;
}
const DWORD protect = view_protect(*window);
const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0;
for (uint64_t i = 0; i < pages; ++i) {
const uint64_t page = found[static_cast<size_t>(i)];
if (!map_page(to + i * kPhysicalPageSize, page, protect)) return false;
if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect)) return false;
}
return true;
}
[[maybe_unused]] const bool g_copy_page_mappings_set =(rt::xmem::copy_page_mappings_fn = &copy_page_mappings, true);

// The title's write watching, on reservations it made with MEM_WRITE_WATCH. One xmem write-watched for the shadows
// (reserve_watched) is refused as the host refuses a region that is not write-watched ((UINT)-1,
// ERROR_INVALID_PARAMETER), as the console would: it isn't write-watched there, and a reset here would clear the bits
Expand Down
Loading
Loading