From 2ec2ca4505e618b2f194900cf1dd918ce309d019 Mon Sep 17 00:00:00 2001 From: TanmaySatyam Date: Wed, 25 Nov 2026 14:36:41 +0530 Subject: [PATCH] d3d12/xs: guard 48-bit host-import VAs and fingerprint-sync custom descriptor heaps Rebased onto upstream main on top of #939 (LEONIDA_DESC_MIRROR) and the below-arena guard: - d3d12_com.h / d3d12_vulkan_device.cpp: add rt::d3d12::host_import_va_in_vsharp_range() so positive offsets >= 2^48 above heap_base also fall back to the arena shadow while preserving LEONIDA_IMPORT_BELOW_ARENA=1 for below-arena imports; add deterministic 48-bit boundary + BDA arena tests in d3d12_existing_heap_test.cpp. - d3d12_xs_device.cpp: layer two missing sync paths directly on top of LEONIDA_DESC_MIRROR and local_accessible(): 1) local_sync_source() before CopyDescriptors / CopyDescriptorsSimple so direct CPU writes to custom-backed source heaps reach destinations immediately on the CPU timeline. 2) Per-slot 64-bit fingerprint sync (local_sync_shader_visible_heaps) before queue ExecuteCommandLists (slots 10/35) for shader-visible custom-backed heaps, picking up pre-filled slots on first submit and title-byte-copied descriptors outside 64-slot root-table binds without copying unchanged slots. - d3d12_console_descriptor_heap_test.cpp: add unit coverage for pre-filled first-submit sync, post-creation direct writes, and CopyDescriptorsSimple / CopyDescriptors from custom-backed heaps. --- runtime/shims/d3d12_com.h | 7 + runtime/shims/d3d12_vulkan_device.cpp | 8 +- runtime/shims/d3d12_xs_device.cpp | 121 +++++++++++++- .../d3d12_console_descriptor_heap_test.cpp | 148 ++++++++++++++++++ runtime/test/d3d12_existing_heap_test.cpp | 65 ++++++++ 5 files changed, 339 insertions(+), 10 deletions(-) diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index c55229a82c..4e9933c949 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -535,6 +535,13 @@ bool bound_descriptor_heap_bases(ID3D12GraphicsCommandList* list, D3D12_GPU_VIRT // The size of every descriptor slot (GetDescriptorHandleIncrementSize), for handle ranges. inline constexpr UINT descriptor_size = 32; +// True when a host-imported buffer's Vulkan device address can be encoded relative to `arena_address` in an RDNA2 +// 48-bit V# buffer descriptor (offset in [0, 2^48)). Unchecked when either address is 0 (no arena or no BDA). +inline constexpr bool host_import_va_in_vsharp_range(VkDeviceAddress arena_address, VkDeviceAddress buffer_va) { + if (arena_address == 0 || buffer_va == 0) return true; + return buffer_va >= arena_address && ((buffer_va - arena_address) >> 48) == 0; +} + // ExecuteCommandLists returns void; this is what the last call on the queue did. leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 4a892c21af..c784c8e13a 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -4936,12 +4936,14 @@ class Resource final wchar_t value[2]{}; return GetEnvironmentVariableW(L"LEONIDA_IMPORT_BELOW_ARENA", value, 2) == 1 && value[0] == L'1'; }(); - if (buffer_va_ && buffer_va_ < device_.arena.address && !keep_low_imports) { + const bool below_arena = buffer_va_ && device_.arena.address && buffer_va_ < device_.arena.address; + const bool outside_vsharp = !rt::d3d12::host_import_va_in_vsharp_range(device_.arena.address, buffer_va_); + if (outside_vsharp && !(below_arena && keep_low_imports)) { static std::atomic logged{0}; if (logged.fetch_add(1, std::memory_order_relaxed) < 8) std::fprintf(stderr, - "[rt] d3d12: an import of title memory at %p (%llu bytes) landed below the arena (device " - "address 0x%llx, arena 0x%llx): a shadow in the arena is used instead\n", + "[rt] d3d12: an import of title memory at %p (%llu bytes) lies outside the 48-bit V# range " + "above the arena (device address 0x%llx, arena 0x%llx): a shadow in the arena is used instead\n", address, static_cast(desc_.Width), static_cast(buffer_va_), static_cast(device_.arena.address)); diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 60d7c62f30..7193971a7b 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -188,8 +188,12 @@ struct DescriptorRange { SIZE_T real_start; SIZE_T stride; bool mapped; + bool shader_visible; const DeviceProxy* device; const HeapProxy* heap; + // Per-slot 64-bit fingerprints of what was last synced from custom_start into real_start for shader-visible + // title-backed heaps. Initialized to 0 so pre-filled non-zero descriptors sync on first submit. + std::vector fingerprints; }; // Immortal: late COM Release calls during process teardown may still unregister a heap or check the table. @@ -215,13 +219,16 @@ DescriptorHeapBackingFree g_heap_free = nullptr; bool g_heap_alloc_explicit = false; bool register_descriptor_range(const DeviceProxy* device, const HeapProxy* heap, SIZE_T custom_start, - SIZE_T byte_size, SIZE_T real_start, SIZE_T stride) { + SIZE_T byte_size, SIZE_T real_start, SIZE_T stride, bool shader_visible) { if (custom_start == 0 || byte_size == 0 || custom_start > (std::numeric_limits::max)() - byte_size) return false; const bool mapped = device->descriptor_mapping && device->descriptor_mapping(heap->real, real_start, byte_size); try { + std::vector fingerprints; + if (shader_visible && stride == 32 && real_start != custom_start) fingerprints.assign(byte_size / stride, 0); std::lock_guard lock(descriptor_range_mutex()); - descriptor_ranges().push_back({custom_start, custom_start + byte_size, real_start, stride, mapped, device, heap}); + descriptor_ranges().push_back({custom_start, custom_start + byte_size, real_start, stride, mapped, + shader_visible, device, heap, std::move(fingerprints)}); g_custom_range_count.store(descriptor_ranges().size(), std::memory_order_release); return true; } catch (const std::bad_alloc&) { @@ -291,11 +298,22 @@ bool local_accessible(std::uint64_t address, std::uint64_t bytes, bool write) { return true; } +// Non-zero 64-bit fingerprint for a non-zero 32-byte descriptor slot; 0 if and only if all 32 bytes are 0. +std::uint64_t descriptor_slot_fingerprint(const std::uint64_t w[4]) { + if ((w[0] | w[1] | w[2] | w[3]) == 0) return 0; + std::uint64_t h = 0xcbf29ce484222325ull; + for (int i = 0; i < 4; ++i) { + h ^= w[i] + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2); + h *= 0x100000001b3ull; + } + return h ? h : 1ull; +} + void local_write_through(const DeviceProxy* device, D3D12_CPU_DESCRIPTOR_HANDLE custom, SIZE_T count) { if (!local_desc_mirror() || !custom.ptr || !count) return; if (!g_custom_range_count.load(std::memory_order_acquire)) return; std::lock_guard lock(descriptor_range_mutex()); - const auto& ranges = descriptor_ranges(); + auto& ranges = descriptor_ranges(); for (auto it = ranges.rbegin(); it != ranges.rend(); ++it) { if (it->device != device || custom.ptr < it->custom_start || custom.ptr >= it->custom_end) continue; const SIZE_T offset = custom.ptr - it->custom_start; @@ -307,6 +325,14 @@ void local_write_through(const DeviceProxy* device, D3D12_CPU_DESCRIPTOR_HANDLE if (real == custom.ptr || (!it->mapped && !local_accessible(real, bytes, false)) || !local_accessible(custom.ptr, bytes, true)) return; std::memcpy(reinterpret_cast(custom.ptr), reinterpret_cast(real), bytes); + if (!it->fingerprints.empty() && it->stride == kLocalDescriptorBytes) { + const SIZE_T first = offset / kLocalDescriptorBytes; + for (SIZE_T i = 0; i < count && first + i < it->fingerprints.size(); ++i) { + std::uint64_t w[4]{}; + std::memcpy(w, reinterpret_cast(custom.ptr + i * kLocalDescriptorBytes), sizeof(w)); + it->fingerprints[first + i] = descriptor_slot_fingerprint(w); + } + } static std::atomic said{false}; if (!said.exchange(true)) std::fprintf(stderr, "[rt] d3d12_xs: local: descriptors written for a title-backed heap are also stored at the " @@ -315,6 +341,79 @@ void local_write_through(const DeviceProxy* device, D3D12_CPU_DESCRIPTOR_HANDLE } } +// Pulls direct CPU writes from a custom-backed source heap into its real backing before CopyDescriptors* reads it. +void local_sync_source(const DeviceProxy* device, D3D12_CPU_DESCRIPTOR_HANDLE custom, SIZE_T count) { + if (!local_desc_mirror() || !custom.ptr || !count) return; + if (!g_custom_range_count.load(std::memory_order_acquire)) return; + std::lock_guard lock(descriptor_range_mutex()); + auto& ranges = descriptor_ranges(); + for (auto it = ranges.rbegin(); it != ranges.rend(); ++it) { + if (it->device != device || custom.ptr < it->custom_start || custom.ptr >= it->custom_end) continue; + const SIZE_T offset = custom.ptr - it->custom_start; + if (!it->stride || offset % it->stride || count > (it->custom_end - custom.ptr) / it->stride) return; + const SIZE_T bytes = count * it->stride; + if (offset > (std::numeric_limits::max)() - it->real_start) return; + const SIZE_T real = it->real_start + offset; + if (real == custom.ptr || !local_accessible(custom.ptr, bytes, false) || + (!it->mapped && !local_accessible(real, bytes, true))) return; + if (it->stride == kLocalDescriptorBytes) { + const SIZE_T first = offset / kLocalDescriptorBytes; + for (SIZE_T i = 0; i < count; ++i) { + const SIZE_T at = i * kLocalDescriptorBytes; + std::uint64_t w[4]{}; + std::memcpy(w, reinterpret_cast(custom.ptr + at), sizeof(w)); + const std::uint64_t fp = descriptor_slot_fingerprint(w); + if (!fp && (it->fingerprints.empty() || first + i >= it->fingerprints.size() || + it->fingerprints[first + i] == 0)) + continue; + std::memcpy(reinterpret_cast(real + at), w, sizeof(w)); + if (first + i < it->fingerprints.size()) it->fingerprints[first + i] = fp; + } + } else { + std::memcpy(reinterpret_cast(real), reinterpret_cast(custom.ptr), bytes); + } + return; + } +} + +// Before ExecuteCommandLists, copies any pre-filled or title-byte-copied descriptors from mapped (shader-visible) +// custom-backed heaps into their GPU-arena real backing using per-slot 64-bit fingerprints. +void local_sync_shader_visible_heaps(const DeviceProxy* device) { + if (!local_desc_mirror() || g_custom_range_count.load(std::memory_order_acquire) == 0) return; + std::size_t synced = 0; + { + std::lock_guard lock(descriptor_range_mutex()); + for (DescriptorRange& range : descriptor_ranges()) { + if ((device && range.device != device) || !range.shader_visible || range.stride != kLocalDescriptorBytes || + range.real_start == range.custom_start || range.fingerprints.empty()) + continue; + const SIZE_T bytes = range.fingerprints.size() * kLocalDescriptorBytes; + if (!local_accessible(range.custom_start, bytes, false) || + (!range.mapped && !local_accessible(range.real_start, bytes, true))) + continue; + auto* dst = reinterpret_cast(range.real_start); + for (SIZE_T i = 0; i < range.fingerprints.size(); ++i) { + const SIZE_T at = i * kLocalDescriptorBytes; + std::uint64_t w[4]{}; + std::memcpy(w, reinterpret_cast(range.custom_start + at), sizeof(w)); + const std::uint64_t fp = descriptor_slot_fingerprint(w); + if (fp == range.fingerprints[i]) continue; + std::memcpy(dst + at, w, sizeof(w)); + range.fingerprints[i] = fp; + ++synced; + } + } + } + if (synced > 0) { + static std::atomic calls{0}; + const std::uint64_t n = calls.fetch_add(1, std::memory_order_relaxed) + 1; + if (n <= 8 || (n & (n - 1)) == 0) + std::fprintf(stderr, + "[rt] d3d12_xs: synced %zu title-written descriptors into shader-visible GPU arena (sync #%llu)\n", + synced, static_cast(n)); + } +} + // Caller holds descriptor_range_mutex, keeping the backing alive for the complete copy. std::uint64_t copy_title_slots(std::uint8_t* dest, SIZE_T source, SIZE_T bytes) { const SIZE_T slots = bytes / kLocalDescriptorBytes; @@ -933,7 +1032,8 @@ void** get_heap_vtable() { HRESULT create_heap_proxy(ID3D12DescriptorHeap* real, DeviceProxy* device, REFIID riid, void** out, SIZE_T custom_cpu_start = 0, SIZE_T custom_byte_size = 0, SIZE_T real_cpu_start = 0, - DescriptorHeapBackingFree custom_free = nullptr, SIZE_T stride = 0) { + DescriptorHeapBackingFree custom_free = nullptr, SIZE_T stride = 0, + bool shader_visible = false) { if (!real || !device) return E_POINTER; if (!out) return E_POINTER; *out = nullptr; @@ -952,7 +1052,8 @@ HRESULT create_heap_proxy(ID3D12DescriptorHeap* real, DeviceProxy* device, REFII proxy_add_ref(device); // the heap keeps its Xbox-ABI device alive for GetDevice if (custom_cpu_start != 0 && - !register_descriptor_range(device, proxy, custom_cpu_start, custom_byte_size, real_cpu_start, stride)) { + !register_descriptor_range(device, proxy, custom_cpu_start, custom_byte_size, real_cpu_start, stride, + shader_visible)) { delete proxy; proxy_release(device); return E_OUTOFMEMORY; @@ -1720,6 +1821,7 @@ std::uint64_t queue_present_x(ChildProxy*, std::uint64_t, std::uint64_t, std::ui // list proxy replaced by its real list (a bundle on the list side unwraps the same way at the console's // ExecuteBundle); the real queue never sees a proxy. std::uint64_t queue_execute_command_lists(ChildProxy* self, std::uint64_t count_word, std::uint64_t lists_word) { + local_sync_shader_visible_heaps(self->device); // 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; @@ -3440,7 +3542,8 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H } hr = create_heap_proxy(heap, self, *riid, out, custom_cpu_start, custom_byte_size, real_cpu_start, - custom_cpu_start ? free_fn : nullptr, stride); + custom_cpu_start ? free_fn : nullptr, stride, + desc && (desc->Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE) != 0); // LOCAL DIAGNOSTIC (fsdraw): which CPU start the title gets for this heap if (leonida::local_diagnostics()) { static std::atomic logged{0}; @@ -3538,7 +3641,10 @@ void proxy_copy_descriptors(DeviceProxy* self, UINT num_dest, const D3D12_CPU_DE src_heap.resize(num_src); src = src_heap.data(); } - for (UINT i = 0; i < num_src; ++i) src[i] = translate_cpu_handle(self, src_starts[i]); + for (UINT i = 0; i < num_src; ++i) { + local_sync_source(self, src_starts[i], src_sizes ? src_sizes[i] : 1); + src[i] = translate_cpu_handle(self, src_starts[i]); + } src_out = src; } } catch (const std::bad_alloc&) { @@ -3552,6 +3658,7 @@ void proxy_copy_descriptors(DeviceProxy* self, UINT num_dest, const D3D12_CPU_DE void proxy_copy_descriptors_simple(DeviceProxy* self, UINT num_descriptors, D3D12_CPU_DESCRIPTOR_HANDLE dest_start, D3D12_CPU_DESCRIPTOR_HANDLE src_start, D3D12_DESCRIPTOR_HEAP_TYPE type) { note_slot(24); + local_sync_source(self, src_start, num_descriptors); const D3D12_CPU_DESCRIPTOR_HANDLE dest = translate_cpu_handle(self, dest_start); const D3D12_CPU_DESCRIPTOR_HANDLE src = translate_cpu_handle(self, src_start); self->real->CopyDescriptorsSimple(num_descriptors, dest, src, type); diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index 471c6a35ac..6cecfc42f9 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include namespace { @@ -168,6 +170,151 @@ bool check_normal_heap_unchanged(ID3D12Device* device) { return true; } +void* g_next_backing = nullptr; +void* test_heap_alloc(std::size_t, std::uint64_t) { + void* p = g_next_backing; + g_next_backing = nullptr; + return p; +} +void test_heap_free(void*, std::uint64_t) {} + +bool check_mapped_title_heap_sync_and_copy(ID3D12Device* real_device) { + struct VirtualMem { + void* p = nullptr; + ~VirtualMem() { if (p) VirtualFree(p, 0, MEM_RELEASE); } + }; + struct AllocGuard { + ~AllocGuard() { + g_next_backing = nullptr; + rt::d3d12_xs::set_descriptor_heap_allocator(nullptr, nullptr); + } + }; + VirtualMem sv_mem{VirtualAlloc(nullptr, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)}; + if (!sv_mem.p) return false; + AllocGuard alloc_guard; + + real_device->AddRef(); + Ref proxy; + if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(proxy.put()), false)) || !proxy.get()) { + real_device->Release(); + std::fprintf(stderr, "create_device_proxy failed\n"); + return false; + } + const UINT stride = real_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + if (stride != 32) { + std::fprintf(stderr, "unexpected CBV_SRV_UAV descriptor stride %u\n", stride); + return false; + } + + // Obtain three distinct, valid CBV descriptor records from the layer. + D3D12_DESCRIPTOR_HEAP_DESC staging_desc{}; + staging_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + staging_desc.NumDescriptors = 4; + staging_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + Ref staging; + if (FAILED(real_device->CreateDescriptorHeap(&staging_desc, IID_PPV_ARGS(staging.put()))) || !staging.get()) { + std::fprintf(stderr, "CreateDescriptorHeap(staging) failed\n"); + return false; + } + const SIZE_T staging_base = staging->GetCPUDescriptorHandleForHeapStart().ptr; + D3D12_CONSTANT_BUFFER_VIEW_DESC cbv0{0x10000ull, 256u}; + D3D12_CONSTANT_BUFFER_VIEW_DESC cbv1{0x20000ull, 512u}; + D3D12_CONSTANT_BUFFER_VIEW_DESC cbv2{0x30000ull, 768u}; + real_device->CreateConstantBufferView(&cbv0, D3D12_CPU_DESCRIPTOR_HANDLE{staging_base}); + real_device->CreateConstantBufferView(&cbv1, D3D12_CPU_DESCRIPTOR_HANDLE{staging_base + stride}); + real_device->CreateConstantBufferView(&cbv2, D3D12_CPU_DESCRIPTOR_HANDLE{staging_base + 2 * stride}); + std::vector bytes0(stride), bytes1(stride), bytes2(stride); + std::memcpy(bytes0.data(), reinterpret_cast(staging_base), stride); + std::memcpy(bytes1.data(), reinterpret_cast(staging_base + stride), stride); + std::memcpy(bytes2.data(), reinterpret_cast(staging_base + 2 * stride), stride); + if (bytes0 == bytes1 || bytes1 == bytes2) { + std::fprintf(stderr, "staging CBVs were not distinct\n"); + return false; + } + + constexpr UINT kSlots = 8; + const SIZE_T heap_bytes = static_cast(kSlots) * stride; + + // 1. Pre-fill slot 0 BEFORE CreateDescriptorHeap(shader-visible) with custom XMem backing: + // must sync into the GPU-arena real heap on the very first ExecuteCommandLists. + std::memset(sv_mem.p, 0, heap_bytes); + std::memcpy(sv_mem.p, bytes0.data(), stride); + + rt::d3d12_xs::set_descriptor_heap_allocator(&test_heap_alloc, &test_heap_free); + g_next_backing = sv_mem.p; + D3D12_DESCRIPTOR_HEAP_DESC sv_desc{}; + sv_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + sv_desc.NumDescriptors = kSlots; + sv_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + Ref sv_heap; + if (FAILED(proxy->CreateDescriptorHeap(&sv_desc, IID_PPV_ARGS(sv_heap.put()))) || !sv_heap.get()) { + std::fprintf(stderr, "proxy CreateDescriptorHeap(custom shader-visible) failed\n"); + return false; + } + auto* sv_real = static_cast(rt::d3d12_xs::real_object_behind_proxy(sv_heap.p)); + if (!sv_real) { + std::fprintf(stderr, "custom shader-visible heap has no real heap\n"); + return false; + } + + D3D12_COMMAND_QUEUE_DESC qdesc{}; + qdesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; + Ref queue; + if (FAILED(proxy->CreateCommandQueue(&qdesc, IID_PPV_ARGS(queue.put()))) || !queue.get()) { + std::fprintf(stderr, "proxy CreateCommandQueue failed\n"); + return false; + } + + // First submit: pre-filled slot 0 must reach sv_real immediately. + queue->ExecuteCommandLists(0, nullptr); + const SIZE_T sv_real_start = sv_real->GetCPUDescriptorHandleForHeapStart().ptr; + if (std::memcmp(reinterpret_cast(sv_real_start), bytes0.data(), stride) != 0) { + std::fprintf(stderr, "pre-filled custom shader-visible heap slot 0 did not sync on first submit\n"); + return false; + } + + // Post-creation direct CPU write to slot 1: must sync on next submit. + std::memcpy(static_cast(sv_mem.p) + stride, bytes1.data(), stride); + queue->ExecuteCommandLists(0, nullptr); + if (std::memcmp(reinterpret_cast(sv_real_start + stride), bytes1.data(), stride) != 0) { + std::fprintf(stderr, "post-creation direct CPU write to slot 1 did not sync on submit\n"); + return false; + } + + // 2. Direct CPU writes into custom backing as source of CopyDescriptorsSimple and CopyDescriptors + // (before any ExecuteCommandLists): must reach an ordinary destination heap on the CPU timeline. + std::memcpy(static_cast(sv_mem.p) + 2 * stride, bytes2.data(), stride); + std::memcpy(static_cast(sv_mem.p) + 3 * stride, bytes0.data(), stride); + + Ref ordinary_dst; + if (FAILED(real_device->CreateDescriptorHeap(&staging_desc, IID_PPV_ARGS(ordinary_dst.put()))) || + !ordinary_dst.get()) { + std::fprintf(stderr, "CreateDescriptorHeap(ordinary_dst) failed\n"); + return false; + } + const SIZE_T dst_start = ordinary_dst->GetCPUDescriptorHandleForHeapStart().ptr; + const SIZE_T src_start = reinterpret_cast(sv_mem.p); + + proxy->CopyDescriptorsSimple(1, D3D12_CPU_DESCRIPTOR_HANDLE{dst_start}, + D3D12_CPU_DESCRIPTOR_HANDLE{src_start + 2 * stride}, + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + if (std::memcmp(reinterpret_cast(dst_start), bytes2.data(), stride) != 0) { + std::fprintf(stderr, "CopyDescriptorsSimple from custom-backed heap missed direct CPU write\n"); + return false; + } + + const D3D12_CPU_DESCRIPTOR_HANDLE dst_range{dst_start + stride}; + const D3D12_CPU_DESCRIPTOR_HANDLE src_range{src_start + 3 * stride}; + const UINT one = 1; + proxy->CopyDescriptors(1, &dst_range, &one, 1, &src_range, &one, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + if (std::memcmp(reinterpret_cast(dst_start + stride), bytes0.data(), stride) != 0) { + std::fprintf(stderr, "CopyDescriptors from custom-backed heap missed direct CPU write\n"); + return false; + } + + return true; +} + } // namespace int main() { @@ -195,6 +342,7 @@ int main() { if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(host.put())))) return 1; if (!check_xs_proxy_slot14(host.get(), false)) return 1; if (!check_xs_proxy_slot14(device.get(), true)) return 1; + if (!check_mapped_title_heap_sync_and_copy(device.get())) return 1; std::fprintf(stderr, "ok\n"); return 0; } diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index d84454fce9..a3e499e0d2 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1867,16 +1867,80 @@ void print_cases(const std::vector& cases, const char* who) { int(c.desc.Properties.CPUPageProperty), int(c.desc.Properties.MemoryPoolPreference), unsigned(c.desc.Flags)); } + +int check_vsharp_48bit_range_guard() { + const char* who = "vsharp 48-bit range guard"; + int failures = 0; + constexpr VkDeviceAddress kArena = 0x00007FFF00000000ull; + constexpr VkDeviceAddress kLimit = (1ull << 48) - 1ull; + // Guard is inactive when either address is 0 (no arena or no BDA). + EXPECT(rt::d3d12::host_import_va_in_vsharp_range(0, kArena)); + EXPECT(rt::d3d12::host_import_va_in_vsharp_range(kArena, 0)); + // Boundary inputs: just below arena, offsets 0 and 2^48 - 1 accepted; offset 2^48 and low VA rejected. + EXPECT(!rt::d3d12::host_import_va_in_vsharp_range(kArena, kArena - 1ull)); + EXPECT(!rt::d3d12::host_import_va_in_vsharp_range(kArena, 0x1000ull)); + EXPECT(rt::d3d12::host_import_va_in_vsharp_range(kArena, kArena)); + EXPECT(rt::d3d12::host_import_va_in_vsharp_range(kArena, kArena + kLimit)); + EXPECT(!rt::d3d12::host_import_va_in_vsharp_range(kArena, kArena + (1ull << 48))); + if (failures == 0) std::printf("%s: deterministic boundary cases passed\n", who); + return failures; +} + +int check_title_placed_with_bda(const vktest::Device& vk) { + const char* who = "vulkan slot-133 buffer (BDA arena)"; + int failures = 0; + Ref bda_dev; + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = vk.instance_proc; + info.instance = vk.instance; + info.physical = vk.physical; + info.device = vk.device; + info.queue = vk.queue; + info.queue_family = vk.family; + info.host_pointer_alignment = vk.host_pointer_alignment; + info.buffer_device_address = true; + EXPECT_HR(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&bda_dev.p)), S_OK); + if (!bda_dev.p) return failures ? failures : 1; + + constexpr SIZE_T size = 0x10000; + void* pages = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + EXPECT(pages != nullptr); + if (!pages) return failures ? failures : 1; + std::memset(pages, 0x5A, size); + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + desc.Width = size; + desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1; + desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + { + Ref buffer; + EXPECT_HR(rt::d3d12::create_buffer_at_host_address(bda_dev.p, pages, desc, 0xFFFFFFFFu, + IID_PPV_ARGS(&buffer.p)), + S_OK); + if (buffer.p) { + const D3D12_GPU_VIRTUAL_ADDRESS gpu_va = buffer->GetGPUVirtualAddress(); + EXPECT(gpu_va != 0); + std::printf("%s: returned valid GPU VA 0x%llx (%s)\n", who, static_cast(gpu_va), + gpu_va == reinterpret_cast(pages) ? "direct host import" + : "arena shadow fallback"); + } + } + VirtualFree(pages, 0, MEM_RELEASE); + return failures; +} } // namespace int main() { _putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0"); vktest::Device vk; + vk.request_buffer_device_address = true; const auto status = vk.initialize(); if (status == vktest::DeviceStatus::unavailable) return 77; if (status != vktest::DeviceStatus::ready) return 1; int failures = 0; + failures += check_vsharp_48bit_range_guard(); { Ref ours; rt::d3d12::VulkanDeviceInfo info{}; @@ -1898,6 +1962,7 @@ int main() { failures += check_import(ours.p, vk.host_pointer_alignment != 0); failures += check_placed_target_backing(ours.p); if (vk.host_pointer_alignment) failures += check_title_placed_all(ours.p); + if (vk.host_pointer_alignment && vk.buffer_device_address) failures += check_title_placed_with_bda(vk); if (vk.host_image_copy) { failures += check_pending_stream_views(ours.p); failures += check_shared_bc_placements(ours.p);