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
qsgtexturematerial.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#include <private/qsgtexture_p.h>
6#include <rhi/qrhi.h>
7
9
10inline static bool isPowerOfTwo(int x)
11{
12 // Assumption: x >= 1
13 return x == (x & -x);
14}
15
16QSGOpaqueTextureMaterialRhiShader::QSGOpaqueTextureMaterialRhiShader(int viewCount)
17{
18 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/opaquetexture.vert.qsb"), viewCount);
19 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/opaquetexture.frag.qsb"), viewCount);
20}
21
22bool QSGOpaqueTextureMaterialRhiShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
23{
24 bool changed = false;
25 QByteArray *buf = state.uniformData();
26 const int matrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
27
28 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
29 if (state.isMatrixDirty()) {
30 const QMatrix4x4 m = state.combinedMatrix(viewIndex);
31 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
32 changed = true;
33 }
34 }
35
36 return changed;
37}
38
39void QSGOpaqueTextureMaterialRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
40 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
41{
42 if (binding != 1)
43 return;
44
45#ifdef QT_NO_DEBUG
46 Q_UNUSED(oldMaterial);
47#endif
48 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
49 QSGOpaqueTextureMaterial *tx = static_cast<QSGOpaqueTextureMaterial *>(newMaterial);
50 QSGTexture *t = tx->texture();
51 if (!t) {
52 *texture = nullptr;
53 return;
54 }
55
56 t->setFiltering(tx->filtering());
57 t->setMipmapFiltering(tx->mipmapFiltering());
58 t->setAnisotropyLevel(tx->anisotropyLevel());
59
60 t->setHorizontalWrapMode(tx->horizontalWrapMode());
61 t->setVerticalWrapMode(tx->verticalWrapMode());
62 if (!state.rhi()->isFeatureSupported(QRhi::NPOTTextureRepeat)) {
63 QSize size = t->textureSize();
64 const bool isNpot = !isPowerOfTwo(size.width()) || !isPowerOfTwo(size.height());
65 if (isNpot) {
66 t->setHorizontalWrapMode(QSGTexture::ClampToEdge);
67 t->setVerticalWrapMode(QSGTexture::ClampToEdge);
68 t->setMipmapFiltering(QSGTexture::None);
69 }
70 }
71
72 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
73 *texture = t;
74}
75
76
77/*!
78 \class QSGOpaqueTextureMaterial
79 \brief The QSGOpaqueTextureMaterial class provides a convenient way of
80 rendering textured geometry in the scene graph.
81 \inmodule QtQuick
82 \ingroup qtquick-scenegraph-materials
83
84 \warning This utility class is only functional when running with the
85 default backend of the Qt Quick scenegraph.
86
87 The opaque textured material will fill every pixel in a geometry with
88 the supplied texture. The material does not respect the opacity of the
89 QSGMaterialShader::RenderState, so opacity nodes in the parent chain
90 of nodes using this material, have no effect.
91
92 The geometry to be rendered with an opaque texture material requires
93 vertices in attribute location 0 and texture coordinates in attribute
94 location 1. The texture coordinate is a 2-dimensional floating-point
95 tuple. The QSGGeometry::defaultAttributes_TexturedPoint2D returns an
96 attribute set compatible with this material.
97
98 The texture to be rendered can be set using setTexture(). How the
99 texture should be rendered can be specified using setMipmapFiltering(),
100 setFiltering(), setHorizontalWrapMode() and setVerticalWrapMode().
101 The rendering state is set on the texture instance just before it
102 is bound.
103
104 The opaque textured material respects the current matrix and the alpha
105 channel of the texture. It will disregard the accumulated opacity in
106 the scenegraph.
107
108 A texture material must have a texture set before it is used as
109 a material in the scene graph.
110 */
111
112
113
114/*!
115 Creates a new QSGOpaqueTextureMaterial.
116
117 The default mipmap filtering and filtering mode is set to
118 QSGTexture::Nearest. The default wrap modes is set to
119 \c QSGTexture::ClampToEdge.
120
121 */
122QSGOpaqueTextureMaterial::QSGOpaqueTextureMaterial()
123 : m_texture(nullptr)
124 , m_filtering(QSGTexture::Nearest)
125 , m_mipmap_filtering(QSGTexture::None)
126 , m_horizontal_wrap(QSGTexture::ClampToEdge)
127 , m_vertical_wrap(QSGTexture::ClampToEdge)
128 , m_anisotropy_level(QSGTexture::AnisotropyNone)
129{
130}
131
132
133/*!
134 \internal
135 */
136QSGMaterialType *QSGOpaqueTextureMaterial::type() const
137{
138 static QSGMaterialType type;
139 return &type;
140}
141
142/*!
143 \internal
144 */
145QSGMaterialShader *QSGOpaqueTextureMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
146{
147 Q_UNUSED(renderMode);
148 return new QSGOpaqueTextureMaterialRhiShader(viewCount());
149}
150
151
152/*!
153 \fn QSGTexture *QSGOpaqueTextureMaterial::texture() const
154
155 Returns this texture material's texture.
156 */
157
158
159
160/*!
161 Sets the texture of this material to \a texture.
162
163 The material does not take ownership of the texture.
164 */
165
166void QSGOpaqueTextureMaterial::setTexture(QSGTexture *texture)
167{
168 m_texture = texture;
169 setFlag(Blending, m_texture ? m_texture->hasAlphaChannel() : false);
170}
171
172
173
174/*!
175 \fn void QSGOpaqueTextureMaterial::setMipmapFiltering(QSGTexture::Filtering filtering)
176
177 Sets the mipmap mode to \a filtering.
178
179 The mipmap filtering mode is set on the texture instance just before the
180 texture is bound for rendering.
181
182 If the texture does not have mipmapping support, enabling mipmapping has no
183 effect.
184 */
185
186
187
188/*!
189 \fn QSGTexture::Filtering QSGOpaqueTextureMaterial::mipmapFiltering() const
190
191 Returns this material's mipmap filtering mode.
192
193 The default mipmap mode is \c QSGTexture::Nearest.
194 */
195
196
197
198/*!
199 \fn void QSGOpaqueTextureMaterial::setFiltering(QSGTexture::Filtering filtering)
200
201 Sets the filtering to \a filtering.
202
203 The filtering mode is set on the texture instance just before the texture
204 is bound for rendering.
205 */
206
207
208
209/*!
210 \fn QSGTexture::Filtering QSGOpaqueTextureMaterial::filtering() const
211
212 Returns this material's filtering mode.
213
214 The default filtering is \c QSGTexture::Nearest.
215 */
216
217
218
219/*!
220 \fn void QSGOpaqueTextureMaterial::setHorizontalWrapMode(QSGTexture::WrapMode mode)
221
222 Sets the horizontal wrap mode to \a mode.
223
224 The horizontal wrap mode is set on the texture instance just before the texture
225 is bound for rendering.
226 */
227
228
229
230 /*!
231 \fn QSGTexture::WrapMode QSGOpaqueTextureMaterial::horizontalWrapMode() const
232
233 Returns this material's horizontal wrap mode.
234
235 The default horizontal wrap mode is \c QSGTexture::ClampToEdge.
236 */
237
238
239
240/*!
241 \fn void QSGOpaqueTextureMaterial::setVerticalWrapMode(QSGTexture::WrapMode mode)
242
243 Sets the vertical wrap mode to \a mode.
244
245 The vertical wrap mode is set on the texture instance just before the texture
246 is bound for rendering.
247 */
248
249
250
251 /*!
252 \fn QSGTexture::WrapMode QSGOpaqueTextureMaterial::verticalWrapMode() const
253
254 Returns this material's vertical wrap mode.
255
256 The default vertical wrap mode is \c QSGTexture::ClampToEdge.
257 */
258
259/*!
260 \fn void QSGOpaqueTextureMaterial::setAnisotropyLevel(QSGTexture::AnisotropyLevel level)
261
262 Sets this material's anistropy level to \a level.
263*/
264
265/*!
266 \fn QSGTexture::AnisotropyLevel QSGOpaqueTextureMaterial::anisotropyLevel() const
267
268 Returns this material's anistropy level.
269*/
270
271/*!
272 \internal
273 */
274
275int QSGOpaqueTextureMaterial::compare(const QSGMaterial *o) const
276{
277 Q_ASSERT(o && type() == o->type());
278 const QSGOpaqueTextureMaterial *other = static_cast<const QSGOpaqueTextureMaterial *>(o);
279 Q_ASSERT(m_texture);
280 Q_ASSERT(other->texture());
281 const qint64 diff = m_texture->comparisonKey() - other->texture()->comparisonKey();
282 if (diff != 0)
283 return diff < 0 ? -1 : 1;
284 if (m_filtering != other->m_filtering)
285 return int(m_filtering) - int(other->m_filtering);
286 if (horizontalWrapMode() != other->horizontalWrapMode())
287 return int(horizontalWrapMode()) - int(other->horizontalWrapMode());
288 return int(verticalWrapMode()) - int(other->verticalWrapMode());
289}
290
291
292
293/*!
294 \class QSGTextureMaterial
295 \brief The QSGTextureMaterial class provides a convenient way of
296 rendering textured geometry in the scene graph.
297 \inmodule QtQuick
298 \ingroup qtquick-scenegraph-materials
299
300 \warning This utility class is only functional when running with the
301 default backend of the Qt Quick scenegraph.
302
303 The textured material will fill every pixel in a geometry with
304 the supplied texture.
305
306 The geometry to be rendered with a texture material requires
307 vertices in attribute location 0 and texture coordinates in attribute
308 location 1. The texture coordinate is a 2-dimensional floating-point
309 tuple. The QSGGeometry::defaultAttributes_TexturedPoint2D returns an
310 attribute set compatible with this material.
311
312 The texture to be rendered can be set using setTexture(). How the
313 texture should be rendered can be specified using setMipmapFiltering(),
314 setFiltering(), setHorizontalWrapMode() and setVerticalWrapMode().
315 The rendering state is set on the texture instance just before it
316 is bound.
317
318 The textured material respects the current matrix and the alpha
319 channel of the texture. It will also respect the accumulated opacity
320 in the scenegraph.
321
322 A texture material must have a texture set before it is used as
323 a material in the scene graph.
324 */
325
326/*!
327 \internal
328 */
329
330QSGMaterialType *QSGTextureMaterial::type() const
331{
332 static QSGMaterialType type;
333 return &type;
334}
335
336/*!
337 \internal
338 */
339
340QSGMaterialShader *QSGTextureMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
341{
342 Q_UNUSED(renderMode);
343 return new QSGTextureMaterialRhiShader(viewCount());
344}
345
346
349{
350 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.vert.qsb"), viewCount);
351 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.frag.qsb"), viewCount);
352}
353
354bool QSGTextureMaterialRhiShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
355{
356 bool changed = false;
357 QByteArray *buf = state.uniformData();
358 const int shaderMatrixCount = newMaterial->viewCount();
359
360 if (state.isOpacityDirty()) {
361 const float opacity = state.opacity();
362 memcpy(buf->data() + 64 * shaderMatrixCount, &opacity, 4);
363 changed = true;
364 }
365
366 changed |= QSGOpaqueTextureMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
367
368 return changed;
369}
370
371QT_END_NAMESPACE
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...
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE bool isPowerOfTwo(int x)