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
31 changes: 31 additions & 0 deletions 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 Down Expand Up @@ -873,6 +877,7 @@ 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 {
Expand Down Expand Up @@ -1349,6 +1354,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 +1421,10 @@ 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 (Slot >= self->slot_count) {
if constexpr (K == ChildKind::queue) {
const XboxQueueSlotRoutes& qroutes = xbox_queue_slot_routes();
Expand Down Expand Up @@ -1438,6 +1461,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,6 +3204,11 @@ 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;
Expand Down
14 changes: 9 additions & 5 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 Down
72 changes: 59 additions & 13 deletions runtime/test/queue_proxy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ int check_slots(const Backend& b) {
}
}

// 10: ExecuteCommandLists passes the (real, not wrapped) command lists through.
// 10 and console slot 35: ExecuteCommandLists passes the (real, not wrapped) command lists through.
EXPECT(xs::queue_slot_overridden(35) && xs::queue_slot_overridden(14) && xs::queue_slot_overridden(15));
EXPECT(!xs::queue_slot_overridden(10) && !xs::queue_slot_overridden(34));
{
Ref<ID3D12CommandAllocator> allocator;
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
Expand All @@ -272,6 +274,11 @@ int check_slots(const Backend& b) {
EXPECT(qhits(10) == before + 1);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 6)));
if (wait_for(label, fence.p, 6)) return 1;
const std::uint64_t before35 = qhits(35);
EXPECT(raw_call(queue.p, 35, 1, reinterpret_cast<std::uint64_t>(lists)) == 0);
EXPECT(qhits(35) == before35 + 1 && xs::queue_slot_first_call_seen(35));
EXPECT(SUCCEEDED(queue->Signal(fence.p, 7)));
if (wait_for(label, fence.p, 7)) return 1;
if (b.vulkan) {
// The layer's own helpers find their queue behind the proxy.
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
Expand All @@ -293,17 +300,17 @@ int check_slots(const Backend& b) {

// 14/15: Signal and Wait take fence proxies; the real queue gets the real fences.
before = qhits(14);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 7)));
EXPECT(SUCCEEDED(queue->Signal(fence.p, 8)));
EXPECT(qhits(14) == before + 1);
if (wait_for(label, fence.p, 7)) return 1;
if (wait_for(label, fence.p, 8)) return 1;
before = qhits(15);
EXPECT(SUCCEEDED(queue->Wait(fence.p, 8))); // the queue waits until the CPU signals 8
EXPECT(SUCCEEDED(queue->Wait(fence.p, 9))); // the queue waits until the CPU signals 9
EXPECT(qhits(15) == before + 1);
EXPECT(SUCCEEDED(queue->Signal(second.p, 1)));
Sleep(50);
EXPECT(second->GetCompletedValue() == 0); // still held by the Wait
before = fhits(10);
EXPECT(SUCCEEDED(fence->Signal(8))); // fence 10, from the CPU
EXPECT(SUCCEEDED(fence->Signal(9))); // fence 10, from the CPU
EXPECT(fhits(10) == before + 1);
if (wait_for(label, second.p, 1)) return 1;

Expand Down Expand Up @@ -359,12 +366,12 @@ int check_slots(const Backend& b) {
EXPECT(fhits(7) == before + 1);
}
before = fhits(8);
EXPECT(fence->GetCompletedValue() == 8);
EXPECT(fence->GetCompletedValue() == 9);
EXPECT(fhits(8) == before + 1);
{
Event done;
before = fhits(9);
EXPECT(SUCCEEDED(fence->SetEventOnCompletion(8, done.h)) && done.set_within(5000));
EXPECT(SUCCEEDED(fence->SetEventOnCompletion(9, done.h)) && done.set_within(5000));
EXPECT(fhits(9) == before + 1);
}
if (fence_slots == 12) {
Expand All @@ -389,8 +396,9 @@ int check_slots(const Backend& b) {
}
}

// The refused tail: every slot from the end of the public chain to the end of the table.
// The refused tail: every unmodelled slot from the end of the public chain to the end of the table.
for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) {
if (xs::queue_slot_overridden(slot)) continue;
before = qhits(slot);
EXPECT(as_hr(call_slot(queue.p, slot)) == E_NOTIMPL);
EXPECT(qhits(slot) == before + 1 && xs::queue_slot_first_call_seen(slot));
Expand Down Expand Up @@ -1460,18 +1468,56 @@ int check_xbox_client(const Backend& b) {
EXPECT(same); // the GPU really ran the list the queue proxy submitted
}

// Device slot 50: EnqueueMakeResident with the objects and a fence proxy to signal.
EXPECT(SUCCEEDED(as_hr(raw(device, 50, D3D12_RESIDENCY_FLAG_NONE, 4, word(pageables), word(fence.p), 2))));
// Second copy with fresh bytes, submitted through console ExecuteCommandLists slot 35.
{
std::uint8_t* bytes = nullptr;
EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&bytes))));
for (UINT64 i = 0; i < kBytes; ++i) bytes[i] = static_cast<std::uint8_t>(i * 11 + 5);
upload->Unmap(0, nullptr);
}
EXPECT(SUCCEEDED(allocator->Reset()));
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
D3D12_RESOURCE_BARRIER to_dest{};
to_dest.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
to_dest.Transition.pResource = gpu.p;
to_dest.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
to_dest.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
to_dest.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
list->ResourceBarrier(1, &to_dest);
list->CopyBufferRegion(gpu.p, 0, upload.p, 0, kBytes);
D3D12_RESOURCE_BARRIER to_source = to_dest;
to_source.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
to_source.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
list->ResourceBarrier(1, &to_source);
list->CopyBufferRegion(readback.p, 0, gpu.p, 0, kBytes);
EXPECT(SUCCEEDED(list->Close()));
EXPECT(raw(queue.p, 35, 1, word(lists)) == 0);
EXPECT(SUCCEEDED(as_hr(raw(queue.p, 14, word(fence.p), 2))));
EXPECT(SUCCEEDED(as_hr(raw(fence.p, 9, 2, word(done.h)))) && done.set_within(5000));
EXPECT(raw(fence.p, 8) >= 2);
{
std::uint8_t* bytes = nullptr;
const D3D12_RANGE read_range{0, kBytes};
EXPECT(SUCCEEDED(readback->Map(0, &read_range, reinterpret_cast<void**>(&bytes))));
bool same = true;
for (UINT64 i = 0; i < kBytes; ++i) same = same && bytes[i] == static_cast<std::uint8_t>(i * 11 + 5);
const D3D12_RANGE nothing{0, 0};
readback->Unmap(0, &nothing);
EXPECT(same); // the GPU really ran the list submitted via queue slot 35
}

// Device slot 50: EnqueueMakeResident with the objects and a fence proxy to signal.
EXPECT(SUCCEEDED(as_hr(raw(device, 50, D3D12_RESIDENCY_FLAG_NONE, 4, word(pageables), word(fence.p), 3))));
EXPECT(SUCCEEDED(as_hr(raw(fence.p, 9, 3, word(done.h)))) && done.set_within(5000));

// Queue slot 15 holds the queue until the CPU (fence slot 10) signals; device slot 45 then sees both fences.
EXPECT(SUCCEEDED(as_hr(raw(queue.p, 15, word(fence.p), 3))));
EXPECT(SUCCEEDED(as_hr(raw(queue.p, 15, word(fence.p), 4))));
EXPECT(SUCCEEDED(as_hr(raw(queue.p, 14, word(second.p), 1))));
Sleep(50);
EXPECT(raw(second.p, 8) == 0);
EXPECT(SUCCEEDED(as_hr(raw(fence.p, 10, 3))));
EXPECT(SUCCEEDED(as_hr(raw(fence.p, 10, 4))));
ID3D12Fence* both[] = {fence.p, second.p};
const UINT64 values[] = {3, 1};
const UINT64 values[] = {4, 1};
EXPECT(SUCCEEDED(as_hr(raw(device, 45, word(both), word(values), 2, D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL,
word(done.h)))) &&
done.set_within(5000));
Expand Down
41 changes: 40 additions & 1 deletion runtime/test/xs_proxy_mock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,42 @@ int check_xbox_slots() {
#endif
}

// ---- 7b. Xbox queue ExecuteCommandLists variant (queue slot 35 -> PC slot 10) ----

int check_xbox_queue_slots() {
Mock* mock = nullptr;
void* device = make_device(mock, 9, true);
EXPECT(device);
void* queue = make_queue(device);
EXPECT(queue);
Mock* const queue_mock = reinterpret_cast<Mock*>(xs::queue_proxy_real_queue(queue));
xs::reset_device_proxy_stats();

EXPECT(xs::queue_slot_overridden(35) && xs::queue_slot_overridden(14) && xs::queue_slot_overridden(15));
EXPECT(!xs::queue_slot_overridden(10) && !xs::queue_slot_overridden(34) && !xs::queue_slot_overridden(36));

// Real objects, not numbers: the array is read to replace list proxies by their real lists, as slot 10's is.
Mock* const list_a = new_mock(Kind::command_list);
Mock* const list_b = new_mock(Kind::command_list);
void* lists[2] = {list_a, list_b};
EXPECT(raw(queue, 35, (0xdeadbeefull << 32) | 2ull, word(lists)) == 0);
EXPECT(xs::queue_slot_hits(35) == 1 && xs::queue_slot_first_call_seen(35));
EXPECT(xs::queue_slot_hits(10) == 0);
EXPECT(queue_mock->calls[35] == 0 && queue_mock->calls[10] == 1);
EXPECT(queue_mock->args[10][0] == 2);
EXPECT(queue_mock->array[10][0] == word(list_a) && queue_mock->array[10][1] == word(list_b));
EXPECT(lists[0] == list_a && lists[1] == list_b); // the caller's array is not written

EXPECT(raw(queue, 35, 0, 0) == 0);
EXPECT(xs::queue_slot_hits(35) == 2 && queue_mock->calls[35] == 0 && queue_mock->calls[10] == 2);
EXPECT(queue_mock->args[10][0] == 0 && queue_mock->args[10][1] == 0);

EXPECT(release(queue) == 0 && release(device) == 0 && mock->refs == 0);
std::printf("%s: queue slot 35 routes ExecuteCommandLists(count, lists) to PC slot 10 with UINT count masking\n",
g_label);
return 0;
}

// ---- 8. Environment overrides for the Xbox device slots (one process each, read once) ----

struct EnvPresenter {
Expand Down Expand Up @@ -1885,8 +1921,10 @@ int check_tail(bool want_guarded) {
// Signal (14) still reaches the real queue, with the real fence.
EXPECT(hr_of(raw(queue, 14, word(fence), 5)) == S_OK && queue_mock->calls[14] == 1 &&
queue_mock->args[14][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[14][1] == 5);
for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot)
for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) {
if (xs::queue_slot_overridden(slot)) continue;
EXPECT(hr_of(raw(queue, slot)) == E_NOTIMPL && queue_mock->calls[slot] == 0);
}
for (unsigned slot = 12; slot < xs::fence_proxy_table_size(); ++slot) EXPECT(hr_of(raw(fence, slot)) == E_NOTIMPL);
EXPECT(xs::queue_table_overruns() == 0 && xs::fence_table_overruns() == 0);
EXPECT(release(queue) == 0 && release(fence) == 0 && release(device) == 0 && mock->refs == 0);
Expand Down Expand Up @@ -2600,6 +2638,7 @@ int main(int argc, char** argv) {
check_render_target_layout() ||
check_committed_resource_leases() ||
check_slot74() ||
check_xbox_queue_slots() ||
check_presentation_wiring())
return 1;
std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news);
Expand Down
Loading