upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorthibaut <thibaut-leaux-angeli@live.fr>2026-11-23 18:33:50 +0100
committerthibaut <thibaut-leaux-angeli@live.fr>2026-11-23 18:33:50 +0100
commit228cb9ec6ce51e178d6d656958db013eeec8cf5c (patch)
tree4408be47f3738c33dd0b080ea424268b0989cd51 /runtime
parentc3fe8252904e5bf7297aae79ba5306ad308bc2fb (diff)
d3d12: slot 133 imports from the page that holds the address and binds at the offset; uncommitted pages fall back
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 the import refused with E_INVALIDARG, halting the boot. The import now starts at the page holding the address and the buffer is bound at the offset; an offset off the buffer's alignment takes the ordinary-buffer fallback. A range not fully committed (xmem commits on first touch) also falls back instead of failing. Only a null address or a zero width are E_INVALIDARG. d3d12_existing_heap_test: a buffer 64 bytes into a page reads back the CPU's writes at its own VA; a 1-byte offset and a partly committed range fall back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp43
-rw-r--r--runtime/test/d3d12_existing_heap_test.cpp24
2 files changed, 39 insertions, 28 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index f0ed747a0..5224524fd 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -1906,24 +1906,30 @@ public:
1906 const VkDeviceSize alignment = device_.info.host_pointer_alignment; 1906 const VkDeviceSize alignment = device_.info.host_pointer_alignment;
1907 if (!device_.get_host_pointer_properties || !alignment) return E_NOTIMPL; 1907 if (!device_.get_host_pointer_properties || !alignment) return E_NOTIMPL;
1908 const auto start = reinterpret_cast<std::uintptr_t>(address); 1908 const auto start = reinterpret_cast<std::uintptr_t>(address);
1909 if (!address || !desc_.Width || (start & (alignment - 1)) || desc_.Width > UINT64_MAX - alignment) 1909 if (!address || !desc_.Width || desc_.Width > UINT64_MAX - 2 * alignment) return E_INVALIDARG;
1910 return E_INVALIDARG; 1910 // The import starts at the page that holds `address`, and the buffer is bound at the offset inside it: the
1911 const VkDeviceSize bytes = (desc_.Width + alignment - 1) & ~(alignment - 1); 1911 // title places small buffers inside a page too (a 16-byte UAV at a 64-byte-aligned address on an RTX 5090,
1912 // found in review), which an import of `address` itself would refuse.
1913 const std::uintptr_t base = start & ~static_cast<std::uintptr_t>(alignment - 1);
1914 void* const base_address = reinterpret_cast<void*>(base);
1915 const VkDeviceSize offset = start - base;
1916 const VkDeviceSize bytes = (offset + desc_.Width + alignment - 1) & ~(alignment - 1);
1912 // The whole imported range must be committed, possibly across several allocations (the retail buffer spans 1917 // The whole imported range must be committed, possibly across several allocations (the retail buffer spans
1913 // three mapped windows): anything else is undefined in Vulkan, so it is E_INVALIDARG here (found in review). 1918 // three mapped windows); a page not committed yet (xmem commits on first touch) or a write-combined one (on
1914 // A write-combined page is not imported: on an RTX 5090 such an import succeeded but GPU reads returned 1919 // an RTX 5090 such an import succeeded but GPU reads returned wrong bytes, found in review) is not imported,
1915 // wrong bytes (found in review), so it takes the ordinary-buffer fallback instead. 1920 // and the ordinary-buffer fallback answers instead.
1916 bool write_combined = false; 1921 bool importable = true;
1917 for (VkDeviceSize at = 0; at < bytes;) { 1922 for (VkDeviceSize at = 0; at < bytes;) {
1918 MEMORY_BASIC_INFORMATION region{}; 1923 MEMORY_BASIC_INFORMATION region{};
1919 if (!VirtualQuery(static_cast<const char*>(address) + at, &region, sizeof(region)) || 1924 if (!VirtualQuery(static_cast<const char*>(base_address) + at, &region, sizeof(region)) ||
1920 region.State != MEM_COMMIT) 1925 region.State != MEM_COMMIT || (region.Protect & (PAGE_WRITECOMBINE | PAGE_NOCACHE))) {
1921 return E_INVALIDARG; 1926 importable = false;
1922 if (region.Protect & (PAGE_WRITECOMBINE | PAGE_NOCACHE)) write_combined = true; 1927 break;
1928 }
1923 const auto region_end = reinterpret_cast<std::uintptr_t>(region.BaseAddress) + region.RegionSize; 1929 const auto region_end = reinterpret_cast<std::uintptr_t>(region.BaseAddress) + region.RegionSize;
1924 at = region_end - start; 1930 at = region_end - base;
1925 } 1931 }
1926 if (write_combined) return E_NOTIMPL; 1932 if (!importable) return E_NOTIMPL;
1927 const bool need_bda = device_.info.buffer_device_address || device_.arena.buffer != VK_NULL_HANDLE; 1933 const bool need_bda = device_.info.buffer_device_address || device_.arena.buffer != VK_NULL_HANDLE;
1928 VkExternalMemoryBufferCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO}; 1934 VkExternalMemoryBufferCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO};
1929 external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT; 1935 external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
@@ -1941,14 +1947,15 @@ public:
1941 vk.vkGetBufferMemoryRequirements(device_.info.device, buffer_, &requirements); 1947 vk.vkGetBufferMemoryRequirements(device_.info.device, buffer_, &requirements);
1942 VkMemoryHostPointerPropertiesEXT host{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; 1948 VkMemoryHostPointerPropertiesEXT host{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT};
1943 if (device_.get_host_pointer_properties(device_.info.device, 1949 if (device_.get_host_pointer_properties(device_.info.device,
1944 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, address, 1950 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, base_address,
1945 &host) != VK_SUCCESS) 1951 &host) != VK_SUCCESS)
1946 return E_INVALIDARG; 1952 return E_NOTIMPL;
1947 const std::uint32_t types = host.memoryTypeBits & requirements.memoryTypeBits; 1953 const std::uint32_t types = host.memoryTypeBits & requirements.memoryTypeBits;
1948 if (!types || bytes < requirements.size) return E_NOTIMPL; 1954 // An offset off the buffer's own alignment cannot be bound: the fallback answers.
1955 if (!types || (offset % requirements.alignment) || bytes < offset + requirements.size) return E_NOTIMPL;
1949 VkImportMemoryHostPointerInfoEXT import{VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT}; 1956 VkImportMemoryHostPointerInfoEXT import{VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT};
1950 import.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT; 1957 import.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1951 import.pHostPointer = address; 1958 import.pHostPointer = base_address;
1952 VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, &import}; 1959 VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, &import};
1953 flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; 1960 flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
1954 // Host-visible and coherent first, as the caller's pages are; then any type both the import and the 1961 // Host-visible and coherent first, as the caller's pages are; then any type both the import and the
@@ -1970,7 +1977,7 @@ public:
1970 if (memory_) break; 1977 if (memory_) break;
1971 } 1978 }
1972 if (!memory_) return E_OUTOFMEMORY; 1979 if (!memory_) return E_OUTOFMEMORY;
1973 if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, 0) != VK_SUCCESS) return E_OUTOFMEMORY; 1980 if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, offset) != VK_SUCCESS) return E_OUTOFMEMORY;
1974 mapped_ = address; 1981 mapped_ = address;
1975 host_import_ = true; 1982 host_import_ = true;
1976 va_ = start; 1983 va_ = start;
diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp
index 44059575f..378aa7819 100644
--- a/runtime/test/d3d12_existing_heap_test.cpp
+++ b/runtime/test/d3d12_existing_heap_test.cpp
@@ -224,9 +224,11 @@ int check_title_placed(ID3D12Device* device, const char* kind, void* pages, UINT
224 texture.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; 224 texture.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
225 Ref<ID3D12Resource> refused; 225 Ref<ID3D12Resource> refused;
226 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, texture, IID_PPV_ARGS(&refused.p)), E_NOTIMPL); 226 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, texture, IID_PPV_ARGS(&refused.p)), E_NOTIMPL);
227 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, bytes + 1, desc, IID_PPV_ARGS(&refused.p)),
228 E_INVALIDARG);
229 EXPECT(refused.p == nullptr); 227 EXPECT(refused.p == nullptr);
228 // An address off the buffer alignment can't be bound inside the import: the ordinary-buffer fallback answers.
229 Ref<ID3D12Resource> fallback;
230 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, bytes + 1, desc, IID_PPV_ARGS(&fallback.p)), S_OK);
231 EXPECT(fallback->GetGPUVirtualAddress() != reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(bytes + 1));
230 std::printf("%s: %s, %llu bytes: GPU VA is the address, a GPU copy reads the CPU's writes\n", who, kind, 232 std::printf("%s: %s, %llu bytes: GPU VA is the address, a GPU copy reads the CPU's writes\n", who, kind,
231 static_cast<unsigned long long>(size)); 233 static_cast<unsigned long long>(size));
232 return 0; 234 return 0;
@@ -238,6 +240,9 @@ int check_title_placed_all(ID3D12Device* device) {
238 void* pages = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); 240 void* pages = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
239 if (!pages) return 1; 241 if (!pages) return 1;
240 failures += check_title_placed(device, "VirtualAlloc range", pages, size); 242 failures += check_title_placed(device, "VirtualAlloc range", pages, size);
243 // Inside a page, as the title places small UAV buffers (64-byte aligned, found in review on an RTX 5090): the
244 // import starts at the page and the buffer is bound at the offset.
245 failures += check_title_placed(device, "64 bytes into a page", static_cast<char*>(pages) + 64, 0x1000 - 64);
241 VirtualFree(pages, 0, MEM_RELEASE); 246 VirtualFree(pages, 0, MEM_RELEASE);
242 HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, DWORD(size), nullptr); 247 HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, DWORD(size), nullptr);
243 void* view = section ? MapViewOfFile(section, FILE_MAP_ALL_ACCESS, 0, 0, size) : nullptr; 248 void* view = section ? MapViewOfFile(section, FILE_MAP_ALL_ACCESS, 0, 0, size) : nullptr;
@@ -247,8 +252,8 @@ int check_title_placed_all(ID3D12Device* device) {
247 CloseHandle(section); 252 CloseHandle(section);
248 253
249 // Not imported: a write-combined range (an RTX 5090 imported it but read wrong bytes back, found in review) 254 // Not imported: a write-combined range (an RTX 5090 imported it but read wrong bytes back, found in review)
250 // falls back to an ordinary buffer, so the resource exists but is not at the pages' address; a range only partly 255 // falls back to an ordinary buffer, so the resource exists but is not at the pages' address; so does a range only
251 // committed is E_INVALIDARG. 256 // partly committed.
252 const char* who = "vulkan slot-133 buffer"; 257 const char* who = "vulkan slot-133 buffer";
253 D3D12_RESOURCE_DESC desc{}; 258 D3D12_RESOURCE_DESC desc{};
254 desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; 259 desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
@@ -267,14 +272,13 @@ int check_title_placed_all(ID3D12Device* device) {
267 char* partial = static_cast<char*>(VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE)); 272 char* partial = static_cast<char*>(VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE));
268 EXPECT(partial != nullptr && VirtualAlloc(partial, size / 2, MEM_COMMIT, PAGE_READWRITE) == partial); 273 EXPECT(partial != nullptr && VirtualAlloc(partial, size / 2, MEM_COMMIT, PAGE_READWRITE) == partial);
269 { 274 {
270 Ref<ID3D12Resource> refused; 275 // Pages not committed yet (xmem commits on first touch) are not imported either: the fallback answers.
271 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, partial, desc, IID_PPV_ARGS(&refused.p)), 276 Ref<ID3D12Resource> fallback;
272 E_INVALIDARG); 277 EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, partial, desc, IID_PPV_ARGS(&fallback.p)), S_OK);
273 EXPECT(refused.p == nullptr); 278 EXPECT(fallback->GetGPUVirtualAddress() != reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(partial));
274 } 279 }
275 VirtualFree(partial, 0, MEM_RELEASE); 280 VirtualFree(partial, 0, MEM_RELEASE);
276 std::printf("%s: a write-combined range falls back to an ordinary buffer, a partly committed one is refused\n", 281 std::printf("%s: a write-combined range and a partly committed one fall back to an ordinary buffer\n", who);
277 who);
278 return failures; 282 return failures;
279} 283}
280 284