Revert "Cache depth buffer average to improve performance"

This reverts commit 303b392439e747e3a10c3443906665bf4b88d5c5.
This commit is contained in:
Nyall Dawson 2025-02-21 11:04:35 +10:00 committed by Stefanos Natsis
parent 572417a082
commit 0ee993ffbe
2 changed files with 8 additions and 18 deletions

View File

@ -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 )
{

View File

@ -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>
double mDepthBufferNonVoidAverage;
std::unique_ptr<Qt3DRender::QCamera> mCameraBefore;