Qt Quick 3D - Clustered Lighting Example
Demonstrates ComputePass usage with Clustered Lighting and volumetric fog.

This example demonstrates how to use the Compute passes to implement clustered lighting.
Unlike a regular scene where lights are specified using PointLight, SpotLight or DirectionalLight, with clustered lighting the user can specify themselves how the lights are handled. In this example the lights are specified by providing a ShaderStorageBuffer with custom lights using custom C++ class.
Since these lights are not in any way processed by Qt Quick 3D, they are really fast and there can be large number of lights in a scene. Downside is that these lights do not allow using the automated shadow generation.
In clustered lighting, the view is divided into 3D grid of clusters and each light gets assigned into each grid locations based on their influence to the scene. This light grid is then read by CustomMaterial that has custom light calculations based on the view position of the fragment. This way each fragment only processes the lights that affect it, which is usually a lot smaller than the total amount of lights.
Cluster Generation Pass
Before the clusters can be used, they must be initialized. The clusters depend only on the view, not on the scene, so they only change when the viewport or the camera's projection changes. Rather than track those changes, the example simply runs the pass every frame.
Before we can start, we need to create the buffer for the clusters.
ShaderStorageBuffer {
id: clusterBuffer
size: clusterPass.gridDimensions.x * clusterPass.gridDimensions.y * clusterPass.gridDimensions.z * (2 * 4 * 4)
}We can then run the clustering ComputePass:
ComputePass {
id: clusterPass
property vector3d gridDimensions : Qt.vector3d(16, 8, 24)
property vector2d screenDimensions : Qt.vector2d(view.width * window.dpr,
view.height * window.dpr)
commands: [
Dispatch {
groupCountX: clusterPass.gridDimensions.x
groupCountY: clusterPass.gridDimensions.y
groupCountZ: clusterPass.gridDimensions.z
}
]
property ShaderStorageBuffer clusterBuffer : clusterBuffer
computeShader: "cluster_shader.comp"
}Light Assignment Pass
We also then need to create buffer for the light data. The data is generated using CustomLightData type we have created.
ShaderStorageBuffer {
id: lightBuffer
bufferData: CustomLightData {
id: lightData
count: settings.lightCount
extents: Qt.vector3d(5000, 500, 5000)
position: Qt.vector3d(0, 200, -1100)
}
}We also need to create buffer that holds indices to lights per grid cell.
ShaderStorageBuffer {
id: lightIndicesBuffer
size: clusterPass.gridDimensions.x * clusterPass.gridDimensions.y * clusterPass.gridDimensions.z * (lightData.count + 1) * 4
}Now we can run the assignment ComputePass
ComputePass {
id: lightAssignmentPass
// ![clusterPass usage]
ComputePass {
id: clusterPass
property vector3d gridDimensions : Qt.vector3d(16, 8, 24)
property vector2d screenDimensions : Qt.vector2d(view.width * window.dpr,
view.height * window.dpr)
commands: [
Dispatch {
groupCountX: clusterPass.gridDimensions.x
groupCountY: clusterPass.gridDimensions.y
groupCountZ: clusterPass.gridDimensions.z
}
]
property ShaderStorageBuffer clusterBuffer : clusterBuffer
computeShader: "cluster_shader.comp"
}
// ![clusterPass usage]
commands: [
Dispatch {
groupCountX: clusterPass.gridDimensions.x
groupCountY: clusterPass.gridDimensions.y
groupCountZ: clusterPass.gridDimensions.z
}
]
property ShaderStorageBuffer lightIndicesBuffers : lightIndicesBuffer
property ShaderStorageBuffer clusterBuffers : clusterBuffer
property ShaderStorageBuffer lightBuffers : lightBuffer
property int numLights : lightData.count
// Constant, linear and quadratic terms. The fog in fog_effect.frag
// ignores the linear term, so keep it at 0 for the fog to match.
property vector3d lightAttenuation : Qt.vector3d(1, 0, 0.01)
property real minIntensity : 1.0 / 200.0
property vector3d gridDimensions : clusterPass.gridDimensions
property vector3d eulerRotation
property vector3d clusterPosition : lightData.position
property real assignmentDistance : settings.distance
computeShader: "assignment_shader.comp"
PropertyAnimation on eulerRotation.y {
id: lightAnimation
from: 0.0
to: 360.0
duration: 30000
running: true
loops: PropertyAnimation.Infinite
}
}The cluster pass is nested in the assignment pass. A nested pass runs before the pass that contains it, so the clusters are ready by the time the lights are assigned to them. In the same way, both passes are nested in the main color pass shown below, so that the lights are assigned before the scene is rendered.
Scene Render Pass
Now we can render the scene and use the CustomLightingMaterial with scene models to apply the lighting. We use the RenderPass to render the scene
RenderPass {
id: mainColorPass
clearColor: "black"
// ![AssignmentPass usage]
ComputePass {
id: lightAssignmentPass
// ![clusterPass usage]
ComputePass {
id: clusterPass
property vector3d gridDimensions : Qt.vector3d(16, 8, 24)
property vector2d screenDimensions : Qt.vector2d(view.width * window.dpr,
view.height * window.dpr)
commands: [
Dispatch {
groupCountX: clusterPass.gridDimensions.x
groupCountY: clusterPass.gridDimensions.y
groupCountZ: clusterPass.gridDimensions.z
}
]
property ShaderStorageBuffer clusterBuffer : clusterBuffer
computeShader: "cluster_shader.comp"
}
// ![clusterPass usage]
commands: [
Dispatch {
groupCountX: clusterPass.gridDimensions.x
groupCountY: clusterPass.gridDimensions.y
groupCountZ: clusterPass.gridDimensions.z
}
]
property ShaderStorageBuffer lightIndicesBuffers : lightIndicesBuffer
property ShaderStorageBuffer clusterBuffers : clusterBuffer
property ShaderStorageBuffer lightBuffers : lightBuffer
property int numLights : lightData.count
// Constant, linear and quadratic terms. The fog in fog_effect.frag
// ignores the linear term, so keep it at 0 for the fog to match.
property vector3d lightAttenuation : Qt.vector3d(1, 0, 0.01)
property real minIntensity : 1.0 / 200.0
property vector3d gridDimensions : clusterPass.gridDimensions
property vector3d eulerRotation
property vector3d clusterPosition : lightData.position
property real assignmentDistance : settings.distance
computeShader: "assignment_shader.comp"
PropertyAnimation on eulerRotation.y {
id: lightAnimation
from: 0.0
to: 360.0
duration: 30000
running: true
loops: PropertyAnimation.Infinite
}
}
// ![AssignmentPass usage]
commands: [
ColorAttachment {
target: mainColorTexture
},
DepthTextureAttachment {
target: mainDepthStencilTexture
},
// 1. Opaque pass
SubRenderPass {
renderPass: RenderPass {
id: opaquePass
materialMode: RenderPass.OriginalMaterial
commands: [
RenderablesFilter {
renderableTypes: RenderablesFilter.Opaque
},
PipelineStateOverride {
depthTestEnabled: true
depthWriteEnabled: true
}
]
}
},
// 2. Skybox: render the environment behind all scene geometry.
SubRenderPass {
renderPass: RenderPass {
id: skyboxPass
passMode: RenderPass.SkyboxPass
commands: [
PipelineStateOverride {
// The skybox is rendered "at infinity" so it must
// depth-test (to be hidden by geometry) but must not
// write depth.
depthTestEnabled: true
depthWriteEnabled: false
}
]
}
},
// 3. 2D items: render any Qt Quick Items embedded in the 3D scene.
SubRenderPass {
renderPass: RenderPass {
id: item2DPass
passMode: RenderPass.Item2DPass
}
},
// 4. Transparent objects: render blended geometry on top of everything else.
SubRenderPass {
renderPass: RenderPass {
id: transparentItemPass
materialMode: RenderPass.OriginalMaterial
commands: [
RenderablesFilter {
renderableTypes: RenderablesFilter.Transparent
},
PipelineStateOverride {
// Enable alpha blending and depth testing so transparent
// objects sort correctly against opaque geometry.
blendEnabled: true
depthTestEnabled: true
targetBlend0.enable: true
targetBlend0.srcColor: RenderTargetBlend.SrcAlpha
targetBlend0.dstColor: RenderTargetBlend.OneMinusSrcAlpha
targetBlend0.srcAlpha: RenderTargetBlend.One
targetBlend0.dstAlpha: RenderTargetBlend.OneMinusSrcAlpha
}
]
}
}
]
}The material is used like any other custom material
Model {
instancing: randomInstancing
source: "asteroid.mesh"
materials: [
CustomLightingMaterial {
instanced: true
baseColor: "turquoise"
lightBuffers: lightBuffer
lightIndicesBuffer: lightIndicesBuffer
gridDimensions: clusterPass.gridDimensions
screenDimensions: clusterPass.screenDimensions
numLights : lightData.count
eulerRotation: lightAssignmentPass.eulerRotation
clusterPosition: lightAssignmentPass.clusterPosition
lightAttenuation: lightAssignmentPass.lightAttenuation
minIntensity: lightAssignmentPass.minIntensity
assignmentDistance: lightAssignmentPass.assignmentDistance
}
]
}Volumetric Fog
The clustered lights also light a volumetric fog, which an Effect adds to the rendered scene. For each pixel, the effect reads the depth from the main color pass's DepthTextureAttachment, which a RenderOutputProvider exposes as a texture, and follows the view ray up to that depth. The background has the depth of the far plane, so the fog covers the background too.
RenderOutputProvider {
id: mainDepthProvider
textureSource: RenderOutputProvider.UserPassDepthTexture
renderPass: mainColorPass
}
Effect {
id: fogEffect
property TextureInput depthTexture : TextureInput {
texture: Texture {
textureProvider: mainDepthProvider
minFilter: Texture.Nearest
magFilter: Texture.Nearest
mipFilter: Texture.None
}
}The effect also uses the light buffers and the same parameters as the light assignment pass.
property ShaderStorageBuffer lightBuffers : lightBuffer
property ShaderStorageBuffer lightIndicesBuffers : lightIndicesBuffer
property real zNear : camera.clipNear
property real zFar : camera.clipFar
property vector3d gridDimensions : clusterPass.gridDimensions
property int numLights : lightData.count
property vector3d lightAttenuation : lightAssignmentPass.lightAttenuation
property real minIntensity : lightAssignmentPass.minIntensity
property real assignmentDistance : lightAssignmentPass.assignmentDistance
property vector3d eulerRotation : lightAssignmentPass.eulerRotation
property vector3d clusterPosition : lightAssignmentPass.clusterPosition
property real fogDensity : settings.fogDensity
// Scales the light the fog scatters, to make the fog glow more
property real lightScattering : 20
// Ice crystals: the share of grid cells that hold a crystal, how far
// the plates tilt away from horizontal in degrees, and the glint
// brightness. crystalTime drives the crystals' drift and turning.
property real crystalAmount : settings.crystalAmount
property real crystalTilt : settings.crystalTilt
property real crystalBrightness : 150
property real crystalTime : 0
// Accumulate instead of looping, so the crystals do not jump back
// to their starting positions when the time wraps around.
FrameAnimation {
running: true
onTriggered: fogEffect.crystalTime += frameTime
}
passes: Pass {
shaders: Shader {
stage: Shader.Fragment
shader: "fog_effect.frag"
}
}The effect splits the ray at the depth slices of the light clusters, so each segment of the ray lies in one cluster and only needs the lights assigned to it. The fog attenuates both the scene behind it and the light scattered further away.
vec3 scattered = vec3(0.0);
float transmittance = 1.0;
int sliceCount = int(gridDimensions.z);
for (int z = 0; z < sliceCount; ++z) {
// The depth slice of the clusters, cut short at the pixel's depth
float sliceStart = zNear * pow(zFar / zNear, float(z) / gridDimensions.z);
if (sliceStart >= pixelDepth)
break;
float sliceEnd = min(zNear * pow(zFar / zNear, float(z + 1) / gridDimensions.z), pixelDepth);
float s0 = sliceStart * rayScale;
float s1 = sliceEnd * rayScale;
float density = fogDensity * 0.0005;
uint offset = clusterIndex(uvec3(clusterXY, z)) * uint(numLights + 1);
uint count = min(uint(numLights), lightIndices[offset]);
vec3 light = vec3(0.0);
for (uint i = 0; i < count; i++) {
Light l = lights[lightIndices[offset + i + 1]];
vec3 p = rv * vec3(l.position) + tv;
float t0 = dot(p, rayNormal);
float h2 = max(dot(p, p) - t0 * t0, 0.0);
light += vec3(l.color) * scatteredAlongRay(s0, s1, t0, h2, l.intensity.x);
}
// Isotropic scattering, attenuated by the fog up to the segment's middle
float segmentTransmittance = exp(-density * (s1 - s0));
scattered += transmittance * sqrt(segmentTransmittance) * density * light * lightScattering;
transmittance *= segmentTransmittance;
}
scattered /= (4.0 * PI);For each light, the light the fog scatters along a segment is integrated in closed form rather than sampled at a few points along the ray, so small, moving lights do not flicker.
// The light a light scatters along the ray between the distances s0 and s1.
// The light is at the distance t0 along the ray and h2 is the square of its
// distance from the ray. The attenuation, intensity / (ax + az * r^2), is
// integrated in closed form. To make the light fade out at the edge of its
// influence radius, which the light assignment uses, the attenuation at that
// radius is subtracted, and the ray is clipped to the influence sphere.
float scatteredAlongRay(float s0, float s1, float t0, float h2, float intensity)
{
float radius = influenceRadius(intensity) * assignmentDistance;
float r2 = radius * radius;
if (h2 >= r2)
return 0.0;
float halfChord = sqrt(r2 - h2);
float a = max(s0, t0 - halfChord) - t0;
float b = min(s1, t0 + halfChord) - t0;
if (a >= b)
return 0.0;
float ax = max(lightAttenuation.x, 1.0e-6);
float az = lightAttenuation.z;
if (az <= 0.0)
return 0.0;
float c = sqrt(ax / az + h2);
float integral = intensity / (az * c) * (atan(b / c) - atan(a / c));
float edge = intensity / (ax + az * r2) * (b - a);
return max(integral - edge, 0.0);
}Ice Crystals
The same effect also adds the glints of tiny ice crystals floating in the air. Unlike the fog, the glints are individual points of light, so they cannot be integrated along the ray. Instead, the effect places the crystals in a grid of cells in world space, with at most one crystal in each cell. Whether a cell holds a crystal, where in the cell it is, and how it is oriented are all derived from a hash of the cell's coordinates, so each crystal stays in the same place from frame to frame without being stored anywhere. The grid moves slowly down over time, which makes the crystals drift through the light.
For each pixel, the effect walks through the cells that the view ray passes, up to the depth of the scene, so geometry hides the crystals behind it. The walk stops after a fixed number of cells, so crystals only appear near the camera. A crystal is drawn when it is within about a pixel of the ray, so it stays a small point at any distance, and it fades with the distance and with the fog in front of it. The lights that can reach the crystal are the ones assigned to the cluster the crystal lies in.
vec3 r0 = random3(uvec3(cell));
if (r0.x < crystalAmount) {
vec3 r1 = random3(uvec3(cell) ^ uvec3(0x9e3779b9u));
// Keep the crystal away from the cell's faces, so its glint is not
// cut off where the ray does not enter the cell.
vec3 toCrystal = (vec3(cell) + 0.1 + 0.8 * vec3(r0.yz, r1.x) - origin) * CRYSTAL_SPACING;
float along = dot(toCrystal, dir);
float footprint = along * pixelAngle * 0.75;
float perp2 = dot(toCrystal, toCrystal) - along * along;
if (along > 0.0 && along < maxDist && perp2 < 9.0 * footprint * footprint) {
vec3 p = viewRotation * toCrystal;
float depth = -p.z;
uint z = uint(clamp(floor(log(depth / zNear) / logDepthRange * gridDimensions.z), 0.0, gridDimensions.z - 1.0));
uint offset = clusterIndex(uvec3(clusterXY, z)) * uint(numLights + 1);
uint count = min(uint(numLights), lightIndices[offset]);
// The plate normal is tilted away from vertical, and the
// crystal slowly turns around the vertical axis.
float theta = tilt * r1.y;
float phi = 2.0 * PI * r1.z + crystalTime * (r0.y - 0.5) * 2.0;
vec3 n = viewRotation * vec3(sin(theta) * cos(phi), cos(theta), sin(theta) * sin(phi));
float spot = exp(-perp2 / (footprint * footprint));
float distanceFade = 1.0 / (1.0 + along * along / (200.0 * 200.0));
result += glint(p, n, offset, count, rv, tv) * spot * distanceFade * exp(-density * along);
}
}Each crystal is a flat plate that reflects light like a two-sided mirror. It only glints when its face is nearly perpendicular to the half vector between the direction to the light and the direction to the camera, so only a few of the crystals light up at a time, and which ones changes as the crystals, the lights, and the camera move. The light reaching the crystal falls off with distance in the same way as the light in the fog.
// The light a light reflects towards the eye from a crystal at the view-space
// position p with the view-space plate normal n.
vec3 glint(vec3 p, vec3 n, uint offset, uint count, mat3 rv, vec3 tv)
{
vec3 v = normalize(-p);
float nv = dot(n, v);
vec3 result = vec3(0.0);
for (uint i = 0; i < count; i++) {
Light l = lights[lightIndices[offset + i + 1]];
vec3 toLight = rv * vec3(l.position) + tv - p;
float d2 = dot(toLight, toLight);
vec3 ld = toLight * inversesqrt(d2);
// A mirror only reflects when the light and the eye are on the same side
if (dot(n, ld) * nv <= 0.0)
continue;
float intensity = l.intensity.x;
float radius = influenceRadius(intensity) * assignmentDistance;
float ax = max(lightAttenuation.x, 1.0e-6);
float az = lightAttenuation.z;
float irradiance = intensity / (ax + az * d2) - intensity / (ax + az * radius * radius);
if (irradiance <= 0.0)
continue;
float lobe = pow(abs(dot(n, normalize(ld + v))), GLINT_SHARPNESS);
result += vec3(l.color) * irradiance * lobe;
}
return result;
}Plate-shaped ice crystals tend to float horizontally. The Ice crystal tilt setting limits how far the plates tilt away from horizontal. With a small tilt, only crystals directly above or below a light reflect it towards the camera, so the glints line up into light pillars. With a large tilt, the plates face every direction and the glints are scattered all around the lights. The Ice crystals setting controls the share of the grid cells that hold a crystal, and setting it to zero turns the glints off.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.