From c03320cfc25d59791ead8f8558aef0373b558388 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Thu, 11 Mar 2021 21:03:53 -0800 Subject: [PATCH] libobs: Add function to count GPU adapters Only implemented for D3D11 for now. --- libobs-d3d11/d3d11-subsystem.cpp | 26 ++++++++++++++++++++++++++ libobs/graphics/graphics-imports.c | 1 + libobs/graphics/graphics-internal.h | 2 ++ libobs/graphics/graphics.c | 10 ++++++++++ libobs/graphics/graphics.h | 2 ++ 5 files changed, 41 insertions(+) diff --git a/libobs-d3d11/d3d11-subsystem.cpp b/libobs-d3d11/d3d11-subsystem.cpp index ce47e730f..03064a96b 100644 --- a/libobs-d3d11/d3d11-subsystem.cpp +++ b/libobs-d3d11/d3d11-subsystem.cpp @@ -2866,3 +2866,29 @@ extern "C" EXPORT void device_unregister_loss_callbacks(gs_device_t *device, } } } + +uint32_t gs_get_adapter_count(void) +{ + uint32_t count = 0; + + const IID factoryIID = (GetWinVer() >= 0x602) ? dxgiFactory2 + : __uuidof(IDXGIFactory1); + ComPtr factory; + HRESULT hr = CreateDXGIFactory1(factoryIID, (void **)factory.Assign()); + if (SUCCEEDED(hr)) { + ComPtr adapter; + for (UINT i = 0; + factory->EnumAdapters1(i, adapter.Assign()) == S_OK; ++i) { + DXGI_ADAPTER_DESC desc; + if (SUCCEEDED(adapter->GetDesc(&desc))) { + /* ignore Microsoft's 'basic' renderer' */ + if (desc.VendorId != 0x1414 && + desc.DeviceId != 0x8c) { + ++count; + } + } + } + } + + return count; +} diff --git a/libobs/graphics/graphics-imports.c b/libobs/graphics/graphics-imports.c index 62eccb333..22131581b 100644 --- a/libobs/graphics/graphics-imports.c +++ b/libobs/graphics/graphics-imports.c @@ -210,6 +210,7 @@ bool load_graphics_imports(struct gs_exports *exports, void *module, GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_destroy); GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_update_frame); GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_get_texture); + GRAPHICS_IMPORT_OPTIONAL(gs_get_adapter_count); GRAPHICS_IMPORT_OPTIONAL(device_texture_create_gdi); GRAPHICS_IMPORT_OPTIONAL(gs_texture_get_dc); GRAPHICS_IMPORT_OPTIONAL(gs_texture_release_dc); diff --git a/libobs/graphics/graphics-internal.h b/libobs/graphics/graphics-internal.h index c249dc0c0..46a57ed39 100644 --- a/libobs/graphics/graphics-internal.h +++ b/libobs/graphics/graphics-internal.h @@ -297,6 +297,8 @@ struct gs_exports { bool (*gs_duplicator_update_frame)(gs_duplicator_t *duplicator); gs_texture_t *(*gs_duplicator_get_texture)(gs_duplicator_t *duplicator); + uint32_t (*gs_get_adapter_count)(void); + gs_texture_t *(*device_texture_create_gdi)(gs_device_t *device, uint32_t width, uint32_t height); diff --git a/libobs/graphics/graphics.c b/libobs/graphics/graphics.c index b23a74a4a..6aaf3ff7c 100644 --- a/libobs/graphics/graphics.c +++ b/libobs/graphics/graphics.c @@ -2913,6 +2913,16 @@ bool gs_duplicator_update_frame(gs_duplicator_t *duplicator) return thread_graphics->exports.gs_duplicator_update_frame(duplicator); } +uint32_t gs_get_adapter_count(void) +{ + if (!gs_valid("gs_get_adapter_count")) + return 0; + if (!thread_graphics->exports.gs_get_adapter_count) + return 0; + + return thread_graphics->exports.gs_get_adapter_count(); +} + gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator) { if (!gs_valid_p("gs_duplicator_get_texture", duplicator)) diff --git a/libobs/graphics/graphics.h b/libobs/graphics/graphics.h index b254e4394..16f59ee06 100644 --- a/libobs/graphics/graphics.h +++ b/libobs/graphics/graphics.h @@ -874,6 +874,8 @@ EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator); EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *duplicator); EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator); +EXPORT uint32_t gs_get_adapter_count(void); + /** creates a windows GDI-lockable texture */ EXPORT gs_texture_t *gs_texture_create_gdi(uint32_t width, uint32_t height);