Monado OpenXR Runtime
Loading...
Searching...
No Matches
openvr_compositor.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Implementation of compositor-related functionality for OpenVR.
6 *
7 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
8 * @ingroup openvr
9 */
10
11#pragma once
12
13#include "xrt/xrt_compositor.h"
14#include "xrt/xrt_config_build.h"
15#include "xrt/xrt_device.h"
16#include "xrt/xrt_system.h"
17#include "xrt/xrt_gfx_vk.h"
18
19#include "util/u_time.h"
20
22
23#include "openvr_interfaces_unified.h"
24
25#include "openvr_events.hpp"
26
27#include <array>
28#include <optional>
29#include <memory>
30
31
32namespace xrt::state_trackers::openvr {
33
35{
36 //! The swapchain in use
37 xrt_swapchain *xsc{nullptr};
38 //! The index into the eye's swapchain for the currently held texture.
40
41 //! The format of the currently held texture.
42 uint32_t format;
43 //! The width of the currently held texture.
44 uint32_t width;
45 //! The height of the currently held texture.
46 uint32_t height;
47 //! The sample count of the currently held texture.
48 uint32_t sample_count;
49
50 //! The bounds of the submitted texture.
52
54 {
55 if (this->xsc != nullptr) {
56 xrt_swapchain_release_image(this->xsc, this->swapchain_index);
57 }
58
59 xrt_swapchain_reference(&this->xsc, nullptr);
60 }
61};
62
64{
66};
67
69{
70 int64_t frame_id{-1};
71 timepoint_ns predicted_display_time{0};
72 time_duration_ns predicted_display_period{0};
73};
74
75/*!
76 * Since eye textures are created by the clients, and not the runtime, we have to do a copy into a runtime-owned
77 * swapchain, this structure cached created swapchains to avoid creating a new one every frame.
78 */
80{
81public: // Fields
82 xrt_swapchain *xsc{nullptr};
83
84 uint32_t width{0};
85 uint32_t height{0};
86 uint32_t format{0};
87 uint32_t sample_count{0};
88
89public: // Methods
90 //! Ensures that there is a swapchain in the cache with the given properties, creating one if necessary.
92 EnsureSwapchain(xrt_compositor *xc, uint32_t format, uint32_t width, uint32_t height, uint32_t sample_count);
93
95};
96
98{
99 std::array<xrt_fov, 2> fovs;
100 std::array<xrt_pose, 2> T_head_eyes;
101
102 xrt_pose head_relation;
103};
104
106{
107private: // Fields
108 //! The state for the current active frame
109 std::optional<FrameState> active_frame{std::nullopt};
110 //! The state for each submit eye.
111 std::array<std::optional<EyeState>, 2> frame_eye_states{std::nullopt, std::nullopt};
112 //! Caches of swapchains for each eye, keyed by the format/size/sample count of the submitted texture.
113 std::array<SwapchainCache, 2> swapchain_caches{};
114
115 //! The active system devices
116 xrt_system_devices *xsysd{nullptr};
117
118 //! The current native compositor
119 xrt_compositor_native *xcn{nullptr};
120
121 xrt_system_compositor *xsysc{nullptr};
122
123 //! The currently active compositor, implicitly also has an active session.
124 xrt_compositor *active_compositor{nullptr};
125
126#ifdef XRT_HAVE_VULKAN
127 struct xrt_compositor_vk *xc_vk{nullptr};
128#endif
129
130 std::optional<RenderStateCache> render_state_cache{std::nullopt};
131
132 std::shared_ptr<Events> events{};
133
134public: // Fields
135 std::optional<timepoint_ns> last_predicted_display_time{std::nullopt};
136 std::optional<time_duration_ns> last_predicted_display_period{std::nullopt};
137
138 std::array<vr::TrackedDevicePose_t, vr::k_unMaxTrackedDeviceCount> last_render_poses{};
139 std::array<vr::TrackedDevicePose_t, vr::k_unMaxTrackedDeviceCount> last_game_poses{};
140
141 vr::ETrackingUniverseOrigin current_tracking_universe{vr::ETrackingUniverseOrigin::TrackingUniverseSeated};
142
143private: // Methods
144#ifdef XRT_HAVE_VULKAN
145 //! Sets up the Vulkan compositor as the active compositor, and begins the session.
147 SetupVulkanCompositor(openvr_logger &logger, vr::VRVulkanTextureData_t &vulkan_data);
148
150 BlitAppImageToSwapchainImage(openvr_logger &logger,
151 xrt_swapchain *xsc,
152 uint32_t dst_index,
153 vr::VRVulkanTextureData_t &texture_data,
154 vr::EColorSpace color_space,
155 const xrt_rect &rect);
156
157 //! Handles submission of a Vulkan texture, which involves copying it into a runtime-owned swapchain image.
158 vr::EVRCompositorError
159 SubmitVulkan(openvr_logger &logger,
160 vr::EVREye eye,
161 vr::VRVulkanTextureData_t &texture_data,
162 vr::EColorSpace color_space,
163 const vr::VRTextureBounds_t &bounds);
164
165 void
166 GetVulkanOutputDevice(openvr_logger &logger, uint64_t *out_device, VkInstance pInstance);
167#endif
168
169 //! Gets the projection layer data from the passed eye state
171 GetProjectionLayerDataForEye(vr::EVREye eye, const EyeState &eye_state);
172
173 //! Completes a frame and submits textures to the runtime.
174 vr::EVRCompositorError
175 CompleteFrame(openvr_logger &logger);
176
177public: // Methods
181 std::shared_ptr<Events> &events);
182
183 ~Compositor();
184
185 /*!
186 * Waits for the next frame and begins it, cancelling/releasing any previously active frame and it's resources,
187 * if it exists.
188 */
189 vr::EVRCompositorError
191
192 //! Submits a texture for an eye, completing the frame if both eyes have been submit.
193 vr::EVRCompositorError
194 Submit(openvr_logger &logger,
195 vr::EVREye eye,
196 const vr::Texture_t &texture,
197 const vr::VRTextureBounds_t &bounds,
198 vr::EVRSubmitFlags nSubmitFlags);
199
200 void
201 GetOutputDevice(openvr_logger &logger,
202 uint64_t *out_device,
203 vr::ETextureType texture_type,
204 VkInstance pInstance);
205
207 GetTimeForPredictions();
208
210 GetFramePeriod();
211
213 GetFrameRenderState(std::array<xrt_fov, 2> &fovs,
214 std::array<xrt_pose, 2> &T_head_eyes,
215 xrt_pose &head_relation);
216};
217
218}; // 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
enum xrt_result xrt_result_t
Result type used across Monado.
Implementation of OpenVR event handling and related functionality.
Logging functions.
Helper struct to make code easier to read.
Definition render_distortion.c:183
Definition openvr_compositor.hpp:106
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:173
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:107
Definition openvr_compositor.hpp:64
Definition openvr_compositor.hpp:35
xrt_swapchain * xsc
The swapchain in use.
Definition openvr_compositor.hpp:37
xrt_rect bounds
The bounds of the submitted texture.
Definition openvr_compositor.hpp:51
uint32_t swapchain_index
The index into the eye's swapchain for the currently held texture.
Definition openvr_compositor.hpp:39
uint32_t width
The width of the currently held texture.
Definition openvr_compositor.hpp:44
uint32_t height
The height of the currently held texture.
Definition openvr_compositor.hpp:46
uint32_t format
The format of the currently held texture.
Definition openvr_compositor.hpp:42
uint32_t sample_count
The sample count of the currently held texture.
Definition openvr_compositor.hpp:48
Definition openvr_compositor.hpp:69
Definition openvr_compositor.hpp:98
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:80
xrt_result_t EnsureSwapchain(xrt_compositor *xc, uint32_t format, uint32_t width, uint32_t height, uint32_t sample_count)
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:2268
Base class for a Vulkan client compositor.
Definition xrt_compositor.h:2063
Common compositor client interface/base.
Definition xrt_compositor.h:1024
All of the pure data values associated with a single view in a projection layer.
Definition xrt_compositor.h:239
A pose composed of a position and orientation.
Definition xrt_defines.h:492
Image rectangle.
Definition xrt_defines.h:457
Common swapchain interface/base.
Definition xrt_compositor.h:571
The system compositor handles composition for a system.
Definition xrt_compositor.h:2496
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition xrt_system.h:215
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
Header declaring XRT graphics interfaces.
Header defining an xrt display or controller device.
Header defining an XRT graphics provider.
Header for system objects.