From aa150a830adb678282ca5ebb89568db04a850924 Mon Sep 17 00:00:00 2001 From: Lubos Date: Sun, 28 Aug 2022 21:09:02 +0200 Subject: [PATCH] OpenXR - Swapchain acquiring fixed --- Common/VR/VRBase.h | 2 +- Common/VR/VRFramebuffer.cpp | 72 +++++++++++++++++++------------------ Common/VR/VRRenderer.cpp | 7 ++-- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/Common/VR/VRBase.h b/Common/VR/VRBase.h index 482c541a91..ec42749424 100644 --- a/Common/VR/VRBase.h +++ b/Common/VR/VRBase.h @@ -99,7 +99,7 @@ typedef struct { XrSwapchainImageOpenGLESKHR* ColorSwapChainImage; XrSwapchainImageOpenGLESKHR* DepthSwapChainImage; unsigned int* FrameBuffers; - bool Acquired; + bool Acquired[2]; } ovrFramebuffer; typedef struct { diff --git a/Common/VR/VRFramebuffer.cpp b/Common/VR/VRFramebuffer.cpp index cda680908d..4ee3fae477 100644 --- a/Common/VR/VRFramebuffer.cpp +++ b/Common/VR/VRFramebuffer.cpp @@ -38,7 +38,8 @@ void ovrFramebuffer_Clear(ovrFramebuffer* frameBuffer) { frameBuffer->DepthSwapChain.Height = 0; frameBuffer->FrameBuffers = NULL; - frameBuffer->Acquired = false; + frameBuffer->Acquired[0] = false; + frameBuffer->Acquired[1] = false; } bool ovrFramebuffer_Create( @@ -72,11 +73,7 @@ bool ovrFramebuffer_Create( swapChainCreateInfo.height = height; swapChainCreateInfo.faceCount = 1; swapChainCreateInfo.mipCount = 1; -#ifdef OPENXR_MULTIVIEW - swapChainCreateInfo.arraySize = 2; -#else - swapChainCreateInfo.arraySize = 1; -#endif + swapChainCreateInfo.arraySize = 1; //TODO:set to 2 to enable multiview frameBuffer->ColorSwapChain.Width = swapChainCreateInfo.width; frameBuffer->ColorSwapChain.Height = swapChainCreateInfo.height; @@ -123,13 +120,13 @@ bool ovrFramebuffer_Create( // Create the frame buffer. GL(glGenFramebuffers(1, &frameBuffer->FrameBuffers[i])); GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->FrameBuffers[i])); -#ifdef OPENXR_MULTIVIEW - GL(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0, 0, 2)); - GL(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, colorTexture, 0, 0, 2)); -#else - GL(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0)); - GL(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0)); -#endif + if (swapChainCreateInfo.arraySize > 1) { + GL(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0, 0, 2)); + GL(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, colorTexture, 0, 0, 2)); + } else { + GL(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0)); + GL(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0)); + } GL(GLenum renderFramebufferStatus = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)); GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); if (renderFramebufferStatus != GL_FRAMEBUFFER_COMPLETE) { @@ -167,33 +164,38 @@ void ovrFramebuffer_Resolve(ovrFramebuffer* frameBuffer) { } void ovrFramebuffer_Acquire(ovrFramebuffer* frameBuffer) { - // Acquire the swapchain image - XrSwapchainImageAcquireInfo acquireInfo = {XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO, NULL}; - OXR(xrAcquireSwapchainImage( - frameBuffer->ColorSwapChain.Handle, &acquireInfo, &frameBuffer->TextureSwapChainIndex)); + ovrSwapChain swapChains[2] = {frameBuffer->ColorSwapChain, frameBuffer->DepthSwapChain}; - XrSwapchainImageWaitInfo waitInfo; - waitInfo.type = XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO; - waitInfo.next = NULL; - waitInfo.timeout = 1000; /* timeout in nanoseconds */ - XrResult res = xrWaitSwapchainImage(frameBuffer->ColorSwapChain.Handle, &waitInfo); - int i = 0; - while ((res != XR_SUCCESS) && (i < 10)) { - res = xrWaitSwapchainImage(frameBuffer->ColorSwapChain.Handle, &waitInfo); - i++; - ALOGV( - " Retry xrWaitSwapchainImage %d times due to XR_TIMEOUT_EXPIRED (duration %f micro seconds)", - i, - waitInfo.timeout * (1E-9)); + for (int index = 0; index < 2; index++) { + XrSwapchainImageAcquireInfo acquireInfo = {XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO, NULL}; + OXR(xrAcquireSwapchainImage(swapChains[index].Handle, &acquireInfo, &frameBuffer->TextureSwapChainIndex)); + + XrSwapchainImageWaitInfo waitInfo; + waitInfo.type = XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO; + waitInfo.next = NULL; + waitInfo.timeout = 1000; /* timeout in nanoseconds */ + XrResult res = xrWaitSwapchainImage(swapChains[index].Handle, &waitInfo); + int i = 0; + while ((res != XR_SUCCESS) && (i < 10)) { + res = xrWaitSwapchainImage(swapChains[index].Handle, &waitInfo); + i++; + ALOGV( + " Retry xrWaitSwapchainImage %d times due to XR_TIMEOUT_EXPIRED (duration %f micro seconds)", + i, + waitInfo.timeout * (1E-9)); + } + frameBuffer->Acquired[index] = res == XR_SUCCESS; } - frameBuffer->Acquired = res == XR_SUCCESS; } void ovrFramebuffer_Release(ovrFramebuffer* frameBuffer) { - if (frameBuffer->Acquired) { - XrSwapchainImageReleaseInfo releaseInfo = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO, NULL}; - OXR(xrReleaseSwapchainImage(frameBuffer->ColorSwapChain.Handle, &releaseInfo)); - frameBuffer->Acquired = false; + ovrSwapChain swapChains[2] = {frameBuffer->ColorSwapChain, frameBuffer->DepthSwapChain}; + for (int index = 0; index < 2; index++) { + if (frameBuffer->Acquired[index]) { + XrSwapchainImageReleaseInfo releaseInfo = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO, NULL}; + OXR(xrReleaseSwapchainImage(swapChains[index].Handle, &releaseInfo)); + frameBuffer->Acquired[index] = false; + } } } diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 455fbfe1f0..0a952de4a8 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -26,8 +26,7 @@ void VR_UpdateStageBounds(ovrApp* pappState) { XrExtent2Df stageBounds = {}; XrResult result; - OXR(result = xrGetReferenceSpaceBoundsRect( - pappState->Session, XR_REFERENCE_SPACE_TYPE_STAGE, &stageBounds)); + OXR(result = xrGetReferenceSpaceBoundsRect(pappState->Session, XR_REFERENCE_SPACE_TYPE_STAGE, &stageBounds)); if (result != XR_SUCCESS) { ALOGV("Stage bounds query failed: using small defaults"); stageBounds.width = 1.0f; @@ -328,9 +327,9 @@ void VR_EndFrame( engine_t* engine ) { glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); ovrFramebuffer* frameBuffer = &engine->appState.Renderer.FrameBuffer; - //TODO:ovrFramebuffer_Resolve(frameBuffer); + //ovrFramebuffer_Resolve(frameBuffer); ovrFramebuffer_Release(frameBuffer); - //TODO:ovrFramebuffer_SetNone(); + ovrFramebuffer_SetNone(); XrCompositionLayerProjectionView projection_layer_elements[2] = {}; int vrMode = vrConfig[VR_CONFIG_MODE];