Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrenderdata.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
8#if QT_CONFIG(thread)
9#include <QtCore/qthreadpool.h>
10#endif // QT_CONFIG(thread)
11
12#include <QtQuick/private/qsgcontext_p.h>
13#include <QtQuick/private/qsgrenderer_p.h>
14
15#include "graphobjects/qssgrenderroot_p.h"
16#include "graphobjects/qssgrenderlayer_p.h"
17#include "graphobjects/qssgrenderitem2d_p.h"
19#include "qssgrenderer_p.h"
20#include "resourcemanager/qssgrenderbuffermanager_p.h"
21
22#include <mutex>
23#include <condition_variable>
24
26
27Q_STATIC_LOGGING_CATEGORY(lcLayerData, "qt.quick3d.render.layerdata")
28
29static void reindexChildNodes(QSSGRenderNode &node, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
30{
31 Q_ASSERT(node.type != QSSGRenderNode::Type::Layer);
32 if (node.type != QSSGRenderNode::Type::Layer) {
33 // Note: In the case of import scenes the version and index might already
34 // have been set. We therefore assume nodes with same version is already
35 // indexed.
36 if (node.h.version() != version)
37 node.h = QSSGRenderNodeHandle(0, version, dfsIdx);
38 for (QSSGRenderNode &chld : node.children)
39 reindexChildNodes(chld, version, ++dfsIdx, ++count);
40 }
41}
42
43static void reindexLayerChildNodes(QSSGRenderLayer &layer, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
44{
45 Q_ASSERT(layer.type == QSSGRenderNode::Type::Layer);
46 if (layer.type == QSSGRenderNode::Type::Layer) {
47 layer.h = QSSGRenderNodeHandle(0, version, 0); // Layer nodes are always indexed at 0;
48 for (QSSGRenderNode &chld : layer.children)
49 reindexChildNodes(chld, version, ++dfsIdx, ++count);
50 }
51}
52
53static void reindex(QSSGRenderRoot *rootNode, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
54{
55 if (rootNode) {
56 Q_ASSERT(rootNode->type == QSSGRenderNode::Type::Root);
57 Q_ASSERT(dfsIdx == 0);
58 rootNode->h = QSSGRenderNodeHandle(0, version, 0);
59 for (QSSGRenderNode &chld : rootNode->children) // These should be layer nodes
60 reindexLayerChildNodes(static_cast<QSSGRenderLayer &>(chld), version, dfsIdx, count);
61 }
62}
63
64enum class Insert { Back, Indexed };
65template <Insert insert = Insert::Back>
66static void collectChildNodesFirst(QSSGRenderNode &node, QSSGGlobalRenderNodeData::NodeStore &outList, [[maybe_unused]] size_t &idx)
67{
68 Q_ASSERT_X(node.type != QSSGRenderNode::Type::Layer, Q_FUNC_INFO, "Unexpected Layer node in child list!");
69 if constexpr (insert == Insert::Indexed)
70 outList[idx++] = &node;
71 else
72 outList.push_back(&node);
73 for (QSSGRenderNode &chld : node.children)
74 collectChildNodesFirst<insert>(chld, outList, idx);
75}
76
77template <Insert insert = Insert::Back>
78static void collectChildNodesSecond(QSSGRenderNode &node, QSSGGlobalRenderNodeData::NodeStore &outList, [[maybe_unused]] size_t &idx)
79{
80 if (node.getGlobalState(QSSGRenderNode::GlobalState::Active)) {
81 if constexpr (insert == Insert::Indexed)
82 outList[idx++] = &node;
83 else
84 outList.push_back(&node);
85 for (QSSGRenderNode &chld : node.children)
86 collectChildNodesSecond<insert>(chld, outList, idx);
87 }
88}
89
90enum class Discard { None, Inactive };
91template <Discard discard = Discard::None, Insert insert = Insert::Back>
92static void collectLayerChildNodes(QSSGRenderLayer *layer, QSSGGlobalRenderNodeData::NodeStore &outList, [[maybe_unused]] size_t &idx)
93{
94 Q_ASSERT(layer->type == QSSGRenderNode::Type::Layer);
95 if (layer) {
96 for (QSSGRenderNode &chld : layer->children) {
97 if constexpr (discard == Discard::Inactive)
98 collectChildNodesSecond<insert>(chld, outList, idx);
99 else
100 collectChildNodesFirst<insert>(chld, outList, idx);
101 }
102 }
103
104 if constexpr (insert == Insert::Back)
105 idx = outList.size();
106}
107
108template <QSSGRenderDataHelpers::Strategy Strategy>
109static bool calcGlobalNodeDataIndexedImpl(QSSGRenderNode *node,
110 const QSSGRenderNodeVersionType version,
111 QSSGGlobalRenderNodeData::GlobalTransformStore &globalTransforms,
112 QSSGGlobalRenderNodeData::GlobalOpacityStore &globalOpacities)
113{
114 using DirtyFlag = QSSGRenderNode::DirtyFlag;
115 using FlagT = QSSGRenderNode::FlagT;
116 constexpr DirtyFlag TransformAndOpacityDirty = DirtyFlag(FlagT(DirtyFlag::TransformDirty) | FlagT(DirtyFlag::OpacityDirty));
117
118 if (Q_UNLIKELY(!node || (node->h.version() != version)))
119 return false;
120
121 constexpr bool forcedRebuild = (Strategy == QSSGRenderDataHelpers::Strategy::Initial);
122 bool retval = forcedRebuild || node->isDirty(TransformAndOpacityDirty);
123
124 // NOTE 1: Nodes shared across windows will be marked with a stick dirty flag.
125 // Officially this isn't supported, but in practice it can happen so
126 // we try to be nice and try to keep things moving (literally).
127 // NOTE 2: We don't change the return value, as that would interfere with the progressive AA.
128 const bool alwaysDirty = node->isDirty(DirtyFlag::StickyDirty);
129
130 if (retval || alwaysDirty) {
131 const auto idx = node->h.index();
132
133 auto &globalTransform = globalTransforms[idx];
134 auto &globalOpacity = globalOpacities[idx];
135 globalOpacity = node->localOpacity;
136 globalTransform = node->localTransform;
137
138 if (QSSGRenderNode *parent = node->parent) {
139 const auto pidx = parent->h.index();
140 const auto pOpacity = globalOpacities[pidx];
141 globalOpacity *= pOpacity;
142
143 if (parent->type != QSSGRenderGraphObject::Type::Layer) {
144 const auto pTransform = globalTransforms[pidx];
145 globalTransform = pTransform * node->localTransform;
146 }
147 }
148 // Clear dirty flags (Transform, Opacity, Active, Pickable)
149 node->clearDirty(TransformAndOpacityDirty);
150 }
151
152 // We always clear dirty in a reasonable manner but if we aren't active
153 // there is no reason to tell the universe if we are dirty or not.
154 return retval;
155}
156
157QSSGRenderDataHelpers::GlobalStateResult QSSGRenderDataHelpers::updateGlobalNodeState(QSSGRenderNode *node, const VersionType version)
158{
159 using LocalState = QSSGRenderNode::LocalState;
160 using GlobalState = QSSGRenderNode::GlobalState;
161 using DirtyFlag = QSSGRenderNode::DirtyFlag;
162 using FlagT = QSSGRenderNode::FlagT;
163
164 constexpr DirtyFlag ClearDirtyMask = DirtyFlag(FlagT(DirtyFlag::ActiveDirty) | FlagT(DirtyFlag::PickableDirty) | FlagT(DirtyFlag::ImportDirty));
165
166 if (Q_UNLIKELY(!node || (node->h.version() != version)))
168
169 const bool activeDirty = node->isDirty(DirtyFlag::ActiveDirty);
170 const bool pickableDirty = node->isDirty(DirtyFlag::PickableDirty);
171 const bool importedDirty = node->isDirty(DirtyFlag::ImportDirty);
172
173 const bool updateState = activeDirty || pickableDirty || importedDirty;
174
175 if (updateState) {
176 const QSSGRenderNode *parent = node->parent;
177 const bool hasParent = (parent != nullptr);
178 const bool globallyActive = node->getLocalState(LocalState::Active) && (!hasParent || parent->getGlobalState(GlobalState::Active));
179 node->flags = globallyActive ? (node->flags | FlagT(GlobalState::Active)) : (node->flags & ~FlagT(GlobalState::Active));
180 const bool globallyPickable = node->getLocalState(LocalState::Pickable) || (hasParent && parent->getGlobalState(GlobalState::Pickable));
181 node->flags = globallyPickable ? (node->flags | FlagT(GlobalState::Pickable)) : (node->flags & ~FlagT(GlobalState::Pickable));
182 const bool globallyImported = node->getLocalState(LocalState::Imported) || (hasParent && parent->getGlobalState(GlobalState::Imported));
183 node->flags = globallyImported ? (node->flags | FlagT(GlobalState::Imported)) : (node->flags & ~FlagT(GlobalState::Imported));
184
185 // Clear dirty flags (Active, Pickable, Imported)
186 node->clearDirty(ClearDirtyMask);
187 }
188
189 return GlobalStateResult((activeDirty << 1) | (pickableDirty << 2));
190}
191
192bool QSSGRenderDataHelpers::calcInstanceTransforms(QSSGRenderNode *node,
193 const VersionType version,
194 QSSGGlobalRenderNodeData::GlobalTransformStore &globalTransforms,
195 QSSGGlobalRenderNodeData::InstanceTransformStore &instanceTransforms)
196{
197 if (Q_UNLIKELY(!node || (node->h.version() != version)))
198 return false;
199
200 constexpr bool retval = true;
201
202 // NOTE: We've already calculated the global states and transforms at this point
203 // so if the node isn't active we don't need to do anything.
204 // We're also assuming the node list is stored depth first order.
205 const auto idx = node->h.index();
206 QSSGRenderNode *parent = node->parent;
207 if (parent && parent->type != QSSGRenderGraphObject::Type::Layer && node->getLocalState(QSSGRenderNode::LocalState::Active)) {
208 const auto pidx = parent->h.index();
209 const auto &pGlobalTransform = globalTransforms[pidx];
210 QSSGRenderNode *instanceRoot = node->instanceRoot;
211 if (instanceRoot == node) {
212 instanceTransforms[idx] = { node->localTransform, pGlobalTransform };
213 } else if (instanceRoot) {
214 auto &[nodeInstanceLocalTransform, nodeInstanceGlobalTransform] = instanceTransforms[idx];
215 auto &[instanceRootLocalTransform, instanceRootGlobalTransform] = instanceTransforms[instanceRoot->h.index()];
216 nodeInstanceGlobalTransform = instanceRootGlobalTransform;
217 //### technically O(n^2) -- we could cache localInstanceTransform if every node in the
218 // tree is guaranteed to have the same instance root. That would require an API change.
219 nodeInstanceLocalTransform = node->localTransform;
220 auto *p = parent;
221 while (p) {
222 if (p == instanceRoot) {
223 nodeInstanceLocalTransform = instanceRootLocalTransform * nodeInstanceLocalTransform;
224 break;
225 }
226 nodeInstanceLocalTransform = p->localTransform * nodeInstanceLocalTransform;
227 p = p->parent;
228 }
229 } else {
230 // By default, we do magic: translation is applied to the global instance transform,
231 // while scale/rotation is local
232 QMatrix4x4 globalInstanceTransform = globalTransforms[pidx];
233 QMatrix4x4 localInstanceTransform = node->localTransform;
234 auto &localInstanceMatrix = *reinterpret_cast<float (*)[4][4]>(localInstanceTransform.data());
235 QVector3D localPos{localInstanceMatrix[3][0], localInstanceMatrix[3][1], localInstanceMatrix[3][2]};
236 localInstanceMatrix[3][0] = 0;
237 localInstanceMatrix[3][1] = 0;
238 localInstanceMatrix[3][2] = 0;
239 globalInstanceTransform = pGlobalTransform;
240 globalInstanceTransform.translate(localPos);
241 instanceTransforms[idx] = { localInstanceTransform, globalInstanceTransform };
242 }
243 } else {
244 instanceTransforms[idx] = { node->localTransform, {} };
245 }
246
247 return retval;
248}
249
250QSSGGlobalRenderNodeData::QSSGGlobalRenderNodeData(QSSGRenderRoot *root)
251#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
252 : m_threadPool(new QThreadPool)
253 , m_rootNode(root)
254#else
255 : m_rootNode(root)
256#endif // QT_CONFIG(thread)
257{
258
259}
260
261QSSGGlobalRenderNodeData::~QSSGGlobalRenderNodeData()
262{
263
264}
265
266void QSSGGlobalRenderNodeData::reindex()
267{
268 if (m_rootNode) {
269 quint32 dfsIdx = 0;
270 m_nodeCount = 0;
271
272 // Bump the version to invalidate all existing handles. This ensures nodes don't start
273 // accessing stale data.
274 ++m_version;
275
276 // Version 0 means "not yet indexed" (default-constructed handle), so skip it.
277 // NOTE: This can happen if the version wraps around.
278 if (Q_UNLIKELY(m_version == 0)) {
279 qCDebug(lcLayerData) << "Version wrap around detected, resetting to 1.";
280 ++m_version;
281 }
282
283 ::reindex(m_rootNode, m_version, dfsIdx, m_nodeCount);
284
285 // Actual storage size (Some nodes, like the layers, will all use index 0).
286 // NOTE: This can differ from the node count, as nodes are collected for each
287 // layer. Since layers can reference nodes outside their view the node list
288 // will contain duplicate nodes when import scene is used!
289 m_size = dfsIdx + 1;
290
291 globalTransforms.resize(m_size, QMatrix4x4{ Qt::Uninitialized });
292 globalOpacities.resize(m_size, 1.0f);
293 instanceTransforms.resize(m_size, { QMatrix4x4{ Qt::Uninitialized }, QMatrix4x4{ Qt::Uninitialized } });
294 normalMatrices.resize(m_size, QMatrix3x3{});
295 meshes.resize(m_size, nullptr);
296 materials.resize(m_size, {});
297
298 collectNodes(m_rootNode);
299 // NOTE: If the tree was dirty we force a full rebuild of the global transforms etc. since
300 // the stored data is invalid for the new index order.
301 updateGlobalState();
302 }
303}
304
305void QSSGGlobalRenderNodeData::invalidate()
306{
307 m_rootNode = nullptr;
308}
309
310QMatrix4x4 QSSGGlobalRenderNodeData::getGlobalTransform(QSSGRenderNodeHandle h, const QMatrix4x4 &defaultValue) const
311{
312 // Ensure we have an valid index.
313 const bool hasId = h.hasId();
314 const bool validVersion = hasId && (h.version() == m_version);
315 const auto index = h.index();
316
317 // NOTE: In effect we are returning the local transform here in some cases, which
318 // is why we don't assert or have hints about the likelyhood of branching here.
319 if (!validVersion || !(globalTransforms.size() > index))
320 return defaultValue;
321
322 return globalTransforms[index];
323}
324
325QMatrix4x4 QSSGGlobalRenderNodeData::getGlobalTransform(QSSGRenderNodeHandle h) const
326{
327 return getGlobalTransform(h, QMatrix4x4{ Qt::Uninitialized });
328}
329
330QMatrix4x4 QSSGGlobalRenderNodeData::getGlobalTransform(const QSSGRenderNode &node) const
331{
332 return getGlobalTransform(node.h, node.localTransform);
333}
334
335float QSSGGlobalRenderNodeData::getGlobalOpacity(QSSGRenderNodeHandle h, float defaultValue) const
336{
337 const bool hasId = h.hasId();
338 const bool validVersion = hasId && (h.version() == m_version);
339 const auto index = h.index();
340
341 if (!validVersion || !(globalOpacities.size() > index))
342 return defaultValue;
343
344 return globalOpacities[index];
345}
346
347float QSSGGlobalRenderNodeData::getGlobalOpacity(const QSSGRenderNode &node) const
348{
349 return getGlobalOpacity(node.h, node.localOpacity);
350}
351
352#if QT_CONFIG(thread)
353#ifdef Q_OS_WASM
354QThreadPool *QSSGGlobalRenderNodeData::threadPool() const { return nullptr; }
355#else
356QThreadPool *QSSGGlobalRenderNodeData::threadPool() const { return m_threadPool.get(); }
357#endif
358#endif // QT_CONFIG(thread)
359
360QSSGGlobalRenderNodeData::LayerNodeView QSSGGlobalRenderNodeData::getLayerNodeView(QSSGRenderLayerHandle h) const
361{
362 const bool hasId = h.hasId();
363 const bool validVersion = hasId && (h.version() == m_version);
364 const auto index = h.index();
365
366 if (!validVersion || !(layerNodes.size() > index))
367 return { };
368
369 auto &section = layerNodes[index];
370
371 return { nodes.data() + section.offset, qsizetype(section.size) };
372}
373
374QSSGGlobalRenderNodeData::LayerNodeView QSSGGlobalRenderNodeData::getLayerNodeView(const QSSGRenderLayer &layer) const
375{
376 return getLayerNodeView(layer.lh);
377}
378
379void QSSGGlobalRenderNodeData::collectNodes(QSSGRenderRoot *rootNode)
380{
381 // 1. Collect all the nodes and create views into the node storage for each layer
382 // 2. Update the global state
383 // 3. Update the global data
384 Q_ASSERT(rootNode != nullptr);
385
386 nodes.clear();
387 nodes.resize(m_nodeCount, nullptr);
388 layerNodes.clear();
389
390 size_t idx = 0;
391 quint32 layerIdx = 0;
392 for (QSSGRenderNode &chld : rootNode->children) {
393 Q_ASSERT(chld.type == QSSGRenderNode::Type::Layer);
394 QSSGRenderLayer *layer = static_cast<QSSGRenderLayer *>(&chld);
395 const size_t offset = idx;
396 collectLayerChildNodes<Discard::None, Insert::Indexed>(layer, nodes, idx);
397 layer->lh = QSSGRenderLayerHandle(layer->h.context(), m_version, layerIdx++);
398 layerNodes.emplace_back(LayerNodeSection{offset , idx - offset});
399 }
400
401 nodes.resize(idx /* idx == next_idx == size */);
402}
403
404void QSSGGlobalRenderNodeData::updateGlobalState()
405{
406 // Update the active and pickable state
407 for (QSSGRenderNode *node : nodes)
408 QSSGRenderDataHelpers::updateGlobalNodeState(node, m_version);
409
410 // Recalculate ALL the global transforms and opacities.
411 for (QSSGRenderNode *node : nodes)
412 calcGlobalNodeDataIndexedImpl<QSSGRenderDataHelpers::Strategy::Initial>(node, m_version, globalTransforms, globalOpacities);
413
414 // FIXME: We shouldn't need to re-create all the instance transforms even when instancing isn't used...
415 for (QSSGRenderNode *node : nodes)
416 QSSGRenderDataHelpers::calcInstanceTransforms(node, m_version, globalTransforms, instanceTransforms);
417}
418
419QSSGGlobalRenderNodeData::InstanceTransforms QSSGGlobalRenderNodeData::getInstanceTransforms(const QSSGRenderNode &node) const
420{
421 return getInstanceTransforms(node.h);
422}
423
424QSSGGlobalRenderNodeData::InstanceTransforms QSSGGlobalRenderNodeData::getInstanceTransforms(QSSGRenderNodeHandle h) const
425{
426 const bool hasId = h.hasId();
427 const bool validVersion = hasId && (h.version() == m_version);
428 const auto index = h.index();
429
430 if (!validVersion || !(instanceTransforms.size() > index))
431 return { };
432
433 return instanceTransforms[index];
434}
435
436QSSGRenderModelData::QSSGRenderModelData(const QSSGGlobalRenderNodeDataPtr &globalNodeData)
437 : m_gnd(globalNodeData)
438 , m_version(globalNodeData->version())
439{
440
441}
442
443QMatrix3x3 QSSGRenderModelData::getNormalMatrix(QSSGRenderNodeHandle h, QMatrix3x3 defaultValue) const
444{
445 const bool hasId = h.hasId();
446 const bool validVersion = hasId && (h.version() == m_gnd->version());
447 const auto index = h.index();
448 if (!validVersion || !(m_gnd->normalMatrices.size() > index))
449 return defaultValue;
450
451 return m_gnd->normalMatrices[index];
452}
453
454QMatrix3x3 QSSGRenderModelData::getNormalMatrix(const QSSGRenderModel &model) const
455{
456 return getNormalMatrix(model.h, QMatrix3x3{ Qt::Uninitialized });
457}
458
459QSSGRenderMesh *QSSGRenderModelData::getMesh(const QSSGRenderModel &model) const
460{
461 const bool hasId = model.h.hasId();
462 const bool validVersion = hasId && (model.h.version() == m_gnd->version());
463 const auto index = model.h.index();
464
465 if (!validVersion || !(m_gnd->meshes.size() > index))
466 return nullptr;
467
468 return m_gnd->meshes[index];
469}
470
471QSSGRenderModelData::MaterialList QSSGRenderModelData::getMaterials(const QSSGRenderModel &model) const
472{
473 const bool hasId = model.h.hasId();
474 const bool validVersion = hasId && (model.h.version() == m_gnd->version());
475 const auto index = model.h.index();
476
477 if (!validVersion || !(m_gnd->materials.size() > index))
478 return {};
479
480 return m_gnd->materials[index];
481}
482
483QSSGRenderModelData::ModelViewProjections QSSGRenderModelData::getModelViewProjection(const QSSGRenderModel &model) const
484{
485 return getModelViewProjection(model.h);
486}
487
489{
490 const bool hasId = h.hasId();
491 const bool validVersion = hasId && (h.version() == m_version);
492 const auto index = h.index();
493
494 if (!validVersion || !(modelViewProjections.size() > index))
495 return {};
496
497 return modelViewProjections[index];
498}
499
500void QSSGRenderModelData::prepareMeshData(const QSSGModelsView &models, QSSGRenderer *renderer)
501{
502 const auto &bufferManager = renderer->contextInterface()->bufferManager();
503
504 const bool globalPickingEnabled = QSSGRendererPrivate::isGlobalPickingEnabled(*renderer);
505
506 for (auto *model : models) {
507 // It's up to the BufferManager to employ the appropriate caching mechanisms, so
508 // loadMesh() is expected to be fast if already loaded. Note that preparing
509 // the same QSSGRenderModel in different QQuickWindows (possible when a
510 // scene is shared between View3Ds where the View3Ds belong to different
511 // windows) leads to a different QSSGRenderMesh since the BufferManager is,
512 // very correctly, per window, and so per scenegraph render thread.
513
514 // Ensure we have a mesh
515 if (auto *theMesh = bufferManager->loadMesh(*model)) {
516 m_gnd->meshes[model->h.index()] = theMesh;
517 // Completely transparent models cannot be pickable. But models with completely
518 // transparent materials still are. This allows the artist to control pickability
519 // in a somewhat fine-grained style.
520 const float modelGlobalOpacity = m_gnd->getGlobalOpacity(*model);
521 const bool canModelBePickable = (modelGlobalOpacity > QSSGRendererPrivate::minimumRenderOpacity)
522 && (globalPickingEnabled
523 || model->getGlobalState(QSSGRenderModel::GlobalState::Pickable));
524 if (canModelBePickable) {
525 // Check if there is BVH data, if not generate it
526 if (!theMesh->bvh) {
527 const QSSGMesh::Mesh mesh = bufferManager->loadLightmapMesh(*model);
528
529 if (mesh.isValid())
530 theMesh->bvh = bufferManager->loadMeshBVH(mesh);
531 else if (model->geometry)
532 theMesh->bvh = bufferManager->loadMeshBVH(model->geometry);
533 else if (!model->meshPath.isNull())
534 theMesh->bvh = bufferManager->loadMeshBVH(model->meshPath);
535
536 if (theMesh->bvh) {
537 const auto &roots = theMesh->bvh->roots();
538 for (qsizetype i = 0, end = qsizetype(roots.size()); i < end; ++i)
539 theMesh->subsets[i].bvhRoot = roots[i];
540 }
541 }
542 }
543 } else {
544 m_gnd->meshes[model->h.index()] = nullptr;
545 }
546 }
547
548 // Now is the time to kick off the vertex/index buffer updates for all the
549 // new meshes (and their submeshes). This here is the last possible place
550 // to kick this off because the rest of the rendering pipeline will only
551 // see the individual sub-objects as "renderable objects".
552 bufferManager->commitBufferResourceUpdates();
553}
554
555void QSSGRenderModelData::prepareMaterials(const QSSGModelsView &models)
556{
557 for (auto *model : models)
558 m_gnd->materials[model->h.index()] = model->materials;
559}
560
561void QSSGRenderModelData::updateModelData(QSSGModelsView &models, QSSGRenderer *renderer, const QSSGRenderCameraDataList &renderCameraData)
562{
563 const bool versionChanged = m_version != m_gnd->version();
564 if (versionChanged)
565 m_version = m_gnd->version();
566
567 const QMatrix4x4 defaultModelViewProjection;
568
569 // Resize per-layer MVP store to the GND storage size so import-scene shared nodes
570 // never alias each other's slots (GND indices are stable and unique across all layers).
571 modelViewProjections.resize(m_gnd->storageSize(), { defaultModelViewProjection, defaultModelViewProjection });
572
573#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
574 bool matrixesDone = false;
575 bool mvpsDone = false;
576 std::mutex mutex;
577 std::condition_variable cv;
578#endif
579
580 // - Normal matrices (stored in GND, shared across layers)
581 const auto doNormalMatrices = [&]() {
582 for (const QSSGRenderModel *model : std::as_const(models)) {
583 auto &normalMatrix = m_gnd->normalMatrices[model->h.index()];
584 const QMatrix4x4 &globalTransform = m_gnd->globalTransforms[model->h.index()];
585 QSSGRenderNode::calculateNormalMatrix(globalTransform, normalMatrix);
586 }
587#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
588 std::lock_guard<std::mutex> guard(mutex);
589 matrixesDone = true;
590 cv.notify_all();
591#endif
592 };
593
594 // - MVPs (per-layer, indexed by GND node index)
595 const auto doMVPs = [&]() {
596 for (const QSSGRenderModel *model : std::as_const(models)) {
597 int mvpCount = 0;
598 const QMatrix4x4 &globalTransform = m_gnd->globalTransforms[model->h.index()];
599 auto &mvp = modelViewProjections[model->h.index()];
600 for (const QSSGRenderCameraData &cameraData : renderCameraData)
601 QSSGRenderNode::calculateMVP(globalTransform, cameraData.viewProjection, mvp[mvpCount++]);
602 }
603#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
604 std::lock_guard<std::mutex> guard(mutex);
605 mvpsDone = true;
606 cv.notify_all();
607#endif
608 };
609
610#if QT_CONFIG(thread)
611 auto *threadPool = m_gnd->threadPool();
612#define qssgTryThreadedStart(func)
613 if (!threadPool) {
614 func();
615 } else if (!threadPool->tryStart(func)) {
616 qWarning("Unable to start thread for %s!", #func);
617 func();
618 }
619#else
620#define qssgTryThreadedStart(func)
621 func();
622#endif // QT_CONFIG(thread)
623
624 qssgTryThreadedStart(doNormalMatrices);
625 qssgTryThreadedStart(doMVPs);
626
627 // While we are waiting for the threads to finish, we can also prepare and update
628 // the materials and meshes.
629 prepareMaterials(models);
630 prepareMeshData(models, renderer);
631
632 // Wait for the threads to finish
633#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
634 std::unique_lock<std::mutex> guard(mutex);
635 cv.wait(guard, [&]() { return matrixesDone && mvpsDone; });
636#endif
637}
638
639bool QSSGRenderDataHelpers::updateGlobalNodeDataIndexed(QSSGRenderNode *node, const VersionType version, QSSGGlobalRenderNodeData::GlobalTransformStore &globalTransforms, QSSGGlobalRenderNodeData::GlobalOpacityStore &globalOpacities)
640{
641 return calcGlobalNodeDataIndexedImpl<Strategy::Update>(node, version, globalTransforms, globalOpacities);
642}
643
644bool QSSGRenderDataHelpers::calcGlobalVariablesIndexed(QSSGRenderNode *node, const VersionType version, QSSGGlobalRenderNodeData::GlobalTransformStore &globalTransforms, QSSGGlobalRenderNodeData::GlobalOpacityStore &globalOpacities)
645{
646 return calcGlobalNodeDataIndexedImpl<Strategy::Initial>(node, version, globalTransforms, globalOpacities);
647}
648
650{
651 const auto foundIt = item2DRenderers.find(&item);
652 return (foundIt != item2DRenderers.cend()) ? foundIt->second : Item2DRenderer{};
653}
654
656{
657 const bool hasId = h.hasId();
658 const bool validVersion = hasId && (h.version() == m_version);
659 const auto index = h.index();
660
661 if (!validVersion || !(modelViewProjections.size() > index))
662 return {};
663
664 return modelViewProjections[index];
665}
666
668{
669 return getModelViewProjection(item.h);
670}
671
672QSSGRenderItem2DData::QSSGRenderItem2DData(const QSSGGlobalRenderNodeDataPtr &globalNodeData)
673{
674 m_gnd = globalNodeData;
675 m_version = globalNodeData->version();
676}
677
682
683void QSSGRenderItem2DData::updateItem2DData(QSSGItem2DsView &items, QSSGRenderer *renderer, const QSSGRenderCameraDataList &renderCameraData)
684{
685 const auto itemCount = size_t(items.size());
686
687 if (itemCount == 0)
688 return;
689
690 const bool versionChanged = m_version != m_gnd->version();
691 if (versionChanged)
692 m_version = m_gnd->version();
693
694 const QMatrix4x4 defaultModelViewProjection;
695
696 const auto &rhiCtx = renderer->contextInterface()->rhiContext();
697
698 // Resize to GND storage size so import-scene shared nodes use their stable GND indices.
699 modelViewProjections.resize(m_gnd->storageSize(), { defaultModelViewProjection, defaultModelViewProjection });
700
701 const auto &clipSpaceCorrMatrix = rhiCtx->rhi()->clipSpaceCorrMatrix();
702
703 // - MVPs (indexed by GND node index)
704 const auto doMVPs = [&]() {
705 for (const QSSGRenderItem2D *item : std::as_const(items)) {
706 int mvpCount = 0;
707 const QMatrix4x4 &globalTransform = m_gnd->globalTransforms[item->h.index()];
708 auto &mvps = modelViewProjections[item->h.index()];
709 for (const QSSGRenderCameraData &cameraData : renderCameraData) {
710 const QMatrix4x4 &mvp = cameraData.viewProjection * globalTransform;
711 mvps[mvpCount++] = clipSpaceCorrMatrix * mvp * flipMatrix;
712 }
713 }
714 };
715
716 doMVPs();
717
718 // Check that we have a renderer and that it hasn't changed (would indicate a context change)
719 // and we need to update all the data.
720 QSGRenderContext *sgRc = QSSGRendererPrivate::getSgRenderContext(*renderer);
721 const bool contextChanged = (item2DRenderContext && item2DRenderContext != sgRc);
722 item2DRenderContext = sgRc;
723
724 for (const QSSGRenderItem2D *theItem2D : std::as_const(items)) {
725 auto item2DRenderer = getItem2DRenderer(*theItem2D);
726 if (contextChanged) {
727 delete item2DRenderer;
728 // NOTE: Should already be "cleared" as the QObject is deleted
729 // but we do it here for clarity.
730 item2DRenderer.clear();
731 }
732
733 if (!item2DRenderer) {
734 item2DRenderer = sgRc->createRenderer(QSGRendererInterface::RenderMode3D);
735 QObject::connect(item2DRenderer, SIGNAL(sceneGraphChanged()), theItem2D->m_frontEndObject, SLOT(update()));
736 }
737
738 if (item2DRenderer->rootNode() != theItem2D->m_rootNode) {
739 item2DRenderer->setRootNode(theItem2D->m_rootNode);
740 theItem2D->m_rootNode->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update.
741 item2DRenderer->nodeChanged(theItem2D->m_rootNode, QSGNode::DirtyForceUpdate); // Force render list update.
742 }
743
744 item2DRenderers[theItem2D] = item2DRenderer;
745 }
746}
747
748void QSSGRenderItem2DData::releaseRenderData(const QSSGRenderItem2D &item)
749{
750 const auto foundIt = item2DRenderers.find(&item);
751 if (foundIt != item2DRenderers.cend()) {
752 delete foundIt->second;
753 item2DRenderers.erase(foundIt);
754
755 // Removing an item should trigger a reindex of the remaining items
756 ++m_version;
757 }
758}
759
761{
762 for (auto &it : item2DRenderers)
763 delete it.second;
764 item2DRenderers.clear();
765 item2DRenderContext = nullptr;
766 modelViewProjections.clear();
767 m_version = 0;
768}
769
770QT_END_NAMESPACE
void releaseRenderData(const QSSGRenderItem2D &item)
void updateItem2DData(QSSGItem2DsView &items, QSSGRenderer *renderer, const QSSGRenderCameraDataList &renderCameraData)
Item2DRenderer getItem2DRenderer(const QSSGRenderItem2D &item) const
ModelViewProjections getModelViewProjection(const QSSGRenderItem2D &item) const
ModelViewProjections getModelViewProjection(QSSGRenderNodeHandle h) const
ModelViewProjections getModelViewProjection(QSSGRenderNodeHandle h) const
void updateModelData(QSSGModelsView &models, QSSGRenderer *renderer, const QSSGRenderCameraDataList &renderCameraData)
QMatrix3x3 getNormalMatrix(QSSGRenderNodeHandle h, QMatrix3x3 defaultValue) const
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static bool calcGlobalNodeDataIndexedImpl(QSSGRenderNode *node, const QSSGRenderNodeVersionType version, QSSGGlobalRenderNodeData::GlobalTransformStore &globalTransforms, QSSGGlobalRenderNodeData::GlobalOpacityStore &globalOpacities)
static void reindex(QSSGRenderRoot *rootNode, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
static void reindexLayerChildNodes(QSSGRenderLayer &layer, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
static QT_BEGIN_NAMESPACE void reindexChildNodes(QSSGRenderNode &node, const QSSGRenderNodeVersionType version, quint32 &dfsIdx, size_t &count)
static void collectLayerChildNodes(QSSGRenderLayer *layer, QSSGGlobalRenderNodeData::NodeStore &outList, size_t &idx)
static void collectChildNodesFirst(QSSGRenderNode &node, QSSGGlobalRenderNodeData::NodeStore &outList, size_t &idx)
#define qssgTryThreadedStart(func)
static void collectChildNodesSecond(QSSGRenderNode &node, QSSGGlobalRenderNodeData::NodeStore &outList, size_t &idx)