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
qquick3dshaderimage.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
6#include <QtQuick3DRuntimeRender/private/qssgrenderimage_p.h>
7#include "private/qssgrenderbuffermanager_p.h"
9
11
12/*!
13 \qmltype ShaderImage
14 \inherits Object3D
15 \inqmlmodule QtQuick3D
16 \brief Declares a GPU image resource that shaders can read and write.
17 \since 6.13
18
19 A ShaderImage is a GPU-side image resource with an explicit \l shape,
20 size and pixel \l format, with no content of its own. Unlike
21 \l Texture, it is not loaded from an image file or a \l TextureData. It
22 is meant to be written to or read from directly in shader code
23 (typically in a \l ComputePass, via \c imageStore()/imageLoad(), but
24 also from a \l CustomMaterial or an \l Effect), rather than sampled as
25 a regular texture.
26
27 A ShaderImage is exposed to a shader by using it as a property in the
28 ComputePass, RenderPass, Effect or CustomMaterial:
29
30 \qml
31 ShaderImage {
32 id: myImage
33 width: 256
34 height: 256
35 }
36
37 ComputePass {
38 computeShader: "generate.comp"
39 property ShaderImage targetImage: myImage
40 }
41 \endqml
42
43 In the shader, the property is declared with the \c SHADER_IMAGE macro,
44 using the same name as the QML property (\c targetImage in the example
45 above), and is then used like a regular GLSL image variable:
46
47 \badcode
48 SHADER_IMAGE(rgba8, targetImage, image2D, writeonly);
49
50 void main() {
51 imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(1.0, 0.0, 0.0, 1.0));
52 }
53 \endcode
54
55 The first argument to \c SHADER_IMAGE is the GLSL image format (such as
56 \c rgba8 or \c r32ui), which must match the ShaderImage's
57 \l format. The third argument is the GLSL image type, which must match
58 the ShaderImage's \l shape, with an \c i or \c u prefix for an integer
59 \l format (for example \c image2D, or \c uimage3D for a three-dimensional
60 image in an unsigned integer format). A ShaderImage whose format or shape
61 does not match the declaration is reported with a warning, and the shader
62 cannot be used; this also applies when the format or the shape changes
63 later. The image type can be followed by any combination of the memory
64 qualifiers \c readonly, \c writeonly, \c coherent, \c volatile, and
65 \c restrict, separated by spaces or commas, matching the GLSL memory
66 qualifiers of the same names. Without qualifiers, the image has implicit
67 read-write access. Any other word is rejected with a warning, and the
68 shader cannot be used. The semicolon after the declaration is optional.
69
70 \note On OpenGL ES, implicit read-write access is only allowed for the
71 \c r32f, \c r32i, and \c r32ui formats. An image in any other format has
72 to be declared either \c readonly or \c writeonly, otherwise the shader
73 fails to compile on OpenGL ES. Not all formats are available on OpenGL
74 ES either; see \l format.
75
76 The declaration is recognized by scanning the shader source before
77 any preprocessing takes place. The format, the name, the image type and
78 the memory qualifiers must therefore be written out as plain identifiers;
79 a \c{#define}'d format is not recognized. Each name can be declared only
80 once in a shader, even in different preprocessor branches, and its format
81 must match the \l format property. Declarations inside comments are
82 ignored. A declaration that does not follow this form is reported with a
83 warning, and the shader cannot be used.
84
85 \note Atomic operations on images (\c{imageAtomicAdd()} and friends) are
86 not supported with the Metal graphics API. Such shaders translate and
87 compile, but the results are undefined, because the translation relies on
88 a hidden helper buffer that is never bound. Atomics on a
89 \l ShaderStorageBuffer are not affected and work on all backends.
90
91 \sa ComputePass, ShaderStorageBuffer
92*/
93
94QQuick3DShaderImage::QQuick3DShaderImage(QQuick3DObject *parent)
95 : QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::ShaderStorageImage)), parent)
96{
97
98}
99
100QQuick3DShaderImage::~QQuick3DShaderImage()
101{
102
103}
104
105/*!
106 \qmlproperty enumeration ShaderImage::shape
107 This property holds the shape of the image: the kind of GPU texture that
108 is created, and the GLSL image type that the shader's \c SHADER_IMAGE
109 declaration has to use. The default value is \c{ShaderImage.Image2D}.
110
111 \value ShaderImage.Image1D A one-dimensional image of \l width texels,
112 \c image1D in the shader.
113 \value ShaderImage.Image2D A two-dimensional image of \l width by
114 \l height texels, \c image2D in the shader.
115 \value ShaderImage.Image3D A three-dimensional image of \l width by
116 \l height by \l depth texels, \c image3D in the shader.
117 \value ShaderImage.Image2DArray An array of \l arraySize two-dimensional
118 images of \l width by \l height texels, \c image2DArray in the
119 shader.
120 \value ShaderImage.ImageCube A cube map with six faces of \l width by
121 \l height texels, \c imageCube in the shader. The width and the
122 height must be equal.
123
124 The size properties that a shape does not use are ignored. With an
125 integer \l format, the image type has an \c i or \c u prefix, such as
126 \c iimage2D or \c uimage3D.
127
128 \note One-dimensional images are not supported with OpenGL ES and
129 Direct3D. Cube map images are not supported with Direct3D, which has no
130 read-write cube texture type; a shader declaring an \c imageCube fails to
131 build there. Arrays of one-dimensional images and arrays of cube maps are
132 not supported.
133
134 Setting a value outside this enumeration is ignored, with a warning, and
135 the previous shape is retained.
136*/
137QQuick3DShaderImage::ImageShape QQuick3DShaderImage::shape() const
138{
139 return m_shape;
140}
141
142/*!
143 \qmlproperty int ShaderImage::width
144 This property holds the width of the image, in texels. The default value is \c 1.
145 Values smaller than \c 1 are ignored.
146*/
147int QQuick3DShaderImage::width() const
148{
149 return m_width;
150}
151
152/*!
153 \qmlproperty int ShaderImage::height
154 This property holds the height of the image, in texels. It is not used
155 by an \c Image1D \l shape. The default value is \c 1. Values smaller than
156 \c 1 are ignored.
157*/
158int QQuick3DShaderImage::height() const
159{
160 return m_height;
161}
162
163/*!
164 \qmlproperty int ShaderImage::depth
165 This property holds the depth of the image, in texels. It is only used
166 by an \c Image3D \l shape. The default value is \c 1. Values smaller than
167 \c 1 are ignored.
168
169 This is a size, not the number of bits per pixel that QImage::depth()
170 returns.
171*/
172int QQuick3DShaderImage::depth() const
173{
174 return m_depth;
175}
176
177/*!
178 \qmlproperty int ShaderImage::arraySize
179 This property holds the number of images in the array. It is only used
180 by an \c Image2DArray \l shape. The default value is \c 1. Values smaller
181 than \c 1 are ignored.
182*/
183int QQuick3DShaderImage::arraySize() const
184{
185 return m_arraySize;
186}
187
188/*!
189 \qmlproperty enumeration ShaderImage::format
190 This property holds the pixel format of the image. The default value is
191 \c{ShaderImage.RGBA8}, an 8-bit-per-channel unsigned normalized RGBA
192 image.
193
194 Each value corresponds to one GLSL image format qualifier, which is what
195 the shader's \c SHADER_IMAGE declaration has to name as its format
196 argument. The two must match: a mismatch is reported as a warning and
197 leaves the image unusable by that shader.
198
199 \value ShaderImage.RGBA8 \c rgba8, the default.
200 \value ShaderImage.R8 \c r8
201 \value ShaderImage.RG8 \c rg8
202 \value ShaderImage.R16 \c r16
203 \value ShaderImage.RG16 \c rg16
204 \value ShaderImage.RGBA16F \c rgba16f
205 \value ShaderImage.RGBA32F \c rgba32f
206 \value ShaderImage.R16F \c r16f
207 \value ShaderImage.R32F \c r32f
208 \value ShaderImage.RGB10A2 \c rgb10_a2, a packed format with 10 bits
209 per color channel and 2 bits of alpha.
210 \value ShaderImage.R8SI \c r8i, a signed integer format.
211 \value ShaderImage.R32SI \c r32i
212 \value ShaderImage.RG32SI \c rg32i
213 \value ShaderImage.RGBA32SI \c rgba32i
214 \value ShaderImage.R8UI \c r8ui, an unsigned integer format.
215 \value ShaderImage.R32UI \c r32ui
216 \value ShaderImage.RG32UI \c rg32ui
217 \value ShaderImage.RGBA32UI \c rgba32ui
218
219 An integer format must be declared with a matching integer image type in
220 the shader (\c iimage2D for the signed and \c uimage2D for the unsigned
221 variants, rather than \c image2D), and is read and written with
222 \c ivec4/\c uvec4 rather than \c vec4.
223
224 \note OpenGL ES supports only a subset of these formats for images:
225 \c RGBA8, \c RGBA16F, \c RGBA32F, \c R32F, \c R32SI, \c RGBA32SI,
226 \c R32UI, and \c RGBA32UI. The other formats (\c R8, \c RG8, \c R16,
227 \c RG16, \c R16F, \c RGB10A2, \c R8SI, \c R8UI, \c RG32SI, and
228 \c RG32UI) are only available with desktop OpenGL, Vulkan, Metal, and
229 Direct3D. On OpenGL ES, a shader that declares one of them fails to
230 build. Of the supported formats, all except \c R32F, \c R32SI, and
231 \c R32UI also need a \c readonly or \c writeonly qualifier in the
232 \c SHADER_IMAGE declaration on OpenGL ES.
233
234 Setting a value outside this enumeration is ignored, with a warning, and
235 the previous format is retained.
236*/
237QQuick3DShaderImage::ImageFormat QQuick3DShaderImage::format() const
238{
239 return m_format;
240}
241
242void QQuick3DShaderImage::setShape(QQuick3DShaderImage::ImageShape shape)
243{
244 if (m_shape == shape) return;
245 if (shape < Image1D || shape > ImageCube) {
246 qWarning() << "ShaderImage shape not supported.";
247 return;
248 }
249 m_shape = shape;
250 emit shapeChanged();
251 markSizeDirty();
252}
253
254void QQuick3DShaderImage::setWidth(int width)
255{
256 if (m_width == width) return;
257 if (width < 1) return;
258 m_width = width;
259 emit widthChanged();
260 markSizeDirty();
261}
262
263void QQuick3DShaderImage::setHeight(int height)
264{
265 if (m_height == height) return;
266 if (height < 1) return;
267 m_height = height;
268 emit heightChanged();
269 markSizeDirty();
270}
271
272void QQuick3DShaderImage::setDepth(int depth)
273{
274 if (m_depth == depth) return;
275 if (depth < 1) return;
276 m_depth = depth;
277 emit depthChanged();
278 markSizeDirty();
279}
280
281void QQuick3DShaderImage::setArraySize(int arraySize)
282{
283 if (m_arraySize == arraySize) return;
284 if (arraySize < 1) return;
285 m_arraySize = arraySize;
286 emit arraySizeChanged();
287 markSizeDirty();
288}
289
290void QQuick3DShaderImage::markSizeDirty()
291{
292 m_dirtyFlags.setFlag(DirtyFlag::SizeDirty);
293 update();
294}
295
296static struct {QRhiTexture::Format rhiFormat; QQuick3DShaderImage::ImageFormat imageFormat; } supportedFormats[] =
297{
298 { QRhiTexture::RGBA8, QQuick3DShaderImage::RGBA8 },
299 { QRhiTexture::R8, QQuick3DShaderImage::R8 },
300 { QRhiTexture::RG8, QQuick3DShaderImage::RG8 },
301 { QRhiTexture::R16, QQuick3DShaderImage::R16 },
302 { QRhiTexture::RG16, QQuick3DShaderImage::RG16 },
303 { QRhiTexture::RGBA16F, QQuick3DShaderImage::RGBA16F },
304 { QRhiTexture::RGBA32F, QQuick3DShaderImage::RGBA32F },
305 { QRhiTexture::R16F, QQuick3DShaderImage::R16F },
306 { QRhiTexture::R32F, QQuick3DShaderImage::R32F },
307 { QRhiTexture::RGB10A2, QQuick3DShaderImage::RGB10A2 },
308 { QRhiTexture::R8SI, QQuick3DShaderImage::R8SI },
309 { QRhiTexture::R32SI, QQuick3DShaderImage::R32SI },
310 { QRhiTexture::RG32SI, QQuick3DShaderImage::RG32SI },
311 { QRhiTexture::RGBA32SI, QQuick3DShaderImage::RGBA32SI },
312 { QRhiTexture::R8UI, QQuick3DShaderImage::R8UI },
313 { QRhiTexture::R32UI, QQuick3DShaderImage::R32UI },
314 { QRhiTexture::RG32UI, QQuick3DShaderImage::RG32UI },
315 { QRhiTexture::RGBA32UI, QQuick3DShaderImage::RGBA32UI }
317
318
319static QRhiTexture::Format toRhiFormat(QQuick3DShaderImage::ImageFormat imageFormat)
320{
321 for (auto fmt : supportedFormats) {
322 if (fmt.imageFormat == imageFormat)
323 return fmt.rhiFormat;
324 }
325 return QRhiTexture::UnknownFormat;
326}
327
328void QQuick3DShaderImage::setFormat(QQuick3DShaderImage::ImageFormat format)
329{
330 if (m_format == format) return;
331 auto rhiFormat = toRhiFormat(format);
332 if (rhiFormat == QRhiTexture::UnknownFormat) {
333 qWarning() << "ShaderImage format not supported.";
334 return;
335 }
336 m_format = format;
337 emit formatChanged();
338 m_dirtyFlags.setFlag(DirtyFlag::FormatDirty);
339 update();
340}
341
342void QQuick3DShaderImage::markAllDirty()
343{
344 QQuick3DObject::markAllDirty();
345 m_dirtyFlags.setFlag(DirtyFlag::FormatDirty);
346 m_dirtyFlags.setFlag(DirtyFlag::SizeDirty);
347}
348
349static QSSGRenderImage::ShaderImageShape toRenderShape(QQuick3DShaderImage::ImageShape shape)
350{
351 switch (shape) {
352 case QQuick3DShaderImage::Image1D: return QSSGRenderImage::ShaderImageShape::Image1D;
353 case QQuick3DShaderImage::Image2D: return QSSGRenderImage::ShaderImageShape::Image2D;
354 case QQuick3DShaderImage::Image3D: return QSSGRenderImage::ShaderImageShape::Image3D;
355 case QQuick3DShaderImage::Image2DArray: return QSSGRenderImage::ShaderImageShape::Image2DArray;
356 case QQuick3DShaderImage::ImageCube: return QSSGRenderImage::ShaderImageShape::ImageCube;
357 }
358 return QSSGRenderImage::ShaderImageShape::Image2D;
359}
360
361QSSGRenderGraphObject *QQuick3DShaderImage::updateSpatialNode(QSSGRenderGraphObject *node)
362{
363 if (!node) {
364 markAllDirty();
365 node = new QSSGRenderImage(QSSGRenderGraphObject::Type::Image2D);
366 }
367 auto *img = static_cast<QSSGRenderImage *>(node);
368 img->m_shaderImage = true;
369 if (m_dirtyFlags.testFlag(DirtyFlag::SizeDirty)) {
370 if (m_shape == ImageCube && m_width != m_height)
371 qWarning() << "ShaderImage: a cube map image must have equal width and height.";
372 img->m_shaderImageShape = toRenderShape(m_shape);
373 img->m_shaderImageSize.x = m_width;
374 img->m_shaderImageSize.y = m_height;
375 img->m_shaderImageSize.z = m_depth;
376 img->m_arraySize = m_arraySize;
377 img->m_flags.setFlag(QSSGRenderImage::Flag::ShaderImageDirty);
378 m_dirtyFlags.setFlag(DirtyFlag::SizeDirty, false);
379 }
380 if (m_dirtyFlags.testFlag(DirtyFlag::FormatDirty)) {
381 img->m_shaderFormat = toRhiFormat(m_format);
382 img->m_flags.setFlag(QSSGRenderImage::Flag::ShaderImageDirty);
383 m_dirtyFlags.setFlag(DirtyFlag::FormatDirty, false);
384 }
385
386 return node;
387}
388
389QT_END_NAMESPACE
Combined button and popup list for selecting options.
static QRhiTexture::Format toRhiFormat(QQuick3DShaderImage::ImageFormat imageFormat)
static QSSGRenderImage::ShaderImageShape toRenderShape(QQuick3DShaderImage::ImageShape shape)