Module: Mesa Branch: main Commit: e08df859234f39af3d693adc235d1b944e884d4e URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=e08df859234f39af3d693adc235d1b944e884d4e
Author: Sil Vilerino <[email protected]> Date: Tue Oct 18 09:05:24 2022 -0400 d3d12: resource_from_handle to validate importing resource ptr from same d3d12 device Reviewed-by: Jesse Natalie <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19063> --- 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 <dxguids/dxguids.h> #include <memory> +#include <wrl/client.h> +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<IUnknown> screen_device; + ComPtr<IUnknown> 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) {
