1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "scene3dsgmaterialshader_p.h"
41
42#include <qopenglcontext.h>
43#include <qopenglfunctions.h>
44#include <QtGui/qsurfaceformat.h>
45
46#include <scene3dsgmaterial_p.h>
47
48QT_BEGIN_NAMESPACE
49
50namespace {
51
52inline bool isPowerOfTwo(int x)
53{
54 // Assumption: x >= 1
55 return x == (x & -x);
56}
57
58}
59
60namespace Qt3DRender {
61
62/*!
63 \class Qt3DCore::SCene3DMaterialShader
64 \internal
65
66 \brief The Qt3DRender::Scene3DSGMaterialShader class is a custom
67 QSGMaterialShader subclass instantiated by a Qt3DRender::Scene3DSGMateria1
68
69 The Qt3DRender::Scene3DSGMaterialShader provides a shader that renders a texture
70 using premultiplied alpha.
71 */
72
73QSGMaterialType Scene3DSGMaterialShader::type;
74
75Scene3DSGMaterialShader::Scene3DSGMaterialShader()
76 : QSGMaterialShader()
77 , m_matrixId(-1)
78 , m_opacityId(-1)
79 , m_visibleId(-1)
80{
81}
82
83const char * const *Scene3DSGMaterialShader::attributeNames() const
84{
85 static char const *const attr[] = { "qt_VertexPosition", "qt_VertexTexCoord", 0 };
86 return attr;
87}
88
89const char *Scene3DSGMaterialShader::vertexShader() const
90{
91 QOpenGLContext *ctx = QOpenGLContext::currentContext();
92 if (ctx->format().version() >= qMakePair(x: 3, y: 2) && ctx->format().profile() == QSurfaceFormat::CoreProfile) {
93 return ""
94 "#version 150 core \n"
95 "uniform mat4 qt_Matrix; \n"
96 "in vec4 qt_VertexPosition; \n"
97 "in vec2 qt_VertexTexCoord; \n"
98 "out vec2 qt_TexCoord; \n"
99 "void main() { \n"
100 " qt_TexCoord = qt_VertexTexCoord; \n"
101 " gl_Position = qt_Matrix * qt_VertexPosition; \n"
102 "}";
103 } else {
104 return ""
105 "uniform highp mat4 qt_Matrix; \n"
106 "attribute highp vec4 qt_VertexPosition; \n"
107 "attribute highp vec2 qt_VertexTexCoord; \n"
108 "varying highp vec2 qt_TexCoord; \n"
109 "void main() { \n"
110 " qt_TexCoord = qt_VertexTexCoord; \n"
111 " gl_Position = qt_Matrix * qt_VertexPosition; \n"
112 "}";
113 }
114}
115
116const char *Scene3DSGMaterialShader::fragmentShader() const
117{
118 QOpenGLContext *ctx = QOpenGLContext::currentContext();
119 if (ctx->format().version() >= qMakePair(x: 3, y: 2) && ctx->format().profile() == QSurfaceFormat::CoreProfile) {
120 return ""
121 "#version 150 core \n"
122 "uniform bool visible; \n"
123 "uniform sampler2D source; \n"
124 "uniform float qt_Opacity; \n"
125 "in vec2 qt_TexCoord; \n"
126 "out vec4 fragColor; \n"
127 "void main() { \n"
128 " if (!visible) discard; \n"
129 " vec4 p = texture(source, qt_TexCoord); \n"
130 " float a = qt_Opacity * p.a; \n"
131 " fragColor = vec4(p.rgb * a, a); \n"
132 "}";
133 } else {
134 return ""
135 "uniform bool visible; \n"
136 "uniform highp sampler2D source; \n"
137 "uniform highp float qt_Opacity; \n"
138 "varying highp vec2 qt_TexCoord; \n"
139 "void main() { \n"
140 " if (!visible) discard; \n"
141 " highp vec4 p = texture2D(source, qt_TexCoord); \n"
142 " highp float a = qt_Opacity * p.a; \n"
143 " gl_FragColor = vec4(p.rgb * a, a); \n"
144 "}";
145 }
146}
147
148void Scene3DSGMaterialShader::initialize()
149{
150 m_matrixId = program()->uniformLocation(name: "qt_Matrix");
151 m_opacityId = program()->uniformLocation(name: "qt_Opacity");
152 m_visibleId = program()->uniformLocation(name: "visible");
153}
154
155void Scene3DSGMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
156{
157 Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
158 Scene3DSGMaterial *tx = static_cast<Scene3DSGMaterial *>(newEffect);
159 Scene3DSGMaterial *oldTx = static_cast<Scene3DSGMaterial *>(oldEffect);
160
161 QSGTexture *t = tx->texture();
162
163 if (t != nullptr) {
164 bool npotSupported = const_cast<QOpenGLContext *>(state.context())
165 ->functions()->hasOpenGLFeature(feature: QOpenGLFunctions::NPOTTextureRepeat);
166 if (!npotSupported) {
167 QSize size = t->textureSize();
168 const bool isNpot = !isPowerOfTwo(x: size.width()) || !isPowerOfTwo(x: size.height());
169 if (isNpot) {
170 t->setHorizontalWrapMode(QSGTexture::ClampToEdge);
171 t->setVerticalWrapMode(QSGTexture::ClampToEdge);
172 }
173 }
174
175 if (oldTx == 0 || oldTx->texture()->textureId() != t->textureId())
176 t->bind();
177 else
178 t->updateBindOptions();
179 }
180
181 if (oldTx == nullptr || oldTx->visible() != tx->visible())
182 program()->setUniformValue(location: m_visibleId, value: tx->visible());
183
184 if (state.isMatrixDirty())
185 program()->setUniformValue(location: m_matrixId, value: state.combinedMatrix());
186
187 if (state.isOpacityDirty())
188 program()->setUniformValue(location: m_opacityId, value: state.opacity());
189}
190
191} // namespace Qt3DRender
192
193QT_END_NAMESPACE
194

source code of qt3d/src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp