Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4b76715
d3d12_xs: extend the device proxy table to 192 slots so slots past th…
Pipodi Nov 23, 2026
4eeb61b
d3d12: OpenExistingHeapFromAddress backs a heap with the caller's mem…
tibzejoker Nov 23, 2026
4d3d1e4
d3d12: a write-combined range opens as a WRITE_COMBINE existing heap,…
tibzejoker Nov 23, 2026
a4551a1
Merge branch 'pr-6953' into slot133-host-va
tibzejoker Nov 23, 2026
9545ba8
d3d12_xs: device slot 133 (CreatePlacedResourceX) makes a buffer on t…
tibzejoker Nov 23, 2026
a32330f
d3d12_xs: slot 133 review fixes: integer flag mask for clang-cl, hand…
tibzejoker Nov 23, 2026
626beeb
Merge remote-tracking branch 'origin/main' into slot133-host-va
tibzejoker Nov 23, 2026
2b584ec
Reset third_party/rpf9-explorer to main's commit (stray gitlink bump,…
tibzejoker Nov 23, 2026
588ef0b
d3d12: slot 133 falls back to an ordinary buffer when the driver will…
tibzejoker Nov 23, 2026
5e26043
Reset third_party/rpf9-explorer to main's commit again (the fallback …
tibzejoker Nov 23, 2026
310773a
d3d12: slot 133 review fixes: no write-combined import, committed-ran…
tibzejoker Nov 23, 2026
c3fe825
d3d12_existing_heap_test: don't compare the host's heap Alignment, wh…
tibzejoker Nov 23, 2026
a07c8c0
xmem: commit physical pages on first touch, not when they are allocated
Cattn Nov 23, 2026
abd85df
xmem: rt::xmem::commit_physical_pages commits pages by page number
Cattn Nov 23, 2026
228cb9e
d3d12: slot 133 imports from the page that holds the address and bind…
tibzejoker Nov 23, 2026
75c2b87
xmem: take the fault lock before reading or changing a mapped page's …
Cattn Nov 23, 2026
4fb24d2
d3d12: a texture at slot 133 gets an ordinary committed texture inste…
tibzejoker Nov 23, 2026
af75a43
xmem: judge a fault on a committed page by the protection at the faul…
Cattn Nov 23, 2026
4336b7a
Merge remote-tracking branch 'origin/main' into slot133-host-va
tibzejoker Nov 23, 2026
6545c9c
tests: check reported cache protection in existing-heap fixture
venetanji Nov 23, 2026
e85ebec
xmem_test: build with /EHa under clang-cl
Cattn Nov 23, 2026
a3eae95
d3d12: slot 133's third argument is a barrier layout; a placed textur…
tibzejoker Nov 23, 2026
df62408
d3d12_existing_heap_test: a driver may refuse the section-view import…
tibzejoker Nov 23, 2026
1d4d295
d3d12_existing_heap_test: the cache-modifier loop's buffer no longer …
tibzejoker Nov 23, 2026
3644c72
d3d12: slot-133 texture layouts are validated; an import reaching the…
tibzejoker Nov 23, 2026
664aff2
Merge #7556 (xmem: commit physical pages on first touch) for the stack
Raavve Nov 23, 2026
cb29a2b
d3d12_xs: slot 133 commits the buffer's xmem pages before the host-po…
Raavve Nov 23, 2026
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
11 changes: 10 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
)
target_include_directories(xmem_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(xmem_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(xmem_test PRIVATE /W4 /permissive- /utf-8)
target_compile_options(xmem_test PRIVATE /W4 /permissive- /utf-8 $<$<CXX_COMPILER_ID:Clang>:/EHa>)
target_link_options(xmem_test PRIVATE /OPT:NOREF)
target_link_libraries(xmem_test PRIVATE psapi)

Expand Down Expand Up @@ -1727,6 +1727,15 @@ if(TARGET Vulkan::Headers)
target_compile_options(d3d12_existing_heap_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_existing_heap COMMAND d3d12_existing_heap_test)
set_tests_properties(d3d12_existing_heap PROPERTIES SKIP_RETURN_CODE 77)
# Slot 133 on untouched xmem physical pages: committed through the pages hook, then imported. xmem.cpp and
# registry.cpp are compiled in, as xmem_test does, to reach the registered XMem* exports.
add_executable(d3d12_xmem_placed_test test/d3d12_xmem_placed_test.cpp shims/xmem.cpp shims/registry.cpp)
target_link_libraries(d3d12_xmem_placed_test PRIVATE gta6rt_d3d12_com d3d12 dxgi psapi)
target_compile_definitions(d3d12_xmem_placed_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_xmem_placed_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(d3d12_xmem_placed_test PRIVATE /OPT:NOREF)
add_test(NAME d3d12_xmem_placed COMMAND d3d12_xmem_placed_test)
set_tests_properties(d3d12_xmem_placed PROPERTIES SKIP_RETURN_CODE 77)

# Fences and timing (D3D-30): multiple-fence waits, clock calibration, adapter LUID, custom heap properties.
add_executable(d3d12_fence_timing_test test/d3d12_fence_timing_test.cpp)
Expand Down
11 changes: 11 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ struct VulkanDeviceInfo {
// colours fall back to transparent black.
bool custom_border_color = false;
bool custom_border_color_without_format = false;
// VK_EXT_external_memory_host is enabled on `device` when this is non-zero (its
// minImportedHostPointerAlignment): OpenExistingHeapFromAddress backs a heap with the caller's own committed
// memory. Zero: existing heaps stay unsupported.
VkDeviceSize host_pointer_alignment = 0;
};

// A Vulkan-backed ID3D12Device (PC ABI, up to ID3D12Device11) on caller-owned Vulkan objects, which must outlive
Expand Down Expand Up @@ -218,6 +222,13 @@ VkBuffer vulkan_buffer(ID3D12Resource* resource);
const VulkanDeviceInfo* vulkan_device_info(ID3D12Device* device);
// True when `device` has VK_KHR_acceleration_structure + VK_KHR_ray_query enabled.
bool device_supports_ray_tracing(ID3D12Device* device);

// Xbox device slot 133 (the console's CreatePlacedResourceX): a buffer on the caller's own pages at `address`, which
// becomes its console VA. The pages are imported with VK_EXT_external_memory_host, so what the CPU writes there is
// what the GPU reads. A buffer description only; E_NOTIMPL on a device that is not the Vulkan layer, without the
// extension, or for a texture.
HRESULT create_buffer_at_host_address(ID3D12Device* device, void* address, const D3D12_RESOURCE_DESC& desc,
std::uint32_t layout, REFIID riid, void** out);
// The VkAccelerationStructureKHR registered for `va` on `device`, or VK_NULL_HANDLE. For tests.
VkAccelerationStructureKHR vulkan_acceleration_structure(ID3D12Device* device, D3D12_GPU_VIRTUAL_ADDRESS va);

Expand Down
358 changes: 350 additions & 8 deletions runtime/shims/d3d12_vulkan_device.cpp

Large diffs are not rendered by default.

72 changes: 70 additions & 2 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
namespace rt::d3d12_xs {
namespace {

constexpr unsigned kMaxSlots = 128;
// The retail title calls device slots well past ID3D12Device14 (slot 133 at least); every slot past the PC device's
// vtable goes through the generic thunk and is refused with E_NOTIMPL, so the table must outlast the highest one.
constexpr unsigned kMaxSlots = 192;
static_assert(kMaxSlots > 133, "the proxy table must cover the slots the retail title calls (up to 133)");
static_assert(kMaxSlots > 83, "the proxy table must cover ID3D12Device14 (83 slots)");
static_assert(sizeof(void*) == 8, "the Xbox-ABI proxy is x64-only");
static_assert(sizeof(D3D12_CPU_DESCRIPTOR_HANDLE) == 8,
Expand Down Expand Up @@ -147,6 +150,7 @@ const char* slot_name(unsigned slot) {
case 78: return "Xbox SetFrameIntervalX";
case 79: return "Xbox ScheduleFrameEventX";
case 80: return "Xbox WaitFrameEventX";
case 133: return "Xbox CreatePlacedResourceX (a buffer on the title's pages)";
default: return "forwarded";
}
}
Expand Down Expand Up @@ -1480,6 +1484,57 @@ HRESULT proxy_create_resource(DeviceProxy* self, const D3D12_HEAP_PROPERTIES* he
*riid, out);
}

// Device slot 133 is the console's CreatePlacedResourceX: (va, desc, state, clear, riid, out), measured on the retail
// executable (docs/retail_boot_chain.md, section 3.4). `va` is the start of a physical-page range the title has just
// mapped, `desc` a 56-byte description with Xbox bits above the PC resource flags and an Xbox layout word, and
// `state` is 0xFFFFFFFF, which is no PC state. The resource is a buffer on those pages, made by the handler the
// device behind the proxy registered (set_placed_at_va_handler), with the PC flags kept and the layout made
// ROW_MAJOR, as slot 76 does.
std::atomic<PlacedAtVaHandler> g_placed_at_va{nullptr};
std::atomic<PlacedAtVaPagesHook> g_placed_at_va_pages{nullptr};

HRESULT proxy_create_placed_resource_x(DeviceProxy* self, void* va, const D3D12_RESOURCE_DESC* xbox_desc,
std::uint32_t state, const IID* riid, void** out) {
note_slot(133);
if (out) *out = nullptr;
// Without a handler (a PC device behind the proxy, or none registered) the slot refuses like any slot past the
// PC vtable, whatever its arguments.
const PlacedAtVaHandler handler = g_placed_at_va.load(std::memory_order_acquire);
if (!handler) return E_NOTIMPL;
if (!out || !xbox_desc || !riid) return E_POINTER;
// An integer mask: a D3D12_RESOURCE_FLAGS constant past the SDK's last enumerator is not a constant expression
// for clang-cl with SDK 10.0.26100 (found in review). The cast is applied to the masked value at run time.
// The PC flags up to RAYTRACING_ACCELERATION_STRUCTURE (0x100) and USE_TIGHT_ALIGNMENT (0x400); 0x200 is
// dropped, as committed_resource_refusal says the host refuses it (found in review).
constexpr std::uint32_t kPcResourceFlags = 0x5ff;
D3D12_RESOURCE_DESC pc_desc = *xbox_desc;
const auto xbox_flags = static_cast<std::uint32_t>(pc_desc.Flags);
pc_desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(xbox_flags & kPcResourceFlags);
// The Xbox layout word (0x101 tiled, 0x1 row-major buffer) is no PC layout: ROW_MAJOR for a buffer and UNKNOWN
// for everything else, as slot 76 does.
pc_desc.Layout = pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR
: D3D12_TEXTURE_LAYOUT_UNKNOWN;
static std::atomic<bool> logged{false};
if (!logged.exchange(true, std::memory_order_relaxed)) {
std::fprintf(stderr,
"[rt] d3d12_xs: slot 133 CreatePlacedResourceX: va=%p dimension=%d width=%llu xbox flags=0x%x "
"state=0x%x -> a buffer on the title's pages\n",
va, static_cast<int>(pc_desc.Dimension), static_cast<unsigned long long>(pc_desc.Width),
xbox_flags, state);
}
// The buffer's pages are committed before the handler walks them (set_placed_at_va_pages_hook).
const PlacedAtVaPagesHook pages = g_placed_at_va_pages.load(std::memory_order_acquire);
if (pages && pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && pc_desc.Width &&
!pages(va, static_cast<std::size_t>(pc_desc.Width))) {
static std::atomic<bool> refused{false};
if (!refused.exchange(true, std::memory_order_relaxed)) {
std::fprintf(stderr, "[rt] d3d12_xs: slot 133: committing the pages of va=%p width=%llu failed (error %lu)\n",
va, static_cast<unsigned long long>(pc_desc.Width), GetLastError());
}
}
return handler(self->real, va, pc_desc, state, *riid, out);
}

template <unsigned Slot>
std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3,
std::uint64_t a4, std::uint64_t a5, std::uint64_t a6, std::uint64_t a7,
Expand Down Expand Up @@ -1521,6 +1576,11 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
return hr_to_u64(proxy_create_compute_pipeline_from_native_code(
self, reinterpret_cast<const void*>(a1), reinterpret_cast<const IID*>(a2),
reinterpret_cast<void**>(a3)));
} else if constexpr (Slot == 133) {
return hr_to_u64(proxy_create_placed_resource_x(self, reinterpret_cast<void*>(a1),
reinterpret_cast<const D3D12_RESOURCE_DESC*>(a2),
static_cast<std::uint32_t>(a3), reinterpret_cast<const IID*>(a5),
reinterpret_cast<void**>(a6)));
} else if constexpr (Slot == 76) {
return hr_to_u64(proxy_create_resource(
self, reinterpret_cast<const D3D12_HEAP_PROPERTIES*>(a1),
Expand Down Expand Up @@ -1698,6 +1758,14 @@ unsigned device_proxy_slot_count(const void* device) {
return static_cast<const DeviceProxy*>(device)->slot_count;
}

void set_placed_at_va_handler(PlacedAtVaHandler handler) {
g_placed_at_va.store(handler, std::memory_order_release);
}

void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) {
g_placed_at_va_pages.store(hook, std::memory_order_release);
}

ID3D12Device* device_proxy_real_device(const void* device) {
if (!is_device_proxy(device)) return nullptr;
return static_cast<const DeviceProxy*>(device)->real;
Expand All @@ -1706,7 +1774,7 @@ ID3D12Device* device_proxy_real_device(const void* device) {
bool device_slot_overridden(unsigned slot) {
if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 31 || slot == 32 || slot == 34 || slot == 35 ||
slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 ||
slot == 75 || slot == 76)
slot == 75 || slot == 76 || slot == 133)
return true;
const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes();
return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event ||
Expand Down
19 changes: 19 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

#include <d3d12.h>

#include <cstddef>
#include <cstdint>

namespace rt::d3d12_xs {
Expand Down Expand Up @@ -89,6 +90,24 @@ unsigned device_proxy_slot_count(const void* device);
// The PC device behind a proxy. Null when `device` is not a proxy.
ID3D12Device* device_proxy_real_device(const void* device);

// Device slot 133 (the console's CreatePlacedResourceX) makes a buffer on the title's pages at `va`. The proxy does
// not create it itself: the device behind it does, through this handler, which the Vulkan layer registers
// (rt::d3d12::create_buffer_at_host_address). With none registered, or a PC device behind the proxy, the slot
// answers E_NOTIMPL.
// `layout` is the title's third argument, a D3D12_BARRIER_LAYOUT (0xFFFFFFFF = UNDEFINED), measured on a retail
// boot: 0x6 SHADER_RESOURCE, 0x3 UNORDERED_ACCESS, as PC CreatePlacedResource2 takes it.
using PlacedAtVaHandler = HRESULT (*)(ID3D12Device* real, void* va, const D3D12_RESOURCE_DESC& desc,
std::uint32_t layout, REFIID riid, void** out);
void set_placed_at_va_handler(PlacedAtVaHandler handler);

// Called by slot 133 for a buffer, before the handler, with the buffer's range [va, va + width). gta6rt registers
// rt::xmem::commit_physical_range: xmem commits physical pages on first touch, so a range the title has mapped but
// not written yet is still reserved, and the handler's import refuses reserved pages (it would answer with its
// fallback buffer, which the title's writes never reach). A false return is logged once and the handler is still
// called. With none registered nothing is done.
using PlacedAtVaPagesHook = bool (*)(const void* va, std::size_t bytes);
void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook);

// True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue
// proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and
// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67 and 76. Slot 10 is intentionally false: the
Expand Down
Loading
Loading