[Bf-blender-cvs] [35c9e3beed6] soc-2019-openxr: Initial VR-session starting/ending

Julian Eisel noreply at git.blender.org
Tue Jun 4 17:03:34 CEST 2019


Commit: 35c9e3beed687a97b633c7ad2c4c41094ee3f9dc
Author: Julian Eisel
Date:   Tue Jun 4 16:42:42 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rB35c9e3beed687a97b633c7ad2c4c41094ee3f9dc

Initial VR-session starting/ending

Adds operator to toggle a VR session, exposed in the Window top-bar
menu. It triggers the needed calls for session creation and destruction.
Setting up the XR-system (a configuration of related devices) is also
done now.

Calling WMR runtime functions fails currently. Not sure why. So while
this executes required routines, it doesn't really work.

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

M	release/scripts/startup/bl_ui/space_topbar.py
M	source/blender/windowmanager/intern/wm_operators.c
M	source/blender/windowmanager/intern/wm_xr.c
M	source/blender/windowmanager/wm.h

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

diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index d99df2eced7..c37f96dd9a6 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -561,11 +561,14 @@ class TOPBAR_MT_window(Menu):
         if sys.platform[:3] == "win":
             layout.separator()
             layout.operator("wm.console_toggle", icon='CONSOLE')
+            layout.separator()
 
         if context.scene.render.use_multiview:
             layout.separator()
             layout.operator("wm.set_stereo_3d")
 
+        # TODO WITH_OPENXR
+        layout.operator("wm.xr_session_toggle")
 
 class TOPBAR_MT_help(Menu):
     bl_label = "Help"
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index 4a99c2de6e7..a3f134a32dc 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -3498,6 +3498,34 @@ static void WM_OT_stereo3d_set(wmOperatorType *ot)
   RNA_def_property_flag(prop, PROP_SKIP_SAVE);
 }
 
+#ifdef WITH_OPENXR
+static int xr_session_toggle_exec(bContext *C, wmOperator *UNUSED(op))
+{
+  struct wmXRContext *xr_context = CTX_wm_xr_context(C);
+
+  if (wm_xr_session_is_running(xr_context)) {
+    wm_xr_session_end(xr_context);
+  }
+  else {
+    wm_xr_session_start(xr_context);
+  }
+  return OPERATOR_FINISHED;
+}
+
+static void WM_OT_xr_session_toggle(wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Toggle VR Session";
+  ot->idname = "WM_OT_xr_session_toggle";
+  ot->description =
+      "Attempt to open a view for use with virtual reality headsets, or close it if already "
+      "opened";
+
+  /* callbacks */
+  ot->exec = xr_session_toggle_exec;
+}
+#endif /* WITH_OPENXR */
+
 void wm_operatortypes_register(void)
 {
   WM_operatortype_append(WM_OT_window_close);
@@ -3535,6 +3563,9 @@ void wm_operatortypes_register(void)
   WM_operatortype_append(WM_OT_call_panel);
   WM_operatortype_append(WM_OT_radial_control);
   WM_operatortype_append(WM_OT_stereo3d_set);
+#ifdef WITH_OPENXR
+  WM_operatortype_append(WM_OT_xr_session_toggle);
+#endif
 #if defined(WIN32)
   WM_operatortype_append(WM_OT_console_toggle);
 #endif
diff --git a/source/blender/windowmanager/intern/wm_xr.c b/source/blender/windowmanager/intern/wm_xr.c
index 447c3295036..103b903fd57 100644
--- a/source/blender/windowmanager/intern/wm_xr.c
+++ b/source/blender/windowmanager/intern/wm_xr.c
@@ -57,6 +57,9 @@ typedef struct wmXRContext {
 
     XrApiLayerProperties *layers;
     uint32_t layer_count;
+
+    XrSystemId system_id;
+    XrSession session;
   } oxr;
 } wmXRContext;
 
@@ -151,8 +154,8 @@ static bool openxr_instance_setup(wmXRContext *context)
   // create_info.enabledExtensionCount = 1;
   create_info.enabledExtensionCount = 0;
   static const char *enabled_extensions[] = {// "XR_KHR_D3D11_enable",
-                                              // "XR_KHR_opengl_enable"
-                                              ""};
+                                             // "XR_KHR_opengl_enable"
+                                             ""};
   create_info.enabledExtensionNames = enabled_extensions;
 
   xrCreateInstance(&create_info, &context->oxr.instance);
@@ -172,8 +175,49 @@ wmXRContext *wm_xr_context_create(void)
 
 void wm_xr_context_destroy(wmXRContext *wm_context)
 {
+  xrDestroySession(wm_context->oxr.session);
   xrDestroyInstance(wm_context->oxr.instance);
+
   MEM_SAFE_FREE(wm_context->oxr.extensions);
   MEM_SAFE_FREE(wm_context->oxr.layers);
+
   MEM_SAFE_FREE(wm_context);
 }
+
+bool wm_xr_session_is_running(const wmXRContext *xr_context)
+{
+  return xr_context->oxr.session != XR_NULL_HANDLE;
+}
+
+/**
+ * A system in OpenXR the combination of some sort of HMD plus controllers and whatever other
+ * devices are managed through OpenXR. So this attempts to init the HMD and the other devices.
+ */
+static void wm_xr_system_init(wmXRContext *xr_context)
+{
+  BLI_assert(xr_context->oxr.instance != XR_NULL_HANDLE);
+  BLI_assert(xr_context->oxr.system_id == XR_NULL_SYSTEM_ID);
+
+  XrSystemGetInfo system_info = {.type = XR_TYPE_SYSTEM_GET_INFO};
+  system_info.formFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
+
+  xrGetSystem(xr_context->oxr.instance, &system_info, &xr_context->oxr.system_id);
+}
+
+void wm_xr_session_start(wmXRContext *xr_context)
+{
+  BLI_assert(xr_context->oxr.instance != XR_NULL_HANDLE);
+  BLI_assert(xr_context->oxr.session == XR_NULL_HANDLE);
+
+  wm_xr_system_init(xr_context);
+
+  XrSessionCreateInfo create_info = {.type = XR_TYPE_SESSION_CREATE_INFO};
+  create_info.systemId = xr_context->oxr.system_id;
+  xrCreateSession(xr_context->oxr.instance, &create_info, &xr_context->oxr.session);
+}
+
+void wm_xr_session_end(wmXRContext *xr_context)
+{
+  xrEndSession(xr_context->oxr.session);
+  xrDestroySession(xr_context->oxr.session);
+}
\ No newline at end of file
diff --git a/source/blender/windowmanager/wm.h b/source/blender/windowmanager/wm.h
index 8ef6a7f5a48..e29da9ef1ef 100644
--- a/source/blender/windowmanager/wm.h
+++ b/source/blender/windowmanager/wm.h
@@ -95,10 +95,16 @@ void wm_stereo3d_set_cancel(bContext *C, wmOperator *op);
 void wm_open_init_load_ui(wmOperator *op, bool use_prefs);
 void wm_open_init_use_scripts(wmOperator *op, bool use_prefs);
 
-#ifdef WITH_OPENXR
 /* wm_xr.c */
+#ifdef WITH_OPENXR
+/* xr-context */
 struct wmXRContext *wm_xr_context_create(void) ATTR_WARN_UNUSED_RESULT;
 void wm_xr_context_destroy(struct wmXRContext *xr_context);
+
+/* sessions */
+bool wm_xr_session_is_running(const struct wmXRContext *xr_context) ATTR_WARN_UNUSED_RESULT;
+void wm_xr_session_start(struct wmXRContext *xr_context) ATTR_NONNULL();
+void wm_xr_session_end(struct wmXRContext *xr_context) ATTR_NONNULL();
 #endif
 
 #endif /* __WM_H__ */



More information about the Bf-blender-cvs mailing list