[Bf-blender-cvs] [4cfc3caa093] soc-2019-openxr: OpenXR swapchain creation
Julian Eisel
noreply at git.blender.org
Sat Jun 22 21:30:30 CEST 2019
Commit: 4cfc3caa093a2159819d351df490e678921d7695
Author: Julian Eisel
Date: Sat Jun 22 21:25:58 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rB4cfc3caa093a2159819d351df490e678921d7695
OpenXR swapchain creation
Nothing special to say. Just calls needed OpenXR functions to create
swapchains. Swapchain images are not created yet.
Interestingly, the Windows Mixed Realtiy portal now pops up when closing
Blender after having created a VR session. So we're getting closer ;)
===================================================================
M intern/ghost/GHOST_C-api.h
M intern/ghost/intern/GHOST_XRSession.cpp
M intern/ghost/intern/GHOST_XR_intern.h
M source/blender/windowmanager/intern/wm_operators.c
===================================================================
diff --git a/intern/ghost/GHOST_C-api.h b/intern/ghost/GHOST_C-api.h
index f13c80a029b..fce56fd58ef 100644
--- a/intern/ghost/GHOST_C-api.h
+++ b/intern/ghost/GHOST_C-api.h
@@ -1006,6 +1006,7 @@ void GHOST_XrGraphicsContextBindFuncs(struct GHOST_XrContext *xr_context,
GHOST_TSuccess GHOST_XrSessionIsRunning(const struct GHOST_XrContext *xr_context);
void GHOST_XrSessionStart(struct GHOST_XrContext *xr_context);
void GHOST_XrSessionEnd(struct GHOST_XrContext *xr_context);
+void GHOST_XrSessionRenderingPrepare(struct GHOST_XrContext *xr_context);
/* events */
GHOST_TSuccess GHOST_XrEventsHandle(struct GHOST_XrContext *xr_context);
diff --git a/intern/ghost/intern/GHOST_XRSession.cpp b/intern/ghost/intern/GHOST_XRSession.cpp
index 1d205bd1d55..71ae96ddd3c 100644
--- a/intern/ghost/intern/GHOST_XRSession.cpp
+++ b/intern/ghost/intern/GHOST_XRSession.cpp
@@ -18,6 +18,7 @@
* \ingroup GHOST
*/
+#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
@@ -32,10 +33,6 @@
#include "GHOST_XR_intern.h"
-/** \file
- * \ingroup wm
- */
-
GHOST_TSuccess GHOST_XrSessionIsRunning(const GHOST_XrContext *xr_context)
{
if ((xr_context == nullptr) || (xr_context->oxr.session == XR_NULL_HANDLE)) {
@@ -158,7 +155,7 @@ void GHOST_XrSessionStateChange(OpenXRData *oxr, const XrEventDataSessionStateCh
XrSessionBeginInfo begin_info{};
begin_info.type = XR_TYPE_SESSION_BEGIN_INFO;
- begin_info.primaryViewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
+ begin_info.primaryViewConfigurationType = oxr->view_type;
xrBeginSession(oxr->session, &begin_info);
break;
}
@@ -170,3 +167,91 @@ void GHOST_XrSessionStateChange(OpenXRData *oxr, const XrEventDataSessionStateCh
break;
}
}
+
+static bool swapchain_format_choose(GHOST_TXrGraphicsBinding gpu_binding_type,
+ const std::vector<int64_t> &runtime_formats,
+ int64_t *r_result)
+{
+ std::vector<int64_t> gpu_binding_formats;
+
+ switch (gpu_binding_type) {
+ case GHOST_kXrGraphicsOpenGL: {
+ gpu_binding_formats = {GL_RGBA8};
+ break;
+ }
+#ifdef WIN32
+ case GHOST_kXrGraphicsD3D11: {
+ gpu_binding_formats = {DXGI_FORMAT_R8G8B8A8_UNORM};
+ break;
+ }
+#endif
+ default:
+ return false;
+ }
+
+ 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;
+}
+
+static bool swapchain_create(OpenXRData *oxr,
+ GHOST_TXrGraphicsBinding gpu_binding_type,
+ 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);
+ std::vector<int64_t> swapchain_formats(format_count);
+ xrEnumerateSwapchainFormats(
+ oxr->session, swapchain_formats.size(), &format_count, swapchain_formats.data());
+ assert(swapchain_formats.size() == format_count);
+
+ if (!swapchain_format_choose(gpu_binding_type, swapchain_formats, &chosen_format)) {
+ fprintf(stderr, "Error: No format matching OpenXR runtime supported swapchain formats found.");
+ return false;
+ }
+
+ create_info.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT |
+ XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
+ create_info.format = chosen_format;
+ create_info.sampleCount = xr_view->recommendedSwapchainSampleCount;
+ create_info.width = xr_view->recommendedImageRectWidth;
+ create_info.height = xr_view->recommendedImageRectHeight;
+ create_info.faceCount = 1;
+ create_info.arraySize = 1;
+ create_info.mipCount = 1;
+ xrCreateSwapchain(oxr->session, &create_info, &swapchain);
+
+ return true;
+}
+
+void GHOST_XrSessionRenderingPrepare(GHOST_XrContext *xr_context)
+{
+ OpenXRData *oxr = &xr_context->oxr;
+ std::vector<XrViewConfigurationView> views;
+ uint32_t view_count;
+
+ xrEnumerateViewConfigurationViews(
+ oxr->instance, oxr->system_id, oxr->view_type, 0, &view_count, nullptr);
+ views.resize(view_count, {XR_TYPE_VIEW_CONFIGURATION_VIEW});
+ xrEnumerateViewConfigurationViews(
+ oxr->instance, oxr->system_id, oxr->view_type, views.size(), &view_count, views.data());
+
+ for (const XrViewConfigurationView &view : views) {
+ swapchain_create(oxr, xr_context->gpu_binding_type, &view);
+ }
+}
diff --git a/intern/ghost/intern/GHOST_XR_intern.h b/intern/ghost/intern/GHOST_XR_intern.h
index 1e4897d4265..5c92b2a6a50 100644
--- a/intern/ghost/intern/GHOST_XR_intern.h
+++ b/intern/ghost/intern/GHOST_XR_intern.h
@@ -32,6 +32,8 @@ typedef struct OpenXRData {
std::vector<XrApiLayerProperties> layers;
XrSystemId system_id;
+ // Only stereo rendering supported now.
+ const XrViewConfigurationType view_type{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO};
XrSession session;
XrSessionState session_state;
} OpenXRData;
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index a5b0687f05d..33b78b04486 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -3679,6 +3679,7 @@ static int wm_xr_session_toggle_exec(bContext *C, wmOperator *UNUSED(op))
xr_session_gpu_binding_context_create,
xr_session_gpu_binding_context_destroy);
GHOST_XrSessionStart(wm->xr_context);
+ GHOST_XrSessionRenderingPrepare(wm->xr_context);
}
return OPERATOR_FINISHED;
}
More information about the Bf-blender-cvs
mailing list