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 | #include "qsggeometry.h" |
5 | #include "qsggeometry_p.h" |
6 | |
7 | #ifdef Q_OS_QNX |
8 | #include <malloc.h> |
9 | #endif |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | |
14 | QSGGeometry::Attribute QSGGeometry::Attribute::create(int attributeIndex, int tupleSize, int primitiveType, bool isPrimitive) |
15 | { |
16 | Attribute a = { .position: attributeIndex, .tupleSize: tupleSize, .type: primitiveType, .isVertexCoordinate: isPrimitive, .attributeType: UnknownAttribute, .reserved: 0 }; |
17 | return a; |
18 | } |
19 | |
20 | QSGGeometry::Attribute QSGGeometry::Attribute::createWithAttributeType(int pos, int tupleSize, int primitiveType, AttributeType attributeType) |
21 | { |
22 | Attribute a; |
23 | a.position = pos; |
24 | a.tupleSize = tupleSize; |
25 | a.type = primitiveType; |
26 | a.isVertexCoordinate = attributeType == PositionAttribute; |
27 | a.attributeType = attributeType; |
28 | a.reserved = 0; |
29 | return a; |
30 | } |
31 | |
32 | /*! |
33 | Convenience function which returns attributes to be used for 2D solid |
34 | color drawing. |
35 | */ |
36 | |
37 | const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_Point2D() |
38 | { |
39 | static Attribute data[] = { |
40 | Attribute::createWithAttributeType(pos: 0, tupleSize: 2, primitiveType: FloatType, attributeType: PositionAttribute) |
41 | }; |
42 | static AttributeSet attrs = { .count: 1, .stride: sizeof(float) * 2, .attributes: data }; |
43 | return attrs; |
44 | } |
45 | |
46 | /*! |
47 | Convenience function which returns attributes to be used for textured 2D drawing. |
48 | */ |
49 | |
50 | const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_TexturedPoint2D() |
51 | { |
52 | static Attribute data[] = { |
53 | Attribute::createWithAttributeType(pos: 0, tupleSize: 2, primitiveType: FloatType, attributeType: PositionAttribute), |
54 | Attribute::createWithAttributeType(pos: 1, tupleSize: 2, primitiveType: FloatType, attributeType: TexCoordAttribute) |
55 | }; |
56 | static AttributeSet attrs = { .count: 2, .stride: sizeof(float) * 4, .attributes: data }; |
57 | return attrs; |
58 | } |
59 | |
60 | /*! |
61 | Convenience function which returns attributes to be used for per vertex colored 2D drawing. |
62 | */ |
63 | |
64 | const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_ColoredPoint2D() |
65 | { |
66 | static Attribute data[] = { |
67 | Attribute::createWithAttributeType(pos: 0, tupleSize: 2, primitiveType: FloatType, attributeType: PositionAttribute), |
68 | Attribute::createWithAttributeType(pos: 1, tupleSize: 4, primitiveType: UnsignedByteType, attributeType: ColorAttribute) |
69 | }; |
70 | static AttributeSet attrs = { .count: 2, .stride: 2 * sizeof(float) + 4 * sizeof(char), .attributes: data }; |
71 | return attrs; |
72 | } |
73 | |
74 | |
75 | /*! |
76 | \class QSGGeometry::Attribute |
77 | \brief The QSGGeometry::Attribute describes a single vertex attribute in a QSGGeometry. |
78 | \inmodule QtQuick |
79 | |
80 | The QSGGeometry::Attribute struct describes the attribute register position, |
81 | the size of the attribute tuple and the attribute type. |
82 | |
83 | It also contains a hint to the renderer if this attribute is the attribute |
84 | describing the position. The scene graph renderer may use this information |
85 | to perform optimizations. |
86 | |
87 | It contains a number of bits which are reserved for future use. |
88 | |
89 | \sa QSGGeometry |
90 | */ |
91 | |
92 | /*! |
93 | \fn QSGGeometry::Attribute QSGGeometry::Attribute::create(int pos, int tupleSize, int primitiveType, bool isPosition) |
94 | |
95 | Creates a new QSGGeometry::Attribute for attribute register \a pos with \a |
96 | tupleSize. The \a primitiveType can be any of the supported types from |
97 | QSGGeometry::Type, such as QSGGeometry::FloatType or |
98 | QSGGeometry::UnsignedByteType. |
99 | |
100 | If the attribute describes the position for the vertex, the \a isPosition |
101 | hint should be set to \c true. The scene graph renderer may use this |
102 | information to perform optimizations. |
103 | |
104 | Use the create function to construct the attribute, rather than an |
105 | initialization list, to ensure that all fields are initialized. |
106 | */ |
107 | |
108 | /*! |
109 | \fn QSGGeometry::Attribute QSGGeometry::Attribute::createWithAttributeType(int pos, int tupleSize, int primitiveType, AttributeType attributeType) |
110 | |
111 | Creates a new QSGGeometry::Attribute for attribute register \a pos with \a |
112 | tupleSize. The \a primitiveType can be any of the supported types from |
113 | QSGGeometry::Type, such as QSGGeometry::FloatType or |
114 | QSGGeometry::UnsignedByteType. |
115 | |
116 | \a attributeType describes the intended use of the attribute. |
117 | |
118 | Use the create function to construct the attribute, rather than an |
119 | initialization list, to ensure that all fields are initialized. |
120 | */ |
121 | |
122 | |
123 | /*! |
124 | \class QSGGeometry::AttributeSet |
125 | \brief The QSGGeometry::AttributeSet describes how the vertices in a QSGGeometry |
126 | are built up. |
127 | \inmodule QtQuick |
128 | |
129 | \sa QSGGeometry |
130 | */ |
131 | |
132 | |
133 | /*! |
134 | \class QSGGeometry::Point2D |
135 | \brief The QSGGeometry::Point2D struct is a convenience struct for accessing |
136 | 2D Points. |
137 | |
138 | \inmodule QtQuick |
139 | */ |
140 | |
141 | |
142 | /*! |
143 | \fn void QSGGeometry::Point2D::set(float x, float y) |
144 | |
145 | Sets the x and y values of this point to \a x and \a y. |
146 | */ |
147 | |
148 | |
149 | /*! |
150 | \class QSGGeometry::ColoredPoint2D |
151 | \brief The QSGGeometry::ColoredPoint2D struct is a convenience struct for accessing |
152 | 2D Points with a color. |
153 | |
154 | \inmodule QtQuick |
155 | */ |
156 | |
157 | |
158 | /*! |
159 | \fn void QSGGeometry::ColoredPoint2D::set(float x, float y, uchar red, uchar green, uchar blue, uchar alpha) |
160 | |
161 | Sets the position of the vertex to \a x and \a y and the color to \a red, \a |
162 | green, \a blue, and \a alpha. |
163 | */ |
164 | |
165 | |
166 | /*! |
167 | \class QSGGeometry::TexturedPoint2D |
168 | \brief The QSGGeometry::TexturedPoint2D struct is a convenience struct for accessing |
169 | 2D Points with texture coordinates. |
170 | |
171 | \inmodule QtQuick |
172 | */ |
173 | |
174 | |
175 | /*! |
176 | \fn void QSGGeometry::TexturedPoint2D::set(float x, float y, float tx, float ty) |
177 | |
178 | Sets the position of the vertex to \a x and \a y and the texture coordinate |
179 | to \a tx and \a ty. |
180 | */ |
181 | |
182 | |
183 | |
184 | /*! |
185 | \class QSGGeometry |
186 | |
187 | \brief The QSGGeometry class provides low-level |
188 | storage for graphics primitives in the \l{Qt Quick Scene Graph}. |
189 | |
190 | \inmodule QtQuick |
191 | |
192 | The QSGGeometry class stores the geometry of the primitives rendered |
193 | with the scene graph. It contains vertex data and optionally index |
194 | data. The mode used to draw the geometry, also called primitive |
195 | topology, is specified with setDrawingMode(). |
196 | |
197 | Vertices can be as simple as points defined by x and y values or |
198 | can be more complex where each vertex contains a normal, texture |
199 | coordinates and a 3D position. The QSGGeometry::AttributeSet is |
200 | used to describe how the vertex data is built up. The attribute |
201 | set can only be specified on construction. The QSGGeometry class |
202 | provides a few convenience attributes and attribute sets by |
203 | default. The defaultAttributes_Point2D() function returns an |
204 | attribute set to be used in normal solid color rectangles, while |
205 | the defaultAttributes_TexturedPoint2D function returns attributes |
206 | to be used for textured 2D geometry. The vertex data is internally |
207 | stored as a \c {void *} and is accessible with the vertexData() |
208 | function. Convenience accessors for the common attribute sets are |
209 | available with vertexDataAsPoint2D() and |
210 | vertexDataAsTexturedPoint2D(). Vertex data is allocated by passing |
211 | a vertex count to the constructor or by calling allocate() later. |
212 | |
213 | The QSGGeometry can optionally contain indices of either unsigned |
214 | 32-bit, unsigned 16-bit, or unsigned 8-bit integers. The index type |
215 | must be specified during construction and cannot be changed. |
216 | |
217 | Below is a snippet illustrating how a geometry composed of |
218 | position and color vertices can be built. |
219 | |
220 | \code |
221 | struct MyPoint2D { |
222 | float x; |
223 | float y; |
224 | float r; |
225 | float g; |
226 | float b; |
227 | float a; |
228 | |
229 | void set(float x_, float y_, float r_, float g_, float b_, float a_) { |
230 | x = x_; |
231 | y = y_; |
232 | r = r_; |
233 | g = g_; |
234 | b = b_; |
235 | a = a_; |
236 | } |
237 | }; |
238 | |
239 | QSGGeometry::Attribute MyPoint2D_Attributes[] = { |
240 | QSGGeometry::Attribute::create(0, 2, FloatType, true), |
241 | QSGGeometry::Attribute::create(1, 4, FloatType, false) |
242 | }; |
243 | |
244 | QSGGeometry::AttributeSet MyPoint2D_AttributeSet = { |
245 | 2, |
246 | sizeof(MyPoint2D), |
247 | MyPoint2D_Attributes |
248 | }; |
249 | |
250 | ... |
251 | |
252 | geometry = new QSGGeometry(MyPoint2D_AttributeSet, 2); |
253 | geometry->setDrawingMode(DrawLines); |
254 | |
255 | MyPoint2D *vertices = static_cast<MyPoint2D *>(geometry->vertexData()); |
256 | vertices[0].set(0, 0, 1, 0, 0, 1); |
257 | vertices[1].set(width(), height(), 0, 0, 1, 1); |
258 | \endcode |
259 | |
260 | The QSGGeometry is a software buffer and client-side in terms of |
261 | accelerated rendering, as the buffers used in 2D graphics typically consist of |
262 | many small buffers that change every frame and do not benefit from |
263 | being uploaded to graphics memory. However, the QSGGeometry |
264 | supports hinting to the renderer that a buffer should be |
265 | uploaded using the setVertexDataPattern() and |
266 | setIndexDataPattern() functions. Whether this hint is respected or |
267 | not is implementation specific. |
268 | |
269 | \sa QSGGeometryNode, {Scene Graph - Custom Geometry} |
270 | |
271 | \note All classes with QSG prefix should be used solely on the scene graph's |
272 | rendering thread. See \l {Scene Graph and Rendering} for more information. |
273 | |
274 | */ |
275 | |
276 | /*! |
277 | \fn int QSGGeometry::attributeCount() const |
278 | |
279 | Returns the number of attributes in the attrbute set used by this geometry. |
280 | */ |
281 | |
282 | /*! |
283 | \fn QSGGeometry::Attribute *QSGGeometry::attributes() const |
284 | |
285 | Returns an array with the attributes of this geometry. The size of the array |
286 | is given with attributeCount(). |
287 | */ |
288 | |
289 | /*! |
290 | \fn uint *QSGGeometry::indexDataAsUInt() |
291 | |
292 | Convenience function to access the index data as a mutable array of |
293 | 32-bit unsigned integers. |
294 | */ |
295 | |
296 | /*! |
297 | \fn const uint *QSGGeometry::indexDataAsUInt() const |
298 | |
299 | Convenience function to access the index data as an immutable array of |
300 | 32-bit unsigned integers. |
301 | */ |
302 | |
303 | /*! |
304 | \fn quint16 *QSGGeometry::indexDataAsUShort() |
305 | |
306 | Convenience function to access the index data as a mutable array of |
307 | 16-bit unsigned integers. |
308 | */ |
309 | |
310 | /*! |
311 | \fn const quint16 *QSGGeometry::indexDataAsUShort() const |
312 | |
313 | Convenience function to access the index data as an immutable array of |
314 | 16-bit unsigned integers. |
315 | */ |
316 | |
317 | /*! |
318 | \fn const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const |
319 | |
320 | Convenience function to access the vertex data as an immutable |
321 | array of QSGGeometry::ColoredPoint2D. |
322 | */ |
323 | |
324 | /*! |
325 | \fn QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() |
326 | |
327 | Convenience function to access the vertex data as a mutable |
328 | array of QSGGeometry::ColoredPoint2D. |
329 | */ |
330 | |
331 | /*! |
332 | \fn const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const |
333 | |
334 | Convenience function to access the vertex data as an immutable |
335 | array of QSGGeometry::TexturedPoint2D. |
336 | */ |
337 | |
338 | /*! |
339 | \fn QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() |
340 | |
341 | Convenience function to access the vertex data as a mutable |
342 | array of QSGGeometry::TexturedPoint2D. |
343 | */ |
344 | |
345 | /*! |
346 | \fn const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const |
347 | |
348 | Convenience function to access the vertex data as an immutable |
349 | array of QSGGeometry::Point2D. |
350 | */ |
351 | |
352 | /*! |
353 | \fn QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() |
354 | |
355 | Convenience function to access the vertex data as a mutable |
356 | array of QSGGeometry::Point2D. |
357 | */ |
358 | |
359 | |
360 | /*! |
361 | Constructs a geometry object based on \a attributes. |
362 | |
363 | The object allocate space for \a vertexCount vertices based on the |
364 | accumulated size in \a attributes and for \a indexCount. |
365 | |
366 | The \a indexType can be UnsignedShortType or \c |
367 | UnsignedIntType. Support for the latter depends on the graphics API |
368 | implementation used at run time, and may not always be available. |
369 | |
370 | Geometry objects are constructed by default with DrawTriangleStrip as |
371 | the drawing mode. |
372 | |
373 | \note \a attributes and the \l Attribute objects referenced by it must |
374 | stay valid for the entire lifetime of the QSGGeometry. |
375 | QSGGeometry stores a reference to \a attributes and does not delete |
376 | the \l Attribute objects. |
377 | */ |
378 | |
379 | QSGGeometry::QSGGeometry(const QSGGeometry::AttributeSet &attributes, |
380 | int vertexCount, |
381 | int indexCount, |
382 | int indexType) |
383 | : m_drawing_mode(DrawTriangleStrip) |
384 | , m_vertex_count(0) |
385 | , m_index_count(0) |
386 | , m_index_type(indexType) |
387 | , m_attributes(attributes) |
388 | , m_data(nullptr) |
389 | , m_index_data_offset(-1) |
390 | , m_server_data(nullptr) |
391 | , m_owns_data(false) |
392 | , m_index_usage_pattern(AlwaysUploadPattern) |
393 | , m_vertex_usage_pattern(AlwaysUploadPattern) |
394 | , m_line_width(1.0) |
395 | { |
396 | Q_UNUSED(m_reserved_bits); |
397 | Q_ASSERT(m_attributes.count > 0); |
398 | Q_ASSERT(m_attributes.stride > 0); |
399 | if (indexType != UnsignedByteType |
400 | && indexType != UnsignedShortType |
401 | && indexType != UnsignedIntType) { |
402 | qFatal(msg: "QSGGeometry: Unsupported index type, %x.\n" , indexType); |
403 | } |
404 | |
405 | // Because allocate reads m_vertex_count, m_index_count and m_owns_data, these |
406 | // need to be set before calling allocate... |
407 | allocate(vertexCount, indexCount); |
408 | } |
409 | |
410 | /*! |
411 | \fn int QSGGeometry::sizeOfVertex() const |
412 | |
413 | Returns the size in bytes of one vertex. |
414 | |
415 | This value comes from the attributes. |
416 | */ |
417 | |
418 | /*! |
419 | \fn int QSGGeometry::sizeOfIndex() const |
420 | |
421 | Returns the byte size of the index type. |
422 | |
423 | This value is either \c 2 when the index type is UnsignedShortType, or |
424 | \c 4 when the index type is UnsignedIntType. |
425 | */ |
426 | |
427 | /*! |
428 | Destroys the geometry object and the vertex and index data it has allocated. |
429 | */ |
430 | |
431 | QSGGeometry::~QSGGeometry() |
432 | { |
433 | if (m_owns_data) |
434 | free(ptr: m_data); |
435 | |
436 | if (m_server_data) |
437 | delete m_server_data; |
438 | } |
439 | |
440 | /*! |
441 | \fn int QSGGeometry::vertexCount() const |
442 | |
443 | Returns the number of vertices in this geometry object. |
444 | */ |
445 | |
446 | /*! |
447 | \fn int QSGGeometry::indexCount() const |
448 | |
449 | Returns the number of indices in this geometry object. |
450 | */ |
451 | |
452 | |
453 | |
454 | /*! |
455 | \fn void *QSGGeometry::vertexData() |
456 | |
457 | Returns a pointer to the raw vertex data of this geometry object. |
458 | |
459 | \sa vertexDataAsPoint2D(), vertexDataAsTexturedPoint2D() |
460 | */ |
461 | |
462 | /*! |
463 | \fn const void *QSGGeometry::vertexData() const |
464 | |
465 | Returns a pointer to the raw vertex data of this geometry object. |
466 | |
467 | \sa vertexDataAsPoint2D(), vertexDataAsTexturedPoint2D() |
468 | */ |
469 | |
470 | /*! |
471 | Returns a pointer to the raw index data of this geometry object. |
472 | |
473 | \sa indexDataAsUShort(), indexDataAsUInt() |
474 | */ |
475 | void *QSGGeometry::indexData() |
476 | { |
477 | return m_index_data_offset < 0 |
478 | ? nullptr |
479 | : ((char *) m_data + m_index_data_offset); |
480 | } |
481 | |
482 | /*! |
483 | Returns a pointer to the raw index data of this geometry object. |
484 | |
485 | \sa indexDataAsUShort(), indexDataAsUInt() |
486 | */ |
487 | const void *QSGGeometry::indexData() const |
488 | { |
489 | return m_index_data_offset < 0 |
490 | ? nullptr |
491 | : ((char *) m_data + m_index_data_offset); |
492 | } |
493 | |
494 | /*! |
495 | \enum QSGGeometry::DrawingMode |
496 | |
497 | Specifies the drawing mode, also called primitive topology. |
498 | |
499 | \note Starting with Qt 6 the scene graph only exposes topologies that are |
500 | supported across all the supported 3D graphics APIs. As a result, the |
501 | values \c DrawLineLoop and \c DrawTriangleFan are no longer supported at |
502 | run time in Qt 6, even though the enum values themselves are still present. |
503 | |
504 | \value DrawPoints |
505 | \value DrawLines |
506 | \omitvalue DrawLineLoop |
507 | \value DrawLineStrip |
508 | \value DrawTriangles |
509 | \value DrawTriangleStrip |
510 | \omitvalue DrawTriangleFan |
511 | */ |
512 | |
513 | /*! |
514 | \enum QSGGeometry::Type |
515 | |
516 | Specifies the component type in the vertex data. |
517 | |
518 | \value ByteType |
519 | \value UnsignedByteType |
520 | \value ShortType |
521 | \value UnsignedShortType |
522 | \value IntType |
523 | \value UnsignedIntType |
524 | \value FloatType |
525 | \value Bytes2Type Added in Qt 5.14. |
526 | \value Bytes3Type Added in Qt 5.14. |
527 | \value Bytes4Type Added in Qt 5.14. |
528 | \value DoubleType Added in Qt 5.14. |
529 | */ |
530 | |
531 | /*! |
532 | Sets the \a mode to be used for drawing this geometry. |
533 | |
534 | The default value is QSGGeometry::DrawTriangleStrip. |
535 | |
536 | \sa DrawingMode |
537 | */ |
538 | void QSGGeometry::setDrawingMode(unsigned int mode) |
539 | { |
540 | m_drawing_mode = mode; |
541 | } |
542 | |
543 | /*! |
544 | Gets the current line or point width or to be used for this geometry. This |
545 | property only applies to line width when the drawingMode is DrawLines or |
546 | DrawLineStrip. When supported, it also applies to point size when the |
547 | drawingMode is DrawPoints. |
548 | |
549 | The default value is \c 1.0 |
550 | |
551 | \note Support for point and line drawing may be limited at run time, |
552 | depending on the platform and graphics API. For example, some APIs do |
553 | not support point sprites and so setting a size other than 1 is not |
554 | possible. |
555 | |
556 | \note The width of \c 1.0 is always supported. |
557 | |
558 | \sa setLineWidth(), drawingMode() |
559 | */ |
560 | float QSGGeometry::lineWidth() const |
561 | { |
562 | return m_line_width; |
563 | } |
564 | |
565 | /*! |
566 | Sets the line or point width to be used for this geometry to \a width. This |
567 | property only applies to line width when the drawingMode is DrawLines or |
568 | DrawLineStrip. When supported, it also applies to point size when the |
569 | drawingMode is DrawPoints. |
570 | |
571 | \note Support for point and line drawing may be limited at run time, |
572 | depending on the platform and graphics API. For example, some APIs do |
573 | not support point sprites and so setting a size other than 1 is not |
574 | possible. |
575 | |
576 | \note The width of \c 1.0 is always supported. |
577 | |
578 | \sa lineWidth(), drawingMode() |
579 | */ |
580 | void QSGGeometry::setLineWidth(float width) |
581 | { |
582 | m_line_width = width; |
583 | } |
584 | |
585 | /*! |
586 | \fn int QSGGeometry::drawingMode() const |
587 | |
588 | Returns the drawing mode of this geometry. |
589 | |
590 | The default value is DrawTriangleStrip. |
591 | */ |
592 | |
593 | /*! |
594 | \fn int QSGGeometry::indexType() const |
595 | |
596 | Returns the primitive type used for indices in this |
597 | geometry object. |
598 | */ |
599 | |
600 | |
601 | /*! |
602 | Resizes the vertex and index data of this geometry object to fit \a vertexCount |
603 | vertices and \a indexCount indices. |
604 | |
605 | Vertex and index data will be invalidated after this call and the caller must |
606 | mark the associated geometry node as dirty, by calling |
607 | node->markDirty(QSGNode::DirtyGeometry) to ensure that the renderer has |
608 | a chance to update internal buffers. |
609 | */ |
610 | void QSGGeometry::allocate(int vertexCount, int indexCount) |
611 | { |
612 | if (vertexCount == m_vertex_count && indexCount == m_index_count) |
613 | return; |
614 | |
615 | m_vertex_count = vertexCount; |
616 | m_index_count = indexCount; |
617 | |
618 | bool canUsePrealloc = m_index_count <= 0; |
619 | int vertexByteSize = m_attributes.stride * m_vertex_count; |
620 | |
621 | if (m_owns_data) |
622 | free(ptr: m_data); |
623 | |
624 | if (canUsePrealloc && vertexByteSize <= (int) sizeof(m_prealloc)) { |
625 | m_data = (void *) &m_prealloc[0]; |
626 | m_index_data_offset = -1; |
627 | m_owns_data = false; |
628 | } else { |
629 | Q_ASSERT(m_index_type == UnsignedIntType || m_index_type == UnsignedShortType); |
630 | int indexByteSize = indexCount * (m_index_type == UnsignedShortType ? sizeof(quint16) : sizeof(quint32)); |
631 | m_data = (void *) malloc(size: vertexByteSize + indexByteSize); |
632 | Q_CHECK_PTR(m_data); |
633 | m_index_data_offset = vertexByteSize; |
634 | m_owns_data = true; |
635 | } |
636 | |
637 | // If we have associated vbo data we could potentially crash later if |
638 | // the old buffers are used with the new vertex and index count, so we force |
639 | // an update in the renderer in that case. This is really the users responsibility |
640 | // but it is cheap for us to enforce this, so why not... |
641 | if (m_server_data) { |
642 | markIndexDataDirty(); |
643 | markVertexDataDirty(); |
644 | } |
645 | |
646 | } |
647 | |
648 | /*! |
649 | Updates the geometry \a g with the coordinates in \a rect. |
650 | |
651 | The function assumes the geometry object contains a single triangle strip |
652 | of QSGGeometry::Point2D vertices |
653 | */ |
654 | void QSGGeometry::updateRectGeometry(QSGGeometry *g, const QRectF &rect) |
655 | { |
656 | Point2D *v = g->vertexDataAsPoint2D(); |
657 | v[0].x = rect.left(); |
658 | v[0].y = rect.top(); |
659 | |
660 | v[1].x = rect.left(); |
661 | v[1].y = rect.bottom(); |
662 | |
663 | v[2].x = rect.right(); |
664 | v[2].y = rect.top(); |
665 | |
666 | v[3].x = rect.right(); |
667 | v[3].y = rect.bottom(); |
668 | } |
669 | |
670 | /*! |
671 | Updates the geometry \a g with the coordinates in \a rect and texture |
672 | coordinates from \a textureRect. |
673 | |
674 | \a textureRect should be in normalized coordinates. |
675 | |
676 | \a g is assumed to be a triangle strip of four vertices of type |
677 | QSGGeometry::TexturedPoint2D. |
678 | */ |
679 | void QSGGeometry::updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect) |
680 | { |
681 | TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D(); |
682 | v[0].x = rect.left(); |
683 | v[0].y = rect.top(); |
684 | v[0].tx = textureRect.left(); |
685 | v[0].ty = textureRect.top(); |
686 | |
687 | v[1].x = rect.left(); |
688 | v[1].y = rect.bottom(); |
689 | v[1].tx = textureRect.left(); |
690 | v[1].ty = textureRect.bottom(); |
691 | |
692 | v[2].x = rect.right(); |
693 | v[2].y = rect.top(); |
694 | v[2].tx = textureRect.right(); |
695 | v[2].ty = textureRect.top(); |
696 | |
697 | v[3].x = rect.right(); |
698 | v[3].y = rect.bottom(); |
699 | v[3].tx = textureRect.right(); |
700 | v[3].ty = textureRect.bottom(); |
701 | } |
702 | |
703 | /*! |
704 | Updates the geometry \a g with the coordinates in \a rect. |
705 | |
706 | The function assumes the geometry object contains a single triangle strip |
707 | of QSGGeometry::ColoredPoint2D vertices |
708 | */ |
709 | void QSGGeometry::updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect) |
710 | { |
711 | ColoredPoint2D *v = g->vertexDataAsColoredPoint2D(); |
712 | v[0].x = rect.left(); |
713 | v[0].y = rect.top(); |
714 | |
715 | v[1].x = rect.left(); |
716 | v[1].y = rect.bottom(); |
717 | |
718 | v[2].x = rect.right(); |
719 | v[2].y = rect.top(); |
720 | |
721 | v[3].x = rect.right(); |
722 | v[3].y = rect.bottom(); |
723 | } |
724 | |
725 | /*! |
726 | \enum QSGGeometry::AttributeType |
727 | |
728 | This enum identifies several attribute types. |
729 | |
730 | \value UnknownAttribute Don't care |
731 | \value PositionAttribute Position |
732 | \value ColorAttribute Color |
733 | \value TexCoordAttribute Texture coordinate |
734 | \value TexCoord1Attribute Texture coordinate 1 |
735 | \value TexCoord2Attribute Texture coordinate 2 |
736 | |
737 | */ |
738 | |
739 | /*! |
740 | \enum QSGGeometry::DataPattern |
741 | |
742 | The DataPattern enum is used to specify the use pattern for the |
743 | vertex and index data in a geometry object. |
744 | |
745 | \value AlwaysUploadPattern The data is always uploaded. This means |
746 | that the user does not need to explicitly mark index and vertex |
747 | data as dirty after changing it. This is the default. |
748 | |
749 | \value DynamicPattern The data is modified repeatedly and drawn |
750 | many times. This is a hint that may provide better |
751 | performance. When set the user must make sure to mark the data as |
752 | dirty after changing it. |
753 | |
754 | \value StaticPattern The data is modified once and drawn many |
755 | times. This is a hint that may provide better performance. When |
756 | set the user must make sure to mark the data as dirty after |
757 | changing it. |
758 | |
759 | \value StreamPattern The data is modified for almost every time it |
760 | is drawn. This is a hint that may provide better performance. When |
761 | set, the user must make sure to mark the data as dirty after |
762 | changing it. |
763 | */ |
764 | |
765 | |
766 | /*! |
767 | \fn QSGGeometry::DataPattern QSGGeometry::indexDataPattern() const |
768 | |
769 | Returns the usage pattern for indices in this geometry. The default |
770 | pattern is AlwaysUploadPattern. |
771 | */ |
772 | |
773 | /*! |
774 | Sets the usage pattern for indices to \a p. |
775 | |
776 | The default is AlwaysUploadPattern. When set to anything other than |
777 | the default, the user must call markIndexDataDirty() after changing |
778 | the index data, in addition to calling QSGNode::markDirty() with |
779 | QSGNode::DirtyGeometry. |
780 | */ |
781 | |
782 | void QSGGeometry::setIndexDataPattern(DataPattern p) |
783 | { |
784 | m_index_usage_pattern = p; |
785 | } |
786 | |
787 | |
788 | |
789 | |
790 | /*! |
791 | \fn QSGGeometry::DataPattern QSGGeometry::vertexDataPattern() const |
792 | |
793 | Returns the usage pattern for vertices in this geometry. The default |
794 | pattern is AlwaysUploadPattern. |
795 | */ |
796 | |
797 | /*! |
798 | Sets the usage pattern for vertices to \a p. |
799 | |
800 | The default is AlwaysUploadPattern. When set to anything other than |
801 | the default, the user must call markVertexDataDirty() after changing |
802 | the vertex data, in addition to calling QSGNode::markDirty() with |
803 | QSGNode::DirtyGeometry. |
804 | */ |
805 | |
806 | void QSGGeometry::setVertexDataPattern(DataPattern p) |
807 | { |
808 | m_vertex_usage_pattern = p; |
809 | } |
810 | |
811 | |
812 | |
813 | |
814 | /*! |
815 | Mark that the vertices in this geometry has changed and must be uploaded |
816 | again. |
817 | |
818 | This function only has an effect when the usage pattern for vertices is |
819 | StaticData and the renderer that renders this geometry uploads the geometry |
820 | into Vertex Buffer Objects (VBOs). |
821 | */ |
822 | void QSGGeometry::markIndexDataDirty() |
823 | { |
824 | m_dirty_index_data = true; |
825 | } |
826 | |
827 | |
828 | |
829 | /*! |
830 | Mark that the vertices in this geometry has changed and must be uploaded |
831 | again. |
832 | |
833 | This function only has an effect when the usage pattern for vertices is |
834 | StaticData and the renderer that renders this geometry uploads the geometry |
835 | into Vertex Buffer Objects (VBOs). |
836 | */ |
837 | void QSGGeometry::markVertexDataDirty() |
838 | { |
839 | m_dirty_vertex_data = true; |
840 | } |
841 | |
842 | |
843 | QT_END_NAMESPACE |
844 | |