From 08226d839613cda8e2f3fe636e306a83c8b3eb79 Mon Sep 17 00:00:00 2001 From: Lubos Date: Mon, 5 Sep 2022 20:49:25 +0200 Subject: [PATCH] OpenXR - Stereo matrices fixed --- Common/GPU/OpenGL/GLQueueRunner.cpp | 32 +++++++++++++++++++++-------- Common/VR/VRRenderer.cpp | 8 +++++++- GPU/GLES/ShaderManagerGLES.cpp | 12 ++--------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Common/GPU/OpenGL/GLQueueRunner.cpp b/Common/GPU/OpenGL/GLQueueRunner.cpp index ce6464449c..d7212985fa 100644 --- a/Common/GPU/OpenGL/GLQueueRunner.cpp +++ b/Common/GPU/OpenGL/GLQueueRunner.cpp @@ -1029,15 +1029,29 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step, bool first, bool last case GLRRenderCommand::UNIFORMSTEREOMATRIX: { _dbg_assert_(curProgram); - int layout = GetStereoBufferIndex(c.uniformMatrix4.name); - if (layout >= 0) { - int size = 2 * 16 * sizeof(float); - glBindBufferBase(GL_UNIFORM_BUFFER, layout, *c.uniformMatrix4.loc); - glBindBuffer(GL_UNIFORM_BUFFER, *c.uniformMatrix4.loc); - void *matrices = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); - memcpy(matrices, c.uniformMatrix4.m, size); - glUnmapBuffer(GL_UNIFORM_BUFFER); - glBindBuffer(GL_UNIFORM_BUFFER, 0); + if (IsMultiviewSupported()) { + int layout = GetStereoBufferIndex(c.uniformMatrix4.name); + if (layout >= 0) { + int size = 2 * 16 * sizeof(float); + glBindBufferBase(GL_UNIFORM_BUFFER, layout, *c.uniformMatrix4.loc); + glBindBuffer(GL_UNIFORM_BUFFER, *c.uniformMatrix4.loc); + void *matrices = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); + memcpy(matrices, c.uniformMatrix4.m, size); + glUnmapBuffer(GL_UNIFORM_BUFFER); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + } + } else { + int loc = c.uniformMatrix4.loc ? *c.uniformMatrix4.loc : -1; + if (c.uniformMatrix4.name) { + loc = curProgram->GetUniformLoc(c.uniformMatrix4.name); + } + if (loc >= 0) { + if (GetVRFBOIndex() == 0) { + glUniformMatrix4fv(loc, 1, false, c.uniformMatrix4.m); + } else { + glUniformMatrix4fv(loc, 1, false, &c.uniformMatrix4.m[16]); + } + } } CHECK_GL_ERROR_IF_DEBUG(); break; diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 1cc8dc06e1..bb882416dc 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -467,7 +467,7 @@ ovrMatrix4f VR_GetMatrix( VRMatrix matrix ) { float near = (float)vrConfig[VR_CONFIG_FOV_SCALE] / 200.0f; output = ovrMatrix4f_CreateProjectionFov(fov.angleLeft, fov.angleRight, fov.angleUp, fov.angleDown, near, 0.0f ); } else if ((matrix == VR_VIEW_MATRIX_LEFT_EYE) || (matrix == VR_VIEW_MATRIX_RIGHT_EYE)) { - XrPosef invView = matrix == VR_VIEW_MATRIX_LEFT_EYE ? invViewTransform[0] : invViewTransform[1]; + XrPosef invView = invViewTransform[0]; // get axis mirroring configuration float mx = vrConfig[VR_CONFIG_MIRROR_PITCH] ? -1 : 1; @@ -499,6 +499,12 @@ ovrMatrix4f VR_GetMatrix( VRMatrix matrix ) { output.M[1][3] -= hmdposition.y * (vrConfig[VR_CONFIG_MIRROR_AXIS_Y] ? -1.0f : 1.0f) * scale; output.M[2][3] -= hmdposition.z * (vrConfig[VR_CONFIG_MIRROR_AXIS_Z] ? -1.0f : 1.0f) * scale; } + if (matrix == VR_VIEW_MATRIX_RIGHT_EYE) { + float ipdScale = 1.0f; + output.M[0][3] += (invViewTransform[1].position.x - invViewTransform[0].position.x) * ipdScale; + output.M[1][3] += (invViewTransform[1].position.y - invViewTransform[0].position.y) * ipdScale; + output.M[2][3] += (invViewTransform[1].position.z - invViewTransform[0].position.z) * ipdScale; + } } else { assert(false); } diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 076edb342b..a1ae7edb61 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -408,11 +408,7 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid, bool useBu ScaleProjMatrix(leftEyeMatrix, useBufferedRendering); ScaleProjMatrix(rightEyeMatrix, useBufferedRendering); - if (IsMultiviewSupported()) { - render_->SetUniformM4x4Stereo("u_proj_lens", &u_proj_lens, leftEyeMatrix.m, rightEyeMatrix.m); - } else { - render_->SetUniformM4x4(&u_proj_lens, GetVRFBOIndex() == 0 ? leftEyeMatrix.m : rightEyeMatrix.m); - } + render_->SetUniformM4x4Stereo("u_proj_lens", &u_proj_lens, leftEyeMatrix.m, rightEyeMatrix.m); } Matrix4x4 flippedMatrix; @@ -535,11 +531,7 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid, bool useBu if (!flatScreen && !is2D) { UpdateVRView(gstate.projMatrix, leftEyeView, rightEyeView); } - if (IsMultiviewSupported()) { - render_->SetUniformM4x4Stereo("u_view", &u_view, leftEyeView, rightEyeView); - } else { - render_->SetUniformM4x4(&u_view, GetVRFBOIndex() == 0 ? leftEyeView : rightEyeView); - } + render_->SetUniformM4x4Stereo("u_view", &u_view, leftEyeView, rightEyeView); } else { SetMatrix4x3(render_, &u_view, gstate.viewMatrix); }