26 #include <string_view>
40 std::vector<XrApiLayerProperties>
layers;
53 GHOST_XrErrorHandlerFn GHOST_XrContext::s_error_handler =
nullptr;
54 void *GHOST_XrContext::s_error_handler_customdata =
nullptr;
63 m_debug(create_info->context_flag & GHOST_kXrContextDebug),
64 m_debug_time(create_info->context_flag & GHOST_kXrContextDebugTime)
74 if (m_oxr->debug_messenger != XR_NULL_HANDLE) {
75 assert(m_oxr->s_xrDestroyDebugUtilsMessengerEXT_fn !=
nullptr);
76 m_oxr->s_xrDestroyDebugUtilsMessengerEXT_fn(m_oxr->debug_messenger);
78 if (m_oxr->instance != XR_NULL_HANDLE) {
80 m_oxr->instance = XR_NULL_HANDLE;
89 printAvailableAPILayersAndExtensionsInfo();
94 const std::vector<GHOST_TXrGraphicsBinding> graphics_binding_types =
95 determineGraphicsBindingTypesToEnable(create_info);
97 assert(m_oxr->instance == XR_NULL_HANDLE);
98 createOpenXRInstance(graphics_binding_types);
99 storeInstanceProperties();
102 m_gpu_binding_type = determineGraphicsBindingTypeToUse(graphics_binding_types);
106 initDebugMessenger();
110 void GHOST_XrContext::createOpenXRInstance(
111 const std::vector<GHOST_TXrGraphicsBinding> &graphics_binding_types)
113 XrInstanceCreateInfo create_info = {XR_TYPE_INSTANCE_CREATE_INFO};
115 std::string(
"Blender").copy(create_info.applicationInfo.applicationName,
116 XR_MAX_APPLICATION_NAME_SIZE);
117 create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
119 getAPILayersToEnable(m_enabled_layers);
120 getExtensionsToEnable(graphics_binding_types, m_enabled_extensions);
121 create_info.enabledApiLayerCount = m_enabled_layers.size();
122 create_info.enabledApiLayerNames = m_enabled_layers.data();
123 create_info.enabledExtensionCount = m_enabled_extensions.size();
124 create_info.enabledExtensionNames = m_enabled_extensions.data();
126 printExtensionsAndAPILayersToEnable();
129 CHECK_XR(xrCreateInstance(&create_info, &m_oxr->instance),
130 "Failed to connect to an OpenXR runtime.");
133 void GHOST_XrContext::storeInstanceProperties()
135 const std::map<std::string, GHOST_TXrOpenXRRuntimeID> runtime_map = {
140 decltype(runtime_map)::const_iterator runtime_map_iter;
142 m_oxr->instance_properties.type = XR_TYPE_INSTANCE_PROPERTIES;
143 CHECK_XR(xrGetInstanceProperties(m_oxr->instance, &m_oxr->instance_properties),
144 "Failed to get OpenXR runtime information. Do you have an active runtime set up?");
146 runtime_map_iter = runtime_map.find(m_oxr->instance_properties.runtimeName);
147 if (runtime_map_iter != runtime_map.end()) {
148 m_runtime_id = runtime_map_iter->second;
159 void GHOST_XrContext::printInstanceInfo()
161 assert(m_oxr->instance != XR_NULL_HANDLE);
163 printf(
"Connected to OpenXR runtime: %s (Version %u.%u.%u)\n",
164 m_oxr->instance_properties.runtimeName,
165 XR_VERSION_MAJOR(m_oxr->instance_properties.runtimeVersion),
166 XR_VERSION_MINOR(m_oxr->instance_properties.runtimeVersion),
167 XR_VERSION_PATCH(m_oxr->instance_properties.runtimeVersion));
170 void GHOST_XrContext::printAvailableAPILayersAndExtensionsInfo()
172 puts(
"Available OpenXR API-layers/extensions:");
173 for (XrApiLayerProperties &layer_info : m_oxr->layers) {
174 printf(
"Layer: %s\n", layer_info.layerName);
176 for (XrExtensionProperties &ext_info : m_oxr->extensions) {
177 printf(
"Extension: %s\n", ext_info.extensionName);
181 void GHOST_XrContext::printExtensionsAndAPILayersToEnable()
183 for (
const char *layer_name : m_enabled_layers) {
184 printf(
"Enabling OpenXR API-Layer: %s\n", layer_name);
186 for (
const char *ext_name : m_enabled_extensions) {
187 printf(
"Enabling OpenXR Extension: %s\n", ext_name);
192 XrDebugUtilsMessageTypeFlagsEXT ,
193 const XrDebugUtilsMessengerCallbackDataEXT *callbackData,
196 puts(
"OpenXR Debug Message:");
197 puts(callbackData->message);
201 void GHOST_XrContext::initDebugMessenger()
203 XrDebugUtilsMessengerCreateInfoEXT create_info = {XR_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
206 if (XR_FAILED(xrGetInstanceProcAddr(
208 "xrCreateDebugUtilsMessengerEXT",
209 (PFN_xrVoidFunction *)&m_oxr->s_xrCreateDebugUtilsMessengerEXT_fn)) ||
210 XR_FAILED(xrGetInstanceProcAddr(
212 "xrDestroyDebugUtilsMessengerEXT",
213 (PFN_xrVoidFunction *)&m_oxr->s_xrDestroyDebugUtilsMessengerEXT_fn))) {
214 m_oxr->s_xrCreateDebugUtilsMessengerEXT_fn =
nullptr;
215 m_oxr->s_xrDestroyDebugUtilsMessengerEXT_fn =
nullptr;
218 "Could not use XR_EXT_debug_utils to enable debug prints. Not a fatal error, "
219 "continuing without the messenger.\n");
223 create_info.messageSeverities = XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
224 XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
225 XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
226 XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
227 create_info.messageTypes = XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
228 XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
229 XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
232 if (XR_FAILED(m_oxr->s_xrCreateDebugUtilsMessengerEXT_fn(
233 m_oxr->instance, &create_info, &m_oxr->debug_messenger))) {
235 "Failed to create OpenXR debug messenger. Not a fatal error, continuing without the "
252 error.user_message = exception->m_msg;
253 error.customdata = s_error_handler_customdata;
257 "Error: \t%s\n\tOpenXR error value: %i\n",
259 exception->m_result);
263 s_error_handler(&
error);
268 s_error_handler = handler_fn;
269 s_error_handler_customdata = customdata;
282 void GHOST_XrContext::initExtensionsEx(std::vector<XrExtensionProperties> &extensions,
283 const char *layer_name)
288 CHECK_XR(xrEnumerateInstanceExtensionProperties(layer_name, 0, &extension_count,
nullptr),
289 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
291 if (extension_count == 0) {
296 for (
uint32_t i = 0; i < extension_count; i++) {
297 XrExtensionProperties ext = {XR_TYPE_EXTENSION_PROPERTIES};
298 extensions.push_back(ext);
302 CHECK_XR(xrEnumerateInstanceExtensionProperties(
303 layer_name, extension_count, &extension_count, extensions.data()),
304 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
307 void GHOST_XrContext::initExtensions()
309 initExtensionsEx(m_oxr->extensions,
nullptr);
312 void GHOST_XrContext::initApiLayers()
317 CHECK_XR(xrEnumerateApiLayerProperties(0, &layer_count,
nullptr),
318 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
320 if (layer_count == 0) {
325 m_oxr->layers = std::vector<XrApiLayerProperties>(layer_count);
326 for (XrApiLayerProperties &layer : m_oxr->layers) {
327 layer.type = XR_TYPE_API_LAYER_PROPERTIES;
331 CHECK_XR(xrEnumerateApiLayerProperties(layer_count, &layer_count, m_oxr->layers.data()),
332 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
333 for (XrApiLayerProperties &layer : m_oxr->layers) {
335 initExtensionsEx(m_oxr->extensions, layer.layerName);
340 const std::string &layer_name)
342 for (
const XrApiLayerProperties &layer_info : layers_info) {
343 if (layer_info.layerName == layer_name) {
352 const std::vector<XrExtensionProperties> &extensions_info,
353 const std::string_view &extension_name)
355 for (
const XrExtensionProperties &ext_info : extensions_info) {
356 if (ext_info.extensionName == extension_name) {
367 void GHOST_XrContext::getAPILayersToEnable(std::vector<const char *> &r_ext_names)
369 static std::vector<std::string> try_layers;
374 try_layers.push_back(
"XR_APILAYER_LUNARG_core_validation");
377 r_ext_names.reserve(try_layers.size());
379 for (
const std::string &layer : try_layers) {
381 r_ext_names.push_back(layer.c_str());
389 case GHOST_kXrGraphicsOpenGL:
390 return XR_KHR_OPENGL_ENABLE_EXTENSION_NAME;
392 case GHOST_kXrGraphicsD3D11:
393 return XR_KHR_D3D11_ENABLE_EXTENSION_NAME;
395 case GHOST_kXrGraphicsUnknown:
396 assert(!
"Could not identify graphics binding to choose.");
406 void GHOST_XrContext::getExtensionsToEnable(
407 const std::vector<GHOST_TXrGraphicsBinding> &graphics_binding_types,
408 std::vector<const char *> &r_ext_names)
410 std::vector<std::string_view> try_ext;
414 try_ext.push_back(XR_EXT_DEBUG_UTILS_EXTENSION_NAME);
417 r_ext_names.reserve(try_ext.size() + graphics_binding_types.size());
421 for (GHOST_TXrGraphicsBinding
type : graphics_binding_types) {
424 r_ext_names.push_back(gpu_binding);
427 for (
const std::string_view &ext : try_ext) {
429 r_ext_names.push_back(ext.data());
438 std::vector<GHOST_TXrGraphicsBinding> GHOST_XrContext::determineGraphicsBindingTypesToEnable(
439 const GHOST_XrContextCreateInfo *create_info)
441 std::vector<GHOST_TXrGraphicsBinding>
result;
442 assert(create_info->gpu_binding_candidates !=
NULL);
443 assert(create_info->gpu_binding_candidates_count > 0);
445 for (
uint32_t i = 0; i < create_info->gpu_binding_candidates_count; i++) {
446 assert(create_info->gpu_binding_candidates[i] != GHOST_kXrGraphicsUnknown);
448 create_info->gpu_binding_candidates[i]);
450 result.push_back(create_info->gpu_binding_candidates[i]);
461 GHOST_TXrGraphicsBinding GHOST_XrContext::determineGraphicsBindingTypeToUse(
462 const std::vector<GHOST_TXrGraphicsBinding> &enabled_types)
465 for (GHOST_TXrGraphicsBinding
type : enabled_types) {
474 assert(
type != GHOST_kXrGraphicsUnknown);
494 if (m_session ==
nullptr) {
495 m_session = std::make_unique<GHOST_XrSession>(*
this);
497 m_session->start(begin_info);
503 if (m_session->isRunning()) {
504 m_session->requestEnd();
514 return m_session && m_session->isRunning();
519 m_session->draw(draw_customdata);
542 GHOST_XrGraphicsContextUnbindFn unbind_fn)
545 m_session->unbindGraphicsContext();
560 return m_session->needsUpsideDownDrawing();
577 return m_custom_funcs;
582 return m_gpu_binding_type;
587 return m_oxr->instance;
static XrBool32 debug_messenger_func(XrDebugUtilsMessageSeverityFlagsEXT, XrDebugUtilsMessageTypeFlagsEXT, const XrDebugUtilsMessengerCallbackDataEXT *callbackData, void *)
static bool openxr_layer_is_available(const std::vector< XrApiLayerProperties > &layers_info, const std::string &layer_name)
static const char * openxr_ext_name_from_wm_gpu_binding(GHOST_TXrGraphicsBinding binding)
static bool openxr_extension_is_available(const std::vector< XrExtensionProperties > &extensions_info, const std::string_view &extension_name)
#define CHECK_XR_ASSERT(call)
#define CHECK_XR(call, error_msg)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
XrInstance getInstance() const
GHOST_TXrOpenXRRuntimeID getOpenXRRuntimeID() const
void setGraphicsContextBindFuncs(GHOST_XrGraphicsContextBindFn bind_fn, GHOST_XrGraphicsContextUnbindFn unbind_fn) override
GHOST_TXrGraphicsBinding getGraphicsBindingType() const
void drawSessionViews(void *draw_customdata) override
void handleSessionStateChange(const XrEventDataSessionStateChanged &lifecycle)
const GHOST_XrCustomFuncs & getCustomFuncs() const
void dispatchErrorMessage(const class GHOST_XrException *exception) const override
bool isDebugTimeMode() const
void setDrawViewFunc(GHOST_XrDrawViewFn draw_view_fn) override
bool needsUpsideDownDrawing() const override
bool isSessionRunning() const override
void startSession(const GHOST_XrSessionBeginInfo *begin_info) override
void endSession() override
GHOST_XrContext(const GHOST_XrContextCreateInfo *create_info)
void initialize(const GHOST_XrContextCreateInfo *create_info)
static void setErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
static void error(const char *str)
GHOST_XrDrawViewFn draw_view_fn
GHOST_XrSessionExitFn session_exit_fn
void * session_exit_customdata
GHOST_XrGraphicsContextUnbindFn gpu_ctx_unbind_fn
GHOST_XrGraphicsContextBindFn gpu_ctx_bind_fn
XrDebugUtilsMessengerEXT debug_messenger
XrInstanceProperties instance_properties
std::vector< XrExtensionProperties > extensions
static PFN_xrDestroyDebugUtilsMessengerEXT s_xrDestroyDebugUtilsMessengerEXT_fn
std::vector< XrApiLayerProperties > layers
static PFN_xrCreateDebugUtilsMessengerEXT s_xrCreateDebugUtilsMessengerEXT_fn