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
qquick3dshadergeometry.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
5
6#include <cfloat>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype ShaderGeometry
12 \inherits Geometry
13 \inqmlmodule QtQuick3D
14 \since 6.13
15 \brief Geometry with no vertex data, generated in the vertex shader.
16
17 Draws \l vertexCount vertices with no vertex buffer. Each position is
18 derived in the vertex shader from \c VERTEX_INDEX.
19
20 This is for geometry that is cheaper to compute than to upload from CPU memory,
21 or that is already on the GPU in some other form, e.g. a storage buffer written
22 by a compute shader.
23
24 \note ShaderGeometry must be used with a \l CustomMaterial that has a vertex shader.
25 A warning will be printed otherwise.
26
27 A subdivided plane is a simple example of where this can be useful. It does not need stored
28 data, since every vertex position is a function of the index.
29
30 \badcode
31 Model {
32 geometry: ShaderGeometry {
33 vertexCount: 127 * 127 * 6 // two triangles per grid cell
34 boundsMinimum: Qt.vector3d(-2000, -300, -2000)
35 boundsMaximum: Qt.vector3d(2000, 300, 2000)
36 }
37 materials: CustomMaterial { vertexShader: "grid.vert" }
38 }
39 \endcode
40
41 where \c grid.vert turns the index into a position:
42
43 \badcode
44 void MAIN()
45 {
46 int tri = VERTEX_INDEX / 3;
47 int corner = VERTEX_INDEX % 3;
48 // ... derive x and z from the cell, y from wherever you like
49 VERTEX = vec3(x, y, z);
50 }
51 \endcode
52
53 The shader above is for a \e shaded material. For an \e unshaded
54 \l CustomMaterial, there is no generated code, so \c VERTEX has no effect.
55 In this case, the shader has to calculate \c POSITION itself:
56
57 \badcode
58 void MAIN()
59 {
60 // ... derive pos from VERTEX_INDEX as above
61 POSITION = MODELVIEWPROJECTION_MATRIX * vec4(pos, 1.0);
62 }
63 \endcode
64
65 \note Set \l boundsMinimum and \l boundsMaximum to enable frustum
66 culling, and also to let the geometry cast a shadow. If bounds are not set,
67 the extent cannot be known.
68
69 \note Morph targets, skinning, per-vertex
70 \l{Model::pickable}{picking} and lightmap baking are all unavailable, since they need real
71 vertex data. Ray picking hits the box given by \l boundsMinimum and
72 \l boundsMaximum instead. Without bounds, or with \l ShaderInstancing, the
73 model cannot be picked. Do not set
74 \l{Model::usedInBakedLighting}{usedInBakedLighting} on the model: lightmap
75 baking will skip it with a warning.
76
77 \note The model does not contribute motion vectors, so effects relying on
78 them, such as temporal anti-aliasing, do not see its motion.
79
80 \sa IndirectShaderGeometry, ShaderInstancing, CustomMaterial
81*/
82
83QQuick3DShaderGeometryBasePrivate::QQuick3DShaderGeometryBasePrivate(Flags flags)
84 : QQuick3DGeometryPrivate(flags)
85{
86}
87
88QQuick3DShaderGeometryBase::QQuick3DShaderGeometryBase(QQuick3DShaderGeometryBasePrivate &dd, QQuick3DObject *parent)
89 : QQuick3DGeometry(dd, parent)
90{
91}
92
93QQuick3DShaderGeometryBase::~QQuick3DShaderGeometryBase() { }
94
95QQuick3DShaderGeometry::QQuick3DShaderGeometry(QQuick3DObject *parent)
96 : QQuick3DShaderGeometryBase(*new QQuick3DShaderGeometryPrivate, parent)
97{
98 updateData();
99}
100
101QQuick3DShaderGeometry::~QQuick3DShaderGeometry() { }
102
103/*!
104 \qmlproperty int ShaderGeometry::vertexCount
105
106 The number of vertices to draw. Zero, the default, draws nothing.
107*/
108
109int QQuick3DShaderGeometry::vertexCount() const
110{
111 Q_D(const QQuick3DShaderGeometry);
112 return d->m_vertexCount;
113}
114
115void QQuick3DShaderGeometry::setVertexCount(int vertexCount)
116{
117 Q_D(QQuick3DShaderGeometry);
118 if (d->m_vertexCount == vertexCount)
119 return;
120 if (vertexCount < 0) {
121 qWarning("ShaderGeometry.vertexCount cannot be negative");
122 return;
123 }
124 d->m_vertexCount = vertexCount;
125 updateData();
126 update();
127 emit vertexCountChanged();
128}
129
130quint32 QQuick3DShaderGeometry::subsetCount() const
131{
132 Q_D(const QQuick3DShaderGeometry);
133 return quint32(d->m_vertexCount);
134}
135
136/*!
137 \qmlproperty enumeration ShaderGeometry::primitiveType
138
139 How the vertices are assembled into primitives. The values are
140 the same as used by \l{QQuick3DGeometry::setPrimitiveType}:
141
142 \value ShaderGeometry.Points
143 \value ShaderGeometry.LineStrip
144 \value ShaderGeometry.Lines
145 \value ShaderGeometry.TriangleStrip
146 \value ShaderGeometry.TriangleFan (not supported on all APIs)
147 \value ShaderGeometry.Triangles (default)
148*/
149
150QQuick3DGeometry::PrimitiveType QQuick3DShaderGeometryBase::primitive() const
151{
152 Q_D(const QQuick3DShaderGeometryBase);
153 return d->m_primitive;
154}
155
156// We can't name it setPrimitiveType, because of QQuick3DGeometry::setPrimitiveType
157
158void QQuick3DShaderGeometryBase::setPrimitive(PrimitiveType primitive)
159{
160 Q_D(QQuick3DShaderGeometryBase);
161 if (d->m_primitive == primitive)
162 return;
163 d->m_primitive = primitive;
164 updateData();
165 update();
166 emit primitiveChanged();
167}
168
169/*!
170 \qmlproperty vector3d ShaderGeometry::boundsMinimum
171
172 One corner of the box the generated vertices stay inside. Set it, together
173 with \l boundsMaximum, to enable frustum culling and to let the geometry
174 cast a shadow.
175
176 \sa boundsMaximum
177*/
178
179QVector3D QQuick3DShaderGeometryBase::boundsMinimum() const
180{
181 Q_D(const QQuick3DShaderGeometryBase);
182 return d->m_boundsMinimum;
183}
184
185void QQuick3DShaderGeometryBase::setBoundsMinimum(const QVector3D &boundsMinimum)
186{
187 Q_D(QQuick3DShaderGeometryBase);
188 if (d->m_boundsSet && d->m_boundsMinimum == boundsMinimum)
189 return;
190 d->m_boundsMinimum = boundsMinimum;
191 d->m_boundsSet = true;
192 updateData();
193 update();
194 emit boundsMinimumChanged();
195}
196
197/*!
198 \qmlproperty vector3d ShaderGeometry::boundsMaximum
199
200 The opposite corner to \l boundsMinimum.
201
202 \sa boundsMinimum
203*/
204
205QVector3D QQuick3DShaderGeometryBase::boundsMaximum() const
206{
207 Q_D(const QQuick3DShaderGeometryBase);
208 return d->m_boundsMaximum;
209}
210
211void QQuick3DShaderGeometryBase::setBoundsMaximum(const QVector3D &boundsMaximum)
212{
213 Q_D(QQuick3DShaderGeometryBase);
214 if (d->m_boundsSet && d->m_boundsMaximum == boundsMaximum)
215 return;
216 d->m_boundsMaximum = boundsMaximum;
217 d->m_boundsSet = true;
218 updateData();
219 update();
220 emit boundsMaximumChanged();
221}
222
223void QQuick3DShaderGeometryBase::updateData()
224{
225 Q_D(QQuick3DShaderGeometryBase);
226 clear();
227 setPrimitiveType(d->m_primitive);
228
229 QVector3D min = d->m_boundsMinimum;
230 QVector3D max = d->m_boundsMaximum;
231 if (!d->m_boundsSet) {
232 // Return an empty box, since a zero-sized one will be treated as a point.
233 min = QVector3D(FLT_MAX, FLT_MAX, FLT_MAX);
234 max = -min;
235 }
236 setBounds(min, max);
237
238 // Add a subset with count but no data to distinguish this from an empty geometry.
239 if (const quint32 count = subsetCount())
240 addSubset(0, count, min, max);
241}
242
243QT_END_NAMESPACE