From 0ee993ffbe25895209212ddfd9d8852e0953b226 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 21 Feb 2025 11:04:35 +1000 Subject: [PATCH] Revert "Cache depth buffer average to improve performance" This reverts commit 303b392439e747e3a10c3443906665bf4b88d5c5. --- src/3d/qgscameracontroller.cpp | 21 +++++++-------------- src/3d/qgscameracontroller.h | 5 +---- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/3d/qgscameracontroller.cpp b/src/3d/qgscameracontroller.cpp index 01f636c6a0d..fe97e31d206 100644 --- a/src/3d/qgscameracontroller.cpp +++ b/src/3d/qgscameracontroller.cpp @@ -240,7 +240,7 @@ void QgsCameraController::readXml( const QDomElement &elem ) setLookingAtPoint( QgsVector3D( x, elev, y ), dist, pitch, yaw ); } -double QgsCameraController::sampleDepthBuffer( int px, int py ) +double QgsCameraController::sampleDepthBuffer( const QImage &buffer, int px, int py ) { double depth = 1; @@ -249,9 +249,9 @@ double QgsCameraController::sampleDepthBuffer( int px, int py ) { for ( int y = py - 3; y <= py + 3; ++y ) { - if ( mDepthBufferImage.valid( x, y ) ) + if ( buffer.valid( x, y ) ) { - depth = std::min( depth, Qgs3DUtils::decodeDepth( mDepthBufferImage.pixel( x, y ) ) ); + depth = std::min( depth, Qgs3DUtils::decodeDepth( buffer.pixel( x, y ) ) ); } } } @@ -259,18 +259,14 @@ double QgsCameraController::sampleDepthBuffer( int px, int py ) if ( depth < 1 ) return depth; - // Cache the computed depth, since averaging over all pixels can be expensive - if ( mDepthBufferNonVoidAverage != -1 ) - return mDepthBufferNonVoidAverage; - // Returns the average of depth values that are not 1 (void area) depth = 0; int samplesCount = 0; - for ( int x = 0; x < mDepthBufferImage.width(); ++x ) + for ( int x = 0; x < buffer.width(); ++x ) { - for ( int y = 0; y < mDepthBufferImage.height(); ++y ) + for ( int y = 0; y < buffer.height(); ++y ) { - double d = Qgs3DUtils::decodeDepth( mDepthBufferImage.pixel( x, y ) ); + double d = Qgs3DUtils::decodeDepth( buffer.pixel( x, y ) ); if ( d < 1 ) { depth += d; @@ -285,8 +281,6 @@ double QgsCameraController::sampleDepthBuffer( int px, int py ) else depth /= samplesCount; - mDepthBufferNonVoidAverage = depth; - return depth; } @@ -325,7 +319,7 @@ void QgsCameraController::onPositionChanged( Qt3DInput::QMouseEvent *mouse ) bool QgsCameraController::screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *mCameraBefore, double &depth, QVector3D &worldPosition ) { - depth = sampleDepthBuffer( position.x(), position.y() ); + depth = sampleDepthBuffer( mDepthBufferImage, position.x(), position.y() ); if ( !std::isfinite( depth ) ) { QgsDebugMsgLevel( QStringLiteral( "screenPointToWorldPos: depth is NaN or Inf. This should not happen." ), 2 ); @@ -1032,7 +1026,6 @@ void QgsCameraController::depthBufferCaptured( const QImage &depthImage ) { mDepthBufferImage = depthImage; mDepthBufferIsReady = true; - mDepthBufferNonVoidAverage = -1; if ( mCurrentOperation == MouseOperation::ZoomWheel ) { diff --git a/src/3d/qgscameracontroller.h b/src/3d/qgscameracontroller.h index bd5c4466dfc..08830805c12 100644 --- a/src/3d/qgscameracontroller.h +++ b/src/3d/qgscameracontroller.h @@ -326,7 +326,7 @@ class _3D_EXPORT QgsCameraController : public QObject * Returns the minimum depth value in the square [px - 3, px + 3] * [py - 3, py + 3] * If the value is 1, the average depth of all non void pixels is returned instead. */ - double sampleDepthBuffer( int px, int py ); + double sampleDepthBuffer( const QImage &buffer, int px, int py ); #ifndef SIP_RUN //! Converts screen point to world position @@ -350,9 +350,6 @@ class _3D_EXPORT QgsCameraController : public QObject bool mDepthBufferIsReady = false; QImage mDepthBufferImage; - // -1 when unset - // TODO: Change to std::optional - double mDepthBufferNonVoidAverage; std::unique_ptr mCameraBefore;