From 8f38f0536c1505cb4b784963eb9cab8cfa1eee9d Mon Sep 17 00:00:00 2001 From: Mads Buvik Sandvei Date: Tue, 2 Jun 2020 21:46:22 +0200 Subject: [PATCH] Separate GLRC for OpenXR to ensure uncontested access. Safety check on predictions to police that they are made only once per frame. --- apps/openmw/mwvr/openxrmanagerimpl.cpp | 30 +++++++++++++++++++++++--- apps/openmw/mwvr/openxrmanagerimpl.hpp | 3 +++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwvr/openxrmanagerimpl.cpp b/apps/openmw/mwvr/openxrmanagerimpl.cpp index 7cc58b8bb..eae8626a1 100644 --- a/apps/openmw/mwvr/openxrmanagerimpl.cpp +++ b/apps/openmw/mwvr/openxrmanagerimpl.cpp @@ -93,10 +93,16 @@ namespace MWVR { // Create Session // TODO: Platform dependent + auto DC = wglGetCurrentDC(); + auto GLRC = wglGetCurrentContext(); + auto XRGLRC = wglCreateContext(DC); + wglShareLists(GLRC, XRGLRC); + wglMakeCurrent(DC, XRGLRC); + mGraphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR; mGraphicsBinding.next = nullptr; - mGraphicsBinding.hDC = wglGetCurrentDC(); - mGraphicsBinding.hGLRC = wglGetCurrentContext(); + mGraphicsBinding.hDC = DC; + mGraphicsBinding.hGLRC = XRGLRC; if (!mGraphicsBinding.hDC) std::cout << "Missing DC" << std::endl; @@ -108,6 +114,7 @@ namespace MWVR createInfo.systemId = mSystemId; CHECK_XRCMD(xrCreateSession(mInstance, &createInfo, &mSession)); assert(mSession); + wglMakeCurrent(DC, GLRC); } LogLayersAndExtensions(); @@ -315,7 +322,11 @@ namespace MWVR int64_t predictedDisplayTime, TrackedSpace space) { - + if (!mPredictionsEnabled) + { + Log(Debug::Error) << "Prediction out of order"; + throw std::logic_error("Prediction out of order"); + } std::array views{ {{XR_TYPE_VIEW}, {XR_TYPE_VIEW}} }; XrViewState viewState{ XR_TYPE_VIEW_STATE }; uint32_t viewCount = 2; @@ -339,6 +350,11 @@ namespace MWVR MWVR::Pose OpenXRManagerImpl::getPredictedLimbPose(int64_t predictedDisplayTime, TrackedLimb limb, TrackedSpace space) { + if (!mPredictionsEnabled) + { + Log(Debug::Error) << "Prediction out of order"; + throw std::logic_error("Prediction out of order"); + } XrSpaceLocation location{ XR_TYPE_SPACE_LOCATION }; XrSpaceVelocity velocity{ XR_TYPE_SPACE_VELOCITY }; location.next = &velocity; @@ -500,6 +516,14 @@ namespace MWVR referenceSpace = mReferenceSpaceView; return referenceSpace; } + void OpenXRManagerImpl::enablePredictions() + { + mPredictionsEnabled = true; + } + void OpenXRManagerImpl::disablePredictions() + { + mPredictionsEnabled = false; + } } namespace osg diff --git a/apps/openmw/mwvr/openxrmanagerimpl.hpp b/apps/openmw/mwvr/openxrmanagerimpl.hpp index ed58b186c..8ad78e28e 100644 --- a/apps/openmw/mwvr/openxrmanagerimpl.hpp +++ b/apps/openmw/mwvr/openxrmanagerimpl.hpp @@ -65,8 +65,11 @@ namespace MWVR void HandleSessionStateChanged(const XrEventDataSessionStateChanged& stateChangedEvent); XrFrameState frameState(); XrSpace getReferenceSpace(TrackedSpace space); + void enablePredictions(); + void disablePredictions(); bool initialized = false; + bool mPredictionsEnabled = false; long long mFrameIndex = 0; XrInstance mInstance = XR_NULL_HANDLE; XrSession mSession = XR_NULL_HANDLE;