diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2021-01-29 07:34:41 +0100 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2021-02-23 13:18:08 +0100 |
commit | c0052816a990c4214b4e2b6f7461d966a1bf39a4 (patch) | |
tree | 5c7436093bb303e949bf714839973f7f0d1007d7 /src | |
parent | 79208e0b3c4730ff6a6f2856dfd509d35a291589 (diff) |
Scene3DSGMaterialShader: restore 5.15 code
Change-Id: If54fb129c5d044fd7f2544c86020b395a223f3b4
Reviewed-by: Mike Krus <mike.krus@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp | 87 | ||||
-rw-r--r-- | src/quick3d/imports/scene3d/scene3dsgmaterialshader_p.h | 11 |
2 files changed, 97 insertions, 1 deletions
diff --git a/src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp b/src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp index 4f5e12208..e0f84b58a 100644 --- a/src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp +++ b/src/quick3d/imports/scene3d/scene3dsgmaterialshader.cpp @@ -158,7 +158,92 @@ void Scene3DSGMaterialShader::updateSampledImage(QSGMaterialShader::RenderState const char * const *Qt3DRender::Scene3DSGMaterialShader::attributeNames() const { - return nullptr; + static char const *const attr[] = { "qt_VertexPosition", "qt_VertexTexCoord", 0 }; + return attr; +} + +const char *Scene3DSGMaterialShader::vertexShader() const +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx->format().version() >= qMakePair(3, 2) && ctx->format().profile() == QSurfaceFormat::CoreProfile) { + return "" + "#version 150 core \n" + "uniform mat4 qt_Matrix; \n" + "in vec4 qt_VertexPosition; \n" + "in vec2 qt_VertexTexCoord; \n" + "out vec2 qt_TexCoord; \n" + "void main() { \n" + " qt_TexCoord = qt_VertexTexCoord; \n" + " gl_Position = qt_Matrix * qt_VertexPosition; \n" + "}"; + } else { + return "" + "uniform highp mat4 qt_Matrix; \n" + "attribute highp vec4 qt_VertexPosition; \n" + "attribute highp vec2 qt_VertexTexCoord; \n" + "varying highp vec2 qt_TexCoord; \n" + "void main() { \n" + " qt_TexCoord = qt_VertexTexCoord; \n" + " gl_Position = qt_Matrix * qt_VertexPosition; \n" + "}"; + } +} +const char *Scene3DSGMaterialShader::fragmentShader() const +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx->format().version() >= qMakePair(3, 2) && ctx->format().profile() == QSurfaceFormat::CoreProfile) { + return "" + "#version 150 core \n" + "uniform sampler2D source; \n" + "uniform float qt_Opacity; \n" + "in vec2 qt_TexCoord; \n" + "out vec4 fragColor; \n" + "void main() { \n" + " vec4 p = texture(source, qt_TexCoord); \n" + " float a = qt_Opacity * p.a; \n" + " fragColor = vec4(p.rgb * a, a); \n" + "}"; + } else { + return "" + "uniform highp sampler2D source; \n" + "uniform highp float qt_Opacity; \n" + "varying highp vec2 qt_TexCoord; \n" + "void main() { \n" + " highp vec4 p = texture2D(source, qt_TexCoord); \n" + " highp float a = qt_Opacity * p.a; \n" + " gl_FragColor = vec4(p.rgb * a, a); \n" + "}"; + } +} +void Scene3DSGMaterialShader::initialize() +{ + m_matrixId = program()->uniformLocation("qt_Matrix"); + m_opacityId = program()->uniformLocation("qt_Opacity"); +} +void Scene3DSGMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) +{ + Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type()); + Scene3DSGMaterial *tx = static_cast<Scene3DSGMaterial *>(newEffect); + Scene3DSGMaterial *oldTx = static_cast<Scene3DSGMaterial *>(oldEffect); + QSGTexture *t = tx->texture(); + bool npotSupported = const_cast<QOpenGLContext *>(state.context()) + ->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextureRepeat); + if (!npotSupported) { + QSize size = t->textureSize(); + const bool isNpot = !isPowerOfTwo(size.width()) || !isPowerOfTwo(size.height()); + if (isNpot) { + t->setHorizontalWrapMode(QSGTexture::ClampToEdge); + t->setVerticalWrapMode(QSGTexture::ClampToEdge); + } + } + if (oldTx == 0 || oldTx->texture()->textureId() != t->textureId()) + t->bind(); + else + t->updateBindOptions(); + if (state.isMatrixDirty()) + program()->setUniformValue(m_matrixId, state.combinedMatrix()); + if (state.isOpacityDirty()) + program()->setUniformValue(m_opacityId, state.opacity()); } #endif diff --git a/src/quick3d/imports/scene3d/scene3dsgmaterialshader_p.h b/src/quick3d/imports/scene3d/scene3dsgmaterialshader_p.h index 942ffba39..b4ac7383e 100644 --- a/src/quick3d/imports/scene3d/scene3dsgmaterialshader_p.h +++ b/src/quick3d/imports/scene3d/scene3dsgmaterialshader_p.h @@ -69,7 +69,18 @@ protected: bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) final; void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) final; #else +public: char const *const *attributeNames() const final; + void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) final; + +protected: + const char *vertexShader() const final; + const char *fragmentShader() const final; + void initialize() final; + +private: + int m_matrixId = -1; + int m_opacityId = -1; #endif }; |