Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
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
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
47
52
54{
55public:
56 SpriteMaterialRhiShader(int viewCount);
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"), viewCount);
67 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/sprite.frag.qsb"), viewCount);
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 const int shaderMatrixCount = newMaterial->viewCount();
84 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
85 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
86 if (state.isMatrixDirty()) {
87 const QMatrix4x4 m = state.combinedMatrix(viewIndex);
88 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
89 changed = true;
90 }
91 }
92
93 float animPosAndData[7] = { mat->animX1, mat->animY1, mat->animX2, mat->animY2,
94 mat->animW, mat->animH, mat->animT };
95 memcpy(buf->data() + 64 * shaderMatrixCount, animPosAndData, 28);
96 changed = true;
97
98 if (state.isOpacityDirty()) {
99 const float opacity = state.opacity();
100 memcpy(buf->data() + 64 * shaderMatrixCount + 16 + 12, &opacity, 4);
101 changed = true;
102 }
103
104 return changed;
105}
106
108 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
109{
110 if (binding != 1)
111 return;
112
113#ifdef QT_NO_DEBUG
114 Q_UNUSED(oldMaterial);
115#endif
116 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
117 QQuickSpriteMaterial *mat = static_cast<QQuickSpriteMaterial *>(newMaterial);
118
119 QSGTexture *t = mat->texture;
120 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
121 *texture = t;
122}
123
129
134
136{
137 2, // Attribute Count
138 (2+2) * sizeof(float),
140};
141
143 : m_material(new QQuickSpriteMaterial)
144 , m_geometryDirty(true)
145 , m_sheetSize(QSize(64, 64))
146{
147 // Setup geometry data
148 m_geometry = new QSGGeometry(Sprite_AttributeSet, 4, 6);
150 quint16 *indices = m_geometry->indexDataAsUShort();
151 indices[0] = 0;
152 indices[1] = 1;
153 indices[2] = 2;
154 indices[3] = 1;
155 indices[4] = 3;
156 indices[5] = 2;
157
158 setGeometry(m_geometry);
159 setMaterial(m_material);
160 setFlag(OwnsGeometry, true);
161 setFlag(OwnsMaterial, true);
162}
163
165{
166 m_material->texture = texture;
167 m_geometryDirty = true;
168 markDirty(DirtyMaterial);
169}
170
172{
173 m_material->animT = time;
174 markDirty(DirtyMaterial);
175}
176
178{
179 if (m_sourceA != source) {
180 m_sourceA = source;
181 m_material->animX1 = static_cast<float>(source.x()) / m_sheetSize.width();
182 m_material->animY1 = static_cast<float>(source.y()) / m_sheetSize.height();
183 markDirty(DirtyMaterial);
184 }
185}
186
188{
189 if (m_sourceB != source) {
190 m_sourceB = source;
191 m_material->animX2 = static_cast<float>(source.x()) / m_sheetSize.width();
192 m_material->animY2 = static_cast<float>(source.y()) / m_sheetSize.height();
193 markDirty(DirtyMaterial);
194 }
195}
196
198{
199 if (m_spriteSize != size) {
200 m_spriteSize = size;
201 m_material->animW = static_cast<float>(size.width()) / m_sheetSize.width();
202 m_material->animH = static_cast<float>(size.height()) / m_sheetSize.height();
203 markDirty(DirtyMaterial);
204 }
205
206}
207
209{
210 if (m_sheetSize != size) {
211 m_sheetSize = size;
212
213 // Update all dependent properties
214 m_material->animX1 = static_cast<float>(m_sourceA.x()) / m_sheetSize.width();
215 m_material->animY1 = static_cast<float>(m_sourceA.y()) / m_sheetSize.height();
216 m_material->animX2 = static_cast<float>(m_sourceB.x()) / m_sheetSize.width();
217 m_material->animY2 = static_cast<float>(m_sourceB.y()) / m_sheetSize.height();
218 m_material->animW = static_cast<float>(m_spriteSize.width()) / m_sheetSize.width();
219 m_material->animH = static_cast<float>(m_spriteSize.height()) / m_sheetSize.height();
220 markDirty(DirtyMaterial);
221 }
222}
223
225{
226 if (m_size != size) {
227 m_size = size;
228 m_geometryDirty = true;
229 }
230}
231
233{
234 m_material->texture->setFiltering(filtering);
235 markDirty(DirtyMaterial);
236}
237
239{
240 if (m_geometryDirty) {
241 updateGeometry();
242 m_geometryDirty = false;
243 }
244}
245
246void QSGDefaultSpriteNode::updateGeometry()
247{
248 if (!m_material->texture)
249 return;
250
251 SpriteVertices *p = (SpriteVertices *) m_geometry->vertexData();
252
253 QRectF texRect = m_material->texture->normalizedTextureSubRect();
254
255 p->v1.tx = texRect.topLeft().x();
256 p->v1.ty = texRect.topLeft().y();
257
258 p->v2.tx = texRect.topRight().x();
259 p->v2.ty = texRect.topRight().y();
260
261 p->v3.tx = texRect.bottomLeft().x();
262 p->v3.ty = texRect.bottomLeft().y();
263
264 p->v4.tx = texRect.bottomRight().x();
265 p->v4.ty = texRect.bottomRight().y();
266
267 p->v1.x = 0;
268 p->v1.y = 0;
269
270 p->v2.x = m_size.width();
271 p->v2.y = 0;
272
273 p->v3.x = 0;
274 p->v3.y = m_size.height();
275
276 p->v4.x = m_size.width();
277 p->v4.y = m_size.height();
278 markDirty(DirtyGeometry);
279}
280
\inmodule QtCore
Definition qbytearray.h:57
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
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:484
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
int viewCount() const
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:208
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:335
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
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.
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
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:48
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.