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 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | class QSGGeometryData; |
13 | |
14 | class Q_QUICK_EXPORT QSGGeometry |
15 | { |
16 | public: |
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 | |
122 | void *vertexData() { return m_data; } |
123 | inline Point2D *vertexDataAsPoint2D(); |
124 | inline TexturedPoint2D *vertexDataAsTexturedPoint2D(); |
125 | inline ColoredPoint2D *vertexDataAsColoredPoint2D(); |
126 | |
127 | inline const void *vertexData() const { return m_data; } |
128 | inline const Point2D *vertexDataAsPoint2D() const; |
129 | inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const; |
130 | inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const; |
131 | |
132 | inline int indexType() const { return m_index_type; } |
133 | |
134 | int indexCount() const { return m_index_count; } |
135 | |
136 | void *indexData(); |
137 | inline uint *indexDataAsUInt(); |
138 | inline quint16 *indexDataAsUShort(); |
139 | |
140 | inline int sizeOfIndex() const; |
141 | |
142 | const void *indexData() const; |
143 | inline const uint *indexDataAsUInt() const; |
144 | inline const quint16 *indexDataAsUShort() const; |
145 | |
146 | inline int attributeCount() const { return m_attributes.count; } |
147 | inline const Attribute *attributes() const { return m_attributes.attributes; } |
148 | inline int sizeOfVertex() const { return m_attributes.stride; } |
149 | |
150 | static void updateRectGeometry(QSGGeometry *g, const QRectF &rect); |
151 | static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect); |
152 | static void updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect); |
153 | |
154 | void setIndexDataPattern(DataPattern p); |
155 | DataPattern indexDataPattern() const { return DataPattern(m_index_usage_pattern); } |
156 | |
157 | void setVertexDataPattern(DataPattern p); |
158 | DataPattern vertexDataPattern() const { return DataPattern(m_vertex_usage_pattern); } |
159 | |
160 | void markIndexDataDirty(); |
161 | void markVertexDataDirty(); |
162 | |
163 | float lineWidth() const; |
164 | void setLineWidth(float w); |
165 | |
166 | private: |
167 | Q_DISABLE_COPY_MOVE(QSGGeometry) |
168 | friend class QSGGeometryData; |
169 | |
170 | int m_drawing_mode; |
171 | int m_vertex_count; |
172 | int m_index_count; |
173 | int m_index_type; |
174 | const AttributeSet &m_attributes; |
175 | void *m_data; |
176 | int m_index_data_offset; |
177 | |
178 | QSGGeometryData *m_server_data; |
179 | |
180 | uint m_owns_data : 1; |
181 | uint m_index_usage_pattern : 2; |
182 | uint m_vertex_usage_pattern : 2; |
183 | uint m_dirty_index_data : 1; |
184 | uint m_dirty_vertex_data : 1; |
185 | uint m_reserved_bits : 25; |
186 | |
187 | float m_prealloc[16]; |
188 | |
189 | float m_line_width; |
190 | }; |
191 | |
192 | inline uint *QSGGeometry::indexDataAsUInt() |
193 | { |
194 | Q_ASSERT(m_index_type == UnsignedIntType); |
195 | return static_cast<uint *>(indexData()); |
196 | } |
197 | |
198 | inline quint16 *QSGGeometry::indexDataAsUShort() |
199 | { |
200 | Q_ASSERT(m_index_type == UnsignedShortType); |
201 | return static_cast<quint16 *>(indexData()); |
202 | } |
203 | |
204 | inline const uint *QSGGeometry::indexDataAsUInt() const |
205 | { |
206 | Q_ASSERT(m_index_type == UnsignedIntType); |
207 | return static_cast<const uint *>(indexData()); |
208 | } |
209 | |
210 | inline const quint16 *QSGGeometry::indexDataAsUShort() const |
211 | { |
212 | Q_ASSERT(m_index_type == UnsignedShortType); |
213 | return static_cast<const quint16 *>(indexData()); |
214 | } |
215 | |
216 | inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() |
217 | { |
218 | Q_ASSERT(m_attributes.count == 1); |
219 | Q_ASSERT(m_attributes.stride == 2 * sizeof(float)); |
220 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
221 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
222 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
223 | return static_cast<Point2D *>(m_data); |
224 | } |
225 | |
226 | inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() |
227 | { |
228 | Q_ASSERT(m_attributes.count == 2); |
229 | Q_ASSERT(m_attributes.stride == 4 * sizeof(float)); |
230 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
231 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
232 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
233 | Q_ASSERT(m_attributes.attributes[1].position == 1); |
234 | Q_ASSERT(m_attributes.attributes[1].tupleSize == 2); |
235 | Q_ASSERT(m_attributes.attributes[1].type == FloatType); |
236 | return static_cast<TexturedPoint2D *>(m_data); |
237 | } |
238 | |
239 | inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() |
240 | { |
241 | Q_ASSERT(m_attributes.count == 2); |
242 | Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char)); |
243 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
244 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
245 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
246 | Q_ASSERT(m_attributes.attributes[1].position == 1); |
247 | Q_ASSERT(m_attributes.attributes[1].tupleSize == 4); |
248 | Q_ASSERT(m_attributes.attributes[1].type == UnsignedByteType); |
249 | return static_cast<ColoredPoint2D *>(m_data); |
250 | } |
251 | |
252 | inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const |
253 | { |
254 | Q_ASSERT(m_attributes.count == 1); |
255 | Q_ASSERT(m_attributes.stride == 2 * sizeof(float)); |
256 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
257 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
258 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
259 | return static_cast<const Point2D *>(m_data); |
260 | } |
261 | |
262 | inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const |
263 | { |
264 | Q_ASSERT(m_attributes.count == 2); |
265 | Q_ASSERT(m_attributes.stride == 4 * sizeof(float)); |
266 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
267 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
268 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
269 | Q_ASSERT(m_attributes.attributes[1].position == 1); |
270 | Q_ASSERT(m_attributes.attributes[1].tupleSize == 2); |
271 | Q_ASSERT(m_attributes.attributes[1].type == FloatType); |
272 | return static_cast<const TexturedPoint2D *>(m_data); |
273 | } |
274 | |
275 | inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const |
276 | { |
277 | Q_ASSERT(m_attributes.count == 2); |
278 | Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char)); |
279 | Q_ASSERT(m_attributes.attributes[0].position == 0); |
280 | Q_ASSERT(m_attributes.attributes[0].tupleSize == 2); |
281 | Q_ASSERT(m_attributes.attributes[0].type == FloatType); |
282 | Q_ASSERT(m_attributes.attributes[1].position == 1); |
283 | Q_ASSERT(m_attributes.attributes[1].tupleSize == 4); |
284 | Q_ASSERT(m_attributes.attributes[1].type == UnsignedByteType); |
285 | return static_cast<const ColoredPoint2D *>(m_data); |
286 | } |
287 | |
288 | int QSGGeometry::sizeOfIndex() const |
289 | { |
290 | if (m_index_type == UnsignedShortType) return 2; |
291 | else if (m_index_type == UnsignedByteType) return 1; |
292 | else if (m_index_type == UnsignedIntType) return 4; |
293 | return 0; |
294 | } |
295 | |
296 | QT_END_NAMESPACE |
297 | |
298 | #endif // QSGGEOMETRY_H |
299 | |