Monado OpenXR Runtime
oxr_conversions.h
Go to the documentation of this file.
1// Copyright 2018-2024, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Smaller helper functions to convert between xrt and OpenXR things.
6 * @author Christoph Haag <christoph.haag@collabora.com>
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup oxr_main
9 */
10
11#pragma once
12
13#include "xrt/xrt_defines.h"
14#include "xrt/xrt_space.h"
17
18
19/*
20 *
21 * Space things.
22 *
23 */
24
25static inline XrSpaceLocationFlags
26xrt_to_xr_space_location_flags(enum xrt_space_relation_flags relation_flags)
27{
28 // clang-format off
29 bool valid_ori = (relation_flags & XRT_SPACE_RELATION_ORIENTATION_VALID_BIT) != 0;
30 bool tracked_ori = (relation_flags & XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT) != 0;
31 bool valid_pos = (relation_flags & XRT_SPACE_RELATION_POSITION_VALID_BIT) != 0;
32 bool tracked_pos = (relation_flags & XRT_SPACE_RELATION_POSITION_TRACKED_BIT) != 0;
33
34 bool linear_vel = (relation_flags & XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT) != 0;
35 bool angular_vel = (relation_flags & XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT) != 0;
36 // clang-format on
37
38 XrSpaceLocationFlags location_flags = (XrSpaceLocationFlags)0;
39 if (valid_ori) {
40 location_flags |= XR_SPACE_LOCATION_ORIENTATION_VALID_BIT;
41 }
42 if (tracked_ori) {
43 location_flags |= XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT;
44 }
45 if (valid_pos) {
46 location_flags |= XR_SPACE_LOCATION_POSITION_VALID_BIT;
47 }
48 if (tracked_pos) {
49 location_flags |= XR_SPACE_LOCATION_POSITION_TRACKED_BIT;
50 }
51 if (linear_vel) {
52 location_flags |= XR_SPACE_VELOCITY_LINEAR_VALID_BIT;
53 }
54 if (angular_vel) {
55 location_flags |= XR_SPACE_VELOCITY_ANGULAR_VALID_BIT;
56 }
57
58 return location_flags;
59}
60
61static inline XrReferenceSpaceType
62oxr_ref_space_to_xr(enum oxr_space_type space_type)
63{
64 switch (space_type) {
65 case OXR_SPACE_TYPE_REFERENCE_VIEW: return XR_REFERENCE_SPACE_TYPE_VIEW;
66 case OXR_SPACE_TYPE_REFERENCE_LOCAL: return XR_REFERENCE_SPACE_TYPE_LOCAL;
67 case OXR_SPACE_TYPE_REFERENCE_LOCAL_FLOOR: return XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT;
68 case OXR_SPACE_TYPE_REFERENCE_STAGE: return XR_REFERENCE_SPACE_TYPE_STAGE;
69 case OXR_SPACE_TYPE_REFERENCE_UNBOUNDED_MSFT: return XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT;
70 case OXR_SPACE_TYPE_REFERENCE_COMBINED_EYE_VARJO: return XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO;
71 case OXR_SPACE_TYPE_REFERENCE_LOCALIZATION_MAP_ML: return XR_REFERENCE_SPACE_TYPE_LOCALIZATION_MAP_ML;
72
73 case OXR_SPACE_TYPE_ACTION: return XR_REFERENCE_SPACE_TYPE_MAX_ENUM;
74 case OXR_SPACE_TYPE_XDEV_POSE: return XR_REFERENCE_SPACE_TYPE_MAX_ENUM;
75 }
76
77 return XR_REFERENCE_SPACE_TYPE_MAX_ENUM;
78}
79
80static inline enum oxr_space_type
81xr_ref_space_to_oxr(XrReferenceSpaceType space_type)
82{
83 switch (space_type) {
84 case XR_REFERENCE_SPACE_TYPE_VIEW: return OXR_SPACE_TYPE_REFERENCE_VIEW;
85 case XR_REFERENCE_SPACE_TYPE_LOCAL: return OXR_SPACE_TYPE_REFERENCE_LOCAL;
86 case XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT: return OXR_SPACE_TYPE_REFERENCE_LOCAL_FLOOR;
87 case XR_REFERENCE_SPACE_TYPE_STAGE: return OXR_SPACE_TYPE_REFERENCE_STAGE;
88 case XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT: return OXR_SPACE_TYPE_REFERENCE_UNBOUNDED_MSFT;
89 case XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO: return OXR_SPACE_TYPE_REFERENCE_COMBINED_EYE_VARJO;
90 case XR_REFERENCE_SPACE_TYPE_LOCALIZATION_MAP_ML: return OXR_SPACE_TYPE_REFERENCE_LOCALIZATION_MAP_ML;
91
92 case XR_REFERENCE_SPACE_TYPE_MAX_ENUM: return (enum oxr_space_type) - 1;
93 }
94
95 // wrap around or negative depending on enum data type, invalid value either way.
96 return (enum oxr_space_type) - 1;
97}
98
99static inline const char *
100xr_ref_space_to_string(XrReferenceSpaceType space_type)
101{
102 switch (space_type) {
103 case XR_REFERENCE_SPACE_TYPE_VIEW: return "XR_REFERENCE_SPACE_TYPE_VIEW";
104 case XR_REFERENCE_SPACE_TYPE_LOCAL: return "XR_REFERENCE_SPACE_TYPE_LOCAL";
105 case XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT: return "XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT";
106 case XR_REFERENCE_SPACE_TYPE_STAGE: return "XR_REFERENCE_SPACE_TYPE_STAGE";
107 case XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT: return "XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT";
108 case XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO: return "XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO";
109 case XR_REFERENCE_SPACE_TYPE_MAX_ENUM: return "XR_REFERENCE_SPACE_TYPE_MAX_ENUM";
110 default: return "UNKNOWN REFERENCE SPACE";
111 }
112}
113
114static inline enum xrt_reference_space_type
115oxr_ref_space_to_xrt(enum oxr_space_type space_type)
116{
117 switch (space_type) {
118 case OXR_SPACE_TYPE_REFERENCE_VIEW: return XRT_SPACE_REFERENCE_TYPE_VIEW;
119 case OXR_SPACE_TYPE_REFERENCE_LOCAL: return XRT_SPACE_REFERENCE_TYPE_LOCAL;
120 case OXR_SPACE_TYPE_REFERENCE_LOCAL_FLOOR: return XRT_SPACE_REFERENCE_TYPE_LOCAL_FLOOR;
121 case OXR_SPACE_TYPE_REFERENCE_STAGE: return XRT_SPACE_REFERENCE_TYPE_STAGE;
122 case OXR_SPACE_TYPE_REFERENCE_UNBOUNDED_MSFT: return XRT_SPACE_REFERENCE_TYPE_UNBOUNDED;
123
124 // Has no mapping to a Monado semantic space.
125 case OXR_SPACE_TYPE_REFERENCE_COMBINED_EYE_VARJO: return XRT_SPACE_REFERENCE_TYPE_INVALID;
126 case OXR_SPACE_TYPE_REFERENCE_LOCALIZATION_MAP_ML: return XRT_SPACE_REFERENCE_TYPE_INVALID;
127 case OXR_SPACE_TYPE_ACTION: return XRT_SPACE_REFERENCE_TYPE_INVALID;
128 case OXR_SPACE_TYPE_XDEV_POSE: return XRT_SPACE_REFERENCE_TYPE_INVALID;
129 }
130
131 /*
132 * This is the default case, we do not have a explicit default case
133 * so that we get warnings for unhandled enum members. This is fine
134 * because the C specification says if there is no default case and
135 * and a non-matching value is given no case is executed.
136 */
137
139}
140
141static inline enum xrt_reference_space_type
142xr_ref_space_to_xrt(XrReferenceSpaceType space_type)
143{
144 switch (space_type) {
145 case XR_REFERENCE_SPACE_TYPE_VIEW: return XRT_SPACE_REFERENCE_TYPE_VIEW;
146 case XR_REFERENCE_SPACE_TYPE_LOCAL: return XRT_SPACE_REFERENCE_TYPE_LOCAL;
147 case XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT: return XRT_SPACE_REFERENCE_TYPE_LOCAL_FLOOR;
148 case XR_REFERENCE_SPACE_TYPE_STAGE: return XRT_SPACE_REFERENCE_TYPE_STAGE;
149 case XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT: return XRT_SPACE_REFERENCE_TYPE_UNBOUNDED;
150
151 case XR_REFERENCE_SPACE_TYPE_COMBINED_EYE_VARJO: return XRT_SPACE_REFERENCE_TYPE_INVALID;
152 case XR_REFERENCE_SPACE_TYPE_LOCALIZATION_MAP_ML: return XRT_SPACE_REFERENCE_TYPE_INVALID;
153 case XR_REFERENCE_SPACE_TYPE_MAX_ENUM: return XRT_SPACE_REFERENCE_TYPE_INVALID;
154 }
155
157}
158
159/*
160 *
161 * IO things.
162 *
163 */
164
165static inline const char *
166xrt_input_type_to_str(enum xrt_input_type type)
167{
168 // clang-format off
169 switch (type) {
170 case XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE: return "XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE";
171 case XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE: return "XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE";
172 case XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE: return "XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE";
173 case XRT_INPUT_TYPE_VEC3_MINUS_ONE_TO_ONE: return "XRT_INPUT_TYPE_VEC3_MINUS_ONE_TO_ONE";
174 case XRT_INPUT_TYPE_BOOLEAN: return "XRT_INPUT_TYPE_BOOLEAN";
175 case XRT_INPUT_TYPE_POSE: return "XRT_INPUT_TYPE_POSE";
176 default: return "XRT_INPUT_UNKNOWN";
177 }
178 // clang-format on
179}
180
181static inline enum xrt_perf_set_level
182xr_perf_level_to_xrt(XrPerfSettingsLevelEXT level)
183{
184 switch (level) {
185 case XR_PERF_SETTINGS_LEVEL_POWER_SAVINGS_EXT: return XRT_PERF_SET_LEVEL_POWER_SAVINGS;
186 case XR_PERF_SETTINGS_LEVEL_SUSTAINED_LOW_EXT: return XRT_PERF_SET_LEVEL_SUSTAINED_LOW;
187 case XR_PERF_SETTINGS_LEVEL_SUSTAINED_HIGH_EXT: return XRT_PERF_SET_LEVEL_SUSTAINED_HIGH;
188 case XR_PERF_SETTINGS_LEVEL_BOOST_EXT: return XRT_PERF_SET_LEVEL_BOOST;
189 default: assert(false); return 0;
190 }
191}
192
193static inline enum xrt_perf_domain
194xr_perf_domain_to_xrt(XrPerfSettingsDomainEXT domain)
195{
196 switch (domain) {
197 case XR_PERF_SETTINGS_DOMAIN_CPU_EXT: return XRT_PERF_DOMAIN_CPU;
198 case XR_PERF_SETTINGS_DOMAIN_GPU_EXT: return XRT_PERF_DOMAIN_GPU;
199 default: assert(false); return 0;
200 }
201}
202
203static inline XrPerfSettingsDomainEXT
204xrt_perf_domain_to_xr(enum xrt_perf_domain domain)
205{
206 switch (domain) {
207 case XRT_PERF_DOMAIN_CPU: return XR_PERF_SETTINGS_DOMAIN_CPU_EXT;
208 case XRT_PERF_DOMAIN_GPU: return XR_PERF_SETTINGS_DOMAIN_GPU_EXT;
209 default: assert(false); return 0;
210 }
211}
212
213static inline XrPerfSettingsSubDomainEXT
214xrt_perf_sub_domain_to_xr(enum xrt_perf_sub_domain subDomain)
215{
216 switch (subDomain) {
217 case XRT_PERF_SUB_DOMAIN_COMPOSITING: return XR_PERF_SETTINGS_SUB_DOMAIN_COMPOSITING_EXT;
218 case XRT_PERF_SUB_DOMAIN_RENDERING: return XR_PERF_SETTINGS_SUB_DOMAIN_RENDERING_EXT;
219 case XRT_PERF_SUB_DOMAIN_THERMAL: return XR_PERF_SETTINGS_SUB_DOMAIN_THERMAL_EXT;
220 default: assert(false); return 0;
221 }
222}
223
224static inline XrPerfSettingsNotificationLevelEXT
225xrt_perf_notify_level_to_xr(enum xrt_perf_notify_level level)
226{
227 switch (level) {
228 case XRT_PERF_NOTIFY_LEVEL_NORMAL: return XR_PERF_SETTINGS_NOTIF_LEVEL_NORMAL_EXT;
229 case XRT_PERF_NOTIFY_LEVEL_WARNING: return XR_PERF_SETTINGS_NOTIF_LEVEL_WARNING_EXT;
230 case XRT_PERF_NOTIFY_LEVEL_IMPAIRED: return XR_PERF_SETTINGS_NOTIF_LEVEL_IMPAIRED_EXT;
231 default: assert(false); return 0;
232 }
233}
oxr_space_type
Internal enum for the type of space, lets us reason about action spaces.
Definition: oxr_defines.h:102
xrt_input_type
Base type of this inputs.
Definition: xrt_defines.h:802
xrt_reference_space_type
Type of a OpenXR mapped reference space, maps to the semantic spaces on the xrt_space_overseer struct...
Definition: xrt_defines.h:599
#define XRT_SPACE_REFERENCE_TYPE_INVALID
An invalid xrt_reference_space_type, since it's invalid it's not listed in the enum.
Definition: xrt_defines.h:620
xrt_space_relation_flags
Flags of which components of a xrt_space_relation is valid.
Definition: xrt_defines.h:629
@ XRT_INPUT_TYPE_POSE
A tracked pose.
Definition: xrt_defines.h:815
@ XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE
Vec2 input, components in [-1, 1].
Definition: xrt_defines.h:809
@ XRT_INPUT_TYPE_VEC3_MINUS_ONE_TO_ONE
Vec3 input, components in [-1, 1].
Definition: xrt_defines.h:811
@ XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE
Float input in [-1, 1].
Definition: xrt_defines.h:807
@ XRT_INPUT_TYPE_BOOLEAN
Boolean (digital, binary) input.
Definition: xrt_defines.h:813
@ XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE
Float input in [0, 1].
Definition: xrt_defines.h:805
Common defines and enums for XRT.
xrt_perf_notify_level
Performance level.
Definition: xrt_defines.h:1953
xrt_perf_domain
Domain type.
Definition: xrt_defines.h:1926
xrt_perf_set_level
Performance level.
Definition: xrt_defines.h:1942
Include all of the openxr headers in one place.
Header defining xrt space and space overseer.
Include all of the Vulkan headers in one place, and cope with any "messy" includes implied by it.