Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qquick3dindirectshadergeometry.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
4
#
include
"qquick3dindirectshadergeometry_p.h"
5
#
include
"qquick3dobject_p.h"
6
7
#
include
<
QtQuick3DRuntimeRender
/
private
/
qssgrendergeometry_p
.
h
>
8
#
include
<
QtQuick3DRuntimeRender
/
private
/
qssgrenderbuffer_p
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
/*!
13
\qmltype IndirectShaderGeometry
14
\inherits Geometry
15
\inqmlmodule QtQuick3D
16
\since 6.13
17
\brief Geometry generated in the vertex shader, with the draw arguments read from a GPU buffer.
18
19
Like \l ShaderGeometry, IndirectShaderGeometry has no vertex data: each
20
position is derived in the vertex shader from \c VERTEX_INDEX. The
21
difference is where the vertex count comes from. ShaderGeometry takes it
22
from a property on the CPU side, whereas IndirectShaderGeometry reads it,
23
together with the instance count, from a \l ShaderStorageBuffer at draw
24
time. This means a \l ComputePass can decide how much to draw without the
25
count having to be read back to the CPU.
26
27
The \l buffer holds one or more draw commands in the layout used by
28
\c glDrawArraysIndirect and \c vkCmdDrawIndirect, four 32-bit unsigned
29
integers each:
30
31
\badcode
32
struct DrawCommand {
33
uint vertexCount;
34
uint instanceCount;
35
uint firstVertex;
36
uint firstInstance;
37
};
38
\endcode
39
40
The instance count is part of the command, and \c INSTANCE_INDEX is
41
available in the vertex shader. For a shaded material to place the
42
instances with \c INSTANCE_TRANSFORM, set
43
\l{Model::instancing}{instancing} to a \l ShaderInstancing, whose
44
\l{ShaderInstancing::instanceCount}{instanceCount} is then ignored. If
45
the \l Model has other \l{Model::instancing}{instancing} set, its
46
per-instance data is still available to the shader, but the number of
47
instances drawn is determined by the buffer.
48
\note Keep \c firstInstance at zero: the behavior when \c{firstInstance != 0} differs between graphics APIs.
49
50
The following example lets a compute shader decide the number of
51
triangles, and draws them:
52
53
\qml
54
View3D {
55
ShaderStorageBuffer {
56
id: argsBuffer
57
size: 16
58
}
59
60
Model {
61
geometry: IndirectShaderGeometry { buffer: argsBuffer }
62
materials: CustomMaterial { vertexShader: "triangles.vert" }
63
64
ComputePass {
65
computeShader: "count.comp"
66
property ShaderStorageBuffer drawArgs: argsBuffer
67
commands: [ Dispatch { groupCountX: 1 } ]
68
}
69
}
70
}
71
\endqml
72
73
where \c count.comp writes the command:
74
75
\badcode
76
layout(local_size_x = 1) in;
77
78
STORAGE_BUFFER(drawArgs, writeonly) {
79
uint vertexCount;
80
uint instanceCount;
81
uint firstVertex;
82
uint firstInstance;
83
}
84
85
void main()
86
{
87
vertexCount = 3 * 100;
88
instanceCount = 1;
89
firstVertex = 0;
90
firstInstance = 0;
91
}
92
\endcode
93
94
\note IndirectShaderGeometry must be used with a \l CustomMaterial that has
95
a vertex shader, as with \l ShaderGeometry.
96
97
\note Indirect drawing requires a graphics API that supports it: OpenGL 4.0
98
or newer, OpenGL ES 3.1 or newer, or Vulkan, Metal, or Direct3D. Otherwise
99
nothing is drawn, and a warning is printed.
100
101
\note If the \l buffer gets its content from a
102
\l{ShaderStorageBuffer::bufferProvider}{bufferProvider}, the QRhiBuffer that
103
the render extension supplies must have been created with the
104
QRhiBuffer::IndirectBuffer usage flag.
105
106
\note As with \l ShaderGeometry, the model is not frustum culled and casts
107
no shadows unless \l boundsMinimum and \l boundsMaximum are set. Ray
108
picking hits the box given by the bounds. Without bounds, or with
109
\l ShaderInstancing, the model cannot be picked. Morph targets,
110
skinning, lightmap baking and motion vectors are not supported.
111
112
\sa ShaderGeometry, ShaderInstancing, ShaderStorageBuffer, ComputePass
113
*/
114
115
/*!
116
\qmlproperty enumeration IndirectShaderGeometry::primitiveType
117
118
How the vertices are assembled into primitives. See
119
\l{ShaderGeometry::primitiveType}.
120
*/
121
122
/*!
123
\qmlproperty vector3d IndirectShaderGeometry::boundsMinimum
124
125
One corner of the box the generated vertices stay inside. See
126
\l{ShaderGeometry::boundsMinimum}.
127
*/
128
129
/*!
130
\qmlproperty vector3d IndirectShaderGeometry::boundsMaximum
131
132
The opposite corner to \l boundsMinimum.
133
*/
134
135
// The buffer's backend node can be created after ours, see updateSpatialNode().
136
QQuick3DIndirectShaderGeometryPrivate::QQuick3DIndirectShaderGeometryPrivate()
137
: QQuick3DShaderGeometryBasePrivate(Flags::RequiresSecondaryUpdate)
138
{
139
}
140
141
QQuick3DIndirectShaderGeometry::QQuick3DIndirectShaderGeometry(QQuick3DObject *parent)
142
: QQuick3DShaderGeometryBase(*
new
QQuick3DIndirectShaderGeometryPrivate, parent)
143
{
144
updateData();
145
}
146
147
QQuick3DIndirectShaderGeometry::~QQuick3DIndirectShaderGeometry() { }
148
149
/*!
150
\qmlproperty ShaderStorageBuffer IndirectShaderGeometry::buffer
151
152
The buffer containing the draw commands. If this is not set,
153
nothing is drawn.
154
*/
155
156
QQuick3DShaderStorageBuffer *QQuick3DIndirectShaderGeometry::buffer()
const
157
{
158
Q_D(
const
QQuick3DIndirectShaderGeometry);
159
return
d->m_buffer;
160
}
161
162
void
QQuick3DIndirectShaderGeometry::setBuffer(QQuick3DShaderStorageBuffer *buffer)
163
{
164
Q_D(QQuick3DIndirectShaderGeometry);
165
if
(d->m_buffer == buffer)
166
return
;
167
QQuick3DObjectPrivate::attachWatcher(
this
, &QQuick3DIndirectShaderGeometry::setBuffer, buffer, d->m_buffer);
168
d->m_buffer = buffer;
169
updateData();
170
update();
171
emit bufferChanged();
172
}
173
174
/*!
175
\qmlproperty int IndirectShaderGeometry::offset
176
177
The byte offset of the first draw command in \l buffer. It must be a
178
multiple of 4. The default value is 0.
179
*/
180
181
int
QQuick3DIndirectShaderGeometry::offset()
const
182
{
183
Q_D(
const
QQuick3DIndirectShaderGeometry);
184
return
d->m_offset;
185
}
186
187
void
QQuick3DIndirectShaderGeometry::setOffset(
int
offset)
188
{
189
Q_D(QQuick3DIndirectShaderGeometry);
190
if
(d->m_offset == offset)
191
return
;
192
if
(offset < 0 || offset % 4 != 0) {
193
qWarning(
"IndirectShaderGeometry.offset must be a non-negative multiple of 4"
);
194
return
;
195
}
196
d->m_offset = offset;
197
update();
198
emit offsetChanged();
199
}
200
201
/*!
202
\qmlproperty int IndirectShaderGeometry::drawCount
203
204
The number of draw commands to read from \l buffer. Zero draws nothing.
205
The default value is 1.
206
207
Values greater than 1 will be emulated by separate draw calls if the
208
GPU does not support multi-draw indirect.
209
*/
210
211
int
QQuick3DIndirectShaderGeometry::drawCount()
const
212
{
213
Q_D(
const
QQuick3DIndirectShaderGeometry);
214
return
d->m_drawCount;
215
}
216
217
void
QQuick3DIndirectShaderGeometry::setDrawCount(
int
drawCount)
218
{
219
Q_D(QQuick3DIndirectShaderGeometry);
220
if
(d->m_drawCount == drawCount)
221
return
;
222
if
(drawCount < 0) {
223
qWarning(
"IndirectShaderGeometry.drawCount cannot be negative"
);
224
return
;
225
}
226
d->m_drawCount = drawCount;
227
updateData();
228
update();
229
emit drawCountChanged();
230
}
231
232
/*!
233
\qmlproperty int IndirectShaderGeometry::stride
234
235
The distance in bytes between consecutive draw commands in \l buffer. It
236
must be a multiple of 4, and at least 16, the size of one command. The
237
default value is 16.
238
*/
239
240
int
QQuick3DIndirectShaderGeometry::stride()
const
241
{
242
Q_D(
const
QQuick3DIndirectShaderGeometry);
243
return
d->m_drawStride;
244
}
245
246
void
QQuick3DIndirectShaderGeometry::setStride(
int
stride)
247
{
248
Q_D(QQuick3DIndirectShaderGeometry);
249
if
(d->m_drawStride == stride)
250
return
;
251
if
(stride < 16 || stride % 4 != 0) {
252
qWarning(
"IndirectShaderGeometry.stride must be a multiple of 4 and at least 16"
);
253
return
;
254
}
255
d->m_drawStride = stride;
256
update();
257
emit strideChanged();
258
}
259
260
// A placeholder count: it only marks the geometry as drawable, the real
261
// count is read from the buffer on the GPU.
262
quint32 QQuick3DIndirectShaderGeometry::subsetCount()
const
263
{
264
Q_D(
const
QQuick3DIndirectShaderGeometry);
265
return
(d->m_buffer && d->m_drawCount > 0) ? 1 : 0;
266
}
267
268
void
QQuick3DIndirectShaderGeometry::itemChange(ItemChange change,
const
ItemChangeData &value)
269
{
270
Q_D(QQuick3DIndirectShaderGeometry);
271
QQuick3DShaderGeometryBase::itemChange(change, value);
272
if
(change == QQuick3DObject::ItemSceneChange && d->m_buffer) {
273
if
(value.sceneManager)
274
QQuick3DObjectPrivate::refSceneManager(d->m_buffer, *value.sceneManager);
275
else
276
QQuick3DObjectPrivate::derefSceneManager(d->m_buffer);
277
}
278
}
279
280
QSSGRenderGraphObject *QQuick3DIndirectShaderGeometry::updateSpatialNode(QSSGRenderGraphObject *node)
281
{
282
Q_D(QQuick3DIndirectShaderGeometry);
283
node = QQuick3DShaderGeometryBase::updateSpatialNode(node);
284
auto
*geometry =
static_cast
<QSSGRenderGeometry *>(node);
285
286
QSSGRenderGeometry::IndirectDraw indirectDraw;
287
indirectDraw.enabled =
true
;
288
if
(d->m_buffer) {
289
indirectDraw.buffer =
static_cast
<QSSGRenderBuffer *>(QQuick3DObjectPrivate::get(d->m_buffer)->spatialNode);
290
if
(!indirectDraw.buffer)
291
d->requestSecondaryUpdate();
292
}
293
indirectDraw.offset = quint32(d->m_offset);
294
indirectDraw.drawCount = quint32(d->m_drawCount);
295
indirectDraw.stride = quint32(d->m_drawStride);
296
geometry->setIndirectDraw(indirectDraw);
297
298
return
node;
299
}
300
301
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtquick3d
src
quick3d
qquick3dindirectshadergeometry.cpp
Generated on
for Qt by
1.18.0