File indexing completed on 2024-05-12 15:42:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "shadowedtexturematerial.h"
0008 
0009 #include <QOpenGLContext>
0010 
0011 QSGMaterialType ShadowedTextureMaterial::staticType;
0012 
0013 ShadowedTextureMaterial::ShadowedTextureMaterial()
0014     : ShadowedRectangleMaterial()
0015 {
0016     setFlag(QSGMaterial::Blending, true);
0017 }
0018 
0019 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0020 QSGMaterialShader *ShadowedTextureMaterial::createShader() const
0021 #else
0022 QSGMaterialShader *ShadowedTextureMaterial::createShader(QSGRendererInterface::RenderMode) const
0023 #endif
0024 {
0025     return new ShadowedTextureShader{shaderType};
0026 }
0027 
0028 QSGMaterialType *ShadowedTextureMaterial::type() const
0029 {
0030     return &staticType;
0031 }
0032 
0033 int ShadowedTextureMaterial::compare(const QSGMaterial *other) const
0034 {
0035     auto material = static_cast<const ShadowedTextureMaterial *>(other);
0036 
0037     auto result = ShadowedRectangleMaterial::compare(other);
0038     if (result == 0) {
0039         if (material->textureSource == textureSource) {
0040             return 0;
0041         } else {
0042             return (material->textureSource < textureSource) ? 1 : -1;
0043         }
0044     }
0045 
0046     return QSGMaterial::compare(other);
0047 }
0048 
0049 ShadowedTextureShader::ShadowedTextureShader(ShadowedRectangleMaterial::ShaderType shaderType)
0050     : ShadowedRectangleShader(shaderType)
0051 {
0052     setShader(shaderType, QStringLiteral("shadowedtexture"));
0053 }
0054 
0055 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0056 void ShadowedTextureShader::initialize()
0057 {
0058     ShadowedRectangleShader::initialize();
0059     program()->setUniformValue("textureSource", 0);
0060 }
0061 
0062 void ShadowedTextureShader::updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0063 {
0064     ShadowedRectangleShader::updateState(state, newMaterial, oldMaterial);
0065 
0066     auto texture = static_cast<ShadowedTextureMaterial *>(newMaterial)->textureSource;
0067     if (texture) {
0068         texture->bind();
0069     }
0070 }
0071 #else
0072 void ShadowedTextureShader::updateSampledImage(QSGMaterialShader::RenderState &state,
0073                                                int binding,
0074                                                QSGTexture **texture,
0075                                                QSGMaterial *newMaterial,
0076                                                QSGMaterial *oldMaterial)
0077 {
0078     Q_UNUSED(state);
0079     Q_UNUSED(oldMaterial);
0080     if (binding == 1) {
0081         *texture = static_cast<ShadowedTextureMaterial *>(newMaterial)->textureSource;
0082     }
0083 }
0084 #endif