Blender V5.3
GHOST_XrSwapchain.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cassert>
10
12#include "GHOST_XrException.hh"
13#include "GHOST_Xr_intern.hh"
14
15#include "GHOST_XrSwapchain.hh"
16
18 using ImageVec = std::vector<XrSwapchainImageBaseHeader *>;
19
20 XrSwapchain swapchain = XR_NULL_HANDLE;
22};
23
25 GHOST_IXrGraphicsBinding &gpu_binding)
26{
27 std::vector<XrSwapchainImageBaseHeader *> images;
28 uint32_t image_count;
29
30 CHECK_XR(xrEnumerateSwapchainImages(swapchain, 0, &image_count, nullptr),
31 "Failed to get count of swapchain images to create for the VR session.");
32 images = gpu_binding.createSwapchainImages(image_count);
33 CHECK_XR(xrEnumerateSwapchainImages(swapchain, images.size(), &image_count, images[0]),
34 "Failed to create swapchain images for the VR session.");
35
36 return images;
37}
38
40 const XrSession &session,
41 const XrViewConfigurationView &view_config)
42 : oxr_(std::make_unique<OpenXRSwapchainData>())
43{
44 XrSwapchainCreateInfo create_info = {XR_TYPE_SWAPCHAIN_CREATE_INFO};
45 uint32_t format_count = 0;
46
47 CHECK_XR(xrEnumerateSwapchainFormats(session, 0, &format_count, nullptr),
48 "Failed to get count of swapchain image formats.");
49 std::vector<int64_t> swapchain_formats(format_count);
50 CHECK_XR(xrEnumerateSwapchainFormats(
51 session, swapchain_formats.size(), &format_count, swapchain_formats.data()),
52 "Failed to get swapchain image formats.");
53 assert(swapchain_formats.size() == format_count);
54
55 std::optional chosen_format = gpu_binding.chooseSwapchainFormat(
56 swapchain_formats, format_, is_srgb_buffer_);
57 if (!chosen_format) {
59 "Error: No format matching OpenXR runtime supported swapchain formats found.");
60 }
61 gpu_format_ = *chosen_format;
62
63 create_info.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_TRANSFER_DST_BIT |
64 XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
65 create_info.format = *chosen_format;
66 create_info.sampleCount = view_config.recommendedSwapchainSampleCount;
67 create_info.width = view_config.recommendedImageRectWidth;
68 create_info.height = view_config.recommendedImageRectHeight;
69 create_info.faceCount = 1;
70 create_info.arraySize = 1;
71 create_info.mipCount = 1;
72
73 CHECK_XR(xrCreateSwapchain(session, &create_info, &oxr_->swapchain),
74 "Failed to create OpenXR swapchain.");
75
76 image_width_ = create_info.width;
77 image_height_ = create_info.height;
78
79 oxr_->swapchain_images = swapchain_images_create(oxr_->swapchain, gpu_binding);
80}
81
83 : oxr_(std::move(other.oxr_)),
84 image_width_(other.image_width_),
85 image_height_(other.image_height_),
86 format_(other.format_),
87 gpu_format_(other.gpu_format_),
88 is_srgb_buffer_(other.is_srgb_buffer_)
89{
90 /* Prevent xrDestroySwapchain call for the moved out item. */
91 other.oxr_ = nullptr;
92}
93
95{
96 /* oxr_ may be nullptr after move. */
97 if (oxr_ && oxr_->swapchain != XR_NULL_HANDLE) {
98 CHECK_XR_ASSERT(xrDestroySwapchain(oxr_->swapchain));
99 }
100}
101
103
104{
105 XrSwapchainImageAcquireInfo acquire_info = {XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO};
106 XrSwapchainImageWaitInfo wait_info = {XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO};
107 uint32_t image_idx;
108
109 CHECK_XR(xrAcquireSwapchainImage(oxr_->swapchain, &acquire_info, &image_idx),
110 "Failed to acquire swapchain image for the VR session.");
111 wait_info.timeout = XR_INFINITE_DURATION;
112 CHECK_XR(xrWaitSwapchainImage(oxr_->swapchain, &wait_info),
113 "Failed to acquire swapchain image for the VR session.");
114
115 return oxr_->swapchain_images[image_idx];
116}
117
119{
120 r_sub_image.swapchain = oxr_->swapchain;
121 r_sub_image.imageRect.offset = {0, 0};
122 r_sub_image.imageRect.extent = {image_width_, image_height_};
123}
124
125GHOST_TXrSwapchainFormat GHOST_XrSwapchain::getFormat() const
126{
127 return format_;
128}
129
131{
132 return gpu_format_;
133}
134
136{
137 return is_srgb_buffer_;
138}
139
141{
142 XrSwapchainImageReleaseInfo release_info = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO};
143
144 CHECK_XR(xrReleaseSwapchainImage(oxr_->swapchain, &release_info),
145 "Failed to release swapchain image used to submit VR session frame.");
146}
static OpenXRSwapchainData::ImageVec swapchain_images_create(XrSwapchain swapchain, GHOST_IXrGraphicsBinding &gpu_binding)
#define CHECK_XR_ASSERT(call)
#define CHECK_XR(call, error_msg)
long long int int64_t
virtual std::vector< XrSwapchainImageBaseHeader * > createSwapchainImages(uint32_t image_count)=0
virtual std::optional< int64_t > chooseSwapchainFormat(const std::vector< int64_t > &runtime_formats, GHOST_TXrSwapchainFormat &r_format, bool &r_is_rgb_format) const =0
GHOST_XrException(const char *msg, int result=0)
int64_t getGPUFormat() const
return the swapchain format selected by the graphic binding.
GHOST_XrSwapchain(GHOST_IXrGraphicsBinding &gpu_binding, const XrSession &session, const XrViewConfigurationView &view_config)
GHOST_TXrSwapchainFormat getFormat() const
void updateCompositionLayerProjectViewSubImage(XrSwapchainSubImage &r_sub_image)
XrSwapchainImageBaseHeader * acquireDrawableSwapchainImage()
#define assert(assertion)
std::vector< XrSwapchainImageBaseHeader * > ImageVec