1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qsgdistancefieldglyphnode_p_p.h"
43 #include "qsgdistancefieldglyphcache_p.h"
44 #include <private/qsgtexture_p.h>
45 #include <QtOpenGL/qglfunctions.h>
50 class QSGDistanceFieldTextMaterialShader : public QSGMaterialShader
53 QSGDistanceFieldTextMaterialShader();
55 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
56 virtual char const *const *attributeNames() const;
59 virtual void initialize();
60 virtual const char *vertexShader() const;
61 virtual const char *fragmentShader() const;
63 void updateAlphaRange(ThresholdFunc thresholdFunc, AntialiasingSpreadFunc spreadFunc);
69 int m_textureScale_id;
75 const char *QSGDistanceFieldTextMaterialShader::vertexShader() const {
77 "uniform highp mat4 matrix; \n"
78 "uniform highp vec2 textureScale; \n"
79 "attribute highp vec4 vCoord; \n"
80 "attribute highp vec2 tCoord; \n"
81 "varying highp vec2 sampleCoord; \n"
83 " sampleCoord = tCoord * textureScale; \n"
84 " gl_Position = matrix * vCoord; \n"
88 const char *QSGDistanceFieldTextMaterialShader::fragmentShader() const {
90 "varying highp vec2 sampleCoord; \n"
91 "uniform sampler2D texture; \n"
92 "uniform lowp vec4 color; \n"
93 "uniform highp float alphaMin; \n"
94 "uniform highp float alphaMax; \n"
96 " gl_FragColor = color * smoothstep(alphaMin, \n"
98 " texture2D(texture, sampleCoord).a); \n"
102 char const *const *QSGDistanceFieldTextMaterialShader::attributeNames() const {
103 static char const *const attr[] = { "vCoord", "tCoord", 0 };
107 QSGDistanceFieldTextMaterialShader::QSGDistanceFieldTextMaterialShader()
113 void QSGDistanceFieldTextMaterialShader::updateAlphaRange(ThresholdFunc thresholdFunc, AntialiasingSpreadFunc spreadFunc)
115 float combinedScale = m_fontScale * m_matrixScale;
116 float base = thresholdFunc(combinedScale);
117 float range = spreadFunc(combinedScale);
119 float alphaMin = qMax(0.0f, base - range);
120 float alphaMax = qMin(base + range, 1.0f);
121 program()->setUniformValue(m_alphaMin_id, GLfloat(alphaMin));
122 program()->setUniformValue(m_alphaMax_id, GLfloat(alphaMax));
125 void QSGDistanceFieldTextMaterialShader::initialize()
127 QSGMaterialShader::initialize();
128 m_matrix_id = program()->uniformLocation("matrix");
129 m_textureScale_id = program()->uniformLocation("textureScale");
130 m_color_id = program()->uniformLocation("color");
131 m_alphaMin_id = program()->uniformLocation("alphaMin");
132 m_alphaMax_id = program()->uniformLocation("alphaMax");
135 void QSGDistanceFieldTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
137 Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
138 QSGDistanceFieldTextMaterial *material = static_cast<QSGDistanceFieldTextMaterial *>(newEffect);
139 QSGDistanceFieldTextMaterial *oldMaterial = static_cast<QSGDistanceFieldTextMaterial *>(oldEffect);
141 bool updated = material->updateTexture();
142 if (updated && !material->glyphCache()->useWorkaroundBrokenFBOReadback())
146 || material->color() != oldMaterial->color()
147 || state.isOpacityDirty()) {
148 QVector4D color(material->color().redF(), material->color().greenF(),
149 material->color().blueF(), material->color().alphaF());
150 color *= state.opacity();
151 program()->setUniformValue(m_color_id, color);
154 bool updateRange = false;
156 || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale()) {
157 m_fontScale = material->glyphCache()->fontScale();
160 if (state.isMatrixDirty()) {
161 program()->setUniformValue(m_matrix_id, state.combinedMatrix());
162 m_matrixScale = qSqrt(state.modelViewMatrix().determinant());
166 updateAlphaRange(material->glyphCache()->manager()->thresholdFunc(),
167 material->glyphCache()->manager()->antialiasingSpreadFunc());
170 Q_ASSERT(material->glyphCache());
174 || oldMaterial->glyphCache()->texture() != material->glyphCache()->texture()) {
175 program()->setUniformValue(m_textureScale_id, QVector2D(1.0 / material->glyphCache()->textureSize().width(),
176 1.0 / material->glyphCache()->textureSize().height()));
177 glBindTexture(GL_TEXTURE_2D, material->glyphCache()->texture());
180 // Set the mag/min filters to be linear. We only need to do this when the texture
181 // has been recreated.
182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
183 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
185 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
190 QSGDistanceFieldTextMaterial::QSGDistanceFieldTextMaterial()
193 setFlag(Blending, true);
196 QSGDistanceFieldTextMaterial::~QSGDistanceFieldTextMaterial()
200 QSGMaterialType *QSGDistanceFieldTextMaterial::type() const
202 static QSGMaterialType type;
206 QSGMaterialShader *QSGDistanceFieldTextMaterial::createShader() const
208 return new QSGDistanceFieldTextMaterialShader;
211 bool QSGDistanceFieldTextMaterial::updateTexture()
213 m_glyph_cache->updateCache();
214 QSize glyphCacheSize = m_glyph_cache->textureSize();
215 if (glyphCacheSize != m_size) {
216 m_size = glyphCacheSize;
224 int QSGDistanceFieldTextMaterial::compare(const QSGMaterial *o) const
226 Q_ASSERT(o && type() == o->type());
227 const QSGDistanceFieldTextMaterial *other = static_cast<const QSGDistanceFieldTextMaterial *>(o);
228 if (m_glyph_cache->fontScale() != other->m_glyph_cache->fontScale()) {
229 qreal s1 = m_glyph_cache->fontScale();
230 qreal s2 = other->m_glyph_cache->fontScale();
231 return int(s2 < s1) - int(s1 < s2);
233 QRgb c1 = m_color.rgba();
234 QRgb c2 = other->m_color.rgba();
235 return int(c2 < c1) - int(c1 < c2);
239 class DistanceFieldStyledTextMaterialShader : public QSGDistanceFieldTextMaterialShader
242 DistanceFieldStyledTextMaterialShader();
244 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
247 virtual void initialize();
248 virtual const char *fragmentShader() const = 0;
253 DistanceFieldStyledTextMaterialShader::DistanceFieldStyledTextMaterialShader()
254 : QSGDistanceFieldTextMaterialShader()
258 void DistanceFieldStyledTextMaterialShader::initialize()
260 QSGDistanceFieldTextMaterialShader::initialize();
261 m_styleColor_id = program()->uniformLocation("styleColor");
264 void DistanceFieldStyledTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
266 QSGDistanceFieldTextMaterialShader::updateState(state, newEffect, oldEffect);
268 QSGDistanceFieldStyledTextMaterial *material = static_cast<QSGDistanceFieldStyledTextMaterial *>(newEffect);
269 QSGDistanceFieldStyledTextMaterial *oldMaterial = static_cast<QSGDistanceFieldStyledTextMaterial *>(oldEffect);
272 || material->styleColor() != oldMaterial->styleColor()
273 || (state.isOpacityDirty())) {
274 QVector4D color(material->styleColor().redF(), material->styleColor().greenF(),
275 material->styleColor().blueF(), material->styleColor().alphaF());
276 color *= state.opacity();
277 program()->setUniformValue(m_styleColor_id, color);
281 QSGDistanceFieldStyledTextMaterial::QSGDistanceFieldStyledTextMaterial()
282 : QSGDistanceFieldTextMaterial()
286 QSGDistanceFieldStyledTextMaterial::~QSGDistanceFieldStyledTextMaterial()
290 int QSGDistanceFieldStyledTextMaterial::compare(const QSGMaterial *o) const
292 Q_ASSERT(o && type() == o->type());
293 const QSGDistanceFieldStyledTextMaterial *other = static_cast<const QSGDistanceFieldStyledTextMaterial *>(o);
294 if (m_styleColor != other->m_styleColor) {
295 QRgb c1 = m_styleColor.rgba();
296 QRgb c2 = other->m_styleColor.rgba();
297 return int(c2 < c1) - int(c1 < c2);
299 return QSGDistanceFieldTextMaterial::compare(o);
303 class DistanceFieldOutlineTextMaterialShader : public DistanceFieldStyledTextMaterialShader
306 DistanceFieldOutlineTextMaterialShader();
308 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
311 virtual void initialize();
312 virtual const char *fragmentShader() const;
314 void updateOutlineAlphaRange(int dfRadius);
316 int m_outlineAlphaMax0_id;
317 int m_outlineAlphaMax1_id;
320 const char *DistanceFieldOutlineTextMaterialShader::fragmentShader() const {
322 "varying highp vec2 sampleCoord; \n"
323 "uniform sampler2D texture; \n"
324 "uniform lowp vec4 color; \n"
325 "uniform lowp vec4 styleColor; \n"
326 "uniform highp float alphaMin; \n"
327 "uniform highp float alphaMax; \n"
328 "uniform highp float outlineAlphaMax0; \n"
329 "uniform highp float outlineAlphaMax1; \n"
331 " mediump float d = texture2D(texture, sampleCoord).a; \n"
332 " gl_FragColor = mix(styleColor, color, smoothstep(alphaMin, alphaMax, d)) \n"
333 " * smoothstep(outlineAlphaMax0, outlineAlphaMax1, d); \n"
337 DistanceFieldOutlineTextMaterialShader::DistanceFieldOutlineTextMaterialShader()
338 : DistanceFieldStyledTextMaterialShader()
342 void DistanceFieldOutlineTextMaterialShader::initialize()
344 DistanceFieldStyledTextMaterialShader::initialize();
345 m_outlineAlphaMax0_id = program()->uniformLocation("outlineAlphaMax0");
346 m_outlineAlphaMax1_id = program()->uniformLocation("outlineAlphaMax1");
349 void DistanceFieldOutlineTextMaterialShader::updateOutlineAlphaRange(int dfRadius)
351 qreal outlineLimit = qMax(qreal(0.2), qreal(0.5 - 0.5 / dfRadius / m_fontScale));
353 qreal combinedScale = m_fontScale * m_matrixScale;
354 qreal alphaMin = qMax(0.0, 0.5 - 0.07 / combinedScale);
355 qreal styleAlphaMin0 = qMax(0.0, outlineLimit - 0.07 / combinedScale);
356 qreal styleAlphaMin1 = qMin(qreal(outlineLimit + 0.07 / combinedScale), alphaMin);
357 program()->setUniformValue(m_outlineAlphaMax0_id, GLfloat(styleAlphaMin0));
358 program()->setUniformValue(m_outlineAlphaMax1_id, GLfloat(styleAlphaMin1));
361 void DistanceFieldOutlineTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
363 DistanceFieldStyledTextMaterialShader::updateState(state, newEffect, oldEffect);
365 QSGDistanceFieldOutlineTextMaterial *material = static_cast<QSGDistanceFieldOutlineTextMaterial *>(newEffect);
366 QSGDistanceFieldOutlineTextMaterial *oldMaterial = static_cast<QSGDistanceFieldOutlineTextMaterial *>(oldEffect);
369 || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale()
370 || state.isMatrixDirty())
371 updateOutlineAlphaRange(material->glyphCache()->distanceFieldRadius());
375 QSGDistanceFieldOutlineTextMaterial::QSGDistanceFieldOutlineTextMaterial()
376 : QSGDistanceFieldStyledTextMaterial()
380 QSGDistanceFieldOutlineTextMaterial::~QSGDistanceFieldOutlineTextMaterial()
384 QSGMaterialType *QSGDistanceFieldOutlineTextMaterial::type() const
386 static QSGMaterialType type;
390 QSGMaterialShader *QSGDistanceFieldOutlineTextMaterial::createShader() const
392 return new DistanceFieldOutlineTextMaterialShader;
396 class DistanceFieldShiftedStyleTextMaterialShader : public DistanceFieldStyledTextMaterialShader
399 DistanceFieldShiftedStyleTextMaterialShader();
401 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
404 virtual void initialize();
405 virtual const char *vertexShader() const;
406 virtual const char *fragmentShader() const;
408 void updateShift(const QSGDistanceFieldGlyphCache *cache, const QPointF& shift);
413 DistanceFieldShiftedStyleTextMaterialShader::DistanceFieldShiftedStyleTextMaterialShader()
414 : DistanceFieldStyledTextMaterialShader()
418 void DistanceFieldShiftedStyleTextMaterialShader::initialize()
420 DistanceFieldStyledTextMaterialShader::initialize();
421 m_shift_id = program()->uniformLocation("shift");
424 void DistanceFieldShiftedStyleTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
426 DistanceFieldStyledTextMaterialShader::updateState(state, newEffect, oldEffect);
428 QSGDistanceFieldShiftedStyleTextMaterial *material = static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(newEffect);
429 QSGDistanceFieldShiftedStyleTextMaterial *oldMaterial = static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(oldEffect);
432 || oldMaterial->glyphCache()->fontScale() != material->glyphCache()->fontScale()
433 || oldMaterial->shift() != material->shift()
434 || oldMaterial->glyphCache()->textureSize() != material->glyphCache()->textureSize()) {
435 updateShift(material->glyphCache(), material->shift());
439 void DistanceFieldShiftedStyleTextMaterialShader::updateShift(const QSGDistanceFieldGlyphCache *cache, const QPointF &shift)
441 QPointF texel(1.0 / cache->fontScale() * shift.x(),
442 1.0 / cache->fontScale() * shift.y());
443 program()->setUniformValue(m_shift_id, texel);
446 const char *DistanceFieldShiftedStyleTextMaterialShader::vertexShader() const
449 "uniform highp mat4 matrix; \n"
450 "uniform highp vec2 textureScale; \n"
451 "attribute highp vec4 vCoord; \n"
452 "attribute highp vec2 tCoord; \n"
453 "uniform highp vec2 shift; \n"
454 "varying highp vec2 sampleCoord; \n"
455 "varying highp vec2 shiftedSampleCoord; \n"
457 " sampleCoord = tCoord * textureScale; \n"
458 " shiftedSampleCoord = (tCoord - shift) * textureScale; \n"
459 " gl_Position = matrix * vCoord; \n"
463 const char *DistanceFieldShiftedStyleTextMaterialShader::fragmentShader() const {
465 "varying highp vec2 sampleCoord; \n"
466 "varying highp vec2 shiftedSampleCoord; \n"
467 "uniform sampler2D texture; \n"
468 "uniform lowp vec4 color; \n"
469 "uniform lowp vec4 styleColor; \n"
470 "uniform highp float alphaMin; \n"
471 "uniform highp float alphaMax; \n"
473 " highp float a = smoothstep(alphaMin, alphaMax, texture2D(texture, sampleCoord).a);\n"
474 " highp vec4 shifted = styleColor * smoothstep(alphaMin, \n"
476 " texture2D(texture, shiftedSampleCoord).a); \n"
477 " gl_FragColor = mix(shifted, color, a); \n"
481 QSGDistanceFieldShiftedStyleTextMaterial::QSGDistanceFieldShiftedStyleTextMaterial()
482 : QSGDistanceFieldStyledTextMaterial()
486 QSGDistanceFieldShiftedStyleTextMaterial::~QSGDistanceFieldShiftedStyleTextMaterial()
490 QSGMaterialType *QSGDistanceFieldShiftedStyleTextMaterial::type() const
492 static QSGMaterialType type;
496 QSGMaterialShader *QSGDistanceFieldShiftedStyleTextMaterial::createShader() const
498 return new DistanceFieldShiftedStyleTextMaterialShader;
502 class QSGSubPixelDistanceFieldTextMaterialShader : public QSGDistanceFieldTextMaterialShader
505 virtual void initialize();
506 virtual void activate();
507 virtual void deactivate();
508 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
511 virtual const char *vertexShader() const;
512 virtual const char *fragmentShader() const;
519 const char *QSGSubPixelDistanceFieldTextMaterialShader::vertexShader() const {
521 "uniform highp mat4 matrix; \n"
522 "uniform highp vec2 textureScale; \n"
523 "uniform highp float fontScale; \n"
524 "uniform highp vec4 vecDelta; \n"
525 "attribute highp vec4 vCoord; \n"
526 "attribute highp vec2 tCoord; \n"
527 "varying highp vec2 sampleCoord; \n"
528 "varying highp vec3 sampleFarLeft; \n"
529 "varying highp vec3 sampleNearLeft; \n"
530 "varying highp vec3 sampleNearRight; \n"
531 "varying highp vec3 sampleFarRight; \n"
533 " sampleCoord = tCoord * textureScale; \n"
534 " gl_Position = matrix * vCoord; \n"
535 // Calculate neighbour pixel position in item space.
536 " highp vec3 wDelta = gl_Position.w * vecDelta.xyw; \n"
537 " highp vec3 farLeft = vCoord.xyw - 0.667 * wDelta; \n"
538 " highp vec3 nearLeft = vCoord.xyw - 0.333 * wDelta; \n"
539 " highp vec3 nearRight = vCoord.xyw + 0.333 * wDelta; \n"
540 " highp vec3 farRight = vCoord.xyw + 0.667 * wDelta; \n"
541 // Calculate neighbour texture coordinate.
542 " highp vec2 scale = textureScale / fontScale; \n"
543 " highp vec2 base = sampleCoord - scale * vCoord.xy; \n"
544 " sampleFarLeft = vec3(base * farLeft.z + scale * farLeft.xy, farLeft.z); \n"
545 " sampleNearLeft = vec3(base * nearLeft.z + scale * nearLeft.xy, nearLeft.z); \n"
546 " sampleNearRight = vec3(base * nearRight.z + scale * nearRight.xy, nearRight.z); \n"
547 " sampleFarRight = vec3(base * farRight.z + scale * farRight.xy, farRight.z); \n"
551 const char *QSGSubPixelDistanceFieldTextMaterialShader::fragmentShader() const {
553 "varying highp vec2 sampleCoord; \n"
554 "varying highp vec3 sampleFarLeft; \n"
555 "varying highp vec3 sampleNearLeft; \n"
556 "varying highp vec3 sampleNearRight; \n"
557 "varying highp vec3 sampleFarRight; \n"
558 "uniform sampler2D texture; \n"
559 "uniform lowp vec4 color; \n"
560 "uniform highp float alphaMin; \n"
561 "uniform highp float alphaMax; \n"
564 " n.x = texture2DProj(texture, sampleFarLeft).a; \n"
565 " n.y = texture2DProj(texture, sampleNearLeft).a; \n"
566 " highp float c = texture2D(texture, sampleCoord).a; \n"
567 " n.z = texture2DProj(texture, sampleNearRight).a; \n"
568 " n.w = texture2DProj(texture, sampleFarRight).a; \n"
571 " n = smoothstep(alphaMin, alphaMax, n); \n"
572 " c = smoothstep(alphaMin, alphaMax, c); \n"
575 " highp vec2 d = min(abs(n.yw - n.xz) * 2., 0.67); \n"
576 " highp vec2 lo = mix(vec2(alphaMin), vec2(0.5), d); \n"
577 " highp vec2 hi = mix(vec2(alphaMax), vec2(0.5), d); \n"
578 " n = smoothstep(lo.xxyy, hi.xxyy, n); \n"
579 " c = smoothstep(lo.x + lo.y, hi.x + hi.y, 2. * c); \n"
581 " gl_FragColor = vec4(0.333 * (n.xyz + n.yzw + c), c) * color.w; \n"
585 //const char *QSGSubPixelDistanceFieldTextMaterialShader::fragmentShader() const {
587 // "#extension GL_OES_standard_derivatives: enable \n"
588 // "varying highp vec2 sampleCoord; \n"
589 // "uniform sampler2D texture; \n"
590 // "uniform lowp vec4 color; \n"
591 // "uniform highp float alphaMin; \n"
592 // "uniform highp float alphaMax; \n"
593 // "void main() { \n"
594 // " highp vec2 delta = dFdx(sampleCoord); \n"
595 // " highp vec4 n; \n"
596 // " n.x = texture2D(texture, sampleCoord - 0.667 * delta).a; \n"
597 // " n.y = texture2D(texture, sampleCoord - 0.333 * delta).a; \n"
598 // " highp float c = texture2D(texture, sampleCoord).a; \n"
599 // " n.z = texture2D(texture, sampleCoord + 0.333 * delta).a; \n"
600 // " n.w = texture2D(texture, sampleCoord + 0.667 * delta).a; \n"
601 // " n = smoothstep(alphaMin, alphaMax, n); \n"
602 // " c = smoothstep(alphaMin, alphaMax, c); \n"
603 // " gl_FragColor = vec4(0.333 * (n.xyz + n.yzw + c), c) * color.w; \n"
607 void QSGSubPixelDistanceFieldTextMaterialShader::initialize()
609 QSGDistanceFieldTextMaterialShader::initialize();
610 m_fontScale_id = program()->uniformLocation("fontScale");
611 m_vecDelta_id = program()->uniformLocation("vecDelta");
614 void QSGSubPixelDistanceFieldTextMaterialShader::activate()
616 QSGDistanceFieldTextMaterialShader::activate();
617 glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR);
620 void QSGSubPixelDistanceFieldTextMaterialShader::deactivate()
622 QSGDistanceFieldTextMaterialShader::deactivate();
623 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
626 void QSGSubPixelDistanceFieldTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
628 Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
629 QSGDistanceFieldTextMaterial *material = static_cast<QSGDistanceFieldTextMaterial *>(newEffect);
630 QSGDistanceFieldTextMaterial *oldMaterial = static_cast<QSGDistanceFieldTextMaterial *>(oldEffect);
632 if (oldMaterial == 0 || material->color() != oldMaterial->color()) {
633 QColor c = material->color();
634 state.context()->functions()->glBlendColor(c.redF(), c.greenF(), c.blueF(), 1.0f);
637 if (oldMaterial == 0 || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale())
638 program()->setUniformValue(m_fontScale_id, GLfloat(material->glyphCache()->fontScale()));
640 if (oldMaterial == 0 || state.isMatrixDirty()) {
641 int viewportWidth = state.viewportRect().width();
642 QMatrix4x4 mat = state.combinedMatrix().inverted();
643 program()->setUniformValue(m_vecDelta_id, mat.column(0) * (qreal(2) / viewportWidth));
646 QSGDistanceFieldTextMaterialShader::updateState(state, newEffect, oldEffect);
649 QSGMaterialType *QSGSubPixelDistanceFieldTextMaterial::type() const
651 static QSGMaterialType type;
655 QSGMaterialShader *QSGSubPixelDistanceFieldTextMaterial::createShader() const
657 return new QSGSubPixelDistanceFieldTextMaterialShader;