1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#define COBJMACROS
25bf215546Sopenharmony_ci#include "dzn_physical_device_enum.h"
26bf215546Sopenharmony_ci#include "dzn_dxgi.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "log.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ciVkResult
31bf215546Sopenharmony_cidzn_enumerate_physical_devices_dxgi(struct dzn_instance *instance)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci   IDXGIFactory4 *factory = dxgi_get_factory(false);
34bf215546Sopenharmony_ci   IDXGIAdapter1 *adapter = NULL;
35bf215546Sopenharmony_ci   VkResult result = VK_SUCCESS;
36bf215546Sopenharmony_ci   for (UINT i = 0; SUCCEEDED(IDXGIFactory4_EnumAdapters1(factory, i, &adapter)); ++i) {
37bf215546Sopenharmony_ci      DXGI_ADAPTER_DESC1 dxgi_desc;
38bf215546Sopenharmony_ci      IDXGIAdapter1_GetDesc1(adapter, &dxgi_desc);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci      struct dzn_physical_device_desc desc = {
41bf215546Sopenharmony_ci         .adapter_luid = dxgi_desc.AdapterLuid,
42bf215546Sopenharmony_ci         .vendor_id = dxgi_desc.VendorId,
43bf215546Sopenharmony_ci         .device_id = dxgi_desc.DeviceId,
44bf215546Sopenharmony_ci         .subsys_id = dxgi_desc.SubSysId,
45bf215546Sopenharmony_ci         .revision = dxgi_desc.Revision,
46bf215546Sopenharmony_ci         .shared_system_memory = dxgi_desc.SharedSystemMemory,
47bf215546Sopenharmony_ci         .dedicated_system_memory = dxgi_desc.DedicatedSystemMemory,
48bf215546Sopenharmony_ci         .dedicated_video_memory = dxgi_desc.DedicatedVideoMemory,
49bf215546Sopenharmony_ci         .is_warp = (dxgi_desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) != 0,
50bf215546Sopenharmony_ci      };
51bf215546Sopenharmony_ci      WideCharToMultiByte(CP_ACP, 0, dxgi_desc.Description, ARRAYSIZE(dxgi_desc.Description),
52bf215546Sopenharmony_ci                          desc.description, ARRAYSIZE(desc.description), NULL, NULL);
53bf215546Sopenharmony_ci      result =
54bf215546Sopenharmony_ci         dzn_instance_add_physical_device(instance, (IUnknown *)adapter, &desc);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci      IDXGIAdapter1_Release(adapter);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      if (result != VK_SUCCESS)
59bf215546Sopenharmony_ci         break;
60bf215546Sopenharmony_ci    }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   IDXGIFactory4_Release(factory);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   return result;
65bf215546Sopenharmony_ci}
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ciIDXGIFactory4 *
68bf215546Sopenharmony_cidxgi_get_factory(bool debug)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   static const GUID IID_IDXGIFactory4 = {
71bf215546Sopenharmony_ci      0x1bc6ea02, 0xef36, 0x464f,
72bf215546Sopenharmony_ci      { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a }
73bf215546Sopenharmony_ci   };
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   HMODULE dxgi_mod = LoadLibraryA("DXGI.DLL");
76bf215546Sopenharmony_ci   if (!dxgi_mod) {
77bf215546Sopenharmony_ci      mesa_loge("failed to load DXGI.DLL\n");
78bf215546Sopenharmony_ci      return NULL;
79bf215546Sopenharmony_ci   }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory);
82bf215546Sopenharmony_ci   PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)GetProcAddress(dxgi_mod, "CreateDXGIFactory2");
85bf215546Sopenharmony_ci   if (!CreateDXGIFactory2) {
86bf215546Sopenharmony_ci      mesa_loge("failed to load CreateDXGIFactory2 from DXGI.DLL\n");
87bf215546Sopenharmony_ci      return NULL;
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   UINT flags = 0;
91bf215546Sopenharmony_ci   if (debug)
92bf215546Sopenharmony_ci      flags |= DXGI_CREATE_FACTORY_DEBUG;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   IDXGIFactory4 *factory;
95bf215546Sopenharmony_ci   HRESULT hr = CreateDXGIFactory2(flags, &IID_IDXGIFactory4, (void **)&factory);
96bf215546Sopenharmony_ci   if (FAILED(hr)) {
97bf215546Sopenharmony_ci      mesa_loge("CreateDXGIFactory2 failed: %08x\n", (int32_t)hr);
98bf215546Sopenharmony_ci      return NULL;
99bf215546Sopenharmony_ci   }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   return factory;
102bf215546Sopenharmony_ci}
103