• 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 #include "d3d12_screen.h"
25 #include "d3d12_public.h"
26 #include "d3d12_debug.h"
27 
28 #include "util/debug.h"
29 #include "util/u_memory.h"
30 #include "util/u_dl.h"
31 
32 #include <dxgi1_4.h>
33 
34 static IDXGIFactory4 *
get_dxgi_factory()35 get_dxgi_factory()
36 {
37    static const GUID IID_IDXGIFactory4 = {
38       0x1bc6ea02, 0xef36, 0x464f,
39       { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a }
40    };
41 
42    util_dl_library *dxgi_mod = util_dl_open(UTIL_DL_PREFIX "dxgi" UTIL_DL_EXT);
43    if (!dxgi_mod) {
44       debug_printf("D3D12: failed to load DXGI.DLL\n");
45       return NULL;
46    }
47 
48    typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory);
49    PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2;
50 
51    CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)util_dl_get_proc_address(dxgi_mod, "CreateDXGIFactory2");
52    if (!CreateDXGIFactory2) {
53       debug_printf("D3D12: failed to load CreateDXGIFactory2 from DXGI.DLL\n");
54       return NULL;
55    }
56 
57    UINT flags = 0;
58 #ifndef DEBUG
59    if (d3d12_debug & D3D12_DEBUG_DEBUG_LAYER)
60 #endif
61       flags |= DXGI_CREATE_FACTORY_DEBUG;
62 
63    IDXGIFactory4 *factory = NULL;
64    HRESULT hr = CreateDXGIFactory2(flags, IID_IDXGIFactory4, (void **)&factory);
65    if (FAILED(hr)) {
66       debug_printf("D3D12: CreateDXGIFactory2 failed: %08x\n", hr);
67       return NULL;
68    }
69 
70    return factory;
71 }
72 
73 static IDXGIAdapter3 *
choose_dxgi_adapter(IDXGIFactory4 * factory,LUID * adapter)74 choose_dxgi_adapter(IDXGIFactory4 *factory, LUID *adapter)
75 {
76    IDXGIAdapter3 *ret;
77    if (adapter) {
78       if (SUCCEEDED(factory->EnumAdapterByLuid(*adapter,
79                                                IID_PPV_ARGS(&ret))))
80          return ret;
81       debug_printf("D3D12: requested adapter missing, falling back to auto-detection...\n");
82    }
83 
84    bool want_warp = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);
85    if (want_warp) {
86       if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&ret))))
87          return ret;
88       debug_printf("D3D12: failed to enum warp adapter\n");
89       return NULL;
90    }
91 
92    // The first adapter is the default
93    IDXGIAdapter1 *adapter1;
94    if (SUCCEEDED(factory->EnumAdapters1(0, &adapter1))) {
95       HRESULT hr = adapter1->QueryInterface(&ret);
96       adapter1->Release();
97       if (SUCCEEDED(hr))
98          return ret;
99    }
100 
101    return NULL;
102 }
103 
104 static const char *
dxgi_get_name(struct pipe_screen * screen)105 dxgi_get_name(struct pipe_screen *screen)
106 {
107    struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(d3d12_screen(screen));
108    static char buf[1000];
109    if (dxgi_screen->description[0] == L'\0')
110       return "D3D12 (Unknown)";
111 
112    snprintf(buf, sizeof(buf), "D3D12 (%S)", dxgi_screen->description);
113    return buf;
114 }
115 
116 static void
dxgi_get_memory_info(struct d3d12_screen * screen,struct d3d12_memory_info * output)117 dxgi_get_memory_info(struct d3d12_screen *screen, struct d3d12_memory_info *output)
118 {
119    struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(screen);
120    DXGI_QUERY_VIDEO_MEMORY_INFO local_info, nonlocal_info;
121    dxgi_screen->adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &local_info);
122    dxgi_screen->adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &nonlocal_info);
123    output->budget = local_info.Budget + nonlocal_info.Budget;
124    output->usage = local_info.CurrentUsage + nonlocal_info.CurrentUsage;
125 }
126 
127 static void
d3d12_deinit_dxgi_screen(struct d3d12_screen * dscreen)128 d3d12_deinit_dxgi_screen(struct d3d12_screen *dscreen)
129 {
130    d3d12_deinit_screen(dscreen);
131    struct d3d12_dxgi_screen *screen = d3d12_dxgi_screen(dscreen);
132    if (screen->adapter) {
133       screen->adapter->Release();
134       screen->adapter = nullptr;
135    }
136    if (screen->factory) {
137       screen->factory->Release();
138       screen->factory = nullptr;
139    }
140 }
141 
142 static void
d3d12_destroy_dxgi_screen(struct pipe_screen * pscreen)143 d3d12_destroy_dxgi_screen(struct pipe_screen *pscreen)
144 {
145    struct d3d12_screen *screen = d3d12_screen(pscreen);
146    d3d12_deinit_dxgi_screen(screen);
147    d3d12_destroy_screen(screen);
148 }
149 
150 static bool
d3d12_init_dxgi_screen(struct d3d12_screen * dscreen)151 d3d12_init_dxgi_screen(struct d3d12_screen *dscreen)
152 {
153    struct d3d12_dxgi_screen *screen = d3d12_dxgi_screen(dscreen);
154    screen->factory = get_dxgi_factory();
155    if (!screen->factory)
156       return false;
157 
158    LUID *adapter_luid = &dscreen->adapter_luid;
159    if (adapter_luid->HighPart == 0 && adapter_luid->LowPart == 0)
160       adapter_luid = nullptr;
161 
162    screen->adapter = choose_dxgi_adapter(screen->factory, adapter_luid);
163    if (!screen->adapter) {
164       debug_printf("D3D12: no suitable adapter\n");
165       return false;
166    }
167 
168    DXGI_ADAPTER_DESC1 adapter_desc = {};
169    if (FAILED(screen->adapter->GetDesc1(&adapter_desc))) {
170       debug_printf("D3D12: failed to retrieve adapter description\n");
171       return false;
172    }
173 
174    LARGE_INTEGER driver_version;
175    screen->adapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &driver_version);
176    screen->base.driver_version = driver_version.QuadPart;
177 
178    screen->base.vendor_id = adapter_desc.VendorId;
179    screen->base.device_id = adapter_desc.DeviceId;
180    screen->base.subsys_id = adapter_desc.SubSysId;
181    screen->base.revision = adapter_desc.Revision;
182    // Note: memory sizes in bytes, but stored in size_t, so may be capped at 4GB.
183    // In that case, adding before conversion to MB can easily overflow.
184    screen->base.memory_size_megabytes = (adapter_desc.DedicatedVideoMemory >> 20) +
185                                         (adapter_desc.DedicatedSystemMemory >> 20) +
186                                         (adapter_desc.SharedSystemMemory >> 20);
187    wcsncpy(screen->description, adapter_desc.Description, ARRAY_SIZE(screen->description));
188    screen->base.base.get_name = dxgi_get_name;
189    screen->base.get_memory_info = dxgi_get_memory_info;
190 
191    if (!d3d12_init_screen(&screen->base, screen->adapter)) {
192       debug_printf("D3D12: failed to initialize DXGI screen\n");
193       return false;
194    }
195 
196    return true;
197 }
198 
199 struct pipe_screen *
d3d12_create_dxgi_screen(struct sw_winsys * winsys,LUID * adapter_luid)200 d3d12_create_dxgi_screen(struct sw_winsys *winsys, LUID *adapter_luid)
201 {
202    struct d3d12_dxgi_screen *screen = CALLOC_STRUCT(d3d12_dxgi_screen);
203    if (!screen)
204       return nullptr;
205 
206    d3d12_init_screen_base(&screen->base, winsys, adapter_luid);
207    screen->base.base.destroy = d3d12_destroy_dxgi_screen;
208    screen->base.init = d3d12_init_dxgi_screen;
209    screen->base.deinit = d3d12_deinit_dxgi_screen;
210 
211    if (!d3d12_init_dxgi_screen(&screen->base)) {
212       d3d12_destroy_dxgi_screen(&screen->base.base);
213       return nullptr;
214    }
215 
216    return &screen->base.base;
217 }
218