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
1 change: 1 addition & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,7 @@ to be recovered from call sites (or the NDA GDK headers, if a developer has acce
| Indirect argument type 105 | The retail title's startup creates five one-argument command signatures (device slot 41) with no root signature; one has type 105 and a 12-byte stride. Its two `ExecuteIndirect` sites, read statically, set the compute state first, as the `DISPATCH` sites do, and pass one command from the same argument buffer, on a second command list | `d3d12_xs_device.cpp` creates it as `DISPATCH` (`D3D12_DISPATCH_ARGUMENTS`). What the console does differently for it is **Open**. |
| 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`; `gta6rt` creates no `XboxPresentation` yet). Without one, slot 81 answers `S_FALSE` with a zero count (no statistics yet) and the queue slots stay refused. Moved with `LEONIDA_XS_FRAME_STATISTICS_SLOT` (3..127) and `LEONIDA_XS_PRESENT_X_SLOT` (default 30); `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). |
| 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. |
Expand Down
70 changes: 69 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,11 @@ static_assert(kQueueSlots * sizeof(void*) <= 4096 && kFenceSlots * sizeof(void*)
kAllocatorSlots * sizeof(void*) <= 4096,
"a table must fit in the first page of its reservation");

constexpr unsigned kQueueExecuteCommandLists = 10; // (UINT, ID3D12CommandList* const*)
constexpr unsigned kQueueSignal = 14, kQueueWait = 15; // (ID3D12Fence*, UINT64)
constexpr unsigned kQueueExecuteCommandListsX = 35; // console ExecuteCommandLists(count, lists) variant
static_assert(kQueueExecuteCommandLists < kQueuePublicSlots && kQueueExecuteCommandListsX < kQueueSlots,
"queue execution slots must stay within their respective tables");

struct ChildProxy {
void** vtable; // offset 0: the caller's ID3D12CommandQueue* / ID3D12Fence* points here
Expand All @@ -841,6 +845,9 @@ struct ChildProxy {
void** real_vtable;
unsigned slot_count; // public slots the real object implements; the rest of the table is refused
std::atomic<unsigned long> refs;
// Allocators only: callback and context recorded by console slot 9 (SetAllocatorCallbackX).
std::atomic<std::uintptr_t> allocator_callback{0};
std::atomic<std::uintptr_t> allocator_callback_context{0};
};

template <ChildKind K>
Expand Down Expand Up @@ -873,10 +880,12 @@ const char* child_public_name(unsigned slot) {
if (slot < 8) return object[slot];
if constexpr (K == ChildKind::queue) {
if (slot < kQueuePublicSlots) return queue[slot - 8];
if (slot == kQueueExecuteCommandListsX) return "ExecuteCommandListsX";
} else if constexpr (K == ChildKind::fence) {
if (slot < kFence1PublicSlots) return fence[slot - 8];
} else {
if (slot == 8) return "Reset";
if (slot == 9) return "SetAllocatorCallbackX";
}
return nullptr;
}
Expand Down Expand Up @@ -1249,8 +1258,11 @@ std::uint64_t WINAPI list_slot(ListProxy* self, std::uint64_t a1, std::uint64_t
std::uint64_t, std::uint64_t);
auto** const real_vtable = *reinterpret_cast<void***>(self->real);
UnwrappedArray heaps;
if constexpr (Slot == 10) {
if constexpr (pc == 10) {
// Reset(allocator, pso): console slot 10, and slot 139 as the retail title calls it. The title hands
// its allocator proxy; the real list takes the real one (and a wrapped pipeline state the same way).
a1 = unwrap_object(a1);
a2 = unwrap_object(a2);
self->pin_cursor_at_limit();
} else if constexpr (Slot == kConsoleExecuteBundle) {
a1 = unwrap_object(a1);
Expand Down Expand Up @@ -1349,6 +1361,20 @@ LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) {
return EXCEPTION_CONTINUE_SEARCH; // diagnostics only: the fault goes on to the title's handlers or the crash
}

std::uint64_t queue_execute_command_lists(ChildProxy* self, std::uint64_t count_word, std::uint64_t lists_word) {
// The array holds what the title got from the proxy: list proxies are replaced by the real lists in a copy, as
// for every other array of objects the real device or queue reads.
UnwrappedArray unwrapped_lists;
if (!unwrapped_lists.fill(lists_word, static_cast<UINT>(count_word))) return hr_to_u64(E_OUTOFMEMORY);
if (unwrapped_lists.data()) lists_word = unwrapped_lists.data();
// Pass zero-extended UINT in 64-bit RDX so both 32-bit PC implementations and 64-bit mock thunks see clean count.
using ExecuteFn = void(WINAPI*)(void*, std::uint64_t, ID3D12CommandList* const*);
reinterpret_cast<ExecuteFn>(self->real_vtable[kQueueExecuteCommandLists])(
self->real, static_cast<std::uint64_t>(static_cast<UINT>(count_word)),
reinterpret_cast<ID3D12CommandList* const*>(lists_word));
return 0;
}

// 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 @@ -1402,6 +1428,28 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
reinterpret_cast<void**>(a2)));
} else {
note_child_slot<K>(Slot);
if constexpr (K == ChildKind::queue && Slot == kQueueExecuteCommandListsX) {
d3d12_trace::record(child_trace_name<K>(Slot));
return queue_execute_command_lists(self, a1, a2);
}
if constexpr (K == ChildKind::allocator && Slot == 9) {
// Console-only ID3D12CommandAllocator::SetAllocatorCallbackX(callback, context, ...), observed past the
// 9-slot PC chain on the retail executable (call shape: function pointer, context, plus trailing words).
// The PC/Vulkan layer manages command-buffer backing directly without invoking a title allocator callback,
// so record the callback and context on the proxy and return S_OK while leaving slots 10+ refused.
self->allocator_callback.store(static_cast<std::uintptr_t>(a1), std::memory_order_relaxed);
self->allocator_callback_context.store(static_cast<std::uintptr_t>(a2), std::memory_order_relaxed);
static std::atomic<bool> logged{false};
if (!logged.exchange(true, std::memory_order_relaxed)) {
std::fprintf(stderr,
"[rt] d3d12_xs: allocator slot 9 (SetAllocatorCallbackX) recorded "
"(callback=0x%llx context=0x%llx a3=0x%llx a4=0x%llx)\n",
static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2),
static_cast<unsigned long long>(a3), static_cast<unsigned long long>(a4));
}
d3d12_trace::record(child_trace_name<K>(Slot));
return hr_to_u64(S_OK);
}
if (Slot >= self->slot_count) {
if constexpr (K == ChildKind::queue) {
const XboxQueueSlotRoutes& qroutes = xbox_queue_slot_routes();
Expand Down Expand Up @@ -1438,6 +1486,9 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
return hr_to_u64(E_NOTIMPL);
}
d3d12_trace::record(child_trace_name<K>(Slot)); // "called": the real object's result is not inspected
if constexpr (K == ChildKind::queue && Slot == kQueueExecuteCommandLists) {
return queue_execute_command_lists(self, a1, a2);
}
if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait)) a1 = unwrap_object(a1);
UnwrappedArray unwrapped_lists;
if constexpr (K == ChildKind::queue && Slot == 10) {
Expand Down Expand Up @@ -3178,11 +3229,28 @@ unsigned fence_proxy_slot_count(const void* fence) {

unsigned queue_proxy_table_size() { return kQueueSlots; }
unsigned fence_proxy_table_size() { return kFenceSlots; }

bool queue_slot_overridden(unsigned slot) {
return slot <= 2 || slot == 7 || slot == kQueueSignal || slot == kQueueWait ||
slot == kQueueExecuteCommandListsX;
}
unsigned allocator_proxy_table_size() { return kAllocatorSlots; }
unsigned allocator_proxy_slot_count(const void* allocator) {
return is_allocator_proxy(allocator) ? static_cast<const ChildProxy*>(allocator)->slot_count : 0;
}

std::uintptr_t allocator_proxy_callback(const void* allocator) {
return is_allocator_proxy(allocator)
? static_cast<const ChildProxy*>(allocator)->allocator_callback.load(std::memory_order_relaxed)
: 0;
}

std::uintptr_t allocator_proxy_callback_context(const void* allocator) {
return is_allocator_proxy(allocator)
? static_cast<const ChildProxy*>(allocator)->allocator_callback_context.load(std::memory_order_relaxed)
: 0;
}

unsigned long command_object_proxy_ref_count(const void* object) {
if (!is_queue_proxy(object) && !is_fence_proxy(object) && !is_allocator_proxy(object)) return 0;
return static_cast<const ChildProxy*>(object)->refs.load(std::memory_order_relaxed);
Expand Down
27 changes: 18 additions & 9 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@
//
// Command queues and fences (device slots 8, 75, 36, and 32 for a shared fence) come back wrapped in queue and fence
// proxies of the same kind: the public chain (19 queue slots, 11 or 12 fence slots in SDK 10.0.26100) is forwarded
// in PC order, and a slot past it, up to the end of the proxy's table (128 queue, 64 fence slots), is logged with its
// first four argument words and refused with E_NOTIMPL. A slot past the table faults on the vtable read inside a
// reserved, never-committed region, and the fault is logged with the slot. That is where the console's queue methods
// (PresentX, SuspendX, ResumeX) will show up; their slots are not known and are not guessed, and that the console
// keeps the public slots is an assumption (docs/d3d12_command_objects.md §5.2). Heap, queue and fence proxies are
// in PC order. Queue slot 35 is the console ExecuteCommandLists(count, lists) variant and forwards to PC slot 10;
// every other slot past the public chain, up to the end of the proxy's table (128 queue, 64 fence slots), is logged
// with its first four argument words and refused with E_NOTIMPL. A slot past the table faults on the vtable read inside a
// reserved, never-committed region, and the fault is logged with the slot. PresentX, SuspendX and ResumeX slots past
// the public chain remain refused until measured and implemented, and that the console keeps the public slots is an
// assumption (docs/d3d12_command_objects.md §5.2). Heap, queue and fence proxies are
// unwrapped wherever a device or queue method hands them to the real runtime: queue Signal/Wait (14/15), device
// CreateSharedHandle (31), MakeResident/Evict (34/35), SetEventOnMultipleFenceCompletion (45), SetResidencyPriority
// (46) and EnqueueMakeResident (50). Command lists are not wrapped, so ExecuteCommandLists passes them through and a
Expand Down Expand Up @@ -216,6 +217,9 @@ unsigned queue_proxy_slot_count(const void* queue);
unsigned fence_proxy_slot_count(const void* fence);
unsigned queue_proxy_table_size();
unsigned fence_proxy_table_size();
// True for queue slots handled by the proxy instead of forwarded verbatim past IUnknown: 0-2 (IUnknown),
// 7 (GetDevice), 14/15 (Signal/Wait unwrap fences), and 35 (Xbox ExecuteCommandLists variant -> PC slot 10).
bool queue_slot_overridden(unsigned slot);
// Live references on a queue, fence or allocator proxy (0 for anything else). Test helper.
unsigned long command_object_proxy_ref_count(const void* object);
// Per-slot call counters and first-call flags, shared by all queue proxies and all fence proxies.
Expand All @@ -230,10 +234,11 @@ std::uint64_t fence_table_overruns();
// or the fault logger failed).
bool command_object_tables_guarded();

// Command allocator proxies (device slot 9): the public chain (9 slots: the base chain and Reset) is forwarded, a
// slot past it up to the end of the table (64 slots) is logged and refused with E_NOTIMPL, and a read past the
// table faults in a guard region, as for queues and fences. The console allocator's extra methods are not
// measured and not guessed. Slot 9 wraps only an allocator that answers kLeonidaCommandAllocatorTag, Leonida's
// Command allocator proxies (device slot 9): the public chain (9 slots: the base chain and Reset) is forwarded,
// slot 9 (console SetAllocatorCallbackX(callback, context, ...)) records the callback and context on the proxy and
// returns S_OK without invoking the callback on the PC/Vulkan path, slots 10..63 are logged and refused with
// E_NOTIMPL, and a read past the table faults in a guard region, as for queues and fences. Slot 9 wraps only an
// allocator that answers kLeonidaCommandAllocatorTag, Leonida's
// own Vulkan-layer allocator: its command lists resolve the proxy (CreateCommandList, Reset). A PC runtime
// allocator goes back unchanged, because the PC runtime's lists would take a proxy for their own allocator; a
// command-list proxy that unwraps at Reset is what lets the host backend wrap allocators too.
Expand All @@ -245,6 +250,9 @@ bool is_allocator_proxy(const void* allocator);
ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator);
unsigned allocator_proxy_slot_count(const void* allocator);
unsigned allocator_proxy_table_size();
// Callback and context recorded by console allocator slot 9 (SetAllocatorCallbackX), or 0 if unset / not a proxy.
std::uintptr_t allocator_proxy_callback(const void* allocator);
std::uintptr_t allocator_proxy_callback_context(const void* allocator);
std::uint64_t allocator_slot_hits(unsigned slot);
bool allocator_slot_first_call_seen(unsigned slot);
std::uint64_t allocator_table_overruns();
Expand All @@ -267,6 +275,7 @@ inline constexpr unsigned console_to_pc_list_slot(unsigned slot) {
if (slot == 60) return 58; // EndEvent (no console SetMarker/BeginEvent)
if (slot == 61) return 59; // ExecuteIndirect (Package)
if (slot == 85) return 16; // CopyTextureRegionX -> CopyTextureRegion
if (slot == 139) return 10; // Reset(allocator, pso), as the retail title calls it (measured, Pipodi)
if (slot == 146) return 80; // D3D12_BARRIER_GROUP (Package; PC GCL7)
return kListNotForwarded;
}
Expand Down
31 changes: 24 additions & 7 deletions runtime/test/allocator_proxy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ using SlotFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t,
std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t);

// A call through slot `slot` with all words zero, the way a title calls a console method the PC interface lacks.
HRESULT call_slot(void* object, unsigned slot) {
// A call through slot `slot` with up to four explicit words (rest zero), the way a title calls a console method the PC
// interface lacks.
HRESULT call_slot(void* object, unsigned slot, std::uint64_t a1 = 0, std::uint64_t a2 = 0, std::uint64_t a3 = 0,
std::uint64_t a4 = 0) {
void** vtable = *reinterpret_cast<void***>(object);
return static_cast<HRESULT>(static_cast<std::uint32_t>(
reinterpret_cast<SlotFn>(vtable[slot])(object, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
reinterpret_cast<SlotFn>(vtable[slot])(object, a1, a2, a3, a4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
}

ULONG_PTR g_fault_address = 0;
Expand Down Expand Up @@ -182,10 +184,24 @@ int check_proxy(const Backend& b, ID3D12CommandAllocator* allocator) {
back->Release();
EXPECT(SUCCEEDED(allocator->SetPrivateDataInterface(key, nullptr)));

// The refused tail: every slot past the public chain, up to the table end, is E_NOTIMPL and counted.
// Console slot 9 (SetAllocatorCallbackX): records callback/context on the proxy and returns S_OK.
const unsigned table = xs::allocator_proxy_table_size();
EXPECT(table == 64);
for (unsigned slot = 9; slot < table; ++slot) {
{
EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0);
constexpr std::uintptr_t fake_cb = 0x11223344u;
constexpr std::uintptr_t fake_ctx = 0x55667788u;
const std::uint64_t before = xs::allocator_slot_hits(9);
EXPECT(call_slot(allocator, 9, fake_cb, fake_ctx, 0, 0x99aabbccu) == S_OK);
EXPECT(xs::allocator_slot_hits(9) == before + 1 && xs::allocator_slot_first_call_seen(9));
EXPECT(xs::allocator_proxy_callback(allocator) == fake_cb &&
xs::allocator_proxy_callback_context(allocator) == fake_ctx);
EXPECT(call_slot(allocator, 9, 0, 0) == S_OK);
EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0);
}

// The refused tail: every slot past slot 9, up to the table end, is E_NOTIMPL and counted.
for (unsigned slot = 10; slot < table; ++slot) {
const std::uint64_t before = xs::allocator_slot_hits(slot);
EXPECT(call_slot(allocator, slot) == E_NOTIMPL);
EXPECT(xs::allocator_slot_hits(slot) == before + 1 && xs::allocator_slot_first_call_seen(slot));
Expand All @@ -201,8 +217,9 @@ int check_proxy(const Backend& b, ID3D12CommandAllocator* allocator) {
EXPECT(g_fault_address == reinterpret_cast<ULONG_PTR>(&vtable[slot]));
EXPECT(xs::allocator_table_overruns() == before + 1);
}
std::printf("%s: allocator proxy: 9 public slots forwarded, slots 9-%u refused, the guard region names a read "
"past the table; QueryInterface/GetDevice/private data keep the proxy identity\n", label, table - 1);
std::printf("%s: allocator proxy: 9 public slots forwarded, slot 9 SetAllocatorCallbackX handled, slots 10-%u "
"refused, the guard region names a read past the table; QueryInterface/GetDevice/private data keep "
"the proxy identity\n", label, table - 1);
return 0;
}

Expand Down
Loading
Loading