33 std::vector<XrApiLayerProperties>
layers;
46GHOST_XrErrorHandlerFn GHOST_XrContext::s_error_handler =
nullptr;
47void *GHOST_XrContext::s_error_handler_customdata =
nullptr;
55 debug_(create_info->context_flag & GHOST_kXrContextDebug),
56 debug_time_(create_info->context_flag & GHOST_kXrContextDebugTime)
66 if (oxr_->debug_messenger != XR_NULL_HANDLE) {
67 assert(oxr_->s_xrDestroyDebugUtilsMessengerEXT_fn !=
nullptr);
68 oxr_->s_xrDestroyDebugUtilsMessengerEXT_fn(oxr_->debug_messenger);
70 if (oxr_->instance != XR_NULL_HANDLE) {
72 oxr_->instance = XR_NULL_HANDLE;
81 printAvailableAPILayersAndExtensionsInfo();
85 const std::vector<GHOST_TXrGraphicsBinding> graphics_binding_types =
86 determineGraphicsBindingTypesToEnable(create_info);
88 assert(oxr_->instance == XR_NULL_HANDLE);
89 createOpenXRInstance(graphics_binding_types);
90 storeInstanceProperties();
93 gpu_binding_type_ = determineGraphicsBindingTypeToUse(graphics_binding_types, create_info);
99void GHOST_XrContext::createOpenXRInstance(
100 const std::vector<GHOST_TXrGraphicsBinding> &graphics_binding_types)
102 XrInstanceCreateInfo create_info = {XR_TYPE_INSTANCE_CREATE_INFO};
108#ifdef XR_API_VERSION_1_0
109 create_info.applicationInfo.apiVersion = XR_API_VERSION_1_0;
111 create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
114 getAPILayersToEnable(enabled_layers_);
115 getExtensionsToEnable(graphics_binding_types, enabled_extensions_);
116 create_info.enabledApiLayerCount = enabled_layers_.size();
117 create_info.enabledApiLayerNames = enabled_layers_.data();
118 create_info.enabledExtensionCount = enabled_extensions_.size();
119 create_info.enabledExtensionNames = enabled_extensions_.data();
120 printExtensionsAndAPILayersToEnable();
122 CHECK_XR(xrCreateInstance(&create_info, &oxr_->instance),
123 "Failed to create OpenXR instance, check active OpenXR runtime.");
126void GHOST_XrContext::storeInstanceProperties()
128 const std::map<std::string, GHOST_TXrOpenXRRuntimeID> runtime_map = {
134 decltype(runtime_map)::const_iterator runtime_map_iter;
136 oxr_->instance_properties.type = XR_TYPE_INSTANCE_PROPERTIES;
137 CHECK_XR(xrGetInstanceProperties(oxr_->instance, &oxr_->instance_properties),
138 "Failed to get OpenXR runtime information. Do you have an active runtime set up?");
140 runtime_map_iter = runtime_map.find(oxr_->instance_properties.runtimeName);
141 if (runtime_map_iter != runtime_map.end()) {
142 runtime_id_ = runtime_map_iter->second;
152void GHOST_XrContext::printSDKVersion()
154 const XrVersion sdk_version = XR_CURRENT_API_VERSION;
157 "OpenXR SDK Version: %u.%u.%u",
158 XR_VERSION_MAJOR(sdk_version),
159 XR_VERSION_MINOR(sdk_version),
160 XR_VERSION_PATCH(sdk_version));
163void GHOST_XrContext::printInstanceInfo()
165 assert(oxr_->instance != XR_NULL_HANDLE);
168 "Connected to OpenXR runtime: %s (Version %u.%u.%u)",
169 oxr_->instance_properties.runtimeName,
170 XR_VERSION_MAJOR(oxr_->instance_properties.runtimeVersion),
171 XR_VERSION_MINOR(oxr_->instance_properties.runtimeVersion),
172 XR_VERSION_PATCH(oxr_->instance_properties.runtimeVersion));
175void GHOST_XrContext::printAvailableAPILayersAndExtensionsInfo()
178 for (
const XrApiLayerProperties &layer_info : oxr_->layers) {
181 for (
const XrExtensionProperties &ext_info : oxr_->extensions) {
186void GHOST_XrContext::printExtensionsAndAPILayersToEnable()
188 for (
const char *layer_name : enabled_layers_) {
191 for (
const char *ext_name : enabled_extensions_) {
197 XrDebugUtilsMessageTypeFlagsEXT ,
198 const XrDebugUtilsMessengerCallbackDataEXT *callbackData,
205void GHOST_XrContext::initDebugMessenger()
207 XrDebugUtilsMessengerCreateInfoEXT create_info = {XR_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
210 if (XR_FAILED(xrGetInstanceProcAddr(
212 "xrCreateDebugUtilsMessengerEXT",
213 (PFN_xrVoidFunction *)&oxr_->s_xrCreateDebugUtilsMessengerEXT_fn)) ||
214 XR_FAILED(xrGetInstanceProcAddr(
216 "xrDestroyDebugUtilsMessengerEXT",
217 (PFN_xrVoidFunction *)&oxr_->s_xrDestroyDebugUtilsMessengerEXT_fn)))
219 oxr_->s_xrCreateDebugUtilsMessengerEXT_fn =
nullptr;
220 oxr_->s_xrDestroyDebugUtilsMessengerEXT_fn =
nullptr;
223 "Could not use XR_EXT_debug_utils to enable debug prints. Not a fatal error, "
224 "continuing without the messenger.");
228 create_info.messageSeverities = XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
229 XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
230 XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
231 XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
232 create_info.messageTypes = XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
233 XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
234 XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
237 if (XR_FAILED(oxr_->s_xrCreateDebugUtilsMessengerEXT_fn(
238 oxr_->instance, &create_info, &oxr_->debug_messenger)))
241 "Failed to create OpenXR debug messenger. Not a fatal error, continuing without the "
257 error.user_message = exception->msg_.data();
258 error.customdata = s_error_handler_customdata;
262 char error_string_buf[XR_MAX_RESULT_STRING_SIZE];
263 xrResultToString(instance,
static_cast<XrResult
>(exception->result_), error_string_buf);
265 "%s (OpenXR error: %s, value: %i)",
272 LOG_GHOST_XR,
"%s (OpenXR error value: %i)", error.user_message, exception->result_);
276 s_error_handler(&error);
281 s_error_handler = handler_fn;
282 s_error_handler_customdata = customdata;
294void GHOST_XrContext::initExtensionsEx(std::vector<XrExtensionProperties> &extensions,
295 const char *layer_name)
297 uint32_t extension_count = 0;
300 CHECK_XR(xrEnumerateInstanceExtensionProperties(layer_name, 0, &extension_count,
nullptr),
301 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
303 if (extension_count == 0) {
308 for (uint32_t i = 0; i < extension_count; i++) {
309 XrExtensionProperties ext = {XR_TYPE_EXTENSION_PROPERTIES};
310 extensions.push_back(ext);
314 CHECK_XR(xrEnumerateInstanceExtensionProperties(
315 layer_name, extension_count, &extension_count, extensions.data()),
316 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
319void GHOST_XrContext::initExtensions()
321 initExtensionsEx(oxr_->extensions,
nullptr);
324void GHOST_XrContext::initApiLayers()
329 CHECK_XR(xrEnumerateApiLayerProperties(0, &layer_count,
nullptr),
330 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
332 if (layer_count == 0) {
337 oxr_->layers = std::vector<XrApiLayerProperties>(layer_count);
338 for (XrApiLayerProperties &
layer : oxr_->layers) {
339 layer.type = XR_TYPE_API_LAYER_PROPERTIES;
343 CHECK_XR(xrEnumerateApiLayerProperties(layer_count, &layer_count, oxr_->layers.data()),
344 "Failed to query OpenXR runtime information. Do you have an active runtime set up?");
345 for (
const XrApiLayerProperties &
layer : oxr_->layers) {
347 initExtensionsEx(oxr_->extensions,
layer.layerName);
352 const std::string &layer_name)
354 for (
const XrApiLayerProperties &layer_info : layers_info) {
355 if (layer_info.layerName == layer_name) {
364 const std::vector<XrExtensionProperties> &extensions_info,
365 const std::string_view &extension_name)
367 for (
const XrExtensionProperties &ext_info : extensions_info) {
368 if (ext_info.extensionName == extension_name) {
379void GHOST_XrContext::getAPILayersToEnable(std::vector<const char *> &r_ext_names)
381 static std::vector<std::string> try_layers;
386 try_layers.push_back(
"XR_APILAYER_LUNARG_core_validation");
389 r_ext_names.reserve(try_layers.size());
391 for (
const std::string &
layer : try_layers) {
393 r_ext_names.push_back(
layer.data());
403 GHOST_TXrGraphicsBinding binding)
408 case GHOST_kXrGraphicsOpenGL:
409#ifdef WITH_OPENGL_BACKEND
410 extension_names.
append(XR_KHR_OPENGL_ENABLE_EXTENSION_NAME);
414 case GHOST_kXrGraphicsVulkan:
415#ifdef WITH_VULKAN_BACKEND
416 extension_names.
append(XR_KHR_VULKAN_ENABLE_EXTENSION_NAME);
417 extension_names.
append(XR_KHR_VULKAN_ENABLE2_EXTENSION_NAME);
421 case GHOST_kXrGraphicsMetal:
422#ifdef WITH_METAL_BACKEND
423 extension_names.
append(XR_KHR_METAL_ENABLE_EXTENSION_NAME);
428# ifdef WITH_OPENGL_BACKEND
429 case GHOST_kXrGraphicsOpenGLD3D11:
431# ifdef WITH_VULKAN_BACKEND
432 case GHOST_kXrGraphicsVulkanD3D11:
434 extension_names.
append(XR_KHR_D3D11_ENABLE_EXTENSION_NAME);
437 case GHOST_kXrGraphicsUnknown:
438 assert(!
"Could not identify graphics binding to choose.");
442 return extension_names;
448void GHOST_XrContext::getExtensionsToEnable(
449 const std::vector<GHOST_TXrGraphicsBinding> &graphics_binding_types,
450 std::vector<const char *> &r_ext_names)
452 std::vector<std::string_view> try_ext;
456 try_ext.push_back(XR_EXT_DEBUG_UTILS_EXTENSION_NAME);
460 try_ext.push_back(XR_EXT_HP_MIXED_REALITY_CONTROLLER_EXTENSION_NAME);
461 try_ext.push_back(XR_HTC_VIVE_COSMOS_CONTROLLER_INTERACTION_EXTENSION_NAME);
462#ifdef XR_HTC_VIVE_FOCUS3_CONTROLLER_INTERACTION_EXTENSION_NAME
463 try_ext.push_back(XR_HTC_VIVE_FOCUS3_CONTROLLER_INTERACTION_EXTENSION_NAME);
465 try_ext.push_back(XR_HTCX_VIVE_TRACKER_INTERACTION_EXTENSION_NAME);
466 try_ext.push_back(XR_HUAWEI_CONTROLLER_INTERACTION_EXTENSION_NAME);
469 try_ext.push_back(XR_MSFT_CONTROLLER_MODEL_EXTENSION_NAME);
472 try_ext.push_back(XR_VARJO_QUAD_VIEWS_EXTENSION_NAME);
475 try_ext.push_back(XR_VARJO_FOVEATED_RENDERING_EXTENSION_NAME);
478 try_ext.push_back(XR_FB_PASSTHROUGH_EXTENSION_NAME);
481 try_ext.push_back(XR_EXT_LOCAL_FLOOR_EXTENSION_NAME);
483 r_ext_names.reserve(try_ext.size() + graphics_binding_types.size());
487 for (GHOST_TXrGraphicsBinding type : graphics_binding_types) {
490 if (extension_names.
size() == 1) {
491 r_ext_names.push_back(extension_names.
first().c_str());
494 for (blender::StringRefNull extension_name : extension_names) {
495 try_ext.push_back(extension_name);
500#if defined(WITH_GHOST_X11)
503 r_ext_names.push_back(XR_MNDX_EGL_ENABLE_EXTENSION_NAME);
507 for (
const std::string_view &ext : try_ext) {
509 r_ext_names.push_back(ext.data());
518std::vector<GHOST_TXrGraphicsBinding> GHOST_XrContext::determineGraphicsBindingTypesToEnable(
519 const GHOST_XrContextCreateInfo *create_info)
521 std::vector<GHOST_TXrGraphicsBinding>
result;
522 assert(create_info->gpu_binding_candidates !=
nullptr);
523 assert(create_info->gpu_binding_candidates_count > 0);
525 for (uint32_t i = 0; i < create_info->gpu_binding_candidates_count; i++) {
526 assert(create_info->gpu_binding_candidates[i] != GHOST_kXrGraphicsUnknown);
528 create_info->gpu_binding_candidates[i]);
530 for (blender::StringRefNull extension_name : extension_names) {
532 result.push_back(create_info->gpu_binding_candidates[i]);
545GHOST_TXrGraphicsBinding GHOST_XrContext::determineGraphicsBindingTypeToUse(
546 const std::vector<GHOST_TXrGraphicsBinding> &enabled_types,
547 const GHOST_XrContextCreateInfo *create_info)
550 for (GHOST_TXrGraphicsBinding type : enabled_types) {
555 ((create_info->context_flag & GHOST_kXrContextGpuNVIDIA) != 0))
563 assert(type != GHOST_kXrGraphicsUnknown);
580 custom_funcs_.session_create_fn = begin_info->create_fn;
581 custom_funcs_.session_exit_fn = begin_info->exit_fn;
582 custom_funcs_.session_exit_customdata = begin_info->exit_customdata;
584 if (session_ ==
nullptr) {
585 session_ = std::make_unique<GHOST_XrSession>(*
this);
593 if (session_->isRunning()) {
594 session_->requestEnd();
604 return session_ && session_->isRunning();
609 session_->draw(draw_customdata);
633 return session_.get();
638 return session_.get();
642 GHOST_XrGraphicsContextUnbindFn unbind_fn)
645 session_->unbindGraphicsContext();
647 custom_funcs_.gpu_ctx_bind_fn = bind_fn;
648 custom_funcs_.gpu_ctx_unbind_fn = unbind_fn;
653 custom_funcs_.draw_view_fn = draw_view_fn;
657 GHOST_XrPassthroughEnabledFn passthrough_enabled_fn)
659 custom_funcs_.passthrough_enabled_fn = passthrough_enabled_fn;
663 GHOST_XrDisablePassthroughFn disable_passthrough_fn)
665 custom_funcs_.disable_passthrough_fn = disable_passthrough_fn;
672 return session_->needsUpsideDownDrawing();
688 return custom_funcs_;
693 return gpu_binding_type_;
698 return oxr_->instance;
713 bool contains = std::find(enabled_extensions_.begin(), enabled_extensions_.end(), ext) !=
714 enabled_extensions_.end();
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
static XrBool32 debug_messenger_func(XrDebugUtilsMessageSeverityFlagsEXT, XrDebugUtilsMessageTypeFlagsEXT, const XrDebugUtilsMessengerCallbackDataEXT *callbackData, void *)
static blender::Vector< blender::StringRefNull > openxr_ext_names_from_wm_gpu_binding(GHOST_TXrGraphicsBinding binding)
Return a list of extension names where at least one must be available to use a graphics binding.
static bool openxr_layer_is_available(const std::vector< XrApiLayerProperties > &layers_info, const std::string &layer_name)
static bool openxr_extension_is_available(const std::vector< XrExtensionProperties > &extensions_info, const std::string_view &extension_name)
CLG_LogRef * LOG_GHOST_XR
#define CHECK_XR_ASSERT(call)
#define CHECK_XR(call, error_msg)
XrInstance getInstance() const
void setDisablePassthroughFunc(GHOST_XrDisablePassthroughFn disable_passthrough_fn) override
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
bool isExtensionEnabled(const char *ext) const
GHOST_XrContext(const GHOST_XrContextCreateInfo *create_info)
void initialize(const GHOST_XrContextCreateInfo *create_info)
static void setErrorHandler(GHOST_XrErrorHandlerFn handler_fn, void *customdata)
void setPassthroughEnabledFunc(GHOST_XrPassthroughEnabledFn passthrough_enabled_fn) override
GHOST_XrSession * getSession() override
GHOST_XrException(const char *msg, int result=0)
void append(const T &value)
#define assert(assertion)
char * STRNCPY(char(&dst)[N], const char *src)
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