OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrDistanceFieldTextureEffect.h" | 8 #include "GrDistanceFieldTextureEffect.h" |
9 #include "GrFontAtlasSizes.h" | 9 #include "GrFontAtlasSizes.h" |
10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 GrGLGPBuilder* pb = args.fPB; | 44 GrGLGPBuilder* pb = args.fPB; |
45 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 45 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
46 SkAssertResult(fsBuilder->enableFeature( | 46 SkAssertResult(fsBuilder->enableFeature( |
47 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 47 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
48 | 48 |
49 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 49 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
50 | 50 |
51 // emit attributes | 51 // emit attributes |
52 vsBuilder->emitAttributes(dfTexEffect); | 52 vsBuilder->emitAttributes(dfTexEffect); |
53 | 53 |
| 54 GrGLVertToFrag st(kVec2f_GrSLType); |
| 55 args.fPB->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); |
| 56 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 57 |
| 58 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 59 args.fPB->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); |
| 60 // this is only used with text, so our texture bounds always match the g
lyph atlas |
| 61 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", " |
| 62 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), |
| 63 dfTexEffect.inTextureCoords()->fName); |
54 #ifdef SK_GAMMA_APPLY_TO_A8 | 64 #ifdef SK_GAMMA_APPLY_TO_A8 |
55 // adjust based on gamma | 65 // adjust based on gamma |
56 const char* distanceAdjustUniName = NULL; | 66 const char* distanceAdjustUniName = NULL; |
57 // width, height, 1/(3*width) | 67 // width, height, 1/(3*width) |
58 fDistanceAdjustUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_
Visibility, | 68 fDistanceAdjustUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_
Visibility, |
59 kFloat_GrSLType, kDefault_GrSLPrecision, | 69 kFloat_GrSLType, kDefault_GrSLPrecision, |
60 "DistanceAdjust", &distanceAdjustUniName); | 70 "DistanceAdjust", &distanceAdjustUniName); |
61 #endif | 71 #endif |
62 | 72 |
63 // Setup pass through color | 73 // Setup pass through color |
64 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, | 74 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, |
65 dfTexEffect.inColor(), &fColorUniform); | 75 dfTexEffect.inColor(), &fColorUniform); |
66 | 76 |
67 // Setup position | 77 // Setup position |
68 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 78 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
69 | 79 |
70 // emit transforms | 80 // emit transforms |
71 const SkMatrix& localMatrix = dfTexEffect.localMatrix(); | |
72 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 81 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
73 localMatrix, args.fTransformsIn, args.fTransformsOu
t); | 82 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
74 | 83 |
75 // add varyings | |
76 GrGLVertToFrag recipScale(kFloat_GrSLType); | |
77 GrGLVertToFrag st(kVec2f_GrSLType); | |
78 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_Distan
ceFieldEffectFlag); | |
79 const char* viewMatrixName = this->uViewM(); | |
80 // view matrix name is NULL if identity matrix | |
81 bool useInverseScale = !localMatrix.isIdentity() && viewMatrixName; | |
82 if (isSimilarity && useInverseScale) { | |
83 args.fPB->addVarying("RecipScale", &recipScale, kHigh_GrSLPrecision)
; | |
84 vsBuilder->codeAppendf("vec2 tx = vec2(%s[0][0], %s[1][0]);", | |
85 viewMatrixName, viewMatrixName); | |
86 vsBuilder->codeAppend("float tx2 = dot(tx, tx);"); | |
87 vsBuilder->codeAppendf("%s = inversesqrt(tx2);", recipScale.vsOut())
; | |
88 } else { | |
89 args.fPB->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); | |
90 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTexture
Coords()->fName); | |
91 } | |
92 | |
93 GrGLVertToFrag uv(kVec2f_GrSLType); | |
94 args.fPB->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); | |
95 // this is only used with text, so our texture bounds always match the g
lyph atlas | |
96 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", " | |
97 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), | |
98 dfTexEffect.inTextureCoords()->fName); | |
99 | |
100 | |
101 // Use highp to work around aliasing issues | 84 // Use highp to work around aliasing issues |
102 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 85 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
103 pb->ctxInfo().stand
ard())); | 86 pb->ctxInfo().stand
ard())); |
104 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); | 87 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
105 | 88 |
106 fsBuilder->codeAppend("\tfloat texColor = "); | 89 fsBuilder->codeAppend("\tfloat texColor = "); |
107 fsBuilder->appendTextureLookup(args.fSamplers[0], | 90 fsBuilder->appendTextureLookup(args.fSamplers[0], |
108 "uv", | 91 "uv", |
109 kVec2f_GrSLType); | 92 kVec2f_GrSLType); |
110 fsBuilder->codeAppend(".r;\n"); | 93 fsBuilder->codeAppend(".r;\n"); |
111 fsBuilder->codeAppend("\tfloat distance = " | 94 fsBuilder->codeAppend("\tfloat distance = " |
112 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFie
ldThreshold ");"); | 95 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFie
ldThreshold ");"); |
113 #ifdef SK_GAMMA_APPLY_TO_A8 | 96 #ifdef SK_GAMMA_APPLY_TO_A8 |
114 // adjust width based on gamma | 97 // adjust width based on gamma |
115 fsBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); | 98 fsBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); |
116 #endif | 99 #endif |
117 | 100 |
118 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 101 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
119 pb->ctxInfo().stand
ard())); | 102 pb->ctxInfo().stand
ard())); |
| 103 fsBuilder->codeAppendf("vec2 st = %s;", st.fsIn()); |
120 fsBuilder->codeAppend("float afwidth;"); | 104 fsBuilder->codeAppend("float afwidth;"); |
121 if (isSimilarity) { | 105 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
122 // For uniform scale, we adjust for the effect of the transformation
on the distance | 106 // For uniform scale, we adjust for the effect of the transformation
on the distance |
123 // either by using the inverse scale in the view matrix, or (if ther
e is no view matrix) | |
124 // by using the length of the gradient of the texture coordinates. W
e use st coordinates | 107 // by using the length of the gradient of the texture coordinates. W
e use st coordinates |
125 // with the latter to ensure we're mapping 1:1 from texel space to p
ixel space. | 108 // to ensure we're mapping 1:1 from texel space to pixel space. |
126 | 109 |
127 // this gives us a smooth step across approximately one fragment | 110 // this gives us a smooth step across approximately one fragment |
128 if (useInverseScale) { | 111 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); |
129 fsBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
"*%s);", | |
130 recipScale.fsIn()); | |
131 } else { | |
132 fsBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
"*dFdx(%s.x));", | |
133 st.fsIn()); | |
134 } | |
135 } else { | 112 } else { |
136 fsBuilder->codeAppendf("vec2 st = %s;", st.fsIn()); | |
137 // For general transforms, to determine the amount of correction we
multiply a unit | 113 // For general transforms, to determine the amount of correction we
multiply a unit |
138 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords | 114 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords |
139 // (which is the inverse transform for this fragment) and take the l
ength of the result. | 115 // (which is the inverse transform for this fragment) and take the l
ength of the result. |
140 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); | 116 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); |
141 // the length of the gradient may be 0, so we need to check for this | 117 // the length of the gradient may be 0, so we need to check for this |
142 // this also compensates for the Adreno, which likes to drop tiles o
n division by 0 | 118 // this also compensates for the Adreno, which likes to drop tiles o
n division by 0 |
143 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); | 119 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
144 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); | 120 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
145 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); | 121 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
146 fsBuilder->codeAppend("} else {"); | 122 fsBuilder->codeAppend("} else {"); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 static inline void GenKey(const GrGeometryProcessor& gp, | 163 static inline void GenKey(const GrGeometryProcessor& gp, |
188 const GrBatchTracker& bt, | 164 const GrBatchTracker& bt, |
189 const GrGLCaps&, | 165 const GrGLCaps&, |
190 GrProcessorKeyBuilder* b) { | 166 GrProcessorKeyBuilder* b) { |
191 const GrDistanceFieldTextureEffect& dfTexEffect = gp.cast<GrDistanceFiel
dTextureEffect>(); | 167 const GrDistanceFieldTextureEffect& dfTexEffect = gp.cast<GrDistanceFiel
dTextureEffect>(); |
192 const DistanceFieldBatchTracker& local = bt.cast<DistanceFieldBatchTrack
er>(); | 168 const DistanceFieldBatchTracker& local = bt.cast<DistanceFieldBatchTrack
er>(); |
193 uint32_t key = dfTexEffect.getFlags(); | 169 uint32_t key = dfTexEffect.getFlags(); |
194 key |= local.fInputColorType << 16; | 170 key |= local.fInputColorType << 16; |
195 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 171 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
196 key |= ComputePosKey(gp.viewMatrix()) << 25; | 172 key |= ComputePosKey(gp.viewMatrix()) << 25; |
197 key |= (!gp.viewMatrix().isIdentity() && !gp.localMatrix().isIdentity())
? 0x1 << 27 : 0x0; | |
198 b->add32(key); | 173 b->add32(key); |
199 } | 174 } |
200 | 175 |
201 private: | 176 private: |
202 GrColor fColor; | 177 GrColor fColor; |
203 UniformHandle fColorUniform; | 178 UniformHandle fColorUniform; |
204 #ifdef SK_GAMMA_APPLY_TO_A8 | 179 #ifdef SK_GAMMA_APPLY_TO_A8 |
205 float fDistanceAdjust; | 180 float fDistanceAdjust; |
206 UniformHandle fDistanceAdjustUni; | 181 UniformHandle fDistanceAdjustUni; |
207 #endif | 182 #endif |
208 | 183 |
209 typedef GrGLGeometryProcessor INHERITED; | 184 typedef GrGLGeometryProcessor INHERITED; |
210 }; | 185 }; |
211 | 186 |
212 /////////////////////////////////////////////////////////////////////////////// | 187 /////////////////////////////////////////////////////////////////////////////// |
213 | 188 |
214 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrColor color, | 189 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrColor color, |
215 const SkMatrix& viewM
atrix, | 190 const SkMatrix& viewM
atrix, |
216 const SkMatrix& local
Matrix, | |
217 GrTexture* texture, | 191 GrTexture* texture, |
218 const GrTextureParams
& params, | 192 const GrTextureParams
& params, |
219 #ifdef SK_GAMMA_APPLY_TO_A8 | 193 #ifdef SK_GAMMA_APPLY_TO_A8 |
220 float distanceAdjust, | 194 float distanceAdjust, |
221 #endif | 195 #endif |
222 uint32_t flags, bool
opaqueVertexColors) | 196 uint32_t flags, bool
opaqueVertexColors) |
223 : INHERITED(color, viewMatrix, localMatrix, opaqueVertexColors) | 197 : INHERITED(color, viewMatrix, SkMatrix::I(), opaqueVertexColors) |
224 , fTextureAccess(texture, params) | 198 , fTextureAccess(texture, params) |
225 #ifdef SK_GAMMA_APPLY_TO_A8 | 199 #ifdef SK_GAMMA_APPLY_TO_A8 |
226 , fDistanceAdjust(distanceAdjust) | 200 , fDistanceAdjust(distanceAdjust) |
227 #endif | 201 #endif |
228 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) | 202 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
229 , fInColor(NULL) { | 203 , fInColor(NULL) { |
230 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); | 204 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
231 this->initClassID<GrDistanceFieldTextureEffect>(); | 205 this->initClassID<GrDistanceFieldTextureEffect>(); |
232 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 206 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
233 if (flags & kColorAttr_DistanceFieldEffectFlag) { | 207 if (flags & kColorAttr_DistanceFieldEffectFlag) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 }; | 274 }; |
301 SkShader::TileMode tileModes[] = { | 275 SkShader::TileMode tileModes[] = { |
302 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 276 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
303 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 277 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
304 }; | 278 }; |
305 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : | 279 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : |
306 GrTextureParams::kNon
e_FilterMode); | 280 GrTextureParams::kNon
e_FilterMode); |
307 | 281 |
308 return GrDistanceFieldTextureEffect::Create(GrRandomColor(random), | 282 return GrDistanceFieldTextureEffect::Create(GrRandomColor(random), |
309 GrProcessorUnitTest::TestMatrix(
random), | 283 GrProcessorUnitTest::TestMatrix(
random), |
310 GrProcessorUnitTest::TestMatrix(
random), | |
311 textures[texIdx], params, | 284 textures[texIdx], params, |
312 #ifdef SK_GAMMA_APPLY_TO_A8 | 285 #ifdef SK_GAMMA_APPLY_TO_A8 |
313 random->nextF(), | 286 random->nextF(), |
314 #endif | 287 #endif |
315 random->nextBool() ? | 288 random->nextBool() ? |
316 kSimilarity_DistanceFieldEff
ectFlag : 0, | 289 kSimilarity_DistanceFieldEff
ectFlag : 0, |
317 random->nextBool()); | 290 random->nextBool()); |
318 } | 291 } |
319 | 292 |
320 /////////////////////////////////////////////////////////////////////////////// | 293 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 557 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
585 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); | 558 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); |
586 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); | 559 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); |
587 GrGLGPBuilder* pb = args.fPB; | 560 GrGLGPBuilder* pb = args.fPB; |
588 | 561 |
589 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 562 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
590 | 563 |
591 // emit attributes | 564 // emit attributes |
592 vsBuilder->emitAttributes(dfTexEffect); | 565 vsBuilder->emitAttributes(dfTexEffect); |
593 | 566 |
| 567 GrGLVertToFrag st(kVec2f_GrSLType); |
| 568 args.fPB->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); |
| 569 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 570 |
| 571 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 572 args.fPB->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); |
| 573 // this is only used with text, so our texture bounds always match the g
lyph atlas |
| 574 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", " |
| 575 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), |
| 576 dfTexEffect.inTextureCoords()->fName); |
| 577 |
594 // setup pass through color | 578 // setup pass through color |
595 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, | 579 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, |
596 &fColorUniform); | 580 &fColorUniform); |
597 | 581 |
598 // Setup position | 582 // Setup position |
599 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 583 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
600 | 584 |
601 // emit transforms | 585 // emit transforms |
602 const SkMatrix& localMatrix = dfTexEffect.localMatrix(); | |
603 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 586 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
604 localMatrix, args.fTransformsIn, args.fTransformsOu
t); | 587 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
605 | 588 |
606 // set up varyings | |
607 bool isUniformScale = SkToBool(dfTexEffect.getFlags() & kUniformScale_Di
stanceFieldEffectMask); | |
608 GrGLVertToFrag recipScale(kFloat_GrSLType); | |
609 GrGLVertToFrag st(kVec2f_GrSLType); | |
610 const char* viewMatrixName = this->uViewM(); | |
611 // view matrix name is NULL if identity matrix | |
612 bool useInverseScale = !localMatrix.isIdentity() && viewMatrixName; | |
613 if (isUniformScale && useInverseScale) { | |
614 args.fPB->addVarying("RecipScale", &recipScale, kHigh_GrSLPrecision)
; | |
615 vsBuilder->codeAppendf("vec2 tx = vec2(%s[0][0], %s[1][0]);", | |
616 viewMatrixName, viewMatrixName); | |
617 vsBuilder->codeAppend("float tx2 = dot(tx, tx);"); | |
618 vsBuilder->codeAppendf("%s = inversesqrt(tx2);", recipScale.vsOut())
; | |
619 } else { | |
620 args.fPB->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); | |
621 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTexture
Coords()->fName); | |
622 } | |
623 | |
624 GrGLVertToFrag uv(kVec2f_GrSLType); | |
625 args.fPB->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); | |
626 // this is only used with text, so our texture bounds always match the g
lyph atlas | |
627 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_A8_RECIP_WIDTH ", " | |
628 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), | |
629 dfTexEffect.inTextureCoords()->fName); | |
630 | |
631 // add frag shader code | |
632 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 589 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
633 | 590 |
634 SkAssertResult(fsBuilder->enableFeature( | 591 SkAssertResult(fsBuilder->enableFeature( |
635 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 592 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
636 | 593 |
637 // create LCD offset adjusted by inverse of transform | 594 // create LCD offset adjusted by inverse of transform |
638 // Use highp to work around aliasing issues | 595 // Use highp to work around aliasing issues |
639 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 596 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
640 pb->ctxInfo().stand
ard())); | 597 pb->ctxInfo().stand
ard())); |
641 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); | 598 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
642 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 599 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
643 pb->ctxInfo().stand
ard())); | 600 pb->ctxInfo().stand
ard())); |
| 601 fsBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); |
| 602 bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_Distance
FieldEffectMask); |
| 603 |
644 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { | 604 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
645 fsBuilder->codeAppend("float delta = -" GR_FONT_ATLAS_LCD_DELTA ";\n
"); | 605 fsBuilder->codeAppend("float delta = -" GR_FONT_ATLAS_LCD_DELTA ";\n
"); |
646 } else { | 606 } else { |
647 fsBuilder->codeAppend("float delta = " GR_FONT_ATLAS_LCD_DELTA ";\n"
); | 607 fsBuilder->codeAppend("float delta = " GR_FONT_ATLAS_LCD_DELTA ";\n"
); |
648 } | 608 } |
649 if (isUniformScale) { | 609 if (isUniformScale) { |
650 if (useInverseScale) { | 610 fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); |
651 fsBuilder->codeAppendf("float dx = %s;", recipScale.fsIn()); | 611 fsBuilder->codeAppend("\tvec2 offset = vec2(dx*delta, 0.0);\n"); |
652 } else { | |
653 fsBuilder->codeAppendf("float dx = dFdx(%s.x);", st.fsIn()); | |
654 } | |
655 fsBuilder->codeAppend("vec2 offset = vec2(dx*delta, 0.0);"); | |
656 } else { | 612 } else { |
657 fsBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); | 613 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
658 | 614 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
659 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); | 615 fsBuilder->codeAppend("\tvec2 offset = delta*Jdx;\n"); |
660 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); | |
661 fsBuilder->codeAppend("vec2 offset = delta*Jdx;"); | |
662 } | 616 } |
663 | 617 |
664 // green is distance to uv center | 618 // green is distance to uv center |
665 fsBuilder->codeAppend("\tvec4 texColor = "); | 619 fsBuilder->codeAppend("\tvec4 texColor = "); |
666 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; | 620 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; |
667 fsBuilder->codeAppend(";\n"); | 621 fsBuilder->codeAppend(";\n"); |
668 fsBuilder->codeAppend("\tvec3 distance;\n"); | 622 fsBuilder->codeAppend("\tvec3 distance;\n"); |
669 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); | 623 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); |
670 // red is distance to left offset | 624 // red is distance to left offset |
671 fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); | 625 fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 const GrGLCaps&, | 714 const GrGLCaps&, |
761 GrProcessorKeyBuilder* b) { | 715 GrProcessorKeyBuilder* b) { |
762 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 716 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
763 gp.cast<GrDistanceFieldLCDTextureEffect>(); | 717 gp.cast<GrDistanceFieldLCDTextureEffect>(); |
764 | 718 |
765 const DistanceFieldLCDBatchTracker& local = bt.cast<DistanceFieldLCDBatc
hTracker>(); | 719 const DistanceFieldLCDBatchTracker& local = bt.cast<DistanceFieldLCDBatc
hTracker>(); |
766 uint32_t key = dfTexEffect.getFlags(); | 720 uint32_t key = dfTexEffect.getFlags(); |
767 key |= local.fInputColorType << 16; | 721 key |= local.fInputColorType << 16; |
768 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 722 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
769 key |= ComputePosKey(gp.viewMatrix()) << 25; | 723 key |= ComputePosKey(gp.viewMatrix()) << 25; |
770 key |= (!gp.viewMatrix().isIdentity() && !gp.localMatrix().isIdentity())
? 0x1 << 27 : 0x0; | |
771 b->add32(key); | 724 b->add32(key); |
772 } | 725 } |
773 | 726 |
774 private: | 727 private: |
775 GrColor fColor; | 728 GrColor fColor; |
776 UniformHandle fColorUniform; | 729 UniformHandle fColorUniform; |
777 GrDistanceFieldLCDTextureEffect::DistanceAdjust fDistanceAdjust; | 730 GrDistanceFieldLCDTextureEffect::DistanceAdjust fDistanceAdjust; |
778 UniformHandle fDistanceAdjustUni; | 731 UniformHandle fDistanceAdjustUni; |
779 | 732 |
780 typedef GrGLGeometryProcessor INHERITED; | 733 typedef GrGLGeometryProcessor INHERITED; |
781 }; | 734 }; |
782 | 735 |
783 /////////////////////////////////////////////////////////////////////////////// | 736 /////////////////////////////////////////////////////////////////////////////// |
784 | 737 |
785 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( | 738 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( |
786 GrColor color, const SkMatrix&
viewMatrix, | 739 GrColor color, const SkMatrix&
viewMatrix, |
787 const SkMatrix& localMatrix, | |
788 GrTexture* texture, const GrTe
xtureParams& params, | 740 GrTexture* texture, const GrTe
xtureParams& params, |
789 DistanceAdjust distanceAdjust, | 741 DistanceAdjust distanceAdjust, |
790 uint32_t flags) | 742 uint32_t flags) |
791 : INHERITED(color, viewMatrix, localMatrix) | 743 : INHERITED(color, viewMatrix, SkMatrix::I()) |
792 , fTextureAccess(texture, params) | 744 , fTextureAccess(texture, params) |
793 , fDistanceAdjust(distanceAdjust) | 745 , fDistanceAdjust(distanceAdjust) |
794 , fFlags(flags & kLCD_DistanceFieldEffectMask){ | 746 , fFlags(flags & kLCD_DistanceFieldEffectMask){ |
795 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); | 747 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); |
796 this->initClassID<GrDistanceFieldLCDTextureEffect>(); | 748 this->initClassID<GrDistanceFieldLCDTextureEffect>(); |
797 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 749 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
798 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 750 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
799 kVec2s_GrVertexAttribT
ype)); | 751 kVec2s_GrVertexAttribT
ype)); |
800 this->addTextureAccess(&fTextureAccess); | 752 this->addTextureAccess(&fTextureAccess); |
801 } | 753 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
862 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 814 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
863 }; | 815 }; |
864 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : | 816 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : |
865 GrTextureParams::kNone_FilterMode); | 817 GrTextureParams::kNone_FilterMode); |
866 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f }; | 818 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f }; |
867 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 819 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
868 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 820 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
869 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 821 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
870 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), | 822 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), |
871 GrProcessorUnitTest::TestMatr
ix(random), | 823 GrProcessorUnitTest::TestMatr
ix(random), |
872 GrProcessorUnitTest::TestMatr
ix(random), | |
873 textures[texIdx], params, | 824 textures[texIdx], params, |
874 wa, | 825 wa, |
875 flags); | 826 flags); |
876 } | 827 } |
OLD | NEW |