From 5159367b4c29713d3b3cdad83ec2718c3cc110b0 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Mon, 18 Dec 2023 15:42:17 -0800 Subject: [PATCH] fix paramsOffset and view flipping --- .../display-plugins/OpenGLDisplayPlugin.cpp | 4 +- .../gpu-gl-common/src/gpu/gl/GLBackend.cpp | 42 +++++++++---------- .../gpu-gl-common/src/gpu/gl/GLBackend.h | 2 +- libraries/gpu/src/gpu/Context.h | 2 +- .../src/ToneMapAndResampleTask.cpp | 2 +- libraries/render/src/render/ResampleTask.cpp | 2 +- tools/gpu-frame-player/src/RenderThread.cpp | 2 +- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp index 03a463c82a..47f22dfaee 100644 --- a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp +++ b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp @@ -357,7 +357,7 @@ void OpenGLDisplayPlugin::customizeContext() { auto presentThread = DependencyManager::get(); Q_ASSERT(thread() == presentThread->thread()); - getGLBackend()->setCameraCorrection(mat4(), mat4(), true); + getGLBackend()->setCameraCorrection(mat4(), mat4(), true, true); for (auto& cursorValue : _cursorsData) { auto& cursorData = cursorValue.second; @@ -701,7 +701,7 @@ void OpenGLDisplayPlugin::present(const std::shared_ptr& if (_currentFrame) { auto correction = getViewCorrection(); - getGLBackend()->setCameraCorrection(correction, _prevRenderView); + getGLBackend()->setCameraCorrection(correction, _prevRenderView, true); _prevRenderView = correction * _currentFrame->view; { withPresentThreadLock([&] { diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp index f4af949558..c0116274ee 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp @@ -387,7 +387,6 @@ void GLBackend::renderPassDraw(const Batch& batch) { case Batch::COMMAND_setModelTransform: case Batch::COMMAND_setViewTransform: case Batch::COMMAND_setProjectionTransform: - case Batch::COMMAND_setContextMirrorViewCorrection: break; case Batch::COMMAND_draw: @@ -413,10 +412,10 @@ void GLBackend::renderPassDraw(const Batch& batch) { //case Batch::COMMAND_setModelTransform: //case Batch::COMMAND_setViewTransform: //case Batch::COMMAND_setProjectionTransform: - //case Batch::COMMAND_setContextMirrorViewCorrection: case Batch::COMMAND_setProjectionJitter: case Batch::COMMAND_setViewportTransform: case Batch::COMMAND_setDepthRangeTransform: + case Batch::COMMAND_setContextMirrorViewCorrection: { PROFILE_RANGE(render_gpu_gl_detail, "transform"); CommandCall call = _commandCalls[(*command)]; @@ -625,24 +624,11 @@ void GLBackend::do_restoreContextViewCorrection(const Batch& batch, size_t param void GLBackend::do_setContextMirrorViewCorrection(const Batch& batch, size_t paramOffset) { bool prevMirrorViewCorrection = _transform._mirrorViewCorrection; - _transform._mirrorViewCorrection = batch._params[paramOffset + 1]._uint != 0; + _transform._mirrorViewCorrection = batch._params[paramOffset]._uint != 0; if (_transform._correction.correction != glm::mat4()) { - // If we were previously not flipped, take this opportunity to save our flipped and unflipped matrices. - if (!prevMirrorViewCorrection) { - _transform._unflippedCorrection = _transform._correction.correction; - quat flippedRotation = glm::quat_cast(_transform._unflippedCorrection); - flippedRotation.y *= -1.0f; - flippedRotation.z *= -1.0f; - vec3 flippedTranslation = _transform._unflippedCorrection[3]; - flippedTranslation.x *= -1.0f; - _transform._flippedCorrection = glm::translate(glm::mat4_cast(flippedRotation), flippedTranslation); - } - - if (prevMirrorViewCorrection != _transform._mirrorViewCorrection) { - setCameraCorrection(_transform._mirrorViewCorrection ? _transform._flippedCorrection : _transform._unflippedCorrection, _transform._correction.prevView); - _transform._invalidView = true; - } + setCameraCorrection(_transform._mirrorViewCorrection ? _transform._flippedCorrection : _transform._unflippedCorrection, _transform._correction.prevView, false); + _transform._invalidView = true; } } @@ -1024,15 +1010,29 @@ void GLBackend::recycle() const { _textureManagement._transferEngine->manageMemory(); } -void GLBackend::setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool reset) { +void GLBackend::setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool primary, bool reset) { auto invCorrection = glm::inverse(correction); auto invPrevView = glm::inverse(prevRenderView); _transform._correction.prevView = (reset ? Mat4() : prevRenderView); _transform._correction.prevViewInverse = (reset ? Mat4() : invPrevView); _transform._correction.correction = correction; _transform._correction.correctionInverse = invCorrection; - _pipeline._cameraCorrectionBuffer._buffer->setSubData(0, _transform._correction); - _pipeline._cameraCorrectionBuffer._buffer->flush(); + + if (!_inRenderTransferPass) { + _pipeline._cameraCorrectionBuffer._buffer->setSubData(0, _transform._correction); + _pipeline._cameraCorrectionBuffer._buffer->flush(); + } + + if (primary) { + _transform._unflippedCorrection = _transform._correction.correction; + quat flippedRotation = glm::quat_cast(_transform._unflippedCorrection); + flippedRotation.y *= -1.0f; + flippedRotation.z *= -1.0f; + vec3 flippedTranslation = _transform._unflippedCorrection[3]; + flippedTranslation.x *= -1.0f; + _transform._flippedCorrection = glm::translate(glm::mat4_cast(flippedRotation), flippedTranslation); + _transform._mirrorViewCorrection = false; + } } void GLBackend::syncProgram(const gpu::ShaderPointer& program) { diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h index fdc0bf983c..5545858877 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h @@ -121,7 +121,7 @@ public: // Shutdown rendering and persist any required resources void shutdown() override; - void setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool reset = false) override; + void setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool primary, bool reset = false) override; void render(const Batch& batch) final override; // This call synchronize the Full Backend cache with the current GLState diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index 1946f447f8..ebc81f14e9 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -66,7 +66,7 @@ public: virtual void syncProgram(const gpu::ShaderPointer& program) = 0; virtual void recycle() const = 0; virtual void downloadFramebuffer(const FramebufferPointer& srcFramebuffer, const Vec4i& region, QImage& destImage) = 0; - virtual void setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool reset = false) {} + virtual void setCameraCorrection(const Mat4& correction, const Mat4& prevRenderView, bool primary, bool reset = false) {} virtual bool supportedTextureFormat(const gpu::Element& format) = 0; diff --git a/libraries/render-utils/src/ToneMapAndResampleTask.cpp b/libraries/render-utils/src/ToneMapAndResampleTask.cpp index 6ac5142e45..36a08427cb 100644 --- a/libraries/render-utils/src/ToneMapAndResampleTask.cpp +++ b/libraries/render-utils/src/ToneMapAndResampleTask.cpp @@ -96,7 +96,7 @@ void ToneMapAndResample::run(const RenderContextPointer& renderContext, const In batch.setViewportTransform(destViewport); batch.setProjectionTransform(glm::mat4()); batch.resetViewTransform(); - bool shouldMirror = _depth % 2 == (args->_renderMode != RenderArgs::MIRROR_RENDER_MODE); + bool shouldMirror = _depth >= (args->_renderMode != RenderArgs::MIRROR_RENDER_MODE); batch.setPipeline(shouldMirror ? _mirrorPipeline : _pipeline); batch.setModelTransform(gpu::Framebuffer::evalSubregionTexcoordTransform(srcBufferSize, args->_viewport)); diff --git a/libraries/render/src/render/ResampleTask.cpp b/libraries/render/src/render/ResampleTask.cpp index 5206767bd2..af82d249cf 100644 --- a/libraries/render/src/render/ResampleTask.cpp +++ b/libraries/render/src/render/ResampleTask.cpp @@ -167,7 +167,7 @@ void UpsampleToBlitFramebuffer::run(const RenderContextPointer& renderContext, c batch.setViewportTransform(viewport); batch.setProjectionTransform(glm::mat4()); batch.resetViewTransform(); - bool shouldMirror = _depth % 2 == (args->_renderMode != RenderArgs::MIRROR_RENDER_MODE); + bool shouldMirror = _depth >= (args->_renderMode != RenderArgs::MIRROR_RENDER_MODE); batch.setPipeline(shouldMirror ? _mirrorPipeline : _pipeline); batch.setModelTransform(gpu::Framebuffer::evalSubregionTexcoordTransform(bufferSize, viewport)); diff --git a/tools/gpu-frame-player/src/RenderThread.cpp b/tools/gpu-frame-player/src/RenderThread.cpp index 0089c1577b..de39dacdea 100644 --- a/tools/gpu-frame-player/src/RenderThread.cpp +++ b/tools/gpu-frame-player/src/RenderThread.cpp @@ -122,7 +122,7 @@ void RenderThread::renderFrame(gpu::FramePointer& frame) { if (_correction != glm::mat4()) { std::unique_lock lock(_frameLock); if (_correction != glm::mat4()) { - _backend->setCameraCorrection(_correction, _activeFrame->view); + _backend->setCameraCorrection(_correction, _activeFrame->view, true); //_prevRenderView = _correction * _activeFrame->view; } }