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
27 #include "util/debug.h"
28 #include "util/u_memory.h"
29 #include "util/u_dl.h"
30
31 #include <directx/dxcore.h>
32 #include <dxguids/dxguids.h>
33
34 static IDXCoreAdapterFactory *
get_dxcore_factory()35 get_dxcore_factory()
36 {
37 typedef HRESULT(WINAPI *PFN_CREATE_DXCORE_ADAPTER_FACTORY)(REFIID riid, void **ppFactory);
38 PFN_CREATE_DXCORE_ADAPTER_FACTORY DXCoreCreateAdapterFactory;
39
40 util_dl_library *dxcore_mod = util_dl_open(UTIL_DL_PREFIX "dxcore" UTIL_DL_EXT);
41 if (!dxcore_mod) {
42 debug_printf("D3D12: failed to load DXCore.DLL\n");
43 return NULL;
44 }
45
46 DXCoreCreateAdapterFactory = (PFN_CREATE_DXCORE_ADAPTER_FACTORY)util_dl_get_proc_address(dxcore_mod, "DXCoreCreateAdapterFactory");
47 if (!DXCoreCreateAdapterFactory) {
48 debug_printf("D3D12: failed to load DXCoreCreateAdapterFactory from DXCore.DLL\n");
49 return NULL;
50 }
51
52 IDXCoreAdapterFactory *factory = NULL;
53 HRESULT hr = DXCoreCreateAdapterFactory(IID_IDXCoreAdapterFactory, (void **)&factory);
54 if (FAILED(hr)) {
55 debug_printf("D3D12: DXCoreCreateAdapterFactory failed: %08x\n", hr);
56 return NULL;
57 }
58
59 return factory;
60 }
61
62 static IDXCoreAdapter *
choose_dxcore_adapter(IDXCoreAdapterFactory * factory,LUID * adapter_luid)63 choose_dxcore_adapter(IDXCoreAdapterFactory *factory, LUID *adapter_luid)
64 {
65 IDXCoreAdapter *adapter = nullptr;
66 if (adapter_luid) {
67 if (SUCCEEDED(factory->GetAdapterByLuid(*adapter_luid, &adapter)))
68 return adapter;
69 debug_printf("D3D12: requested adapter missing, falling back to auto-detection...\n");
70 }
71
72 IDXCoreAdapterList *list = nullptr;
73 if (SUCCEEDED(factory->CreateAdapterList(1, &DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS, &list))) {
74
75 #ifndef _WIN32
76 // Pick the user selected adapter if any
77 char *adapter_name = getenv("MESA_D3D12_DEFAULT_ADAPTER_NAME");
78 if (adapter_name) {
79 for (unsigned i=0; i<list->GetAdapterCount(); i++) {
80 if (SUCCEEDED(list->GetAdapter(i, &adapter))) {
81
82 size_t desc_size;
83 if (!SUCCEEDED(adapter->GetPropertySize(DXCoreAdapterProperty::DriverDescription, &desc_size))) {
84 adapter->Release();
85 continue;
86 }
87
88 char *desc = (char*)malloc(desc_size);
89 if (!desc) {
90 adapter->Release();
91 continue;
92 }
93
94 if (!SUCCEEDED(adapter->GetProperty(DXCoreAdapterProperty::DriverDescription, desc_size, desc))) {
95 free(desc);
96 adapter->Release();
97 continue;
98 }
99
100 if (strcasestr(desc, adapter_name)) {
101 free(desc);
102 return adapter;
103 } else {
104 free(desc);
105 adapter->Release();
106 }
107 }
108 }
109 debug_printf("D3D12: Couldn't find an adapter containing the substring (%s)\n", adapter_name);
110 }
111 #endif
112
113 // Adapter not specified or not found, so pick an integrated adapter if possible
114 for (unsigned i = 0; i < list->GetAdapterCount(); ++i) {
115 if (SUCCEEDED(list->GetAdapter(i, &adapter))) {
116 bool is_integrated;
117 if (SUCCEEDED(adapter->GetProperty(DXCoreAdapterProperty::IsIntegrated, &is_integrated)) && is_integrated)
118 return adapter;
119 adapter->Release();
120 }
121 }
122
123 // No integrated GPUs, so pick the first valid one
124 if (list->GetAdapterCount() > 0 && SUCCEEDED(list->GetAdapter(0, &adapter)))
125 return adapter;
126 }
127
128 return NULL;
129 }
130
131 static const char *
dxcore_get_name(struct pipe_screen * screen)132 dxcore_get_name(struct pipe_screen *screen)
133 {
134 struct d3d12_dxcore_screen *dxcore_screen = d3d12_dxcore_screen(d3d12_screen(screen));
135 static char buf[1000];
136 if (dxcore_screen->description[0] == '\0')
137 return "D3D12 (Unknown)";
138
139 snprintf(buf, sizeof(buf), "D3D12 (%s)", dxcore_screen->description);
140 return buf;
141 }
142
143 static void
dxcore_get_memory_info(struct d3d12_screen * screen,struct d3d12_memory_info * output)144 dxcore_get_memory_info(struct d3d12_screen *screen, struct d3d12_memory_info *output)
145 {
146 struct d3d12_dxcore_screen *dxcore_screen = d3d12_dxcore_screen(screen);
147 DXCoreAdapterMemoryBudget local_info, nonlocal_info;
148 DXCoreAdapterMemoryBudgetNodeSegmentGroup local_node_segment = { 0, DXCoreSegmentGroup::Local };
149 DXCoreAdapterMemoryBudgetNodeSegmentGroup nonlocal_node_segment = { 0, DXCoreSegmentGroup::NonLocal };
150 dxcore_screen->adapter->QueryState(DXCoreAdapterState::AdapterMemoryBudget, &local_node_segment, &local_info);
151 dxcore_screen->adapter->QueryState(DXCoreAdapterState::AdapterMemoryBudget, &nonlocal_node_segment, &nonlocal_info);
152 output->budget = local_info.budget + nonlocal_info.budget;
153 output->usage = local_info.currentUsage + nonlocal_info.currentUsage;
154 }
155
156 static void
d3d12_deinit_dxcore_screen(struct d3d12_screen * dscreen)157 d3d12_deinit_dxcore_screen(struct d3d12_screen *dscreen)
158 {
159 d3d12_deinit_screen(dscreen);
160 struct d3d12_dxcore_screen *screen = d3d12_dxcore_screen(dscreen);
161 if (screen->adapter) {
162 screen->adapter->Release();
163 screen->adapter = nullptr;
164 }
165 if (screen->factory) {
166 screen->factory->Release();
167 screen->factory = nullptr;
168 }
169 }
170
171 static void
d3d12_destroy_dxcore_screen(struct pipe_screen * pscreen)172 d3d12_destroy_dxcore_screen(struct pipe_screen *pscreen)
173 {
174 struct d3d12_screen *screen = d3d12_screen(pscreen);
175 d3d12_deinit_dxcore_screen(screen);
176 d3d12_destroy_screen(screen);
177 }
178
179 static bool
d3d12_init_dxcore_screen(struct d3d12_screen * dscreen)180 d3d12_init_dxcore_screen(struct d3d12_screen *dscreen)
181 {
182 struct d3d12_dxcore_screen *screen = d3d12_dxcore_screen(dscreen);
183
184 screen->factory = get_dxcore_factory();
185 if (!screen->factory)
186 return false;
187
188 LUID *adapter_luid = &dscreen->adapter_luid;
189 if (adapter_luid->HighPart == 0 && adapter_luid->LowPart == 0)
190 adapter_luid = nullptr;
191
192 screen->adapter = choose_dxcore_adapter(screen->factory, adapter_luid);
193 if (!screen->adapter) {
194 debug_printf("D3D12: no suitable adapter\n");
195 return false;
196 }
197
198 DXCoreHardwareID hardware_ids = {};
199 uint64_t dedicated_video_memory, dedicated_system_memory, shared_system_memory;
200 if (FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hardware_ids)) ||
201 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedAdapterMemory, &dedicated_video_memory)) ||
202 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedSystemMemory, &dedicated_system_memory)) ||
203 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::SharedSystemMemory, &shared_system_memory)) ||
204 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DriverVersion, &screen->base.driver_version)) ||
205 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DriverDescription,
206 sizeof(screen->description),
207 screen->description))) {
208 debug_printf("D3D12: failed to retrieve adapter description\n");
209 return false;
210 }
211
212 screen->base.vendor_id = hardware_ids.vendorID;
213 screen->base.device_id = hardware_ids.deviceID;
214 screen->base.subsys_id = hardware_ids.subSysID;
215 screen->base.revision = hardware_ids.revision;
216 screen->base.memory_size_megabytes = (dedicated_video_memory + dedicated_system_memory + shared_system_memory) >> 20;
217 screen->base.base.get_name = dxcore_get_name;
218 screen->base.get_memory_info = dxcore_get_memory_info;
219
220 if (!d3d12_init_screen(&screen->base, screen->adapter)) {
221 debug_printf("D3D12: failed to initialize DXCore screen\n");
222 return false;
223 }
224
225 return true;
226 }
227
228 struct pipe_screen *
d3d12_create_dxcore_screen(struct sw_winsys * winsys,LUID * adapter_luid)229 d3d12_create_dxcore_screen(struct sw_winsys *winsys, LUID *adapter_luid)
230 {
231 struct d3d12_dxcore_screen *screen = CALLOC_STRUCT(d3d12_dxcore_screen);
232 if (!screen)
233 return nullptr;
234
235 d3d12_init_screen_base(&screen->base, winsys, adapter_luid);
236 screen->base.base.destroy = d3d12_destroy_dxcore_screen;
237 screen->base.init = d3d12_init_dxcore_screen;
238 screen->base.deinit = d3d12_deinit_dxcore_screen;
239
240 if (!d3d12_init_dxcore_screen(&screen->base)) {
241 d3d12_destroy_dxcore_screen(&screen->base.base);
242 return nullptr;
243 }
244
245 return &screen->base.base;
246 }
247