diff options
35 files changed, 314 insertions, 1495 deletions
diff --git a/examples/quick/scenegraph/CMakeLists.txt b/examples/quick/scenegraph/CMakeLists.txt index 61e66aa322..2a5db7da54 100644 --- a/examples/quick/scenegraph/CMakeLists.txt +++ b/examples/quick/scenegraph/CMakeLists.txt @@ -5,7 +5,6 @@ add_subdirectory(customgeometry) add_subdirectory(threadedanimation) if(QT_FEATURE_opengl OR QT_FEATURE_opengles2 OR QT_FEATURE_opengles3) add_subdirectory(graph) - add_subdirectory(simplematerial) add_subdirectory(fboitem) add_subdirectory(openglunderqml) add_subdirectory(textureinthread) diff --git a/examples/quick/scenegraph/graph/graph.qrc b/examples/quick/scenegraph/graph/graph.qrc index 2adaeac751..b7fc7539f8 100644 --- a/examples/quick/scenegraph/graph/graph.qrc +++ b/examples/quick/scenegraph/graph/graph.qrc @@ -1,9 +1,9 @@ <RCC> <qresource prefix="/scenegraph/graph"> <file>main.qml</file> - <file>shaders/noisy.vsh</file> - <file>shaders/noisy.fsh</file> - <file>shaders/line.vsh</file> - <file>shaders/line.fsh</file> + <file>shaders/noisy.vert.qsb</file> + <file>shaders/noisy.frag.qsb</file> + <file>shaders/line.vert.qsb</file> + <file>shaders/line.frag.qsb</file> </qresource> </RCC> diff --git a/examples/quick/scenegraph/graph/linenode.cpp b/examples/quick/scenegraph/graph/linenode.cpp index cb7f1c8e7f..5d4d8acd20 100644 --- a/examples/quick/scenegraph/graph/linenode.cpp +++ b/examples/quick/scenegraph/graph/linenode.cpp @@ -52,45 +52,90 @@ #include <QtGui/QColor> -#include <QtQuick/QSGSimpleMaterial> +#include <QtQuick/QSGMaterial> -struct LineMaterial +class LineShader : public QSGMaterialRhiShader { - QColor color; - float spread; - float size; +public: + LineShader() { + setShaderFileName(VertexStage, QLatin1String(":/scenegraph/graph/shaders/line.vert.qsb")); + setShaderFileName(FragmentStage, QLatin1String(":/scenegraph/graph/shaders/line.frag.qsb")); + } + + bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; }; -class LineShader : public QSGSimpleMaterialShader<LineMaterial> +class LineMaterial : public QSGMaterial { - QSG_DECLARE_SIMPLE_SHADER(LineShader, LineMaterial) - public: - LineShader() { - setShaderSourceFile(QOpenGLShader::Vertex, ":/scenegraph/graph/shaders/line.vsh"); - setShaderSourceFile(QOpenGLShader::Fragment, ":/scenegraph/graph/shaders/line.fsh"); + LineMaterial() + { + setFlag(SupportsRhiShader); + setFlag(Blending); } - QList<QByteArray> attributes() const override { return QList<QByteArray>() << "pos" << "t"; } + QSGMaterialType *type() const override + { + static QSGMaterialType type; + return &type; + } - void updateState(const LineMaterial *m, const LineMaterial *) override { - program()->setUniformValue(id_color, m->color); - program()->setUniformValue(id_spread, m->spread); - program()->setUniformValue(id_size, m->size); + QSGMaterialShader *createShader() const override + { + Q_ASSERT(flags().testFlag(RhiShaderWanted)); + return new LineShader; } - void resolveUniforms() override { - id_spread = program()->uniformLocation("spread"); - id_size = program()->uniformLocation("size"); - id_color = program()->uniformLocation("color"); + int compare(const QSGMaterial *m) const override + { + const LineMaterial *other = static_cast<const LineMaterial *>(m); + + if (int diff = int(state.color.rgb()) - int(other->state.color.rgb())) + return diff; + + if (int diff = state.size - other->state.size) + return diff; + + if (int diff = state.spread - other->state.spread) + return diff; + + return 0; } -private: - int id_color = -1; - int id_spread = -1; - int id_size = -1; + struct { + QColor color; + float size; + float spread; + } state; }; +bool LineShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *) +{ + QByteArray *buf = state.uniformData(); + Q_ASSERT(buf->size() >= 92); + + if (state.isMatrixDirty()) { + const QMatrix4x4 m = state.combinedMatrix(); + memcpy(buf->data(), m.constData(), 64); + } + + if (state.isOpacityDirty()) { + const float opacity = state.opacity(); + memcpy(buf->data() + 80, &opacity, 4); + } + + LineMaterial *mat = static_cast<LineMaterial *>(newMaterial); + float c[4] = { float(mat->state.color.redF()), + float(mat->state.color.greenF()), + float(mat->state.color.blueF()), + float(mat->state.color.alphaF()) }; + memcpy(buf->data() + 64, c, 16); + memcpy(buf->data() + 84, &mat->state.size, 4); + memcpy(buf->data() + 88, &mat->state.spread, 4); + + return true; +} + struct LineVertex { float x; float y; @@ -114,11 +159,11 @@ LineNode::LineNode(float size, float spread, const QColor &color) setGeometry(&m_geometry); m_geometry.setDrawingMode(GL_TRIANGLE_STRIP); - QSGSimpleMaterial<LineMaterial> *m = LineShader::createMaterial(); - m->state()->color = color; - m->state()->size = size; - m->state()->spread = spread; - m->setFlag(QSGMaterial::Blending); + LineMaterial *m = new LineMaterial; + m->state.color = color; + m->state.size = size; + m->state.spread = spread; + setMaterial(m); setFlag(OwnsMaterial); } diff --git a/examples/quick/scenegraph/graph/main.cpp b/examples/quick/scenegraph/graph/main.cpp index 854fad84e3..95c310131b 100644 --- a/examples/quick/scenegraph/graph/main.cpp +++ b/examples/quick/scenegraph/graph/main.cpp @@ -56,6 +56,8 @@ int main(int argc, char *argv[]) { + qputenv("QSG_RHI", "1"); // ### Qt 6 remove, this will be the default anyway + QGuiApplication a(argc, argv); QQuickView view; diff --git a/examples/quick/scenegraph/graph/noisynode.cpp b/examples/quick/scenegraph/graph/noisynode.cpp index de5a616da2..d67110f48a 100644 --- a/examples/quick/scenegraph/graph/noisynode.cpp +++ b/examples/quick/scenegraph/graph/noisynode.cpp @@ -51,63 +51,113 @@ #include "noisynode.h" #include <QtCore/QRandomGenerator> -#include <QtQuick/QSGSimpleMaterialShader> + #include <QtQuick/QSGTexture> #include <QtQuick/QQuickWindow> +#include <QtQuick/QSGMaterial> #define NOISE_SIZE 64 -struct NoisyMaterial +class NoisyShader : public QSGMaterialRhiShader { - ~NoisyMaterial() { - delete texture; +public: + NoisyShader() { + setShaderFileName(VertexStage, QLatin1String(":/scenegraph/graph/shaders/noisy.vert.qsb")); + setShaderFileName(FragmentStage, QLatin1String(":/scenegraph/graph/shaders/noisy.frag.qsb")); } - QColor color; - QSGTexture *texture; + bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; + void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, + QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; }; -class NoisyShader : public QSGSimpleMaterialShader<NoisyMaterial> +class NoisyMaterial : public QSGMaterial { - QSG_DECLARE_SIMPLE_SHADER(NoisyShader, NoisyMaterial) - public: - NoisyShader() { - setShaderSourceFile(QOpenGLShader::Vertex, ":/scenegraph/graph/shaders/noisy.vsh"); - setShaderSourceFile(QOpenGLShader::Fragment, ":/scenegraph/graph/shaders/noisy.fsh"); + NoisyMaterial() + { + setFlag(SupportsRhiShader); + setFlag(Blending); + } + + ~NoisyMaterial() + { + delete state.texture; } - QList<QByteArray> attributes() const override { return QList<QByteArray>() << "aVertex" << "aTexCoord"; } + QSGMaterialType *type() const override + { + static QSGMaterialType type; + return &type; + } - void updateState(const NoisyMaterial *m, const NoisyMaterial *) override { + QSGMaterialShader *createShader() const override + { + Q_ASSERT(flags().testFlag(RhiShaderWanted)); + return new NoisyShader; + } - // Set the color - program()->setUniformValue(id_color, m->color); + int compare(const QSGMaterial *m) const override + { + const NoisyMaterial *other = static_cast<const NoisyMaterial *>(m); - // Bind the texture and set program to use texture unit 0 (the default) - m->texture->bind(); + if (int diff = int(state.color.rgb()) - int(other->state.color.rgb())) + return diff; - // Then set the texture size so we can adjust the texture coordinates accordingly in the - // vertex shader.. - QSize s = m->texture->textureSize(); - program()->setUniformValue(id_textureSize, QSizeF(1.0 / s.width(), 1.0 / s.height())); - } + if (!state.texture || !other->state.texture) + return state.texture ? 1 : -1; - void resolveUniforms() override { - id_texture = program()->uniformLocation("texture"); - id_textureSize = program()->uniformLocation("textureSize"); - id_color = program()->uniformLocation("color"); + if (int diff = state.texture->comparisonKey() - other->state.texture->comparisonKey()) + return diff; - // We will only use texture unit 0, so set it only once. - program()->setUniformValue(id_texture, 0); + return 0; } -private: - int id_color = -1; - int id_texture = -1; - int id_textureSize = -1; + struct { + QColor color; + QSGTexture *texture; + } state; }; +bool NoisyShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *) +{ + QByteArray *buf = state.uniformData(); + Q_ASSERT(buf->size() >= 92); + + if (state.isMatrixDirty()) { + const QMatrix4x4 m = state.combinedMatrix(); + memcpy(buf->data(), m.constData(), 64); + } + + if (state.isOpacityDirty()) { + const float opacity = state.opacity(); + memcpy(buf->data() + 88, &opacity, 4); + } + + NoisyMaterial *mat = static_cast<NoisyMaterial *>(newMaterial); + float c[4] = { float(mat->state.color.redF()), + float(mat->state.color.greenF()), + float(mat->state.color.blueF()), + float(mat->state.color.alphaF()) }; + memcpy(buf->data() + 64, c, 16); + + const QSize s = mat->state.texture->textureSize(); + float textureSize[2] = { 1.0f / s.width(), 1.0f / s.height() }; + memcpy(buf->data() + 80, textureSize, 8); + + return true; +} + +void NoisyShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture, + QSGMaterial *newMaterial, QSGMaterial *) +{ + Q_UNUSED(state); + Q_UNUSED(binding); + + NoisyMaterial *mat = static_cast<NoisyMaterial *>(newMaterial); + *texture = mat->state.texture; +} + NoisyNode::NoisyNode(QQuickWindow *window) { // Make some noise... @@ -123,10 +173,9 @@ NoisyNode::NoisyNode(QQuickWindow *window) t->setHorizontalWrapMode(QSGTexture::Repeat); t->setVerticalWrapMode(QSGTexture::Repeat); - QSGSimpleMaterial<NoisyMaterial> *m = NoisyShader::createMaterial(); - m->state()->texture = t; - m->state()->color = QColor::fromRgbF(0.95, 0.95, 0.97); - m->setFlag(QSGMaterial::Blending); + NoisyMaterial *m = new NoisyMaterial; + m->state.texture = t; + m->state.color = QColor::fromRgbF(0.95, 0.95, 0.97); setMaterial(m); setFlag(OwnsMaterial, true); diff --git a/examples/quick/scenegraph/graph/shaders/compile.bat b/examples/quick/scenegraph/graph/shaders/compile.bat new file mode 100644 index 0000000000..4ec3f2ec8e --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/compile.bat @@ -0,0 +1,54 @@ +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:: +:: Copyright (C) 2019 The Qt Company Ltd. +:: Contact: https://www.qt.io/licensing/ +:: +:: This file is part of the examples of the Qt Toolkit. +:: +:: $QT_BEGIN_LICENSE:BSD$ +:: Commercial License Usage +:: Licensees holding valid commercial Qt licenses may use this file in +:: accordance with the commercial license agreement provided with the +:: Software or, alternatively, in accordance with the terms contained in +:: a written agreement between you and The Qt Company. For licensing terms +:: and conditions see https://www.qt.io/terms-conditions. For further +:: information use the contact form at https://www.qt.io/contact-us. +:: +:: BSD License Usage +:: Alternatively, you may use this file under the terms of the BSD license +:: as follows: +:: +:: "Redistribution and use in source and binary forms, with or without +:: modification, are permitted provided that the following conditions are +:: met: +:: * Redistributions of source code must retain the above copyright +:: notice, this list of conditions and the following disclaimer. +:: * Redistributions in binary form must reproduce the above copyright +:: notice, this list of conditions and the following disclaimer in +:: the documentation and/or other materials provided with the +:: distribution. +:: * Neither the name of The Qt Company Ltd nor the names of its +:: contributors may be used to endorse or promote products derived +:: from this software without specific prior written permission. +:: +:: +:: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +:: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +:: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +:: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +:: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +:: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +:: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +:: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +:: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +:: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +:: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +:: +:: $QT_END_LICENSE$ +:: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: + +qsb -b --glsl "150,120,100 es" --hlsl 50 --msl 12 -o noisy.vert.qsb noisy.vert +qsb --glsl "150,120,100 es" --hlsl 50 --msl 12 -o noisy.frag.qsb noisy.frag +qsb -b --glsl "150,120,100 es" --hlsl 50 --msl 12 -o line.vert.qsb line.vert +qsb --glsl "150,120,100 es" --hlsl 50 --msl 12 -o line.frag.qsb line.frag diff --git a/examples/quick/scenegraph/graph/shaders/line.frag b/examples/quick/scenegraph/graph/shaders/line.frag new file mode 100644 index 0000000000..44b5b24c94 --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/line.frag @@ -0,0 +1,21 @@ +#version 440 + +layout(location = 0) in float vT; + +layout(location = 0) out vec4 fragColor; + +layout(std140, binding = 0) uniform buf { + mat4 qt_Matrix; + vec4 color; + float qt_Opacity; + float size; + float spread; +}; + +#define PI 3.14159265358979323846 + +void main(void) +{ + float tt = smoothstep(spread, 1.0, sin(vT * PI)); + fragColor = color * qt_Opacity * tt; +} diff --git a/examples/quick/scenegraph/graph/shaders/line.frag.qsb b/examples/quick/scenegraph/graph/shaders/line.frag.qsb Binary files differnew file mode 100644 index 0000000000..7efcd94c37 --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/line.frag.qsb diff --git a/examples/quick/scenegraph/graph/shaders/line.fsh b/examples/quick/scenegraph/graph/shaders/line.fsh deleted file mode 100644 index 77e05a2d15..0000000000 --- a/examples/quick/scenegraph/graph/shaders/line.fsh +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -uniform lowp vec4 color; -uniform lowp float qt_Opacity; -uniform lowp float spread; - -varying lowp float vT; - -#define PI 3.14159265358979323846 - -void main(void) -{ - lowp float tt = smoothstep(spread, 1.0, sin(vT * PI)); - - gl_FragColor = color * qt_Opacity * tt; -} diff --git a/examples/quick/scenegraph/graph/shaders/line.vert b/examples/quick/scenegraph/graph/shaders/line.vert new file mode 100644 index 0000000000..eac4273f0a --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/line.vert @@ -0,0 +1,25 @@ +#version 440 + +layout(location = 0) in vec4 pos; +layout(location = 1) in float t; + +layout(location = 0) out float vT; + +layout(std140, binding = 0) uniform buf { + mat4 qt_Matrix; + vec4 color; + float qt_Opacity; + float size; + float spread; +}; + +out gl_PerVertex { vec4 gl_Position; }; + +void main(void) +{ + vec4 adjustedPos = pos; + adjustedPos.y += (t * size); + gl_Position = qt_Matrix * adjustedPos; + + vT = t; +} diff --git a/examples/quick/scenegraph/graph/shaders/line.vert.qsb b/examples/quick/scenegraph/graph/shaders/line.vert.qsb Binary files differnew file mode 100644 index 0000000000..c17b16960f --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/line.vert.qsb diff --git a/examples/quick/scenegraph/graph/shaders/line.vsh b/examples/quick/scenegraph/graph/shaders/line.vsh deleted file mode 100644 index 4f00ab9e5d..0000000000 --- a/examples/quick/scenegraph/graph/shaders/line.vsh +++ /dev/null @@ -1,66 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -attribute highp vec4 pos; -attribute highp float t; - -uniform lowp float size; -uniform highp mat4 qt_Matrix; - -varying lowp float vT; - -void main(void) -{ - vec4 adjustedPos = pos; - adjustedPos.y += (t * size); - gl_Position = qt_Matrix * adjustedPos; - - vT = t; -} diff --git a/examples/quick/scenegraph/graph/shaders/noisy.frag b/examples/quick/scenegraph/graph/shaders/noisy.frag new file mode 100644 index 0000000000..8cea9de02b --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/noisy.frag @@ -0,0 +1,24 @@ +#version 440 + +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in vec2 vShadeCoord; + +layout(location = 0) out vec4 fragColor; + +layout(std140, binding = 0) uniform buf { + mat4 qt_Matrix; + vec4 color; + vec2 textureSize; + float qt_Opacity; +}; + +layout(binding = 1) uniform sampler2D qt_Texture; + +#define PI 3.14159265358979323846 + +void main() +{ + float shade = texture(qt_Texture, vTexCoord).r * 0.05 - length(vec2(0.5, 0.4) - vShadeCoord) * 0.3; + vec4 c = vec4(color.xyz + shade, color.w); + fragColor = c * qt_Opacity; +} diff --git a/examples/quick/scenegraph/graph/shaders/noisy.frag.qsb b/examples/quick/scenegraph/graph/shaders/noisy.frag.qsb Binary files differnew file mode 100644 index 0000000000..77f29bd253 --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/noisy.frag.qsb diff --git a/examples/quick/scenegraph/graph/shaders/noisy.fsh b/examples/quick/scenegraph/graph/shaders/noisy.fsh deleted file mode 100644 index 14ea675360..0000000000 --- a/examples/quick/scenegraph/graph/shaders/noisy.fsh +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -uniform sampler2D texture; -uniform lowp float qt_Opacity; -uniform lowp vec4 color; - -varying highp vec2 vTexCoord; -varying lowp vec2 vShadeCoord; - -#define PI 3.14159265358979323846 - -void main() -{ - lowp float shade = texture2D(texture, vTexCoord).r * 0.05 - length(vec2(0.5, 0.4) - vShadeCoord) * 0.3; - lowp vec4 c = vec4(color.xyz + shade, color.w); - gl_FragColor = c * qt_Opacity; -} diff --git a/examples/quick/scenegraph/graph/shaders/noisy.vert b/examples/quick/scenegraph/graph/shaders/noisy.vert new file mode 100644 index 0000000000..5728f2a02f --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/noisy.vert @@ -0,0 +1,22 @@ +#version 440 + +layout(location = 0) in vec4 aVertex; +layout(location = 1) in vec2 aTexCoord; + +layout(location = 0) out vec2 vTexCoord; +layout(location = 1) out vec2 vShadeCoord; + +layout(std140, binding = 0) uniform buf { + mat4 qt_Matrix; + vec4 color; + vec2 textureSize; + float qt_Opacity; +}; + +out gl_PerVertex { vec4 gl_Position; }; + +void main() { + gl_Position = qt_Matrix * aVertex; + vTexCoord = aVertex.xy * textureSize; + vShadeCoord = aTexCoord; +} diff --git a/examples/quick/scenegraph/graph/shaders/noisy.vert.qsb b/examples/quick/scenegraph/graph/shaders/noisy.vert.qsb Binary files differnew file mode 100644 index 0000000000..d405024983 --- /dev/null +++ b/examples/quick/scenegraph/graph/shaders/noisy.vert.qsb diff --git a/examples/quick/scenegraph/graph/shaders/noisy.vsh b/examples/quick/scenegraph/graph/shaders/noisy.vsh deleted file mode 100644 index 1f89dbcc29..0000000000 --- a/examples/quick/scenegraph/graph/shaders/noisy.vsh +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -attribute highp vec4 aVertex; -attribute highp vec2 aTexCoord; - -uniform highp mat4 qt_Matrix; -uniform highp vec2 textureSize; - -varying highp vec2 vTexCoord; -varying lowp vec2 vShadeCoord; - -void main() { - gl_Position = qt_Matrix * aVertex; - vTexCoord = aVertex.xy * textureSize; - vShadeCoord = aTexCoord; -} diff --git a/examples/quick/scenegraph/scenegraph.pro b/examples/quick/scenegraph/scenegraph.pro index 235b51cd72..b9ce61e21d 100644 --- a/examples/quick/scenegraph/scenegraph.pro +++ b/examples/quick/scenegraph/scenegraph.pro @@ -3,7 +3,6 @@ TEMPLATE = subdirs qtConfig(opengl(es1|es2)?) { SUBDIRS += \ graph \ - simplematerial \ fboitem \ openglunderqml \ textureinthread \ diff --git a/examples/quick/scenegraph/simplematerial/CMakeLists.txt b/examples/quick/scenegraph/simplematerial/CMakeLists.txt deleted file mode 100644 index 332f972d60..0000000000 --- a/examples/quick/scenegraph/simplematerial/CMakeLists.txt +++ /dev/null @@ -1,53 +0,0 @@ -# Generated from simplematerial.pro. - -cmake_minimum_required(VERSION 3.14) -project(simplematerial LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -set(INSTALL_EXAMPLEDIR "examples/quick/scenegraph/simplematerial") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Quick) - -add_qt_gui_executable(simplematerial - simplematerial.cpp - simplematerialitem.cpp simplematerialitem.h -) -target_link_libraries(simplematerial PUBLIC - Qt::Core - Qt::Gui - Qt::Quick -) - - -# Resources: -set(simplematerial_resource_files - "main.qml" -) - -qt6_add_resources(simplematerial "simplematerial" - PREFIX - "/scenegraph/simplematerial" - FILES - ${simplematerial_resource_files} -) - -install(TARGETS simplematerial - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) - -set_target_properties(simplematerial PROPERTIES - QT_QML_MODULE_INSTALL_QMLTYPES TRUE - QT_QML_MODULE_VERSION 1.0 - QT_QML_MODULE_URI SimpleMaterial -) - -qt6_qml_type_registration(simplematerial) diff --git a/examples/quick/scenegraph/simplematerial/doc/images/simplematerial-example.jpg b/examples/quick/scenegraph/simplematerial/doc/images/simplematerial-example.jpg Binary files differdeleted file mode 100644 index 08db8dee4e..0000000000 --- a/examples/quick/scenegraph/simplematerial/doc/images/simplematerial-example.jpg +++ /dev/null diff --git a/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc b/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc deleted file mode 100644 index d7de1613e4..0000000000 --- a/examples/quick/scenegraph/simplematerial/doc/src/simplematerial.qdoc +++ /dev/null @@ -1,180 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/*! - \example scenegraph/simplematerial - \title Scene Graph - Simple Material - \ingroup qtquickexamples - \brief Shows how to define a scene graph material to fill a shape. - - \image simplematerial-example.jpg - - In this example, we will make use of the \l - QSGSimpleMaterialShader class to fill a shape in the scene - graph. This is a convenience class intended to avoid a lot of the - boilerplate code required when creating materials with the \l - QSGMaterial, \l QSGMaterialShader and \l QSGMaterialType classes - directly. - - A simple material consists of two parts: the material state and - the material shader. The material shader has one instance per - scene graph and contains the actual OpenGL shader program and - information about which attributes and uniforms it uses. The - material state is what we assign to each individual node; in this - case to give them different colors. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 1 - - The first thing we do when creating custom materials with the - simplified scheme is to create a state class. In this case the - state class contains only one member, a QColor. It also defines a - compare function which the scene graph can use to reorder the node - rendering. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 2 - - Next we define the material shader, by subclassing a template - instantiation of \l QSGSimpleMaterialShader with our \c State. - - Then we use the macro \l QSG_DECLARE_SIMPLE_COMPARABLE_SHADER() - which will generate some boilerplate code for us. Since our \c - State class has a compare function, we declare that the states can - be compared. It would have been possible to remove the \c - State::compare() function and instead declare the shader with \l - QSG_DECLARE_SIMPLE_SHADER(), but this could then reduce performance - in certain use cases. - - The state struct is used as a template parameter to - automatically generate a \l QSGMaterialType for us, so it is - crucial that the pair of shader and state are made up of unique - classes. Using the same \c State class in multiple shaders will - will lead to undefined behavior. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 3 - - Next comes the declaration of the shader source code, where we - define a vertex and fragment shader. The simple material assumes - the presence of \c qt_Matrix in the vertex shader and \c - qt_Opacity in the fragment shader. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 4 - - We reimplement the \c attributes function to return the name of - the \c aVertex and \c aTexCoord attributes. These attributes - will be mapped to attribute indices 0 and 1 in the node's - geometry. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 6 - - Uniforms can be accessed either by name or by index, where index - is faster than name. We reimplement the \c resolveUniforms() - function to find the index of the \c color uniform. We do not have - to worry about resolving \c qt_Opacity or \c qt_Matrix as these - are handled by the baseclass. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 5 - - The \c updateState() function is called once for every unique - state and we use it to update the shader program with the current - color. The previous state is passed in as a second parameter so - that the user can update only that which has changed. In our - use case, where all the colors are different, the updateState() - function will be called once for every node. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 7 - - The \c ColorNode class is supposed to draw something, so it needs - to be a subclass of \l QSGGeometryNode. - - Since our shader expects both a position and a texture coordinate, - we use the default attribute set \l - QSGGeometry::defaultAttributes_TexturedPoint2D() and declare that - the geometry consists of a total of four vertices. To avoid the - allocation, we make the QSGGeometry a member of the - QSGGeometryNode. - - When we used the macro \l QSG_DECLARE_SIMPLE_COMPARABLE_SHADER() above, - it defined the \c createMaterial() function which we use to - instantiate materials for our \c State struct. - - As we will be making use of opacity in our custom material, we - need to set the \l QSGMaterial::Blending flag. The scene graph may - use this flag to either disable or enable \c GL_BLEND when drawing - the node or to reorder the drawing of the node. - - Finally, we tell the node to take ownership of the material, so we - do not have to explicitly memory-manage it. - - \snippet scenegraph/simplematerial/simplematerialitem.h 8 - - Since the Item is providing its own graphics to the scene graph, - we set the flag \l QQuickItem::ItemHasContents. We also make sure - the item is exposed to QML by adding the QML_ELEMENT macro. - - \snippet scenegraph/simplematerial/simplematerialitem.cpp 9 - - Whenever the Item has changed graphically, the \l - QQuickItem::updatePaintNode() function is called. - - \note The scene graph may be rendered in a different thread than the - GUI thread and \l QQuickItem::updatePaintNode() is one of the few - places where it is safe to access properties of the QML - object. Any interaction with the scene graph from a custom \l - QQuickItem should be contained within this function. The function is - called on the rendering thread while the GUI thread is blocked. - - The first time this function is called for an \c Item instance, - the node will be 0, and so we create a new one. For every consecutive - call, the node will be what we returned previously. There are - scenarios where the scene graph will be removed and rebuilt from - scratch however, so one should always check the node and recreate - it if required. - - Once we have a \c ColorNode, we update its geometry and material - state. Finally, we notify the scene graph that the node has - undergone changes to its geometry and material. - - \snippet scenegraph/simplematerial/simplematerial.cpp 11 - - The \c main() function of the application opens up a \l QQuickView - with our QML file. - - \snippet scenegraph/simplematerial/main.qml 1 - - In the QML file, we import our custom type so we can instantiate - it. - - \snippet scenegraph/simplematerial/main.qml 2 - - Then we create a column containing three instances of our custom item, - each with a different color. - - \snippet scenegraph/simplematerial/main.qml 3 - - And finally we overlay a short descriptive text. - - */ diff --git a/examples/quick/scenegraph/simplematerial/main.qml b/examples/quick/scenegraph/simplematerial/main.qml deleted file mode 100644 index 98d0e92940..0000000000 --- a/examples/quick/scenegraph/simplematerial/main.qml +++ /dev/null @@ -1,107 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [1] -import QtQuick 2.0 -import SimpleMaterial 1.0 - -Rectangle { - width: 320 - height: 480 - color: "black" - -//! [1] //! [2] - - Column { - anchors.fill: parent - - SimpleMaterialItem { - width: parent.width; - height: parent.height / 3; - color: "steelblue" - } - - SimpleMaterialItem { - width: parent.width; - height: parent.height / 3; - color: "darkorchid" - } - - SimpleMaterialItem { - width: parent.width; - height: parent.height / 3; - color: "springgreen" - } - } - - -//! [2] //! [3] - Rectangle { - color: Qt.rgba(0, 0, 0, 0.8) - radius: 10 - antialiasing: true - border.width: 1 - border.color: "black" - anchors.fill: label - anchors.margins: -10 - } - - Text { - id: label - color: "white" - wrapMode: Text.WordWrap - text: "These three gradient boxes are colorized using a custom material." - anchors.right: parent.right - anchors.left: parent.left - anchors.bottom: parent.bottom - anchors.margins: 20 - } -} -//! [3] diff --git a/examples/quick/scenegraph/simplematerial/simplematerial.cpp b/examples/quick/scenegraph/simplematerial/simplematerial.cpp deleted file mode 100644 index 35866bd6d0..0000000000 --- a/examples/quick/scenegraph/simplematerial/simplematerial.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <qguiapplication.h> -#include <qquickview.h> - -//! [11] -int main(int argc, char **argv) -{ - QGuiApplication app(argc, argv); - - QQuickView view; - view.setResizeMode(QQuickView::SizeRootObjectToView); - view.setSource(QUrl("qrc:///scenegraph/simplematerial/main.qml")); - view.show(); - - return app.exec(); -} -//! [11] diff --git a/examples/quick/scenegraph/simplematerial/simplematerial.pro b/examples/quick/scenegraph/simplematerial/simplematerial.pro deleted file mode 100644 index 131af45974..0000000000 --- a/examples/quick/scenegraph/simplematerial/simplematerial.pro +++ /dev/null @@ -1,20 +0,0 @@ - -QT += quick - -CONFIG += qmltypes -QML_IMPORT_NAME = SimpleMaterial -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += \ - simplematerial.cpp \ - simplematerialitem.cpp -RESOURCES += simplematerial.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/simplematerial -qml.files = main.qml -qml.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/simplematerial - -INSTALLS += target qml - -HEADERS += \ - simplematerialitem.h diff --git a/examples/quick/scenegraph/simplematerial/simplematerial.qrc b/examples/quick/scenegraph/simplematerial/simplematerial.qrc deleted file mode 100644 index 4cc1eaafe4..0000000000 --- a/examples/quick/scenegraph/simplematerial/simplematerial.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<RCC> - <qresource prefix="/scenegraph/simplematerial"> - <file>main.qml</file> - </qresource> -</RCC> diff --git a/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp b/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp deleted file mode 100644 index c4c7b2e07e..0000000000 --- a/examples/quick/scenegraph/simplematerial/simplematerialitem.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "simplematerialitem.h" - -#include <QtQuick/qsgsimplematerial.h> -#include <QtQuick/qsggeometry.h> -#include <QtQuick/qsgnode.h> - -//! [1] -struct State -{ - QColor color; - - int compare(const State *other) const { - uint rgb = color.rgba(); - uint otherRgb = other->color.rgba(); - - if (rgb == otherRgb) { - return 0; - } else if (rgb < otherRgb) { - return -1; - } else { - return 1; - } - } -}; -//! [1] - -//! [2] -class Shader : public QSGSimpleMaterialShader<State> -{ - QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State); -//! [2] //! [3] -public: - - const char *vertexShader() const override { - return - "attribute highp vec4 aVertex; \n" - "attribute highp vec2 aTexCoord; \n" - "uniform highp mat4 qt_Matrix; \n" - "varying highp vec2 texCoord; \n" - "void main() { \n" - " gl_Position = qt_Matrix * aVertex; \n" - " texCoord = aTexCoord; \n" - "}"; - } - - const char *fragmentShader() const override { - return - "uniform lowp float qt_Opacity; \n" - "uniform lowp vec4 color; \n" - "varying highp vec2 texCoord; \n" - "void main () \n" - "{ \n" - " gl_FragColor = texCoord.y * texCoord.x * color * qt_Opacity; \n" - "}"; - } -//! [3] //! [4] - QList<QByteArray> attributes() const override - { - return QList<QByteArray>() << "aVertex" << "aTexCoord"; - } -//! [4] //! [5] - void updateState(const State *state, const State *) override - { - program()->setUniformValue(id_color, state->color); - } -//! [5] //! [6] - void resolveUniforms() override - { - id_color = program()->uniformLocation("color"); - } - -private: - int id_color; -//! [6] -}; - - -//! [7] -class ColorNode : public QSGGeometryNode -{ -public: - ColorNode() - : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4) - { - setGeometry(&m_geometry); - - QSGSimpleMaterial<State> *material = Shader::createMaterial(); - material->setFlag(QSGMaterial::Blending); - setMaterial(material); - setFlag(OwnsMaterial); - } - - QSGGeometry m_geometry; -}; -//! [7] - -void SimpleMaterialItem::setColor(const QColor &color) { - if (m_color != color) { - m_color = color; - emit colorChanged(); - update(); - } -} - -//! [9] -QSGNode *SimpleMaterialItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) -{ - ColorNode *n = static_cast<ColorNode *>(node); - if (!node) - n = new ColorNode(); - - QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1)); - static_cast<QSGSimpleMaterial<State>*>(n->material())->state()->color = m_color; - - n->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial); - - return n; -} -//! [9] diff --git a/examples/quick/scenegraph/simplematerial/simplematerialitem.h b/examples/quick/scenegraph/simplematerial/simplematerialitem.h deleted file mode 100644 index 5775ad097b..0000000000 --- a/examples/quick/scenegraph/simplematerial/simplematerialitem.h +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef SIMPLEMATERIALITEM_H -#define SIMPLEMATERIALITEM_H - -#include <QtQuick/qquickitem.h> - -//! [8] -class SimpleMaterialItem : public QQuickItem -{ - Q_OBJECT - - Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) - QML_ELEMENT - -public: - - SimpleMaterialItem() { setFlag(ItemHasContents, true); } - - void setColor(const QColor &color); - QColor color() const { return m_color; } - -signals: - void colorChanged(); - -private: - QColor m_color; - -public: - QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; -}; -//! [8] - -#endif // SIMPLEMATERIALITEM_H diff --git a/src/quick/CMakeLists.txt b/src/quick/CMakeLists.txt index 24d015f576..88ddc4e257 100644 --- a/src/quick/CMakeLists.txt +++ b/src/quick/CMakeLists.txt @@ -131,7 +131,6 @@ qt_add_module(Quick scenegraph/util/qsgninepatchnode.cpp scenegraph/util/qsgninepatchnode.h scenegraph/util/qsgplaintexture.cpp scenegraph/util/qsgplaintexture_p.h scenegraph/util/qsgrectanglenode.cpp scenegraph/util/qsgrectanglenode.h - scenegraph/util/qsgsimplematerial.cpp scenegraph/util/qsgsimplematerial.h scenegraph/util/qsgsimplerectnode.cpp scenegraph/util/qsgsimplerectnode.h scenegraph/util/qsgsimpletexturenode.cpp scenegraph/util/qsgsimpletexturenode.h scenegraph/util/qsgtexturematerial.cpp scenegraph/util/qsgtexturematerial.h scenegraph/util/qsgtexturematerial_p.h diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index 707f149baf..a1f8fd35d8 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -3802,7 +3802,7 @@ void QQuickItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeo \note All classes with QSG prefix should be used solely on the scene graph's rendering thread. See \l {Scene Graph and Rendering} for more information. - \sa QSGMaterial, QSGSimpleMaterial, QSGGeometryNode, QSGGeometry, + \sa QSGMaterial, QSGGeometryNode, QSGGeometry, QSGFlatColorMaterial, QSGTextureMaterial, QSGNode::markDirty(), {Graphics Resource Handling} */ diff --git a/src/quick/scenegraph/coreapi/qsgmaterialshader.cpp b/src/quick/scenegraph/coreapi/qsgmaterialshader.cpp index 9049dd5826..64956e2ca4 100644 --- a/src/quick/scenegraph/coreapi/qsgmaterialshader.cpp +++ b/src/quick/scenegraph/coreapi/qsgmaterialshader.cpp @@ -69,10 +69,6 @@ const char *QSGMaterialShaderPrivate::loadShaderSource(QOpenGLShader::ShaderType \inmodule QtQuick \ingroup qtquick-scenegraph-materials - The QSGMaterialShader API is relatively low-level. A more convenient API, - which provides almost all the same features, is available through - QSGSimpleMaterialShader. - \warning This class is only functional when running with the legacy OpenGL renderer of the Qt Quick scenegraph. diff --git a/src/quick/scenegraph/coreapi/qsgnode.cpp b/src/quick/scenegraph/coreapi/qsgnode.cpp index a35629d874..e22ffa10d2 100644 --- a/src/quick/scenegraph/coreapi/qsgnode.cpp +++ b/src/quick/scenegraph/coreapi/qsgnode.cpp @@ -850,7 +850,7 @@ void QSGBasicGeometryNode::setGeometry(QSGGeometry *geometry) \note All classes with QSG prefix should be used solely on the scene graph's rendering thread. See \l {Scene Graph and Rendering} for more information. - \sa QSGGeometry, QSGMaterial, QSGSimpleMaterial + \sa QSGGeometry, QSGMaterial */ diff --git a/src/quick/scenegraph/scenegraph.pri b/src/quick/scenegraph/scenegraph.pri index 56dda508c6..494ff7276e 100644 --- a/src/quick/scenegraph/scenegraph.pri +++ b/src/quick/scenegraph/scenegraph.pri @@ -55,7 +55,6 @@ HEADERS += \ $$PWD/util/qsgsimpletexturenode.h \ $$PWD/util/qsgtextureprovider.h \ $$PWD/util/qsgflatcolormaterial.h \ - $$PWD/util/qsgsimplematerial.h \ $$PWD/util/qsgtexturematerial.h \ $$PWD/util/qsgtexturematerial_p.h \ $$PWD/util/qsgvertexcolormaterial.h \ @@ -70,7 +69,6 @@ SOURCES += \ $$PWD/util/qsgsimpletexturenode.cpp \ $$PWD/util/qsgtextureprovider.cpp \ $$PWD/util/qsgflatcolormaterial.cpp \ - $$PWD/util/qsgsimplematerial.cpp \ $$PWD/util/qsgtexturematerial.cpp \ $$PWD/util/qsgvertexcolormaterial.cpp \ $$PWD/util/qsgrectanglenode.cpp \ diff --git a/src/quick/scenegraph/util/qsgsimplematerial.cpp b/src/quick/scenegraph/util/qsgsimplematerial.cpp deleted file mode 100644 index 1064caccc7..0000000000 --- a/src/quick/scenegraph/util/qsgsimplematerial.cpp +++ /dev/null @@ -1,254 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/*! - \class QSGSimpleMaterialShader - - \brief The QSGSimpleMaterialShader class provides a convenient way of - building custom OpenGL-based materials for the scene graph. - - \inmodule QtQuick - \ingroup qtquick-scenegraph-materials - - \warning This utility class is only functional when running with the legacy - OpenGL renderer of the Qt Quick scenegraph. Its usage is not recommended in - new application code. - - Where the QSGMaterial and QSGMaterialShader API requires a bit of - boilerplate code to create a functioning material, the - QSGSimpleMaterialShader tries to hide some of this through the use - of templates. - - QSGSimpleMaterialShader::vertexShader() and - QSGSimpleMaterialShader::fragmentShader() are used to specify the - actual shader source code. The names of the vertex attributes - should be listed in the QSGSimpleMaterialShader::attributes() - - QSGSimpleMaterialShader::updateState() is used to push the material - state to the OpenGL shader program. - - The actual OpenGL shader program is accessible through the - QSGSimpleMaterialShader::program() function. - - Each QSGSimpleMaterialShader implementation operates on a unique - state struct. The state struct must be declared using the - \c {QSG_DECLARE_SIMPLE_SHADER} macro. - - Here is a simple example of a custom solid-color: - - \code - struct Color - { - float r, g, b, a; - }; - - class MinimalShader : public QSGSimpleMaterialShader<Color> - { - QSG_DECLARE_SIMPLE_SHADER(MinimalShader, Color) - public: - - const char *vertexShader() const { - return - "attribute highp vec4 vertex; \n" - "uniform highp mat4 qt_Matrix; \n" - "void main() { \n" - " gl_Position = qt_Matrix * vertex; \n" - "}"; - } - - const char *fragmentShader() const { - return - "uniform lowp float qt_Opacity; \n" - "uniform lowp vec4 color; \n" - "void main() { \n" - " gl_FragColor = color * qt_Opacity; \n" - "}"; - } - - QList<QByteArray> attributes() const { - return QList<QByteArray>() << "vertex"; - } - - void updateState(const Color *color, const Color *) { - program()->setUniformValue("color", color->r, color->g, color->b, color->a); - } - - }; - \endcode - - Instances of materials using this shader can be created using the - createMaterial() function which will be defined by the - QSG_DECLARE_SIMPLE_SHADER macro. - - \code - QSGSimpleMaterial<Color> *material = MinimalShader::createMaterial(); - material->state()->r = 1; - material->state()->g = 0; - material->state()->b = 0; - material->state()->a = 1; - - node->setMaterial(material); - \endcode - - The scene graph will often try to find materials that have the - same or at least similar state so that these can be batched - together inside the renderer, which gives better performance. To - specify sortable material states, use - QSG_DECLARE_SIMPLE_COMPARABLE_SHADER instead of - QSG_DECLARE_SIMPLE_SHADER. The state struct must then also define - the function: - - \code - int compare(const Type *other) const; - \endcode - - \warning The QSGSimpleMaterialShader relies on template - instantiation to create a QSGMaterialType which the scene graph - renderer internally uses to identify this shader. For this reason, - the unique QSGSimpleMaterialShader implementation must be - instantiated with a unique C++ type. - - \note All classes with QSG prefix should be used solely on the scene graph's - rendering thread. See \l {Scene Graph and Rendering} for more information. - - \sa {Scene Graph - Simple Material} - */ - -/*! - \macro QSG_DECLARE_SIMPLE_SHADER(Shader, State) - \relates QSGSimpleMaterialShader - - This macro is used to declare a QSGMaterialType and a \c - createMaterial() function for \a Shader with the given \a State. - */ - -/*! - \macro QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State) - \relates QSGSimpleMaterialShader - - This macro is used to declare a QSGMaterialType and a \c - createMaterial() function for \a Shader with the given \a State, - where the \a State class must define a compare function on the - form: - - \code - int compare(const State *other) const; - \endcode -*/ - - -/*! - \fn template <typename State> char const *const *QSGSimpleMaterialShader<State>::attributeNames() const - \internal - */ - -/*! - \fn template <typename State> void QSGSimpleMaterialShader<State>::initialize() - \internal - */ - -/*! - \fn template <typename State> void QSGSimpleMaterialShader<State>::resolveUniforms() - - Reimplement this function to resolve the location of named uniforms - in the shader program. - - This function is called when the material shader is initialized. - */ - -/*! - \fn template <typename State> const char *QSGSimpleMaterialShader<State>::uniformMatrixName() const - - Returns the name for the transform matrix uniform of this item. - The default value is \c qt_Matrix. - */ - -/*! - \fn template <typename State> const char *QSGSimpleMaterialShader<State>::uniformOpacityName() const - - Returns the name for the opacity uniform of this item. - The default value is \c qt_Opacity. - */ - -/*! - \fn template <typename State> void QSGSimpleMaterialShader<State>::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) - \internal - */ - - -/*! - \fn template <typename State> QList<QByteArray> QSGSimpleMaterialShader<State>::attributes() const - - Returns a list of names, declaring the vertex attributes in the - vertex shader. -*/ - -/*! - \fn template <typename State> void QSGSimpleMaterialShader<State>::updateState(const State *newState, const State *oldState) - - Called whenever the state of this shader should be updated from - \a oldState to \a newState, typical for each new set of - geometries being drawn. - - Both the old and the new state are passed in so that the - implementation can compare and minimize the state changes when - applicable. -*/ - -/*! - \class QSGSimpleMaterial - - \inmodule QtQuick - \ingroup qtquick-scenegraph-materials - - \brief The QSGSimpleMaterial class is a template generated class - used to store the state used with a QSGSimpleMateralShader. - - The state of the material is accessible through the template - generated state() function. - - \inmodule QtQuick - - \note All classes with QSG prefix should be used solely on the scene graph's - rendering thread. See \l {Scene Graph and Rendering} for more information. - - \sa QSGSimpleMaterialShader -*/ - - diff --git a/src/quick/scenegraph/util/qsgsimplematerial.h b/src/quick/scenegraph/util/qsgsimplematerial.h deleted file mode 100644 index 79180ca8e2..0000000000 --- a/src/quick/scenegraph/util/qsgsimplematerial.h +++ /dev/null @@ -1,219 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSGSIMPLEMATERIAL_H -#define QSGSIMPLEMATERIAL_H - -#include <QtQuick/qsgmaterial.h> - -QT_BEGIN_NAMESPACE - -template <typename State> -class QSGSimpleMaterialShader : public QSGMaterialShader -{ -public: - void initialize() override { - QSGMaterialShader::initialize(); -#if QT_CONFIG(opengl) - m_id_matrix = program()->uniformLocation(uniformMatrixName()); - if (m_id_matrix < 0) { - qFatal("QSGSimpleMaterialShader does not implement 'uniform highp mat4 %s;' in its vertex shader", - uniformMatrixName()); - } - - const char *opacity = uniformOpacityName(); - if (opacity) { - m_id_opacity = program()->uniformLocation(uniformOpacityName()); - if (m_id_opacity < 0) { - qFatal("QSGSimpleMaterialShader does not implement 'uniform lowp float %s' in its fragment shader", - uniformOpacityName()); - } - } else { - m_id_opacity = -1; - } -#endif - resolveUniforms(); - } - - // ### Qt 6: make both virtual and fix docs - const char *uniformMatrixName() const { return "qt_Matrix"; } - const char *uniformOpacityName() const { return "qt_Opacity"; } - - void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; - - virtual void updateState(const State *newState, const State *oldState) = 0; - - virtual void resolveUniforms() {} - - virtual QList<QByteArray> attributes() const = 0; - - char const *const *attributeNames() const override - { - if (m_attribute_pointers.size()) - return m_attribute_pointers.constData(); - - QList<QByteArray> names = attributes(); - - // Calculate the total number of bytes needed, so we don't get rellocs and - // bad pointers while copying over the individual names. - // Add an extra byte pr entry for the '\0' char. - int total = 0; - for (int i=0; i<names.size(); ++i) - total += names.at(i).size() + 1; - m_attribute_name_data.reserve(total); - - // Copy over the names - for (int i=0; i<names.size(); ++i) { - m_attribute_pointers << m_attribute_name_data.constData() + m_attribute_name_data.size(); - m_attribute_name_data.append(names.at(i)); - m_attribute_name_data.append('\0'); - } - - // Append the "null" terminator - m_attribute_pointers << 0; - - return m_attribute_pointers.constData(); - } - -private: - int m_id_matrix; - int m_id_opacity; - - mutable QByteArray m_attribute_name_data; - mutable QVector<const char *> m_attribute_pointers; -}; - -#define QSG_DECLARE_SIMPLE_SHADER(Shader, State) \ -static QSGMaterialShader *createShader() \ -{ \ - return new Shader; \ -} \ -public: \ -static QSGSimpleMaterial<State> *createMaterial() \ -{ \ - return new QSGSimpleMaterial<State>(createShader); \ -} - - -typedef QSGMaterialShader *(*PtrShaderCreateFunc)(); - - -template <typename State> -class QSGSimpleMaterial : public QSGMaterial -{ -public: -#ifndef Q_CLANG_QDOC - QSGSimpleMaterial(const State &aState, PtrShaderCreateFunc func) - : m_state(aState) - , m_func(func) - { - } - - QSGSimpleMaterial(PtrShaderCreateFunc func) - : m_func(func) - { - } - - QSGMaterialShader *createShader() const override { return m_func(); } - QSGMaterialType *type() const override { return &m_type; } - - State *state() { return &m_state; } - const State *state() const { return &m_state; } -#endif - -private: - static QSGMaterialType m_type; - State m_state; - PtrShaderCreateFunc m_func; -}; - -#define QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State) \ -static QSGMaterialShader *createShader() \ -{ \ - return new Shader; \ -} \ -public: \ -static QSGSimpleMaterialComparableMaterial<State> *createMaterial() \ -{ \ - return new QSGSimpleMaterialComparableMaterial<State>(createShader); \ -} - -template <typename State> -class QSGSimpleMaterialComparableMaterial : public QSGSimpleMaterial<State> -{ - -public: - QSGSimpleMaterialComparableMaterial(const State &state, PtrShaderCreateFunc func) - : QSGSimpleMaterial<State>(state, func) {} - - QSGSimpleMaterialComparableMaterial(PtrShaderCreateFunc func) - : QSGSimpleMaterial<State>(func) {} - - int compare(const QSGMaterial *other) const override { - return QSGSimpleMaterialComparableMaterial<State>::state()->compare(static_cast<const QSGSimpleMaterialComparableMaterial<State> *>(other)->state()); - } -}; - - -template <typename State> -QSGMaterialType QSGSimpleMaterial<State>::m_type; - - -template <typename State> -Q_INLINE_TEMPLATE void QSGSimpleMaterialShader<State>::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) -{ -#if QT_CONFIG(opengl) - if (state.isMatrixDirty()) - program()->setUniformValue(m_id_matrix, state.combinedMatrix()); - if (state.isOpacityDirty() && m_id_opacity >= 0) - program()->setUniformValue(m_id_opacity, state.opacity()); -#else - Q_UNUSED(state) -#endif - State *ns = static_cast<QSGSimpleMaterial<State> *>(newMaterial)->state(); - State *old = nullptr; - if (oldMaterial) - old = static_cast<QSGSimpleMaterial<State> *>(oldMaterial)->state(); - updateState(ns, old); -} - -QT_END_NAMESPACE - - -#endif |