35#include <Qt3DCore/QAttribute>
36#include <Qt3DCore/QBuffer>
37#include <Qt3DCore/QGeometry>
38#include <Qt3DRender/QAbstractTexture>
39#include <Qt3DRender/QBlendEquation>
40#include <Qt3DRender/QBlendEquationArguments>
41#include <Qt3DRender/QBlitFramebuffer>
42#include <Qt3DRender/QColorMask>
43#include <Qt3DRender/QGeometryRenderer>
44#include <Qt3DRender/QGraphicsApiFilter>
45#include <Qt3DRender/QNoDepthMask>
46#include <Qt3DRender/QNoDraw>
47#include <Qt3DRender/QSortPolicy>
48#include <Qt3DRender/QTechnique>
51#include "tracy/Tracy.hpp"
54#include "moc_qgsframegraph.cpp"
67void QgsFrameGraph::constructForwardRenderPass()
72void QgsFrameGraph::constructHighlightsPass()
77void QgsFrameGraph::constructShadowRenderPass()
82void QgsFrameGraph::constructOverlayTexturePass( Qt3DRender::QFrameGraphNode *topNode )
87void QgsFrameGraph::constructPostprocessingPass( Qt3DRender::QFrameGraphNode *topNode )
90 QgsPostprocessingRenderView *pprv =
new QgsPostprocessingRenderView(
sPostprocRenderView,
this, mSize, mRootEntity );
94void QgsFrameGraph::updateThumbnailTextureSize()
98 const int width = ( mSize.width() / 10 ) & ~3;
99 const int height = ( mSize.height() / 10 ) & ~3;
100 mThumbnailTexture->setSize( width, height );
103void QgsFrameGraph::constructThumbnailCapturePass()
111 mThumbnailTexture =
new Qt3DRender::QTexture2D(
this );
112 mThumbnailTexture->setFormat( Qt3DRender::QAbstractTexture::RGBA8_UNorm );
113 updateThumbnailTextureSize();
115 Qt3DRender::QRenderTargetOutput *renderTargetOutput =
new Qt3DRender::QRenderTargetOutput(
this );
116 renderTargetOutput->setTexture( mThumbnailTexture );
118 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget(
this );
119 renderTarget->addOutput( renderTargetOutput );
122 Qt3DRender::QBlitFramebuffer *blit =
new Qt3DRender::QBlitFramebuffer( mMainViewPort );
123 blit->setObjectName(
"Framebuffer copy for thumbnail" );
125 blit->setDestination( renderTarget );
126 blit->setSourceRect( QRectF( 0, 0, 1, 1 ) );
127 blit->setDestinationRect( QRectF( 0, 0, 1, 1 ) );
128 blit->setInterpolationMethod( Qt3DRender::QBlitFramebuffer::Linear );
130 Qt3DRender::QRenderTargetSelector *targetSelector =
new Qt3DRender::QRenderTargetSelector( mMainViewPort );
131 targetSelector->setObjectName(
"Thumbnail capture target selector" );
132 targetSelector->setTarget( renderTarget );
134 mThumbnailCapture =
new Qt3DRender::QRenderCapture( targetSelector );
135 mThumbnailCapture->setObjectName(
"Thumbnail capture" );
140 Qt3DRender::QRenderCaptureReply *reply1 = mThumbnailCapture->requestCapture();
141 Qt3DRender::QRenderCaptureReply *reply2 = mThumbnailCapture->requestCapture();
142 for ( Qt3DRender::QRenderCaptureReply *reply : { reply1, reply2 } )
143 connect( reply, &Qt3DRender::QRenderCaptureReply::completed,
this, [
this, reply]() { onThumbnailCaptureCompleted( reply ); } );
146void QgsFrameGraph::onThumbnailCaptureCompleted( Qt3DRender::QRenderCaptureReply *reply )
148 reply->deleteLater();
152 QImage rgbaImage = reply->image().convertToFormat( QImage::Format_RGBA8888 );
156 rgbaImage.constBits(),
157 static_cast<uint16_t
>( rgbaImage.width() ),
158 static_cast<uint16_t
>( rgbaImage.height() ),
165 Qt3DRender::QRenderCaptureReply *nextReply = mThumbnailCapture->requestCapture();
166 connect( nextReply, &Qt3DRender::QRenderCaptureReply::completed,
this, [
this, nextReply]() { onThumbnailCaptureCompleted( nextReply ); } );
169void QgsFrameGraph::constructAmbientOcclusionRenderPass()
173 QgsAmbientOcclusionRenderView *aorv =
new QgsAmbientOcclusionRenderView(
sAmbientOcclusionRenderView, mMainCamera, mSize, forwardDepthTexture, mRootEntity );
177void QgsFrameGraph::constructBloomRenderPass()
181 QgsBloomRenderView *rv =
new QgsBloomRenderView(
sBloomRenderView, forwardColorTexture, mSize, mRootEntity );
185void QgsFrameGraph::constructRubberBandsPass( Qt3DRender::QFrameGraphNode *topNode )
192void QgsFrameGraph::constructDepthRenderPass()
196 QgsDepthRenderView *rv =
new QgsDepthRenderView(
sDepthRenderView, mSize, forwardDepthTexture, mRootEntity );
207 for ( Qt3DRender::QParameter *param : parameters )
209 mGlobalParamsStorage->addParameter( param );
255 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
257 QObject *surfaceObj =
dynamic_cast<QObject *
>( surface );
258 Q_ASSERT( surfaceObj );
260 mRenderSurfaceSelector->setSurface( surfaceObj );
261 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
263 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
264 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
266 mGlobalParamsStorage =
new Qt3DRender::QRenderPassFilter( mMainViewPort );
267 mGlobalParamsStorage->setObjectName(
"GlobalParametersStore" );
271 constructShadowRenderPass();
274 constructForwardRenderPass();
277 constructHighlightsPass();
280 constructRubberBandsPass( mGlobalParamsStorage );
282 mMsaaBlitNode =
new Qt3DRender::QBlitFramebuffer( mGlobalParamsStorage );
283 mMsaaBlitNode->setObjectName(
"MsaaBlitFramebuffer" );
284 mMsaaBlitNode->setEnabled(
false );
285 new Qt3DRender::QNoDraw( mMsaaBlitNode );
287 mMsaaDepthBlitNode =
new Qt3DRender::QBlitFramebuffer( mGlobalParamsStorage );
288 mMsaaDepthBlitNode->setObjectName(
"MsaaDepthBlitFramebuffer" );
289 mMsaaDepthBlitNode->setEnabled(
false );
290 new Qt3DRender::QNoDraw( mMsaaDepthBlitNode );
293 constructDepthRenderPass();
296 constructAmbientOcclusionRenderPass();
298 constructBloomRenderPass();
301 constructPostprocessingPass( mGlobalParamsStorage );
304 constructThumbnailCapturePass();
309 Qt3DRender::QRenderTargetSelector *targetSelector =
new Qt3DRender::QRenderTargetSelector( mGlobalParamsStorage );
311 mMsaaClearBuffers =
new Qt3DRender::QClearBuffers( targetSelector );
312 mMsaaClearBuffers->setEnabled(
false );
313 mMsaaClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
314 new Qt3DRender::QNoDraw( mMsaaClearBuffers );
319 if ( mRenderViewMap.find( name ) != mRenderViewMap.end() )
321 mRenderViewMap[name]->topGraphNode()->setParent( ( QNode * )
nullptr );
322 mRenderViewMap.erase( name );
329 if ( mRenderViewMap.find( name ) == mRenderViewMap.end() )
331 mRenderViewMap[name] = std::move(
renderView );
332 mRenderViewMap[name]->topGraphNode()->setParent( topNode ? topNode : mGlobalParamsStorage );
333 mRenderViewMap[name]->updateWindowResize( mSize.width(), mSize.height() );
344 if ( mRenderViewMap[name] )
346 mRenderViewMap[name]->setEnabled( enable );
352 if ( mRenderViewMap.find( name ) != mRenderViewMap.end() )
354 return mRenderViewMap[name].get();
361 return mRenderViewMap[name] !=
nullptr && mRenderViewMap[name]->
isEnabled();
401 const QList<QgsLightSource *> lightSources = mapSettings.
lightSources();
404 const QString lightSourceId = shadowSettings.
lightSource();
406 int globalLightIndex = 0;
409 int backupLightSourceIndex = 0;
410 for (
int i = 0; !light && i < lightSources.size(); i++ )
412 if ( !backupLightSource )
416 backupLightSource = directionalLight;
417 backupLightSourceIndex = i;
421 backupLightSource = sunLight;
422 backupLightSourceIndex = i;
426 if ( lightSources[i]->
id() == lightSourceId )
428 light = lightSources[i];
429 globalLightIndex = i;
437 light = backupLightSource;
438 globalLightIndex = backupLightSourceIndex;
446 lightDirection = directionalLight->direction();
450 lightDirection = sunLight->direction( mapSettings );
480 if ( mDepthTextureDebugging )
487 delete mDepthTextureDebugging;
488 mDepthTextureDebugging =
nullptr;
495 QObject *top = mRenderSurfaceSelector;
496 while ( top->parent() &&
dynamic_cast<Qt3DRender::QFrameGraphNode *
>( top->parent() ) )
500 context.
lowestId = mMainCamera->id().id();
503 return strList.join(
"\n" ) + QString(
"\n" );
509 return strList.join(
"\n" ) + QString(
"\n" );
525 for (
auto it = mRenderViewMap.begin(); it != mRenderViewMap.end(); ++it )
531 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
533 mMsaaBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
534 mMsaaBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
535 mMsaaDepthBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
536 mMsaaDepthBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
538 if ( mThumbnailTexture )
539 updateThumbnailTextureSize();
559 mMsaaEnabled = enabled;
561 if ( !enabled && mMsaaBlitConfigured )
563 mMsaaBlitNode->setSource(
nullptr );
564 mMsaaBlitNode->setDestination(
nullptr );
565 mMsaaDepthBlitNode->setSource(
nullptr );
566 mMsaaDepthBlitNode->setDestination(
nullptr );
567 mMsaaBlitConfigured =
false;
568 mMsaaClearBuffers->setEnabled(
false );
573 if ( enabled && !mMsaaBlitConfigured )
575 mMsaaBlitConfigured =
true;
578 mMsaaBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
579 mMsaaBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
580 mMsaaBlitNode->setSourceAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
581 mMsaaBlitNode->setDestinationAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
582 mMsaaBlitNode->setInterpolationMethod( Qt3DRender::QBlitFramebuffer::Nearest );
586 mMsaaDepthBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
587 mMsaaDepthBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
588 mMsaaDepthBlitNode->setSourceAttachmentPoint( Qt3DRender::QRenderTargetOutput::DepthStencil );
589 mMsaaDepthBlitNode->setDestinationAttachmentPoint( Qt3DRender::QRenderTargetOutput::DepthStencil );
590 mMsaaDepthBlitNode->setInterpolationMethod( Qt3DRender::QBlitFramebuffer::Nearest );
592 mMsaaClearBuffers->setEnabled(
true );
598 mMsaaBlitNode->setEnabled( enabled );
599 mMsaaDepthBlitNode->setEnabled( enabled );
Qt::Corner debugDepthMapCorner() const
Returns the corner where the shadow map preview is displayed.
bool debugDepthMapEnabled() const
Returns whether the shadow map debugging is enabled.
QgsShadowSettings shadowSettings() const
Returns the current configuration of shadows.
QList< QgsLightSource * > lightSources() const
Returns list of directional light sources defined in the scene.
double debugDepthMapSize() const
Returns the size of the shadow map preview.
bool is2DMapOverlayEnabled() const
Returns whether 2D map overlay is enabled.
bool eyeDomeLightingEnabled() const
Returns whether eye dome lighting is used.
Base class for 3D render view.
virtual void setEnabled(bool enable)
Enable or disable via enable the render view sub tree.
virtual bool isEnabled() const
Returns true if render view is enabled.
virtual void updateWindowResize(int width, int height)
Called when 3D window is resized.
Container class that holds different objects related to ambient occlusion rendering.
void setRadius(float radius)
Delegates to QgsAmbientOcclusionRenderEntity::setRadius.
void setEnabled(bool enable) override
Enable or disable via enable the render view sub tree.
void setIntensity(float intensity)
Delegates to QgsAmbientOcclusionRenderEntity::setIntensity.
void setThreshold(float threshold)
Delegates to QgsAmbientOcclusionRenderEntity::setThreshold.
Contains the configuration of ambient occlusion rendering.
float radius() const
Returns the radius parameter of the ambient occlusion effect.
bool isEnabled() const
Returns whether ambient occlusion effect is enabled.
float intensity() const
Returns the shading factor of the ambient occlusion effect.
float threshold() const
Returns at what amount of occlusion the effect will kick in.
Container class that holds different objects related to bloom rendering.
Contains the configuration of the lighting "bloom" effect.
double radius() const
Returns the filter radius for the bloom.
bool isEnabled() const
Returns whether the bloom effect is enabled.
Contains the configuration of the scene's color grading settings, such as exposure and tone mapping.
Container class that holds different objects related to depth rendering.
Qt3DRender::QRenderCapture * renderCapture()
Returns the render capture object used to take an image of the depth buffer of the scene.
Definition of a directional light in a 3D map scene.
Container class that holds different objects related to forward rendering.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color).
Qt3DRender::QTexture2D * colorTexture() const
Returns forward color texture.
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
void setMsaaEnabled(bool enabled)
Sets whether multisample anti-aliasing (MSAA) is enabled.
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
Qt3DRender::QTexture2D * depthTexture() const
Returns forward depth texture.
void addClipPlanes(int nrClipPlanes)
Setups nrClipPlanes clip planes in the forward pass to enable OpenGL clipping.
void removeClipPlanes()
Disables OpenGL clipping.
Qt3DRender::QRenderTarget * msaaRenderTarget() const
Returns the multisampled render target used as blit source when MSAA is enabled.
Qt3DRender::QRenderTarget * regularRenderTarget() const
Returns the regular (single-sample) render target used as blit destination and postprocessing input.
static QStringList dumpFrameGraph(const Qt3DCore::QNode *node, FgDumpContext context)
Returns a tree view of the frame graph starting from node. The object ids will be given relatively to...
static QStringList dumpSceneGraph(const Qt3DCore::QNode *node, FgDumpContext context)
Returns a tree view of the scene graph starting from node. The object ids will be given relatively to...
void addGlobalParameters(const QList< Qt3DRender::QParameter * > ¶meters)
Adds additional global parameters to the graph.
void setMsaaEnabled(bool enabled)
Sets whether multisample anti-aliasing (MSAA) is enabled.
void updateAmbientOcclusionSettings(const QgsAmbientOcclusionSettings &settings)
Updates settings for ambient occlusion.
void updateEyeDomeSettings(const Qgs3DMapSettings &settings)
Updates settings for eye dome lighting.
void updateBloomSettings(const QgsBloomSettings &settings)
Updates settings for the bloom lighting effect.
bool isRenderViewEnabled(const QString &name)
Returns true if the render view named name is found and enabled.
void setRenderViewEnabled(const QString &name, bool enable)
Enables or disables the render view named name according to enable.
void addClipPlanes(int nrClipPlanes)
Setups nrClipPlanes clip planes in the forward pass to enable OpenGL clipping.
void updateColorGradingSettings(const QgsColorGradingSettings &settings)
Updates settings for color grading.
static const QString sPostprocRenderView
Postprocessing render view name.
void unregisterRenderView(const QString &name)
Unregisters the render view named name, if any.
bool registerRenderView(std::unique_ptr< QgsAbstractRenderView > renderView, const QString &name, Qt3DRender::QFrameGraphNode *topNode=nullptr)
Registers a new the render view renderView with name name.
QString dumpFrameGraph() const
Dumps frame graph as string.
QgsPostprocessingRenderView & postprocessingRenderView()
Returns post processing renderview.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
QgsAmbientOcclusionRenderView & ambientOcclusionRenderView()
Returns ambient occlusion renderview.
Qt3DRender::QRenderCapture * depthRenderCapture()
Returns the render capture object used to take an image of the depth buffer of the scene.
QgsAbstractRenderView * renderView(const QString &name)
Returns the render view named name, if any.
void removeClipPlanes()
Disables OpenGL clipping.
QgsRubberBandRenderView & rubberBandRenderView()
Returns rubber band renderview.
static const QString sOverlayRenderView
QgsDepthRenderView & depthRenderView()
Returns depth renderview.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color).
static const QString sDepthRenderView
static const QString sForwardRenderView
static const QString sBloomRenderView
static const QString sShadowRenderView
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
void updateShadowSettings(const Qgs3DMapSettings &mapSettings)
Updates shadow bias, light and texture size according to shadowSettings and lightSources.
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
QgsHighlightsRenderView & highlightsRenderView()
Returns the highlights renderview, used for rendering highlight overlays of identified features.
static const QString sAxiS3DRenderView
void updateDebugDepthMapSettings(const Qgs3DMapSettings &settings)
Updates settings for depth debug map.
QgsForwardRenderView & forwardRenderView()
Returns forward renderview.
static const QString sAmbientOcclusionRenderView
Ambient occlusion render view name.
QgsOverlayTextureRenderView & overlayTextureRenderView()
Returns overlay texture renderview.
QString dumpSceneGraph() const
Dumps scene graph as string.
QgsShadowRenderView & shadowRenderView()
Returns shadow renderview.
void setSize(QSize s)
Sets the size of the buffers used for rendering.
QgsBloomRenderView & bloomRenderView()
Returns the bloom render view.
static const QString sHighlightsRenderView
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
QgsFrameGraph(QSurface *surface, QSize s, Qt3DRender::QCamera *mainCamera, Qt3DCore::QEntity *root)
Constructor.
Qt3DRender::QRenderCapture * renderCapture()
Returns the render capture object used to take an image of the scene.
static const QString sRubberRenderView
Container class that holds different objects related to highlighting identified features.
void setRenderTarget(Qt3DRender::QRenderTarget *target)
Switches the render target (called when toggling MSAA on/off).
Base class for light sources in 3d scenes.
An entity responsible for rendering an overlay texture in 3D view.
Simple render view to preview overlay textures in 3D view.
Qt3DRender::QLayer * overlayLayer() const
Returns layer in which entities must be added in the in order to be processed by this renderview.
void updateShadowSettings(const QgsShadowSettings &shadowSettings, const QgsVector3D &lightDir, int size, int globalLightIndex)
Sets shadow rendering to use a directional light.
void setAmbientOcclusionEnabled(bool enabled)
Sets whether screen space ambient occlusion is enabled.
void setShadowRenderingEnabled(bool enabled)
Sets whether shadow rendering is enabled.
void setBloomEnabled(bool enabled)
Sets whether physically based bloom is enabled.
void updateEyeDomeSettings(const Qgs3DMapSettings &settings)
Updates eye dome lighting settings from settings.
void updateBloomSettings(const QgsBloomSettings &settings)
Sets bloom rendering to use a directional light.
void setEyeDomeLightingEnabled(bool enabled)
Sets whether eye dome lighting is enabled.
void updateColorGradingSettings(const QgsColorGradingSettings &settings)
Updates settings for color grading.
Container class that holds different objects related to postprocessing rendering.
Qt3DRender::QRenderCapture * renderCapture() const
Returns the render capture object used to take an image of the postprocessing buffer of the scene.
QgsPostprocessingEntity * entity() const
Returns the QT3D entity used to do the rendering.
void setOffScreenRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
QgsOverlayTextureRenderView * overlayTextureRenderView() const
Returns overlay texture render view.
Container class that holds different objects related to rubberband rendering.
Qt3DRender::QRenderTargetSelector * renderTargetSelector() const
Returns rubberband tard selector.
Container class that holds different objects related to shadow rendering.
void setMapSize(int width, int height)
Update shadow depth texture size.
void setEnabled(bool enable) override
Enable or disable via enable the renderview sub tree.
Contains configuration for rendering shadows.
bool renderShadows() const
Returns whether shadow rendering is enabled.
static int qualityToMapResolution(Qgis::ShadowQuality quality)
Returns the shadow map resolution corresponding to the specified shadow quality.
QString lightSource() const
Returns the ID of the light source casting shadows.
Qgis::ShadowQuality shadowQuality() const
Returns the quality of the shadow map texture used to generate the shadows.
Definition of a sun light in a 3D map scene.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...