diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0273f9339b..21a3861978 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1870,8 +1870,8 @@ class Resource final // No host pointer: this is DEFAULT-heap arena memory the GPU may write, so its mapped bytes can be // stale. host_at() must not treat it as a CPU-written instance pointer list. // A default-heap AS resource is one of these ranges: a GENERIC acceleration structure covers it where - // it sits. It is named by its real device address (heap base + offset), as instance descriptors and - // BuildRaytracingAccelerationStructure hand that address to the driver, not a console VA. + // it sits. It is named by its device address (heap base + offset, as the driver reports it), as instance + // descriptors and BuildRaytracingAccelerationStructure hand that address to the driver, not a console VA. if (device_.info.ray_tracing && initial_state == D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE && vk.vkCreateAccelerationStructureKHR) { @@ -1882,7 +1882,27 @@ class Resource final asci.type = VK_ACCELERATION_STRUCTURE_TYPE_GENERIC_KHR; if (vk.vkCreateAccelerationStructureKHR(device_.info.device, &asci, nullptr, &accel_) == VK_SUCCESS) { gpu_va_ = device_.arena.address + va_; + // An instance names its BLAS by a value obtained from vkGetAccelerationStructureDeviceAddressKHR + // (VUID-vkCmdBuildAccelerationStructuresKHR-pInfos-12281), so ask for it, as the committed path + // below does. For GENERIC the spec only promises that these addresses keep the offsets between + // the structures of one buffer; drivers return buffer address + offset (vkd3d-proton checks the + // same). If one ever does not, the driver's address is the one instances must hold. + if (vk.vkGetAccelerationStructureDeviceAddressKHR) { + VkAccelerationStructureDeviceAddressInfoKHR dai{ + VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR}; + dai.accelerationStructure = accel_; + const std::uint64_t as_va = + vk.vkGetAccelerationStructureDeviceAddressKHR(device_.info.device, &dai); + if (as_va && as_va != gpu_va_) { + log_once("GENERIC acceleration structure address is not its buffer address + offset; " + "arena offset in KiB:", int(arena_offset_ / 1024)); + buffer_va_ = gpu_va_; + gpu_va_ = as_va; + } + } device_.context->register_acceleration_structure(gpu_va_, accel_); + if (buffer_va_ && buffer_va_ != gpu_va_) + device_.context->register_acceleration_structure(buffer_va_, accel_); } } device_.context->add_resource(id(), binding); diff --git a/runtime/test/d3d12_indirect_rt_test.cpp b/runtime/test/d3d12_indirect_rt_test.cpp index 6a921d8207..e27c7bad0e 100644 --- a/runtime/test/d3d12_indirect_rt_test.cpp +++ b/runtime/test/d3d12_indirect_rt_test.cpp @@ -75,6 +75,38 @@ D3D12_RESOURCE_DESC make_buf_desc(UINT64 bytes, D3D12_RESOURCE_FLAGS flags = D3D return d; } +// A driver whose GENERIC acceleration structures do not sit at buffer address + offset, through the device's own +// function lookup (VulkanDeviceInfo::get_instance_proc): vkGetAccelerationStructureDeviceAddressKHR answers the real +// address plus kShiftedAsDelta. Only resource creation and the address lookup run on such a device, no builds: the +// shifted address names no real structure. +constexpr VkDeviceAddress kShiftedAsDelta = 0x10000000; +PFN_vkGetInstanceProcAddr g_real_gipa = nullptr; +PFN_vkGetDeviceProcAddr g_real_gdpa = nullptr; +PFN_vkGetAccelerationStructureDeviceAddressKHR g_real_as_address = nullptr; + +VKAPI_ATTR VkDeviceAddress VKAPI_CALL shifted_as_address(VkDevice device, + const VkAccelerationStructureDeviceAddressInfoKHR* info) { + const VkDeviceAddress real = g_real_as_address(device, info); + return real ? real + kShiftedAsDelta : 0; +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL shifted_gdpa(VkDevice device, const char* name) { + if (!std::strcmp(name, "vkGetAccelerationStructureDeviceAddressKHR")) { + g_real_as_address = + reinterpret_cast(g_real_gdpa(device, name)); + return g_real_as_address ? reinterpret_cast(&shifted_as_address) : nullptr; + } + return g_real_gdpa(device, name); +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL shifted_gipa(VkInstance instance, const char* name) { + if (!std::strcmp(name, "vkGetDeviceProcAddr")) { + g_real_gdpa = reinterpret_cast(g_real_gipa(instance, name)); + return reinterpret_cast(&shifted_gdpa); + } + return g_real_gipa(instance, name); +} + } // namespace int main() { @@ -290,6 +322,7 @@ int main() { DF(vkCreateComputePipelines); DF(vkDestroyPipeline); DF(vkCreateDescriptorSetLayout); + DF(vkGetAccelerationStructureDeviceAddressKHR); DF(vkDestroyDescriptorSetLayout); DF(vkCreateDescriptorPool); DF(vkDestroyDescriptorPool); @@ -775,6 +808,63 @@ int main() { rt::d3d12::vulkan_acceleration_structure(dev, tlas_ptr_buf->GetGPUVirtualAddress()); if (!tlas_vk || !tlas_ptr_vk) return fail("vulkan_acceleration_structure(TLAS)"); + // The instance above holds the BLAS's GetGPUVirtualAddress(). Vulkan wants a value obtained from + // vkGetAccelerationStructureDeviceAddressKHR there (VUID-vkCmdBuildAccelerationStructuresKHR-pInfos-12281), + // so every AS resource's GPU VA must be what the driver reports for the structure behind it. GPU-assisted + // validation only accepts addresses it has seen that call return. + if (!vkGetAccelerationStructureDeviceAddressKHR) return fail("vkGetAccelerationStructureDeviceAddressKHR"); + for (ID3D12Resource* as_buf : {blas_buf, tlas_buf, tlas_ptr_buf}) { + const D3D12_GPU_VIRTUAL_ADDRESS va = as_buf->GetGPUVirtualAddress(); + VkAccelerationStructureDeviceAddressInfoKHR dai{ + VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR}; + dai.accelerationStructure = rt::d3d12::vulkan_acceleration_structure(dev, va); + if (!dai.accelerationStructure) return fail("vulkan_acceleration_structure(AS resource)"); + const VkDeviceAddress driver_va = vkGetAccelerationStructureDeviceAddressKHR(vk_dev, &dai); + if (driver_va != va) { + std::fprintf(stderr, + "FAIL: AS resource GPU VA 0x%llx, vkGetAccelerationStructureDeviceAddressKHR 0x%llx\n", + static_cast(va), static_cast(driver_va)); + return 1; + } + } + std::printf(" [PASS] AS resource GPU VAs are the driver's acceleration structure addresses\n"); + + // The same on a driver that answers another address than buffer address + offset: the resource's GPU VA is + // the driver's, the arena address still names the structure, and releasing the resource forgets both. + { + g_real_gipa = gipa; + rt::d3d12::VulkanDeviceInfo shifted_info = info; + shifted_info.get_instance_proc = &shifted_gipa; + ID3D12Device5* shifted = nullptr; + if (FAILED(rt::d3d12::create_vulkan_device(shifted_info, IID_PPV_ARGS(&shifted))) || !shifted) + return fail("create_vulkan_device(shifted AS addresses)"); + ID3D12Resource* as_buf = nullptr; + if (FAILED(shifted->CreateCommittedResource(&def_hp, D3D12_HEAP_FLAG_NONE, &blas_desc, + D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, + nullptr, IID_PPV_ARGS(&as_buf)))) + return fail("CreateCommittedResource(AS, shifted addresses)"); + const D3D12_GPU_VIRTUAL_ADDRESS va = as_buf->GetGPUVirtualAddress(); + VkAccelerationStructureDeviceAddressInfoKHR dai{ + VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR}; + dai.accelerationStructure = rt::d3d12::vulkan_acceleration_structure(shifted, va); + if (!dai.accelerationStructure) return fail("shifted AS: the GPU VA does not name the structure"); + const VkDeviceAddress buffer_va = vkGetAccelerationStructureDeviceAddressKHR(vk_dev, &dai); + if (va != buffer_va + kShiftedAsDelta) { + std::fprintf(stderr, "FAIL: shifted AS: GPU VA 0x%llx, driver address 0x%llx\n", + static_cast(va), + static_cast(buffer_va + kShiftedAsDelta)); + return 1; + } + if (rt::d3d12::vulkan_acceleration_structure(shifted, buffer_va) != dai.accelerationStructure) + return fail("shifted AS: the arena address does not name the structure"); + as_buf->Release(); + if (rt::d3d12::vulkan_acceleration_structure(shifted, va) || + rt::d3d12::vulkan_acceleration_structure(shifted, buffer_va)) + return fail("shifted AS: an address still names the structure after Release"); + shifted->Release(); + std::printf(" [PASS] a driver address other than buffer address + offset is the GPU VA; both freed\n"); + } + // BLAS compaction size (inline postbuild + standalone 2-source EmitPostbuild), CLONE, COMPACT, and // negative rejection checks. ID3D12Fence* copy_fence = nullptr;