From 9f85ac6de2fda8feb0895015e2a46b7a4359a4c1 Mon Sep 17 00:00:00 2001 From: robot00f Date: Tue, 24 Nov 2026 21:36:03 -0300 Subject: [PATCH] d3d12: drop UNIFORM_BUFFER_BIT on sub-page host imports refused by 256B UBO alignment Resource::create_host_buffer adds VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT to every imported host buffer. On GPUs whose minUniformBufferOffsetAlignment is 256 bytes (measured on NVIDIA GeForce GTX 1050 Ti, Pascal), that raises VkMemoryRequirements::alignment from 16 to 256 and rounds requirements.size up to a multiple of 256, refusing 64-byte-aligned sub-page slot-133 placements such as "64 bytes into a page" in d3d12_existing_heap_test with E_NOTIMPL. D3D12 constant-buffer views require 256-byte address and size alignment anyway, and the layer encodes CBVs as raw GFX10 V# descriptors rather than Vulkan uniform buffers. Recreate the VkBuffer without VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT when the UBO requirement rejects a sub-page offset or size, compare remaining bytes without addition wrap, and add a 16-byte end-of-range sub-page case to d3d12_existing_heap_test. --- runtime/shims/d3d12_vulkan_device.cpp | 10 ++++++++++ runtime/test/d3d12_existing_heap_test.cpp | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 45f899b2f3..cca69370fb 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5828,6 +5828,16 @@ class Resource final }; VkMemoryRequirements requirements{}; vk.vkGetBufferMemoryRequirements(device_.info.device, buffer_, &requirements); + // On GPUs where UNIFORM_BUFFER_BIT rounds requirements.size past `bytes` (e.g. above minImportedHostPointerAlignment), + // D3D12 CBVs need 256-byte address and size alignment anyway and the layer encodes CBVs as raw V#s, so drop + // UNIFORM_BUFFER_BIT when the UBO size rounding exceeds the imported host range. + if (requirements.size > bytes) { + vk.vkDestroyBuffer(device_.info.device, buffer_, nullptr); + buffer_ = VK_NULL_HANDLE; + info.usage &= ~VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; + if (vk.vkCreateBuffer(device_.info.device, &info, nullptr, &buffer_) != VK_SUCCESS) return decline(E_OUTOFMEMORY); + vk.vkGetBufferMemoryRequirements(device_.info.device, buffer_, &requirements); + } VkMemoryHostPointerPropertiesEXT host{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; if (device_.get_host_pointer_properties(device_.info.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, base_address, diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 3d65fc7409..2d1ad54b4a 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -606,9 +606,11 @@ int check_title_placed_all(ID3D12Device* device) { failures += check_title_placed(device, "VirtualAlloc range", pages, size); failures += check_placed_texture_views(device, pages); // Inside a page, as the title places small UAV buffers (64-byte aligned, found in review on an RTX 5090): the - // import starts at the page and the buffer is bound at the offset. + // import starts at the page and the buffer is bound at the offset. Also check a 16-byte slice 64 bytes from the + // end of the committed range, where 256-byte uniform-buffer size rounding would run past the page. failures += check_title_placed(device, "64 bytes into a page", static_cast(pages) + 64, 0x1000 - 64); failures += check_host_prefix_buffers(device); + failures += check_title_placed(device, "16 bytes near the end of the range", static_cast(pages) + size - 64, 16); VirtualFree(pages, 0, MEM_RELEASE); HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, DWORD(size), nullptr); void* view = section ? MapViewOfFile(section, FILE_MAP_ALL_ACCESS, 0, 0, size) : nullptr;