From 21dc1f573bbf9c22ebe10fad4e29478dee1ebee8 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Sun, 29 Mar 2020 09:55:33 -0700 Subject: [PATCH 1/2] win-capture: Cleaner COM usage Remove hard-coded GUIDs, call correct functions, and clean up unnecessary casts. --- .../win-capture/graphics-hook/CMakeLists.txt | 4 ++- .../graphics-hook/vulkan-capture.c | 26 +++++++------------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/plugins/win-capture/graphics-hook/CMakeLists.txt b/plugins/win-capture/graphics-hook/CMakeLists.txt index 4141861f8..1759219c1 100644 --- a/plugins/win-capture/graphics-hook/CMakeLists.txt +++ b/plugins/win-capture/graphics-hook/CMakeLists.txt @@ -53,7 +53,9 @@ target_include_directories(graphics-hook PUBLIC "${CMAKE_BINARY_DIR}/plugins/win-capture/graphics-hook/config") target_link_libraries(graphics-hook - ipc-util psapi) + dxguid + ipc-util + psapi) if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(_output_suffix "64") diff --git a/plugins/win-capture/graphics-hook/vulkan-capture.c b/plugins/win-capture/graphics-hook/vulkan-capture.c index f8ae6bc12..77a82892e 100644 --- a/plugins/win-capture/graphics-hook/vulkan-capture.c +++ b/plugins/win-capture/graphics-hook/vulkan-capture.c @@ -35,13 +35,6 @@ /* use the loader's dispatch table pointer as a key for internal data maps */ #define GET_LDT(x) (*(void **)x) -/* clang-format off */ -static const GUID dxgi_factory1_guid = -{0x770aae78, 0xf26f, 0x4dba, {0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87}}; -static const GUID dxgi_resource_guid = -{0x035f3ab4, 0x482e, 0x4e50, {0xb4, 0x1f, 0x8a, 0x7f, 0x8b, 0xd8, 0x96, 0x0b}}; -/* clang-format on */ - static bool vulkan_seen = false; static SRWLOCK mutex = SRWLOCK_INIT; // Faster CRITICAL_SECTION @@ -228,7 +221,7 @@ static void vk_shtex_free(struct vk_data *data) NULL); if (swap->d3d11_tex) { - ID3D11Resource_Release(swap->d3d11_tex); + ID3D11Texture2D_Release(swap->d3d11_tex); } swap->handle = INVALID_HANDLE_VALUE; @@ -384,7 +377,7 @@ static inline bool vk_shtex_init_d3d11(struct vk_data *data) { D3D_FEATURE_LEVEL level_used; IDXGIFactory1 *factory; - IDXGIAdapter *adapter; + IDXGIAdapter1 *adapter; HRESULT hr; HMODULE d3d11 = load_system_library("d3d11.dll"); @@ -415,14 +408,13 @@ static inline bool vk_shtex_init_d3d11(struct vk_data *data) return false; } - hr = create_factory(&dxgi_factory1_guid, (void **)&factory); + hr = create_factory(&IID_IDXGIFactory1, &factory); if (FAILED(hr)) { flog_hr("failed to create factory", hr); return false; } - hr = IDXGIFactory1_EnumAdapters1(factory, 0, - (IDXGIAdapter1 **)&adapter); + hr = IDXGIFactory1_EnumAdapters1(factory, 0, &adapter); IDXGIFactory1_Release(factory); if (FAILED(hr)) { @@ -437,11 +429,12 @@ static inline bool vk_shtex_init_d3d11(struct vk_data *data) D3D_FEATURE_LEVEL_9_3, }; - hr = create(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, feature_levels, + hr = create((IDXGIAdapter *)adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, + feature_levels, sizeof(feature_levels) / sizeof(D3D_FEATURE_LEVEL), D3D11_SDK_VERSION, &data->d3d11_device, &level_used, &data->d3d11_context); - IDXGIAdapter_Release(adapter); + IDXGIAdapter1_Release(adapter); if (FAILED(hr)) { flog_hr("failed to create device", hr); @@ -480,8 +473,8 @@ static inline bool vk_shtex_init_d3d11_tex(struct vk_data *data, return false; } - hr = ID3D11Device_QueryInterface(swap->d3d11_tex, &dxgi_resource_guid, - (void **)&dxgi_res); + hr = ID3D11Texture2D_QueryInterface(swap->d3d11_tex, &IID_IDXGIResource, + &dxgi_res); if (FAILED(hr)) { flog_hr("failed to get IDXGIResource", hr); return false; @@ -1369,6 +1362,7 @@ OBS_CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *cinfo, swap->format = cinfo->imageFormat; swap->hwnd = find_surf_hwnd(data->inst_data, cinfo->surface); swap->image_count = count; + swap->d3d11_tex = NULL; return VK_SUCCESS; } From 8987fc6c81bf63dbd18e1a4336b2a40d858f7916 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Sun, 29 Mar 2020 09:58:16 -0700 Subject: [PATCH 2/2] win-capture: Verify VK_KHR_external_memory_win32 support Good practice to verify necessary extensions exist. We'll probably need keyed mutex extension in the future to support Intel. --- .../graphics-hook/vulkan-capture.c | 41 +++++++++++++++++++ .../graphics-hook/vulkan-capture.h | 1 + 2 files changed, 42 insertions(+) diff --git a/plugins/win-capture/graphics-hook/vulkan-capture.c b/plugins/win-capture/graphics-hook/vulkan-capture.c index 77a82892e..d30465ff1 100644 --- a/plugins/win-capture/graphics-hook/vulkan-capture.c +++ b/plugins/win-capture/graphics-hook/vulkan-capture.c @@ -1108,6 +1108,7 @@ static VkResult VKAPI OBS_CreateInstance(const VkInstanceCreateInfo *cinfo, GETADDR(DestroySurfaceKHR); GETADDR(GetPhysicalDeviceMemoryProperties); GETADDR(GetPhysicalDeviceImageFormatProperties2); + GETADDR(EnumerateDeviceExtensionProperties); #undef GETADDR data->valid = !funcs_not_found; @@ -1289,6 +1290,46 @@ static VkResult VKAPI OBS_CreateDevice(VkPhysicalDevice phy_device, goto fail; } + const char *required_device_extensions[] = { + VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME}; + + uint32_t device_extension_count = 0; + ret = ifuncs->EnumerateDeviceExtensionProperties( + phy_device, NULL, &device_extension_count, NULL); + if (ret != VK_SUCCESS) + goto fail; + + VkExtensionProperties *device_extensions = _malloca( + sizeof(VkExtensionProperties) * device_extension_count); + ret = ifuncs->EnumerateDeviceExtensionProperties( + phy_device, NULL, &device_extension_count, device_extensions); + if (ret != VK_SUCCESS) + goto fail; + + bool extensions_found = true; + for (uint32_t i = 0; i < _countof(required_device_extensions); i++) { + const char *const required_extension = + required_device_extensions[i]; + + bool found = false; + for (uint32_t j = 0; j < device_extension_count; j++) { + if (!strcmp(required_extension, + device_extensions[j].extensionName)) { + found = true; + break; + } + } + + if (!found) { + flog("missing device extension: %s", + required_extension); + extensions_found = false; + } + } + + if (!extensions_found) + goto fail; + VkFormat format = VK_FORMAT_R8G8B8A8_UNORM; VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; diff --git a/plugins/win-capture/graphics-hook/vulkan-capture.h b/plugins/win-capture/graphics-hook/vulkan-capture.h index 15651153f..b52ccac60 100644 --- a/plugins/win-capture/graphics-hook/vulkan-capture.h +++ b/plugins/win-capture/graphics-hook/vulkan-capture.h @@ -9,6 +9,7 @@ struct vk_inst_funcs { DEF_FUNC(DestroySurfaceKHR); DEF_FUNC(GetPhysicalDeviceMemoryProperties); DEF_FUNC(GetPhysicalDeviceImageFormatProperties2); + DEF_FUNC(EnumerateDeviceExtensionProperties); }; struct vk_device_funcs {