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
qsggeometry.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSGGEOMETRY_H
5#define QSGGEOMETRY_H
6
7#include <QtQuick/qtquickglobal.h>
8#include <QtCore/QRectF>
9
10QT_BEGIN_NAMESPACE
11
12class QSGGeometryData;
13
14class Q_QUICK_EXPORT QSGGeometry
15{
16public:
17 enum AttributeType {
18 UnknownAttribute,
19 PositionAttribute,
20 ColorAttribute,
21 TexCoordAttribute,
22 TexCoord1Attribute,
23 TexCoord2Attribute
24 };
25
26 enum DataPattern {
27 AlwaysUploadPattern = 0,
28 StreamPattern = 1,
29 DynamicPattern = 2,
30 StaticPattern = 3
31 };
32
33 enum DrawingMode {
34 DrawPoints = 0x0000,
35 DrawLines = 0x0001,
36 DrawLineLoop = 0x0002,
37 DrawLineStrip = 0x0003,
38 DrawTriangles = 0x0004,
39 DrawTriangleStrip = 0x0005,
40 DrawTriangleFan = 0x0006
41 };
42
43 enum Type {
44 ByteType = 0x1400,
45 UnsignedByteType = 0x1401,
46 ShortType = 0x1402,
47 UnsignedShortType = 0x1403,
48 IntType = 0x1404,
49 UnsignedIntType = 0x1405,
50 FloatType = 0x1406,
51 Bytes2Type = 0x1407,
52 Bytes3Type = 0x1408,
53 Bytes4Type = 0x1409,
54 DoubleType = 0x140A
55 };
56
57 struct Q_QUICK_EXPORT Attribute
58 {
59 int position;
60 int tupleSize;
61 int type;
62
63 uint isVertexCoordinate : 1;
64
65 AttributeType attributeType : 4;
66
67 uint reserved : 27;
68
69 static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition = false);
70 static Attribute createWithAttributeType(int pos, int tupleSize, int primitiveType, AttributeType attributeType);
71 };
72
73 struct AttributeSet {
74 int count;
75 int stride;
76 const Attribute *attributes;
77 };
78
79 struct Point2D {
80 float x, y;
81 void set(float nx, float ny) {
82 x = nx; y = ny;
83 }
84 };
85 struct TexturedPoint2D {
86 float x, y;
87 float tx, ty;
88 void set(float nx, float ny, float ntx, float nty) {
89 x = nx; y = ny; tx = ntx; ty = nty;
90 }
91 };
92 struct ColoredPoint2D {
93 float x, y;
94 unsigned char r, g, b, a;
95 void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
96 x = nx; y = ny;
97 r = nr;
98 g = ng;
99 b = nb;
100 a = na;
101 }
102 };
103
104 static const AttributeSet &defaultAttributes_Point2D();
105 static const AttributeSet &defaultAttributes_TexturedPoint2D();
106 static const AttributeSet &defaultAttributes_ColoredPoint2D();
107
108 QSGGeometry(const QSGGeometry::AttributeSet &attribs,
109 int vertexCount,
110 int indexCount = 0,
111 int indexType = UnsignedShortType);
112 virtual ~QSGGeometry();
113
114 // must use unsigned int to be compatible with the old GLenum to keep BC
115 void setDrawingMode(unsigned int mode);
116 inline unsigned int drawingMode() const { return m_drawing_mode; }
117
118 void allocate(int vertexCount, int indexCount = 0);
119
120 int vertexCount() const { return m_vertex_count; }
121 void setVertexCount(int count);
122
123 void *vertexData() { return m_data; }
124 inline Point2D *vertexDataAsPoint2D();
125 inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
126 inline ColoredPoint2D *vertexDataAsColoredPoint2D();
127
128 inline const void *vertexData() const { return m_data; }
129 inline const Point2D *vertexDataAsPoint2D() const;
130 inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
131 inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
132
133 inline int indexType() const { return m_index_type; }
134
135 int indexCount() const { return m_index_count; }
136 void setIndexCount(int count);
137
138 void *indexData();
139 inline uint *indexDataAsUInt();
140 inline quint16 *indexDataAsUShort();
141
142 inline int sizeOfIndex() const;
143
144 const void *indexData() const;
145 inline const uint *indexDataAsUInt() const;
146 inline const quint16 *indexDataAsUShort() const;
147
148 inline int attributeCount() const { return m_attributes.count; }
149 inline const Attribute *attributes() const { return m_attributes.attributes; }
150 inline int sizeOfVertex() const { return m_attributes.stride; }
151
152 static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
153 static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
154 static void updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect);
155
156 void setIndexDataPattern(DataPattern p);
157 DataPattern indexDataPattern() const { return DataPattern(m_index_usage_pattern); }
158
159 void setVertexDataPattern(DataPattern p);
160 DataPattern vertexDataPattern() const { return DataPattern(m_vertex_usage_pattern); }
161
162 void markIndexDataDirty();
163 void markVertexDataDirty();
164
165 float lineWidth() const;
166 void setLineWidth(float w);
167
168private:
169 Q_DISABLE_COPY_MOVE(QSGGeometry)
170 friend class QSGGeometryData;
171
172 int m_drawing_mode;
173 int m_vertex_count;
174 int m_index_count;
175 int m_index_type;
176 const AttributeSet &m_attributes;
177 void *m_data;
178 int m_index_data_offset;
179
180 QSGGeometryData *m_server_data;
181
182 uint m_owns_data : 1;
183 uint m_index_usage_pattern : 2;
184 uint m_vertex_usage_pattern : 2;
185 uint m_dirty_index_data : 1;
186 uint m_dirty_vertex_data : 1;
187 uint m_reserved_bits : 25;
188
189 float m_prealloc[16];
190
191 float m_line_width;
192};
193
194inline uint *QSGGeometry::indexDataAsUInt()
195{
196 Q_ASSERT(m_index_type == UnsignedIntType);
197 return static_cast<uint *>(indexData());
198}
199
200inline quint16 *QSGGeometry::indexDataAsUShort()
201{
202 Q_ASSERT(m_index_type == UnsignedShortType);
203 return static_cast<quint16 *>(indexData());
204}
205
206inline const uint *QSGGeometry::indexDataAsUInt() const
207{
208 Q_ASSERT(m_index_type == UnsignedIntType);
209 return static_cast<const uint *>(indexData());
210}
211
212inline const quint16 *QSGGeometry::indexDataAsUShort() const
213{
214 Q_ASSERT(m_index_type == UnsignedShortType);
215 return static_cast<const quint16 *>(indexData());
216}
217
218inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
219{
220 Q_ASSERT(m_attributes.count == 1);
221 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
222 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
223 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
224 Q_ASSERT(m_attributes.attributes[0].position == 0);
225 return static_cast<Point2D *>(m_data);
226}
227
228inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
229{
230 Q_ASSERT(m_attributes.count == 2);
231 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
232 Q_ASSERT(m_attributes.attributes[0].position == 0);
233 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
234 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
235 Q_ASSERT(m_attributes.attributes[1].position == 1);
236 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
237 Q_ASSERT(m_attributes.attributes[1].type == FloatType);
238 return static_cast<TexturedPoint2D *>(m_data);
239}
240
241inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
242{
243 Q_ASSERT(m_attributes.count == 2);
244 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
245 Q_ASSERT(m_attributes.attributes[0].position == 0);
246 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
247 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
248 Q_ASSERT(m_attributes.attributes[1].position == 1);
249 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
250 Q_ASSERT(m_attributes.attributes[1].type == UnsignedByteType);
251 return static_cast<ColoredPoint2D *>(m_data);
252}
253
254inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
255{
256 Q_ASSERT(m_attributes.count == 1);
257 Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
258 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
259 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
260 Q_ASSERT(m_attributes.attributes[0].position == 0);
261 return static_cast<const Point2D *>(m_data);
262}
263
264inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
265{
266 Q_ASSERT(m_attributes.count == 2);
267 Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
268 Q_ASSERT(m_attributes.attributes[0].position == 0);
269 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
270 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
271 Q_ASSERT(m_attributes.attributes[1].position == 1);
272 Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
273 Q_ASSERT(m_attributes.attributes[1].type == FloatType);
274 return static_cast<const TexturedPoint2D *>(m_data);
275}
276
277inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
278{
279 Q_ASSERT(m_attributes.count == 2);
280 Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
281 Q_ASSERT(m_attributes.attributes[0].position == 0);
282 Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
283 Q_ASSERT(m_attributes.attributes[0].type == FloatType);
284 Q_ASSERT(m_attributes.attributes[1].position == 1);
285 Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
286 Q_ASSERT(m_attributes.attributes[1].type == UnsignedByteType);
287 return static_cast<const ColoredPoint2D *>(m_data);
288}
289
290int QSGGeometry::sizeOfIndex() const
291{
292 if (m_index_type == UnsignedShortType) return 2;
293 else if (m_index_type == UnsignedByteType) return 1;
294 else if (m_index_type == UnsignedIntType) return 4;
295 return 0;
296}
297
298QT_END_NAMESPACE
299
300#endif // QSGGEOMETRY_H
The QSGGeometry class provides low-level storage for graphics primitives in the \l{Qt Quick Scene Gra...
Definition qsggeometry.h:15