diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 22d5f1f419..6e835f95b9 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5725,13 +5725,13 @@ class Resource final // Apply the same backing size rule to imports. The committed-range check and Vulkan bind requirements // below cover the padding as well. The import never initializes or writes the caller's padding. const VkDeviceSize backing_size = need_bda ? align_up(desc_.Width, sizeof(std::uint32_t)) : desc_.Width; - // The import starts at the page that holds `address`, and the buffer is bound at the offset inside it: the - // title places small buffers inside a page too (a 16-byte UAV at a 64-byte-aligned address on an RTX 5090, - // found in review), which an import of `address` itself would refuse. + // The import starts at the page that holds `address`, and so does the Vulkan buffer, with the D3D12 buffer at + // the offset inside it: the title places small buffers inside a page too (a 16-byte UAV at a 64-byte-aligned + // address on an RTX 5090, found in review), which an import of `address` itself would refuse. const std::uintptr_t base = start & ~static_cast(alignment - 1); void* const base_address = reinterpret_cast(base); const VkDeviceSize offset = start - base; - const VkDeviceSize bytes = align_up(offset + backing_size, alignment); + VkDeviceSize bytes = align_up(offset + backing_size, alignment); // grows to the buffer's memory size below // An imported buffer's VA is its host address, and it joins the same BufferVaIndex as default-heap buffers, // whose VAs are offsets in [0, arena size). A host range that reaches into that window (low host addresses, // as under Wine) would shadow arena buffers in the index's nearest-start lookup, so it takes the fallback @@ -5771,7 +5771,11 @@ class Resource final VkExternalMemoryBufferCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO}; external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT; VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, &external}; - info.size = backing_size; + // The Vulkan buffer starts at the page that holds `address` and the D3D12 buffer is `offset` bytes into it + // (ResourceBinding::buffer_offset), so the imported memory binds at 0 whatever the title's alignment: the + // retail title places small buffers 32 bytes apart (one at +0xc60 of a page), which a bind at `offset` + // refuses (an RTX 5080 asks 64). + info.size = offset + backing_size; info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | @@ -5805,9 +5809,20 @@ class Resource final &host) != VK_SUCCESS) return decline(E_NOTIMPL); const std::uint32_t types = host.memoryTypeBits & requirements.memoryTypeBits; - // An offset off the buffer's own alignment cannot be bound: the fallback answers. - if (!types || (offset % requirements.alignment) || requirements.size > bytes - offset) - return decline(E_NOTIMPL); + // The buffer's memory can be larger than its pages hold (64 bytes for a 32-byte buffer at +0xfe0 of a page, + // measured on the retail boot): the import then takes the next pages too, when they are committed and + // writable as above, and the fallback answers when they are not. + const VkDeviceSize needed = align_up((std::max)(VkDeviceSize(info.size), requirements.size), alignment); + for (VkDeviceSize at = bytes; at < needed;) { + MEMORY_BASIC_INFORMATION region{}; + if (!VirtualQuery(static_cast(base_address) + at, ®ion, sizeof(region)) || + region.State != MEM_COMMIT || (region.Protect & (PAGE_WRITECOMBINE | PAGE_NOCACHE | PAGE_GUARD)) || + !(region.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE))) + return decline(E_NOTIMPL); + at = reinterpret_cast(region.BaseAddress) + region.RegionSize - base; + } + bytes = (std::max)(bytes, needed); + if (!types || requirements.size > bytes) return decline(E_NOTIMPL); VkImportMemoryHostPointerInfoEXT import{VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT}; import.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT; import.pHostPointer = base_address; @@ -5839,12 +5854,12 @@ class Resource final if (memory_) break; } if (!memory_) return decline(E_OUTOFMEMORY); - if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, offset) != VK_SUCCESS) + if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, 0) != VK_SUCCESS) return decline(E_OUTOFMEMORY); if (need_bda && vk.vkGetBufferDeviceAddress) { VkBufferDeviceAddressInfo bda{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bda.buffer = buffer_; - buffer_va_ = vk.vkGetBufferDeviceAddress(device_.info.device, &bda); + buffer_va_ = vk.vkGetBufferDeviceAddress(device_.info.device, &bda) + offset; // `address` itself gpu_va_ = buffer_va_; } // An import the driver put below the arena cannot be named by a V#: the V# keeps 48 bits of (device address @@ -5882,6 +5897,7 @@ class Resource final } leonida::ResourceBinding binding{}; binding.buffer = buffer_; + binding.buffer_offset = offset; binding.indirect_arguments = (info.usage & VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT) != 0; binding.size = desc_.Width; binding.host = static_cast(address); diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index d84454fce9..bfa314bdc3 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -296,8 +296,8 @@ int check_title_placed(ID3D12Device* device, const char* kind, void* pages, UINT EXPECT(FAILED(rt::d3d12::create_buffer_at_host_address(device, pages, too_many_mips, 0x6u, IID_PPV_ARGS(&refused_texture.p))) && refused_texture.p == nullptr); - // An address off the buffer alignment can't be bound inside the import: a shadow buffer answers, at the same VA - // (a driver whose buffer alignment is 1 imports it; the VA is the address either way). + // An address off the buffer alignment is imported from its page (the Vulkan buffer starts at the page and the + // D3D12 buffer is an offset into it); a driver that refuses the import gets the shadow buffer, at the same VA. Ref fallback; desc.Width = size - 1; EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, bytes + 1, desc, 0xFFFFFFFFu, IID_PPV_ARGS(&fallback.p)), S_OK); diff --git a/runtime/test/d3d12_xmem_placed_test.cpp b/runtime/test/d3d12_xmem_placed_test.cpp index 7c64a23cf6..6ee3f4a5e5 100644 --- a/runtime/test/d3d12_xmem_placed_test.cpp +++ b/runtime/test/d3d12_xmem_placed_test.cpp @@ -415,10 +415,27 @@ int run(ID3D12Device* real, void* device, bool eager) { for (UINT64 i = 0; i < size; ++i) window[i] = static_cast(i * 131u + 7u); if (gpu_reads_cpu_writes(real, buffer.p, window, size)) return 1; } - rt::d3d12_xs::set_placed_at_va_pages_hook(nullptr); std::printf("%s: with commit_physical_range: %llu bytes committed, GPU VA is the window, a GPU copy reads the " "CPU's writes\n", who, static_cast(size)); + // A small buffer at a 32-byte-aligned address inside a page, as the retail title places one (+0xc60): the Vulkan + // buffer starts at the page and its memory binds there (a bind at the offset is refused off the buffer's + // alignment, 64 on an RTX 5080), so it is an import, not a shadow, and the GPU reads the CPU's writes. + { + auto piece = desc; + piece.Width = 0x40; + std::uint8_t* const at = window + 0xc60; + const std::uint64_t imports = rt::d3d12::title_host_imports(real); + Ref buffer; + EXPECT(place(device, at, piece, &buffer.p) == S_OK && buffer.p); + EXPECT(rt::d3d12::title_host_imports(real) == imports + 1); + EXPECT(buffer->GetGPUVirtualAddress() == reinterpret_cast(at)); + for (UINT64 i = 0; i < piece.Width; ++i) at[i] = static_cast(i * 29u + 3u); + if (gpu_reads_cpu_writes(real, buffer.p, at, piece.Width)) return 1; + } + rt::d3d12_xs::set_placed_at_va_pages_hook(nullptr); + std::printf("%s: a 64-byte buffer at +0xc60 of a page is imported and the GPU reads the CPU's writes\n", who); + EXPECT(map(window, kPages, nullptr) == window); EXPECT(free_pages(count, pages) == TRUE); return 0;