From 074275d91170e59768e5ecc41ea9d36b2ee264d6 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Fri, 1 Jul 2022 13:38:14 -0700 Subject: [PATCH] dzn: Add a DXCore enumeration path WSL doesn't have DXGI, but it does have DXCore. DXCore also has a nice property that it filters to only D3D12-capable adapters. We can rely on DXCore as a first option even for Windows, because we'll be able to let the Vulkan loader do preference sorting, instead of having to rely on DXGI to do it for us. Reviewed-by: Boris Brezillon Reviewed-by: Bill Kristiansen Part-of: --- src/microsoft/vulkan/dzn_device.c | 6 +- src/microsoft/vulkan/dzn_dxcore.cpp | 95 +++++++++++++++++++ .../vulkan/dzn_physical_device_enum.h | 3 + src/microsoft/vulkan/meson.build | 1 + 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/microsoft/vulkan/dzn_dxcore.cpp diff --git a/src/microsoft/vulkan/dzn_device.c b/src/microsoft/vulkan/dzn_device.c index 46b81f8f5a5..2bce231969b 100644 --- a/src/microsoft/vulkan/dzn_device.c +++ b/src/microsoft/vulkan/dzn_device.c @@ -1015,11 +1015,13 @@ dzn_EnumeratePhysicalDevices(VkInstance inst, VK_FROM_HANDLE(dzn_instance, instance, inst); if (!instance->physical_devices_enumerated) { + VkResult result = dzn_enumerate_physical_devices_dxcore(instance); #ifdef _WIN32 - VkResult result = dzn_enumerate_physical_devices_dxgi(instance); + if (result != VK_SUCCESS) + result = dzn_enumerate_physical_devices_dxgi(instance); +#endif if (result != VK_SUCCESS) return result; -#endif } VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, diff --git a/src/microsoft/vulkan/dzn_dxcore.cpp b/src/microsoft/vulkan/dzn_dxcore.cpp new file mode 100644 index 00000000000..7d744834e59 --- /dev/null +++ b/src/microsoft/vulkan/dzn_dxcore.cpp @@ -0,0 +1,95 @@ +/* + * Copyright © Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "dzn_physical_device_enum.h" +#include +#include + +#include "u_dl.h" +#include "log.h" + +VkResult +dzn_enumerate_physical_devices_dxcore(struct dzn_instance *instance) +{ + util_dl_library *dxcore = util_dl_open(UTIL_DL_PREFIX "dxcore" UTIL_DL_EXT); + if (!dxcore) { + mesa_loge("Failed to load DXCore\n"); + return VK_ERROR_INITIALIZATION_FAILED; + } + + using PFNDXCoreCreateAdapterFactory = HRESULT (APIENTRY*)(REFIID, void **); + PFNDXCoreCreateAdapterFactory create_func = (PFNDXCoreCreateAdapterFactory)util_dl_get_proc_address(dxcore, "DXCoreCreateAdapterFactory"); + if (!create_func) { + mesa_loge("Failed to load DXCoreCreateAdapterFactory\n"); + return VK_ERROR_INITIALIZATION_FAILED; + } + + IDXCoreAdapterFactory *factory; + if (FAILED(create_func(IID_PPV_ARGS(&factory)))) { + mesa_loge("Failed to create DXCore adapter factory\n"); + return VK_ERROR_INITIALIZATION_FAILED; + } + + IDXCoreAdapterList *list; + if (FAILED(factory->CreateAdapterList(1, &DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS, IID_PPV_ARGS(&list)))) { + factory->Release(); + mesa_loge("Failed to create DXCore adapter list\n"); + return VK_ERROR_INITIALIZATION_FAILED; + } + + VkResult result = VK_SUCCESS; + uint32_t adapter_count = list->GetAdapterCount(); + IDXCoreAdapter *adapter; + for (uint32_t i = 0; i < adapter_count && result == VK_SUCCESS; ++i) { + result = VK_ERROR_INITIALIZATION_FAILED; + if (SUCCEEDED(list->GetAdapter(i, IID_PPV_ARGS(&adapter)))) { + dzn_physical_device_desc desc = { 0 }; + DXCoreHardwareID hardware_id; + bool is_hardware; + if (FAILED(adapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hardware_id)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::DedicatedAdapterMemory, &desc.dedicated_video_memory)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::SharedSystemMemory, &desc.shared_system_memory)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::DedicatedSystemMemory, &desc.dedicated_system_memory)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &desc.adapter_luid)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::IsHardware, &is_hardware)) || + FAILED(adapter->GetProperty(DXCoreAdapterProperty::DriverDescription, sizeof(desc.description), desc.description))) { + mesa_loge("Failed to retrieve DXCore adapter properties\n"); + result = VK_ERROR_INITIALIZATION_FAILED; + } else { + desc.vendor_id = hardware_id.vendorID; + desc.device_id = hardware_id.deviceID; + desc.subsys_id = hardware_id.subSysID; + desc.revision = hardware_id.revision; + desc.is_warp = !is_hardware; + result = dzn_instance_add_physical_device(instance, adapter, &desc); + } + + adapter->Release(); + } + } + + list->Release(); + factory->Release(); + return result; +} + diff --git a/src/microsoft/vulkan/dzn_physical_device_enum.h b/src/microsoft/vulkan/dzn_physical_device_enum.h index bc47b1616bb..9f3faf43034 100644 --- a/src/microsoft/vulkan/dzn_physical_device_enum.h +++ b/src/microsoft/vulkan/dzn_physical_device_enum.h @@ -56,6 +56,9 @@ struct dzn_physical_device_desc { VkResult dzn_enumerate_physical_devices_dxgi(struct dzn_instance *instance); +VkResult +dzn_enumerate_physical_devices_dxcore(struct dzn_instance *instance); + VkResult dzn_instance_add_physical_device(struct dzn_instance *instance, IUnknown *adapter, diff --git a/src/microsoft/vulkan/meson.build b/src/microsoft/vulkan/meson.build index fe593d96519..d22ec00d30a 100644 --- a/src/microsoft/vulkan/meson.build +++ b/src/microsoft/vulkan/meson.build @@ -34,6 +34,7 @@ libdzn_files = files( 'dzn_cmd_buffer.c', 'dzn_descriptor_set.c', 'dzn_device.c', + 'dzn_dxcore.cpp', 'dzn_image.c', 'dzn_meta.c', 'dzn_nir.c',