From bee68059cb893b47dbab8430ae227a9aca804d0d Mon Sep 17 00:00:00 2001 From: Sil Vilerino Date: Mon, 20 Nov 2023 22:08:18 -0500 Subject: [PATCH] d3d12: Autodetect d3d12_video_buffer imported handle/resource format and dimensions when not passed Reviewed-by: Jesse Natalie Part-of: --- .../drivers/d3d12/d3d12_video_buffer.cpp | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/gallium/drivers/d3d12/d3d12_video_buffer.cpp b/src/gallium/drivers/d3d12/d3d12_video_buffer.cpp index 14dc1a6156d..dffb038e3a3 100644 --- a/src/gallium/drivers/d3d12/d3d12_video_buffer.cpp +++ b/src/gallium/drivers/d3d12/d3d12_video_buffer.cpp @@ -33,6 +33,8 @@ #include "vl/vl_video_buffer.h" #include "util/u_sampler.h" #include "frontend/winsys_handle.h" +#include "d3d12_format.h" +#include "d3d12_screen.h" static struct pipe_video_buffer * d3d12_video_buffer_create_impl(struct pipe_context *pipe, @@ -119,17 +121,45 @@ failed: return nullptr; } - /** * creates a video buffer from a handle */ struct pipe_video_buffer * -d3d12_video_buffer_from_handle( struct pipe_context *pipe, - const struct pipe_video_buffer *tmpl, - struct winsys_handle *handle, - unsigned usage) +d3d12_video_buffer_from_handle(struct pipe_context *pipe, + const struct pipe_video_buffer *tmpl, + struct winsys_handle *handle, + unsigned usage) { - return d3d12_video_buffer_create_impl(pipe, tmpl, handle, usage); + struct pipe_video_buffer updated_template = {}; + if ((handle->format == PIPE_FORMAT_NONE) || (tmpl == nullptr) || (tmpl->buffer_format == PIPE_FORMAT_NONE) || + (tmpl->width == 0) || (tmpl->height == 0)) { + ID3D12Resource *d3d12_res = nullptr; + if (handle->type == WINSYS_HANDLE_TYPE_D3D12_RES) { + d3d12_res = (ID3D12Resource *) handle->com_obj; + } else if (handle->type == WINSYS_HANDLE_TYPE_FD) { +#ifdef _WIN32 + HANDLE d3d_handle = handle->handle; +#else + HANDLE d3d_handle = (HANDLE) (intptr_t) handle->handle; +#endif + if (FAILED(d3d12_screen(pipe->screen)->dev->OpenSharedHandle(d3d_handle, IID_PPV_ARGS(&d3d12_res)))) { + return NULL; + } + } + D3D12_RESOURCE_DESC res_desc = GetDesc(d3d12_res); + updated_template.width = res_desc.Width; + updated_template.height = res_desc.Height; + updated_template.buffer_format = d3d12_get_pipe_format(res_desc.Format); + handle->format = updated_template.buffer_format; + + // if passed an external com_ptr (e.g WINSYS_HANDLE_TYPE_D3D12_RES) do not release it + if (handle->type == WINSYS_HANDLE_TYPE_FD) + d3d12_res->Release(); + } else { + updated_template = *tmpl; + } + + return d3d12_video_buffer_create_impl(pipe, &updated_template, handle, usage); } /**