[Bf-blender-cvs] [d749e8a2c47] soc-2019-openxr: Create graphics binding specific swapchain images

Julian Eisel noreply at git.blender.org
Sun Jun 23 02:16:56 CEST 2019


Commit: d749e8a2c47f782af62d203e3b66742490541d24
Author: Julian Eisel
Date:   Sun Jun 23 02:06:07 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rBd749e8a2c47f782af62d203e3b66742490541d24

Create graphics binding specific swapchain images

Following the OpenXR SDK's example code very closely here. We have to do
some nasty converting of graphics binding specific image vectors to
generalized base ones. The SDK's approach seems like a good way to go
about this.

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

M	intern/ghost/intern/GHOST_IXRGraphicsBinding.h
M	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/intern/GHOST_IXRGraphicsBinding.h b/intern/ghost/intern/GHOST_IXRGraphicsBinding.h
index 6dafb3b3aa7..794153f251f 100644
--- a/intern/ghost/intern/GHOST_IXRGraphicsBinding.h
+++ b/intern/ghost/intern/GHOST_IXRGraphicsBinding.h
@@ -40,7 +40,10 @@ class GHOST_IXrGraphicsBinding {
   } 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;
+  virtual bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats,
+                                     int64_t *r_result) const = 0;
+  virtual std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(
+      uint32_t image_count) = 0;
 
  protected:
   /* Use GHOST_XrGraphicsBindingCreateFromType */
diff --git a/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp b/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
index 974c3cc2377..f42fa0fe059 100644
--- a/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
+++ b/intern/ghost/intern/GHOST_XRGraphicsBinding.cpp
@@ -19,6 +19,7 @@
  */
 
 #include <algorithm>
+#include <list>
 
 #if defined(WITH_X11)
 #  include "GHOST_ContextGLX.h"
@@ -74,11 +75,33 @@ class GHOST_XrGraphicsBindingOpenGL : public GHOST_IXrGraphicsBinding {
 #endif
   }
 
-  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats, int64_t *r_result) override
+  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats,
+                             int64_t *r_result) const override
   {
     std::vector<int64_t> gpu_binding_formats = {GL_RGBA8};
     return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
   }
+
+  std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
+  {
+    std::vector<XrSwapchainImageOpenGLKHR> ogl_images(image_count);
+    std::vector<XrSwapchainImageBaseHeader *> base_images;
+
+    // Need to return vector of base header pointers, so of a different type. Need to build a new
+    // list with this type, and keep the initial one alive.
+    for (XrSwapchainImageOpenGLKHR &image : ogl_images) {
+      image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
+      base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
+    }
+
+    // Keep alive.
+    m_image_cache.push_back(std::move(ogl_images));
+
+    return base_images;
+  }
+
+ private:
+  std::list<std::vector<XrSwapchainImageOpenGLKHR>> m_image_cache;
 };
 
 #ifdef WIN32
@@ -92,11 +115,33 @@ class GHOST_XrGraphicsBindingD3D : public GHOST_IXrGraphicsBinding {
     oxr_binding.d3d11.device = ctx_d3d->m_device.Get();
   }
 
-  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats, int64_t *r_result) override
+  bool chooseSwapchainFormat(std::vector<int64_t> runtime_formats,
+                             int64_t *r_result) const 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);
   }
+
+  std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
+  {
+    std::vector<XrSwapchainImageD3D11KHR> d3d_images(image_count);
+    std::vector<XrSwapchainImageBaseHeader *> base_images;
+
+    // Need to return vector of base header pointers, so of a different type. Need to build a new
+    // list with this type, and keep the initial one alive.
+    for (XrSwapchainImageD3D11KHR &image : d3d_images) {
+      image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR;
+      base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
+    }
+
+    // Keep alive.
+    m_image_cache.push_back(std::move(d3d_images));
+
+    return base_images;
+  }
+
+ private:
+  std::list<std::vector<XrSwapchainImageD3D11KHR>> m_image_cache;
 };
 #endif  // WIN32
 
diff --git a/intern/ghost/intern/GHOST_XRSession.cpp b/intern/ghost/intern/GHOST_XRSession.cpp
index 167a1c5d280..399c0fbc065 100644
--- a/intern/ghost/intern/GHOST_XRSession.cpp
+++ b/intern/ghost/intern/GHOST_XRSession.cpp
@@ -122,24 +122,37 @@ void GHOST_XrSessionStateChange(OpenXRData *oxr, const XrEventDataSessionStateCh
   }
 }
 
-static bool swapchain_create(const GHOST_XrContext *xr_context,
-                             OpenXRData *oxr,
-                             const XrViewConfigurationView *xr_view)
+static std::vector<XrSwapchainImageBaseHeader *> swapchain_images_create(
+    XrSwapchain swapchain, GHOST_IXrGraphicsBinding *gpu_binding)
+{
+  std::vector<XrSwapchainImageBaseHeader *> images;
+  uint32_t image_count;
+
+  xrEnumerateSwapchainImages(swapchain, 0, &image_count, nullptr);
+  images = gpu_binding->createSwapchainImages(image_count);
+  xrEnumerateSwapchainImages(swapchain, images.size(), &image_count, images[0]);
+
+  return images;
+}
+
+static XrSwapchain swapchain_create(const XrSession session,
+                                    GHOST_IXrGraphicsBinding *gpu_binding,
+                                    const XrViewConfigurationView *xr_view)
 {
   XrSwapchainCreateInfo create_info{XR_TYPE_SWAPCHAIN_CREATE_INFO};
   XrSwapchain swapchain;
   uint32_t format_count = 0;
   int64_t chosen_format;
 
-  xrEnumerateSwapchainFormats(oxr->session, 0, &format_count, nullptr);
+  xrEnumerateSwapchainFormats(session, 0, &format_count, nullptr);
   std::vector<int64_t> swapchain_formats(format_count);
   xrEnumerateSwapchainFormats(
-      oxr->session, swapchain_formats.size(), &format_count, swapchain_formats.data());
+      session, swapchain_formats.size(), &format_count, swapchain_formats.data());
   assert(swapchain_formats.size() == format_count);
 
-  if (!xr_context->gpu_binding->chooseSwapchainFormat(swapchain_formats, &chosen_format)) {
+  if (!gpu_binding->chooseSwapchainFormat(swapchain_formats, &chosen_format)) {
     fprintf(stderr, "Error: No format matching OpenXR runtime supported swapchain formats found.");
-    return false;
+    return nullptr;
   }
 
   create_info.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT |
@@ -151,9 +164,9 @@ static bool swapchain_create(const GHOST_XrContext *xr_context,
   create_info.faceCount = 1;
   create_info.arraySize = 1;
   create_info.mipCount = 1;
-  xrCreateSwapchain(oxr->session, &create_info, &swapchain);
+  xrCreateSwapchain(session, &create_info, &swapchain);
 
-  return true;
+  return swapchain;
 }
 
 void GHOST_XrSessionRenderingPrepare(GHOST_XrContext *xr_context)
@@ -169,6 +182,9 @@ void GHOST_XrSessionRenderingPrepare(GHOST_XrContext *xr_context)
       oxr->instance, oxr->system_id, oxr->view_type, views.size(), &view_count, views.data());
 
   for (const XrViewConfigurationView &view : views) {
-    swapchain_create(xr_context, oxr, &view);
+    XrSwapchain swapchain = swapchain_create(oxr->session, xr_context->gpu_binding.get(), &view);
+    auto images = swapchain_images_create(swapchain, xr_context->gpu_binding.get());
+
+    oxr->swapchain_images.insert(std::make_pair(swapchain, std::move(images)));
   }
 }
diff --git a/intern/ghost/intern/GHOST_XR_intern.h b/intern/ghost/intern/GHOST_XR_intern.h
index 0d02fbff693..2fb5bdcdff9 100644
--- a/intern/ghost/intern/GHOST_XR_intern.h
+++ b/intern/ghost/intern/GHOST_XR_intern.h
@@ -21,6 +21,7 @@
 #ifndef __GHOST_XR_INTERN_H__
 #define __GHOST_XR_INTERN_H__
 
+#include <map>
 #include <memory>
 #include <vector>
 
@@ -38,6 +39,8 @@ typedef struct OpenXRData {
   const XrViewConfigurationType view_type{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO};
   XrSession session;
   XrSessionState session_state;
+
+  std::map<XrSwapchain, std::vector<XrSwapchainImageBaseHeader *>> swapchain_images;
 } OpenXRData;
 
 typedef struct GHOST_XrContext {



More information about the Bf-blender-cvs mailing list