d3d12: Add DXCore screen variation
authorJesse Natalie <jenatali@microsoft.com>
Tue, 8 Dec 2020 20:21:11 +0000 (12:21 -0800)
committerMarge Bot <eric+marge@anholt.net>
Wed, 16 Dec 2020 16:25:54 +0000 (16:25 +0000)
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 <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7937>

src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp [new file with mode: 0644]
src/gallium/drivers/d3d12/d3d12_public.h
src/gallium/drivers/d3d12/d3d12_screen.h
src/gallium/drivers/d3d12/meson.build

diff --git a/src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp b/src/gallium/drivers/d3d12/d3d12_dxcore_screen.cpp
new file mode 100644 (file)
index 0000000..aa95366
--- /dev/null
@@ -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 <directx/dxcore.h>
+
+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;
+}
index 8f3f3dbf130b8847a3fb480e930135f3ba854306..c0d9c66a8d767faaee63ed644f2e4b3e74084ae7 100644 (file)
@@ -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
index 5910b538ae28f2a4deee208fc67604c932662ff6..1d32ad91e2e52993388ffafa2e4080162dad1df6 100644 (file)
@@ -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);
 
index 56cb4741df1814102143708cb297e070511014df..7203fe1694c7d86f41ab4637d4bc85e3d0b1dd4d 100644 (file)
@@ -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',