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