ComputePass QML Type

Defines a custom compute shader pass. More...

Import Statement: import QtQuick3D
Since: Qt 6.13
Inherits:

Object3D

Properties

Detailed Description

A ComputePass runs a GLSL compute shader as part of the 3D scene's rendering pipeline. Unlike a RenderPass or an Effect, a ComputePass does not render any geometry itself; it is used to prepare or process data that is later consumed by a material, an Effect, or another ComputePass – typically data written into a ShaderImage or a ShaderStorageBuffer.

A ComputePass becomes active for a scene when it is placed as a child of a View3D, a Node, or another pass. Within a single frame, a ComputePass runs after the scene's internal preparation passes, which produce the depth, normal, and ambient occlusion textures, the shadow maps, the screen texture, the reflection probes, and the textures of texture provider extensions, and before the scene's main opaque and transparent rendering. Its results are therefore visible to the main rendering and the effects of the same frame, but not to those preparation passes.

Note: If the results of a ComputePass change what the preparation passes render, for example a buffer that a CustomMaterial uses to displace vertices, those passes use the results of the previous frame: the shadow maps, DEPTH_TEXTURE, SCREEN_TEXTURE, the ambient occlusion, and the reflection probes are one frame behind the main rendering, and see uninitialized content in the first frame.

The order against other passes, RenderPass and ComputePass alike, is expressed by nesting: a nested pass runs before its parent, so a pass that produces data for another one is declared inside its consumer. The relative order of passes at the same nesting depth is not defined. See Pass Ordering for the details.

Note: A ComputePass requires a graphics API with compute support: OpenGL 4.3 or newer, OpenGL ES 3.1 or newer, Vulkan, Metal, or Direct3D. On a backend without compute support, Qt Quick 3D skips the pass and issues a warning.

Note: OpenGL ES 3.1 only guarantees four shader storage buffers and four images per compute shader. Using the light data takes two of those storage buffers. ShaderImage also has format and memory qualifier restrictions on OpenGL ES; see ShaderImage::format.

The following example dispatches a compute shader that fills a ShaderImage with a solid color, one invocation per texel:

 import QtQuick3D

 View3D {
     ShaderImage {
         id: myImage
         width: 256
         height: 256
     }

     ComputePass {
         computeShader: "fill.comp"
         property ShaderImage targetImage: myImage
         commands: [
             Dispatch {
                 groupCountX: (myImage.width + 7) / 8
                 groupCountY: (myImage.height + 7) / 8
             }
         ]
     }
 }

Exposing data to the shader

As with RenderPass, Effect, and CustomMaterial, any QML property declared on a ComputePass is automatically exposed as a uniform to its compute shader. Properties of type ShaderImage are exposed to the shader as a GLSL image, declared with the SHADER_IMAGE macro, and properties of type ShaderStorageBuffer as a shader storage buffer, declared with the STORAGE_BUFFER macro.

Built-in uniforms

The compute shader can also use the following built-in uniforms without a matching QML property. Referencing an identifier in the shader source binds the corresponding uniform automatically, as in CustomMaterial and Effect shaders:

  • VIEW_MATRIX -> mat4, the view (camera) matrix.
  • INVERSE_VIEW_MATRIX -> mat4, the inverse of the view matrix.
  • PROJECTION_MATRIX -> mat4, the projection matrix.
  • INVERSE_PROJECTION_MATRIX -> mat4, the inverse of the projection matrix.
  • VIEWPROJECTION_MATRIX -> mat4, the combined view-projection matrix.
  • INVERSE_VIEWPROJECTION_MATRIX -> mat4, the inverse of the view-projection matrix.
  • CAMERA_POSITION -> vec3, the camera position in world space.
  • CAMERA_DIRECTION -> vec3, the camera direction vector.
  • CAMERA_PROPERTIES -> vec3, the near and far clip distances for the camera, in x and y (z is unused).
  • FRAMEBUFFER_Y_UP -> float, 1.0 or -1.0 depending on whether the graphics API's framebuffer coordinate system has Y pointing up.
  • NDC_Y_UP -> float, 1.0 or -1.0 depending on whether the graphics API's normalized device coordinate system has Y pointing up.
  • NEAR_CLIP_VALUE -> float, 0.0 or -1.0 depending on whether the graphics API's clip space near plane is at 0.0 or -1.0.
  • FRAME -> float, a frame counter, incremented after each rendered frame in the View3D.

Note: With multiview rendering active, the camera matrix, position, and direction uniforms above become arrays. CAMERA_PROPERTIES, FRAMEBUFFER_Y_UP, NDC_Y_UP, NEAR_CLIP_VALUE, and FRAME do not. See ComputePass and multiview rendering.

Screen, depth, and other textures

As with CustomMaterial and Effect, referencing one of the following identifiers causes the corresponding texture to be prepared and bound automatically:

IdentifierTypeDescription
SCREEN_TEXTURE, SCREEN_MIP_TEXTUREsampler2DA copy of the scene's opaque color buffer, captured in a dedicated pass that runs before the ComputePass itself. Because a ComputePass always runs before the scene's main opaque and transparent rendering (see above), this does not include transparent objects, the skybox, or anything drawn by an Effect.
DEPTH_TEXTUREsampler2DThe scene's opaque depth buffer, subject to the same opaque-only caveat as SCREEN_TEXTURE.
AO_TEXTUREsampler2DThe ambient occlusion buffer. Referencing this identifier enables the SSAO pass for the layer even if aoEnabled is false.
NORMAL_ROUGHNESS_TEXTUREsampler2DA texture with the world-space normals (rgb) and roughness (a) of the opaque objects in the scene. Always a plain sampler2D, even with multiview rendering active.

Lights

A ComputePass exposes light data differently from CustomMaterial and Effect. A CustomMaterial exposes light data through per-light callback functions, such as DIRECTIONAL_LIGHT(), which the engine calls once for each active light. An Effect has no lighting model. A compute shader has no current fragment to shade, so a ComputePass provides accessor functions that take a light index, and the shader loops over the lights itself:

IdentifierTypeDescription
DIRECTIONAL_LIGHT_COUNTuintThe number of active directional lights.
DIRECTIONAL_LIGHT_DIFFUSE(index)vec3The light's diffuse color.
DIRECTIONAL_LIGHT_DIRECTION(index)vec3The light's direction.
POINT_LIGHT_COUNTuintThe number of active point lights.
POINT_LIGHT_DIFFUSE(index)vec3The light's diffuse color.
POINT_LIGHT_POSITION(index)vec3The light's world-space position.
POINT_LIGHT_ATTENUATION(index)vec3The constant, linear, and quadratic attenuation factors, in x, y, and z.
SPOT_LIGHT_COUNTuintThe number of active spot lights.
SPOT_LIGHT_DIFFUSE(index)vec3The light's diffuse color.
SPOT_LIGHT_POSITION(index)vec3The light's world-space position.
SPOT_LIGHT_ATTENUATION(index)vec3The constant, linear, and quadratic attenuation factors, in x, y, and z.
SPOT_LIGHT_DIRECTION(index)vec3The light's direction.
SPOT_LIGHT_CONES(index)vec2The cosines of the inner and outer cone angles, in x and y.

index is a uint and is zero-based per light type, so a spot light's index does not depend on how many point lights the scene has.

Referencing any of these identifiers makes all of them available to the shader.

Note: There is no separate intensity or brightness identifier. The colors are linear (not sRGB) and have brightness premultiplied into them already.

Directional lights and positional (point and spot) lights are passed in two separate fixed-size shader storage buffers, each holding 16 KB of light data. The resulting limits are available to the shader as the compile-time constants QSSG_MAX_DIRECTIONAL_LIGHTS and QSSG_MAX_POSITIONAL_LIGHTS, the latter being a limit on the number of point and spot lights combined. Lights beyond these limits are ignored, with a warning.

The two light buffers use shader storage buffer bindings of their own, so they count toward the device's storage buffer limit. OpenGL ES 3.1 only guarantees four per compute shader, which leaves two for ShaderStorageBuffer properties when the light data is used.

Shadows

If a light with castsShadow enabled is present, its shadow map can be sampled with one of the following macros, each returning an illumination factor between 0.0 (fully shadowed) and 1.0 (fully lit):

  • DIRECTIONAL_LIGHT_SHADOW(index, viewSpaceDepth, worldPosition) – for the directional light at the same index the DIRECTIONAL_LIGHT_* accessors above take. viewSpaceDepth (the world position transformed by VIEW_MATRIX, .z) selects the correct cascade.
  • POINT_LIGHT_SHADOW(index, worldPosition) – for the point light at index.
  • SPOT_LIGHT_SHADOW(index, worldPosition) – for the spot light at index.

QSSG_SHADOW_SOFTNESS is a compile-time constant (not a uniform), reflecting the softest softShadowQuality among the shadow-casting lights in the scene (0, 4, 8, or 16).

Note: The QSSG_ prefix marks QSSG_MAX_DIRECTIONAL_LIGHTS, QSSG_MAX_POSITIONAL_LIGHTS, and QSSG_SHADOW_SOFTNESS as reflecting the renderer's internals rather than a stable API. Their values, and the constants themselves, may change in future Qt versions.

Referencing a _SHADOW macro also makes the light identifiers from the previous section available, so a shader that only samples shadows does not need to reference any of them.

ComputePass and multiview rendering

Multiview rendering is relevant to a compute shader when it is enabled and the compute shader uses any screen textures (SCREEN_TEXTURE, SCREEN_MIP_TEXTURE, DEPTH_TEXTURE, and AO_TEXTURE) or camera uniforms (any VIEW or PROJECTION matrices, camera position and direction, but not properties).

To tell Qt Quick 3D that the shader supports multiview rendering, reference QSHADER_VIEW_COUNT in the shader. If it is not referenced, a warning is issued.

When multiview is enabled, all of the built-in camera uniforms listed above, except CAMERA_PROPERTIES, become arrays of size QSHADER_VIEW_COUNT, and SCREEN_TEXTURE, SCREEN_MIP_TEXTURE, DEPTH_TEXTURE, and AO_TEXTURE become sampler2DArray instead of sampler2D, one layer per eye. NORMAL_ROUGHNESS_TEXTURE always stays a plain sampler2D, even with multiview active.

A ComputePass has no built-in VIEW_INDEX, unlike the fragment shader of a CustomMaterial or an Effect. In those shaders, VIEW_INDEX derives from the vertex stage's gl_ViewIndex, and compute has no equivalent. Qt Quick 3D dispatches a ComputePass exactly once, regardless of the view count. To produce per-eye results under multiview rendering, the compute shader must derive the eye index itself, for example from one dispatch dimension. It then writes to a ShaderImage with arraySize set to the view count, declared as image2DArray in the shader.

See also ShaderImage, ShaderStorageBuffer, Dispatch, Effect, and CustomMaterial.

Property Documentation

commands : list<Command>

This property holds the list of commands for the compute pass.

The commands in the list are executed in the order they appear in the list. The only commands relevant to a ComputePass are Dispatch and AddDefine.

Dispatch specifies how many work groups to dispatch the compute shader with. A ComputePass without a Dispatch command does not execute its shader. If more than one Dispatch command is present, only the first one is executed. Setting any group count of the Dispatch command to 0 disables the ComputePass.

AddDefine injects a #define into the shader compilation for a ComputePass. This lets you branch shader behavior at compile time depending on which pass is being executed.

See also Dispatch and AddDefine.

computeShader : url

This property holds the URL of the compute shader used by the pass.

As with the shader properties of RenderPass, Effect, and CustomMaterial, relative URLs are resolved relative to the component's (the .qml file's) location, and it is possible to omit the file scheme.