| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef OSGXR_OPENXR_INSTANCE |
| 5 |
#define OSGXR_OPENXR_INSTANCE 1 |
| 6 |
|
| 7 |
#include "Quirks.h" |
| 8 |
|
| 9 |
#include <map> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <osg/Referenced> |
| 13 |
#include <osg/observer_ptr> |
| 14 |
#include <osg/ref_ptr> |
| 15 |
|
| 16 |
#include <openxr/openxr.h> |
| 17 |
#define XR_USE_GRAPHICS_API_OPENGL |
| 18 |
#include <openxr/openxr_platform.h> |
| 19 |
|
| 20 |
#define XR_APILAYER_LUNARG_core_validation "XR_APILAYER_LUNARG_core_validation" |
| 21 |
|
| 22 |
namespace osgXR { |
| 23 |
|
| 24 |
namespace OpenXR { |
| 25 |
|
| 26 |
class DebugUtilsCallback; |
| 27 |
class DebugUtilsMessenger; |
| 28 |
class EventHandler; |
| 29 |
class System; |
| 30 |
class Session; |
| 31 |
|
| 32 |
class Instance : public osg::Referenced |
| 33 |
{ |
| 34 |
public: |
| 35 |
|
| 36 |
static Instance *instance(); |
| 37 |
|
| 38 |
Instance(); |
| 39 |
virtual ~Instance(); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static void invalidateLayers(); |
| 44 |
static void invalidateExtensions(); |
| 45 |
static bool hasLayer(const char *name); |
| 46 |
static bool hasExtension(const char *name); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
void setValidationLayer(bool layerValidation) |
| 51 |
{ |
| 52 |
_layerValidation = layerValidation; |
| 53 |
} |
| 54 |
|
| 55 |
void setDebugUtils(bool debugUtils) |
| 56 |
{ |
| 57 |
_debugUtils = debugUtils; |
| 58 |
} |
| 59 |
|
| 60 |
void setDefaultDebugCallback(OpenXR::DebugUtilsCallback *callback); |
| 61 |
|
| 62 |
void setDepthInfo(bool depthInfo) |
| 63 |
{ |
| 64 |
_depthInfo = depthInfo; |
| 65 |
} |
| 66 |
|
| 67 |
void setVisibilityMask(bool visibilityMask) |
| 68 |
{ |
| 69 |
_visibilityMask = visibilityMask; |
| 70 |
} |
| 71 |
|
| 72 |
typedef enum { |
| 73 |
|
| 74 |
INIT_SUCCESS, |
| 75 |
|
| 76 |
INIT_LATER, |
| 77 |
|
| 78 |
INIT_FAIL, |
| 79 |
} InitResult; |
| 80 |
InitResult init(const char *appName, uint32_t appVersion); |
| 81 |
|
| 82 |
void deinit(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
inline bool valid() const |
| 87 |
{ |
| 88 |
return _instance != XR_NULL_HANDLE; |
| 89 |
} |
| 90 |
|
| 91 |
inline bool lost() const |
| 92 |
{ |
| 93 |
return _lost; |
| 94 |
} |
| 95 |
|
| 96 |
bool check(XrResult result, const char *actionMsg) const; |
| 97 |
|
| 98 |
class Result |
| 99 |
{ |
| 100 |
public: |
| 101 |
XrResult result = XR_SUCCESS; |
| 102 |
const char *action = nullptr; |
| 103 |
char resultName[XR_MAX_RESULT_STRING_SIZE] = {}; |
| 104 |
|
| 105 |
bool failed() const |
| 106 |
{ |
| 107 |
return XR_FAILED(result); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
bool getError(Result &error) const |
| 113 |
{ |
| 114 |
error = _lastError; |
| 115 |
return error.failed(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
inline XrInstance getXrInstance() const |
| 121 |
{ |
| 122 |
return _instance; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
inline const char *getRuntimeName() const |
| 127 |
{ |
| 128 |
return _properties.runtimeName; |
| 129 |
} |
| 130 |
inline XrVersion getRuntimeVersion() const |
| 131 |
{ |
| 132 |
return _properties.runtimeVersion; |
| 133 |
} |
| 134 |
|
| 135 |
inline bool getQuirk(Quirk quirk) const |
| 136 |
{ |
| 137 |
return _quirks[quirk]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
bool supportsDebugUtils() const |
| 143 |
{ |
| 144 |
return _supportsDebugUtils; |
| 145 |
} |
| 146 |
|
| 147 |
bool supportsCompositionLayerDepth() const |
| 148 |
{ |
| 149 |
return _supportsCompositionLayerDepth; |
| 150 |
} |
| 151 |
|
| 152 |
bool supportsVisibilityMask() const |
| 153 |
{ |
| 154 |
return _supportsVisibilityMask; |
| 155 |
} |
| 156 |
|
| 157 |
PFN_xrVoidFunction getProcAddr(const char *name) const; |
| 158 |
|
| 159 |
XrResult getOpenGLGraphicsRequirements(XrSystemId systemId, |
| 160 |
XrGraphicsRequirementsOpenGLKHR* graphicsRequirements) const |
| 161 |
{ |
| 162 |
if (!_xrGetOpenGLGraphicsRequirementsKHR) |
| 163 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 164 |
return _xrGetOpenGLGraphicsRequirementsKHR(_instance, systemId, |
| 165 |
graphicsRequirements); |
| 166 |
} |
| 167 |
|
| 168 |
XrResult xrSetDebugUtilsObjectName(const XrDebugUtilsObjectNameInfoEXT *nameInfo) const |
| 169 |
{ |
| 170 |
if (!_xrSetDebugUtilsObjectNameEXT) |
| 171 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 172 |
return _xrSetDebugUtilsObjectNameEXT(_instance, nameInfo); |
| 173 |
} |
| 174 |
|
| 175 |
XrResult xrCreateDebugUtilsMessenger(const XrDebugUtilsMessengerCreateInfoEXT *createInfo, |
| 176 |
XrDebugUtilsMessengerEXT *messenger) const |
| 177 |
{ |
| 178 |
if (!_xrCreateDebugUtilsMessengerEXT) |
| 179 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 180 |
return _xrCreateDebugUtilsMessengerEXT(_instance, createInfo, |
| 181 |
messenger); |
| 182 |
} |
| 183 |
|
| 184 |
XrResult xrDestroyDebugUtilsMessenger(XrDebugUtilsMessengerEXT messenger) const |
| 185 |
{ |
| 186 |
if (!_xrDestroyDebugUtilsMessengerEXT) |
| 187 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 188 |
return _xrDestroyDebugUtilsMessengerEXT(messenger); |
| 189 |
} |
| 190 |
|
| 191 |
XrResult xrSubmitDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT messageSeverity, |
| 192 |
XrDebugUtilsMessageTypeFlagsEXT messageTypes, |
| 193 |
const XrDebugUtilsMessengerCallbackDataEXT* callbackData) const |
| 194 |
{ |
| 195 |
if (!_xrSubmitDebugUtilsMessageEXT) |
| 196 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 197 |
return _xrSubmitDebugUtilsMessageEXT(_instance, messageSeverity, |
| 198 |
messageTypes, callbackData); |
| 199 |
} |
| 200 |
|
| 201 |
XrResult xrSessionBeginDebugUtilsLabelRegion(XrSession session, |
| 202 |
const XrDebugUtilsLabelEXT *labelInfo) const |
| 203 |
{ |
| 204 |
if (!_xrSessionBeginDebugUtilsLabelRegionEXT) |
| 205 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 206 |
return _xrSessionBeginDebugUtilsLabelRegionEXT(session, labelInfo); |
| 207 |
} |
| 208 |
|
| 209 |
XrResult xrSessionEndDebugUtilsLabelRegion(XrSession session) const |
| 210 |
{ |
| 211 |
if (!_xrSessionEndDebugUtilsLabelRegionEXT) |
| 212 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 213 |
return _xrSessionEndDebugUtilsLabelRegionEXT(session); |
| 214 |
} |
| 215 |
|
| 216 |
XrResult xrSessionInsertDebugUtilsLabel(XrSession session, |
| 217 |
const XrDebugUtilsLabelEXT *labelInfo) const |
| 218 |
{ |
| 219 |
if (!_xrSessionInsertDebugUtilsLabelEXT) |
| 220 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 221 |
return _xrSessionInsertDebugUtilsLabelEXT(session, labelInfo); |
| 222 |
} |
| 223 |
|
| 224 |
XrResult xrGetVisibilityMask(XrSession session, |
| 225 |
XrViewConfigurationType viewConfigurationType, |
| 226 |
uint32_t viewIndex, |
| 227 |
XrVisibilityMaskTypeKHR visibilityMaskType, |
| 228 |
XrVisibilityMaskKHR *visibilityMask) const |
| 229 |
{ |
| 230 |
if (!_xrGetVisibilityMaskKHR) |
| 231 |
return XR_ERROR_FUNCTION_UNSUPPORTED; |
| 232 |
return _xrGetVisibilityMaskKHR(session, viewConfigurationType, |
| 233 |
viewIndex, visibilityMaskType, |
| 234 |
visibilityMask); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
System *getSystem(XrFormFactor formFactor, bool *supported = nullptr); |
| 240 |
|
| 241 |
|
| 242 |
void invalidateSystem(XrFormFactor formFactor); |
| 243 |
void registerSession(Session *session); |
| 244 |
void unregisterSession(Session *session); |
| 245 |
Session *getSession(XrSession xrSession); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void pollEvents(EventHandler *handler); |
| 250 |
|
| 251 |
protected: |
| 252 |
|
| 253 |
|
| 254 |
bool _layerValidation; |
| 255 |
bool _debugUtils; |
| 256 |
bool _depthInfo; |
| 257 |
bool _visibilityMask; |
| 258 |
|
| 259 |
|
| 260 |
osg::ref_ptr<DebugUtilsCallback> _defaultDebugCallback; |
| 261 |
osg::ref_ptr<DebugUtilsMessenger> _defaultDebugMessenger; |
| 262 |
|
| 263 |
|
| 264 |
XrInstance _instance; |
| 265 |
mutable bool _lost; |
| 266 |
mutable Result _lastError; |
| 267 |
|
| 268 |
|
| 269 |
bool _supportsDebugUtils; |
| 270 |
bool _supportsCompositionLayerDepth; |
| 271 |
bool _supportsVisibilityMask; |
| 272 |
|
| 273 |
PFN_xrGetOpenGLGraphicsRequirementsKHR _xrGetOpenGLGraphicsRequirementsKHR = nullptr; |
| 274 |
PFN_xrSetDebugUtilsObjectNameEXT _xrSetDebugUtilsObjectNameEXT = nullptr; |
| 275 |
PFN_xrCreateDebugUtilsMessengerEXT _xrCreateDebugUtilsMessengerEXT = nullptr; |
| 276 |
PFN_xrDestroyDebugUtilsMessengerEXT _xrDestroyDebugUtilsMessengerEXT = nullptr; |
| 277 |
PFN_xrSubmitDebugUtilsMessageEXT _xrSubmitDebugUtilsMessageEXT = nullptr; |
| 278 |
PFN_xrSessionBeginDebugUtilsLabelRegionEXT _xrSessionBeginDebugUtilsLabelRegionEXT = nullptr; |
| 279 |
PFN_xrSessionEndDebugUtilsLabelRegionEXT _xrSessionEndDebugUtilsLabelRegionEXT = nullptr; |
| 280 |
PFN_xrSessionInsertDebugUtilsLabelEXT _xrSessionInsertDebugUtilsLabelEXT = nullptr; |
| 281 |
PFN_xrGetVisibilityMaskKHR _xrGetVisibilityMaskKHR = nullptr; |
| 282 |
|
| 283 |
|
| 284 |
XrInstanceProperties _properties; |
| 285 |
|
| 286 |
|
| 287 |
Quirks _quirks; |
| 288 |
|
| 289 |
|
| 290 |
mutable std::vector<System *> _systems; |
| 291 |
|
| 292 |
|
| 293 |
std::map<XrSession, Session *> _sessions; |
| 294 |
}; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
#endif |