Monado OpenXR Runtime
Loading...
Searching...
No Matches
openvr_compositor.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// Copyright 2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Implementation of compositor-related functionality for OpenVR.
7 *
8 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
9 * @ingroup openvr
10 */
11
12#pragma once
13
14#include "xrt/xrt_compositor.h"
15#include "xrt/xrt_config_build.h"
16#include "xrt/xrt_device.h"
17#include "xrt/xrt_system.h"
18#include "xrt/xrt_gfx_vk.h"
19
20#ifdef XRT_HAVE_VULKAN
21#include "vk/vk_helpers.h"
22#include "vk/vk_cmd_pool.h"
23
26#endif
27
28#include "util/u_time.h"
29
31
32#include "openvr_interfaces_unified.h"
33
34#include "openvr_events.hpp"
35
36#include <array>
37#include <optional>
38#include <memory>
39
40
41namespace xrt::state_trackers::openvr {
42
44{
45 //! The swapchain in use
46 xrt_swapchain *xsc{nullptr};
47 //! The index into the eye's swapchain for the currently held texture.
49
50 //! The width of the currently held runtime swapchain texture.
51 uint32_t width;
52 //! The height of the currently held runtime swapchain texture.
53 uint32_t height;
54
55 //! The sub-image of the runtime swapchain texture to submit to the compositor.
57
59 {
60 if (this->xsc != nullptr) {
61 xrt_swapchain_release_image(this->xsc, this->swapchain_index);
62 }
63
64 xrt_swapchain_reference(&this->xsc, nullptr);
65 }
66};
67
69{
71};
72
74{
75 int64_t frame_id{-1};
76 timepoint_ns predicted_display_time{0};
77 time_duration_ns predicted_display_period{0};
78};
79
80/*!
81 * Since eye textures are created by the clients, and not the runtime, we have to do a copy into a runtime-owned
82 * swapchain, this structure cached created swapchains to avoid creating a new one every frame.
83 */
85{
86public: // Fields
87 xrt_swapchain *xsc{nullptr};
88
89 uint32_t width{0};
90 uint32_t height{0};
91 uint32_t storage_format{0};
92 uint32_t sample_format{0};
93
94public: // Methods
95 //! Ensures that there is a swapchain in the cache with the given properties, creating one if necessary.
98 uint32_t storage_format,
99 uint32_t sample_format,
100 uint32_t width,
101 uint32_t height,
102 xrt_swapchain_usage_bits usage_bits,
104
105 //! Resets the internal state of the cache, releasing any swapchain it holds.
106 void
107 reset();
108
110};
111
113{
114 std::array<xrt_fov, 2> fovs;
115 std::array<xrt_pose, 2> T_head_eyes;
116
117 xrt_pose head_relation;
118};
119
121{
122private: // Fields
123 //! The state for the current active frame
124 std::optional<FrameState> active_frame{std::nullopt};
125 //! The state for each submit eye.
126 std::array<std::optional<EyeState>, 2> frame_eye_states{std::nullopt, std::nullopt};
127 //! Caches of swapchains for each eye, keyed by the runtime storage/sample formats and eye image size.
128 std::array<SwapchainCache, 2> swapchain_caches{};
129
130 //! The active system devices
131 xrt_system_devices *xsysd{nullptr};
132
133 //! The current native compositor
134 xrt_compositor_native *xcn{nullptr};
135
136 xrt_system_compositor *xsysc{nullptr};
137
138 //! The currently active compositor, implicitly also has an active session.
139 xrt_compositor *active_compositor{nullptr};
140
141#ifdef XRT_HAVE_VULKAN
142 //! The active Vulkan compositor.
143 struct xrt_compositor *xc_vk{nullptr};
144
145 //! The vk_bundle for the compositor.
146 vk_bundle *vk{nullptr};
147
148 //! The command buffer pool for doing compositor work.
149 vk_cmd_pool *cmd_pool;
150
151 //! The shaders for the compositor.
152 render_shaders shaders;
153
154 struct
155 {
156 //! The sampler to use during the blit operation
157 VkSampler sampler{VK_NULL_HANDLE};
158
159 //! Vulkan pipeline cache object passed to vkCreateComputePipelines.
160 VkPipelineCache driver_pipeline_cache{VK_NULL_HANDLE};
161
162 //! Get-or-create cache for blit_ms.comp specialization variants.
163 struct render_blit_ms_pipeline_cache *pipeline_cache{nullptr};
164
165 //! Pipeline layout for blit pipelines.
166 VkPipelineLayout pipeline_layout{VK_NULL_HANDLE};
167
168 //! Descriptor pool for blit.
169 VkDescriptorPool descriptor_pool{VK_NULL_HANDLE};
170
171 //! Descriptor set layout for blit.
172 VkDescriptorSetLayout descriptor_set_layout{VK_NULL_HANDLE};
173
174 //! Descriptor set for blit.
175 VkDescriptorSet descriptor_set{VK_NULL_HANDLE};
176
177 //! The pipelines for blitting/copying app textures into runtime swapchain images.
178 std::array<VkPipeline, RENDER_BLIT_RESOLVE_COLOR_MODE_COUNT> pipelines{};
179 } blit;
180#endif
181
182 std::optional<RenderStateCache> render_state_cache{std::nullopt};
183
184 std::shared_ptr<Events> events{};
185
186public: // Fields
187 std::optional<timepoint_ns> last_predicted_display_time{std::nullopt};
188 std::optional<time_duration_ns> last_predicted_display_period{std::nullopt};
189
190 std::array<vr::TrackedDevicePose_t, vr::k_unMaxTrackedDeviceCount> last_render_poses{};
191 std::array<vr::TrackedDevicePose_t, vr::k_unMaxTrackedDeviceCount> last_game_poses{};
192
193 vr::ETrackingUniverseOrigin current_tracking_universe{vr::ETrackingUniverseOrigin::TrackingUniverseSeated};
194
195private: // Methods
196#ifdef XRT_HAVE_VULKAN
198 setupBlitPipelines(openvr_logger &logger);
199
200 //! Sets up the Vulkan compositor as the active compositor, and begins the session.
202 setupVulkanCompositor(openvr_logger &logger, vr::VRVulkanTextureData_t &vulkan_data);
203
204 void
205 destroyVulkanResources();
206
208 transferAppImageToSwapchainImage(openvr_logger &logger,
209 xrt_swapchain *xsc,
210 uint32_t dst_index,
211 vr::VRVulkanTextureData_t &texture_data,
212 VkFormat storage_format,
213 vr::EColorSpace color_space,
214 const xrt_rect &rect);
215
216 //! Handles submission of a Vulkan texture, which involves copying it into a runtime-owned swapchain image.
217 vr::EVRCompositorError
218 submitVulkan(openvr_logger &logger,
219 vr::EVREye eye,
220 vr::VRVulkanTextureData_t &texture_data,
221 vr::EColorSpace color_space,
222 const vr::VRTextureBounds_t &bounds);
223
224 void
225 getVulkanOutputDevice(openvr_logger &logger, uint64_t *out_device, VkInstance pInstance);
226#endif
227
228 //! Gets the projection layer data from the passed eye state
230 getProjectionLayerDataForEye(vr::EVREye eye, const EyeState &eye_state);
231
232 //! Completes a frame and submits textures to the runtime.
233 vr::EVRCompositorError
234 completeFrame(openvr_logger &logger);
235
236public: // Methods
240 std::shared_ptr<Events> &events);
241
242 ~Compositor();
243
244 /*!
245 * Waits for the next frame and begins it, cancelling/releasing any previously active frame and it's resources,
246 * if it exists.
247 */
248 vr::EVRCompositorError
250
251 //! Submits a texture for an eye, completing the frame if both eyes have been submit.
252 vr::EVRCompositorError
253 submit(openvr_logger &logger,
254 vr::EVREye eye,
255 const vr::Texture_t &texture,
256 const vr::VRTextureBounds_t &bounds,
257 vr::EVRSubmitFlags nSubmitFlags);
258
259 void
260 getOutputDevice(openvr_logger &logger,
261 uint64_t *out_device,
262 vr::ETextureType texture_type,
263 VkInstance pInstance);
264
266 getTimeForPredictions();
267
269 getFramePeriod();
270
272 getFrameRenderState(std::array<xrt_fov, 2> &fovs,
273 std::array<xrt_pose, 2> &T_head_eyes,
274 xrt_pose &head_relation);
275};
276
277}; // namespace xrt::state_trackers::openvr
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition u_time.h:88
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition xrt_compositor.h:563
xrt_swapchain_create_flags
Special flags for creating swapchain images.
Definition xrt_compositor.h:545
enum xrt_result xrt_result_t
Result type used across Monado.
Implementation of OpenVR event handling and related functionality.
Logging functions.
The NEW compositor rendering code header.
Shader loading interface.
Definition m_space.cpp:87
Holds all shaders.
Definition render_shaders_interface.h:26
Helper struct to make code easier to read.
Definition render_distortion.c:183
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition vk_helpers.h:81
Small helper to manage lock around a command pool.
Definition vk_cmd_pool.h:33
Definition openvr_compositor.hpp:121
vr::EVRCompositorError waitBeginFrame(openvr_logger &logger)
Waits for the next frame and begins it, cancelling/releasing any previously active frame and it's res...
Definition openvr_compositor.cpp:141
vr::EVRCompositorError submit(openvr_logger &logger, vr::EVREye eye, const vr::Texture_t &texture, const vr::VRTextureBounds_t &bounds, vr::EVRSubmitFlags nSubmitFlags)
Submits a texture for an eye, completing the frame if both eyes have been submit.
Definition openvr_compositor.cpp:207
Definition openvr_compositor.hpp:69
Definition openvr_compositor.hpp:44
xrt_swapchain * xsc
The swapchain in use.
Definition openvr_compositor.hpp:46
xrt_rect bounds
The sub-image of the runtime swapchain texture to submit to the compositor.
Definition openvr_compositor.hpp:56
uint32_t swapchain_index
The index into the eye's swapchain for the currently held texture.
Definition openvr_compositor.hpp:48
uint32_t width
The width of the currently held runtime swapchain texture.
Definition openvr_compositor.hpp:51
uint32_t height
The height of the currently held runtime swapchain texture.
Definition openvr_compositor.hpp:53
Definition openvr_compositor.hpp:74
Definition openvr_compositor.hpp:113
Since eye textures are created by the clients, and not the runtime, we have to do a copy into a runti...
Definition openvr_compositor.hpp:85
void reset()
Resets the internal state of the cache, releasing any swapchain it holds.
Definition openvr_compositor.cpp:94
xrt_result_t ensureSwapchain(xrt_compositor *xc, uint32_t storage_format, uint32_t sample_format, uint32_t width, uint32_t height, xrt_swapchain_usage_bits usage_bits, xrt_swapchain_create_flags flags)
Ensures that there is a swapchain in the cache with the given properties, creating one if necessary.
Definition openvr_compositor.cpp:31
Definition openvr_logger.hpp:30
Main compositor server interface.
Definition xrt_compositor.h:2218
Common compositor client interface/base.
Definition xrt_compositor.h:1046
All of the pure data values associated with a single view in a projection layer.
Definition xrt_compositor.h:261
A pose composed of a position and orientation.
Definition xrt_defines.h:502
Image rectangle.
Definition xrt_defines.h:467
Common swapchain interface/base.
Definition xrt_compositor.h:593
The system compositor handles composition for a system.
Definition xrt_compositor.h:2459
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition xrt_system.h:216
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
Command pool helpers.
Common Vulkan code header.
Header declaring XRT graphics interfaces.
Header defining an xrt display or controller device.
Header defining an XRT graphics provider.
Header for system objects.