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
qquick3dshaderstoragebuffer.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
9#include <ssg/qssgrenderextensions.h>
10
11#include <QtQuick3DRuntimeRender/private/qssgrenderbuffer_p.h>
12
14
15/*!
16 \qmltype ShaderStorageBuffer
17 \inherits Object3D
18 \inqmlmodule QtQuick3D
19 \brief Declares a GPU buffer resource that shaders can read or write.
20 \since 6.13
21
22 A ShaderStorageBuffer is a GPU-side shader storage buffer, with content
23 that is either uploaded from the CPU (via \l bufferData) or produced
24 entirely on the GPU (by a \l{ComputePass}{ComputePass}, a
25 \l CustomMaterial, or an \l Effect writing to it). It is meant to be
26 read from and/or written to directly in shader code, rather than used
27 as a regular uniform buffer.
28
29 A ShaderStorageBuffer is exposed to a shader by using it as a property in the
30 ComputePass, RenderPass, Effect, or CustomMaterial:
31
32 \qml
33 ShaderStorageBuffer {
34 id: myBuffer
35 size: 1024
36 }
37
38 ComputePass {
39 computeShader: "generate.comp"
40 property ShaderStorageBuffer targetBuffer: myBuffer
41 }
42 \endqml
43
44 In the shader, the property is declared with the \c STORAGE_BUFFER
45 macro, using the same name as the QML property (\c targetBuffer in the
46 example above):
47
48 \badcode
49 STORAGE_BUFFER(targetBuffer, writeonly) {
50 vec4 values[];
51 }
52
53 void main() {
54 values[gl_GlobalInvocationID.x] = vec4(1.0);
55 }
56 \endcode
57
58 The name can be followed by any combination of the memory qualifiers
59 \c readonly, \c writeonly, \c coherent, \c volatile, and \c restrict,
60 separated by spaces or commas, matching the GLSL memory qualifiers of the
61 same names. Without qualifiers, as in \c{STORAGE_BUFFER(targetBuffer)},
62 the buffer has implicit read-write access. Any other word is rejected
63 with a warning, and the shader cannot be used.
64
65 \note The declaration is recognized by scanning the shader source before
66 any preprocessing takes place. The name and the memory qualifiers must
67 therefore be written out as plain identifiers; \c{#define}'d values are
68 not recognized. Each name can be declared only once in a shader, even in
69 different preprocessor branches, while declarations inside comments are
70 ignored. Like other GLSL interface blocks, the block can end with a
71 semicolon. A declaration that does not follow this form is reported with
72 a warning, and the shader cannot be used.
73
74 A buffer without \l bufferData or \l bufferProvider is filled with zeros
75 whenever its storage is allocated, unless
76 \l zeroInitialized is set to \c false. Call \l requestClear() to reset the
77 content at a later point without a dedicated compute pass, for example
78 when restarting a simulation. Resets that have to be done every frame are
79 better done in a ComputePass.
80
81 \sa ShaderStorageBufferData, ComputePass, ShaderImage
82*/
83
84QQuick3DShaderStorageBuffer::QQuick3DShaderStorageBuffer(QQuick3DObject *parent)
85 : QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::ShaderStorageBuffer, QQuick3DObjectPrivate::Flags::RequiresSecondaryUpdate)), parent)
86{
87
88}
89
90QQuick3DShaderStorageBuffer::~QQuick3DShaderStorageBuffer()
91{
92 QObject::disconnect(m_modifiedConnection);
93}
94
95/*!
96 \qmlproperty int ShaderStorageBuffer::size
97 This property holds the size of the buffer, in bytes.
98
99 Setting \l bufferData automatically updates this property to match the
100 size of the assigned data; setting \c size directly is meant for
101 buffers whose content is produced entirely on the GPU, without a
102 \l bufferData. Setting \c size is ignored if \l bufferData or \l bufferProvider
103 is also set.
104
105 \sa zeroInitialized
106*/
107quint32 QQuick3DShaderStorageBuffer::size() const
108{
109 return m_size;
110}
111
112/*!
113 \qmlproperty RenderExtension ShaderStorageBuffer::bufferProvider
114 This property holds a \l RenderExtension that is responsible for
115 providing this buffer's backing GPU resource, for advanced use cases
116 where the buffer needs to be created and managed by custom C++
117 rendering code rather than by the engine.
118
119 \note When \c bufferProvider is set, the \l size is \e{not} automatically updated to
120 match the size provided, but setting size manually is still ignored.
121*/
122QQuick3DRenderExtension *QQuick3DShaderStorageBuffer::bufferProvider() const
123{
124 return m_provider;
125}
126
127/*!
128 \qmlproperty ShaderStorageBufferData ShaderStorageBuffer::bufferData
129 This property holds the \l ShaderStorageBufferData providing this buffer's
130 content from the CPU. Assigning it uploads the data returned by
131 \l{QQuick3DShaderStorageBufferData::}{bufferData()} to the GPU and updates \l size to
132 match; setting it to \c null resets \l size to \c 0.
133*/
134QQuick3DShaderStorageBufferData *QQuick3DShaderStorageBuffer::bufferData() const
135{
136 return m_data;
137}
138
139/*!
140 \qmlproperty bool ShaderStorageBuffer::zeroInitialized
141 This property determines whether the buffer is filled with zeros whenever
142 its storage is allocated. The default value is \c true.
143
144 The storage is allocated when the buffer is first used, and again when a
145 change of \l size requires a new allocation. The previous content is not
146 preserved across a new allocation. Changing this property only affects
147 later allocations.
148
149 Set it to \c false when the shaders that use the buffer write all of it
150 before reading it, to avoid the cost of the clear. The content of the
151 buffer is then undefined until it is written.
152
153 \note With OpenGL ES, the clear is performed by a compute dispatch.
154
155 This property has no effect when \l bufferData or \l bufferProvider is set.
156
157 \sa requestClear()
158*/
159bool QQuick3DShaderStorageBuffer::zeroInitialized() const
160{
161 return m_zeroInitialized;
162}
163
164void QQuick3DShaderStorageBuffer::setZeroInitialized(bool zeroInitialized)
165{
166 if (m_zeroInitialized == zeroInitialized)
167 return;
168 m_zeroInitialized = zeroInitialized;
169 m_dirtyFlags.setFlag(DirtyFlag::ZeroInitializedDirty);
170 emit zeroInitializedChanged();
171 update();
172}
173
174/*!
175 \qmlmethod void ShaderStorageBuffer::requestClear(int value)
176
177 Requests that every byte of the buffer is set to \a value, which must be
178 in the range 0 to 255, and requests a new frame. The default \a value
179 is 0. The clear is performed before the buffer is next used by a shader.
180 Several calls before that result in a single clear, with the last
181 \a value. A \l RenderExtension that gets the buffer with
182 QSSGFrameData::getRhiResource() in QSSGRenderExtension::prepareData()
183 triggers the clear; later in the frame it does not, and sees the previous
184 content until a shader has used the buffer.
185
186 This makes occasional resets, such as restarting a simulation, possible
187 without a dedicated compute pass. Resets that have to be done every
188 frame, for example of counters or accumulators, are better done in a
189 ComputePass that runs before the passes that use the buffer. The pass is
190 then ordered against the other passes, and does not depend on when the
191 QML code calling this method runs relative to the rendered frames.
192
193 \note With OpenGL ES, the clear is performed by a compute dispatch.
194
195 Since \a value is a byte, 0 clears integers and floats to zero, and 255
196 sets signed integers to -1, unsigned integers to their maximum value, and
197 floats to NaN.
198
199 The clear also applies when \l bufferData is set, and replaces its
200 content. The buffer then stays cleared until \l bufferData provides new
201 content. If that happens before the clear is performed, the new content
202 replaces the clear.
203
204 This method has no effect when \l bufferProvider is set. The provider owns
205 the buffer, and clearing it is the provider's responsibility.
206
207 \sa zeroInitialized
208*/
209void QQuick3DShaderStorageBuffer::requestClear(int value)
210{
211 if (m_provider) {
212 qWarning() << "ShaderStorageBuffer::requestClear(): not supported when bufferProvider is set";
213 return;
214 }
215 if (value < 0 || value > 255) {
216 qWarning() << "ShaderStorageBuffer::requestClear(): value" << value << "is not in the range 0 to 255";
217 return;
218 }
219 m_clearRequested = true;
220 m_clearValue = quint8(value);
221 update();
222}
223
224void QQuick3DShaderStorageBuffer::setBufferProvider(QQuick3DRenderExtension *provider)
225{
226 if (m_provider == provider)
227 return;
228
229 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DShaderStorageBuffer::setBufferProvider, provider, m_provider);
230
231 m_provider = provider;
232
233 m_dirtyFlags.setFlag(DirtyFlag::ExtensionDirty);
234
235 emit bufferProviderChanged();
236 update();
237}
238
239void QQuick3DShaderStorageBuffer::setSize(quint32 size)
240{
241 if (m_data || m_provider) {
242 qWarning() << "Size is set automatically from bufferData or bufferProvider. Setting size manually ignored.";
243 return;
244 }
245 setSizePriv(size);
246}
247
248void QQuick3DShaderStorageBuffer::setSizePriv(quint32 size)
249{
250 if (m_size == size) return;
251 m_size = size;
252 m_dirtyFlags.setFlag(DirtyFlag::SizeDirty);
253 emit sizeChanged();
254 update();
255}
256
257void QQuick3DShaderStorageBuffer::setBufferData(QQuick3DShaderStorageBufferData *data)
258{
259 if (m_data == data) return;
260 QObject::disconnect(m_modifiedConnection);
261 QObject::disconnect(m_destroyedConnection);
262 m_data = data;
263 if (data) {
264 m_destroyedConnection = QObject::connect(data, &QObject::destroyed, this, [this]() {
265 setBufferData(nullptr);
266 });
267 m_modifiedConnection = QObject::connect(data, &QQuick3DShaderStorageBufferData::bufferDataDirty, this, [&](){
268 m_dirtyFlags.setFlag(DirtyFlag::DataDirty);
269 setSizePriv(m_data->bufferData().size());
270 update();
271 });
272 setSizePriv(m_data->bufferData().size());
273 } else {
274 setSizePriv(0);
275 }
276 m_dirtyFlags.setFlag(DirtyFlag::DataDirty);
277 emit bufferDataChanged();
278 update();
279}
280
281void QQuick3DShaderStorageBuffer::markAllDirty()
282{
283 m_dirtyFlags = DirtyFlags(0xFFFF);
284 QQuick3DObject::markAllDirty();
285}
286
287void QQuick3DShaderStorageBuffer::itemChange(QQuick3DObject::ItemChange change, const QQuick3DObject::ItemChangeData &value)
288{
289 QQuick3DObject::itemChange(change, value);
290 if (change == QQuick3DObject::ItemChange::ItemSceneChange) {
291 if (m_provider) {
292 const auto &sceneManager = value.sceneManager;
293 if (sceneManager)
294 QQuick3DObjectPrivate::refSceneManager(m_provider, *sceneManager);
295 else
296 QQuick3DObjectPrivate::derefSceneManager(m_provider);
297 }
298 }
299}
300
301QSSGRenderGraphObject *QQuick3DShaderStorageBuffer::updateSpatialNode(QSSGRenderGraphObject *node)
302{
303 if (!node) {
304 markAllDirty();
305 node = new QSSGRenderBuffer();
306 }
307 QQuick3DObject::updateSpatialNode(node);
308 auto bufferNode = static_cast<QSSGRenderBuffer *>(node);
309
310 if (m_dirtyFlags.testFlag(DirtyFlag::SizeDirty)) {
311 m_dirtyFlags.setFlag(DirtyFlag::SizeDirty, false);
312
313 bufferNode->m_size = m_size;
314 bufferNode->m_flags.setFlag(QSSGRenderBuffer::Flag::Dirty);
315 }
316
317 if (m_dirtyFlags.testFlag(DirtyFlag::ZeroInitializedDirty)) {
318 m_dirtyFlags.setFlag(DirtyFlag::ZeroInitializedDirty, false);
319 bufferNode->m_zeroInitialized = m_zeroInitialized;
320 }
321
322 if (m_clearRequested) {
323 m_clearRequested = false;
324 bufferNode->m_clearValue = m_clearValue;
325 bufferNode->m_flags.setFlag(QSSGRenderBuffer::Flag::ClearRequested);
326 }
327
328 if (m_dirtyFlags.testFlag(DirtyFlag::ExtensionDirty)) {
329 QSSGRenderExtension *extensionsSource = nullptr;
330 bool waitingForProvider = false;
331 if (m_provider) {
332 auto *sn = QQuick3DObjectPrivate::get(m_provider)->spatialNode;
333 // NOTE: We don't clear if we haven't gotten the spatial node yet, as
334 // we'll be called once _again_ when the extensions have been processed.
335 waitingForProvider = (sn == nullptr);
336 if (sn && QSSG_GUARD(QSSGRenderGraphObject::isExtension(sn->type)))
337 extensionsSource = static_cast<QSSGRenderExtension *>(sn);
338 }
339
340 if (!waitingForProvider && bufferNode->m_extensionsSource != extensionsSource) {
341 bufferNode->m_extensionsSource = extensionsSource;
342 bufferNode->m_flags.setFlag(QSSGRenderBuffer::Flag::Dirty);
343 }
344
345 if (waitingForProvider)
346 QQuick3DObjectPrivate::get(this)->requestSecondaryUpdate();
347 m_dirtyFlags.setFlag(DirtyFlag::ExtensionDirty, waitingForProvider);
348 }
349 if (m_dirtyFlags.testFlag(DirtyFlag::DataDirty)) {
350 if (m_data)
351 bufferNode->m_data = m_data->bufferData();
352 else
353 bufferNode->m_data.clear();
354 bufferNode->m_flags.setFlag(QSSGRenderBuffer::Flag::Dirty);
355 bufferNode->m_flags.setFlag(QSSGRenderBuffer::Flag::DataDirty);
356 m_dirtyFlags.setFlag(DirtyFlag::DataDirty, false);
357 }
358
359 return bufferNode;
360}
361
362QT_END_NAMESPACE
Combined button and popup list for selecting options.