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