blob: 2998a5473358333f737af6680b31e0a874048713 [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrDistanceFieldTextureEffect.h"
9#include "gl/GrGLEffect.h"
10#include "gl/GrGLSL.h"
11#include "gl/GrGLTexture.h"
12#include "gl/GrGLVertexEffect.h"
13#include "GrTBackendEffectFactory.h"
14#include "GrTexture.h"
15
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +000016// The distance field is constructed as unsigned char values, so that the zero value is at 128,
17// and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero threshold is 128/255.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000018#define MULTIPLIER "7.96875"
jvanverth@google.comd830d132013-11-11 20:54:09 +000019#define THRESHOLD "0.50196078431"
20
21class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect {
22public:
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +000023 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000024 const GrDrawEffect& drawEffect)
25 : INHERITED (factory)
26 , fTextureSize(SkSize::Make(-1.f,-1.f)) {}
jvanverth@google.comd830d132013-11-11 20:54:09 +000027
28 virtual void emitCode(GrGLFullShaderBuilder* builder,
29 const GrDrawEffect& drawEffect,
30 EffectKey key,
31 const char* outputColor,
32 const char* inputColor,
33 const TransformedCoordsArray&,
34 const TextureSamplerArray& samplers) SK_OVERRIDE {
35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs());
36
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000037 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverth@google.comd830d132013-11-11 20:54:09 +000038
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000039 SkString fsCoordName;
40 const char* vsCoordName;
41 const char* fsCoordNamePtr;
42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr);
43 fsCoordName = fsCoordNamePtr;
44
45 const char* attrName0 =
jvanverth@google.comd830d132013-11-11 20:54:09 +000046 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000047 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0);
48
49 const char* textureSizeUniName = NULL;
50 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
51 kVec2f_GrSLType, "TextureSize",
52 &textureSizeUniName);
jvanverth@google.comd830d132013-11-11 20:54:09 +000053
54 builder->fsCodeAppend("\tvec4 texColor = ");
55 builder->fsAppendTextureLookup(samplers[0],
56 fsCoordName.c_str(),
57 kVec2f_GrSLType);
58 builder->fsCodeAppend(";\n");
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000059 builder->fsCodeAppend("\tfloat distance = "MULTIPLIER"*(texColor.r - "THRESHOLD");\n");
60
61 // we adjust for the effect of the transformation on the distance by using
62 // the length of the gradient of the texture coordinates. We use st coordinates
63 // to ensure we're mapping 1:1 from texel space to pixel space.
64 builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), textureSizeUniName);
65 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n");
66 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n");
67 builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n");
68 builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jdy.x,\n");
69 builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jdy.y);\n");
70
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 // this gives us a smooth step across approximately one fragment
jvanverth@google.comd830d132013-11-11 20:54:09 +000072 // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2)
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000073 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n");
74 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n");
jvanverth@google.comd830d132013-11-11 20:54:09 +000075
76 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
77 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str());
78 }
79
80 virtual void setData(const GrGLUniformManager& uman,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000081 const GrDrawEffect& drawEffect) SK_OVERRIDE {
82 SkASSERT(fTextureSizeUni.isValid());
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +000083 const GrDistanceFieldTextureEffect& distanceFieldEffect =
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000084 drawEffect.castEffect<GrDistanceFieldTextureEffect>();
85 if (distanceFieldEffect.getSize().width() != fTextureSize.width() ||
86 distanceFieldEffect.getSize().height() != fTextureSize.height()) {
87 fTextureSize = distanceFieldEffect.getSize();
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +000088 uman.set2f(fTextureSizeUni,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000089 distanceFieldEffect.getSize().width(),
90 distanceFieldEffect.getSize().height());
91 }
92 }
jvanverth@google.comd830d132013-11-11 20:54:09 +000093
94private:
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000095 GrGLUniformManager::UniformHandle fTextureSizeUni;
96 SkSize fTextureSize;
97
jvanverth@google.comd830d132013-11-11 20:54:09 +000098 typedef GrGLVertexEffect INHERITED;
99};
100
101///////////////////////////////////////////////////////////////////////////////
102
103GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000104 const GrTextureParams& params,
105 const SkISize& size)
106 : fTextureAccess(texture, params)
107 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height()))) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000108 this->addTextureAccess(&fTextureAccess);
109 this->addVertexAttrib(kVec2f_GrSLType);
110}
111
112bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
113 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureEffect>(other);
114 return fTextureAccess == cte.fTextureAccess;
115}
116
117void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color,
118 uint32_t* validFlags) const {
119 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
120 GrPixelConfigIsOpaque(this->texture(0)->config())) {
121 *validFlags = kA_GrColorComponentFlag;
122 } else {
123 *validFlags = 0;
124 }
125}
126
127const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const {
128 return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance();
129}
130
131///////////////////////////////////////////////////////////////////////////////
132
133GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect);
134
135GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random,
136 GrContext*,
137 const GrDrawTargetCaps&,
138 GrTexture* textures[]) {
139 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
140 GrEffectUnitTest::kAlphaTextureIdx;
141 static const SkShader::TileMode kTileModes[] = {
142 SkShader::kClamp_TileMode,
143 SkShader::kRepeat_TileMode,
144 SkShader::kMirror_TileMode,
145 };
146 SkShader::TileMode tileModes[] = {
147 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
148 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
149 };
150 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
151 GrTextureParams::kNone_FilterMode);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000152 SkISize size = SkISize::Make(1024, 2048);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000153
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000154 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000155}