| 1 |
|
| 2 |
|
| 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 |
|
| 32 |
Session(System *system, osgViewer::GraphicsWindow *window); |
| 33 |
|
| 34 |
virtual ~Session(); |
| 35 |
|
| 36 |
|
| 37 |
void releaseGLObjects(osg::State *state = nullptr); |
| 38 |
|
| 39 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
void addActionSet(ActionSet *actionSet); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
bool attachActionSets(); |
| 57 |
|
| 58 |
|
| 59 |
Path getCurrentInteractionProfile(const Path &subactionPath) const; |
| 60 |
|
| 61 |
|
| 62 |
bool getActionBoundSources(Action *action, |
| 63 |
std::vector<XrPath> &sourcePaths) const; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
std::string getInputSourceLocalizedName(XrPath sourcePath, |
| 72 |
XrInputSourceLocalizedNameFlags whichComponents) const; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void activateActionSet(ActionSet *actionSet, |
| 78 |
Path subactionPath = Path()); |
| 79 |
|
| 80 |
void deactivateActionSet(ActionSet *actionSet, |
| 81 |
Path subactionPath = Path()); |
| 82 |
|
| 83 |
bool syncActions(); |
| 84 |
|
| 85 |
|
| 86 |
unsigned int getActionSyncCount() const |
| 87 |
{ |
| 88 |
return _actionSyncCount; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
inline bool isReady() const |
| 95 |
{ |
| 96 |
return _state == XR_SESSION_STATE_READY; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
inline bool isRunning() const |
| 101 |
{ |
| 102 |
return _running; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
inline bool isExiting() const |
| 107 |
{ |
| 108 |
return _exiting; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
bool checkCurrent() const; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
void makeCurrent() const; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
void releaseContext() const; |
| 209 |
|
| 210 |
|
| 211 |
typedef enum ContextAction { |
| 212 |
|
| 213 |
CONTEXT_IGNORE = 0, |
| 214 |
|
| 215 |
CONTEXT_RESTORE, |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
CONTEXT_RELEASE |
| 222 |
} ContextAction; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
bool shouldSwitchContext() const |
| 232 |
{ |
| 233 |
return _instance->getQuirk(QUIRK_GL_CONTEXT_IGNORED) && |
| 234 |
!checkCurrent(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
bool shouldRestoreContext() const |
| 243 |
{ |
| 244 |
return _instance->getQuirk(QUIRK_GL_CONTEXT_CLEARED) && |
| 245 |
checkCurrent(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 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 |
|
| 290 |
|
| 291 |
inline bool check(XrResult result, const char *actionMsg) const |
| 292 |
{ |
| 293 |
return _session->check(result, actionMsg); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 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 |
|
| 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 |
|
| 386 |
|
| 387 |
bool begin(); |
| 388 |
bool end(); |
| 389 |
|
| 390 |
protected: |
| 391 |
|
| 392 |
|
| 393 |
osg::ref_ptr<Session> _session; |
| 394 |
XrTime _time; |
| 395 |
XrDuration _period; |
| 396 |
bool _shouldRender; |
| 397 |
|
| 398 |
|
| 399 |
unsigned int _osgFrameNumber; |
| 400 |
|
| 401 |
|
| 402 |
OpenThreads::Mutex _locateViewsMutex; |
| 403 |
|
| 404 |
|
| 405 |
bool _locatedViews; |
| 406 |
XrViewState _viewState; |
| 407 |
std::vector<XrView> _views; |
| 408 |
|
| 409 |
|
| 410 |
bool _begun; |
| 411 |
XrEnvironmentBlendMode _envBlendMode; |
| 412 |
std::vector<osg::ref_ptr<CompositionLayer> > _layers; |
| 413 |
}; |
| 414 |
|
| 415 |
osg::ref_ptr<Frame> waitFrame(); |
| 416 |
|
| 417 |
|
| 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 |
|
| 432 |
osg::observer_ptr<osgViewer::GraphicsWindow> _window; |
| 433 |
|
| 434 |
|
| 435 |
osg::ref_ptr<Instance> _instance; |
| 436 |
const System *_system; |
| 437 |
XrSession _session; |
| 438 |
const System::ViewConfiguration *_viewConfiguration; |
| 439 |
|
| 440 |
|
| 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 |
|
| 447 |
XrSessionState _state; |
| 448 |
bool _running; |
| 449 |
bool _exiting; |
| 450 |
mutable bool _lost; |
| 451 |
|
| 452 |
|
| 453 |
mutable bool _readSwapchainFormats; |
| 454 |
mutable SwapchainFormats _swapchainFormats; |
| 455 |
|
| 456 |
|
| 457 |
osg::ref_ptr<Space> _localSpace; |
| 458 |
XrTime _lastDisplayTime; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 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 |
} |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
#endif |