| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQuick 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 "qsgvertexcolormaterial.h" |
| 41 | #if QT_CONFIG(opengl) |
| 42 | # include <qopenglshaderprogram.h> |
| 43 | #endif |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | class QSGVertexColorMaterialShader : public QSGMaterialShader |
| 47 | { |
| 48 | public: |
| 49 | QSGVertexColorMaterialShader(); |
| 50 | |
| 51 | void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override; |
| 52 | char const *const *attributeNames() const override; |
| 53 | |
| 54 | private: |
| 55 | void initialize() override; |
| 56 | #if QT_CONFIG(opengl) |
| 57 | int m_matrix_id; |
| 58 | int m_opacity_id; |
| 59 | #endif |
| 60 | }; |
| 61 | |
| 62 | QSGVertexColorMaterialShader::QSGVertexColorMaterialShader() |
| 63 | { |
| 64 | #if QT_CONFIG(opengl) |
| 65 | setShaderSourceFile(type: QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/scenegraph/shaders/vertexcolor.vert" )); |
| 66 | setShaderSourceFile(type: QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/scenegraph/shaders/vertexcolor.frag" )); |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | void QSGVertexColorMaterialShader::updateState(const RenderState &state, QSGMaterial * /*newEffect*/, QSGMaterial *) |
| 71 | { |
| 72 | #if QT_CONFIG(opengl) |
| 73 | if (state.isOpacityDirty()) |
| 74 | program()->setUniformValue(location: m_opacity_id, value: state.opacity()); |
| 75 | |
| 76 | if (state.isMatrixDirty()) |
| 77 | program()->setUniformValue(location: m_matrix_id, value: state.combinedMatrix()); |
| 78 | #else |
| 79 | Q_UNUSED(state) |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | char const *const *QSGVertexColorMaterialShader::attributeNames() const |
| 84 | { |
| 85 | static const char *const attr[] = { "vertexCoord" , "vertexColor" , nullptr }; |
| 86 | return attr; |
| 87 | } |
| 88 | |
| 89 | void QSGVertexColorMaterialShader::initialize() |
| 90 | { |
| 91 | #if QT_CONFIG(opengl) |
| 92 | m_matrix_id = program()->uniformLocation(name: "matrix" ); |
| 93 | m_opacity_id = program()->uniformLocation(name: "opacity" ); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | |
| 98 | class QSGVertexColorMaterialRhiShader : public QSGMaterialRhiShader |
| 99 | { |
| 100 | public: |
| 101 | QSGVertexColorMaterialRhiShader(); |
| 102 | |
| 103 | bool updateUniformData(RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override; |
| 104 | }; |
| 105 | |
| 106 | QSGVertexColorMaterialRhiShader::QSGVertexColorMaterialRhiShader() |
| 107 | { |
| 108 | setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/vertexcolor.vert.qsb" )); |
| 109 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/vertexcolor.frag.qsb" )); |
| 110 | } |
| 111 | |
| 112 | bool QSGVertexColorMaterialRhiShader::updateUniformData(RenderState &state, |
| 113 | QSGMaterial * /*newEffect*/, |
| 114 | QSGMaterial * /*oldEffect*/) |
| 115 | { |
| 116 | bool changed = false; |
| 117 | QByteArray *buf = state.uniformData(); |
| 118 | |
| 119 | if (state.isMatrixDirty()) { |
| 120 | const QMatrix4x4 m = state.combinedMatrix(); |
| 121 | memcpy(dest: buf->data(), src: m.constData(), n: 64); |
| 122 | changed = true; |
| 123 | } |
| 124 | |
| 125 | if (state.isOpacityDirty()) { |
| 126 | const float opacity = state.opacity(); |
| 127 | memcpy(dest: buf->data() + 64, src: &opacity, n: 4); |
| 128 | changed = true; |
| 129 | } |
| 130 | |
| 131 | return changed; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /*! |
| 136 | \class QSGVertexColorMaterial |
| 137 | \brief The QSGVertexColorMaterial class provides a convenient way of rendering per-vertex |
| 138 | colored geometry in the scene graph. |
| 139 | |
| 140 | \inmodule QtQuick |
| 141 | \ingroup qtquick-scenegraph-materials |
| 142 | |
| 143 | \warning This utility class is only functional when running with the |
| 144 | default backend of the Qt Quick scenegraph. |
| 145 | |
| 146 | The vertex color material will give each vertex in a geometry a color. Pixels between |
| 147 | vertices will be linearly interpolated. The colors can contain transparency. |
| 148 | |
| 149 | The geometry to be rendered with vertex color must have the following layout. Attribute |
| 150 | position 0 must contain vertices. Attribute position 1 must contain colors, a tuple of |
| 151 | 4 values with RGBA layout. Both floats in the range of 0 to 1 and unsigned bytes in |
| 152 | the range 0 to 255 are valid for the color values. |
| 153 | |
| 154 | \note The rendering pipeline expects pixels with premultiplied alpha. |
| 155 | |
| 156 | QSGGeometry::defaultAttributes_ColoredPoint2D() can be used to construct an attribute |
| 157 | set that is compatible with this material. |
| 158 | |
| 159 | The vertex color material respects both current opacity and current matrix when |
| 160 | updating it's rendering state. |
| 161 | */ |
| 162 | |
| 163 | |
| 164 | /*! |
| 165 | Creates a new vertex color material. |
| 166 | */ |
| 167 | |
| 168 | QSGVertexColorMaterial::QSGVertexColorMaterial() |
| 169 | { |
| 170 | setFlag(flags: Blending, on: true); |
| 171 | setFlag(flags: SupportsRhiShader, on: true); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /*! |
| 176 | int QSGVertexColorMaterial::compare() const |
| 177 | |
| 178 | As the vertex color material has all its state in the vertex attributes, |
| 179 | all materials will be equal. |
| 180 | |
| 181 | \internal |
| 182 | */ |
| 183 | |
| 184 | int QSGVertexColorMaterial::compare(const QSGMaterial * /* other */) const |
| 185 | { |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | \internal |
| 191 | */ |
| 192 | |
| 193 | QSGMaterialType *QSGVertexColorMaterial::type() const |
| 194 | { |
| 195 | static QSGMaterialType type; |
| 196 | return &type; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | /*! |
| 202 | \internal |
| 203 | */ |
| 204 | |
| 205 | QSGMaterialShader *QSGVertexColorMaterial::createShader() const |
| 206 | { |
| 207 | if (flags().testFlag(flag: RhiShaderWanted)) |
| 208 | return new QSGVertexColorMaterialRhiShader; |
| 209 | else |
| 210 | return new QSGVertexColorMaterialShader; |
| 211 | } |
| 212 | |
| 213 | QT_END_NAMESPACE |
| 214 | |