aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2013-10-26 18:48:56 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-31 12:54:28 +0100
commit426f6aa672b94d8324321bc6c6d4f1a358eebedc (patch)
tree8af3e5b0aa2fdcdb7655672669f5ae277151cc3d /src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
parent34c85bb56c92316a6ce1c79d25f9653fec14791c (diff)
Refactor shaders into seprate GLSL source files
The default implementation of QSGShaderMaterial::vertexShader() and fragmentShader() now loads the GLSL source from a list of source files that can be specified via the setShaderSourceFile() or setShaderSourceFiles() functions. Multiple shader source files for each shader stage are supported. Each source file will be read in the order specified and concatenated together before being compiled. The other places where Qt Quick 2 loads shader source code have been adapted to use the new QSGShaderSourceBuilder, which is also used internally by QSGMaterial. This puts Qt Quick 2 into a better state ready to support OpenGL core profile and to load different shaders based upon OpenGL version, profile, GPU vendor, platform, etc. Change-Id: I1a66213c2ce788413168eb48c7bc5317e61988a2 Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'src/quick/scenegraph/qsgdefaultglyphnode_p.cpp')
-rw-r--r--src/quick/scenegraph/qsgdefaultglyphnode_p.cpp159
1 files changed, 19 insertions, 140 deletions
diff --git a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
index bf92bf8b39..35c4d1c506 100644
--- a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
+++ b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
@@ -40,6 +40,7 @@
****************************************************************************/
#include "qsgdefaultglyphnode_p_p.h"
+#include <private/qsgmaterialshader_p.h>
#include <qopenglshaderprogram.h>
@@ -75,8 +76,6 @@ public:
protected:
virtual void initialize();
- virtual const char *vertexShader() const;
- virtual const char *fragmentShader() const;
int m_matrix_id;
int m_color_id;
@@ -85,30 +84,6 @@ protected:
QFontEngineGlyphCache::Type m_cacheType;
};
-const char *QSGTextMaskShader::vertexShader() const {
- return
- "uniform highp mat4 matrix; \n"
- "uniform highp vec2 textureScale; \n"
- "attribute highp vec4 vCoord; \n"
- "attribute highp vec2 tCoord; \n"
- "varying highp vec2 sampleCoord; \n"
- "void main() { \n"
- " sampleCoord = tCoord * textureScale; \n"
- " gl_Position = matrix * vCoord; \n"
- "}";
-}
-
-const char *QSGTextMaskShader::fragmentShader() const {
- return
- "varying highp vec2 sampleCoord; \n"
- "uniform sampler2D _qt_texture; \n"
- "uniform lowp vec4 color; \n"
- "void main() { \n"
- " lowp vec4 glyph = texture2D(_qt_texture, sampleCoord); \n"
- " gl_FragColor = vec4(glyph.rgb * color.a, glyph.a); \n"
- "}";
-}
-
char const *const *QSGTextMaskShader::attributeNames() const
{
static char const *const attr[] = { "vCoord", "tCoord", 0 };
@@ -116,8 +91,11 @@ char const *const *QSGTextMaskShader::attributeNames() const
}
QSGTextMaskShader::QSGTextMaskShader(QFontEngineGlyphCache::Type cacheType)
- : m_cacheType(cacheType)
+ : QSGMaterialShader(*new QSGMaterialShaderPrivate),
+ m_cacheType(cacheType)
{
+ setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/scenegraph/shaders/textmask.vert"));
+ setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/scenegraph/shaders/textmask.frag"));
}
static inline qreal fontSmoothingGamma()
@@ -186,22 +164,13 @@ class QSG8BitTextMaskShader : public QSGTextMaskShader
public:
QSG8BitTextMaskShader(QFontEngineGlyphCache::Type cacheType)
: QSGTextMaskShader(cacheType)
- {}
+ {
+ setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/scenegraph/shaders/8bittextmask.frag"));
+ }
virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
- virtual const char *fragmentShader() const;
};
-const char *QSG8BitTextMaskShader::fragmentShader() const {
- return
- "varying highp vec2 sampleCoord; \n"
- "uniform lowp sampler2D texture; \n"
- "uniform lowp vec4 color; \n"
- "void main() { \n"
- " gl_FragColor = color * texture2D(texture, sampleCoord).a; \n"
- "}";
-}
-
void QSG8BitTextMaskShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
{
QSGTextMaskShader::updateState(state, newEffect, oldEffect);
@@ -220,28 +189,18 @@ public:
QSG24BitTextMaskShader(QFontEngineGlyphCache::Type cacheType)
: QSGTextMaskShader(cacheType)
, m_useSRGB(false)
- {}
+ {
+ setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/scenegraph/shaders/24bittextmask.frag"));
+ }
virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
virtual void initialize();
void activate();
void deactivate();
- virtual const char *fragmentShader() const;
uint m_useSRGB : 1;
};
-const char *QSG24BitTextMaskShader::fragmentShader() const {
- return
- "varying highp vec2 sampleCoord; \n"
- "uniform lowp sampler2D texture; \n"
- "uniform lowp float color; // just the alpha, really... \n"
- "void main() { \n"
- " lowp vec4 glyph = texture2D(texture, sampleCoord); \n"
- " gl_FragColor = vec4(glyph.rgb * color, glyph.a); \n"
- "}";
-}
-
void QSG24BitTextMaskShader::initialize()
{
QSGTextMaskShader::initialize();
@@ -302,14 +261,15 @@ class QSGStyledTextShader : public QSG8BitTextMaskShader
public:
QSGStyledTextShader(QFontEngineGlyphCache::Type cacheType)
: QSG8BitTextMaskShader(cacheType)
- { }
+ {
+ setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/scenegraph/shaders/styledtext.vert"));
+ setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/scenegraph/shaders/styledtext.frag"));
+ }
virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
private:
virtual void initialize();
- virtual const char *vertexShader() const;
- virtual const char *fragmentShader() const;
int m_shift_id;
int m_styleColor_id;
@@ -367,98 +327,17 @@ void QSGStyledTextShader::updateState(const RenderState &state,
program()->setUniformValue(m_matrix_id, state.combinedMatrix());
}
-const char *QSGStyledTextShader::vertexShader() const
-{
- return
- "uniform highp mat4 matrix; \n"
- "uniform highp vec2 textureScale; \n"
- "uniform highp vec2 shift; \n"
- "attribute highp vec4 vCoord; \n"
- "attribute highp vec2 tCoord; \n"
- "varying highp vec2 sampleCoord; \n"
- "varying highp vec2 shiftedSampleCoord; \n"
- "void main() { \n"
- " sampleCoord = tCoord * textureScale; \n"
- " shiftedSampleCoord = (tCoord - shift) * textureScale; \n"
- " gl_Position = matrix * vCoord; \n"
- "}";
-}
-
-const char *QSGStyledTextShader::fragmentShader() const
-{
- return
- "varying highp vec2 sampleCoord; \n"
- "varying highp vec2 shiftedSampleCoord; \n"
- "uniform sampler2D _qt_texture; \n"
- "uniform lowp vec4 color; \n"
- "uniform lowp vec4 styleColor; \n"
- "void main() { \n"
- " lowp float glyph = texture2D(_qt_texture, sampleCoord).a; \n"
- " lowp float style = clamp(texture2D(_qt_texture, shiftedSampleCoord).a - glyph, \n"
- " 0.0, 1.0); \n"
- " gl_FragColor = style * styleColor + glyph * color; \n"
- "}";
-}
-
-
class QSGOutlinedTextShader : public QSGStyledTextShader
{
public:
QSGOutlinedTextShader(QFontEngineGlyphCache::Type cacheType)
: QSGStyledTextShader(cacheType)
- { }
-
-private:
- const char *vertexShader() const;
- const char *fragmentShader() const;
+ {
+ setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/scenegraph/shaders/outlinedtext.vert"));
+ setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/scenegraph/shaders/outlinedtext.frag"));
+ }
};
-const char *QSGOutlinedTextShader::vertexShader() const
-{
- return
- "uniform highp mat4 matrix; \n"
- "uniform highp vec2 textureScale; \n"
- "uniform highp vec2 shift; \n"
- "attribute highp vec4 vCoord; \n"
- "attribute highp vec2 tCoord; \n"
- "varying highp vec2 sampleCoord; \n"
- "varying highp vec2 sCoordUp; \n"
- "varying highp vec2 sCoordDown; \n"
- "varying highp vec2 sCoordLeft; \n"
- "varying highp vec2 sCoordRight; \n"
- "void main() { \n"
- " sampleCoord = tCoord * textureScale; \n"
- " sCoordUp = (tCoord - vec2(0.0, -1.0)) * textureScale; \n"
- " sCoordDown = (tCoord - vec2(0.0, 1.0)) * textureScale; \n"
- " sCoordLeft = (tCoord - vec2(-1.0, 0.0)) * textureScale; \n"
- " sCoordRight = (tCoord - vec2(1.0, 0.0)) * textureScale; \n"
- " gl_Position = matrix * vCoord; \n"
- "}";
-}
-
-const char *QSGOutlinedTextShader::fragmentShader() const
-{
- return
- "varying highp vec2 sampleCoord; \n"
- "varying highp vec2 sCoordUp; \n"
- "varying highp vec2 sCoordDown; \n"
- "varying highp vec2 sCoordLeft; \n"
- "varying highp vec2 sCoordRight; \n"
- "uniform sampler2D _qt_texture; \n"
- "uniform lowp vec4 color; \n"
- "uniform lowp vec4 styleColor; \n"
- "void main() { \n"
- "lowp float glyph = texture2D(_qt_texture, sampleCoord).a; \n"
- " lowp float outline = clamp(clamp(texture2D(_qt_texture, sCoordUp).a + \n"
- " texture2D(_qt_texture, sCoordDown).a + \n"
- " texture2D(_qt_texture, sCoordLeft).a + \n"
- " texture2D(_qt_texture, sCoordRight).a, \n"
- " 0.0, 1.0) - glyph, \n"
- " 0.0, 1.0); \n"
- " gl_FragColor = outline * styleColor + glyph * color; \n"
- "}";
-}
-
QSGTextMaskMaterial::QSGTextMaskMaterial(const QRawFont &font, int cacheType)
: m_texture(0)
, m_glyphCache(0)