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 // No adapter specified or not found, pick 0 as the default
114 if (list->GetAdapterCount() > 0 && SUCCEEDED(list->GetAdapter(0, &adapter)))
115 return adapter;
116 }
117
118 return NULL;
119 }
120
121 static const char *
dxcore_get_name(struct pipe_screen * screen)122 dxcore_get_name(struct pipe_screen *screen)
123 {
124 struct d3d12_dxcore_screen *dxcore_screen = d3d12_dxcore_screen(d3d12_screen(screen));
125 static char buf[1000];
126 if (dxcore_screen->description[0] == '\0')
127 return "D3D12 (Unknown)";
128
129 snprintf(buf, sizeof(buf), "D3D12 (%s)", dxcore_screen->description);
130 return buf;
131 }
132
133 struct pipe_screen *
d3d12_create_dxcore_screen(struct sw_winsys * winsys,LUID * adapter_luid)134 d3d12_create_dxcore_screen(struct sw_winsys *winsys, LUID *adapter_luid)
135 {
136 struct d3d12_dxcore_screen *screen = CALLOC_STRUCT(d3d12_dxcore_screen);
137 if (!screen)
138 return nullptr;
139
140 screen->factory = get_dxcore_factory();
141 if (!screen->factory) {
142 FREE(screen);
143 return nullptr;
144 }
145
146 screen->adapter = choose_dxcore_adapter(screen->factory, adapter_luid);
147 if (!screen->adapter) {
148 debug_printf("D3D12: no suitable adapter\n");
149 FREE(screen);
150 return nullptr;
151 }
152
153 DXCoreHardwareID hardware_ids = {};
154 uint64_t dedicated_video_memory, dedicated_system_memory, shared_system_memory;
155 if (FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hardware_ids)) ||
156 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedAdapterMemory, &dedicated_video_memory)) ||
157 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedSystemMemory, &dedicated_system_memory)) ||
158 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::SharedSystemMemory, &shared_system_memory)) ||
159 FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DriverDescription,
160 sizeof(screen->description),
161 screen->description))) {
162 debug_printf("D3D12: failed to retrieve adapter description\n");
163 FREE(screen);
164 return nullptr;
165 }
166
167 screen->base.vendor_id = hardware_ids.vendorID;
168 screen->base.memory_size_megabytes = (dedicated_video_memory + dedicated_system_memory + shared_system_memory) >> 20;
169 screen->base.base.get_name = dxcore_get_name;
170
171 if (!d3d12_init_screen(&screen->base, winsys, screen->adapter)) {
172 debug_printf("D3D12: failed to initialize DXCore screen\n");
173 FREE(screen);
174 return nullptr;
175 }
176
177 return &screen->base.base;
178 }
179