[Bf-blender-cvs] [a8519dbac2b] temp-ghost_openxr: Lazy-create XR-context

Julian Eisel noreply at git.blender.org
Wed Jun 19 01:03:24 CEST 2019


Commit: a8519dbac2b61d7fa240684f3129e1b458d23901
Author: Julian Eisel
Date:   Wed Jun 19 00:44:42 2019 +0200
Branches: temp-ghost_openxr
https://developer.blender.org/rBa8519dbac2b61d7fa240684f3129e1b458d23901

Lazy-create XR-context

Creating the context causes the OpenXR loader to try connect to a
runtime. That would involve reading the OS'es active_runtime.json and
dynamic linking based on that. So better avoid doing this on startup.
Also: don't pay for what you don't use!

===================================================================

M	intern/ghost/intern/GHOST_XRSession.cpp
M	source/blender/windowmanager/intern/wm.c
M	source/blender/windowmanager/intern/wm_operators.c

===================================================================

diff --git a/intern/ghost/intern/GHOST_XRSession.cpp b/intern/ghost/intern/GHOST_XRSession.cpp
index 835a02ff730..be1a82bc1d8 100644
--- a/intern/ghost/intern/GHOST_XRSession.cpp
+++ b/intern/ghost/intern/GHOST_XRSession.cpp
@@ -117,10 +117,11 @@ void GHOST_XR_session_start(wmXRContext *xr_context)
 
   GHOST_XR_graphics_context_bind(*xr_context);
   if (xr_context->gpu_ctx == nullptr) {
-    fprintf(stderr,
-            "Invalid API usage: No graphics context returned through the callback set with "
-            "GHOST_XR_graphics_context_bind_funcs(). This is required for session starting (through "
-            "GHOST_XR_session_start()).\n");
+    fprintf(
+        stderr,
+        "Invalid API usage: No graphics context returned through the callback set with "
+        "GHOST_XR_graphics_context_bind_funcs(). This is required for session starting (through "
+        "GHOST_XR_session_start()).\n");
     return;
   }
 
@@ -139,7 +140,8 @@ void GHOST_XR_session_end(wmXRContext *xr_context)
   GHOST_XR_graphics_context_unbind(*xr_context);
 }
 
-void GHOST_XR_session_state_change(OpenXRData *oxr, const XrEventDataSessionStateChanged &lifecycle)
+void GHOST_XR_session_state_change(OpenXRData *oxr,
+                                   const XrEventDataSessionStateChanged &lifecycle)
 {
   oxr->session_state = lifecycle.state;
 
diff --git a/source/blender/windowmanager/intern/wm.c b/source/blender/windowmanager/intern/wm.c
index b3a080d12ed..ddb4dfe2a4a 100644
--- a/source/blender/windowmanager/intern/wm.c
+++ b/source/blender/windowmanager/intern/wm.c
@@ -293,22 +293,6 @@ void WM_check(bContext *C)
     wm->message_bus = WM_msgbus_create();
   }
 
-#ifdef WITH_OPENXR
-  if (wm->xr_context == NULL) {
-    const eWM_xrGraphicsBinding gpu_bindings_candidates[] = {
-        WM_XR_GRAPHICS_OPENGL,
-#  ifdef WIN32
-        WM_XR_GRAPHICS_D3D11,
-#  endif
-    };
-    const wmXRContextCreateInfo create_info = {
-        .gpu_binding_candidates = gpu_bindings_candidates,
-        .gpu_binding_candidates_count = ARRAY_SIZE(gpu_bindings_candidates)};
-
-    wm->xr_context = GHOST_XR_context_create(&create_info);
-  }
-#endif
-
   /* case: fileread */
   /* note: this runs in bg mode to set the screen context cb */
   if ((wm->initialized & WM_WINDOW_IS_INITIALIZED) == 0) {
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index 4b87970e6e8..acd6809a1f0 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -3641,12 +3641,34 @@ static void xr_session_window_create(bContext *C)
 }
 #  endif /* WIN32 */
 
-static int xr_session_toggle_exec(bContext *C, wmOperator *UNUSED(op))
+static bool wm_xr_ensure_context(wmWindowManager *wm)
+{
+  if (wm->xr_context) {
+    return true;
+  }
+
+  const eWM_xrGraphicsBinding gpu_bindings_candidates[] = {
+      WM_XR_GRAPHICS_OPENGL,
+#  ifdef WIN32
+      WM_XR_GRAPHICS_D3D11,
+#  endif
+  };
+  const wmXRContextCreateInfo create_info = {
+      .gpu_binding_candidates = gpu_bindings_candidates,
+      .gpu_binding_candidates_count = ARRAY_SIZE(gpu_bindings_candidates)};
+
+  wm->xr_context = GHOST_XR_context_create(&create_info);
+
+  return wm->xr_context != NULL;
+}
+
+static int wm_xr_session_toggle_exec(bContext *C, wmOperator *UNUSED(op))
 {
   wmWindowManager *wm = CTX_wm_manager(C);
   struct wmXRContext *xr_context = wm->xr_context;
 
-  if (xr_context == NULL) {
+  /* Lazy-create xr context - tries to dynlink to the runtime, reading active_runtime.json. */
+  if (wm_xr_ensure_context(wm) == false) {
     return OPERATOR_CANCELLED;
   }
 
@@ -3675,7 +3697,7 @@ static void WM_OT_xr_session_toggle(wmOperatorType *ot)
       "opened";
 
   /* callbacks */
-  ot->exec = xr_session_toggle_exec;
+  ot->exec = wm_xr_session_toggle_exec;
 }
 #endif /* WITH_OPENXR */



More information about the Bf-blender-cvs mailing list