Path: blob/21.2-virgl/src/gallium/drivers/d3d12/d3d12_dxgi_screen.cpp
4570 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "d3d12_screen.h"24#include "d3d12_public.h"25#include "d3d12_debug.h"2627#include "util/debug.h"28#include "util/u_memory.h"29#include "util/u_dl.h"3031#include <dxgi1_4.h>3233static IDXGIFactory4 *34get_dxgi_factory()35{36static const GUID IID_IDXGIFactory4 = {370x1bc6ea02, 0xef36, 0x464f,38{ 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a }39};4041util_dl_library *dxgi_mod = util_dl_open(UTIL_DL_PREFIX "dxgi" UTIL_DL_EXT);42if (!dxgi_mod) {43debug_printf("D3D12: failed to load DXGI.DLL\n");44return NULL;45}4647typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY2)(UINT flags, REFIID riid, void **ppFactory);48PFN_CREATE_DXGI_FACTORY2 CreateDXGIFactory2;4950CreateDXGIFactory2 = (PFN_CREATE_DXGI_FACTORY2)util_dl_get_proc_address(dxgi_mod, "CreateDXGIFactory2");51if (!CreateDXGIFactory2) {52debug_printf("D3D12: failed to load CreateDXGIFactory2 from DXGI.DLL\n");53return NULL;54}5556UINT flags = 0;57#ifndef DEBUG58if (d3d12_debug & D3D12_DEBUG_DEBUG_LAYER)59#endif60flags |= DXGI_CREATE_FACTORY_DEBUG;6162IDXGIFactory4 *factory = NULL;63HRESULT hr = CreateDXGIFactory2(flags, IID_IDXGIFactory4, (void **)&factory);64if (FAILED(hr)) {65debug_printf("D3D12: CreateDXGIFactory2 failed: %08x\n", hr);66return NULL;67}6869return factory;70}7172static IDXGIAdapter1 *73choose_dxgi_adapter(IDXGIFactory4 *factory, LUID *adapter)74{75IDXGIAdapter1 *ret;76if (adapter) {77if (SUCCEEDED(factory->EnumAdapterByLuid(*adapter,78IID_PPV_ARGS(&ret))))79return ret;80debug_printf("D3D12: requested adapter missing, falling back to auto-detection...\n");81}8283bool want_warp = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);84if (want_warp) {85if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&ret))))86return ret;87debug_printf("D3D12: failed to enum warp adapter\n");88return NULL;89}9091// The first adapter is the default92if (SUCCEEDED(factory->EnumAdapters1(0, &ret)))93return ret;9495return NULL;96}9798static const char *99dxgi_get_name(struct pipe_screen *screen)100{101struct d3d12_dxgi_screen *dxgi_screen = d3d12_dxgi_screen(d3d12_screen(screen));102static char buf[1000];103if (dxgi_screen->description[0] == L'\0')104return "D3D12 (Unknown)";105106snprintf(buf, sizeof(buf), "D3D12 (%S)", dxgi_screen->description);107return buf;108}109110struct pipe_screen *111d3d12_create_dxgi_screen(struct sw_winsys *winsys, LUID *adapter_luid)112{113struct d3d12_dxgi_screen *screen = CALLOC_STRUCT(d3d12_dxgi_screen);114if (!screen)115return nullptr;116117screen->factory = get_dxgi_factory();118if (!screen->factory) {119FREE(screen);120return nullptr;121}122123screen->adapter = choose_dxgi_adapter(screen->factory, adapter_luid);124if (!screen->adapter) {125debug_printf("D3D12: no suitable adapter\n");126FREE(screen);127return nullptr;128}129130DXGI_ADAPTER_DESC1 adapter_desc = {};131if (FAILED(screen->adapter->GetDesc1(&adapter_desc))) {132debug_printf("D3D12: failed to retrieve adapter description\n");133FREE(screen);134return nullptr;135}136137screen->base.vendor_id = adapter_desc.VendorId;138// Note: memory sizes in bytes, but stored in size_t, so may be capped at 4GB.139// In that case, adding before conversion to MB can easily overflow.140screen->base.memory_size_megabytes = (adapter_desc.DedicatedVideoMemory >> 20) +141(adapter_desc.DedicatedSystemMemory >> 20) +142(adapter_desc.SharedSystemMemory >> 20);143wcsncpy(screen->description, adapter_desc.Description, ARRAY_SIZE(screen->description));144screen->base.base.get_name = dxgi_get_name;145146if (!d3d12_init_screen(&screen->base, winsys, screen->adapter)) {147debug_printf("D3D12: failed to initialize DXGI screen\n");148FREE(screen);149return nullptr;150}151152return &screen->base.base;153}154155156