From 49967ea306aec0197aaeb0f9ed8d356ac7b149c0 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Fri, 1 Jul 2022 09:39:03 -0700 Subject: [PATCH] dzn: Move DXGI code to a separate file and only build it on Windows The prototypes for physical device enumeration are moved to a new dedicated header so that it can be included from a DXCore path, which will C++, in the next commit Reviewed-by: Boris Brezillon Reviewed-by: Bill Kristiansen Part-of: --- src/microsoft/vulkan/dzn_device.c | 38 +------ src/microsoft/vulkan/dzn_dxgi.c | 102 ++++++++++++++++++ src/microsoft/vulkan/dzn_dxgi.h | 34 ++++++ .../vulkan/dzn_physical_device_enum.h | 68 ++++++++++++ src/microsoft/vulkan/dzn_private.h | 18 +--- src/microsoft/vulkan/dzn_util.c | 38 ------- src/microsoft/vulkan/meson.build | 1 + 7 files changed, 211 insertions(+), 88 deletions(-) create mode 100644 src/microsoft/vulkan/dzn_dxgi.c create mode 100644 src/microsoft/vulkan/dzn_dxgi.h create mode 100644 src/microsoft/vulkan/dzn_physical_device_enum.h diff --git a/src/microsoft/vulkan/dzn_device.c b/src/microsoft/vulkan/dzn_device.c index 08109dfe32c..46b81f8f5a5 100644 --- a/src/microsoft/vulkan/dzn_device.c +++ b/src/microsoft/vulkan/dzn_device.c @@ -49,6 +49,7 @@ #ifdef _WIN32 #include #include +#include "dzn_dxgi.h" #endif #include @@ -994,7 +995,7 @@ dzn_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice, }; } -static VkResult +VkResult dzn_instance_add_physical_device(struct dzn_instance *instance, IUnknown *adapter, const struct dzn_physical_device_desc *desc) @@ -1014,40 +1015,11 @@ dzn_EnumeratePhysicalDevices(VkInstance inst, VK_FROM_HANDLE(dzn_instance, instance, inst); if (!instance->physical_devices_enumerated) { - IDXGIFactory4 *factory = dxgi_get_factory(false); - IDXGIAdapter1 *adapter = NULL; - VkResult result = VK_SUCCESS; - for (UINT i = 0; SUCCEEDED(IDXGIFactory4_EnumAdapters1(factory, i, &adapter)); ++i) { - DXGI_ADAPTER_DESC1 dxgi_desc; - IDXGIAdapter1_GetDesc1(adapter, &dxgi_desc); - - struct dzn_physical_device_desc desc = { - .adapter_luid = dxgi_desc.AdapterLuid, - .vendor_id = dxgi_desc.VendorId, - .device_id = dxgi_desc.DeviceId, - .subsys_id = dxgi_desc.SubSysId, - .revision = dxgi_desc.Revision, - .shared_system_memory = dxgi_desc.SharedSystemMemory, - .dedicated_system_memory = dxgi_desc.DedicatedSystemMemory, - .dedicated_video_memory = dxgi_desc.DedicatedVideoMemory, - .is_warp = (dxgi_desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) != 0, - }; - WideCharToMultiByte(CP_ACP, 0, dxgi_desc.Description, ARRAYSIZE(dxgi_desc.Description), - desc.description, ARRAYSIZE(desc.description), NULL, NULL); - - result = - dzn_instance_add_physical_device(instance, (IUnknown *)adapter, &desc); - - IDXGIAdapter1_Release(adapter); - - if (result != VK_SUCCESS) - break; - } - - IDXGIFactory4_Release(factory); - +#ifdef _WIN32 + VkResult result = dzn_enumerate_physical_devices_dxgi(instance); if (result != VK_SUCCESS) return result; +#endif } VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, diff --git a/src/microsoft/vulkan/dzn_dxgi.c b/src/microsoft/vulkan/dzn_dxgi.c new file mode 100644 index 00000000000..519396684a0 --- /dev/null +++ b/src/microsoft/vulkan/dzn_dxgi.c @@ -0,0 +1,102 @@ +/* + * 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. + */ + +#define COBJMACROS +#include "dzn_physical_device_enum.h" +#include "dzn_dxgi.h" + +#include "log.h" + +VkResult +dzn_enumerate_physical_devices_dxgi(struct dzn_instance *instance) +{ + IDXGIFactory4 *factory = dxgi_get_factory(false); + IDXGIAdapter1 *adapter = NULL; + VkResult result = VK_SUCCESS; + for (UINT i = 0; SUCCEEDED(IDXGIFactory4_EnumAdapters1(factory, i, &adapter)); ++i) { + DXGI_ADAPTER_DESC1 dxgi_desc; + IDXGIAdapter1_GetDesc1(adapter, &dxgi_desc); + + struct dzn_physical_device_desc desc = { + .adapter_luid = dxgi_desc.AdapterLuid, + .vendor_id = dxgi_desc.VendorId, + .device_id = dxgi_desc.DeviceId, + .subsys_id = dxgi_desc.SubSysId, + .revision = dxgi_desc.Revision, + .shared_system_memory = dxgi_desc.SharedSystemMemory, + .dedicated_system_memory = dxgi_desc.DedicatedSystemMemory, + .dedicated_video_memory = dxgi_desc.DedicatedVideoMemory, + .is_warp = (dxgi_desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) != 0, + }; + WideCharToMultiByte(CP_ACP, 0, dxgi_desc.Description, ARRAYSIZE(dxgi_desc.Description), + desc.description, ARRAYSIZE(desc.description), NULL, NULL); + result = + dzn_instance_add_physical_device(instance, (IUnknown *)adapter, &desc); + + IDXGIAdapter1_Release(adapter); + + if (result != VK_SUCCESS) + break; + } + + IDXGIFactory4_Release(factory); + + return result; +} + +IDXGIFactory4 * +dxgi_get_factory(bool debug) +{ + static const GUID IID_IDXGIFactory4 = { + 0x1bc6ea02, 0xef36, 0x464f, + { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a } + }; + + HMODULE dxgi_mod = LoadLibraryA("DXGI.DLL"); + if (!dxgi_mod) { + mesa_loge("failed to load DXGI.DLL\n"); + return NULL; + } + + typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory); + PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2; + + CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)GetProcAddress(dxgi_mod, "CreateDXGIFactory2"); + if (!CreateDXGIFactory2) { + mesa_loge("failed to load CreateDXGIFactory2 from DXGI.DLL\n"); + return NULL; + } + + UINT flags = 0; + if (debug) + flags |= DXGI_CREATE_FACTORY_DEBUG; + + IDXGIFactory4 *factory; + HRESULT hr = CreateDXGIFactory2(flags, &IID_IDXGIFactory4, (void **)&factory); + if (FAILED(hr)) { + mesa_loge("CreateDXGIFactory2 failed: %08x\n", (int32_t)hr); + return NULL; + } + + return factory; +} diff --git a/src/microsoft/vulkan/dzn_dxgi.h b/src/microsoft/vulkan/dzn_dxgi.h new file mode 100644 index 00000000000..f0a477b33f6 --- /dev/null +++ b/src/microsoft/vulkan/dzn_dxgi.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef DZN_DXGI_H +#define DZN_DXGI_H + +#include + +struct dzn_instance; + +IDXGIFactory4 * +dxgi_get_factory(bool debug); + +#endif /* DZN_DXGI_H */ \ No newline at end of file diff --git a/src/microsoft/vulkan/dzn_physical_device_enum.h b/src/microsoft/vulkan/dzn_physical_device_enum.h new file mode 100644 index 00000000000..bc47b1616bb --- /dev/null +++ b/src/microsoft/vulkan/dzn_physical_device_enum.h @@ -0,0 +1,68 @@ +/* + * 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. + */ + +#ifndef DZN_PHYSICAL_DEVICE_ENUM_H +#define DZN_PHYSICAL_DEVICE_ENUM_H + +#include + +#include + +#ifndef _WIN32 +#include +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct dzn_instance; + +struct dzn_physical_device_desc { + LUID adapter_luid; + uint32_t vendor_id; + uint32_t device_id; + uint32_t subsys_id; + uint32_t revision; + uint64_t shared_system_memory; + uint64_t dedicated_system_memory; + uint64_t dedicated_video_memory; + bool is_warp; + char description[128]; +}; + +VkResult +dzn_enumerate_physical_devices_dxgi(struct dzn_instance *instance); + +VkResult +dzn_instance_add_physical_device(struct dzn_instance *instance, + IUnknown *adapter, + const struct dzn_physical_device_desc *desc); + +#ifdef __cplusplus +} +#endif + +#endif /* DZN_PHYSICAL_DEVICE_ENUM_H */ diff --git a/src/microsoft/vulkan/dzn_private.h b/src/microsoft/vulkan/dzn_private.h index ee21053f764..66f65e35bf4 100644 --- a/src/microsoft/vulkan/dzn_private.h +++ b/src/microsoft/vulkan/dzn_private.h @@ -53,13 +53,13 @@ #include "dzn_entrypoints.h" #include "dzn_nir.h" +#include "dzn_physical_device_enum.h" #include #include #define D3D12_IGNORE_SDK_LAYERS #include -#include #include #include "spirv_to_dxil.h" @@ -175,19 +175,6 @@ dzn_meta_blits_get_context(struct dzn_device *device, #define MAX_SYNC_TYPES 3 #define MAX_QUEUE_FAMILIES 3 -struct dzn_physical_device_desc { - LUID adapter_luid; - uint32_t vendor_id; - uint32_t device_id; - uint32_t subsys_id; - uint32_t revision; - uint64_t shared_system_memory; - uint64_t dedicated_system_memory; - uint64_t dedicated_video_memory; - bool is_warp; - char description[128]; -}; - struct dzn_physical_device { struct vk_physical_device vk; struct list_head link; @@ -234,9 +221,6 @@ dzn_physical_device_get_mem_type_mask_for_resource(const struct dzn_physical_dev #define dzn_debug_ignored_stype(sType) \ mesa_logd("%s: ignored VkStructureType %u\n", __func__, (sType)) -IDXGIFactory4 * -dxgi_get_factory(bool debug); - PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE d3d12_get_serialize_root_sig(void); diff --git a/src/microsoft/vulkan/dzn_util.c b/src/microsoft/vulkan/dzn_util.c index 3e1c84d12c5..6b81caddc6a 100644 --- a/src/microsoft/vulkan/dzn_util.c +++ b/src/microsoft/vulkan/dzn_util.c @@ -34,7 +34,6 @@ #include #include -#include static const DXGI_FORMAT formats[PIPE_FORMAT_COUNT] = { #define MAP_FORMAT_NORM(FMT) \ @@ -297,43 +296,6 @@ dzn_translate_rect(D3D12_RECT *out, out->bottom = in->offset.y + in->extent.height; } -IDXGIFactory4 * -dxgi_get_factory(bool debug) -{ - static const GUID IID_IDXGIFactory4 = { - 0x1bc6ea02, 0xef36, 0x464f, - { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a } - }; - - HMODULE dxgi_mod = LoadLibraryA("DXGI.DLL"); - if (!dxgi_mod) { - mesa_loge("failed to load DXGI.DLL\n"); - return NULL; - } - - typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory); - PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2; - - CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)GetProcAddress(dxgi_mod, "CreateDXGIFactory2"); - if (!CreateDXGIFactory2) { - mesa_loge("failed to load CreateDXGIFactory2 from DXGI.DLL\n"); - return NULL; - } - - UINT flags = 0; - if (debug) - flags |= DXGI_CREATE_FACTORY_DEBUG; - - IDXGIFactory4 *factory; - HRESULT hr = CreateDXGIFactory2(flags, &IID_IDXGIFactory4, (void **)&factory); - if (FAILED(hr)) { - mesa_loge("CreateDXGIFactory2 failed: %08x\n", (int32_t)hr); - return NULL; - } - - return factory; -} - static ID3D12Debug * get_debug_interface() { diff --git a/src/microsoft/vulkan/meson.build b/src/microsoft/vulkan/meson.build index 74da9673e98..fe593d96519 100644 --- a/src/microsoft/vulkan/meson.build +++ b/src/microsoft/vulkan/meson.build @@ -59,6 +59,7 @@ dzn_flags = [ ] if with_platform_windows dzn_flags += '-DVK_USE_PLATFORM_WIN32_KHR' + libdzn_files += files('dzn_dxgi.c') endif if cc.get_argument_syntax() != 'msvc'