Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qsgdefaultspritenode.cpp
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
5
6#include <QtQuick/QSGMaterial>
7
9
10struct SpriteVertex {
11 float x;
12 float y;
13 float tx;
14 float ty;
15};
16
22};
23
25{
26public:
29 QSGMaterialType *type() const override { static QSGMaterialType type; return &type; }
31
32 QSGTexture *texture = nullptr;
33
34 float animT = 0.0f;
35 float animX1 = 0.0f;
36 float animY1 = 0.0f;
37 float animX2 = 0.0f;
38 float animY2 = 0.0f;
39 float animW = 1.0f;
40 float animH = 1.0f;
41};
42
44{
45 setFlag(Blending, true);
46}
47
49{
50 delete texture;
51}
52
54{
55public:
57
59 QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
61 QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
62};
63
65{
66 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/sprite.vert.qsb"));
67 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/sprite.frag.qsb"));
68}
69
71 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
72{
73#ifdef QT_NO_DEBUG
74 Q_UNUSED(oldMaterial);
75#endif
76 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
77 QQuickSpriteMaterial *mat = static_cast<QQuickSpriteMaterial *>(newMaterial);
78
79 bool changed = false;
80 QByteArray *buf = state.uniformData();
81 Q_ASSERT(buf->size() >= 96);
82
83 if (state.isMatrixDirty()) {
84 const QMatrix4x4 m = state.combinedMatrix();
85 memcpy(buf->data(), m.constData(), 64);
86 changed = true;
87 }
88
89 float animPosAndData[7] = { mat->animX1, mat->animY1, mat->animX2, mat->animY2,
90 mat->animW, mat->animH, mat->animT };
91 memcpy(buf->data() + 64, animPosAndData, 28);
92 changed = true;
93
94 if (state.isOpacityDirty()) {
95 const float opacity = state.opacity();
96 memcpy(buf->data() + 92, &opacity, 4);
97 changed = true;
98 }
99
100 return changed;
101}
102
104 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
105{
106 if (binding != 1)
107 return;
108
109#ifdef QT_NO_DEBUG
110 Q_UNUSED(oldMaterial);
111#endif
112 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
113 QQuickSpriteMaterial *mat = static_cast<QQuickSpriteMaterial *>(newMaterial);
114
115 QSGTexture *t = mat->texture;
116 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
117 *texture = t;
118}
119
121{
122 Q_UNUSED(renderMode);
123 return new SpriteMaterialRhiShader;
124}
125
129};
130
132{
133 2, // Attribute Count
134 (2+2) * sizeof(float),
136};
137
139 : m_material(new QQuickSpriteMaterial)
140 , m_geometryDirty(true)
141 , m_sheetSize(QSize(64, 64))
142{
143 // Setup geometry data
144 m_geometry = new QSGGeometry(Sprite_AttributeSet, 4, 6);
146 quint16 *indices = m_geometry->indexDataAsUShort();
147 indices[0] = 0;
148 indices[1] = 1;
149 indices[2] = 2;
150 indices[3] = 1;
151 indices[4] = 3;
152 indices[5] = 2;
153
154 setGeometry(m_geometry);
155 setMaterial(m_material);
156 setFlag(OwnsGeometry, true);
157 setFlag(OwnsMaterial, true);
158}
159
161{
162 m_material->texture = texture;
163 m_geometryDirty = true;
164 markDirty(DirtyMaterial);
165}
166
168{
169 m_material->animT = time;
170 markDirty(DirtyMaterial);
171}
172
174{
175 if (m_sourceA != source) {
176 m_sourceA = source;
177 m_material->animX1 = static_cast<float>(source.x()) / m_sheetSize.width();
178 m_material->animY1 = static_cast<float>(source.y()) / m_sheetSize.height();
179 markDirty(DirtyMaterial);
180 }
181}
182
184{
185 if (m_sourceB != source) {
186 m_sourceB = source;
187 m_material->animX2 = static_cast<float>(source.x()) / m_sheetSize.width();
188 m_material->animY2 = static_cast<float>(source.y()) / m_sheetSize.height();
189 markDirty(DirtyMaterial);
190 }
191}
192
194{
195 if (m_spriteSize != size) {
196 m_spriteSize = size;
197 m_material->animW = static_cast<float>(size.width()) / m_sheetSize.width();
198 m_material->animH = static_cast<float>(size.height()) / m_sheetSize.height();
199 markDirty(DirtyMaterial);
200 }
201
202}
203
205{
206 if (m_sheetSize != size) {
207 m_sheetSize = size;
208
209 // Update all dependent properties
210 m_material->animX1 = static_cast<float>(m_sourceA.x()) / m_sheetSize.width();
211 m_material->animY1 = static_cast<float>(m_sourceA.y()) / m_sheetSize.height();
212 m_material->animX2 = static_cast<float>(m_sourceB.x()) / m_sheetSize.width();
213 m_material->animY2 = static_cast<float>(m_sourceB.y()) / m_sheetSize.height();
214 m_material->animW = static_cast<float>(m_spriteSize.width()) / m_sheetSize.width();
215 m_material->animH = static_cast<float>(m_spriteSize.height()) / m_sheetSize.height();
216 markDirty(DirtyMaterial);
217 }
218}
219
221{
222 if (m_size != size) {
223 m_size = size;
224 m_geometryDirty = true;
225 }
226}
227
229{
230 m_material->texture->setFiltering(filtering);
231 markDirty(DirtyMaterial);
232}
233
235{
236 if (m_geometryDirty) {
237 updateGeometry();
238 m_geometryDirty = false;
239 }
240}
241
242void QSGDefaultSpriteNode::updateGeometry()
243{
244 if (!m_material->texture)
245 return;
246
247 SpriteVertices *p = (SpriteVertices *) m_geometry->vertexData();
248
249 QRectF texRect = m_material->texture->normalizedTextureSubRect();
250
251 p->v1.tx = texRect.topLeft().x();
252 p->v1.ty = texRect.topLeft().y();
253
254 p->v2.tx = texRect.topRight().x();
255 p->v2.ty = texRect.topRight().y();
256
257 p->v3.tx = texRect.bottomLeft().x();
258 p->v3.ty = texRect.bottomLeft().y();
259
260 p->v4.tx = texRect.bottomRight().x();
261 p->v4.ty = texRect.bottomRight().y();
262
263 p->v1.x = 0;
264 p->v1.y = 0;
265
266 p->v2.x = m_size.width();
267 p->v2.y = 0;
268
269 p->v3.x = 0;
270 p->v3.y = m_size.height();
271
272 p->v4.x = m_size.width();
273 p->v4.y = m_size.height();
274 markDirty(DirtyGeometry);
275}
276
\inmodule QtCore
Definition qbytearray.h:57
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
\inmodule QtCore\reentrant
Definition qpoint.h:23
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:127
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:132
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr QPointF bottomLeft() const noexcept
Returns the position of the rectangle's bottom-left corner.
Definition qrect.h:513
constexpr QPointF topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
Definition qrect.h:510
constexpr QPointF bottomRight() const noexcept
Returns the position of the rectangle's bottom-right corner.
Definition qrect.h:511
constexpr QPointF topRight() const noexcept
Returns the position of the rectangle's top-right corner.
Definition qrect.h:512
void setTexture(QSGTexture *texture) override
void setSheetSize(const QSize &size) override
void setSourceA(const QPoint &source) override
void setSize(const QSizeF &size) override
void setSpriteSize(const QSize &size) override
void setSourceB(const QPoint &source) override
void setTime(float time) override
void setFiltering(QSGTexture::Filtering filtering) override
The QSGGeometry class provides low-level storage for graphics primitives in the \l{Qt Quick Scene Gra...
Definition qsggeometry.h:15
void setDrawingMode(unsigned int mode)
Sets the mode to be used for drawing this geometry.
void * vertexData()
Returns a pointer to the raw vertex data of this geometry object.
quint16 * indexDataAsUShort()
Convenience function to access the index data as a mutable array of 16-bit unsigned integers.
Encapsulates the current rendering state during a call to QSGMaterialShader::updateUniformData() and ...
The QSGMaterialShader class represents a graphics API independent shader program.
void setShaderFileName(Stage stage, const QString &filename)
Sets the filename for the shader for the specified stage.
The QSGMaterial class encapsulates rendering state for a shader program.
Definition qsgmaterial.h:15
virtual QSGMaterialType * type() const =0
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
void setFlag(Flags flags, bool on=true)
Sets the flags flags on this material if on is true; otherwise clears the attribute.
RenderMode
\value RenderMode2D Normal 2D rendering \value RenderMode2DNoDepthBuffer Normal 2D rendering with dep...
\inmodule QtQuick
Definition qsgtexture.h:20
virtual void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
Call this function to enqueue image upload operations to resourceUpdates, in case there are any pendi...
virtual QRectF normalizedTextureSubRect() const
Returns the rectangle inside textureSize() that this texture represents in normalized coordinates.
void setFiltering(Filtering filter)
Sets the sampling mode to filter.
Filtering
Specifies how sampling of texels should filter when texture coordinates are not pixel aligned.
Definition qsgtexture.h:34
\inmodule QtCore
Definition qsize.h:207
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
else opt state
[0]
Combined button and popup list for selecting options.
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLuint texture
GLsizei GLenum const void * indices
GLsizei GLsizei GLchar * source
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QSGGeometry::Attribute Sprite_Attributes[]
static QSGGeometry::AttributeSet Sprite_AttributeSet
#define QStringLiteral(str)
#define Q_UNUSED(x)
unsigned short quint16
Definition qtypes.h:43
The QSGGeometry::AttributeSet describes how the vertices in a QSGGeometry are built up.
Definition qsggeometry.h:73
The QSGGeometry::Attribute describes a single vertex attribute in a QSGGeometry.
Definition qsggeometry.h:58
static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition=false)
Creates a new QSGGeometry::Attribute for attribute register pos with tupleSize.
The QSGMaterialType class is used as a unique type token in combination with QSGMaterial.