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 34static IDXGIFactory4 * 35get_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 73static IDXGIAdapter1 * 74choose_dxgi_adapter(IDXGIFactory4 *factory, LUID *adapter) 75{ 76 IDXGIAdapter1 *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 if (SUCCEEDED(factory->EnumAdapters1(0, &ret))) 94 return ret; 95 96 return NULL; 97} 98 99static const char * 100dxgi_get_name(struct pipe_screen *screen) 101{ 102 struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(d3d12_screen(screen)); 103 static char buf[1000]; 104 if (dxgi_screen->description[0] == L'\0') 105 return "D3D12 (Unknown)"; 106 107 snprintf(buf, sizeof(buf), "D3D12 (%S)", dxgi_screen->description); 108 return buf; 109} 110 111struct pipe_screen * 112d3d12_create_dxgi_screen(struct sw_winsys *winsys, LUID *adapter_luid) 113{ 114 struct d3d12_dxgi_screen *screen = CALLOC_STRUCT(d3d12_dxgi_screen); 115 if (!screen) 116 return nullptr; 117 118 screen->factory = get_dxgi_factory(); 119 if (!screen->factory) { 120 FREE(screen); 121 return nullptr; 122 } 123 124 screen->adapter = choose_dxgi_adapter(screen->factory, adapter_luid); 125 if (!screen->adapter) { 126 debug_printf("D3D12: no suitable adapter\n"); 127 FREE(screen); 128 return nullptr; 129 } 130 131 DXGI_ADAPTER_DESC1 adapter_desc = {}; 132 if (FAILED(screen->adapter->GetDesc1(&adapter_desc))) { 133 debug_printf("D3D12: failed to retrieve adapter description\n"); 134 FREE(screen); 135 return nullptr; 136 } 137 138 screen->base.vendor_id = adapter_desc.VendorId; 139 // Note: memory sizes in bytes, but stored in size_t, so may be capped at 4GB. 140 // In that case, adding before conversion to MB can easily overflow. 141 screen->base.memory_size_megabytes = (adapter_desc.DedicatedVideoMemory >> 20) + 142 (adapter_desc.DedicatedSystemMemory >> 20) + 143 (adapter_desc.SharedSystemMemory >> 20); 144 wcsncpy(screen->description, adapter_desc.Description, ARRAY_SIZE(screen->description)); 145 screen->base.base.get_name = dxgi_get_name; 146 147 if (!d3d12_init_screen(&screen->base, winsys, screen->adapter)) { 148 debug_printf("D3D12: failed to initialize DXGI screen\n"); 149 FREE(screen); 150 return nullptr; 151 } 152 153 return &screen->base.base; 154} 155