The open source OpenXR runtime

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

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2347>

authored by

korejan and committed by
Korcan Hussein
3d6b272e 6a26019a

+30 -11
+10 -3
src/xrt/auxiliary/d3d/d3d_d3d12_allocator.cpp
··· 1 - // Copyright 2020-2022, Collabora, Ltd. 1 + // Copyright 2020-2024, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file 5 5 * @brief D3D12 backed image buffer allocator. 6 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 7 * @author Fernando Velazquez Innella <finnella@magicleap.com> 8 + * @author Korcan Hussein <korcan.hussein@collabora.com> 8 9 * @ingroup aux_d3d 9 10 */ 10 11 ··· 74 75 const xrt_swapchain_create_info &xsci, 75 76 size_t image_count, 76 77 std::vector<wil::com_ptr<ID3D12Resource>> &out_images, 77 - std::vector<wil::unique_handle> &out_handles) 78 + std::vector<wil::unique_handle> &out_handles, 79 + std::uint64_t &out_image_mem_size) 78 80 try { 79 81 if (0 != (xsci.create & XRT_SWAPCHAIN_CREATE_PROTECTED_CONTENT)) { 80 82 return XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED; ··· 155 157 } 156 158 out_images = std::move(images); 157 159 out_handles = std::move(handles); 160 + const D3D12_RESOURCE_ALLOCATION_INFO alloc_info = device.GetResourceAllocationInfo(0, 1, &desc); 161 + out_image_mem_size = alloc_info.SizeInBytes; 158 162 return XRT_SUCCESS; 159 163 } 160 164 DEFAULT_CATCH(XRT_ERROR_ALLOCATION) ··· 177 181 try { 178 182 d3d12_allocator *d3da = reinterpret_cast<d3d12_allocator *>(xina); 179 183 184 + std::uint64_t image_mem_size = 0; 180 185 std::vector<wil::com_ptr<ID3D12Resource>> images; 181 186 std::vector<wil::unique_handle> handles; 182 187 auto result = xrt::auxiliary::d3d::d3d12::allocateSharedImages( // ··· 184 189 *xsci, // xsci 185 190 image_count, // image_count 186 191 images, // out_images 187 - handles); // out_handles 192 + handles, // out_handles 193 + image_mem_size); // out_image_mem_size (in bytes) 188 194 189 195 if (result != XRT_SUCCESS) { 190 196 return result; ··· 193 199 for (size_t i = 0; i < image_count; ++i) { 194 200 out_images[i].handle = handles[i].release(); 195 201 out_images[i].is_dxgi_handle = false; 202 + out_images[i].size = image_mem_size; 196 203 } 197 204 198 205 return XRT_SUCCESS;
+6 -2
src/xrt/auxiliary/d3d/d3d_d3d12_allocator.hpp
··· 1 - // Copyright 2020-2023, Collabora, Ltd. 1 + // Copyright 2020-2024, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file 5 5 * @brief Higher-level D3D12-backed image buffer allocation routine. 6 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 7 * @author Fernando Velazquez Innella <finnella@magicleap.com> 8 + * @author Korcan Hussein <korcan.hussein@collabora.com> 8 9 * @ingroup aux_d3d 9 10 */ 10 11 ··· 17 18 #include <wil/com.h> 18 19 #include <wil/resource.h> 19 20 21 + #include <cstdint> 20 22 #include <vector> 21 23 22 24 ··· 32 34 * @param keyed_mutex Whether to create images with a shared "keyed mutex" as well 33 35 * @param[out] out_images A vector that will be cleared and populated with the images. 34 36 * @param[out] out_handles A vector that will be cleared and populated with the corresponding native handles. 37 + * @param[out] out_image_mem_size The image memory allocation size in bytes 35 38 * 36 39 * @return xrt_result_t, one of: 37 40 * - @ref XRT_SUCCESS ··· 45 48 const xrt_swapchain_create_info &xsci, 46 49 size_t image_count, 47 50 std::vector<wil::com_ptr<ID3D12Resource>> &out_images, 48 - std::vector<wil::unique_handle> &out_handles); 51 + std::vector<wil::unique_handle> &out_handles, 52 + std::uint64_t &out_image_mem_size); 49 53 50 54 }; // namespace xrt::auxiliary::d3d::d3d12
+9 -4
src/xrt/compositor/client/comp_d3d12_client.cpp
··· 1 - // Copyright 2019-2023, Collabora, Ltd. 1 + // Copyright 2019-2024, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 6 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 7 * @author Jakob Bornecrantz <jakob@collabora.com> 8 8 * @author Fernando Velazquez Innella <finnella@magicleap.com> 9 + * @author Korcan Hussein <korcan.hussein@collabora.com> 9 10 * @ingroup comp_client 10 11 */ 11 12 ··· 494 495 std::unique_ptr<struct client_d3d12_swapchain> sc = std::make_unique<struct client_d3d12_swapchain>(); 495 496 sc->data = std::make_unique<client_d3d12_swapchain_data>(c->log_level); 496 497 auto &data = sc->data; 498 + std::uint64_t image_mem_size = 0; 497 499 498 500 // Allocate images 499 501 xret = xrt::auxiliary::d3d::d3d12::allocateSharedImages( // ··· 501 503 xinfo, // 502 504 image_count, // 503 505 data->images, // 504 - data->handles); // 506 + data->handles, // 507 + image_mem_size); // 505 508 if (xret != XRT_SUCCESS) { 506 509 return xret; 507 510 } ··· 597 600 xinfo, // xsci 598 601 image_count, // image_count 599 602 data->comp_images, // out_images 600 - data->comp_handles); // out_handles 603 + data->comp_handles, // out_handles 604 + image_mem_size); // out_image_mem_size (in bytes) 601 605 if (xret != XRT_SUCCESS) { 602 606 return xret; 603 607 } ··· 628 632 std::vector<wil::unique_handle> &handles = compositorNeedsCopy ? data->comp_handles : data->handles; 629 633 630 634 // Import into the native compositor, to create the corresponding swapchain which we wrap. 631 - xret = xrt::compositor::client::importFromHandleDuplicates(*(c->xcn), handles, vkinfo, true, sc->xsc); 635 + xret = xrt::compositor::client::importFromHandleDuplicates(*(c->xcn), handles, vkinfo, image_mem_size, true, 636 + sc->xsc); 632 637 if (xret != XRT_SUCCESS) { 633 638 D3D_ERROR(c, "Error importing D3D swapchain into native compositor"); 634 639 return xret;
+5 -2
src/xrt/compositor/client/comp_d3d_common.hpp
··· 1 - // Copyright 2019-2022, Collabora, Ltd. 1 + // Copyright 2019-2024, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 6 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 7 * @author Jakob Bornecrantz <jakob@collabora.com> 8 8 * @author Fernando Velazquez Innella <finnella@magicleap.com> 9 + * @author Korcan Hussein <korcan.hussein@collabora.com> 9 10 * @ingroup comp_client 10 11 */ 11 12 #pragma once ··· 41 42 * @param xcn The native compositor 42 43 * @param handles A vector of uniquely-owned handles. These will be duplicated, not consumed, by this import. 43 44 * @param vkinfo The swapchain create info, with format as a Vulkan constant 45 + * @param image_mem_size The image memory allocation size in bytes 44 46 * @param use_dedicated_allocation Passed through to @ref xrt_image_native 45 47 * @param[out] out_xsc The swapchain to populate 46 48 * @return XRT_SUCCESS if everything went well, otherwise whatever error a call internally returned. ··· 49 51 importFromHandleDuplicates(xrt_compositor_native &xcn, 50 52 std::vector<wil::unique_handle> const &handles, 51 53 const struct xrt_swapchain_create_info &vkinfo, 54 + const std::uint64_t image_mem_size, 52 55 bool use_dedicated_allocation, 53 56 unique_swapchain_ref &out_xsc) 54 57 { ··· 65 68 wil::unique_handle duped{u_graphics_buffer_ref(handle.get())}; 66 69 xrt_image_native xin{}; 67 70 xin.handle = duped.get(); 68 - xin.size = 0; 71 + xin.size = image_mem_size; 69 72 xin.use_dedicated_allocation = use_dedicated_allocation; 70 73 71 74 handlesForImport.emplace_back(std::move(duped));