[Bf-blender-cvs] [41e2f2e75f3] soc-2019-openxr: Proper creation & destruction of OpenXR instances

Julian Eisel noreply at git.blender.org
Thu May 30 14:50:06 CEST 2019


Commit: 41e2f2e75f301fbbfcde8a0c97414f9f019ea63e
Author: Julian Eisel
Date:   Thu May 30 14:25:00 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rB41e2f2e75f301fbbfcde8a0c97414f9f019ea63e

Proper creation & destruction of OpenXR instances

Adds wm_xr.c for an XR management API and creates/destroys the OpenXR
instance through this. This is as planned in my proposal, to lock the
OpenXR calls behind an abstraction.
XR data will be stored in a (non-public) wmXRContext struct within the
window-manager.

For now, creates the OpenXR instance on startup. I think it's better to
lazy setup this, as in, only creating the instance once the user starts
the first XR session. Just to avoid costs for something that may not be
used (the OpenXR loader we use will try loading and parsing the system's
OpenXR active_runtime.json on instance creation). That's for later when
I introduce session management though.

Also added a context getter for the xr-context, which is unused but may
be handy later.

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

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/context.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_windowmanager_types.h
M	source/blender/windowmanager/CMakeLists.txt
M	source/blender/windowmanager/intern/wm.c
M	source/blender/windowmanager/intern/wm_init_exit.c
A	source/blender/windowmanager/intern/wm_xr.c
M	source/blender/windowmanager/wm.h

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

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index 0d2998cc51e..a9fd56c375a 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -157,6 +157,9 @@ struct ARegion *CTX_wm_menu(const bContext *C);
 struct wmGizmoGroup *CTX_wm_gizmo_group(const bContext *C);
 struct wmMsgBus *CTX_wm_message_bus(const bContext *C);
 struct ReportList *CTX_wm_reports(const bContext *C);
+#ifdef WITH_OPENXR
+struct wmXRContext *CTX_wm_xr_context(const bContext *C);
+#endif
 
 struct View3D *CTX_wm_view3d(const bContext *C);
 struct RegionView3D *CTX_wm_region_view3d(const bContext *C);
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index e564e91749c..b5e9ce4d7e4 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -612,6 +612,10 @@ if(WITH_OPENVDB)
   endif()
 endif()
 
+if(WITH_OPENXR)
+  add_definitions(-DWITH_OPENXR)
+endif()
+
 ## Warnings as errors, this is too strict!
 #if(MSVC)
 #   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c
index 4ae87713aa5..08efb3a8263 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.c
@@ -742,6 +742,13 @@ struct ReportList *CTX_wm_reports(const bContext *C)
   return NULL;
 }
 
+#ifdef WITH_OPENXR
+struct wmXRContext *CTX_wm_xr_context(const bContext *C)
+{
+  return C->wm.manager ? C->wm.manager->xr_context : NULL;
+}
+#endif
+
 View3D *CTX_wm_view3d(const bContext *C)
 {
   ScrArea *sa = CTX_wm_area(C);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index d08c9f23db1..9d9081745f7 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -7684,6 +7684,7 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
   wm->undo_stack = NULL;
 
   wm->message_bus = NULL;
+  wm->xr_context = NULL;
 
   BLI_listbase_clear(&wm->jobs);
   BLI_listbase_clear(&wm->drags);
diff --git a/source/blender/makesdna/DNA_windowmanager_types.h b/source/blender/makesdna/DNA_windowmanager_types.h
index 171c0a25187..41063753537 100644
--- a/source/blender/makesdna/DNA_windowmanager_types.h
+++ b/source/blender/makesdna/DNA_windowmanager_types.h
@@ -42,6 +42,7 @@ struct wmKeyMap;
 struct wmMsgBus;
 struct wmOperator;
 struct wmOperatorType;
+struct wmXRContext;
 
 /* forwards */
 struct PointerRNA;
@@ -177,7 +178,9 @@ typedef struct wmWindowManager {
   char par[7];
 
   struct wmMsgBus *message_bus;
-
+  //#ifdef WITH_OPENXR
+  struct wmXRContext *xr_context;
+  //#endif
 } wmWindowManager;
 
 /* wmWindowManager.initialized */
diff --git a/source/blender/windowmanager/CMakeLists.txt b/source/blender/windowmanager/CMakeLists.txt
index e1e11b3759f..559b2888e7c 100644
--- a/source/blender/windowmanager/CMakeLists.txt
+++ b/source/blender/windowmanager/CMakeLists.txt
@@ -175,9 +175,13 @@ if(WITH_COMPOSITOR)
 endif()
 
 if(WITH_OPENXR)
+  list(APPEND SRC
+    intern/wm_xr.c
+  )
   list(APPEND INC_SYS
     ${OPENXR_SDK_INCLUDES}
   )
+
   add_definitions(-DWITH_OPENXR)
 endif()
 
diff --git a/source/blender/windowmanager/intern/wm.c b/source/blender/windowmanager/intern/wm.c
index 77e17ad4687..6e07ab057dc 100644
--- a/source/blender/windowmanager/intern/wm.c
+++ b/source/blender/windowmanager/intern/wm.c
@@ -290,6 +290,12 @@ void WM_check(bContext *C)
     wm->message_bus = WM_msgbus_create();
   }
 
+#ifdef WITH_OPENXR
+  if (wm->xr_context == NULL) {
+    wm->xr_context = wm_xr_context_init();
+  }
+#endif
+
   /* case: fileread */
   /* note: this runs in bg mode to set the screen context cb */
   if ((wm->initialized & WM_WINDOW_IS_INITIALIZED) == 0) {
@@ -374,6 +380,12 @@ void wm_close_and_free(bContext *C, wmWindowManager *wm)
     WM_msgbus_destroy(wm->message_bus);
   }
 
+#ifdef WITH_OPENXR
+  if (wm->xr_context != NULL) {
+    wm_xr_context_destroy(wm->xr_context);
+  }
+#endif
+
   BLI_freelistN(&wm->paintcursors);
 
   WM_drag_free_list(&wm->drags);
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 27f4fded6cf..400f9412f3e 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -128,6 +128,8 @@
 #  include "openxr/openxr.h"
 #endif
 
+#include "wm.h"
+
 #include "DRW_engine.h"
 
 #ifdef WITH_OPENSUBDIV
diff --git a/source/blender/windowmanager/intern/wm_xr.c b/source/blender/windowmanager/intern/wm_xr.c
new file mode 100644
index 00000000000..d3ed9a8b10e
--- /dev/null
+++ b/source/blender/windowmanager/intern/wm_xr.c
@@ -0,0 +1,75 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup wm
+ *
+ * Abstraction for XR (VR, AR, MR, ..) access via OpenXR.
+ */
+
+#include <string.h>
+
+#include "CLG_log.h"
+
+#include "BKE_context.h"
+
+#include "DNA_windowmanager_types.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_assert.h"
+#include "BLI_compiler_attrs.h"
+#include "BLI_string.h"
+
+#include "openxr/openxr.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+#include "wm.h"
+
+#if !defined(WITH_OPENXR)
+static_assert(false, "WITH_OPENXR not defined, but wm_xr.c is being compiled.");
+#endif
+
+typedef struct wmXRContext {
+  struct OpenXRData {
+    XrInstance instance;
+  } oxr;
+} wmXRContext;
+
+wmXRContext *wm_xr_context_init(void)
+{
+  XrInstanceCreateInfo create_info = {.type = XR_TYPE_INSTANCE_CREATE_INFO};
+  XrInstance instance = {0};
+
+  wmXRContext *wm_context = MEM_callocN(sizeof(*wm_context), "wmXRContext");
+
+  BLI_assert(wm_context->oxr.instance == XR_NULL_HANDLE);
+
+  BLI_strncpy(
+      create_info.applicationInfo.applicationName, "Blender", XR_MAX_APPLICATION_NAME_SIZE);
+  create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
+
+  xrCreateInstance(&create_info, &instance);
+
+  return wm_context;
+}
+
+void wm_xr_context_destroy(wmXRContext *wm_context)
+{
+  xrDestroyInstance(wm_context->oxr.instance);
+  MEM_SAFE_FREE(wm_context);
+}
diff --git a/source/blender/windowmanager/wm.h b/source/blender/windowmanager/wm.h
index 5cbaea4f99b..b00cfdbefc1 100644
--- a/source/blender/windowmanager/wm.h
+++ b/source/blender/windowmanager/wm.h
@@ -95,4 +95,10 @@ 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 */
+struct wmXRContext *wm_xr_context_init(void) ATTR_WARN_UNUSED_RESULT;
+void wm_xr_context_destroy(struct wmXRContext *xr_context);
+#endif
+
 #endif /* __WM_H__ */



More information about the Bf-blender-cvs mailing list