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 <boris.brezillon@collabora.com>
Reviewed-by: Bill Kristiansen <billkris@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17340>
This commit is contained in:
Jesse Natalie 2022-07-01 09:39:03 -07:00 committed by Marge Bot
parent 68c65de264
commit 49967ea306
7 changed files with 211 additions and 88 deletions

View File

@ -49,6 +49,7 @@
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#include "dzn_dxgi.h"
#endif
#include <directx/d3d12sdklayers.h>
@ -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,

View File

@ -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;
}

View File

@ -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 <dxgi1_4.h>
struct dzn_instance;
IDXGIFactory4 *
dxgi_get_factory(bool debug);
#endif /* DZN_DXGI_H */

View File

@ -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 <vulkan/vulkan.h>
#include <unknwn.h>
#ifndef _WIN32
#include <wsl/winadapter.h>
#endif
#include <stdbool.h>
#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 */

View File

@ -53,13 +53,13 @@
#include "dzn_entrypoints.h"
#include "dzn_nir.h"
#include "dzn_physical_device_enum.h"
#include <vulkan/vulkan.h>
#include <vulkan/vk_icd.h>
#define D3D12_IGNORE_SDK_LAYERS
#include <unknwn.h>
#include <dxgi1_4.h>
#include <directx/d3d12.h>
#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);

View File

@ -34,7 +34,6 @@
#include <directx/d3d12sdklayers.h>
#include <util/u_dl.h>
#include <dxgi1_4.h>
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()
{

View File

@ -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'