QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsfgutils.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 "qgsfgutils.h"
17
18#include <QMetaEnum>
19#include <Qt3DRender/QGeometryRenderer>
20#include <Qt3DRender/QTechnique>
21#include <Qt3DRender/QGraphicsApiFilter>
22#include <Qt3DRender/QBlendEquation>
23#include <Qt3DRender/QColorMask>
24#include <Qt3DRender/QSortPolicy>
25#include <Qt3DRender/QPointSize>
26#include <Qt3DRender/QSeamlessCubemap>
27#include <Qt3DRender/QNoDepthMask>
28#include <Qt3DRender/QBlendEquationArguments>
29#include <Qt3DExtras/QTextureMaterial>
30#include <Qt3DRender/QAbstractTexture>
31#include <Qt3DRender/QNoDraw>
32#include <Qt3DRender/QEffect>
33
34QStringList QgsFgUtils::dumpSceneGraph( const Qt3DCore::QNode *node, FgDumpContext context )
35{
36 return dumpSG( context, node );
37}
38
39QStringList QgsFgUtils::dumpFrameGraph( const Qt3DCore::QNode *node, FgDumpContext context )
40{
41 return dumpFG( context, node );
42}
43
44QString QgsFgUtils::formatIdName( FgDumpContext context, quint64 id, const QString &name )
45{
46 QString fixedName = name.isEmpty() ? QLatin1String( "<no_name>" ) : name;
47 return QLatin1String( "{%1/%2}" ).arg( QString::number( id - context.lowestId ) ).arg( fixedName );
48}
49
50QString QgsFgUtils::formatIdName( FgDumpContext context, const Qt3DRender::QAbstractTexture *texture )
51{
52 QString fixedName = texture->objectName().isEmpty() ? QLatin1String( "<no_name>" ) : texture->objectName();
53 return QLatin1String( "{%1[%2]/%3" )
54 .arg( QString::number( texture->id().id() - context.lowestId ), QString( QMetaEnum::fromType<Qt3DRender::QAbstractTexture::TextureFormat>().valueToKey( texture->format() ) ), fixedName );
55}
56
57QString QgsFgUtils::formatNode( FgDumpContext context, const Qt3DCore::QNode *node )
58{
59 QString res = QLatin1String( "(%1%2)" )
60 .arg( QLatin1String( node->metaObject()->className() ) )
61 .arg( formatIdName( context, node->id().id(), node->objectName() ) );
62 if ( !node->isEnabled() )
63 res += QLatin1String( " [D]" );
64 return res;
65}
66
67QString QgsFgUtils::formatList( const QStringList &lst )
68{
69 return QString( QLatin1String( "[ %1 ]" ) ).arg( lst.join( QLatin1String( ", " ) ) );
70}
71
72QString QgsFgUtils::formatLongList( const QStringList &lst, int level )
73{
74 QString out = formatList( lst );
75 if ( out.size() < 200 )
76 return out;
77
78 out = QString( QLatin1String( "[\n" ) );
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( QLatin1String( "]" ) );
85 return out + end.rightJustified( end.length() + ( 1 + level ) * 2, ' ' );
86}
87
88QString QgsFgUtils::formatField( const QString &name, const QString &value )
89{
90 if ( value == "<no_value>" )
91 return QString( QLatin1String( "(%1)" ) ).arg( name );
92 return QString( QLatin1String( "(%1:%2)" ) ).arg( name, value );
93}
94
95QString QgsFgUtils::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(
126 texImg->metaObject()->className(),
127 formatIdName( context, texImg->id().id(), texImg->objectName() )
128 );
129 }
130 }
131 }
132 if ( const auto *material = qobject_cast<const Qt3DRender::QMaterial *>( comp ) )
133 {
134 if ( material->effect() )
135 {
136 const auto techniques = material->effect()->techniques();
137 for ( const auto *tech : techniques )
138 {
139 extractTextureParam( context, tech->parameters(), fl );
140 const auto passes = tech->renderPasses();
141 for ( const auto *pass : passes )
142 {
143 extractTextureParam( context, pass->parameters(), fl );
144 }
145 }
146 extractTextureParam( context, material->effect()->parameters(), fl );
147 }
148 extractTextureParam( context, material->parameters(), fl );
149 if ( !fl.empty() )
150 res += formatList( fl );
151 }
152
153 componentNames << res;
154 }
155 res += formatLongList( componentNames, level );
156 }
157
158 return res;
159}
160
161QStringList QgsFgUtils::dumpSG( FgDumpContext context, const Qt3DCore::QNode *node, int level )
162{
163 QStringList reply;
164 const auto *entity = qobject_cast<const Qt3DCore::QEntity *>( node );
165 if ( entity )
166 {
167 QString res = dumpSGEntity( context, entity, level );
168 reply += res.rightJustified( res.length() + level * 2, ' ' );
169 level++;
170 }
171
172 const auto children = node->childNodes();
173 for ( auto *child : children )
174 reply += dumpSG( context, child, level );
175
176 return reply;
177}
178
179QString QgsFgUtils::dumpFGNode( FgDumpContext context, const Qt3DRender::QFrameGraphNode *node )
180{
181 QString res = formatNode( context, node );
182
183 if ( const auto *lf = qobject_cast<const Qt3DRender::QLayerFilter *>( node ) )
184 {
185 QStringList sl;
186 const auto layers = lf->layers();
187 for ( auto layer : layers )
188 {
189 sl += formatIdName( context, layer->id().id(), layer->objectName() );
190 }
191
192 QStringList fl;
193 fl += formatField( QMetaEnum::fromType<Qt3DRender::QLayerFilter::FilterMode>().valueToKey( lf->filterMode() ), formatList( sl ) );
194 res += QString( " %1" ).arg( formatList( fl ) );
195 }
196
197 else if ( const auto *cs = qobject_cast<const Qt3DRender::QCameraSelector *>( node ) )
198 {
199 QStringList fl;
200 fl += formatField( cs->camera()->metaObject()->className(), formatIdName( context, cs->camera()->id().id(), cs->camera()->objectName() ) );
201 res += QString( " %1" ).arg( formatList( fl ) );
202 }
203
204 else if ( const auto *rss = qobject_cast<const Qt3DRender::QRenderStateSet *>( node ) )
205 {
206 QStringList sl;
207 const auto renderStates = rss->renderStates();
208 for ( auto rs : renderStates )
209 {
210 if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QCullFace *>( rs ) )
211 {
212 sl += formatField( "QCullFace", QMetaEnum::fromType<Qt3DRender::QCullFace::CullingMode>().valueToKey( rs_cast->mode() ) );
213 }
214 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QBlendEquation *>( rs ) )
215 {
216 sl += formatField( "QBlendEquation", QMetaEnum::fromType<Qt3DRender::QBlendEquation::BlendFunction>().valueToKey( rs_cast->blendFunction() ) );
217 }
218 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QBlendEquationArguments *>( rs ) )
219 {
220 QStringList fl;
221 fl += formatField( "sourceRgb", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->sourceRgb() ) );
222 fl += formatField( "destinationRgb", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->destinationRgb() ) );
223 fl += formatField( "sourceAlpha", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->sourceAlpha() ) );
224 fl += formatField( "destinationAlpha", QMetaEnum::fromType<Qt3DRender::QBlendEquationArguments::Blending>().valueToKey( rs_cast->destinationAlpha() ) );
225 fl += formatField( "bufferIndex", QString::number( rs_cast->bufferIndex() ) );
226
227 sl += formatField( "QBlendEquationArguments", formatList( fl ) );
228 }
229 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QColorMask *>( rs ) )
230 {
231 QStringList fl;
232 fl += formatField( "red", ( rs_cast->isRedMasked() ? QLatin1String( "true" ) : QLatin1String( "false" ) ) );
233 fl += formatField( "green", ( rs_cast->isGreenMasked() ? QLatin1String( "true" ) : QLatin1String( "false" ) ) );
234 fl += formatField( "blue", ( rs_cast->isBlueMasked() ? QLatin1String( "true" ) : QLatin1String( "false" ) ) );
235 fl += formatField( "alpha", ( rs_cast->isAlphaMasked() ? QLatin1String( "true" ) : QLatin1String( "false" ) ) );
236 sl += formatField( "QColorMask", formatList( fl ) );
237 }
238 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QDepthTest *>( rs ) )
239 {
240 sl += formatField( "QDepthTest", QMetaEnum::fromType<Qt3DRender::QDepthTest::DepthFunction>().valueToKey( rs_cast->depthFunction() ) );
241 }
242 else if ( qobject_cast<const Qt3DRender::QNoDepthMask *>( rs ) )
243 {
244 sl += formatField( "QNoDepthMask", "<no_value>" );
245 }
246 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QPointSize *>( rs ) )
247 {
248 QStringList fl;
249 fl += formatField( "sizeMode", QMetaEnum::fromType<Qt3DRender::QPointSize::SizeMode>().valueToKey( rs_cast->sizeMode() ) );
250 fl += formatField( "value", QString::number( rs_cast->value() ) );
251 sl += formatField( "QPointSize", formatList( fl ) );
252 }
253 else if ( const auto *rs_cast = qobject_cast<const Qt3DRender::QPolygonOffset *>( rs ) )
254 {
255 QStringList fl;
256 fl += formatField( "scaleFactor", QString::number( rs_cast->scaleFactor() ) );
257 fl += formatField( "depthSteps", QString::number( rs_cast->depthSteps() ) );
258 sl += formatField( "QPolygonOffset", formatList( fl ) );
259 }
260 else if ( qobject_cast<const Qt3DRender::QSeamlessCubemap *>( rs ) )
261 {
262 sl += formatField( "QSeamlessCubemap", "<no_value>" );
263 }
264 }
265 res += QString( " %1" ).arg( formatList( sl ) );
266 }
267
268 else if ( const auto *rs = qobject_cast<const Qt3DRender::QRenderTargetSelector *>( node ) )
269 {
270 if ( rs->target() )
271 {
272 QStringList sl;
273 const auto outputs = rs->target()->outputs();
274 for ( auto output : outputs )
275 {
276 sl += formatField( QMetaEnum::fromType<Qt3DRender::QRenderTargetOutput::AttachmentPoint>().valueToKey( output->attachmentPoint() ), formatIdName( context, output->texture() ) );
277 }
278 QStringList fl;
279 fl += formatField( QLatin1String( "outputs" ), formatList( sl ) );
280 res += QString( " %1" ).arg( formatList( fl ) );
281 }
282 }
283 // if (!n->isEnabled())
284 // res += QLatin1String(" [D]");
285 return res;
286}
287
288QStringList QgsFgUtils::dumpFG( FgDumpContext context, const Qt3DCore::QNode *node, int level )
289{
290 QStringList reply;
291
292 const Qt3DRender::QFrameGraphNode *fgNode = qobject_cast<const Qt3DRender::QFrameGraphNode *>( node );
293 if ( fgNode )
294 {
295 QString res = dumpFGNode( context, fgNode );
296 reply += res.rightJustified( res.length() + level * 2, ' ' );
297 }
298
299 const auto children = node->childNodes();
300 const int inc = fgNode ? 1 : 0;
301 for ( auto *child : children )
302 {
303 auto *childFGNode = qobject_cast<Qt3DCore::QNode *>( child );
304 if ( childFGNode )
305 reply += dumpFG( context, childFGNode, level + inc );
306 }
307
308 return reply;
309}
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...