From f6736480035281f9d37a69a3f65114226a5ac9a8 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Tue, 8 Dec 2020 12:21:11 -0800 Subject: [PATCH] d3d12: Add DXCore screen variation Not all Windows platforms have DXGI, and neither does WSL. Instead, we can use the DXCore API for adapter enumeration. Reviewed-by: Erik Faye-Lund Part-of: --- src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp | 137 ++++++++++++++++++++++ src/gallium/drivers/d3d12/d3d12_public.h | 3 + src/gallium/drivers/d3d12/d3d12_screen.h | 14 +++ src/gallium/drivers/d3d12/meson.build | 1 + 4 files changed, 155 insertions(+) create mode 100644 src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp diff --git a/src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp b/src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp new file mode 100644 index 0000000..aa95366 --- /dev/null +++ b/src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp @@ -0,0 +1,137 @@ +/* + * Copyright © Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "d3d12_screen.h" +#include "d3d12_public.h" + +#include "util/debug.h" +#include "util/u_memory.h" + +#include + +static IDXCoreAdapterFactory * +get_dxcore_factory() +{ + typedef HRESULT(WINAPI *PFN_CREATE_DXCORE_ADAPTER_FACTORY)(REFIID riid, void **ppFactory); + PFN_CREATE_DXCORE_ADAPTER_FACTORY DXCoreCreateAdapterFactory; + + HMODULE hDXCoreMod = LoadLibrary("DXCore.DLL"); + if (!hDXCoreMod) { + debug_printf("D3D12: failed to load DXCore.DLL\n"); + return NULL; + } + + DXCoreCreateAdapterFactory = (PFN_CREATE_DXCORE_ADAPTER_FACTORY)GetProcAddress(hDXCoreMod, "DXCoreCreateAdapterFactory"); + if (!DXCoreCreateAdapterFactory) { + debug_printf("D3D12: failed to load DXCoreCreateAdapterFactory from DXCore.DLL\n"); + return NULL; + } + + IDXCoreAdapterFactory *factory = NULL; + HRESULT hr = DXCoreCreateAdapterFactory(IID_IDXCoreAdapterFactory, (void **)&factory); + if (FAILED(hr)) { + debug_printf("D3D12: DXCoreCreateAdapterFactory failed: %08x\n", hr); + return NULL; + } + + return factory; +} + +static IDXCoreAdapter * +choose_dxcore_adapter(IDXCoreAdapterFactory *factory, LUID *adapter) +{ + IDXCoreAdapter *ret; + if (adapter) { + if (SUCCEEDED(factory->GetAdapterByLuid(*adapter, &ret))) + return ret; + debug_printf("D3D12: requested adapter missing, falling back to auto-detection...\n"); + } + + // The first adapter is the default + IDXCoreAdapterList *list = nullptr; + if (SUCCEEDED(factory->CreateAdapterList(1, &DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS, &list))) { + if (list->GetAdapterCount() > 0 && SUCCEEDED(list->GetAdapter(0, &ret))) + return ret; + } + + return NULL; +} + +static const char * +dxcore_get_name(struct pipe_screen *screen) +{ + struct d3d12_dxcore_screen *dxcore_screen = d3d12_dxcore_screen(d3d12_screen(screen)); + static char buf[1000]; + if (dxcore_screen->description[0] == '\0') + return "D3D12 (Unknown)"; + + snprintf(buf, sizeof(buf), "D3D12 (%s)", dxcore_screen->description); + return buf; +} + +struct pipe_screen * +d3d12_create_dxcore_screen(struct sw_winsys *winsys, LUID *adapter_luid) +{ + struct d3d12_dxcore_screen *screen = CALLOC_STRUCT(d3d12_dxcore_screen); + if (!screen) + return nullptr; + + screen->factory = get_dxcore_factory(); + if (!screen->factory) { + FREE(screen); + return nullptr; + } + + screen->adapter = choose_dxcore_adapter(screen->factory, adapter_luid); + if (!screen->adapter) { + debug_printf("D3D12: no suitable adapter\n"); + FREE(screen); + return nullptr; + } + + DXCoreHardwareID hardware_ids = {}; + uint64_t dedicated_video_memory, dedicated_system_memory, shared_system_memory; + if (FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hardware_ids)) || + FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedAdapterMemory, &dedicated_video_memory)) || + FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DedicatedSystemMemory, &dedicated_system_memory)) || + FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::SharedSystemMemory, &shared_system_memory)) || + FAILED(screen->adapter->GetProperty(DXCoreAdapterProperty::DriverDescription, + sizeof(screen->description), + screen->description))) { + debug_printf("D3D12: failed to retrieve adapter description\n"); + FREE(screen); + return nullptr; + } + + screen->base.vendor_id = hardware_ids.vendorID; + screen->base.memory_size_megabytes = (dedicated_video_memory + dedicated_system_memory + shared_system_memory) >> 20; + screen->base.base.get_name = dxcore_get_name; + + if (!d3d12_init_screen(&screen->base, winsys, screen->adapter)) { + debug_printf("D3D12: failed to initialize DXCore screen\n"); + FREE(screen); + return nullptr; + } + + return &screen->base.base; +} diff --git a/src/gallium/drivers/d3d12/d3d12_public.h b/src/gallium/drivers/d3d12/d3d12_public.h index 8f3f3db..c0d9c66 100644 --- a/src/gallium/drivers/d3d12/d3d12_public.h +++ b/src/gallium/drivers/d3d12/d3d12_public.h @@ -34,6 +34,9 @@ extern "C" { struct pipe_screen * d3d12_create_dxgi_screen(struct sw_winsys *winsys, LUID *adapter_luid); +struct pipe_screen * +d3d12_create_dxcore_screen(struct sw_winsys *winsys, LUID *adapter_luid); + #ifdef __cplusplus } #endif diff --git a/src/gallium/drivers/d3d12/d3d12_screen.h b/src/gallium/drivers/d3d12/d3d12_screen.h index 5910b53..1d32ad9 100644 --- a/src/gallium/drivers/d3d12/d3d12_screen.h +++ b/src/gallium/drivers/d3d12/d3d12_screen.h @@ -80,6 +80,20 @@ d3d12_dxgi_screen(struct d3d12_screen *screen) return (struct d3d12_dxgi_screen *)screen; } +struct d3d12_dxcore_screen { + struct d3d12_screen base; + + struct IDXCoreAdapterFactory *factory; + struct IDXCoreAdapter *adapter; + char description[256]; +}; + +static inline struct d3d12_dxcore_screen * +d3d12_dxcore_screen(struct d3d12_screen *screen) +{ + return (struct d3d12_dxcore_screen *)screen; +} + bool d3d12_init_screen(struct d3d12_screen *screen, struct sw_winsys *winsys, IUnknown *adapter); diff --git a/src/gallium/drivers/d3d12/meson.build b/src/gallium/drivers/d3d12/meson.build index 56cb474..7203fe1 100644 --- a/src/gallium/drivers/d3d12/meson.build +++ b/src/gallium/drivers/d3d12/meson.build @@ -27,6 +27,7 @@ files_libd3d12 = files( 'd3d12_context.cpp', 'd3d12_descriptor_pool.cpp', 'd3d12_draw.cpp', + 'd3d12_dxcore_screen.cpp', 'd3d12_dxgi_screen.cpp', 'd3d12_fence.cpp', 'd3d12_format.c', -- 2.7.4