QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsframegraphutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfgutils.cpp
3 --------------------------------------
4 Date : August 2024
5 Copyright : (C) 2024 by Mike Krus / Benoit De Mezzo
6 Email : mike dot krus at kdab dot com / benoit dot de dot mezzo at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsframegraphutils.h"
17
18#include <QMetaEnum>
19#include <QString>
20#include <Qt3DExtras/QTextureMaterial>
21#include <Qt3DRender/QAbstractTexture>
22#include <Qt3DRender/QBlendEquation>
23#include <Qt3DRender/QBlendEquationArguments>
24#include <Qt3DRender/QColorMask>
25#include <Qt3DRender/QEffect>
26#include <Qt3DRender/QGeometryRenderer>
27#include <Qt3DRender/QGraphicsApiFilter>
28#include <Qt3DRender/QNoDepthMask>
29#include <Qt3DRender/QNoDraw>
30#include <Qt3DRender/QPointSize>
31#include <Qt3DRender/QSeamlessCubemap>
32#include <Qt3DRender/QSortPolicy>
33#include <Qt3DRender/QTechnique>
34
35using namespace Qt::StringLiterals;
36
37QStringList QgsFrameGraphUtils::dumpSceneGraph( const Qt3DCore::QNode *node, FgDumpContext context )
38{
39 return dumpSG( context, node );
40}
41
42QStringList QgsFrameGraphUtils::dumpFrameGraph( const Qt3DCore::QNode *node, FgDumpContext context )
43{
44 return dumpFG( context, node );
45}
46
47QString QgsFrameGraphUtils::formatIdName( FgDumpContext context, quint64 id, const QString &name )
48{
49 QString fixedName = name.isEmpty() ? "<no_name>"_L1 : name;
50 return "{%1/%2}"_L1.arg( QString::number( id - context.lowestId ) ).arg( fixedName );
51}
52
53QString QgsFrameGraphUtils::formatIdName( FgDumpContext context, const Qt3DRender::QAbstractTexture *texture )
54{
55 QString fixedName = texture->objectName().isEmpty() ? "<no_name>"_L1 : texture->objectName();
56 return "{%1[%2]/%3"_L1.arg( QString::number( texture->id().id() - context.lowestId ), QString( QMetaEnum::fromType<Qt3DRender::QAbstractTexture::TextureFormat>().valueToKey( texture->format() ) ), fixedName );
57}
58
59QString QgsFrameGraphUtils::formatNode( FgDumpContext context, const Qt3DCore::QNode *node )
60{
61 QString res = "(%1%2)"_L1.arg( QLatin1String( node->metaObject()->className() ) ).arg( formatIdName( context, node->id().id(), node->objectName() ) );
62 if ( !node->isEnabled() )
63 res += " [D]"_L1;
64 return res;
65}
66
67QString QgsFrameGraphUtils::formatList( const QStringList &lst )
68{
69 return QString( "[ %1 ]"_L1 ).arg( lst.join( ", "_L1 ) );
70}
71
72QString QgsFrameGraphUtils::formatLongList( const QStringList &lst, int level )
73{
74 QString out = formatList( lst );
75 if ( out.size() < 200 )
76 return out;
77
78 out = QString( "[\n"_L1 );
79 for ( QString item : lst )
80 {
81 item = QString( "-> %1\n" ).arg( item );
82 out += item.rightJustified( item.length() + ( 1 + level ) * 2, ' ' );
83 }
84 QString end( "]"_L1 );
85 return out + end.rightJustified( end.length() + ( 1 + level ) * 2, ' ' );
86}
87
88QString QgsFrameGraphUtils::formatField( const QString &name, const QString &value )
89{
90 if ( value == "<no_value>" )
91 return QString( "(%1)"_L1 ).arg( name );
92 return QString( "(%1:%2)"_L1 ).arg( name, value );
93}
94
95QString QgsFrameGraphUtils::dumpSGEntity( FgDumpContext context, const Qt3DCore::QEntity *node, int level )
96{
97 auto extractTextureParam = []( FgDumpContext context, const QVector<Qt3DRender::QParameter *> &params, QStringList &fl ) {
98 for ( const auto *param : params )
99 {
100 if ( strstr( param->value().typeName(), "QAbstractTexture*" ) )
101 {
102 const Qt3DRender::QAbstractTexture *tex = param->value().value<Qt3DRender::QAbstractTexture *>();
103 fl += formatField( param->name(), formatIdName( context, tex ) );
104 }
105 }
106 };
107
108
109 QString res = formatNode( context, node );
110 const auto &components = node->components();
111 if ( !components.isEmpty() )
112 {
113 QStringList componentNames;
114 for ( const auto &comp : components )
115 {
116 QString res = formatNode( context, comp );
117 QStringList fl;
118 if ( const auto *textMat = qobject_cast<const Qt3DExtras::QTextureMaterial *>( comp ) )
119 {
120 if ( textMat->texture() )
121 {
122 const auto texImages = textMat->texture()->textureImages();
123 for ( const auto *texImg : texImages )
124 {
125 fl += formatField( texImg->metaObject()->className(), formatIdName( context, texImg->id().id(), texImg->objectName() ) );
126 }
127 }
128 }
129 if ( const auto *material = qobject_cast<const Qt3DRender::QMaterial *>( comp ) )
130 {
131 if ( material->effect() )
132 {
133 const auto techniques = material->effect()->techniques();
134 for ( const auto *tech : techniques )
135 {
136 extractTextureParam( context, tech->parameters(), fl );
137 const auto passes = tech->renderPasses();
138 for ( const auto *pass : passes )
139 {
140 extractTextureParam( context, pass->parameters(), fl );
141 }
142 }
143 extractTextureParam( context, material->effect()->parameters(), fl );
144 }
145 extractTextureParam( context, material->parameters(), fl );
146 if ( !fl.empty() )
147 res += formatList( fl );
148 }
149
150 componentNames << res;
151 }
152 res += formatLongList( componentNames, level );
153 }
154
155 return res;
156}
157
158QStringList QgsFrameGraphUtils::dumpSG( FgDumpContext context, const Qt3DCore::QNode *node, int level )
159{
160 QStringList reply;
161 const auto *entity = qobject_cast<const Qt3DCore::QEntity *>( node );
162 if ( entity )
163 {
164 QString res = dumpSGEntity( context, entity, level );
165 reply += res.rightJustified( res.length() + level * 2, ' ' );
166 level++;
167 }
168
169 const auto children = node->childNodes();
170 for ( auto *child : children )
171 reply += dumpSG( context, child, level );
172
173 return reply;
174}
175
176QString QgsFrameGraphUtils::dumpFGNode( FgDumpContext context, const Qt3DRender::QFrameGraphNode *node )
177{
178 QString res = formatNode( context, node );
179
180 if ( const auto *lf = qobject_cast<const Qt3DRender::QLayerFilter *>( node ) )
181 {
182 QStringList sl;
183 const auto layers = lf->layers();
184 for ( auto layer : layers )
185 {
186 sl += formatIdName( context, layer->id().id(), layer->objectName() );
187 }
188
189 QStringList fl;
190 fl += formatField( QMetaEnum::fromType<Qt3DRender::QLayerFilter::FilterMode>().valueToKey( lf->filterMode() ), formatList( sl ) );
191 res += QString( " %1" ).arg( formatList( fl ) );
192 }
193
194 else if ( const auto *cs = qobject_cast<const Qt3DRender::QCameraSelector *>( node ) )
195 {
196 QStringList fl;
197 fl += formatField( cs->camera()->metaObject()->className(), formatIdName( context, cs->camera()->id().id(), cs->camera()->objectName() ) );
198 res += QString( " %1" ).arg( formatList( fl ) );
199 }
200
201 else if ( const auto *rss = qobject_cast<const Qt3DRender::QRenderStateSet *>( node ) )
202 {
203 QStringList sl;
204 const auto renderStates = rss->renderStates();
205 for ( auto rs : renderStates )
206 {
207 if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QCullFace *>( rs ) )
208 {
209 sl += formatField( "QCullFace", QMetaEnum::fromType<Qt3DRender::QCullFace::CullingMode>().valueToKey( rs_cast->mode() ) );
210 }
211 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QBlendEquation *>( rs ) )
212 {
213 sl += formatField( "QBlendEquation", QMetaEnum::fromType<Qt3DRender::QBlendEquation::BlendFunction>().valueToKey( rs_cast->blendFunction() ) );
214 }
215 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QBlendEquationArguments *>( rs ) )
216 {
217 QStringList fl;
218 fl += formatField( "sourceRgb", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->sourceRgb() ) );
219 fl += formatField( "destinationRgb", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->destinationRgb() ) );
220 fl += formatField( "sourceAlpha", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->sourceAlpha() ) );
221 fl += formatField( "destinationAlpha", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->destinationAlpha() ) );
222 fl += formatField( "bufferIndex", QString::number( rs_cast->bufferIndex() ) );
223
224 sl += formatField( "QBlendEquationArguments", formatList( fl ) );
225 }
226 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QColorMask *>( rs ) )
227 {
228 QStringList fl;
229 fl += formatField( "red", ( rs_cast->isRedMasked() ? "true"_L1 : "false"_L1 ) );
230 fl += formatField( "green", ( rs_cast->isGreenMasked() ? "true"_L1 : "false"_L1 ) );
231 fl += formatField( "blue", ( rs_cast->isBlueMasked() ? "true"_L1 : "false"_L1 ) );
232 fl += formatField( "alpha", ( rs_cast->isAlphaMasked() ? "true"_L1 : "false"_L1 ) );
233 sl += formatField( "QColorMask", formatList( fl ) );
234 }
235 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QDepthTest *>( rs ) )
236 {
237 sl += formatField( "QDepthTest", QMetaEnum::fromType<Qt3DRender::QDepthTest::DepthFunction>().valueToKey( rs_cast->depthFunction() ) );
238 }
239 else if ( qobject_cast<const Qt3DRender::QNoDepthMask *>( rs ) )
240 {
241 sl += formatField( "QNoDepthMask", "<no_value>" );
242 }
243 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QPointSize *>( rs ) )
244 {
245 QStringList fl;
246 fl += formatField( "sizeMode", QMetaEnum::fromType<Qt3DRender::QPointSize::SizeMode>().valueToKey( rs_cast->sizeMode() ) );
247 fl += formatField( "value", QString::number( rs_cast->value() ) );
248 sl += formatField( "QPointSize", formatList( fl ) );
249 }
250 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QPolygonOffset *>( rs ) )
251 {
252 QStringList fl;
253 fl += formatField( "scaleFactor", QString::number( rs_cast->scaleFactor() ) );
254 fl += formatField( "depthSteps", QString::number( rs_cast->depthSteps() ) );
255 sl += formatField( "QPolygonOffset", formatList( fl ) );
256 }
257 else if ( qobject_cast<const Qt3DRender::QSeamlessCubemap *>( rs ) )
258 {
259 sl += formatField( "QSeamlessCubemap", "<no_value>" );
260 }
261 }
262 res += QString( " %1" ).arg( formatList( sl ) );
263 }
264
265 else if ( const auto *rs = qobject_cast<const Qt3DRender::QRenderTargetSelector *>( node ) )
266 {
267 if ( rs->target() )
268 {
269 QStringList sl;
270 const auto outputs = rs->target()->outputs();
271 for ( auto output : outputs )
272 {
273 sl += formatField( QMetaEnum::fromType<Qt3DRender::QRenderTargetOutput::AttachmentPoint>().valueToKey( output->attachmentPoint() ), formatIdName( context, output->texture() ) );
274 }
275 QStringList fl;
276 fl += formatField( "outputs"_L1, formatList( sl ) );
277 res += QString( " %1" ).arg( formatList( fl ) );
278 }
279 }
280 // if (!n->isEnabled())
281 // res += " [D]"_L1;
282 return res;
283}
284
285QStringList QgsFrameGraphUtils::dumpFG( FgDumpContext context, const Qt3DCore::QNode *node, int level )
286{
287 QStringList reply;
288
289 const Qt3DRender::QFrameGraphNode *fgNode = qobject_cast<const Qt3DRender::QFrameGraphNode *>( node );
290 if ( fgNode )
291 {
292 QString res = dumpFGNode( context, fgNode );
293 reply += res.rightJustified( res.length() + level * 2, ' ' );
294 }
295
296 const auto children = node->childNodes();
297 const int inc = fgNode ? 1 : 0;
298 for ( auto *child : children )
299 {
300 auto *childFGNode = qobject_cast<Qt3DCore::QNode *>( child );
301 if ( childFGNode )
302 reply += dumpFG( context, childFGNode, level + inc );
303 }
304
305 return reply;
306}
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...