Debian Codegraph @

3rdparty/osgXR/src/OpenXR/Session.h

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-only
2 // Copyright (C) 2021 James Hogan <[email protected]>
3
4 #ifndef OSGXR_OPENXR_SESSION
5 #define OSGXR_OPENXR_SESSION 1
6
7 #include "Path.h"
8 #include "System.h"
9
10 #include <osg/Geometry>
11 #include <osg/Referenced>
12 #include <osg/ref_ptr>
13 #include <osgViewer/GraphicsWindow>
14 #include <OpenThreads/Mutex>
15
16 #include <set>
17
18 namespace osgXR {
19
20 namespace OpenXR {
21
22 class Action;
23 class ActionSet;
24 class CompositionLayer;
25 class Space;
26
27 class Session : public osg::Referenced
28 {
29 public:
30
31 // GL context must not be bound in another thread
32 Session(System *system, osgViewer::GraphicsWindow *window);
33 // GL context must not be bound in another thread
34 virtual ~Session();
35
36 // GL context must not be bound in another thread
37 void releaseGLObjects(osg::State *state = nullptr);
38
39 // Error checking
40
41 inline bool valid() const
42 {
43 return _session != XR_NULL_HANDLE;
44 }
45
46 bool check(XrResult result, const char *actionMsg) const;
47
48 // Action set attachment
49
50 /// Add an action set to the list.
51 void addActionSet(ActionSet *actionSet);
52 /**
53 * Attach the added action sets to the OpenXR session.
54 * @return true on success, false on failure.
55 */
56 bool attachActionSets();
57
58 /// Get the current interaction profile for the given subaction path.
59 Path getCurrentInteractionProfile(const Path &subactionPath) const;
60
61 /// Get a list of bound source paths for an action.
62 bool getActionBoundSources(Action *action,
63 std::vector<XrPath> &sourcePaths) const;
64
65 /**
66 * Get a localized name for an input source.
67 * @param sourcePath Input source path.
68 * @param whichComponents Which components to include.
69 * @return Localized name string
70 */
71 std::string getInputSourceLocalizedName(XrPath sourcePath,
72 XrInputSourceLocalizedNameFlags whichComponents) const;
73
74 // Action syncing
75
76 /// Activate a certain action set.
77 void activateActionSet(ActionSet *actionSet,
78 Path subactionPath = Path());
79 /// Deactivate a certain action set.
80 void deactivateActionSet(ActionSet *actionSet,
81 Path subactionPath = Path());
82 /// Sync active action sets.
83 bool syncActions();
84
85 /// Get the number of action sync counts that have taken place.
86 unsigned int getActionSyncCount() const
87 {
88 return _actionSyncCount;
89 }
90
91 // Accessors
92
93 // Find whether the session is ready to begin
94 inline bool isReady() const
95 {
96 return _state == XR_SESSION_STATE_READY;
97 }
98
99 // Find whether the session is running
100 inline bool isRunning() const
101 {
102 return _running;
103 }
104
105 // Find whether the session is already in the process of exiting
106 inline bool isExiting() const
107 {
108 return _exiting;
109 }
110
111 // Find whether the session has been lost
112 inline bool isLost() const
113 {
114 return _lost || _instance->lost();
115 }
116
117 inline osgViewer::GraphicsWindow *getWindow() const
118 {
119 return _window.get();
120 }
121
122 // State management
123
124 inline XrSessionState getState() const
125 {
126 return _state;
127 }
128
129 inline void setState(XrSessionState state)
130 {
131 _state = state;
132 }
133
134
135 // Conversions
136
137 inline const osg::ref_ptr<Instance> getInstance() const
138 {
139 return _instance;
140 }
141
142 inline const System *getSystem() const
143 {
144 return _system;
145 }
146
147 inline XrInstance getXrInstance() const
148 {
149 return _system->getXrInstance();
150 }
151
152 inline XrSystemId getXrSystemId() const
153 {
154 return _system->getXrSystemId();
155 }
156
157 inline XrSession getXrSession()
158 {
159 return _session;
160 }
161
162 // Queries
163
164 typedef std::vector<int64_t> SwapchainFormats;
165 const SwapchainFormats &getSwapchainFormats() const;
166
167 Space *getLocalSpace();
168 XrTime getLastDisplayTime() const
169 {
170 return _lastDisplayTime;
171 }
172
173 void updateVisibilityMasks(XrViewConfigurationType viewConfigurationType,
174 uint32_t viewIndex);
175 osg::ref_ptr<osg::Geometry> getVisibilityMask(uint32_t viewIndex,
176 XrVisibilityMaskTypeKHR visibilityMaskType,
177 bool force = false);
178
179 // Operations
180
181 /**
182 * Check whether the session's GLX context is current.
183 * This is intended for internal use only by the functions below.
184 * @return whether the session's GLX context is current.
185 */
186 bool checkCurrent() const;
187
188 /**
189 * Make the GLX context current.
190 * This makes the GLX context current to workaround broken XR runtimes.
191 * It should be used to ensure the context is current before XR calls
192 * affected by QUIRK_GL_CONTEXT_IGNORED if shouldSwitchContext()
193 * (because the XR runtime will fail to do so), and to restore the
194 * context after XR calls affected by QUIRK_GL_CONTEXT_CLEARED if
195 * shouldRestoreContext() or getRestoreAction() == CONTEXT_RESTORE
196 * (because the XR runtime will have explicitly released the context).
197 */
198 void makeCurrent() const;
199 /**
200 * Release the GLX context so that none is current.
201 * This makes no GLX context current to workaround broken XR runtimes.
202 * It should be used release the switched context after XR calls
203 * affected by QUIRK_GL_CONTEXT_IGNORED if shouldSwitchContext()
204 * (because we had to call makeCurrent() before the call), and after XR
205 * calls affected by QUIRK_GL_CONTEXT_CHANGED if getRestoreAction() ==
206 * CONTEXT_RELEASE (because the XR runtime failed to do so).
207 */
208 void releaseContext() const;
209
210 /// Action to perform before or after XR call due to quirks.
211 typedef enum ContextAction {
212 /// No action is necessary.
213 CONTEXT_IGNORE = 0,
214 /// The GLX context should be restored.
215 CONTEXT_RESTORE,
216 /**
217 * The GLX context should be released leaving no context current.
218 * It is assumed that no other context will need restoring in its
219 * place.
220 */
221 CONTEXT_RELEASE
222 } ContextAction;
223
224 /**
225 * Find whether for an XR call affected by QUIRK_GL_CONTEXT_IGNORED it
226 * is necessary to switch the context prior to the XR call with
227 * makeCurrent(), and also release it afterwards with releaseContext.
228 * @return true if the context should be switched and released,
229 * false otherwise.
230 */
231 bool shouldSwitchContext() const
232 {
233 return _instance->getQuirk(QUIRK_GL_CONTEXT_IGNORED) &&
234 !checkCurrent();
235 }
236 /**
237 * Find whether for an XR call affected by QUIRK_GL_CONTEXT_CLEARED it
238 * is necessary to restore the context after the XR call.
239 * @return true if the context should be restored after,
240 * false otherwise.
241 */
242 bool shouldRestoreContext() const
243 {
244 return _instance->getQuirk(QUIRK_GL_CONTEXT_CLEARED) &&
245 checkCurrent();
246 }
247 /**
248 * Find what GL context action to perform after an XR call affected by
249 * QUIRK_GL_CONTEXT_CLEARED or QUIRK_GL_CONTEXT_CHANGED.
250 * @return CONTEXT_RESTORE if the runtime may clear the context and it
251 * will need restoring.
252 * CONTEXT_RELEASE if the runtime may change the context and
253 * fail to release it.
254 * CONTEXT_IGNORE otherwise.
255 */
256 ContextAction getRestoreAction() const
257 {
258 bool cleared = _instance->getQuirk(QUIRK_GL_CONTEXT_CLEARED);
259 bool changed = _instance->getQuirk(QUIRK_GL_CONTEXT_CHANGED);
260 if (cleared || changed)
261 {
262 bool current = checkCurrent();
263 if (cleared && current)
264 return CONTEXT_RESTORE;
265 if (changed && !current)
266 return CONTEXT_RELEASE;
267 }
268 return CONTEXT_IGNORE;
269 }
270
271
272 bool begin(const System::ViewConfiguration &viewConfiguration);
273 void end();
274 void requestExit();
275
276 const System::ViewConfiguration *getViewConfiguration() const
277 {
278 return _viewConfiguration;
279 }
280
281 class Frame : public osg::Referenced
282 {
283 public:
284
285 Frame(osg::ref_ptr<Session> session, XrFrameState *frameState);
286
287 virtual ~Frame();
288
289 // Error checking
290
291 inline bool check(XrResult result, const char *actionMsg) const
292 {
293 return _session->check(result, actionMsg);
294 }
295
296 // Accessors
297
298 inline Session *getSession()
299 {
300 return _session;
301 }
302
303 inline bool shouldRender() const
304 {
305 return _shouldRender;
306 }
307
308 inline bool hasBegun() const
309 {
310 return _begun;
311 }
312
313 inline XrTime getTime() const
314 {
315 return _time;
316 }
317
318 void locateViews();
319
320 void checkLocateViews()
321 {
322 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_locateViewsMutex);
323 if (!_locatedViews)
324 locateViews();
325 }
326
327 bool isOrientationValid()
328 {
329 checkLocateViews();
330 return _viewState.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT;
331 }
332 bool isPositionValid()
333 {
334 checkLocateViews();
335 return _viewState.viewStateFlags & XR_VIEW_STATE_POSITION_VALID_BIT;
336 }
337 bool isOrientationTracked()
338 {
339 checkLocateViews();
340 return _viewState.viewStateFlags & XR_VIEW_STATE_ORIENTATION_TRACKED_BIT;
341 }
342 bool isPositionTracked()
343 {
344 checkLocateViews();
345 return _viewState.viewStateFlags & XR_VIEW_STATE_POSITION_TRACKED_BIT;
346 }
347 uint32_t getNumViews()
348 {
349 checkLocateViews();
350 return _views.size();
351 }
352 const XrFovf &getViewFov(uint32_t index)
353 {
354 checkLocateViews();
355 return _views[index].fov;
356 }
357 const XrPosef &getViewPose(uint32_t index)
358 {
359 checkLocateViews();
360 return _views[index].pose;
361 }
362
363 // Modifiers
364
365 inline void setEnvBlendMode(XrEnvironmentBlendMode envBlendMode)
366 {
367 _envBlendMode = envBlendMode;
368 }
369 inline XrEnvironmentBlendMode getEnvBlendMode() const
370 {
371 return _envBlendMode;
372 }
373
374 inline void setOsgFrameNumber(unsigned int osgFrameNumber)
375 {
376 _osgFrameNumber = osgFrameNumber;
377 }
378 inline unsigned int getOsgFrameNumber() const
379 {
380 return _osgFrameNumber;
381 }
382
383 void addLayer(osg::ref_ptr<CompositionLayer> layer);
384
385 // Operations
386
387 bool begin();
388 bool end();
389
390 protected:
391
392 // Frame info
393 osg::ref_ptr<Session> _session;
394 XrTime _time;
395 XrDuration _period;
396 bool _shouldRender;
397
398 // OpenSceneGraph frame
399 unsigned int _osgFrameNumber;
400
401 // For access to _locatedViews etc
402 OpenThreads::Mutex _locateViewsMutex;
403
404 // View locations (protected by _locateViewsMutex)
405 bool _locatedViews;
406 XrViewState _viewState;
407 std::vector<XrView> _views;
408
409 // Frame end info
410 bool _begun;
411 XrEnvironmentBlendMode _envBlendMode;
412 std::vector<osg::ref_ptr<CompositionLayer> > _layers;
413 };
414
415 osg::ref_ptr<Frame> waitFrame();
416
417 // OpenXR extension wrappers
418 XrResult xrGetVisibilityMask(const System::ViewConfiguration &viewConfiguration,
419 uint32_t viewIndex,
420 XrVisibilityMaskTypeKHR visibilityMaskType,
421 XrVisibilityMaskKHR *visibilityMask) const
422 {
423 return _instance->xrGetVisibilityMask(_session,
424 viewConfiguration.getType(),
425 viewIndex, visibilityMaskType,
426 visibilityMask);
427 }
428
429 protected:
430
431 // Init data
432 osg::observer_ptr<osgViewer::GraphicsWindow> _window;
433
434 // Session data
435 osg::ref_ptr<Instance> _instance;
436 const System *_system;
437 XrSession _session;
438 const System::ViewConfiguration *_viewConfiguration;
439
440 // Action sets
441 std::set<osg::ref_ptr<ActionSet>> _actionSets;
442 typedef std::pair<ActionSet *, XrPath> ActionSetSubactionPair;
443 std::set<ActionSetSubactionPair> _activeActionSets;
444 unsigned int _actionSyncCount;
445
446 // Session state
447 XrSessionState _state;
448 bool _running;
449 bool _exiting;
450 mutable bool _lost;
451
452 // Swapchain formats
453 mutable bool _readSwapchainFormats;
454 mutable SwapchainFormats _swapchainFormats;
455
456 // Reference spaces
457 osg::ref_ptr<Space> _localSpace;
458 XrTime _lastDisplayTime;
459
460 /*
461 * Visibility mask geometry cache.
462 * We keep visibility mask geometries cached to avoid duplication and so
463 * we can update them after a VisibilityMaskChangedKHR event.
464 */
465 typedef osg::ref_ptr<osg::Geometry> VisMaskGeometry;
466 typedef std::vector<VisMaskGeometry> VisMaskGeometryView;
467 typedef std::vector<VisMaskGeometryView> VisMaskGeometries;
468 VisMaskGeometries _visMaskCache;
469 };
470
471 } // osgXR::OpenXR
472
473 } // osgXR
474
475 #endif