diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f317958f12..32910eff44 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2575,7 +2575,7 @@ if(TARGET Vulkan::Headers) # Console Xbox CreateDescriptorHeap extended ABI (issue #313 comment 7107265058): the title passes address/size # after the PC desc and expects GetCPU/GPUDescriptorHandleForHeapStart() == address. Synthetic test; no title-memory # aliasing / ยง3.3 backing coverage. - add_executable(d3d12_console_descriptor_heap_test test/d3d12_console_descriptor_heap_test.cpp shims/d3d12_xs_device.cpp) + add_executable(d3d12_console_descriptor_heap_test test/d3d12_console_descriptor_heap_test.cpp) target_link_libraries(d3d12_console_descriptor_heap_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) target_compile_definitions(d3d12_console_descriptor_heap_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(d3d12_console_descriptor_heap_test PRIVATE /W4 /permissive- /utf-8) diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index 553f57f207..9f63210766 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp @@ -141,7 +141,9 @@ struct SHAPE_XMA_CONTEXT { uint32_t reserved1 : 6; uint32_t loopStartOffset : 26; uint32_t loopEndOffset : 6; - uint32_t reserved[10]; + // The bit-fields above are five dwords; eleven more reach the 64 bytes asserted below (ten left it at 60, and + // the assertion failed on every compiler). The named fields keep their offsets. + uint32_t reserved[11]; }; static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); @@ -162,7 +164,8 @@ struct SHAPE_PCM_CONTEXT { uint32_t reserved6 : 15; uint32_t format : 2; uint32_t reserved7 : 14; - uint32_t reserved[1]; + // Six dwords of bit-fields; two more reach the 32 bytes asserted below (one left it at 28). + uint32_t reserved[2]; }; static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK"); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index a6911d0f4e..46a5e5b0a9 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5008,6 +5008,7 @@ class Device final // standalone device gta6rt --run uses both come through here (found in review: registering only in // create_vulkan_device left the retail path without it). rt::d3d12_xs::set_placed_at_va_handler(&create_buffer_at_host_address); + rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); state_.info = info; queue_users_.resize(info.command_queues.size(), 0); compute_users_.resize(info.compute_queues.size(), 0); @@ -5965,6 +5966,18 @@ class Device final return d3d12_trace::result(method, hand_out(heap, riid, out)); } + // A heap whose CPU and GPU start is a console address (create_console_descriptor_heap, which checks `desc`). + HRESULT make_console_descriptor_heap(const D3D12_DESCRIPTOR_HEAP_DESC& desc, std::uint64_t console_address, + std::uint64_t console_size, REFIID riid, void** out) { + auto* heap = new DescriptorHeap(state_, desc, console_address, console_size); + heap->attach_device(identity()); + if (!heap->ok()) { + heap->Release(); + return E_OUTOFMEMORY; + } + return hand_out(heap, riid, out); + } + HRESULT STDMETHODCALLTYPE CreateQueryHeap(const D3D12_QUERY_HEAP_DESC* desc, REFIID riid, void** out) override { if (out) *out = nullptr; // The host runtime's order (compare_invalid_arguments, "query heap:"): the description first, refused with @@ -9998,13 +10011,7 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP auto* d = Device::from(real ? real : device); if (!d) return E_INVALIDARG; - auto* heap = new DescriptorHeap(d->state(), *desc, console_address, console_size); - heap->attach_device(d->identity()); - if (!heap->ok()) { - heap->Release(); - return E_OUTOFMEMORY; - } - return hand_out(heap, riid, out); + return d->make_console_descriptor_heap(*desc, console_address, console_size, riid, out); } std::filesystem::path configured_shader_directory(std::string* error, const std::filesystem::path& config) { diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..02a6e1640f 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1579,6 +1579,9 @@ static bool readable_range(const void* ptr, std::size_t bytes) { return true; } +// The Vulkan layer's maker of a heap at a console address (set_console_descriptor_heap_handler), or null. +std::atomic g_console_descriptor_heap{nullptr}; + HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid, void** out) { note_slot(14); @@ -1599,13 +1602,18 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H std::uint64_t size = 0; std::memcpy(&address, tail, sizeof(address)); std::memcpy(&size, bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC) + sizeof(address), sizeof(size)); - // Console-extended slot 14 (issue #313): address/size follow the PC desc. Only take this path when a - // non-zero console VA is present; normal PC 16-byte descs never enter it. - if (address != 0) { - console_extended = true; - hr = rt::d3d12::create_console_descriptor_heap( - self->real, desc, static_cast(address), size, - __uuidof(ID3D12DescriptorHeap), &created); + // Console-extended slot 14 (issue #313): address/size follow the PC desc. A PC caller passes 16 bytes, and + // the 16 after them are whatever its stack holds, so a non-zero address alone does not tell the two + // apart (d3d12_descriptor_test entered this path with stack bytes as the address and faulted on the + // first descriptor write). The size must also be exactly the heap's descriptors. + // ASSUMPTION: the console passes size = NumDescriptors * increment; not measured on a retail call here. + const std::uint64_t increment = self->real->GetDescriptorHandleIncrementSize(desc->Type); + if (address != 0 && increment != 0 && size == std::uint64_t(desc->NumDescriptors) * increment) { + if (const ConsoleDescriptorHeapHandler handler = + g_console_descriptor_heap.load(std::memory_order_acquire)) { + console_extended = true; + hr = handler(self->real, desc, address, size, __uuidof(ID3D12DescriptorHeap), &created); + } } } } @@ -2037,6 +2045,10 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler) { g_placed_at_va.store(handler, std::memory_order_release); } +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) { + g_console_descriptor_heap.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); } diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..a120aaacd7 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -100,6 +100,16 @@ using PlacedAtVaHandler = HRESULT (*)(ID3D12Device* real, void* va, const D3D12_ std::uint32_t layout, REFIID riid, void** out); void set_placed_at_va_handler(PlacedAtVaHandler handler); +// Device slot 14 with the console-extended description (a PC D3D12_DESCRIPTOR_HEAP_DESC followed by an address and a +// size) makes the heap through this handler, which the Vulkan layer registers +// (rt::d3d12::create_console_descriptor_heap). The proxy does not name that layer itself: this file is also built +// without it (the mock and COM tests). With none registered the slot creates a plain PC heap, as it does for a +// description with no console address. +using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12_DESCRIPTOR_HEAP_DESC* desc, + std::uint64_t console_address, std::uint64_t console_size, + REFIID riid, void** out); +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler 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 diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index 5f694b5a2a..3a6335d3cb 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -24,6 +24,7 @@ struct Ref { return &p; } T* get() const { return p; } + T* operator->() const { return p; } }; bool check_console_addressed_heap(ID3D12Device* device) { @@ -65,7 +66,10 @@ bool check_console_addressed_heap(ID3D12Device* device) { bool check_xs_proxy_slot14(ID3D12Device* real_device) { void* proxy_raw = nullptr; - if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(&proxy_raw))) || !proxy_raw) { + // The proxy takes over one reference to the device on success (d3d12_xs_device.h); the caller keeps its own. + real_device->AddRef(); + if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, __uuidof(ID3D12Device), &proxy_raw)) || !proxy_raw) { + real_device->Release(); std::fprintf(stderr, "create_device_proxy failed\n"); return false; } @@ -90,11 +94,27 @@ bool check_xs_proxy_slot14(ID3D12Device* real_device) { static_cast(hr)); return false; } - const auto cpu = heap->GetCPUDescriptorHandleForHeapStart(); - if (cpu.ptr != static_cast(console.address)) { + // The heap that comes back is the Xbox-ABI heap proxy: its slot 9 returns the 8-byte handle in RAX, not through + // the hidden pointer a PC GetCPUDescriptorHandleForHeapStart call passes. + using XboxHeapHandleFn = std::uint64_t(WINAPI*)(void* heap); + void** const heap_vtable = *reinterpret_cast(heap.get()); + const std::uint64_t cpu = reinterpret_cast(heap_vtable[9])(heap.get()); + if (cpu != console.address) { std::fprintf(stderr, "proxy CPU heap start mismatch: got 0x%llx want 0x%llx\n", - static_cast(cpu.ptr), - static_cast(console.address)); + static_cast(cpu), static_cast(console.address)); + return false; + } + + // A PC description followed by bytes that are not a console address and size makes a plain heap. + console.size = 3; + Ref plain; + if (FAILED(proxy->CreateDescriptorHeap(&console.desc, IID_PPV_ARGS(plain.put()))) || !plain.get()) { + std::fprintf(stderr, "proxy CreateDescriptorHeap(plain) failed\n"); + return false; + } + void** const plain_vtable = *reinterpret_cast(plain.get()); + if (reinterpret_cast(plain_vtable[9])(plain.get()) == console.address) { + std::fprintf(stderr, "a description whose tail is not address/size took the console path\n"); return false; } return true; @@ -123,9 +143,15 @@ bool check_normal_heap_unchanged(ID3D12Device* device) { int main() { vktest::Device vk; - const auto status = vk.initialize(); - if (status == vktest::DeviceStatus::unavailable) return 77; - if (status != vktest::DeviceStatus::ready) return 1; + vk.request_buffer_device_address = true; + if (!vk.create()) { + std::puts("SKIP: no suitable Vulkan 1.3 (or 1.2 + KHR) device"); + return 77; + } + if (!vk.buffer_device_address) { + std::puts("SKIP: bufferDeviceAddress is required"); + return 77; + } Ref device; rt::d3d12::VulkanDeviceInfo info{}; @@ -135,6 +161,9 @@ int main() { info.device = vk.device; info.queue = vk.queue; info.queue_family = vk.family; + info.buffer_device_address = vk.buffer_device_address; + info.features = vk.enabled; + info.subgroup = vk.subgroup; if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(device.put()))) || !device.get()) { std::fprintf(stderr, "create_vulkan_device failed\n"); return 1;