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
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"), viewCount);
19 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/opaquetexture.frag.qsb"), viewCount);
20}
21
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
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
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
137{
138 static QSGMaterialType type;
139 return &type;
140}
141
150
151
171
172
173
276{
277 Q_ASSERT(o && type() == o->type());
278 const QSGOpaqueTextureMaterial *other = static_cast<const QSGOpaqueTextureMaterial *>(o);
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 return int(m_filtering) - int(other->m_filtering);
285}
286
287
288
327{
328 static QSGMaterialType type;
329 return &type;
330}
331
341
342
345{
346 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.vert.qsb"), viewCount);
347 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/texture.frag.qsb"), viewCount);
348}
349
351{
352 bool changed = false;
353 QByteArray *buf = state.uniformData();
354 const int shaderMatrixCount = newMaterial->viewCount();
355
356 if (state.isOpacityDirty()) {
357 const float opacity = state.opacity();
358 memcpy(buf->data() + 64 * shaderMatrixCount, &opacity, 4);
359 changed = true;
360 }
361
362 changed |= QSGOpaqueTextureMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
363
364 return changed;
365}
366
\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:1841
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.
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.
@ None
Definition qhash.cpp:531
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
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:60
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
The QSGMaterialType class is used as a unique type token in combination with QSGMaterial.