Qt 6.x
The Qt SDK
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
17{
18 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/opaquetexture.vert.qsb"));
19 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/opaquetexture.frag.qsb"));
20}
21
23{
24 bool changed = false;
25 QByteArray *buf = state.uniformData();
26
27 if (state.isMatrixDirty()) {
28 const QMatrix4x4 m = state.combinedMatrix();
29 memcpy(buf->data(), m.constData(), 64);
30 changed = true;
31 }
32
33 return changed;
34}
35
37 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
38{
39 if (binding != 1)
40 return;
41
42#ifdef QT_NO_DEBUG
43 Q_UNUSED(oldMaterial);
44#endif
45 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
46 QSGOpaqueTextureMaterial *tx = static_cast<QSGOpaqueTextureMaterial *>(newMaterial);
47 QSGTexture *t = tx->texture();
48 if (!t) {
49 *texture = nullptr;
50 return;
51 }
52
53 t->setFiltering(tx->filtering());
54 t->setMipmapFiltering(tx->mipmapFiltering());
55 t->setAnisotropyLevel(tx->anisotropyLevel());
56
57 t->setHorizontalWrapMode(tx->horizontalWrapMode());
58 t->setVerticalWrapMode(tx->verticalWrapMode());
59 if (!state.rhi()->isFeatureSupported(QRhi::NPOTTextureRepeat)) {
60 QSize size = t->textureSize();
61 const bool isNpot = !isPowerOfTwo(size.width()) || !isPowerOfTwo(size.height());
62 if (isNpot) {
63 t->setHorizontalWrapMode(QSGTexture::ClampToEdge);
64 t->setVerticalWrapMode(QSGTexture::ClampToEdge);
65 t->setMipmapFiltering(QSGTexture::None);
66 }
67 }
68
69 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
70 *texture = t;
71}
72
73
120 : m_texture(nullptr)
121 , m_filtering(QSGTexture::Nearest)
122 , m_mipmap_filtering(QSGTexture::None)
123 , m_horizontal_wrap(QSGTexture::ClampToEdge)
124 , m_vertical_wrap(QSGTexture::ClampToEdge)
125 , m_anisotropy_level(QSGTexture::AnisotropyNone)
126{
127}
128
129
134{
135 static QSGMaterialType type;
136 return &type;
137}
138
143{
144 Q_UNUSED(renderMode);
146}
147
148
164{
167}
168
169
170
273{
274 Q_ASSERT(o && type() == o->type());
275 const QSGOpaqueTextureMaterial *other = static_cast<const QSGOpaqueTextureMaterial *>(o);
277 Q_ASSERT(other->texture());
278 const qint64 diff = m_texture->comparisonKey() - other->texture()->comparisonKey();
279 if (diff != 0)
280 return diff < 0 ? -1 : 1;
281 return int(m_filtering) - int(other->m_filtering);
282}
283
284
285
324{
325 static QSGMaterialType type;
326 return &type;
327}
328
334{
335 Q_UNUSED(renderMode);
337}
338
339
341{
342 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.vert.qsb"));
343 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.frag.qsb"));
344}
345
347{
348 bool changed = false;
349 QByteArray *buf = state.uniformData();
350
351 if (state.isOpacityDirty()) {
352 const float opacity = state.opacity();
353 memcpy(buf->data() + 64, &opacity, 4);
354 changed = true;
355 }
356
357 changed |= QSGOpaqueTextureMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
358
359 return changed;
360}
361
\inmodule QtCore
Definition qbytearray.h:57
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
@ NPOTTextureRepeat
Definition qrhi.h:1803
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.
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,...
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...
The QSGOpaqueTextureMaterial class provides a convenient way of rendering textured geometry in the sc...
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
QSGTexture::AnisotropyLevel anisotropyLevel() const
Returns this material's anistropy level.
QSGOpaqueTextureMaterial()
Creates a new QSGOpaqueTextureMaterial.
QSGTexture::WrapMode verticalWrapMode() const
Returns this material's vertical wrap mode.
int compare(const QSGMaterial *other) const override
QSGTexture::Filtering filtering() const
Returns this material's filtering mode.
void setTexture(QSGTexture *texture)
Sets the texture of this material to texture.
QSGTexture::WrapMode horizontalWrapMode() const
Returns this material's horizontal wrap mode.
QSGTexture::Filtering mipmapFiltering() const
Returns this material's mipmap filtering mode.
QSGMaterialType * type() const override
QSGTexture * texture() const
Returns this texture material's texture.
RenderMode
\value RenderMode2D Normal 2D rendering \value RenderMode2DNoDepthBuffer Normal 2D rendering with dep...
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...
QSGMaterialType * type() const override
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
\inmodule QtQuick
Definition qsgtexture.h:20
void setFiltering(Filtering filter)
Sets the sampling mode to filter.
virtual bool hasAlphaChannel() const =0
Returns true if the texture data contains an alpha channel.
virtual qint64 comparisonKey() const =0
Returns a key suitable for comparing textures.
\inmodule QtCore
Definition qsize.h:25
else opt state
[0]
Combined button and popup list for selecting options.
GLint GLint GLint GLint GLint x
[0]
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLuint texture
GLdouble GLdouble t
Definition qopenglext.h:243
static bool isPowerOfTwo(int x)
static bool isNpot(int width, int height=1, int depth=1)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QT_BEGIN_NAMESPACE bool isPowerOfTwo(int x)
#define QStringLiteral(str)
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:55
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
The QSGMaterialType class is used as a unique type token in combination with QSGMaterial.