Skip to content
Closed
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
171 changes: 154 additions & 17 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <new>
#include <memory>
#include <mutex>
#include <new>
#include <utility>

namespace rt::d3d12_xs {
Expand Down Expand Up @@ -76,8 +77,20 @@ struct DeviceProxy {
std::atomic<std::uint32_t> frame_latency{1};
std::atomic<bool> frame_event_scheduled{false};
std::atomic<std::uint64_t> frame_next_token{1};
std::atomic<bool> frame_suspended{false};
std::atomic<std::uint64_t> present_calls{0};
std::mutex presentation_mutex;
void* presentation_ctx = nullptr;
const XboxPresentationOps* presentation_ops = nullptr;
};

bool get_device_presentation(DeviceProxy* self, void*& ctx, const XboxPresentationOps*& ops) {
const std::lock_guard lock(self->presentation_mutex);
ctx = self->presentation_ctx;
ops = self->presentation_ops;
return ctx != nullptr && ops != nullptr;
}

constexpr unsigned kHeapSlots = 11; // IUnknown + ID3D12Object + DeviceChild + Pageable + GetDesc + 2 handles

struct HeapProxy {
Expand Down Expand Up @@ -157,6 +170,7 @@ const char* slot_name(unsigned slot) {
case 78: return "Xbox SetFrameIntervalX";
case 79: return "Xbox ScheduleFrameEventX";
case 80: return "Xbox WaitFrameEventX";
case 81: return "Xbox GetFrameStatisticsX";
case 133: return "Xbox CreatePlacedResourceX (a buffer on the title's pages)";
default: return "forwarded";
}
Expand All @@ -178,11 +192,18 @@ void note_slot(unsigned slot) {
// that disagrees). A value outside that range is logged once and ignored. The table is read once, on the first
// generic slot call, so a test that wants other slots sets the variables before it creates a device.
struct XboxDeviceSlotRoutes {
unsigned frame_interval = 78; // SetFrameIntervalX(output, interval_us, latency, flags)
unsigned schedule_event = 79; // ScheduleFrameEventX(event, offset_us, reserved, flags)
unsigned wait_event = 80; // WaitFrameEventX(event, timeout_ms, reserved, flags, &token)
unsigned queue_x = 48; // CreateCommandQueueX(desc, riid, out)
unsigned list_x = 73; // CreateCommandListX(desc, allocator, initial_state, riid, out)
unsigned frame_interval = 78; // SetFrameIntervalX(output, interval_us, latency, flags)
unsigned schedule_event = 79; // ScheduleFrameEventX(event, offset_us, reserved, flags)
unsigned wait_event = 80; // WaitFrameEventX(event, timeout_ms, reserved, flags, &token)
unsigned frame_statistics = 81; // GetFrameStatisticsX(token, type_mask, &count, stats)
unsigned queue_x = 48; // CreateCommandQueueX(desc, riid, out)
unsigned list_x = 73; // CreateCommandListX(desc, allocator, initial_state, riid, out)
};

struct XboxQueueSlotRoutes {
unsigned suspend = 23; // SuspendX(flags)
unsigned resume = 24; // ResumeX()
unsigned present = 32; // PresentX(plane_count, planes, params)
};

unsigned env_device_slot(const char* name, unsigned fallback) {
Expand Down Expand Up @@ -210,20 +231,41 @@ const XboxDeviceSlotRoutes& xbox_device_slot_routes() {
const unsigned frame_interval = env_device_slot("LEONIDA_XS_FRAME_INTERVAL_SLOT", r.frame_interval);
const unsigned schedule_event = env_device_slot("LEONIDA_XS_SCHEDULE_EVENT_SLOT", r.schedule_event);
const unsigned wait_event = env_device_slot("LEONIDA_XS_WAIT_EVENT_SLOT", r.wait_event);
const unsigned frame_statistics = env_device_slot("LEONIDA_XS_FRAME_STATISTICS_SLOT", r.frame_statistics);
const unsigned queue_x = env_device_slot("LEONIDA_XS_QUEUE_X_SLOT", r.queue_x);
const unsigned list_x = env_device_slot("LEONIDA_XS_LIST_X_SLOT", r.list_x);
const bool moved = frame_interval != r.frame_interval || schedule_event != r.schedule_event ||
wait_event != r.wait_event || queue_x != r.queue_x || list_x != r.list_x;
wait_event != r.wait_event || frame_statistics != r.frame_statistics ||
queue_x != r.queue_x || list_x != r.list_x;
r.frame_interval = frame_interval;
r.schedule_event = schedule_event;
r.wait_event = wait_event;
r.frame_statistics = frame_statistics;
r.queue_x = queue_x;
r.list_x = list_x;
if (moved)
std::fprintf(stderr,
"[rt] d3d12_xs: Xbox device slots: SetFrameIntervalX=%u ScheduleFrameEventX=%u "
"WaitFrameEventX=%u CreateCommandQueueX=%u CreateCommandListX=%u\n",
r.frame_interval, r.schedule_event, r.wait_event, r.queue_x, r.list_x);
"WaitFrameEventX=%u GetFrameStatisticsX=%u CreateCommandQueueX=%u CreateCommandListX=%u\n",
r.frame_interval, r.schedule_event, r.wait_event, r.frame_statistics, r.queue_x, r.list_x);
return r;
}();
return routes;
}

const XboxQueueSlotRoutes& xbox_queue_slot_routes() {
static const XboxQueueSlotRoutes routes = [] {
XboxQueueSlotRoutes r;
const unsigned suspend = env_device_slot("LEONIDA_XS_SUSPEND_SLOT", r.suspend);
const unsigned resume = env_device_slot("LEONIDA_XS_RESUME_SLOT", r.resume);
const unsigned present = env_device_slot("LEONIDA_XS_PRESENT_SLOT", r.present);
const bool moved = suspend != r.suspend || resume != r.resume || present != r.present;
r.suspend = suspend;
r.resume = resume;
r.present = present;
if (moved)
std::fprintf(stderr, "[rt] d3d12_xs: Xbox queue slots: SuspendX=%u ResumeX=%u PresentX=%u\n",
r.suspend, r.resume, r.present);
return r;
}();
return routes;
Expand Down Expand Up @@ -1213,6 +1255,55 @@ 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) {
const XboxQueueSlotRoutes& qroutes = xbox_queue_slot_routes();
void* pres_ctx = nullptr;
const XboxPresentationOps* pres_ops = nullptr;
if (Slot == qroutes.suspend) {
log_xbox_route_once(Slot, "Queue::SuspendX", a1, a2, a3, a4, 0);
if (get_device_presentation(self->device, pres_ctx, pres_ops) && pres_ops->suspend_x) {
const HRESULT hr = pres_ops->suspend_x(pres_ctx, static_cast<UINT>(a1));
d3d12_trace::record("ID3D12CommandQueue::SuspendX", SUCCEEDED(hr) ? nullptr : "failed");
return hr_to_u64(hr);
}
self->device->frame_event_scheduled.store(false, std::memory_order_relaxed);
self->device->frame_suspended.store(true, std::memory_order_relaxed);
d3d12_trace::record("ID3D12CommandQueue::SuspendX");
return hr_to_u64(S_OK);
}
if (Slot == qroutes.resume) {
log_xbox_route_once(Slot, "Queue::ResumeX", a1, a2, a3, a4, 0);
if (get_device_presentation(self->device, pres_ctx, pres_ops) && pres_ops->resume_x) {
const HRESULT hr = pres_ops->resume_x(pres_ctx);
d3d12_trace::record("ID3D12CommandQueue::ResumeX", SUCCEEDED(hr) ? nullptr : "failed");
return hr_to_u64(hr);
}
self->device->frame_suspended.store(false, std::memory_order_relaxed);
d3d12_trace::record("ID3D12CommandQueue::ResumeX");
return hr_to_u64(S_OK);
}
if (Slot == qroutes.present) {
log_xbox_route_once(Slot, "Queue::PresentX", a1, a2, a3, a4, 0);
const auto plane_count = static_cast<std::uint32_t>(a1);
if (get_device_presentation(self->device, pres_ctx, pres_ops) && pres_ops->present_x) {
const HRESULT hr = pres_ops->present_x(pres_ctx, plane_count, reinterpret_cast<const void*>(a2),
reinterpret_cast<const void*>(a3));
d3d12_trace::record("ID3D12CommandQueue::PresentX", SUCCEEDED(hr) ? nullptr : "failed");
return hr_to_u64(hr);
}
if (plane_count > 2 || (plane_count > 0 && a2 == 0)) {
d3d12_trace::record("ID3D12CommandQueue::PresentX", "invalid_arg");
return hr_to_u64(E_INVALIDARG);
}
if (self->device->frame_suspended.load(std::memory_order_relaxed)) {
d3d12_trace::record("ID3D12CommandQueue::PresentX", "suspended");
return hr_to_u64(HRESULT_FROM_WIN32(ERROR_INVALID_STATE));
}
self->device->present_calls.fetch_add(1, std::memory_order_relaxed);
d3d12_trace::record("ID3D12CommandQueue::PresentX");
return hr_to_u64(S_OK);
}
}
if (Slot >= self->slot_count) {
bool expected = false;
if (ChildStats<K>::beyond_warned[Slot].compare_exchange_strong(expected, true,
Expand Down Expand Up @@ -1380,8 +1471,13 @@ HRESULT proxy_create_command_queue(DeviceProxy* self, const D3D12_COMMAND_QUEUE_
// 60 Hz), and latency is the allowed frames in flight. S_OK even for 0: a boot must not abort here, and nothing paces.
HRESULT proxy_set_frame_interval_x(DeviceProxy* self, std::uint64_t output, std::uint64_t interval_us,
std::uint64_t latency, std::uint64_t flags) {
(void)output;
(void)flags;
void* pres_ctx = nullptr;
const XboxPresentationOps* pres_ops = nullptr;
if (get_device_presentation(self, pres_ctx, pres_ops) && pres_ops->set_frame_interval) {
return pres_ops->set_frame_interval(pres_ctx, reinterpret_cast<IUnknown*>(output),
static_cast<UINT>(interval_us), static_cast<UINT>(latency),
static_cast<UINT>(flags));
}
self->frame_interval_us.store(static_cast<std::uint32_t>(interval_us), std::memory_order_relaxed);
self->frame_latency.store(static_cast<std::uint32_t>(latency ? latency : 1), std::memory_order_relaxed);
return S_OK;
Expand All @@ -1390,9 +1486,12 @@ HRESULT proxy_set_frame_interval_x(DeviceProxy* self, std::uint64_t output, std:
// Xbox device slot 79 (public HLE): ScheduleFrameEventX(event, offset_us, pEvent, flags); our pacer has only the origin.
HRESULT proxy_schedule_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t offset_us,
std::uint64_t flags) {
(void)event;
(void)offset_us;
(void)flags;
void* pres_ctx = nullptr;
const XboxPresentationOps* pres_ops = nullptr;
if (get_device_presentation(self, pres_ctx, pres_ops) && pres_ops->schedule_frame_event) {
return pres_ops->schedule_frame_event(pres_ctx, static_cast<UINT>(event), static_cast<UINT>(offset_us),
static_cast<UINT>(flags));
}
self->frame_event_scheduled.store(true, std::memory_order_relaxed);
return S_OK;
}
Expand All @@ -1402,17 +1501,39 @@ HRESULT proxy_schedule_frame_event_x(DeviceProxy* self, std::uint64_t event, std
// a zero wait cannot stall the title. A null token pointer is E_POINTER, as a _Out_ parameter.
HRESULT proxy_wait_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t timeout_ms,
std::uint64_t flags, std::uint64_t token_word) {
(void)event;
(void)timeout_ms;
(void)flags;
auto* token = reinterpret_cast<std::uint64_t*>(token_word);
void* pres_ctx = nullptr;
const XboxPresentationOps* pres_ops = nullptr;
if (get_device_presentation(self, pres_ctx, pres_ops) && pres_ops->wait_frame_event) {
return pres_ops->wait_frame_event(pres_ctx, static_cast<UINT>(event), static_cast<DWORD>(timeout_ms),
static_cast<UINT>(flags), token);
}
if (!token) return E_POINTER;
std::uint64_t value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed);
if (value == 0) value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed); // skip the null token
*token = value;
return S_OK;
}

// Xbox device slot 81 (measured in retail exe): GetFrameStatisticsX(token, type_mask, &count, stats).
// Before a window/presenter is attached, writes *count = 0 and returns S_FALSE (matches XboxPresentation).
HRESULT proxy_get_frame_statistics_x(DeviceProxy* self, std::uint64_t token, std::uint64_t type_mask,
std::uint64_t count_word, std::uint64_t stats_word) {
auto* count = reinterpret_cast<std::uint32_t*>(count_word);
void* pres_ctx = nullptr;
const XboxPresentationOps* pres_ops = nullptr;
if (get_device_presentation(self, pres_ctx, pres_ops) && pres_ops->get_frame_statistics) {
return pres_ops->get_frame_statistics(pres_ctx, token, static_cast<UINT>(type_mask), count,
reinterpret_cast<void*>(stats_word));
}
(void)token;
(void)type_mask;
(void)stats_word;
if (!count) return E_POINTER;
*count = 0;
return S_FALSE;
}

// Xbox device slot 73 (public HLE): CreateCommandListX(desc, allocator, initial_state, riid, out). The console call is
// not the PC six-argument form: the type is the first word of D3D12XBOX_COMMAND_LIST_DESC and there is no node mask
// (the HLE passes 0). The second desc word is logged, not interpreted: its public layout is not established.
Expand Down Expand Up @@ -2038,6 +2159,10 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
log_xbox_route_once(Slot, "WaitFrameEventX", a1, a2, a3, a4, a5);
return hr_to_u64(proxy_wait_frame_event_x(self, a1, a2, a4, a5));
}
if (Slot == routes.frame_statistics) {
log_xbox_route_once(Slot, "GetFrameStatisticsX", a1, a2, a3, a4, a5);
return hr_to_u64(proxy_get_frame_statistics_x(self, a1, a2, a3, a4));
}
if (Slot == routes.queue_x) {
log_xbox_route_once(Slot, "CreateCommandQueueX", a1, a2, a3, a4, a5);
return hr_to_u64(proxy_create_command_queue_xbox(self, reinterpret_cast<const std::uint32_t*>(a1),
Expand Down Expand Up @@ -2220,6 +2345,18 @@ bool set_device_adapter_provider(void* device, DxgiAdapterProvider provider) {
return true;
}

bool set_device_presentation(void* device, void* ctx, const XboxPresentationOps* ops) {
auto* proxy = static_cast<DeviceProxy*>(device_proxy_primary(device));
if (!proxy) {
std::fprintf(stderr, "[rt] d3d12_xs: set_device_presentation: %p is not a device proxy\n", device);
return false;
}
const std::lock_guard lock(proxy->presentation_mutex);
proxy->presentation_ctx = ctx;
proxy->presentation_ops = ops;
return true;
}

bool is_heap_proxy(const void* heap) {
return heap && *reinterpret_cast<void* const*>(heap) == reinterpret_cast<void*>(get_heap_vtable());
}
Expand Down
14 changes: 14 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ void* device_proxy_from_dxgi_device(void* dxgi_device);
// The device proxy itself, given the proxy or its DXGI pointer; null for anything else.
void* device_proxy_primary(void* device);

// Optional presentation backend (e.g. rt::d3d12::XboxPresentation) bound to a device proxy. When set, device
// slots 78-81 (SetFrameIntervalX, ScheduleFrameEventX, WaitFrameEventX, GetFrameStatisticsX) and queue slots
// 23/24/32 (SuspendX, ResumeX, PresentX) forward to these callbacks instead of the headless HLE fallback.
struct XboxPresentationOps {
HRESULT (*set_frame_interval)(void* ctx, IUnknown* output, UINT interval_us, UINT latency, UINT flags) = nullptr;
HRESULT (*schedule_frame_event)(void* ctx, UINT event, UINT offset_us, UINT flags) = nullptr;
HRESULT (*wait_frame_event)(void* ctx, UINT event, DWORD timeout_ms, UINT flags, std::uint64_t* token) = nullptr;
HRESULT (*get_frame_statistics)(void* ctx, std::uint64_t token, UINT type_mask, UINT* count, void* out) = nullptr;
HRESULT (*present_x)(void* ctx, UINT plane_count, const void* planes, const void* params) = nullptr;
HRESULT (*suspend_x)(void* ctx, UINT flags) = nullptr;
HRESULT (*resume_x)(void* ctx) = nullptr;
};
bool set_device_presentation(void* device, void* ctx, const XboxPresentationOps* ops);

// True when `heap` is a descriptor-heap proxy created by device slot 14.
bool is_heap_proxy(const void* heap);
ID3D12DescriptorHeap* heap_proxy_real_heap(const void* heap);
Expand Down
31 changes: 31 additions & 0 deletions runtime/shims/xbox_present.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "xbox_present.h"

#include "d3d12_trace.h"
#include "d3d12_xs_device.h"

#include "../src/vulkan_present.h"

Expand Down Expand Up @@ -314,4 +315,34 @@ HRESULT XboxPresentation::ResumeX() {
return S_OK;
}

bool bind_device_presentation(void* device, XboxPresentation* presentation) {
static const rt::d3d12_xs::XboxPresentationOps kOps{
[](void* ctx, IUnknown* output, UINT interval_us, UINT latency, UINT flags) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->SetFrameIntervalX(output, interval_us, latency, flags);
},
[](void* ctx, UINT event, UINT offset_us, UINT flags) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->ScheduleFrameEventX(event, offset_us, flags);
},
[](void* ctx, UINT event, DWORD timeout_ms, UINT flags, std::uint64_t* token) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->WaitFrameEventX(event, timeout_ms, flags, token);
},
[](void* ctx, std::uint64_t token, UINT type_mask, UINT* count, void* out) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->GetFrameStatisticsX(
token, type_mask, count, static_cast<leonida::FrameStatistic*>(out));
},
[](void* ctx, UINT plane_count, const void* planes, const void* params) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->PresentX(
plane_count, static_cast<const XboxPresentPlaneParameters*>(planes),
static_cast<const XboxPresentParameters*>(params));
},
[](void* ctx, UINT flags) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->SuspendX(flags);
},
[](void* ctx) -> HRESULT {
return static_cast<XboxPresentation*>(ctx)->ResumeX();
},
};
return rt::d3d12_xs::set_device_presentation(device, presentation, presentation ? &kOps : nullptr);
}

} // namespace rt::d3d12
4 changes: 4 additions & 0 deletions runtime/shims/xbox_present.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,8 @@ class XboxPresentation {

inline constexpr UINT xbox_frame_event_origin = leonida::frame_event_origin;

// Wires an XboxPresentation instance into a d3d12_xs device proxy (or its DXGI pointer) so Xbox device
// slots 78-81 and queue slots 23/24/32 drive this presentation object. Pass nullptr to unbind.
bool bind_device_presentation(void* device, XboxPresentation* presentation);

} // namespace rt::d3d12
Loading
Loading