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
qquick3drenderpass.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#include <QtQuick3D/private/qquick3dscenemanager_p.h>
9#include <QtQuick3DUtils/private/qssgassert_p.h>
10#include <QtQuick3DRuntimeRender/private/qssgrenderuserpass_p.h>
11#include <QtQuick3DRuntimeRender/private/qssgshadermaterialadapter_p.h>
12#include <QtQuick3DRuntimeRender/private/qssgrendershadertokenizer_p.h>
13#include <QtQuick3DRuntimeRender/private/qssgrenderimage_p.h>
14#include <QtQuick3DRuntimeRender/private/qssgrenderbuffer_p.h>
15#include <QtQuick3D/private/qquick3dshaderutils_p.h>
16
17#include <QtCore/QLoggingCategory>
18
20
21Q_LOGGING_CATEGORY(lcQuick3DRenderPass, "qt.quick3d.renderpass")
22
23/*!
24 \qmltype RenderPass
25 \inherits Object3D
26 \inqmlmodule QtQuick3D
27 \brief Defines a custom render pass for rendering 3D content.
28 \since 6.11
29
30 A RenderPass defines a rendering step and the render target it writes
31 into. It is the combination of three concerns:
32
33 \list
34 \li \b {Where to render} — one or more output textures declared as
35 \l ColorAttachment or \l DepthTextureAttachment commands in
36 \l {RenderPass::commands}{commands}.
37 \li \b {What to render} — which scene objects the pass draws,
38 controlled by \l RenderablesFilter commands. Sub-divisions of
39 work within the same render target are described using
40 \l SubRenderPass commands.
41 \li \b {How to render} — the \l materialMode and any
42 \l PipelineStateOverride commands.
43 \endlist
44
45 A RenderPass becomes active for a scene when it is placed as a child
46 of a \l View3D or a \l Node, or of another RenderPass.
47
48 \section1 Render order
49
50 Nesting a RenderPass inside another RenderPass expresses ordering:
51 nested passes render \e before their parent, so a pass that produces a
52 texture consumed by another pass is declared as a child of its
53 consumer, and results merge towards the top of the pass hierarchy.
54 Only RenderPass and \l ComputePass ancestors count towards the nesting
55 depth; other ancestors, such as \l Node items used for grouping, do not
56 affect the order. The relative render order of passes at the same
57 nesting depth is not defined; use nesting to express an ordering
58 requirement.
59
60 Nesting affects ordering only; a nested pass still renders into its
61 own render target. To render into the parent pass's render target,
62 reference the pass from a \l SubRenderPass command instead. A pass
63 referenced by a SubRenderPass command is invoked by its parent and
64 never renders on its own.
65
66 The following example sets up a simple off-screen pass that renders
67 all scene objects into a custom texture, which can then be consumed
68 by a material or a \l SimpleQuadRenderer:
69
70 \qml
71 import QtQuick3D
72
73 View3D {
74 // Declare the off-screen color buffer
75 RenderPassTexture {
76 id: myColorTexture
77 format: RenderPassTexture.RGBA8
78 }
79
80 // The render pass: where + what + how
81 RenderPass {
82 id: myRenderPass
83 commands: [
84 // Where: attach the texture as the color output
85 ColorAttachment {
86 name: "color0"
87 target: myColorTexture
88 },
89 // What: render all objects into it
90 RenderablesFilter {
91 renderableTypes: RenderablesFilter.Opaque | RenderablesFilter.Transparent
92 }
93 ]
94 }
95 }
96 \endqml
97
98 \section1 Exposing data to the shaders
99
100 As with Effects and Custom Materials, the RenderPass will expose and
101 update user-defined properties to the shaders automatically. Any QML
102 properties declared on a RenderPass subtype will be available as
103 uniforms in the shader.
104
105 Properties of type \l ShaderImage are exposed to the
106 augment shader as a GLSL image, and properties of type \l ShaderStorageBuffer
107 as a shader storage buffer, declared there with the \c SHADER_IMAGE and
108 \c STORAGE_BUFFER macros. Those declarations go outside \c{MAIN_FRAGMENT_AUGMENT()},
109 since only the code around that function is emitted at file scope. See
110 \l{Augment Shaders for Multiple Render Targets} for the details.
111
112 \sa SubRenderPass, RenderOutputProvider, RenderablesFilter, ShaderImage,
113 ShaderStorageBuffer
114*/
115
116QQuick3DRenderPass::QQuick3DRenderPass(QQuick3DObject *parent)
117 : QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::RenderPass, QQuick3DObjectPrivate::Flags::RequiresSecondaryUpdate)), parent)
118 , QQuick3DPropertyChangedTracker(this, QQuick3DSuperClassInfo<QQuick3DRenderPass>())
119{
120}
121QQuick3DRenderPass::AugmentSplitResult QQuick3DRenderPass::splitAugmentShader(const QByteArray &augment,
122 QByteArray &preamble,
123 QByteArray &body)
124{
125 // We have to pick apart the shader string such that the contents of the
126 // "void MAIN_FRAGMENT_AUGMENT() { }" function are taken out, and get added to the end of
127 // the shader generation to overwrite the "output" of the shader. Everything outside the
128 // function ends up as preamble code that is pasted in before the real main() (helper
129 // functions, resolvable #includes etc.).
130 //
131 // Tokenize the augment shader rather than doing a plain string search plus raw brace
132 // counting: the keyword is then ignored if it only appears in a comment, and braces inside
133 // comments don't throw off the matching.
134 QSSGShaderTokenizer tok;
135 tok.initialize(augment.constData());
136
137 const char *const begin = augment.constData();
138 const char *const end = begin + augment.size();
139
140 const char *funcStart = nullptr; // Start of the return type ("void") of the augment function.
141 const char *bodyStart = nullptr; // First character inside the function's outer braces.
142 const char *bodyEnd = nullptr; // The matching closing brace of the function body.
143 const char *afterFunc = nullptr; // First character after the closing brace.
144
145 const char *prevIdentStart = nullptr;
146 QByteArrayView prevIdent;
147
148 for (QSSGShaderTokenizer::Token t = tok.next(); t != QSSGShaderTokenizer::Token_EOF; t = tok.next()) {
149 if (t != QSSGShaderTokenizer::Token_Identifier)
150 continue;
151
152 const QByteArrayView id(tok.identifier, tok.pos - tok.identifier);
153 // The augment entry point is declared as "void MAIN_FRAGMENT_AUGMENT()"; requiring the
154 // preceding "void" avoids matching a call site or other mention of the name.
155 if (id == QByteArrayLiteral("MAIN_FRAGMENT_AUGMENT") && prevIdent == QByteArrayLiteral("void")) {
156 funcStart = prevIdentStart;
157 // Scan forward to the opening brace (skipping the parameter list); stop early on a
158 // semicolon, which would indicate a declaration without a body.
159 QSSGShaderTokenizer::Token n = tok.next();
160 while (n != QSSGShaderTokenizer::Token_EOF
161 && n != QSSGShaderTokenizer::Token_OpenBrace
162 && n != QSSGShaderTokenizer::Token_SemiColon) {
163 n = tok.next();
164 }
165 if (n == QSSGShaderTokenizer::Token_OpenBrace) {
166 bodyStart = tok.pos; // tok.pos is one past the '{'.
167 // Track brace depth to find the matching closing brace.
168 int depth = 1;
169 for (QSSGShaderTokenizer::Token b = tok.next(); b != QSSGShaderTokenizer::Token_EOF; b = tok.next()) {
170 if (b == QSSGShaderTokenizer::Token_OpenBrace) {
171 ++depth;
172 } else if (b == QSSGShaderTokenizer::Token_CloseBrace) {
173 if (--depth == 0) {
174 bodyEnd = tok.pos - 1; // tok.pos is one past the '}'.
175 afterFunc = tok.pos;
176 break;
177 }
178 }
179 }
180 }
181 break;
182 }
183
184 prevIdentStart = tok.identifier;
185 prevIdent = id;
186 }
187
188 if (!funcStart)
189 return AugmentSplitResult::FunctionNotFound;
190 if (!bodyStart)
191 return AugmentSplitResult::OpeningBraceNotFound;
192 if (!bodyEnd)
193 return AugmentSplitResult::ClosingBraceNotFound;
194
195 body = QByteArray(bodyStart, bodyEnd - bodyStart);
196 preamble = QByteArray(begin, funcStart - begin);
197 preamble += QByteArray(afterFunc, end - afterFunc);
198 return AugmentSplitResult::Ok;
199}
200
201QSSGRenderGraphObject *QQuick3DRenderPass::updateSpatialNode(QSSGRenderGraphObject *node)
202{
203 QSSGRenderUserPass *renderPassNode = static_cast<QSSGRenderUserPass *>(node);
204
205 if (!renderPassNode) {
206 renderPassNode = new QSSGRenderUserPass;
207 markAllDirty();
208 }
209
210 // The role follows the SubRenderPass reference count maintained by the
211 // commands referencing this pass. A role change rebuilds the command list,
212 // which marks the scene manager's pass set dirty, so synchronize()
213 // re-derives the classification-dependent state (nesting depth,
214 // declaration order) for the new role.
215 const bool isSubPass = (m_subRenderPassRef > 0);
216 if ((renderPassNode->role == QSSGRenderUserPass::Role::SubPass) != isSubPass)
217 m_dirtyAttributes |= Dirty::CommandsDirty;
218
219 renderPassNode->role = isSubPass ? QSSGRenderUserPass::Role::SubPass : QSSGRenderUserPass::Role::TopLevel;
220
221 auto &uniformProps = renderPassNode->shaderAugmentation.propertyUniforms;
222
223 if (m_dirtyAttributes & Dirty::TextureDirty) {
224 // Properties -> uniforms.
225 uniformProps = extractProperties();
226 static const QMetaMethod onResourceDirty = staticMetaObject.method(staticMetaObject.indexOfSlot("onShaderResourceDirty()"));
227 m_shaderResources.trackProperties(staticMetaObject.propertyCount(), onResourceDirty);
228 clearDirty(Dirty::TextureDirty);
229 // The images and buffers declared in the shader depend on the properties.
230 m_dirtyAttributes |= Dirty::AugmentShaderDirty;
231 }
232
233 if (m_dirtyAttributes & Dirty::CommandsDirty) {
234 renderPassNode->resetCommands();
235 clearDirty(Dirty::CommandsDirty);
236 for (QQuick3DShaderUtilsRenderCommand *command : std::as_const(m_commands)) {
237 if (auto *cmd = command->cloneCommand())
238 renderPassNode->commands.push_back(cmd);
239 else
240 markDirty(CommandsDirty, true); // Try again next time
241 }
242
243 // The command list drives the top-level/sub-pass classification (a pass
244 // referenced by a SubRenderPass command is a sub-pass). Rebuilding it may
245 // have changed that, so ask synchronize() to re-derive the classification.
246 if (QQuick3DSceneManager *sceneManager = QQuick3DObjectPrivate::get(this)->sceneManager)
247 sceneManager->userRenderPassesDirty = true;
248 }
249
250 // processMetadata() writes the shader's image and buffer declarations into
251 // the property values, so these are resolved again every time the shader
252 // is processed.
253 QByteArrayList metadata;
254 if (m_dirtyAttributes & Dirty::AugmentShaderDirty) {
255 clearDirty(Dirty::PropertyDirty);
256 const bool allHaveNodes = QSSGShaderUtils::resolveResourceProperties(this, uniformProps, metadata);
257 if (!allHaveNodes)
258 markDirty(Dirty::PropertyDirty, true); // Try again next time
259 } else if (m_dirtyAttributes & Dirty::PropertyDirty) {
260 clearDirty(Dirty::PropertyDirty);
261 if (!m_shaderResources.updateResources())
262 markDirty(Dirty::PropertyDirty, true); // Try again next time
263 QSSGShaderUtils::updateResourceProperties(this, uniformProps);
264 }
265
266 // Clear Dirty
267 if (m_dirtyAttributes & Dirty::ClearDirty) {
268 renderPassNode->renderTargetFlags = QRhiTextureRenderTarget::Flags(m_renderTargetFlags.toInt());
269 renderPassNode->clearColor = m_clearColor;
270 renderPassNode->depthStencilClearValue = { m_depthClearValue, m_stencilClearValue };
271
272 clearDirty(Dirty::ClearDirty);
273 }
274
275 if (m_dirtyAttributes & Dirty::PassTypeDirty) {
276 switch (m_passMode) {
277 case UserPass:
278 renderPassNode->passMode = QSSGRenderUserPass::UserPass;
279 break;
280 case SkyboxPass:
281 renderPassNode->passMode = QSSGRenderUserPass::SkyboxPass;
282 break;
283 case Item2DPass:
284 renderPassNode->passMode = QSSGRenderUserPass::Item2DPass;
285 break;
286 }
287
288 clearDirty(Dirty::PassTypeDirty);
289 }
290
291 // If not a user pass, we're done
292 if (m_passMode != UserPass)
293 return renderPassNode;
294
295 renderPassNode->materialMode = QSSGRenderUserPass::MaterialModes(m_materialMode);
296 clearDirty(Dirty::MaterialModeDirty);
297
298 if (renderPassNode->materialMode == QSSGRenderUserPass::OverrideMaterial) {
299 clearDirty(Dirty::OverrideMaterialDirty);
300 if (m_overrideMaterial) {
301 // Set the backend material
302 QSSGRenderGraphObject *graphObject = QQuick3DObjectPrivate::get(m_overrideMaterial)->spatialNode;
303 if (graphObject)
304 renderPassNode->overrideMaterial = graphObject;
305 else
306 markDirty(OverrideMaterialDirty, true); // Try again next time
307 } else {
308 // Set nullptr
309 renderPassNode->overrideMaterial = nullptr;
310 }
311 } else if (renderPassNode->materialMode == QSSGRenderUserPass::OriginalMaterial) {
312 // Nothing to do
313 } else if (renderPassNode->materialMode == QSSGRenderUserPass::AugmentMaterial) {
314 // Augment Shaders
315 if (m_dirtyAttributes & AugmentShaderDirty) {
316 QByteArray augmentSnippet;
317 QByteArray augmentPreamble;
318 if (!m_augmentShader.isEmpty()) {
319 const QQmlContext *context = qmlContext(this);
320 QByteArray shaderPathKey("augment material --");
321 QByteArray augment = QSSGShaderUtils::resolveShader(m_augmentShader, context, shaderPathKey);
322 QByteArrayList stageMetadata;
323 if (!processMetadata(uniformProps, metadata, stageMetadata, augment)) {
324 qWarning("RenderPass: Shader Image and/or Buffer processing failed.");
325 clearDirty(Dirty::AugmentShaderDirty);
326 return renderPassNode;
327 }
328
329 switch (QQuick3DRenderPass::splitAugmentShader(augment, augmentPreamble, augmentSnippet)) {
330 case AugmentSplitResult::Ok:
331 break;
332 case AugmentSplitResult::FunctionNotFound:
333 qWarning("QQuick3DRenderPass: Could not find MAIN_FRAGMENT_AUGMENT() function in shader %s", qPrintable(m_augmentShader.toString()));
334 break;
335 case AugmentSplitResult::OpeningBraceNotFound:
336 qWarning("QQuick3DRenderPass: Could not find the opening brace of MAIN_FRAGMENT_AUGMENT() in shader %s", qPrintable(m_augmentShader.toString()));
337 break;
338 case AugmentSplitResult::ClosingBraceNotFound:
339 qWarning("QQuick3DRenderPass: Could not find the closing brace of MAIN_FRAGMENT_AUGMENT() in shader %s", qPrintable(m_augmentShader.toString()));
340 break;
341 }
342 }
343
344 renderPassNode->shaderAugmentation.body = augmentSnippet;
345 renderPassNode->shaderAugmentation.preamble = augmentPreamble;
346 renderPassNode->markDirty(QSSGRenderUserPass::DirtyFlag::ShaderDirty);
347 }
348 clearDirty(Dirty::AugmentShaderDirty);
349 }
350
351 return renderPassNode;
352}
353
354void QQuick3DRenderPass::itemChange(ItemChange change, const ItemChangeData &value)
355{
356 if (change == QQuick3DObject::ItemSceneChange) {
357 updateSceneManager(value.sceneManager);
358 } else if (change == QQuick3DObject::ItemParentHasChanged) {
359 // The parent determines the pass's validity and nesting depth, both
360 // derived in synchronize(). A same-manager reparent does not travel
361 // through pass registration or removal, so mark the set dirty here.
362 if (QQuick3DSceneManager *sceneManager = QQuick3DObjectPrivate::get(this)->sceneManager)
363 sceneManager->userRenderPassesDirty = true;
364 }
365}
366
367void QQuick3DRenderPass::markTrackedPropertyDirty(QMetaProperty property, DirtyPropertyHint hint)
368{
369 Q_UNUSED(property);
370
371 // FIXME: As with the property tracking for Effects and Custom materials we
372 // should really track which property changed and only update that one.
373 if (hint == DirtyPropertyHint::Reference) {
374 // FIXME: We should verify that the property is actually a texture property.
375 markDirty(Dirty::TextureDirty);
376 } else {
377 markDirty(Dirty::PropertyDirty);
378 }
379}
380
381void QQuick3DRenderPass::onMaterialDestroyed(QObject *object)
382{
383 if (m_overrideMaterial == object) {
384 m_overrideMaterial = nullptr;
385 emit overrideMaterialChanged();
386 markDirty(OverrideMaterialDirty);
387 }
388}
389
390void QQuick3DRenderPass::onCommandChanged()
391{
392 markDirty(CommandsDirty);
393}
394
395void QQuick3DRenderPass::qmlAppendCommand(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, QQuick3DShaderUtilsRenderCommand *command)
396{
397 if (!command)
398 return;
399
400 QQuick3DRenderPass *that = qobject_cast<QQuick3DRenderPass *>(list->object);
401
402 if (!command->parentItem())
403 command->setParentItem(that);
404
405 that->m_commands.push_back(command);
406 that->markDirty(CommandsDirty);
407
408 // Re-clone commands whenever a property changes so the render thread
409 // sees the updated state.
410 QObject::connect(command, &QQuick3DShaderUtilsRenderCommand::changed, that, &QQuick3DRenderPass::onCommandChanged);
411}
412
413QQuick3DShaderUtilsRenderCommand *QQuick3DRenderPass::qmlCommandAt(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, qsizetype index)
414{
415 QQuick3DRenderPass *that = qobject_cast<QQuick3DRenderPass *>(list->object);
416 return that->m_commands.at(index);
417}
418
419qsizetype QQuick3DRenderPass::qmlCommandCount(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
420{
421 QQuick3DRenderPass *that = qobject_cast<QQuick3DRenderPass *>(list->object);
422 return that->m_commands.size();
423}
424
425void QQuick3DRenderPass::qmlCommandClear(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
426{
427 QQuick3DRenderPass *that = qobject_cast<QQuick3DRenderPass *>(list->object);
428 for (QQuick3DShaderUtilsRenderCommand *cmd : std::as_const(that->m_commands))
429 QObject::disconnect(cmd, nullptr, that, nullptr);
430 that->m_commands.clear();
431 that->markDirty(CommandsDirty);
432}
433
434void QQuick3DRenderPass::updateSceneManager(QQuick3DSceneManager *sceneManager)
435{
436 m_shaderResources.updateSceneManager(sceneManager);
437 updateTrackedSceneManager(sceneManager);
438 if (sceneManager) {
439 // Handle inline override material that may not have had a scene manager when it was set
440 if (m_overrideMaterial && !m_overrideMaterial->parentItem() && !QQuick3DObjectPrivate::get(m_overrideMaterial)->sceneManager) {
441 if (!m_overrideMaterialRefed) {
442 QQuick3DObjectPrivate::refSceneManager(m_overrideMaterial, *sceneManager);
443 m_overrideMaterialRefed = true;
444 }
445 }
446 } else {
447 // Deref the material when scene manager is removed
448 if (m_overrideMaterial && m_overrideMaterialRefed) {
449 QQuick3DObjectPrivate::derefSceneManager(m_overrideMaterial);
450 m_overrideMaterialRefed = false;
451 }
452 }
453}
454
455void QQuick3DRenderPass::onShaderResourceDirty()
456{
457 // Swapping one object for another only changes the binding, but a property
458 // gaining or losing its object, or an image of another format, changes the
459 // generated shader.
460 if (m_shaderResources.hasResourceLayoutChanged())
461 markResourcesDirty();
462 else
463 markDirty(Dirty::PropertyDirty);
464}
465
466void QQuick3DRenderPass::markResourcesDirty()
467{
468 // extractProperties() only re-reads dirty properties, and one that was
469 // null at the first extraction has no watcher of its own.
470 for (int pid : m_shaderResources.resourceProperties())
471 addDirtyProperty(pid);
472 markDirty(Dirty::TextureDirty);
473}
474
475void QQuick3DRenderPass::markDirty(Dirty type, bool requestSecondaryUpdate)
476{
477 if (!(m_dirtyAttributes & quint32(type))) {
478 m_dirtyAttributes |= quint32(type);
479 update();
480 }
481
482 if (requestSecondaryUpdate)
483 QQuick3DObjectPrivate::get(this)->requestSecondaryUpdate();
484}
485
486void QQuick3DRenderPass::clearDirty(Dirty type)
487{
488 m_dirtyAttributes &= ~quint32(type);
489}
490
491/*!
492 \qmlproperty list<RenderCommand> RenderPass::commands
493 This property holds the list of render commands for the render pass.
494
495 The commands in the list are executed in the order they appear in the list.
496
497 \note The commands for RenderPass and Effects are similar but not the same, only
498 those marked as compatible can be used with this RenderPass.
499
500 \sa SubRenderPass,
501 PipelineStateOverride,
502 RenderablesFilter,
503 RenderPassTexture,
504 ColorAttachment,
505 DepthTextureAttachment,
506 DepthStencilAttachment,
507 AddDefine,
508 renderTargetBlend
509*/
510
511QQmlListProperty<QQuick3DShaderUtilsRenderCommand> QQuick3DRenderPass::commands()
512{
513 return QQmlListProperty<QQuick3DShaderUtilsRenderCommand>(this,
514 nullptr,
515 QQuick3DRenderPass::qmlAppendCommand,
516 QQuick3DRenderPass::qmlCommandCount,
517 QQuick3DRenderPass::qmlCommandAt,
518 QQuick3DRenderPass::qmlCommandClear);
519}
520
521/*!
522 \qmlproperty color RenderPass::clearColor
523 This property holds the clear color for the render pass.
524
525 \default Qt.black
526*/
527QColor QQuick3DRenderPass::clearColor() const
528{
529 return m_clearColor;
530}
531
532void QQuick3DRenderPass::setClearColor(const QColor &newClearColor)
533{
534 if (m_clearColor == newClearColor)
535 return;
536 m_clearColor = newClearColor;
537 emit clearColorChanged();
538 markDirty(ClearDirty);
539}
540
541/*!
542 \qmlproperty RenderPass::MaterialModes RenderPass::materialMode
543 Controls how object materials are handled when rendering into this pass.
544
545 \value RenderPass.OriginalMaterial
546 Objects are rendered using their own assigned materials, with full
547 lighting, textures, and material properties applied normally. This is
548 the standard mode for rendering a faithful copy of the scene into a
549 custom render target — for example, a secondary viewpoint for a
550 reflection probe, a rear-view camera, or a picture-in-picture effect.
551 The \c overrideMaterial, \c augmentShader, and \c shaders properties
552 are not used in this mode.
553
554 \value RenderPass.AugmentMaterial
555 Each object is rendered with its own material, but the contents of
556 the \c {MAIN_FRAGMENT_AUGMENT()} function defined in \l augmentShader
557 are injected after the original material's output definition. This
558 allows the augment code to read the material's computed color and write
559 to additional color outputs defined by \l ColorAttachment commands in
560 the pass. This is useful for multi-render-target (MRT) passes that need
561 per-material shading, such as writing the lit color to one attachment
562 and a world-space normal to another in a single draw call.
563
564 \value RenderPass.OverrideMaterial
565 All objects rendered by this pass use the single \l overrideMaterial
566 instead of their own. This is useful for depth-only passes, shadow maps,
567 silhouette or outline effects, and any other case where you want all
568 geometry to be shaded identically regardless of what material is
569 assigned to it. The \c augmentShader property is not used in this mode.
570
571 \default RenderPass.OriginalMaterial
572*/
573QQuick3DRenderPass::MaterialModes QQuick3DRenderPass::materialMode() const
574{
575 return m_materialMode;
576}
577
578void QQuick3DRenderPass::setMaterialMode(MaterialModes newMaterialMode)
579{
580 if (m_materialMode == newMaterialMode)
581 return;
582 m_materialMode = newMaterialMode;
583 emit materialModeChanged();
584 markDirty(MaterialModeDirty);
585}
586
587/*!
588 \qmlproperty Material RenderPass::overrideMaterial
589 This property holds the override material for the render pass when
590 \l{RenderPass::materialMode}{materialMode} is set to \c OverrideMaterial.
591*/
592QQuick3DMaterial *QQuick3DRenderPass::overrideMaterial() const
593{
594 return m_overrideMaterial;
595}
596
597void QQuick3DRenderPass::setOverrideMaterial(QQuick3DMaterial *newOverrideMaterial)
598{
599 if (m_overrideMaterial == newOverrideMaterial)
600 return;
601
602 // Deref the old material if we had ref'd it
603 if (m_overrideMaterial && m_overrideMaterialRefed) {
604 QQuick3DObjectPrivate::derefSceneManager(m_overrideMaterial);
605 m_overrideMaterialRefed = false;
606 }
607
608 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DRenderPass::setOverrideMaterial, newOverrideMaterial, m_overrideMaterial);
609
610 m_overrideMaterial = newOverrideMaterial;
611
612 // Handle inline material declarations by ensuring they get registered with the scene manager
613 if (m_overrideMaterial && m_overrideMaterial->parentItem() == nullptr) {
614 // If the material has no parent, check if it has a hierarchical parent that's a QQuick3DObject
615 // and re-parent it to that, e.g., inline materials
616 QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(m_overrideMaterial->parent());
617 if (parentItem) {
618 m_overrideMaterial->setParentItem(parentItem);
619 } else {
620 // If no valid parent was found, make sure the material refs our scene manager
621 const auto &sceneManager = QQuick3DObjectPrivate::get(this)->sceneManager;
622 if (sceneManager) {
623 QQuick3DObjectPrivate::refSceneManager(m_overrideMaterial, *sceneManager);
624 m_overrideMaterialRefed = true;
625 }
626 // else: If there's no scene manager, defer until one is set, see itemChange()
627 }
628 }
629
630 emit overrideMaterialChanged();
631 markDirty(OverrideMaterialDirty);
632}
633
634/*!
635 \qmlproperty url RenderPass::augmentShader
636 This property holds the augment shader URL for the render pass when
637 \l{RenderPass::materialMode}{materialMode} is set to \c AugmentMaterial.
638
639 The shader file should contain a function with the following signature:
640 \badcode
641 void MAIN_FRAGMENT_AUGMENT() {
642 // Custom shader code here
643 }
644 \endcode
645
646 This function will be combined with the existing fragment shader of the material
647 being used by the object being rendered in this render pass. Allowing users to
648 augment the existing material shader with custom code.
649*/
650QUrl QQuick3DRenderPass::augmentShader() const
651{
652 return m_augmentShader;
653}
654
655void QQuick3DRenderPass::setAugmentShader(const QUrl &newAugmentShader)
656{
657 if (m_augmentShader == newAugmentShader)
658 return;
659 m_augmentShader = newAugmentShader;
660 emit augmentShaderChanged();
661 markDirty(AugmentShaderDirty);
662}
663
664/*!
665 \qmlproperty RenderPass::PassMode RenderPass::passMode
666 This property holds the pass mode for the render pass.
667
668 In addition to standard user render passes, Qt Quick 3D supports
669 users to manually triggering internal render passes for rendering
670 the skybox and 2D items.
671
672 \value RenderPass.UserPass A user specified render pass.
673 \value RenderPass.SkyboxPass Qt Quick 3D's built-in skybox render pass.
674 \value RenderPass.Item2DPass Qt Quick 3D's built-in 2D item render pass.
675 \default RenderPass.UserPass
676*/
677
678QQuick3DRenderPass::PassMode QQuick3DRenderPass::passMode() const
679{
680 return m_passMode;
681}
682
683void QQuick3DRenderPass::setPassMode(PassMode newPassMode)
684{
685 if (m_passMode == newPassMode)
686 return;
687 m_passMode = newPassMode;
688 emit passModeChanged();
689 markDirty(PassTypeDirty);
690}
691
692/*!
693 \qmlproperty real RenderPass::depthClearValue
694 This property holds the depth clear value for the render pass.
695
696 \default 1.0
697*/
698float QQuick3DRenderPass::depthClearValue() const
699{
700 return m_depthClearValue;
701}
702
703void QQuick3DRenderPass::setDepthClearValue(float newDepthClearValue)
704{
705 if (qFuzzyCompare(m_depthClearValue, newDepthClearValue))
706 return;
707 m_depthClearValue = newDepthClearValue;
708 emit depthClearValueChanged();
709 markDirty(ClearDirty);
710}
711
712/*!
713 \qmlproperty int RenderPass::stencilClearValue
714 This property holds the stencil clear value for the render pass.
715
716 \default 0
717*/
718quint32 QQuick3DRenderPass::stencilClearValue() const
719{
720 return m_stencilClearValue;
721}
722
723void QQuick3DRenderPass::setStencilClearValue(quint32 newStencilClearValue)
724{
725 if (m_stencilClearValue == newStencilClearValue)
726 return;
727 m_stencilClearValue = newStencilClearValue;
728 emit stencilClearValueChanged();
729 markDirty(ClearDirty);
730}
731
732/*!
733 \qmlproperty RenderPass::RenderTargetFlags RenderPass::renderTargetFlags
734 This property holds the render target flags for the render pass. These flags affect how
735 the render target contents are handled at the beginning and end of each frame.
736
737 \value RenderPass.None No special behavior. Color and depth/stencil contents are cleared at the start of each frame.
738 \value RenderPass.PreserveColorContents Preserve the color contents of the render target between frames, so the previous frame's output remains until explicitly overwritten.
739 \value RenderPass.PreserveDepthStencilContents Preserve the depth and stencil contents of the render target between frames.
740 \value RenderPass.DoNotStoreDepthStencilContents Do not store the depth and stencil contents of the render target after rendering (may improve performance on tiled GPUs).
741
742 \default RenderPass.None
743
744 \sa QRhiTextureRenderTarget::Flags
745*/
746
747QQuick3DRenderPass::RenderTargetFlags QQuick3DRenderPass::renderTargetFlags() const
748{
749 return m_renderTargetFlags;
750}
751
752void QQuick3DRenderPass::setRenderTargetFlags(RenderTargetFlags newRenderTargetFlags)
753{
754 if (m_renderTargetFlags == newRenderTargetFlags)
755 return;
756 m_renderTargetFlags = newRenderTargetFlags;
757 emit renderTargetFlagsChanged();
758 markDirty(ClearDirty);
759}
760
761// A reference change can flip the pass's role, so the pass is updated to
762// mirror the new role to its backend node, and the scene manager's pass set is
763// marked dirty so the classification-dependent state (nesting depth,
764// declaration order) is re-derived. The scene manager may be null when the
765// pass has not entered a scene yet; registration sets the flag then.
766static void notifySubPassRefChange(QQuick3DRenderPass *pass)
767{
768 pass->update();
769 if (QQuick3DSceneManager *sceneManager = QQuick3DObjectPrivate::get(pass)->sceneManager)
770 sceneManager->userRenderPassesDirty = true;
771}
772
773void QQuick3DRenderPassPrivateHelper::subPassRef(QQuick3DRenderPass *pass)
774{
775 Q_ASSERT(pass);
776 ++pass->m_subRenderPassRef;
778}
779
780void QQuick3DRenderPassPrivateHelper::subPassDeref(QQuick3DRenderPass *pass)
781{
782 Q_ASSERT(pass);
783 if (QSSG_GUARD(pass->m_subRenderPassRef > 0))
784 --pass->m_subRenderPassRef;
786}
787
788QT_END_NAMESPACE
void subPassDeref(QQuick3DRenderPass *pass)
void subPassRef(QQuick3DRenderPass *pass)
Combined button and popup list for selecting options.
static void notifySubPassRefChange(QQuick3DRenderPass *pass)