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
qquick3dcomputepass.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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#include <QtQuick3D/private/qquick3dshaderstoragebuffer_p.h>
6#include <QtQuick3D/private/qquick3dshaderimage_p.h>
7#include <QtQuick3D/private/qquick3dcomputepass_p.h>
8
9#include <QtQuick3DRuntimeRender/private/qssgrendercomputepass_p.h>
10#include <QtQuick3DRuntimeRender/private/qssgrenderbuffer_p.h>
11#include <QtQuick3DRuntimeRender/private/qssgshadermaterialadapter_p.h>
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \qmltype ComputePass
17 \inherits Object3D
18 \inqmlmodule QtQuick3D
19 \brief Defines a custom compute shader pass.
20 \since 6.13
21
22 A ComputePass runs a GLSL compute shader as part of the 3D scene's
23 rendering pipeline. Unlike a \l RenderPass or an \l Effect, a
24 ComputePass does not render any geometry itself; it is used to prepare
25 or process data that is later consumed by a material, an \l Effect, or
26 another ComputePass -- typically data written into a \l ShaderImage or
27 a \l ShaderStorageBuffer.
28
29 A ComputePass becomes active for a scene when it is placed as a child
30 of a \l View3D, a \l Node, or another pass. Within a single frame, a
31 ComputePass by default executes after the scene's internal preparation
32 passes, which produce the depth, normal, and ambient occlusion textures,
33 the shadow maps, the screen texture, the reflection probes, and the
34 textures of texture provider extensions, and before the scene's main
35 pass. Its results are therefore visible to the main pass and the effects
36 of the same frame, but not to those preparation passes. See
37 \l executionStage for executing the pass before them. By default the pass
38 executes in every rendered frame; see \l executionMode and
39 \l requestExecution() for executing it only when needed.
40
41 \note If the results of a ComputePass in the default stage change what the
42 preparation passes render, for example a buffer that a \l CustomMaterial
43 uses to displace vertices, those passes use the results of the previous
44 frame: the shadow maps, \c DEPTH_TEXTURE, \c SCREEN_TEXTURE, the ambient
45 occlusion, and the reflection probes are one frame behind the main
46 pass, and see uninitialized content in the first frame. Set
47 \l executionStage to \c ComputePass.StartOfFrame to avoid this.
48
49 The order against other passes, \l RenderPass and ComputePass alike, is
50 expressed by nesting: a nested pass runs before its parent, so a pass
51 that produces data for another one is declared inside its consumer. The
52 relative order of passes at the same nesting depth is not defined. See
53 \l{Pass Ordering} for the details.
54
55 \note A ComputePass requires a graphics API with compute support:
56 OpenGL 4.3 or newer, OpenGL ES 3.1 or newer, Vulkan, Metal, or Direct3D.
57 On a backend without compute support, Qt Quick 3D skips the pass
58 and issues a warning.
59
60 \note OpenGL ES 3.1 only guarantees four shader storage buffers and four
61 images per compute shader. Using the \l{Lights}{light data} takes two of
62 those storage buffers. \l ShaderImage also has format and memory
63 qualifier restrictions on OpenGL ES; see \l{ShaderImage::format}.
64
65 The following example dispatches a compute shader that fills a
66 \l ShaderImage with a solid color, one invocation per texel:
67
68 \qml
69 import QtQuick3D
70
71 View3D {
72 ShaderImage {
73 id: myImage
74 width: 256
75 height: 256
76 }
77
78 ComputePass {
79 computeShader: "fill.comp"
80 property ShaderImage targetImage: myImage
81 commands: [
82 Dispatch {
83 groupCountX: (myImage.width + 7) / 8
84 groupCountY: (myImage.height + 7) / 8
85 }
86 ]
87 }
88 }
89 \endqml
90
91 \section1 Exposing data to the shader
92
93 As with \l RenderPass, \l Effect, and \l CustomMaterial, any QML property declared
94 on a ComputePass is automatically exposed as a uniform to its compute shader.
95 Properties of type \l ShaderImage are exposed to
96 the shader as a GLSL image, declared with the \c SHADER_IMAGE macro, and
97 properties of type \l ShaderStorageBuffer as a shader storage buffer,
98 declared with the \c STORAGE_BUFFER macro.
99
100 \section1 Built-in uniforms
101
102 The compute shader can also use the following built-in uniforms without
103 a matching QML property. Referencing an identifier in the shader source
104 binds the corresponding uniform automatically, as in \l CustomMaterial
105 and \l Effect shaders:
106
107 \list
108 \li \c VIEW_MATRIX -> mat4, the view (camera) matrix.
109 \li \c INVERSE_VIEW_MATRIX -> mat4, the inverse of the view matrix.
110 \li \c PROJECTION_MATRIX -> mat4, the projection matrix.
111 \li \c INVERSE_PROJECTION_MATRIX -> mat4, the inverse of the projection matrix.
112 \li \c VIEWPROJECTION_MATRIX -> mat4, the combined view-projection matrix.
113 \li \c INVERSE_VIEWPROJECTION_MATRIX -> mat4, the inverse of the view-projection matrix.
114 \li \c CAMERA_POSITION -> vec3, the camera position in world space.
115 \li \c CAMERA_DIRECTION -> vec3, the camera direction vector.
116 \li \c CAMERA_PROPERTIES -> vec3, the near and far clip distances for the camera, in \c x
117 and \c y (\c z is unused).
118 \li \c FRAMEBUFFER_Y_UP -> float, \c{1.0} or \c{-1.0} depending on whether the graphics API's
119 framebuffer coordinate system has Y pointing up.
120 \li \c NDC_Y_UP -> float, \c{1.0} or \c{-1.0} depending on whether the graphics API's
121 normalized device coordinate system has Y pointing up.
122 \li \c NEAR_CLIP_VALUE -> float, \c{0.0} or \c{-1.0} depending on whether the graphics API's
123 clip space near plane is at \c{0.0} or \c{-1.0}.
124 \li \c FRAME -> float, a frame counter, incremented after each rendered frame in the
125 View3D.
126 \endlist
127
128 \note With \l{Multiview Rendering}{multiview rendering} active, the camera matrix, position,
129 and direction uniforms above become arrays. \c CAMERA_PROPERTIES, \c FRAMEBUFFER_Y_UP,
130 \c NDC_Y_UP, \c NEAR_CLIP_VALUE, and \c FRAME do not. See
131 \l{ComputePass and multiview rendering}.
132
133 \section1 Screen, depth, and other textures
134
135 As with \l CustomMaterial and \l Effect, referencing one of the following identifiers causes
136 the corresponding texture to be prepared and bound automatically:
137
138 \table
139 \header
140 \li Identifier
141 \li Type
142 \li Description
143 \row
144 \li \c SCREEN_TEXTURE, \c SCREEN_MIP_TEXTURE
145 \li \c sampler2D
146 \li A copy of the scene's opaque color buffer, captured in a dedicated
147 pass that runs before the ComputePass itself. Because a ComputePass
148 runs before the scene's main pass (see above), this does
149 \e{not} include transparent objects, the skybox, or anything
150 drawn by an Effect.
151 Not available in the \c ComputePass.StartOfFrame
152 \l executionStage.
153 \row
154 \li \c DEPTH_TEXTURE
155 \li \c sampler2D
156 \li The scene's opaque depth buffer, subject to the same opaque-only
157 caveat as \c SCREEN_TEXTURE.
158 \row
159 \li \c AO_TEXTURE
160 \li \c sampler2D
161 \li The ambient occlusion buffer. Referencing this identifier enables
162 the SSAO pass for the layer even if
163 \l{SceneEnvironment::aoEnabled}{aoEnabled} is \c false.
164 \row
165 \li \c NORMAL_ROUGHNESS_TEXTURE
166 \li \c sampler2D
167 \li A texture with the world-space normals (rgb) and roughness (a) of
168 the opaque objects in the scene. Always a plain \c sampler2D, even
169 with multiview rendering active.
170 \endtable
171
172 \section1 Lights
173
174 A ComputePass exposes light data differently from \l CustomMaterial and
175 \l Effect. A CustomMaterial exposes light data through per-light
176 callback functions, such as \c DIRECTIONAL_LIGHT(), which the engine
177 calls once for each active light. An Effect has no lighting model. A
178 compute shader has no current fragment to shade, so a ComputePass
179 provides accessor functions that take a light index, and the shader
180 loops over the lights itself:
181
182 \table
183 \header
184 \li Identifier
185 \li Type
186 \li Description
187 \row
188 \li \c DIRECTIONAL_LIGHT_COUNT
189 \li \c uint
190 \li The number of active directional lights.
191 \row
192 \li \c{DIRECTIONAL_LIGHT_DIFFUSE(index)}
193 \li \c vec3
194 \li The light's diffuse color.
195 \row
196 \li \c{DIRECTIONAL_LIGHT_DIRECTION(index)}
197 \li \c vec3
198 \li The light's direction.
199 \row
200 \li \c POINT_LIGHT_COUNT
201 \li \c uint
202 \li The number of active point lights.
203 \row
204 \li \c{POINT_LIGHT_DIFFUSE(index)}
205 \li \c vec3
206 \li The light's diffuse color.
207 \row
208 \li \c{POINT_LIGHT_POSITION(index)}
209 \li \c vec3
210 \li The light's world-space position.
211 \row
212 \li \c{POINT_LIGHT_ATTENUATION(index)}
213 \li \c vec3
214 \li The constant, linear, and quadratic attenuation factors, in \c x,
215 \c y, and \c z.
216 \row
217 \li \c SPOT_LIGHT_COUNT
218 \li \c uint
219 \li The number of active spot lights.
220 \row
221 \li \c{SPOT_LIGHT_DIFFUSE(index)}
222 \li \c vec3
223 \li The light's diffuse color.
224 \row
225 \li \c{SPOT_LIGHT_POSITION(index)}
226 \li \c vec3
227 \li The light's world-space position.
228 \row
229 \li \c{SPOT_LIGHT_ATTENUATION(index)}
230 \li \c vec3
231 \li The constant, linear, and quadratic attenuation factors, in \c x,
232 \c y, and \c z.
233 \row
234 \li \c{SPOT_LIGHT_DIRECTION(index)}
235 \li \c vec3
236 \li The light's direction.
237 \row
238 \li \c{SPOT_LIGHT_CONES(index)}
239 \li \c vec2
240 \li The cosines of the inner and outer cone angles, in \c x and \c y.
241 \endtable
242
243 \c index is a \c uint and is zero-based per light type, so a spot light's index does not
244 depend on how many point lights the scene has.
245
246 Referencing any of these identifiers makes all of them available to the shader.
247
248 \note There is no separate intensity or brightness identifier. The colors are linear (not
249 sRGB) and have \l{Light::brightness}{brightness} premultiplied into them already.
250
251 Directional lights and positional (point and spot) lights are passed in two separate
252 fixed-size shader storage buffers, each holding 16 KB of light data. The resulting
253 limits are available to the shader as the compile-time constants
254 \c QSSG_MAX_DIRECTIONAL_LIGHTS and \c QSSG_MAX_POSITIONAL_LIGHTS, the latter being a limit
255 on the number of point and spot lights \e{combined}. Lights beyond these limits are ignored,
256 with a warning.
257
258 The two light buffers use shader storage buffer bindings of their own, so they count
259 toward the device's storage buffer limit. OpenGL ES 3.1 only guarantees four per compute
260 shader, which leaves two for \l ShaderStorageBuffer properties when the light data is used.
261
262 \section1 Shadows
263
264 If a light with \l{Light::castsShadow}{castsShadow} enabled is present, its shadow map can be
265 sampled with one of the following macros, each returning an illumination factor between
266 \c{0.0} (fully shadowed) and \c{1.0} (fully lit):
267
268 \list
269 \li \c{DIRECTIONAL_LIGHT_SHADOW(index, viewSpaceDepth, worldPosition)} -- for the directional
270 light at the same \c index the \c DIRECTIONAL_LIGHT_* accessors above take.
271 \c viewSpaceDepth (the world position transformed by \c VIEW_MATRIX, \c{.z}) selects the
272 correct cascade.
273 \li \c{POINT_LIGHT_SHADOW(index, worldPosition)} -- for the point light at \c index.
274 \li \c{SPOT_LIGHT_SHADOW(index, worldPosition)} -- for the spot light at \c index.
275 \endlist
276
277 \c QSSG_SHADOW_SOFTNESS is a compile-time constant (not a uniform), reflecting the softest
278 \l{Light::softShadowQuality}{softShadowQuality} among the shadow-casting lights in the scene
279 (\c{0}, \c{4}, \c{8}, or \c{16}).
280
281 \note The \c QSSG_ prefix marks \c QSSG_MAX_DIRECTIONAL_LIGHTS, \c QSSG_MAX_POSITIONAL_LIGHTS,
282 and \c QSSG_SHADOW_SOFTNESS as reflecting the renderer's internals rather than a stable API.
283 Their values, and the constants themselves, may change in future Qt versions.
284
285 Referencing a \c{_SHADOW} macro also makes the light identifiers from the previous section
286 available, so a shader that only samples shadows does not need to reference any of them.
287
288 \section1 ComputePass and multiview rendering
289
290 Multiview rendering is relevant to a compute shader when it is enabled and the compute shader
291 uses any screen textures (\c SCREEN_TEXTURE, \c SCREEN_MIP_TEXTURE, \c DEPTH_TEXTURE, and
292 \c AO_TEXTURE) or camera uniforms (any VIEW or PROJECTION matrices, camera position and direction,
293 but not properties).
294
295 To tell Qt Quick 3D that the shader supports multiview rendering,
296 reference \c QSHADER_VIEW_COUNT in the shader. If it is not referenced, a
297 warning is issued.
298
299 When multiview is enabled, all of the built-in camera
300 uniforms listed above, except \c CAMERA_PROPERTIES, become arrays of size
301 \c QSHADER_VIEW_COUNT, and \c SCREEN_TEXTURE, \c SCREEN_MIP_TEXTURE, \c DEPTH_TEXTURE, and
302 \c AO_TEXTURE become \c sampler2DArray instead of \c sampler2D, one layer per eye.
303 \c NORMAL_ROUGHNESS_TEXTURE always stays a plain \c sampler2D, even with multiview active.
304
305 A ComputePass has no built-in \c VIEW_INDEX, unlike the fragment shader
306 of a \l CustomMaterial or an \l Effect. In those shaders, \c VIEW_INDEX
307 derives from the vertex stage's \c gl_ViewIndex, and compute has no
308 equivalent. Qt Quick 3D dispatches a ComputePass exactly once,
309 regardless of the view count. To produce per-eye results under
310 multiview rendering, the compute shader must derive the eye index
311 itself, for example from one dispatch dimension. It then writes to a
312 \l ShaderImage with \l{ShaderImage::arraySize}{arraySize} set to the
313 view count, declared as \c image2DArray in the shader.
314
315 \sa ShaderImage, ShaderStorageBuffer, Dispatch, Effect, CustomMaterial
316*/
317
318QQuick3DComputePass::QQuick3DComputePass(QQuick3DObject *parent)
319 : QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::ComputePass, QQuick3DObjectPrivate::Flags::RequiresSecondaryUpdate)), parent)
320 , QQuick3DPropertyChangedTracker(this, QQuick3DSuperClassInfo<QQuick3DComputePass>())
321{
322}
323
324QSSGRenderGraphObject *QQuick3DComputePass::updateSpatialNode(QSSGRenderGraphObject *node)
325{
326 QSSGRenderComputePass *computePassNode = static_cast<QSSGRenderComputePass *>(node);
327
328 if (!computePassNode) {
329 computePassNode = new QSSGRenderComputePass;
330 markAllDirty();
331 }
332
333 computePassNode->executionMode = QSSGRenderComputePass::ExecutionMode(m_executionMode);
334 computePassNode->executionSerial = m_executionSerial;
335 clearDirty(Dirty::StateDirty);
336
337 auto &uniformProps = computePassNode->computeShader.propertyUniforms;
338
339 if (m_dirtyAttributes & Dirty::ResourceDirty) {
340 // Properties -> uniforms.
341 // NOTE: Calling extractProperties clears existing properties
342 uniformProps = extractProperties();
343 static const QMetaMethod onResourceDirty = staticMetaObject.method(staticMetaObject.indexOfSlot("onShaderResourceDirty()"));
344 m_shaderResources.trackProperties(staticMetaObject.propertyCount(), onResourceDirty);
345 clearDirty(Dirty::ResourceDirty);
346 // The images and buffers declared in the shader depend on the properties.
347 m_dirtyAttributes |= Dirty::ComputeShaderDirty;
348 }
349
350 if (m_dirtyAttributes & Dirty::CommandsDirty) {
351 computePassNode->resetCommands();
352 for (QQuick3DShaderUtilsRenderCommand *command : std::as_const(m_commands))
353 computePassNode->commands.append(command->cloneCommand());
354 clearDirty(Dirty::CommandsDirty);
355 computePassNode->clearWarnings();
356 }
357
358 if (m_dirtyAttributes & Dirty::ComputeShaderDirty) {
359 clearDirty(Dirty(Dirty::ComputeShaderDirty | Dirty::PropertyDirty));
360 // processMetadata() writes the shader's image and buffer declarations
361 // into the property values, so these are resolved again every time the
362 // shader is processed.
363 QByteArrayList metadata;
364 const bool allHaveNodes = QSSGShaderUtils::resolveResourceProperties(this, uniformProps, metadata);
365 if (!allHaveNodes)
366 markDirty(Dirty::PropertyDirty, true); // Try again next time
367
368 if (!m_computeShader.isEmpty()) {
369 const QQmlContext *context = qmlContext(this);
370 QByteArray shaderPathKey("compute shader --");
371 QByteArray shaderData = QSSGShaderUtils::resolveShader(m_computeShader, context, shaderPathKey);
372
373 computePassNode->computeShader.metadata.clear();
374 if (!processMetadata(uniformProps, metadata, computePassNode->computeShader.metadata, shaderData)) {
375 qWarning("ComputePass: Shader Image and/or Buffer processing failed in compute shader.");
376 // The properties now refer to the new resources, which the
377 // pipeline built from the old body may not match, so stop
378 // dispatching until the shader can be processed again.
379 computePassNode->computeShader.body = {};
380 computePassNode->markDirty(QSSGRenderComputePass::DirtyFlag::ShaderDirty);
381 return computePassNode;
382 }
383
384 computePassNode->computeShader.body = shaderData;
385 } else {
386 computePassNode->computeShader.body = {};
387 computePassNode->computeShader.metadata.clear();
388 }
389 computePassNode->markDirty(QSSGRenderComputePass::DirtyFlag::ShaderDirty);
390 computePassNode->clearWarnings();
391 } else if (m_dirtyAttributes & Dirty::PropertyDirty) {
392 clearDirty(Dirty::PropertyDirty);
393 if (!m_shaderResources.updateResources())
394 markDirty(Dirty::PropertyDirty, true); // Try again next time
395 QSSGShaderUtils::updateResourceProperties(this, uniformProps);
396 }
397 return computePassNode;
398}
399
400void QQuick3DComputePass::itemChange(ItemChange change, const ItemChangeData &value)
401{
402 if (change == QQuick3DObject::ItemSceneChange) {
403 m_shaderResources.updateSceneManager(value.sceneManager);
404 updateTrackedSceneManager(value.sceneManager);
405 } else if (change == QQuick3DObject::ItemParentHasChanged) {
406 // The parent determines the pass's validity and nesting depth, both
407 // derived in synchronize(). A same-manager reparent does not travel
408 // through pass registration or removal, so mark the set dirty here.
409 if (QQuick3DSceneManager *sceneManager = QQuick3DObjectPrivate::get(this)->sceneManager)
410 sceneManager->userRenderPassesDirty = true;
411 }
412}
413
414void QQuick3DComputePass::markTrackedPropertyDirty(QMetaProperty property, DirtyPropertyHint hint)
415{
416 Q_UNUSED(property);
417
418 // FIXME: As with the property tracking for Effects and Custom materials we
419 // should really track which property changed and only update that one.
420 if (hint == DirtyPropertyHint::Reference) {
421 // FIXME: We should verify that the property is actually a texture property.
422 markDirty(Dirty::ResourceDirty);
423 } else {
424 markDirty(Dirty::PropertyDirty);
425 }
426}
427
428void QQuick3DComputePass::onShaderResourceDirty()
429{
430 // Swapping one object for another only changes the binding, but a property
431 // gaining or losing its object, or an image of another format, changes the
432 // generated shader.
433 if (m_shaderResources.hasResourceLayoutChanged())
434 markResourcesDirty();
435 else
436 markDirty(Dirty::PropertyDirty);
437}
438
439void QQuick3DComputePass::markResourcesDirty()
440{
441 // extractProperties() only re-reads dirty properties, and one that was
442 // null at the first extraction has no watcher of its own.
443 for (int pid : m_shaderResources.resourceProperties())
444 addDirtyProperty(pid);
445 markDirty(Dirty::ResourceDirty);
446}
447
448void QQuick3DComputePass::markDirty(DirtyT type, bool requestSecondaryUpdate)
449{
450 if (!(m_dirtyAttributes & type)) {
451 m_dirtyAttributes |= type;
452 update();
453 }
454
455 if (requestSecondaryUpdate)
456 QQuick3DObjectPrivate::get(this)->requestSecondaryUpdate();
457}
458
459void QQuick3DComputePass::clearDirty(DirtyT type)
460{
461 m_dirtyAttributes &= ~type;
462}
463
464void QQuick3DComputePass::onCommandChanged()
465{
466 markDirty(CommandsDirty);
467}
468
469void QQuick3DComputePass::qmlAppendCommand(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, QQuick3DShaderUtilsRenderCommand *command)
470{
471 if (!command)
472 return;
473
474 QQuick3DComputePass *that = qobject_cast<QQuick3DComputePass *>(list->object);
475
476 if (!command->parentItem())
477 command->setParentItem(that);
478
479 that->m_commands.push_back(command);
480 that->markDirty(CommandsDirty);
481
482 QObject::connect(command, &QQuick3DShaderUtilsRenderCommand::changed, that, &QQuick3DComputePass::onCommandChanged);
483}
484
485QQuick3DShaderUtilsRenderCommand *QQuick3DComputePass::qmlCommandAt(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, qsizetype index)
486{
487 QQuick3DComputePass *that = qobject_cast<QQuick3DComputePass *>(list->object);
488 return that->m_commands.at(index);
489}
490
491qsizetype QQuick3DComputePass::qmlCommandCount(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
492{
493 QQuick3DComputePass *that = qobject_cast<QQuick3DComputePass *>(list->object);
494 return that->m_commands.size();
495}
496
497void QQuick3DComputePass::qmlCommandClear(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
498{
499 QQuick3DComputePass *that = qobject_cast<QQuick3DComputePass *>(list->object);
500 for (QQuick3DShaderUtilsRenderCommand *cmd : std::as_const(that->m_commands))
501 QObject::disconnect(cmd, nullptr, that, nullptr);
502 that->m_commands.clear();
503 that->markDirty(CommandsDirty);
504}
505
506/*!
507 \qmlproperty list<Command> ComputePass::commands
508 This property holds the list of commands for the compute pass.
509
510 The commands in the list are executed in the order they appear in the
511 list. The only commands relevant to a ComputePass are
512 \l Dispatch and \l AddDefine.
513
514 Dispatch specifies how many work groups to dispatch the
515 compute shader with. A ComputePass without a Dispatch command does not
516 execute its shader. If more than one Dispatch command is present, only
517 the first one is executed. Setting any group count of the Dispatch
518 command to \c 0 disables the ComputePass.
519
520 AddDefine injects a \c {#define} into the shader compilation for
521 a \l ComputePass. This lets you branch shader behavior at compile time
522 depending on which pass is being executed.
523
524 \sa Dispatch, AddDefine
525*/
526
527QQmlListProperty<QQuick3DShaderUtilsRenderCommand> QQuick3DComputePass::commands()
528{
529 return QQmlListProperty<QQuick3DShaderUtilsRenderCommand>(this,
530 nullptr,
531 QQuick3DComputePass::qmlAppendCommand,
532 QQuick3DComputePass::qmlCommandCount,
533 QQuick3DComputePass::qmlCommandAt,
534 QQuick3DComputePass::qmlCommandClear);
535}
536
537/*!
538 \qmlproperty url ComputePass::computeShader
539 This property holds the URL of the compute shader used by the pass.
540
541 As with the shader properties of \l RenderPass, \l Effect, and
542 \l CustomMaterial, relative URLs are resolved relative to the
543 component's (the \c{.qml} file's) location, and it is possible to omit
544 the \c file scheme.
545*/
546QUrl QQuick3DComputePass::computeShader() const
547{
548 return m_computeShader;
549}
550
551void QQuick3DComputePass::setComputeShader(const QUrl &computeShader)
552{
553 if (m_computeShader == computeShader)
554 return;
555 m_computeShader = computeShader;
556 emit computeShaderChanged();
557 markDirty(ComputeShaderDirty);
558}
559
560/*!
561 \qmlproperty enumeration ComputePass::executionMode
562
563 This property determines in which frames the pass executes.
564
565 \value ComputePass.EveryFrame
566 The pass executes in every frame the View3D renders. This is the
567 default.
568 \value ComputePass.OnRequest
569 The pass executes once when it first can, and after that once for
570 each call to \l requestExecution().
571
572 Use \c OnRequest for a pass that initializes data, and for a pass whose
573 input changes only occasionally, calling \l requestExecution() when the
574 input changes:
575
576 \qml
577 PerspectiveCamera {
578 id: camera
579 onScenePositionChanged: cullPass.requestExecution()
580 }
581
582 ComputePass {
583 id: cullPass
584 executionMode: ComputePass.OnRequest
585 computeShader: "cull.comp" // reads CAMERA_POSITION
586 }
587 \endqml
588
589 A pass that is due to execute but cannot, for example because its shader
590 failed to compile or the View3D has no camera, keeps the request until
591 it can. A pass whose \l Dispatch has a group count of 0 does not execute,
592 so a pass is disabled by setting a group count to 0. An \c OnRequest pass
593 executes once when it is enabled if it has a request pending, including
594 the initial one.
595
596 \note A ComputePass does not by itself cause the View3D to render, except
597 when \l requestExecution() is called: with \c EveryFrame, the pass
598 executes only in the frames that are rendered for some other reason.
599
600 \note A pass is not needed just to initialize a \l ShaderStorageBuffer:
601 it is filled with zeros whenever its storage is allocated, unless
602 \l{ShaderStorageBuffer::zeroInitialized}{zeroInitialized} is \c false.
603 For occasional resets, such as restarting a simulation, use
604 \l{ShaderStorageBuffer::requestClear()}{requestClear()}.
605
606 \sa requestExecution(), ShaderStorageBuffer::requestClear()
607*/
608QQuick3DComputePass::ExecutionMode QQuick3DComputePass::executionMode() const
609{
610 return m_executionMode;
611}
612
613void QQuick3DComputePass::setExecutionMode(ExecutionMode executionMode)
614{
615 if (m_executionMode == executionMode)
616 return;
617 m_executionMode = executionMode;
618 emit executionModeChanged();
619 markDirty(StateDirty);
620}
621
622/*!
623 \qmlproperty enumeration ComputePass::executionStage
624
625 This property determines where in the frame the pass executes. The
626 stages are listed in the order they execute in.
627
628 \value ComputePass.StartOfFrame
629 The pass executes before any of the built-in passes, and before
630 texture and buffer provider extensions. This stage is meant for
631 producing data that the built-in passes consume, for example a
632 buffer a vertex shader reads, so that the shadow maps are rendered
633 from the same data as the scene. A pass in this stage cannot rely on
634 the built-in textures: \c SCREEN_TEXTURE, \c DEPTH_TEXTURE,
635 \c AO_TEXTURE, and \c NORMAL_ROUGHNESS_TEXTURE are not available
636 to it, and the shadow maps, when the scene has them, are from the
637 previous frame, and do not exist yet in the first frame. Similarly,
638 the output of a texture or buffer provider extension, such as a
639 \l{ShaderStorageBuffer::bufferProvider}{bufferProvider}, is from the
640 previous frame.
641 \value ComputePass.PreColor
642 The pass executes together with the scene's \l{RenderPass}{RenderPasses},
643 after the built-in passes that produce the depth, normal, ambient
644 occlusion, and screen textures, the shadow maps, and the reflection
645 probes, and before the main pass, which renders the scene's opaque
646 and transparent objects. This is the default.
647
648 A pass never executes in a later stage than a pass it is nested in, so
649 that a nested pass always executes before its ancestors. A pass therefore
650 executes in the earliest of its own stage and the stages of the passes
651 it is nested in. A \l RenderPass nested in a ComputePass in the
652 \c StartOfFrame stage executes in that stage too, with the same
653 limitations on the built-in textures. A pass
654 that only produces data for a \c StartOfFrame pass does not need to set
655 the stage itself:
656
657 \qml
658 ComputePass {
659 id: consumer
660 executionMode: ComputePass.OnRequest
661 executionStage: ComputePass.StartOfFrame
662
663 ComputePass {
664 id: producer
665 // Executes in the StartOfFrame stage, before consumer.
666 }
667 }
668 \endqml
669*/
670QQuick3DComputePass::ExecutionStage QQuick3DComputePass::executionStage() const
671{
672 return m_executionStage;
673}
674
675void QQuick3DComputePass::setExecutionStage(ExecutionStage executionStage)
676{
677 if (m_executionStage == executionStage)
678 return;
679 m_executionStage = executionStage;
680 emit executionStageChanged();
681 if (QQuick3DSceneManager *sceneManager = QQuick3DObjectPrivate::get(this)->sceneManager)
682 sceneManager->userRenderPassesDirty = true;
683 markDirty(StateDirty);
684}
685
686/*!
687 \qmlmethod void ComputePass::requestExecution()
688
689 Requests that the pass executes in the next frame. This also makes the
690 View3D render that frame, so the request does not wait for something
691 else to change in the scene. Several calls before that frame result in
692 a single execution. A request made while the pass cannot execute, for
693 example while the View3D is not rendering, is kept until it can. When
694 several View3D objects in the same window show the scene the pass
695 belongs to, the pass executes in only one of them per request, since
696 they share its buffers and images.
697
698 This works with any \l executionMode.
699*/
700void QQuick3DComputePass::requestExecution()
701{
702 ++m_executionSerial;
703 markDirty(StateDirty);
704}
705
706QT_END_NAMESPACE