aux/d3d,c/client: fix d3d12 "zero size" swapchain error

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2347>
This commit is contained in:
korejan 2024-11-06 15:17:06 +00:00 committed by Korcan Hussein
parent 6a26019aba
commit 3d6b272ee0
4 changed files with 30 additions and 11 deletions

View file

@ -1,10 +1,11 @@
// Copyright 2020-2022, Collabora, Ltd. // Copyright 2020-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
/*! /*!
* @file * @file
* @brief D3D12 backed image buffer allocator. * @brief D3D12 backed image buffer allocator.
* @author Rylie Pavlik <rylie.pavlik@collabora.com> * @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @author Fernando Velazquez Innella <finnella@magicleap.com> * @author Fernando Velazquez Innella <finnella@magicleap.com>
* @author Korcan Hussein <korcan.hussein@collabora.com>
* @ingroup aux_d3d * @ingroup aux_d3d
*/ */
@ -74,7 +75,8 @@ allocateSharedImages(ID3D12Device &device,
const xrt_swapchain_create_info &xsci, const xrt_swapchain_create_info &xsci,
size_t image_count, size_t image_count,
std::vector<wil::com_ptr<ID3D12Resource>> &out_images, std::vector<wil::com_ptr<ID3D12Resource>> &out_images,
std::vector<wil::unique_handle> &out_handles) std::vector<wil::unique_handle> &out_handles,
std::uint64_t &out_image_mem_size)
try { try {
if (0 != (xsci.create & XRT_SWAPCHAIN_CREATE_PROTECTED_CONTENT)) { if (0 != (xsci.create & XRT_SWAPCHAIN_CREATE_PROTECTED_CONTENT)) {
return XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED; return XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED;
@ -155,6 +157,8 @@ try {
} }
out_images = std::move(images); out_images = std::move(images);
out_handles = std::move(handles); out_handles = std::move(handles);
const D3D12_RESOURCE_ALLOCATION_INFO alloc_info = device.GetResourceAllocationInfo(0, 1, &desc);
out_image_mem_size = alloc_info.SizeInBytes;
return XRT_SUCCESS; return XRT_SUCCESS;
} }
DEFAULT_CATCH(XRT_ERROR_ALLOCATION) DEFAULT_CATCH(XRT_ERROR_ALLOCATION)
@ -177,6 +181,7 @@ d3d12_images_allocate(struct xrt_image_native_allocator *xina,
try { try {
d3d12_allocator *d3da = reinterpret_cast<d3d12_allocator *>(xina); d3d12_allocator *d3da = reinterpret_cast<d3d12_allocator *>(xina);
std::uint64_t image_mem_size = 0;
std::vector<wil::com_ptr<ID3D12Resource>> images; std::vector<wil::com_ptr<ID3D12Resource>> images;
std::vector<wil::unique_handle> handles; std::vector<wil::unique_handle> handles;
auto result = xrt::auxiliary::d3d::d3d12::allocateSharedImages( // auto result = xrt::auxiliary::d3d::d3d12::allocateSharedImages( //
@ -184,7 +189,8 @@ d3d12_images_allocate(struct xrt_image_native_allocator *xina,
*xsci, // xsci *xsci, // xsci
image_count, // image_count image_count, // image_count
images, // out_images images, // out_images
handles); // out_handles handles, // out_handles
image_mem_size); // out_image_mem_size (in bytes)
if (result != XRT_SUCCESS) { if (result != XRT_SUCCESS) {
return result; return result;
@ -193,6 +199,7 @@ d3d12_images_allocate(struct xrt_image_native_allocator *xina,
for (size_t i = 0; i < image_count; ++i) { for (size_t i = 0; i < image_count; ++i) {
out_images[i].handle = handles[i].release(); out_images[i].handle = handles[i].release();
out_images[i].is_dxgi_handle = false; out_images[i].is_dxgi_handle = false;
out_images[i].size = image_mem_size;
} }
return XRT_SUCCESS; return XRT_SUCCESS;

View file

@ -1,10 +1,11 @@
// Copyright 2020-2023, Collabora, Ltd. // Copyright 2020-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
/*! /*!
* @file * @file
* @brief Higher-level D3D12-backed image buffer allocation routine. * @brief Higher-level D3D12-backed image buffer allocation routine.
* @author Rylie Pavlik <rylie.pavlik@collabora.com> * @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @author Fernando Velazquez Innella <finnella@magicleap.com> * @author Fernando Velazquez Innella <finnella@magicleap.com>
* @author Korcan Hussein <korcan.hussein@collabora.com>
* @ingroup aux_d3d * @ingroup aux_d3d
*/ */
@ -17,6 +18,7 @@
#include <wil/com.h> #include <wil/com.h>
#include <wil/resource.h> #include <wil/resource.h>
#include <cstdint>
#include <vector> #include <vector>
@ -32,6 +34,7 @@ namespace xrt::auxiliary::d3d::d3d12 {
* @param keyed_mutex Whether to create images with a shared "keyed mutex" as well * @param keyed_mutex Whether to create images with a shared "keyed mutex" as well
* @param[out] out_images A vector that will be cleared and populated with the images. * @param[out] out_images A vector that will be cleared and populated with the images.
* @param[out] out_handles A vector that will be cleared and populated with the corresponding native handles. * @param[out] out_handles A vector that will be cleared and populated with the corresponding native handles.
* @param[out] out_image_mem_size The image memory allocation size in bytes
* *
* @return xrt_result_t, one of: * @return xrt_result_t, one of:
* - @ref XRT_SUCCESS * - @ref XRT_SUCCESS
@ -45,6 +48,7 @@ allocateSharedImages(ID3D12Device &device,
const xrt_swapchain_create_info &xsci, const xrt_swapchain_create_info &xsci,
size_t image_count, size_t image_count,
std::vector<wil::com_ptr<ID3D12Resource>> &out_images, std::vector<wil::com_ptr<ID3D12Resource>> &out_images,
std::vector<wil::unique_handle> &out_handles); std::vector<wil::unique_handle> &out_handles,
std::uint64_t &out_image_mem_size);
}; // namespace xrt::auxiliary::d3d::d3d12 }; // namespace xrt::auxiliary::d3d::d3d12

View file

@ -1,4 +1,4 @@
// Copyright 2019-2023, Collabora, Ltd. // Copyright 2019-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
/*! /*!
* @file * @file
@ -6,6 +6,7 @@
* @author Rylie Pavlik <rylie.pavlik@collabora.com> * @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @author Jakob Bornecrantz <jakob@collabora.com> * @author Jakob Bornecrantz <jakob@collabora.com>
* @author Fernando Velazquez Innella <finnella@magicleap.com> * @author Fernando Velazquez Innella <finnella@magicleap.com>
* @author Korcan Hussein <korcan.hussein@collabora.com>
* @ingroup comp_client * @ingroup comp_client
*/ */
@ -494,6 +495,7 @@ try {
std::unique_ptr<struct client_d3d12_swapchain> sc = std::make_unique<struct client_d3d12_swapchain>(); std::unique_ptr<struct client_d3d12_swapchain> sc = std::make_unique<struct client_d3d12_swapchain>();
sc->data = std::make_unique<client_d3d12_swapchain_data>(c->log_level); sc->data = std::make_unique<client_d3d12_swapchain_data>(c->log_level);
auto &data = sc->data; auto &data = sc->data;
std::uint64_t image_mem_size = 0;
// Allocate images // Allocate images
xret = xrt::auxiliary::d3d::d3d12::allocateSharedImages( // xret = xrt::auxiliary::d3d::d3d12::allocateSharedImages( //
@ -501,7 +503,8 @@ try {
xinfo, // xinfo, //
image_count, // image_count, //
data->images, // data->images, //
data->handles); // data->handles, //
image_mem_size); //
if (xret != XRT_SUCCESS) { if (xret != XRT_SUCCESS) {
return xret; return xret;
} }
@ -597,7 +600,8 @@ try {
xinfo, // xsci xinfo, // xsci
image_count, // image_count image_count, // image_count
data->comp_images, // out_images data->comp_images, // out_images
data->comp_handles); // out_handles data->comp_handles, // out_handles
image_mem_size); // out_image_mem_size (in bytes)
if (xret != XRT_SUCCESS) { if (xret != XRT_SUCCESS) {
return xret; return xret;
} }
@ -628,7 +632,8 @@ try {
std::vector<wil::unique_handle> &handles = compositorNeedsCopy ? data->comp_handles : data->handles; std::vector<wil::unique_handle> &handles = compositorNeedsCopy ? data->comp_handles : data->handles;
// Import into the native compositor, to create the corresponding swapchain which we wrap. // Import into the native compositor, to create the corresponding swapchain which we wrap.
xret = xrt::compositor::client::importFromHandleDuplicates(*(c->xcn), handles, vkinfo, true, sc->xsc); xret = xrt::compositor::client::importFromHandleDuplicates(*(c->xcn), handles, vkinfo, image_mem_size, true,
sc->xsc);
if (xret != XRT_SUCCESS) { if (xret != XRT_SUCCESS) {
D3D_ERROR(c, "Error importing D3D swapchain into native compositor"); D3D_ERROR(c, "Error importing D3D swapchain into native compositor");
return xret; return xret;

View file

@ -1,4 +1,4 @@
// Copyright 2019-2022, Collabora, Ltd. // Copyright 2019-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
/*! /*!
* @file * @file
@ -6,6 +6,7 @@
* @author Rylie Pavlik <rylie.pavlik@collabora.com> * @author Rylie Pavlik <rylie.pavlik@collabora.com>
* @author Jakob Bornecrantz <jakob@collabora.com> * @author Jakob Bornecrantz <jakob@collabora.com>
* @author Fernando Velazquez Innella <finnella@magicleap.com> * @author Fernando Velazquez Innella <finnella@magicleap.com>
* @author Korcan Hussein <korcan.hussein@collabora.com>
* @ingroup comp_client * @ingroup comp_client
*/ */
#pragma once #pragma once
@ -41,6 +42,7 @@ using unique_swapchain_ref =
* @param xcn The native compositor * @param xcn The native compositor
* @param handles A vector of uniquely-owned handles. These will be duplicated, not consumed, by this import. * @param handles A vector of uniquely-owned handles. These will be duplicated, not consumed, by this import.
* @param vkinfo The swapchain create info, with format as a Vulkan constant * @param vkinfo The swapchain create info, with format as a Vulkan constant
* @param image_mem_size The image memory allocation size in bytes
* @param use_dedicated_allocation Passed through to @ref xrt_image_native * @param use_dedicated_allocation Passed through to @ref xrt_image_native
* @param[out] out_xsc The swapchain to populate * @param[out] out_xsc The swapchain to populate
* @return XRT_SUCCESS if everything went well, otherwise whatever error a call internally returned. * @return XRT_SUCCESS if everything went well, otherwise whatever error a call internally returned.
@ -49,6 +51,7 @@ static inline xrt_result_t
importFromHandleDuplicates(xrt_compositor_native &xcn, importFromHandleDuplicates(xrt_compositor_native &xcn,
std::vector<wil::unique_handle> const &handles, std::vector<wil::unique_handle> const &handles,
const struct xrt_swapchain_create_info &vkinfo, const struct xrt_swapchain_create_info &vkinfo,
const std::uint64_t image_mem_size,
bool use_dedicated_allocation, bool use_dedicated_allocation,
unique_swapchain_ref &out_xsc) unique_swapchain_ref &out_xsc)
{ {
@ -65,7 +68,7 @@ importFromHandleDuplicates(xrt_compositor_native &xcn,
wil::unique_handle duped{u_graphics_buffer_ref(handle.get())}; wil::unique_handle duped{u_graphics_buffer_ref(handle.get())};
xrt_image_native xin{}; xrt_image_native xin{};
xin.handle = duped.get(); xin.handle = duped.get();
xin.size = 0; xin.size = image_mem_size;
xin.use_dedicated_allocation = use_dedicated_allocation; xin.use_dedicated_allocation = use_dedicated_allocation;
handlesForImport.emplace_back(std::move(duped)); handlesForImport.emplace_back(std::move(duped));