QSGDistanceFieldGlyphCache code refactoring.
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / qsgdistancefieldglyphnode_p.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgdistancefieldglyphnode_p_p.h"
43 #include "qsgdistancefieldglyphcache_p.h"
44 #include <private/qsgtexture_p.h>
45 #include <QtOpenGL/qglfunctions.h>
46 #include <qmath.h>
47
48 QT_BEGIN_NAMESPACE
49
50 class QSGDistanceFieldTextMaterialShader : public QSGMaterialShader
51 {
52 public:
53     QSGDistanceFieldTextMaterialShader();
54
55     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
56     virtual char const *const *attributeNames() const;
57
58 protected:
59     virtual void initialize();
60     virtual const char *vertexShader() const;
61     virtual const char *fragmentShader() const;
62
63     void updateAlphaRange(ThresholdFunc thresholdFunc, AntialiasingSpreadFunc spreadFunc);
64
65     float m_fontScale;
66     float m_matrixScale;
67
68     int m_matrix_id;
69     int m_textureScale_id;
70     int m_alphaMin_id;
71     int m_alphaMax_id;
72     int m_color_id;
73 };
74
75 const char *QSGDistanceFieldTextMaterialShader::vertexShader() const {
76     return
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"
82         "void main() {                                  \n"
83         "     sampleCoord = tCoord * textureScale;      \n"
84         "     gl_Position = matrix * vCoord;            \n"
85         "}";
86 }
87
88 const char *QSGDistanceFieldTextMaterialShader::fragmentShader() const {
89     return
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"
95         "void main() {                                                               \n"
96         "    gl_FragColor = color * smoothstep(alphaMin,                             \n"
97         "                                      alphaMax,                             \n"
98         "                                      texture2D(texture, sampleCoord).a);   \n"
99         "}";
100 }
101
102 char const *const *QSGDistanceFieldTextMaterialShader::attributeNames() const {
103     static char const *const attr[] = { "vCoord", "tCoord", 0 };
104     return attr;
105 }
106
107 QSGDistanceFieldTextMaterialShader::QSGDistanceFieldTextMaterialShader()
108     : m_fontScale(1.0)
109     , m_matrixScale(1.0)
110 {
111 }
112
113 void QSGDistanceFieldTextMaterialShader::updateAlphaRange(ThresholdFunc thresholdFunc, AntialiasingSpreadFunc spreadFunc)
114 {
115     float combinedScale = m_fontScale * m_matrixScale;
116     float base = thresholdFunc(combinedScale);
117     float range = spreadFunc(combinedScale);
118
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));
123 }
124
125 void QSGDistanceFieldTextMaterialShader::initialize()
126 {
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");
133 }
134
135 void QSGDistanceFieldTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
136 {
137     Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
138     QSGDistanceFieldTextMaterial *material = static_cast<QSGDistanceFieldTextMaterial *>(newEffect);
139     QSGDistanceFieldTextMaterial *oldMaterial = static_cast<QSGDistanceFieldTextMaterial *>(oldEffect);
140
141     bool updated = material->updateTexture();
142     if (updated && !material->glyphCache()->useWorkaroundBrokenFBOReadback())
143         activate();
144
145     if (oldMaterial == 0
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);
152     }
153
154     bool updateRange = false;
155     if (oldMaterial == 0
156             || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale()) {
157         m_fontScale = material->glyphCache()->fontScale();
158         updateRange = true;
159     }
160     if (state.isMatrixDirty()) {
161         program()->setUniformValue(m_matrix_id, state.combinedMatrix());
162         m_matrixScale = qSqrt(state.modelViewMatrix().determinant());
163         updateRange = true;
164     }
165     if (updateRange) {
166         updateAlphaRange(material->glyphCache()->manager()->thresholdFunc(),
167                          material->glyphCache()->manager()->antialiasingSpreadFunc());
168     }
169
170     Q_ASSERT(material->glyphCache());
171
172     if (updated
173             || oldMaterial == 0
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());
178
179         if (updated) {
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);
186         }
187     }
188 }
189
190 QSGDistanceFieldTextMaterial::QSGDistanceFieldTextMaterial()
191     : m_glyph_cache(0)
192 {
193    setFlag(Blending, true);
194 }
195
196 QSGDistanceFieldTextMaterial::~QSGDistanceFieldTextMaterial()
197 {
198 }
199
200 QSGMaterialType *QSGDistanceFieldTextMaterial::type() const
201 {
202     static QSGMaterialType type;
203     return &type;
204 }
205
206 QSGMaterialShader *QSGDistanceFieldTextMaterial::createShader() const
207 {
208     return new QSGDistanceFieldTextMaterialShader;
209 }
210
211 bool QSGDistanceFieldTextMaterial::updateTexture()
212 {
213     m_glyph_cache->updateCache();
214     QSize glyphCacheSize = m_glyph_cache->textureSize();
215     if (glyphCacheSize != m_size) {
216         m_size = glyphCacheSize;
217
218         return true;
219     } else {
220         return false;
221     }
222 }
223
224 int QSGDistanceFieldTextMaterial::compare(const QSGMaterial *o) const
225 {
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);
232     }
233     QRgb c1 = m_color.rgba();
234     QRgb c2 = other->m_color.rgba();
235     return int(c2 < c1) - int(c1 < c2);
236 }
237
238
239 class DistanceFieldStyledTextMaterialShader : public QSGDistanceFieldTextMaterialShader
240 {
241 public:
242     DistanceFieldStyledTextMaterialShader();
243
244     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
245
246 protected:
247     virtual void initialize();
248     virtual const char *fragmentShader() const = 0;
249
250     int m_styleColor_id;
251 };
252
253 DistanceFieldStyledTextMaterialShader::DistanceFieldStyledTextMaterialShader()
254     : QSGDistanceFieldTextMaterialShader()
255 {
256 }
257
258 void DistanceFieldStyledTextMaterialShader::initialize()
259 {
260     QSGDistanceFieldTextMaterialShader::initialize();
261     m_styleColor_id = program()->uniformLocation("styleColor");
262 }
263
264 void DistanceFieldStyledTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
265 {
266     QSGDistanceFieldTextMaterialShader::updateState(state, newEffect, oldEffect);
267
268     QSGDistanceFieldStyledTextMaterial *material = static_cast<QSGDistanceFieldStyledTextMaterial *>(newEffect);
269     QSGDistanceFieldStyledTextMaterial *oldMaterial = static_cast<QSGDistanceFieldStyledTextMaterial *>(oldEffect);
270
271     if (oldMaterial == 0
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);
278     }
279 }
280
281 QSGDistanceFieldStyledTextMaterial::QSGDistanceFieldStyledTextMaterial()
282     : QSGDistanceFieldTextMaterial()
283 {
284 }
285
286 QSGDistanceFieldStyledTextMaterial::~QSGDistanceFieldStyledTextMaterial()
287 {
288 }
289
290 int QSGDistanceFieldStyledTextMaterial::compare(const QSGMaterial *o) const
291 {
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);
298     }
299     return QSGDistanceFieldTextMaterial::compare(o);
300 }
301
302
303 class DistanceFieldOutlineTextMaterialShader : public DistanceFieldStyledTextMaterialShader
304 {
305 public:
306     DistanceFieldOutlineTextMaterialShader();
307
308     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
309
310 protected:
311     virtual void initialize();
312     virtual const char *fragmentShader() const;
313
314     void updateOutlineAlphaRange(int dfRadius);
315
316     int m_outlineAlphaMax0_id;
317     int m_outlineAlphaMax1_id;
318 };
319
320 const char *DistanceFieldOutlineTextMaterialShader::fragmentShader() const {
321     return
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"
330             "void main() {                                                                        \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"
334             "}";
335 }
336
337 DistanceFieldOutlineTextMaterialShader::DistanceFieldOutlineTextMaterialShader()
338     : DistanceFieldStyledTextMaterialShader()
339 {
340 }
341
342 void DistanceFieldOutlineTextMaterialShader::initialize()
343 {
344     DistanceFieldStyledTextMaterialShader::initialize();
345     m_outlineAlphaMax0_id = program()->uniformLocation("outlineAlphaMax0");
346     m_outlineAlphaMax1_id = program()->uniformLocation("outlineAlphaMax1");
347 }
348
349 void DistanceFieldOutlineTextMaterialShader::updateOutlineAlphaRange(int dfRadius)
350 {
351     qreal outlineLimit = qMax(qreal(0.2), qreal(0.5 - 0.5 / dfRadius / m_fontScale));
352
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));
359 }
360
361 void DistanceFieldOutlineTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
362 {
363     DistanceFieldStyledTextMaterialShader::updateState(state, newEffect, oldEffect);
364
365     QSGDistanceFieldOutlineTextMaterial *material = static_cast<QSGDistanceFieldOutlineTextMaterial *>(newEffect);
366     QSGDistanceFieldOutlineTextMaterial *oldMaterial = static_cast<QSGDistanceFieldOutlineTextMaterial *>(oldEffect);
367
368     if (oldMaterial == 0
369             || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale()
370             || state.isMatrixDirty())
371         updateOutlineAlphaRange(material->glyphCache()->distanceFieldRadius());
372 }
373
374
375 QSGDistanceFieldOutlineTextMaterial::QSGDistanceFieldOutlineTextMaterial()
376     : QSGDistanceFieldStyledTextMaterial()
377 {
378 }
379
380 QSGDistanceFieldOutlineTextMaterial::~QSGDistanceFieldOutlineTextMaterial()
381 {
382 }
383
384 QSGMaterialType *QSGDistanceFieldOutlineTextMaterial::type() const
385 {
386     static QSGMaterialType type;
387     return &type;
388 }
389
390 QSGMaterialShader *QSGDistanceFieldOutlineTextMaterial::createShader() const
391 {
392     return new DistanceFieldOutlineTextMaterialShader;
393 }
394
395
396 class DistanceFieldShiftedStyleTextMaterialShader : public DistanceFieldStyledTextMaterialShader
397 {
398 public:
399     DistanceFieldShiftedStyleTextMaterialShader();
400
401     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
402
403 protected:
404     virtual void initialize();
405     virtual const char *vertexShader() const;
406     virtual const char *fragmentShader() const;
407
408     void updateShift(const QSGDistanceFieldGlyphCache *cache, const QPointF& shift);
409
410     int m_shift_id;
411 };
412
413 DistanceFieldShiftedStyleTextMaterialShader::DistanceFieldShiftedStyleTextMaterialShader()
414     : DistanceFieldStyledTextMaterialShader()
415 {
416 }
417
418 void DistanceFieldShiftedStyleTextMaterialShader::initialize()
419 {
420     DistanceFieldStyledTextMaterialShader::initialize();
421     m_shift_id = program()->uniformLocation("shift");
422 }
423
424 void DistanceFieldShiftedStyleTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
425 {
426     DistanceFieldStyledTextMaterialShader::updateState(state, newEffect, oldEffect);
427
428     QSGDistanceFieldShiftedStyleTextMaterial *material = static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(newEffect);
429     QSGDistanceFieldShiftedStyleTextMaterial *oldMaterial = static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(oldEffect);
430
431     if (oldMaterial == 0
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());
436     }
437 }
438
439 void DistanceFieldShiftedStyleTextMaterialShader::updateShift(const QSGDistanceFieldGlyphCache *cache, const QPointF &shift)
440 {
441     QPointF texel(1.0 / cache->fontScale() * shift.x(),
442                   1.0 / cache->fontScale() * shift.y());
443     program()->setUniformValue(m_shift_id, texel);
444 }
445
446 const char *DistanceFieldShiftedStyleTextMaterialShader::vertexShader() const
447 {
448     return
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"
456             "void main() {                                              \n"
457             "     sampleCoord = tCoord * textureScale;                  \n"
458             "     shiftedSampleCoord = (tCoord - shift) * textureScale; \n"
459             "     gl_Position = matrix * vCoord;                        \n"
460             "}";
461 }
462
463 const char *DistanceFieldShiftedStyleTextMaterialShader::fragmentShader() const {
464     return
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"
472             "void main() {                                                                         \n"
473             "    highp float a = smoothstep(alphaMin, alphaMax, texture2D(texture, sampleCoord).a);\n"
474             "    highp vec4 shifted = styleColor * smoothstep(alphaMin,                            \n"
475             "                                                 alphaMax,                            \n"
476             "                                                 texture2D(texture, shiftedSampleCoord).a); \n"
477             "    gl_FragColor = mix(shifted, color, a);                                            \n"
478             "}";
479 }
480
481 QSGDistanceFieldShiftedStyleTextMaterial::QSGDistanceFieldShiftedStyleTextMaterial()
482     : QSGDistanceFieldStyledTextMaterial()
483 {
484 }
485
486 QSGDistanceFieldShiftedStyleTextMaterial::~QSGDistanceFieldShiftedStyleTextMaterial()
487 {
488 }
489
490 QSGMaterialType *QSGDistanceFieldShiftedStyleTextMaterial::type() const
491 {
492     static QSGMaterialType type;
493     return &type;
494 }
495
496 QSGMaterialShader *QSGDistanceFieldShiftedStyleTextMaterial::createShader() const
497 {
498     return new DistanceFieldShiftedStyleTextMaterialShader;
499 }
500
501
502 class QSGSubPixelDistanceFieldTextMaterialShader : public QSGDistanceFieldTextMaterialShader
503 {
504 public:
505     virtual void initialize();
506     virtual void activate();
507     virtual void deactivate();
508     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
509
510 protected:
511     virtual const char *vertexShader() const;
512     virtual const char *fragmentShader() const;
513
514 private:
515     int m_fontScale_id;
516     int m_vecDelta_id;
517 };
518
519 const char *QSGSubPixelDistanceFieldTextMaterialShader::vertexShader() const {
520     return
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"
532         "void main() {                                                          \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"
548         "}";
549 }
550
551 const char *QSGSubPixelDistanceFieldTextMaterialShader::fragmentShader() const {
552     return
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"
562         "void main() {                                                          \n"
563         "    highp vec4 n;                                                      \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"
569 #if 0
570         // Blurrier, faster.
571         "    n = smoothstep(alphaMin, alphaMax, n);                             \n"
572         "    c = smoothstep(alphaMin, alphaMax, c);                             \n"
573 #else
574         // Sharper, slower.
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"
580 #endif
581         "    gl_FragColor = vec4(0.333 * (n.xyz + n.yzw + c), c) * color.w;     \n"
582         "}";
583 }
584
585 //const char *QSGSubPixelDistanceFieldTextMaterialShader::fragmentShader() const {
586 //    return
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"
604 //        "}";
605 //}
606
607 void QSGSubPixelDistanceFieldTextMaterialShader::initialize()
608 {
609     QSGDistanceFieldTextMaterialShader::initialize();
610     m_fontScale_id = program()->uniformLocation("fontScale");
611     m_vecDelta_id = program()->uniformLocation("vecDelta");
612 }
613
614 void QSGSubPixelDistanceFieldTextMaterialShader::activate()
615 {
616     QSGDistanceFieldTextMaterialShader::activate();
617     glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR);
618 }
619
620 void QSGSubPixelDistanceFieldTextMaterialShader::deactivate()
621 {
622     QSGDistanceFieldTextMaterialShader::deactivate();
623     glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
624 }
625
626 void QSGSubPixelDistanceFieldTextMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
627 {
628     Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
629     QSGDistanceFieldTextMaterial *material = static_cast<QSGDistanceFieldTextMaterial *>(newEffect);
630     QSGDistanceFieldTextMaterial *oldMaterial = static_cast<QSGDistanceFieldTextMaterial *>(oldEffect);
631
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);
635     }
636
637     if (oldMaterial == 0 || material->glyphCache()->fontScale() != oldMaterial->glyphCache()->fontScale())
638         program()->setUniformValue(m_fontScale_id, GLfloat(material->glyphCache()->fontScale()));
639
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));
644     }
645
646     QSGDistanceFieldTextMaterialShader::updateState(state, newEffect, oldEffect);
647 }
648
649 QSGMaterialType *QSGSubPixelDistanceFieldTextMaterial::type() const
650 {
651     static QSGMaterialType type;
652     return &type;
653 }
654
655 QSGMaterialShader *QSGSubPixelDistanceFieldTextMaterial::createShader() const
656 {
657     return new QSGSubPixelDistanceFieldTextMaterialShader;
658 }
659
660
661 QT_END_NAMESPACE