From e08df859234f39af3d693adc235d1b944e884d4e Mon Sep 17 00:00:00 2001 From: Sil Vilerino Date: Tue, 18 Oct 2022 09:05:24 -0400 Subject: [PATCH] d3d12: resource_from_handle to validate importing resource ptr from same d3d12 device Reviewed-by: Jesse Natalie Part-of: --- src/gallium/drivers/d3d12/d3d12_resource.cpp | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/gallium/drivers/d3d12/d3d12_resource.cpp b/src/gallium/drivers/d3d12/d3d12_resource.cpp index cb0346b7938..bcf02c3b403 100644 --- a/src/gallium/drivers/d3d12/d3d12_resource.cpp +++ b/src/gallium/drivers/d3d12/d3d12_resource.cpp @@ -41,6 +41,9 @@ #include #include +#include +using Microsoft::WRL::ComPtr; + #ifndef GENERIC_ALL // This is only added to winadapter.h in newer DirectX-Headers #define GENERIC_ALL 0x10000000L @@ -449,6 +452,32 @@ d3d12_resource_from_handle(struct pipe_screen *pscreen, HANDLE d3d_handle = (HANDLE) (intptr_t) handle->handle; #endif + if (handle->type == WINSYS_HANDLE_TYPE_D3D12_RES) { + ComPtr screen_device; + ComPtr res_device; + screen->dev->QueryInterface(screen_device.GetAddressOf()); + ((ID3D12DeviceChild *)handle->com_obj)->GetDevice(IID_PPV_ARGS(res_device.GetAddressOf())); + + if (screen_device.Get() != res_device.Get()) { + debug_printf("d3d12: Importing resource - Resource's parent device (%p) does not" + " match d3d12 device (%p) instance from this pipe_screen." + " Attempting to re-import via NT Handle...\n", screen_device.Get(), res_device.Get()); + + handle->type = WINSYS_HANDLE_TYPE_FD; + HRESULT hr = screen->dev->CreateSharedHandle(((ID3D12DeviceChild *)handle->com_obj), + nullptr, + GENERIC_ALL, + nullptr, + &d3d_handle); + + if (FAILED(hr)) { + debug_printf("d3d12: Error %x - Couldn't export incoming resource com_obj " + "(%p) via shared NT handle.\n", hr, handle->com_obj); + return NULL; + } + } + } + #ifdef _WIN32 HANDLE d3d_handle_to_close = nullptr; if (handle->type == WINSYS_HANDLE_TYPE_WIN32_NAME) {