[Bf-blender-cvs] [8663aa80e63] soc-2019-openxr: Use abstract class/interface for generalized graphics binding operations

Julian Eisel noreply at git.blender.org
Sun Jun 23 00:50:51 CEST 2019


Commit: 8663aa80e6346aa01d62898714bae5370c65aa28
Author: Julian Eisel
Date:   Sun Jun 23 00:37:17 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rB8663aa80e6346aa01d62898714bae5370c65aa28

Use abstract class/interface for generalized graphics binding operations

Rather than having switch case blocks throughout the session code, give
binding operations an own interface with dedicated implementations.

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

M	intern/ghost/CMakeLists.txt
M	intern/ghost/intern/GHOST_ContextD3D.h
M	intern/ghost/intern/GHOST_ContextGLX.h
M	intern/ghost/intern/GHOST_ContextWGL.h
A	intern/ghost/intern/GHOST_IXRGraphicsBinding.h
M	intern/ghost/intern/GHOST_XR.cpp
A	intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
M	intern/ghost/intern/GHOST_XRSession.cpp
M	intern/ghost/intern/GHOST_XR_intern.h

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

diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index 912888bbce6..7e47b7eab08 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -351,10 +351,12 @@ if(WITH_OPENXR)
   list(APPEND SRC
     intern/GHOST_Xr.cpp
     intern/GHOST_XrEvent.cpp
+    intern/GHOST_XrGraphicsBinding.cpp
     intern/GHOST_XrSession.cpp
 
     intern/GHOST_Xr_intern.h
     intern/GHOST_Xr_openxr_includes.h
+    intern/GHOST_IXrGraphicsBinding.h
 #    intern/GHOST_XrSession.h
   )
   list(APPEND INC
diff --git a/intern/ghost/intern/GHOST_ContextD3D.h b/intern/ghost/intern/GHOST_ContextD3D.h
index 407684cf643..a4ff58a0b46 100644
--- a/intern/ghost/intern/GHOST_ContextD3D.h
+++ b/intern/ghost/intern/GHOST_ContextD3D.h
@@ -32,7 +32,7 @@
 
 class GHOST_ContextD3D : public GHOST_Context {
   /* XR code needs low level graphics data to send to OpenXR. */
-  friend class GHOST_XrGraphicsBinding;
+  friend class GHOST_XrGraphicsBindingD3D;
 
  public:
   GHOST_ContextD3D(bool stereoVisual, HWND hWnd);
diff --git a/intern/ghost/intern/GHOST_ContextGLX.h b/intern/ghost/intern/GHOST_ContextGLX.h
index 96777fb2983..07d2601cd17 100644
--- a/intern/ghost/intern/GHOST_ContextGLX.h
+++ b/intern/ghost/intern/GHOST_ContextGLX.h
@@ -39,7 +39,7 @@
 
 class GHOST_ContextGLX : public GHOST_Context {
   /* XR code needs low level graphics data to send to OpenXR. */
-  friend class GHOST_XrGraphicsBinding;
+  friend class GHOST_XrGraphicsBindingOpenGL;
 
  public:
   /**
diff --git a/intern/ghost/intern/GHOST_ContextWGL.h b/intern/ghost/intern/GHOST_ContextWGL.h
index 08450b388cb..99d1bb6efe5 100644
--- a/intern/ghost/intern/GHOST_ContextWGL.h
+++ b/intern/ghost/intern/GHOST_ContextWGL.h
@@ -36,7 +36,7 @@
 
 class GHOST_ContextWGL : public GHOST_Context {
   /* XR code needs low level graphics data to send to OpenXR. */
-  friend class GHOST_XrGraphicsBinding;
+  friend class GHOST_XrGraphicsBindingOpenGL;
 
  public:
   /**
diff --git a/intern/ghost/intern/GHOST_IXRGraphicsBinding.h b/intern/ghost/intern/GHOST_IXRGraphicsBinding.h
new file mode 100644
index 00000000000..6dafb3b3aa7
--- /dev/null
+++ b/intern/ghost/intern/GHOST_IXRGraphicsBinding.h
@@ -0,0 +1,50 @@
+/*
+ * 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 GHOST
+ */
+
+#ifndef __GHOST_IXRGRAPHICSBINDING_H__
+#define __GHOST_IXRGRAPHICSBINDING_H__
+
+#include <memory>
+#include <vector>
+#include "GHOST_XR_openxr_includes.h"
+
+class GHOST_IXrGraphicsBinding {
+  friend std::unique_ptr<GHOST_IXrGraphicsBinding> GHOST_XrGraphicsBindingCreateFromType(
+      GHOST_TXrGraphicsBinding type);
+
+ public:
+  union {
+#if defined(WITH_X11)
+    XrGraphicsBindingOpenGLXlibKHR glx;
+#elif defined(WIN32)
+    XrGraphicsBindingOpenGLWin32KHR wgl;
+    XrGraphicsBindingD3D11KHR d3d11;
+#endif
+  } oxr_binding;
+
+  virtual void initFromGhostContext(class GHOST_Context *ghost_ctx) = 0;
+  virtual bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats, int64_t *r_result) = 0;
+
+ protected:
+  /* Use GHOST_XrGraphicsBindingCreateFromType */
+  GHOST_IXrGraphicsBinding() = default;
+};
+
+#endif /* __GHOST_IXRGRAPHICSBINDING_H__ */
diff --git a/intern/ghost/intern/GHOST_XR.cpp b/intern/ghost/intern/GHOST_XR.cpp
index 94759e53a3b..ff104e49e47 100644
--- a/intern/ghost/intern/GHOST_XR.cpp
+++ b/intern/ghost/intern/GHOST_XR.cpp
@@ -255,7 +255,6 @@ void GHOST_XrContextDestroy(GHOST_XrContext *xr_context)
 
   /* Unbinding may involve destruction, so call here too */
   GHOST_XrGraphicsContextUnbind(*xr_context);
-  delete xr_context->gpu_binding;
 
   if (oxr->session != XR_NULL_HANDLE) {
     xrDestroySession(oxr->session);
diff --git a/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp b/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
new file mode 100644
index 00000000000..974c3cc2377
--- /dev/null
+++ b/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
@@ -0,0 +1,116 @@
+/*
+ * 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 GHOST
+ */
+
+#include <algorithm>
+
+#if defined(WITH_X11)
+#  include "GHOST_ContextGLX.h"
+#elif defined(WIN32)
+#  include "GHOST_ContextWGL.h"
+#  include "GHOST_ContextD3D.h"
+#endif
+#include "GHOST_C-api.h"
+#include "GHOST_XR_intern.h"
+
+#include "GHOST_IXRGraphicsBinding.h"
+
+bool choose_swapchain_format_from_candidates(std::vector<int64_t> gpu_binding_formats,
+                                             std::vector<int64_t> runtime_formats,
+                                             int64_t *r_result)
+{
+  if (gpu_binding_formats.empty()) {
+    return false;
+  }
+
+  auto res = std::find_first_of(gpu_binding_formats.begin(),
+                                gpu_binding_formats.end(),
+                                runtime_formats.begin(),
+                                runtime_formats.end());
+  if (res == gpu_binding_formats.end()) {
+    return false;
+  }
+
+  *r_result = *res;
+  return true;
+}
+
+class GHOST_XrGraphicsBindingOpenGL : public GHOST_IXrGraphicsBinding {
+ public:
+  void initFromGhostContext(GHOST_Context *ghost_ctx) override
+  {
+#if defined(WITH_X11)
+    GHOST_ContextGLX *ctx_glx = static_cast<GHOST_ContextGLX *>(ghost_ctx);
+    XVisualInfo *visual_info = glXGetVisualFromFBConfig(ctx_glx->m_display, ctx_glx->m_fbconfig);
+
+    oxr_binding.glx.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
+    oxr_binding.glx.xDisplay = ctx_glx->m_display;
+    oxr_binding.glx.glxFBConfig = ctx_glx->m_fbconfig;
+    oxr_binding.glx.glxDrawable = ctx_glx->m_window;
+    oxr_binding.glx.glxContext = ctx_glx->m_context;
+    oxr_binding.glx.visualid = visual_info->visualid;
+#elif defined(WIN32)
+    GHOST_ContextWGL *ctx_wgl = static_cast<GHOST_ContextWGL *>(ghost_ctx);
+
+    oxr_binding.wgl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
+    oxr_binding.wgl.hDC = ctx_wgl->m_hDC;
+    oxr_binding.wgl.hGLRC = ctx_wgl->m_hGLRC;
+#endif
+  }
+
+  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats, int64_t *r_result) override
+  {
+    std::vector<int64_t> gpu_binding_formats = {GL_RGBA8};
+    return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
+  }
+};
+
+#ifdef WIN32
+class GHOST_XrGraphicsBindingD3D : public GHOST_IXrGraphicsBinding {
+ public:
+  void initFromGhostContext(GHOST_Context *ghost_ctx) override
+  {
+    GHOST_ContextD3D *ctx_d3d = static_cast<GHOST_ContextD3D *>(ghost_ctx);
+
+    oxr_binding.d3d11.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
+    oxr_binding.d3d11.device = ctx_d3d->m_device.Get();
+  }
+
+  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats, int64_t *r_result) override
+  {
+    std::vector<int64_t> gpu_binding_formats = {DXGI_FORMAT_R8G8B8A8_UNORM};
+    return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
+  }
+};
+#endif  // WIN32
+
+std::unique_ptr<GHOST_IXrGraphicsBinding> GHOST_XrGraphicsBindingCreateFromType(
+    GHOST_TXrGraphicsBinding type)
+{
+  switch (type) {
+    case GHOST_kXrGraphicsOpenGL:
+      return std::unique_ptr<GHOST_XrGraphicsBindingOpenGL>(new GHOST_XrGraphicsBindingOpenGL());
+#ifdef WIN32
+    case GHOST_kXrGraphicsD3D11:
+      return std::unique_ptr<GHOST_XrGraphicsBindingD3D>(new GHOST_XrGraphicsBindingD3D());
+#endif
+    default:
+      return nullptr;
+  }
+}
diff --git a/intern/ghost/intern/GHOST_XRSession.cpp b/intern/ghost/intern/GHOST_XRSession.cpp
index 71ae96ddd3c..167a1c5d280 100644
--- a/intern/ghost/intern/GHOST_XRSession.cpp
+++ b/intern/ghost/intern/GHOST_XRSession.cpp
@@ -21,15 +21,10 @@
 #include <algorithm>
 #include <cassert>
 #include <cstdio>
-#include <cstring>
 
 #include "GHOST_C-api.h"
-#if defined(WITH_X11)
-#  include "GHOST_ContextGLX.h"
-#elif defined(WIN32)
-#  include "GHOST_ContextWGL.h"
-#  include "GHOST_ContextD3D.h"
-#endif
+
+#include "GHOST_IXRGraphicsBinding.h"
 
 #include "GHOST_XR_intern.h"
 
@@ -64,46 +59,6 @@ static void GHOST_XrSystemInit(OpenXRData *oxr)
   xrGetSystem(oxr->instance, &system_info, &oxr->system_id);
 }
 
-void GHOST_XrGraphicsBinding::initFromGhostContext(GHOST_TXrGraphicsBinding type,
-                                                   GHOST_Context *ghost_ctx)
-{
-  switch (type) {
-    case GHOST_kXrGraphicsOpenGL: {
-#if defined(WITH_X11)
-      GHOST_ContextGLX *ctx_glx = static_cast<GHOST_ContextGLX *>(ghost_ctx);
-      XVisualInfo *visual_info = glXGetVisualFromFBConfig(ctx_glx->m_display, ctx_glx->m_fbconfig);
-
-      oxr_binding.glx.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
-      oxr_binding.glx.xDisplay = ctx_glx->m_display;
-      oxr_binding.glx.glxFBConfig = ctx_glx->m_fbconfig;
-      oxr_binding.glx.glxDrawable = ctx_glx->m_window;
-      oxr_binding.glx.glxContext = ctx_glx->m_context;
-      oxr_binding.glx.visualid = visual_info->visualid;
-#elif defined(WIN32)
-      GHOST_ContextWGL 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list