1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "d3d12_screen.h" 25bf215546Sopenharmony_ci#include "d3d12_public.h" 26bf215546Sopenharmony_ci#include "d3d12_debug.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "util/debug.h" 29bf215546Sopenharmony_ci#include "util/u_memory.h" 30bf215546Sopenharmony_ci#include "util/u_dl.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include <dxgi1_4.h> 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cistatic IDXGIFactory4 * 35bf215546Sopenharmony_ciget_dxgi_factory() 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci static const GUID IID_IDXGIFactory4 = { 38bf215546Sopenharmony_ci 0x1bc6ea02, 0xef36, 0x464f, 39bf215546Sopenharmony_ci { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a } 40bf215546Sopenharmony_ci }; 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci util_dl_library *dxgi_mod = util_dl_open(UTIL_DL_PREFIX "dxgi" UTIL_DL_EXT); 43bf215546Sopenharmony_ci if (!dxgi_mod) { 44bf215546Sopenharmony_ci debug_printf("D3D12: failed to load DXGI.DLL\n"); 45bf215546Sopenharmony_ci return NULL; 46bf215546Sopenharmony_ci } 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory); 49bf215546Sopenharmony_ci PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)util_dl_get_proc_address(dxgi_mod, "CreateDXGIFactory2"); 52bf215546Sopenharmony_ci if (!CreateDXGIFactory2) { 53bf215546Sopenharmony_ci debug_printf("D3D12: failed to load CreateDXGIFactory2 from DXGI.DLL\n"); 54bf215546Sopenharmony_ci return NULL; 55bf215546Sopenharmony_ci } 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci UINT flags = 0; 58bf215546Sopenharmony_ci#ifndef DEBUG 59bf215546Sopenharmony_ci if (d3d12_debug & D3D12_DEBUG_DEBUG_LAYER) 60bf215546Sopenharmony_ci#endif 61bf215546Sopenharmony_ci flags |= DXGI_CREATE_FACTORY_DEBUG; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci IDXGIFactory4 *factory = NULL; 64bf215546Sopenharmony_ci HRESULT hr = CreateDXGIFactory2(flags, IID_IDXGIFactory4, (void **)&factory); 65bf215546Sopenharmony_ci if (FAILED(hr)) { 66bf215546Sopenharmony_ci debug_printf("D3D12: CreateDXGIFactory2 failed: %08x\n", hr); 67bf215546Sopenharmony_ci return NULL; 68bf215546Sopenharmony_ci } 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci return factory; 71bf215546Sopenharmony_ci} 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_cistatic IDXGIAdapter3 * 74bf215546Sopenharmony_cichoose_dxgi_adapter(IDXGIFactory4 *factory, LUID *adapter) 75bf215546Sopenharmony_ci{ 76bf215546Sopenharmony_ci IDXGIAdapter3 *ret; 77bf215546Sopenharmony_ci if (adapter) { 78bf215546Sopenharmony_ci if (SUCCEEDED(factory->EnumAdapterByLuid(*adapter, 79bf215546Sopenharmony_ci IID_PPV_ARGS(&ret)))) 80bf215546Sopenharmony_ci return ret; 81bf215546Sopenharmony_ci debug_printf("D3D12: requested adapter missing, falling back to auto-detection...\n"); 82bf215546Sopenharmony_ci } 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci bool want_warp = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false); 85bf215546Sopenharmony_ci if (want_warp) { 86bf215546Sopenharmony_ci if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&ret)))) 87bf215546Sopenharmony_ci return ret; 88bf215546Sopenharmony_ci debug_printf("D3D12: failed to enum warp adapter\n"); 89bf215546Sopenharmony_ci return NULL; 90bf215546Sopenharmony_ci } 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci // The first adapter is the default 93bf215546Sopenharmony_ci IDXGIAdapter1 *adapter1; 94bf215546Sopenharmony_ci if (SUCCEEDED(factory->EnumAdapters1(0, &adapter1))) { 95bf215546Sopenharmony_ci HRESULT hr = adapter1->QueryInterface(&ret); 96bf215546Sopenharmony_ci adapter1->Release(); 97bf215546Sopenharmony_ci if (SUCCEEDED(hr)) 98bf215546Sopenharmony_ci return ret; 99bf215546Sopenharmony_ci } 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci return NULL; 102bf215546Sopenharmony_ci} 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_cistatic const char * 105bf215546Sopenharmony_cidxgi_get_name(struct pipe_screen *screen) 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(d3d12_screen(screen)); 108bf215546Sopenharmony_ci static char buf[1000]; 109bf215546Sopenharmony_ci if (dxgi_screen->description[0] == L'\0') 110bf215546Sopenharmony_ci return "D3D12 (Unknown)"; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci snprintf(buf, sizeof(buf), "D3D12 (%S)", dxgi_screen->description); 113bf215546Sopenharmony_ci return buf; 114bf215546Sopenharmony_ci} 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_cistatic void 117bf215546Sopenharmony_cidxgi_get_memory_info(struct d3d12_screen *screen, struct d3d12_memory_info *output) 118bf215546Sopenharmony_ci{ 119bf215546Sopenharmony_ci struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(screen); 120bf215546Sopenharmony_ci DXGI_QUERY_VIDEO_MEMORY_INFO local_info, nonlocal_info; 121bf215546Sopenharmony_ci dxgi_screen->adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &local_info); 122bf215546Sopenharmony_ci dxgi_screen->adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &nonlocal_info); 123bf215546Sopenharmony_ci output->budget = local_info.Budget + nonlocal_info.Budget; 124bf215546Sopenharmony_ci output->usage = local_info.CurrentUsage + nonlocal_info.CurrentUsage; 125bf215546Sopenharmony_ci} 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_cistatic void 128bf215546Sopenharmony_cid3d12_deinit_dxgi_screen(struct d3d12_screen *dscreen) 129bf215546Sopenharmony_ci{ 130bf215546Sopenharmony_ci d3d12_deinit_screen(dscreen); 131bf215546Sopenharmony_ci struct d3d12_dxgi_screen *screen = d3d12_dxgi_screen(dscreen); 132bf215546Sopenharmony_ci if (screen->adapter) { 133bf215546Sopenharmony_ci screen->adapter->Release(); 134bf215546Sopenharmony_ci screen->adapter = nullptr; 135bf215546Sopenharmony_ci } 136bf215546Sopenharmony_ci if (screen->factory) { 137bf215546Sopenharmony_ci screen->factory->Release(); 138bf215546Sopenharmony_ci screen->factory = nullptr; 139bf215546Sopenharmony_ci } 140bf215546Sopenharmony_ci} 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_cistatic void 143bf215546Sopenharmony_cid3d12_destroy_dxgi_screen(struct pipe_screen *pscreen) 144bf215546Sopenharmony_ci{ 145bf215546Sopenharmony_ci struct d3d12_screen *screen = d3d12_screen(pscreen); 146bf215546Sopenharmony_ci d3d12_deinit_dxgi_screen(screen); 147bf215546Sopenharmony_ci d3d12_destroy_screen(screen); 148bf215546Sopenharmony_ci} 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_cistatic bool 151bf215546Sopenharmony_cid3d12_init_dxgi_screen(struct d3d12_screen *dscreen) 152bf215546Sopenharmony_ci{ 153bf215546Sopenharmony_ci struct d3d12_dxgi_screen *screen = d3d12_dxgi_screen(dscreen); 154bf215546Sopenharmony_ci screen->factory = get_dxgi_factory(); 155bf215546Sopenharmony_ci if (!screen->factory) 156bf215546Sopenharmony_ci return false; 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci LUID *adapter_luid = &dscreen->adapter_luid; 159bf215546Sopenharmony_ci if (adapter_luid->HighPart == 0 && adapter_luid->LowPart == 0) 160bf215546Sopenharmony_ci adapter_luid = nullptr; 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci screen->adapter = choose_dxgi_adapter(screen->factory, adapter_luid); 163bf215546Sopenharmony_ci if (!screen->adapter) { 164bf215546Sopenharmony_ci debug_printf("D3D12: no suitable adapter\n"); 165bf215546Sopenharmony_ci return false; 166bf215546Sopenharmony_ci } 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci DXGI_ADAPTER_DESC1 adapter_desc = {}; 169bf215546Sopenharmony_ci if (FAILED(screen->adapter->GetDesc1(&adapter_desc))) { 170bf215546Sopenharmony_ci debug_printf("D3D12: failed to retrieve adapter description\n"); 171bf215546Sopenharmony_ci return false; 172bf215546Sopenharmony_ci } 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci LARGE_INTEGER driver_version; 175bf215546Sopenharmony_ci screen->adapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &driver_version); 176bf215546Sopenharmony_ci screen->base.driver_version = driver_version.QuadPart; 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci screen->base.vendor_id = adapter_desc.VendorId; 179bf215546Sopenharmony_ci screen->base.device_id = adapter_desc.DeviceId; 180bf215546Sopenharmony_ci screen->base.subsys_id = adapter_desc.SubSysId; 181bf215546Sopenharmony_ci screen->base.revision = adapter_desc.Revision; 182bf215546Sopenharmony_ci // Note: memory sizes in bytes, but stored in size_t, so may be capped at 4GB. 183bf215546Sopenharmony_ci // In that case, adding before conversion to MB can easily overflow. 184bf215546Sopenharmony_ci screen->base.memory_size_megabytes = (adapter_desc.DedicatedVideoMemory >> 20) + 185bf215546Sopenharmony_ci (adapter_desc.DedicatedSystemMemory >> 20) + 186bf215546Sopenharmony_ci (adapter_desc.SharedSystemMemory >> 20); 187bf215546Sopenharmony_ci wcsncpy(screen->description, adapter_desc.Description, ARRAY_SIZE(screen->description)); 188bf215546Sopenharmony_ci screen->base.base.get_name = dxgi_get_name; 189bf215546Sopenharmony_ci screen->base.get_memory_info = dxgi_get_memory_info; 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci if (!d3d12_init_screen(&screen->base, screen->adapter)) { 192bf215546Sopenharmony_ci debug_printf("D3D12: failed to initialize DXGI screen\n"); 193bf215546Sopenharmony_ci return false; 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci 196bf215546Sopenharmony_ci return true; 197bf215546Sopenharmony_ci} 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_cistruct pipe_screen * 200bf215546Sopenharmony_cid3d12_create_dxgi_screen(struct sw_winsys *winsys, LUID *adapter_luid) 201bf215546Sopenharmony_ci{ 202bf215546Sopenharmony_ci struct d3d12_dxgi_screen *screen = CALLOC_STRUCT(d3d12_dxgi_screen); 203bf215546Sopenharmony_ci if (!screen) 204bf215546Sopenharmony_ci return nullptr; 205bf215546Sopenharmony_ci 206bf215546Sopenharmony_ci d3d12_init_screen_base(&screen->base, winsys, adapter_luid); 207bf215546Sopenharmony_ci screen->base.base.destroy = d3d12_destroy_dxgi_screen; 208bf215546Sopenharmony_ci screen->base.init = d3d12_init_dxgi_screen; 209bf215546Sopenharmony_ci screen->base.deinit = d3d12_deinit_dxgi_screen; 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci if (!d3d12_init_dxgi_screen(&screen->base)) { 212bf215546Sopenharmony_ci d3d12_destroy_dxgi_screen(&screen->base.base); 213bf215546Sopenharmony_ci return nullptr; 214bf215546Sopenharmony_ci } 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci return &screen->base.base; 217bf215546Sopenharmony_ci} 218