| 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 "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
| 10 #include "GrTexture.h" | 11 #include "GrTexture.h" |
| 11 #include "SkDistanceFieldGen.h" | 12 #include "SkDistanceFieldGen.h" |
| 12 #include "gl/GrGLProcessor.h" | 13 #include "gl/GrGLProcessor.h" |
| 13 #include "gl/GrGLSL.h" | 14 #include "gl/GrGLSL.h" |
| 14 #include "gl/GrGLTexture.h" | 15 #include "gl/GrGLTexture.h" |
| 15 #include "gl/GrGLGeometryProcessor.h" | 16 #include "gl/GrGLGeometryProcessor.h" |
| 16 #include "gl/builders/GrGLProgramBuilder.h" | 17 #include "gl/builders/GrGLProgramBuilder.h" |
| 17 | 18 |
| 18 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 | 19 // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/
2 |
| 19 #define SK_DistanceFieldAAFactor "0.7071" | 20 #define SK_DistanceFieldAAFactor "0.7071" |
| 20 | 21 |
| 21 struct DistanceFieldBatchTracker { | 22 struct DistanceFieldBatchTracker { |
| 22 GrGPInput fInputColorType; | 23 GrGPInput fInputColorType; |
| 23 GrColor fColor; | 24 GrColor fColor; |
| 24 bool fUsesLocalCoords; | 25 bool fUsesLocalCoords; |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { | 28 class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { |
| 28 public: | 29 public: |
| 29 GrGLDistanceFieldTextureEffect(const GrGeometryProcessor&, | 30 GrGLDistanceFieldTextureEffect(const GrGeometryProcessor&, |
| 30 const GrBatchTracker&) | 31 const GrBatchTracker&) |
| 31 : fColor(GrColor_ILLEGAL) | 32 : fColor(GrColor_ILLEGAL) |
| 32 , fTextureSize(SkISize::Make(-1,-1)) | |
| 33 #ifdef SK_GAMMA_APPLY_TO_A8 | 33 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 34 , fLuminance(-1.0f) | 34 , fLuminance(-1.0f) |
| 35 #endif | 35 #endif |
| 36 {} | 36 {} |
| 37 | 37 |
| 38 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ | 38 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
| 39 const GrDistanceFieldTextureEffect& dfTexEffect = | 39 const GrDistanceFieldTextureEffect& dfTexEffect = |
| 40 args.fGP.cast<GrDistanceFieldTextureEffect>(); | 40 args.fGP.cast<GrDistanceFieldTextureEffect>(); |
| 41 const DistanceFieldBatchTracker& local = args.fBT.cast<DistanceFieldBatc
hTracker>(); | 41 const DistanceFieldBatchTracker& local = args.fBT.cast<DistanceFieldBatc
hTracker>(); |
| 42 GrGLGPBuilder* pb = args.fPB; | 42 GrGLGPBuilder* pb = args.fPB; |
| 43 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 43 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
| 44 SkAssertResult(fsBuilder->enableFeature( | 44 SkAssertResult(fsBuilder->enableFeature( |
| 45 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 45 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 46 | 46 |
| 47 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 47 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 48 | 48 |
| 49 // emit attributes | 49 // emit attributes |
| 50 vsBuilder->emitAttributes(dfTexEffect); | 50 vsBuilder->emitAttributes(dfTexEffect); |
| 51 | 51 |
| 52 GrGLVertToFrag v(kVec2f_GrSLType); | 52 GrGLVertToFrag st(kVec2f_GrSLType); |
| 53 args.fPB->addVarying("TextureCoords", &v); | 53 args.fPB->addVarying("IntTextureCoords", &st); |
| 54 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoord
s()->fName); | 54 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 55 |
| 56 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 57 args.fPB->addVarying("TextureCoords", &uv); |
| 58 // this is only used with text, so our texture bounds always match the g
lyph atlas |
| 59 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " |
| 60 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), |
| 61 dfTexEffect.inTextureCoords()->fName); |
| 55 | 62 |
| 56 // Setup pass through color | 63 // Setup pass through color |
| 57 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, | 64 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, |
| 58 dfTexEffect.inColor(), &fColorUniform); | 65 dfTexEffect.inColor(), &fColorUniform); |
| 59 | 66 |
| 60 // Setup position | 67 // Setup position |
| 61 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 68 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
| 62 | 69 |
| 63 // emit transforms | 70 // emit transforms |
| 64 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 71 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
| 65 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); | 72 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
| 66 | 73 |
| 67 const char* textureSizeUniName = NULL; | |
| 68 fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | |
| 69 kVec2f_GrSLType, kDefault_GrSLPre
cision, | |
| 70 "TextureSize", &textureSizeUniNam
e); | |
| 71 | |
| 72 // Use highp to work around aliasing issues | 74 // Use highp to work around aliasing issues |
| 73 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 75 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 74 pb->ctxInfo().stand
ard())); | 76 pb->ctxInfo().stand
ard())); |
| 75 fsBuilder->codeAppendf("vec2 uv = %s;\n", v.fsIn()); | 77 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
| 76 | 78 |
| 77 fsBuilder->codeAppend("\tfloat texColor = "); | 79 fsBuilder->codeAppend("\tfloat texColor = "); |
| 78 fsBuilder->appendTextureLookup(args.fSamplers[0], | 80 fsBuilder->appendTextureLookup(args.fSamplers[0], |
| 79 "uv", | 81 "uv", |
| 80 kVec2f_GrSLType); | 82 kVec2f_GrSLType); |
| 81 fsBuilder->codeAppend(".r;\n"); | 83 fsBuilder->codeAppend(".r;\n"); |
| 82 fsBuilder->codeAppend("\tfloat distance = " | 84 fsBuilder->codeAppend("\tfloat distance = " |
| 83 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFie
ldThreshold ");"); | 85 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFie
ldThreshold ");"); |
| 84 | 86 |
| 85 // we adjust for the effect of the transformation on the distance by usi
ng | 87 // we adjust for the effect of the transformation on the distance by usi
ng |
| 86 // the length of the gradient of the texture coordinates. We use st coor
dinates | 88 // the length of the gradient of the texture coordinates. We use st coor
dinates |
| 87 // to ensure we're mapping 1:1 from texel space to pixel space. | 89 // to ensure we're mapping 1:1 from texel space to pixel space. |
| 88 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 90 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 89 pb->ctxInfo().stand
ard())); | 91 pb->ctxInfo().stand
ard())); |
| 90 fsBuilder->codeAppendf("vec2 st = uv*%s;\n", textureSizeUniName); | 92 fsBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); |
| 91 fsBuilder->codeAppend("\tfloat afwidth;\n"); | 93 fsBuilder->codeAppend("\tfloat afwidth;\n"); |
| 92 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { | 94 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
| 93 // this gives us a smooth step across approximately one fragment | 95 // this gives us a smooth step across approximately one fragment |
| 94 fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*
dFdx(st.x));\n"); | 96 fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*
dFdx(st.x));\n"); |
| 95 } else { | 97 } else { |
| 96 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); | 98 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 97 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); | 99 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
| 98 | 100 |
| 99 fsBuilder->codeAppend("\tvec2 uv_grad;\n"); | 101 fsBuilder->codeAppend("\tvec2 uv_grad;\n"); |
| 100 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 102 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 130 fsBuilder->codeAppend(";\n"); | 132 fsBuilder->codeAppend(";\n"); |
| 131 fsBuilder->codeAppend("\tval = gammaColor.r;\n"); | 133 fsBuilder->codeAppend("\tval = gammaColor.r;\n"); |
| 132 #endif | 134 #endif |
| 133 | 135 |
| 134 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 136 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
| 135 } | 137 } |
| 136 | 138 |
| 137 virtual void setData(const GrGLProgramDataManager& pdman, | 139 virtual void setData(const GrGLProgramDataManager& pdman, |
| 138 const GrPrimitiveProcessor& proc, | 140 const GrPrimitiveProcessor& proc, |
| 139 const GrBatchTracker& bt) SK_OVERRIDE { | 141 const GrBatchTracker& bt) SK_OVERRIDE { |
| 140 SkASSERT(fTextureSizeUni.isValid()); | |
| 141 | |
| 142 GrTexture* texture = proc.texture(0); | |
| 143 if (texture->width() != fTextureSize.width() || | |
| 144 texture->height() != fTextureSize.height()) { | |
| 145 fTextureSize = SkISize::Make(texture->width(), texture->height()); | |
| 146 pdman.set2f(fTextureSizeUni, | |
| 147 SkIntToScalar(fTextureSize.width()), | |
| 148 SkIntToScalar(fTextureSize.height())); | |
| 149 } | |
| 150 #ifdef SK_GAMMA_APPLY_TO_A8 | 142 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 151 const GrDistanceFieldTextureEffect& dfTexEffect = | 143 const GrDistanceFieldTextureEffect& dfTexEffect = |
| 152 proc.cast<GrDistanceFieldTextureEffect>(); | 144 proc.cast<GrDistanceFieldTextureEffect>(); |
| 153 float luminance = dfTexEffect.getLuminance(); | 145 float luminance = dfTexEffect.getLuminance(); |
| 154 if (luminance != fLuminance) { | 146 if (luminance != fLuminance) { |
| 155 pdman.set1f(fLuminanceUni, luminance); | 147 pdman.set1f(fLuminanceUni, luminance); |
| 156 fLuminance = luminance; | 148 fLuminance = luminance; |
| 157 } | 149 } |
| 158 #endif | 150 #endif |
| 159 | 151 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 177 uint32_t key = dfTexEffect.getFlags(); | 169 uint32_t key = dfTexEffect.getFlags(); |
| 178 key |= local.fInputColorType << 16; | 170 key |= local.fInputColorType << 16; |
| 179 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 171 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
| 180 key |= ComputePosKey(gp.viewMatrix()) << 25; | 172 key |= ComputePosKey(gp.viewMatrix()) << 25; |
| 181 b->add32(key); | 173 b->add32(key); |
| 182 } | 174 } |
| 183 | 175 |
| 184 private: | 176 private: |
| 185 GrColor fColor; | 177 GrColor fColor; |
| 186 UniformHandle fColorUniform; | 178 UniformHandle fColorUniform; |
| 187 UniformHandle fTextureSizeUni; | |
| 188 SkISize fTextureSize; | |
| 189 UniformHandle fLuminanceUni; | 179 UniformHandle fLuminanceUni; |
| 190 #ifdef SK_GAMMA_APPLY_TO_A8 | 180 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 191 float fLuminance; | 181 float fLuminance; |
| 192 #endif | 182 #endif |
| 193 | 183 |
| 194 typedef GrGLGeometryProcessor INHERITED; | 184 typedef GrGLGeometryProcessor INHERITED; |
| 195 }; | 185 }; |
| 196 | 186 |
| 197 /////////////////////////////////////////////////////////////////////////////// | 187 /////////////////////////////////////////////////////////////////////////////// |
| 198 | 188 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 215 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) | 205 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
| 216 , fInColor(NULL) { | 206 , fInColor(NULL) { |
| 217 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); | 207 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
| 218 this->initClassID<GrDistanceFieldTextureEffect>(); | 208 this->initClassID<GrDistanceFieldTextureEffect>(); |
| 219 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 209 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
| 220 if (flags & kColorAttr_DistanceFieldEffectFlag) { | 210 if (flags & kColorAttr_DistanceFieldEffectFlag) { |
| 221 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); | 211 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); |
| 222 this->setHasVertexColor(); | 212 this->setHasVertexColor(); |
| 223 } | 213 } |
| 224 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 214 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
| 225 kVec2f_GrVertexAttribT
ype)); | 215 kVec2s_GrVertexAttribT
ype)); |
| 226 this->addTextureAccess(&fTextureAccess); | 216 this->addTextureAccess(&fTextureAccess); |
| 227 #ifdef SK_GAMMA_APPLY_TO_A8 | 217 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 228 this->addTextureAccess(&fGammaTextureAccess); | 218 this->addTextureAccess(&fGammaTextureAccess); |
| 229 #endif | 219 #endif |
| 230 } | 220 } |
| 231 | 221 |
| 232 bool GrDistanceFieldTextureEffect::onIsEqual(const GrGeometryProcessor& other) c
onst { | 222 bool GrDistanceFieldTextureEffect::onIsEqual(const GrGeometryProcessor& other) c
onst { |
| 233 const GrDistanceFieldTextureEffect& cte = other.cast<GrDistanceFieldTextureE
ffect>(); | 223 const GrDistanceFieldTextureEffect& cte = other.cast<GrDistanceFieldTextureE
ffect>(); |
| 234 return | 224 return |
| 235 #ifdef SK_GAMMA_APPLY_TO_A8 | 225 #ifdef SK_GAMMA_APPLY_TO_A8 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 struct DistanceFieldNoGammaBatchTracker { | 309 struct DistanceFieldNoGammaBatchTracker { |
| 320 GrGPInput fInputColorType; | 310 GrGPInput fInputColorType; |
| 321 GrColor fColor; | 311 GrColor fColor; |
| 322 bool fUsesLocalCoords; | 312 bool fUsesLocalCoords; |
| 323 }; | 313 }; |
| 324 | 314 |
| 325 class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { | 315 class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { |
| 326 public: | 316 public: |
| 327 GrGLDistanceFieldNoGammaTextureEffect(const GrGeometryProcessor&, | 317 GrGLDistanceFieldNoGammaTextureEffect(const GrGeometryProcessor&, |
| 328 const GrBatchTracker&) | 318 const GrBatchTracker&) |
| 329 : fColor(GrColor_ILLEGAL), fTextureSize(SkISize::Make(-1, -1)) {} | 319 : fColor(GrColor_ILLEGAL) {} |
| 330 | 320 |
| 331 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ | 321 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
| 332 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = | 322 const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = |
| 333 args.fGP.cast<GrDistanceFieldNoGammaTextureEffect>(); | 323 args.fGP.cast<GrDistanceFieldNoGammaTextureEffect>(); |
| 334 | 324 |
| 335 const DistanceFieldNoGammaBatchTracker& local = | 325 const DistanceFieldNoGammaBatchTracker& local = |
| 336 args.fBT.cast<DistanceFieldNoGammaBatchTracker>(); | 326 args.fBT.cast<DistanceFieldNoGammaBatchTracker>(); |
| 337 GrGLGPBuilder* pb = args.fPB; | 327 GrGLGPBuilder* pb = args.fPB; |
| 338 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 328 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
| 339 SkAssertResult(fsBuilder->enableFeature( | 329 SkAssertResult(fsBuilder->enableFeature( |
| 340 GrGLFragmentShaderBuilder::kStandardDerivat
ives_GLSLFeature)); | 330 GrGLFragmentShaderBuilder::kStandardDerivat
ives_GLSLFeature)); |
| 341 | 331 |
| 342 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 332 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 343 | 333 |
| 344 // emit attributes | 334 // emit attributes |
| 345 vsBuilder->emitAttributes(dfTexEffect); | 335 vsBuilder->emitAttributes(dfTexEffect); |
| 346 | 336 |
| 347 GrGLVertToFrag v(kVec2f_GrSLType); | 337 GrGLVertToFrag st(kVec2f_GrSLType); |
| 348 args.fPB->addVarying("TextureCoords", &v); | 338 args.fPB->addVarying("IntTextureCoords", &st); |
| 339 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 340 |
| 341 const char* recipTextureSizeUniName = NULL; |
| 342 fRecipTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kVertex_
Visibility, |
| 343 kVec2f_GrSLType, kDefault_Gr
SLPrecision, |
| 344 "RecipTextureSize", &recipTe
xtureSizeUniName); |
| 345 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 346 args.fPB->addVarying("TextureCoords", &uv); |
| 347 // this GP can be used with either text or paths, so texture bound param
s need to |
| 348 // be set by a uniform |
| 349 vsBuilder->codeAppendf("%s = %s*%s;", uv.vsOut(), recipTextureSizeUniNam
e, |
| 350 dfTexEffect.inTextureCoords()->fName); |
| 349 | 351 |
| 350 // setup pass through color | 352 // setup pass through color |
| 351 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, | 353 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, |
| 352 dfTexEffect.inColor(), &fColorUniform); | 354 dfTexEffect.inColor(), &fColorUniform); |
| 353 | 355 |
| 354 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoord
s()->fName); | |
| 355 | |
| 356 // Setup position | 356 // Setup position |
| 357 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 357 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
| 358 | 358 |
| 359 // emit transforms | 359 // emit transforms |
| 360 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 360 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
| 361 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); | 361 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
| 362 | 362 |
| 363 const char* textureSizeUniName = NULL; | |
| 364 fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | |
| 365 kVec2f_GrSLType, kDefault_GrSLPrec
ision, | |
| 366 "TextureSize", &textureSizeUniName
); | |
| 367 | |
| 368 // Use highp to work around aliasing issues | 363 // Use highp to work around aliasing issues |
| 369 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 364 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 370 pb->ctxInfo().stand
ard())); | 365 pb->ctxInfo().stand
ard())); |
| 371 fsBuilder->codeAppendf("vec2 uv = %s;", v.fsIn()); | 366 fsBuilder->codeAppendf("vec2 uv = %s;", uv.fsIn()); |
| 372 | 367 |
| 373 fsBuilder->codeAppend("float texColor = "); | 368 fsBuilder->codeAppend("float texColor = "); |
| 374 fsBuilder->appendTextureLookup(args.fSamplers[0], | 369 fsBuilder->appendTextureLookup(args.fSamplers[0], |
| 375 "uv", | 370 "uv", |
| 376 kVec2f_GrSLType); | 371 kVec2f_GrSLType); |
| 377 fsBuilder->codeAppend(".r;"); | 372 fsBuilder->codeAppend(".r;"); |
| 378 fsBuilder->codeAppend("float distance = " | 373 fsBuilder->codeAppend("float distance = " |
| 379 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold
");"); | 374 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold
");"); |
| 380 | 375 |
| 381 // we adjust for the effect of the transformation on the distance by usi
ng | 376 // we adjust for the effect of the transformation on the distance by usi
ng |
| 382 // the length of the gradient of the texture coordinates. We use st coor
dinates | 377 // the length of the gradient of the texture coordinates. We use st coor
dinates |
| 383 // to ensure we're mapping 1:1 from texel space to pixel space. | 378 // to ensure we're mapping 1:1 from texel space to pixel space. |
| 384 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 379 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 385 pb->ctxInfo().stand
ard())); | 380 pb->ctxInfo().stand
ard())); |
| 386 fsBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName); | 381 fsBuilder->codeAppendf("vec2 st = %s;", st.fsIn()); |
| 387 fsBuilder->codeAppend("float afwidth;"); | 382 fsBuilder->codeAppend("float afwidth;"); |
| 388 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { | 383 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
| 389 // this gives us a smooth step across approximately one fragment | 384 // this gives us a smooth step across approximately one fragment |
| 390 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); | 385 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); |
| 391 } else { | 386 } else { |
| 392 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); | 387 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
| 393 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); | 388 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
| 394 | 389 |
| 395 fsBuilder->codeAppend("vec2 uv_grad;"); | 390 fsBuilder->codeAppend("vec2 uv_grad;"); |
| 396 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 391 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 411 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); | 406 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); |
| 412 } | 407 } |
| 413 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); | 408 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); |
| 414 | 409 |
| 415 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 410 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
| 416 } | 411 } |
| 417 | 412 |
| 418 virtual void setData(const GrGLProgramDataManager& pdman, | 413 virtual void setData(const GrGLProgramDataManager& pdman, |
| 419 const GrPrimitiveProcessor& proc, | 414 const GrPrimitiveProcessor& proc, |
| 420 const GrBatchTracker& bt) SK_OVERRIDE { | 415 const GrBatchTracker& bt) SK_OVERRIDE { |
| 421 SkASSERT(fTextureSizeUni.isValid()); | 416 SkASSERT(fRecipTextureSizeUni.isValid()); |
| 422 | 417 |
| 423 GrTexture* texture = proc.texture(0); | 418 GrTexture* texture = proc.texture(0); |
| 424 if (texture->width() != fTextureSize.width() || | 419 if (texture->width() != fTextureSize.width() || |
| 425 texture->height() != fTextureSize.height()) { | 420 texture->height() != fTextureSize.height()) { |
| 426 fTextureSize = SkISize::Make(texture->width(), texture->height()); | 421 fTextureSize = SkISize::Make(texture->width(), texture->height()); |
| 427 pdman.set2f(fTextureSizeUni, | 422 pdman.set2f(fRecipTextureSizeUni, |
| 428 SkIntToScalar(fTextureSize.width()), | 423 1.0f/SkIntToScalar(fTextureSize.width()), |
| 429 SkIntToScalar(fTextureSize.height())); | 424 1.0f/SkIntToScalar(fTextureSize.height())); |
| 430 } | 425 } |
| 431 | 426 |
| 432 this->setUniformViewMatrix(pdman, proc.viewMatrix()); | 427 this->setUniformViewMatrix(pdman, proc.viewMatrix()); |
| 433 | 428 |
| 434 const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoG
ammaBatchTracker>(); | 429 const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoG
ammaBatchTracker>(); |
| 435 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColo
r) { | 430 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColo
r) { |
| 436 GrGLfloat c[4]; | 431 GrGLfloat c[4]; |
| 437 GrColorToRGBAFloat(local.fColor, c); | 432 GrColorToRGBAFloat(local.fColor, c); |
| 438 pdman.set4fv(fColorUniform, 1, c); | 433 pdman.set4fv(fColorUniform, 1, c); |
| 439 fColor = local.fColor; | 434 fColor = local.fColor; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 450 const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoG
ammaBatchTracker>(); | 445 const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoG
ammaBatchTracker>(); |
| 451 uint32_t key = dfTexEffect.getFlags(); | 446 uint32_t key = dfTexEffect.getFlags(); |
| 452 key |= local.fInputColorType << 16; | 447 key |= local.fInputColorType << 16; |
| 453 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 448 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
| 454 key |= ComputePosKey(gp.viewMatrix()) << 25; | 449 key |= ComputePosKey(gp.viewMatrix()) << 25; |
| 455 b->add32(key); | 450 b->add32(key); |
| 456 } | 451 } |
| 457 | 452 |
| 458 private: | 453 private: |
| 459 UniformHandle fColorUniform; | 454 UniformHandle fColorUniform; |
| 460 UniformHandle fTextureSizeUni; | 455 UniformHandle fRecipTextureSizeUni; |
| 461 GrColor fColor; | 456 GrColor fColor; |
| 462 SkISize fTextureSize; | 457 SkISize fTextureSize; |
| 463 | 458 |
| 464 typedef GrGLGeometryProcessor INHERITED; | 459 typedef GrGLGeometryProcessor INHERITED; |
| 465 }; | 460 }; |
| 466 | 461 |
| 467 /////////////////////////////////////////////////////////////////////////////// | 462 /////////////////////////////////////////////////////////////////////////////// |
| 468 | 463 |
| 469 GrDistanceFieldNoGammaTextureEffect::GrDistanceFieldNoGammaTextureEffect( | 464 GrDistanceFieldNoGammaTextureEffect::GrDistanceFieldNoGammaTextureEffect( |
| 470 GrColor color, | 465 GrColor color, |
| 471 const SkMatrix& viewMatrix, | 466 const SkMatrix& viewMatrix, |
| 472 GrTexture* texture, | 467 GrTexture* texture, |
| 473 const GrTextureParams& params, | 468 const GrTextureParams& params, |
| 474 uint32_t flags, | 469 uint32_t flags, |
| 475 bool opaqueVertexColors) | 470 bool opaqueVertexColors) |
| 476 : INHERITED(color, viewMatrix, SkMatrix::I(), opaqueVertexColors) | 471 : INHERITED(color, viewMatrix, SkMatrix::I(), opaqueVertexColors) |
| 477 , fTextureAccess(texture, params) | 472 , fTextureAccess(texture, params) |
| 478 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) | 473 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
| 479 , fInColor(NULL) { | 474 , fInColor(NULL) { |
| 480 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); | 475 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
| 481 this->initClassID<GrDistanceFieldNoGammaTextureEffect>(); | 476 this->initClassID<GrDistanceFieldNoGammaTextureEffect>(); |
| 482 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 477 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
| 483 if (flags & kColorAttr_DistanceFieldEffectFlag) { | 478 if (flags & kColorAttr_DistanceFieldEffectFlag) { |
| 484 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); | 479 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexA
ttribType)); |
| 485 this->setHasVertexColor(); | 480 this->setHasVertexColor(); |
| 486 } | 481 } |
| 487 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 482 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
| 488 kVec2f_GrVertexAttribT
ype)); | 483 kVec2s_GrVertexAttribT
ype)); |
| 489 this->addTextureAccess(&fTextureAccess); | 484 this->addTextureAccess(&fTextureAccess); |
| 490 } | 485 } |
| 491 | 486 |
| 492 bool GrDistanceFieldNoGammaTextureEffect::onIsEqual(const GrGeometryProcessor& o
ther) const { | 487 bool GrDistanceFieldNoGammaTextureEffect::onIsEqual(const GrGeometryProcessor& o
ther) const { |
| 493 const GrDistanceFieldNoGammaTextureEffect& cte = | 488 const GrDistanceFieldNoGammaTextureEffect& cte = |
| 494 other.cast<GrDistanceFieldNoGam
maTextureEffect>(); | 489 other.cast<GrDistanceFieldNoGam
maTextureEffect>(); |
| 495 return fFlags == cte.fFlags; | 490 return fFlags == cte.fFlags; |
| 496 } | 491 } |
| 497 | 492 |
| 498 void GrDistanceFieldNoGammaTextureEffect::onGetInvariantOutputCoverage(GrInitInv
ariantOutput* out) const{ | 493 void GrDistanceFieldNoGammaTextureEffect::onGetInvariantOutputCoverage(GrInitInv
ariantOutput* out) const{ |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 GrGPInput fInputColorType; | 560 GrGPInput fInputColorType; |
| 566 GrColor fColor; | 561 GrColor fColor; |
| 567 bool fUsesLocalCoords; | 562 bool fUsesLocalCoords; |
| 568 }; | 563 }; |
| 569 | 564 |
| 570 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { | 565 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
| 571 public: | 566 public: |
| 572 GrGLDistanceFieldLCDTextureEffect(const GrGeometryProcessor&, | 567 GrGLDistanceFieldLCDTextureEffect(const GrGeometryProcessor&, |
| 573 const GrBatchTracker&) | 568 const GrBatchTracker&) |
| 574 : fColor(GrColor_ILLEGAL) | 569 : fColor(GrColor_ILLEGAL) |
| 575 , fTextureSize(SkISize::Make(-1,-1)) | |
| 576 , fTextColor(GrColor_ILLEGAL) {} | 570 , fTextColor(GrColor_ILLEGAL) {} |
| 577 | 571 |
| 578 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ | 572 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
| 579 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 573 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 580 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); | 574 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); |
| 581 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); | 575 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); |
| 582 GrGLGPBuilder* pb = args.fPB; | 576 GrGLGPBuilder* pb = args.fPB; |
| 583 | 577 |
| 584 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 578 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 585 | 579 |
| 586 // emit attributes | 580 // emit attributes |
| 587 vsBuilder->emitAttributes(dfTexEffect); | 581 vsBuilder->emitAttributes(dfTexEffect); |
| 588 | 582 |
| 589 GrGLVertToFrag v(kVec2f_GrSLType); | 583 GrGLVertToFrag st(kVec2f_GrSLType); |
| 590 args.fPB->addVarying("TextureCoords", &v); | 584 args.fPB->addVarying("IntTextureCoords", &st); |
| 591 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoord
s()->fName); | 585 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 592 | 586 |
| 587 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 588 args.fPB->addVarying("TextureCoords", &uv); |
| 589 // this is only used with text, so our texture bounds always match the g
lyph atlas |
| 590 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " |
| 591 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), |
| 592 dfTexEffect.inTextureCoords()->fName); |
| 593 |
| 593 // setup pass through color | 594 // setup pass through color |
| 594 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, | 595 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, |
| 595 &fColorUniform); | 596 &fColorUniform); |
| 596 | 597 |
| 597 // Setup position | 598 // Setup position |
| 598 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 599 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
| 599 | 600 |
| 600 // emit transforms | 601 // emit transforms |
| 601 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 602 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
| 602 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); | 603 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
| 603 | 604 |
| 604 const char* textureSizeUniName = NULL; | |
| 605 // width, height, 1/(3*width) | |
| 606 fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | |
| 607 kVec3f_GrSLType, kDefault_GrSLPrec
ision, | |
| 608 "TextureSize", &textureSizeUniName
); | |
| 609 | |
| 610 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 605 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
| 611 | 606 |
| 612 SkAssertResult(fsBuilder->enableFeature( | 607 SkAssertResult(fsBuilder->enableFeature( |
| 613 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 608 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 614 | 609 |
| 615 // create LCD offset adjusted by inverse of transform | 610 // create LCD offset adjusted by inverse of transform |
| 616 // Use highp to work around aliasing issues | 611 // Use highp to work around aliasing issues |
| 617 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 612 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 618 pb->ctxInfo().stand
ard())); | 613 pb->ctxInfo().stand
ard())); |
| 619 fsBuilder->codeAppendf("vec2 uv = %s;\n", v.fsIn()); | 614 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
| 620 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 615 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 621 pb->ctxInfo().stand
ard())); | 616 pb->ctxInfo().stand
ard())); |
| 622 fsBuilder->codeAppendf("vec2 st = uv*%s.xy;\n", textureSizeUniName); | 617 fsBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); |
| 623 bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_Distance
FieldEffectMask); | 618 bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_Distance
FieldEffectMask); |
| 619 |
| 620 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
| 621 fsBuilder->codeAppend("float delta = -" GR_FONT_ATLAS_LCD_DELTA ";\n
"); |
| 622 } else { |
| 623 fsBuilder->codeAppend("float delta = " GR_FONT_ATLAS_LCD_DELTA ";\n"
); |
| 624 } |
| 624 if (isUniformScale) { | 625 if (isUniformScale) { |
| 625 fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); | 626 fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); |
| 626 fsBuilder->codeAppendf("\tvec2 offset = vec2(dx*%s.z, 0.0);\n", text
ureSizeUniName); | 627 fsBuilder->codeAppend("\tvec2 offset = vec2(dx*delta, 0.0);\n"); |
| 627 } else { | 628 } else { |
| 628 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); | 629 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 629 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); | 630 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
| 630 fsBuilder->codeAppendf("\tvec2 offset = %s.z*Jdx;\n", textureSizeUni
Name); | 631 fsBuilder->codeAppend("\tvec2 offset = delta*Jdx;\n"); |
| 631 } | 632 } |
| 632 | 633 |
| 633 // green is distance to uv center | 634 // green is distance to uv center |
| 634 fsBuilder->codeAppend("\tvec4 texColor = "); | 635 fsBuilder->codeAppend("\tvec4 texColor = "); |
| 635 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; | 636 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; |
| 636 fsBuilder->codeAppend(";\n"); | 637 fsBuilder->codeAppend(";\n"); |
| 637 fsBuilder->codeAppend("\tvec3 distance;\n"); | 638 fsBuilder->codeAppend("\tvec3 distance;\n"); |
| 638 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); | 639 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); |
| 639 // red is distance to left offset | 640 // red is distance to left offset |
| 640 fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); | 641 fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad.
y*Jdy.y);\n"); | 682 fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad.
y*Jdy.y);\n"); |
| 682 | 683 |
| 683 // this gives us a smooth step across approximately one fragment | 684 // this gives us a smooth step across approximately one fragment |
| 684 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*leng
th(grad);\n"); | 685 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*leng
th(grad);\n"); |
| 685 } | 686 } |
| 686 | 687 |
| 687 fsBuilder->codeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3
(afwidth), distance), 1.0);\n"); | 688 fsBuilder->codeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3
(afwidth), distance), 1.0);\n"); |
| 688 | 689 |
| 689 // adjust based on gamma | 690 // adjust based on gamma |
| 690 const char* textColorUniName = NULL; | 691 const char* textColorUniName = NULL; |
| 691 // width, height, 1/(3*width) | |
| 692 fTextColorUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 692 fTextColorUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 693 kVec3f_GrSLType, kDefault_GrSLPreci
sion, | 693 kVec3f_GrSLType, kDefault_GrSLPreci
sion, |
| 694 "TextColor", &textColorUniName); | 694 "TextColor", &textColorUniName); |
| 695 | 695 |
| 696 fsBuilder->codeAppendf("\tuv = vec2(val.x, %s.x);\n", textColorUniName); | 696 fsBuilder->codeAppendf("\tuv = vec2(val.x, %s.x);\n", textColorUniName); |
| 697 fsBuilder->codeAppend("float gammaColor = "); | 697 fsBuilder->codeAppend("float gammaColor = "); |
| 698 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 698 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 699 fsBuilder->codeAppend(".r;\n"); | 699 fsBuilder->codeAppend(".r;\n"); |
| 700 fsBuilder->codeAppend("\tval.x = gammaColor;\n"); | 700 fsBuilder->codeAppend("\tval.x = gammaColor;\n"); |
| 701 | 701 |
| 702 fsBuilder->codeAppendf("\tuv = vec2(val.y, %s.y);\n", textColorUniName); | 702 fsBuilder->codeAppendf("\tuv = vec2(val.y, %s.y);\n", textColorUniName); |
| 703 fsBuilder->codeAppend("\tgammaColor = "); | 703 fsBuilder->codeAppend("\tgammaColor = "); |
| 704 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 704 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 705 fsBuilder->codeAppend(".r;\n"); | 705 fsBuilder->codeAppend(".r;\n"); |
| 706 fsBuilder->codeAppend("\tval.y = gammaColor;\n"); | 706 fsBuilder->codeAppend("\tval.y = gammaColor;\n"); |
| 707 | 707 |
| 708 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); | 708 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); |
| 709 fsBuilder->codeAppend("\tgammaColor = "); | 709 fsBuilder->codeAppend("\tgammaColor = "); |
| 710 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 710 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 711 fsBuilder->codeAppend(".r;\n"); | 711 fsBuilder->codeAppend(".r;\n"); |
| 712 fsBuilder->codeAppend("\tval.z = gammaColor;\n"); | 712 fsBuilder->codeAppend("\tval.z = gammaColor;\n"); |
| 713 | 713 |
| 714 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 714 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
| 715 } | 715 } |
| 716 | 716 |
| 717 virtual void setData(const GrGLProgramDataManager& pdman, | 717 virtual void setData(const GrGLProgramDataManager& pdman, |
| 718 const GrPrimitiveProcessor& processor, | 718 const GrPrimitiveProcessor& processor, |
| 719 const GrBatchTracker& bt) SK_OVERRIDE { | 719 const GrBatchTracker& bt) SK_OVERRIDE { |
| 720 SkASSERT(fTextureSizeUni.isValid()); | |
| 721 SkASSERT(fTextColorUni.isValid()); | 720 SkASSERT(fTextColorUni.isValid()); |
| 722 | 721 |
| 723 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 722 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 724 processor.cast<GrDistanceFieldLCDTextureEffect>(); | 723 processor.cast<GrDistanceFieldLCDTextureEffect>(); |
| 725 GrTexture* texture = processor.texture(0); | |
| 726 if (texture->width() != fTextureSize.width() || | |
| 727 texture->height() != fTextureSize.height()) { | |
| 728 fTextureSize = SkISize::Make(texture->width(), texture->height()); | |
| 729 float delta = 1.0f/(3.0f*texture->width()); | |
| 730 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { | |
| 731 delta = -delta; | |
| 732 } | |
| 733 pdman.set3f(fTextureSizeUni, | |
| 734 SkIntToScalar(fTextureSize.width()), | |
| 735 SkIntToScalar(fTextureSize.height()), | |
| 736 delta); | |
| 737 } | |
| 738 | |
| 739 GrColor textColor = dfTexEffect.getTextColor(); | 724 GrColor textColor = dfTexEffect.getTextColor(); |
| 740 if (textColor != fTextColor) { | 725 if (textColor != fTextColor) { |
| 741 static const float ONE_OVER_255 = 1.f / 255.f; | 726 static const float ONE_OVER_255 = 1.f / 255.f; |
| 742 pdman.set3f(fTextColorUni, | 727 pdman.set3f(fTextColorUni, |
| 743 GrColorUnpackR(textColor) * ONE_OVER_255, | 728 GrColorUnpackR(textColor) * ONE_OVER_255, |
| 744 GrColorUnpackG(textColor) * ONE_OVER_255, | 729 GrColorUnpackG(textColor) * ONE_OVER_255, |
| 745 GrColorUnpackB(textColor) * ONE_OVER_255); | 730 GrColorUnpackB(textColor) * ONE_OVER_255); |
| 746 fTextColor = textColor; | 731 fTextColor = textColor; |
| 747 } | 732 } |
| 748 | 733 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 768 uint32_t key = dfTexEffect.getFlags(); | 753 uint32_t key = dfTexEffect.getFlags(); |
| 769 key |= local.fInputColorType << 16; | 754 key |= local.fInputColorType << 16; |
| 770 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 755 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
| 771 key |= ComputePosKey(gp.viewMatrix()) << 25; | 756 key |= ComputePosKey(gp.viewMatrix()) << 25; |
| 772 b->add32(key); | 757 b->add32(key); |
| 773 } | 758 } |
| 774 | 759 |
| 775 private: | 760 private: |
| 776 GrColor fColor; | 761 GrColor fColor; |
| 777 UniformHandle fColorUniform; | 762 UniformHandle fColorUniform; |
| 778 UniformHandle fTextureSizeUni; | |
| 779 SkISize fTextureSize; | |
| 780 UniformHandle fTextColorUni; | 763 UniformHandle fTextColorUni; |
| 781 SkColor fTextColor; | 764 SkColor fTextColor; |
| 782 | 765 |
| 783 typedef GrGLGeometryProcessor INHERITED; | 766 typedef GrGLGeometryProcessor INHERITED; |
| 784 }; | 767 }; |
| 785 | 768 |
| 786 /////////////////////////////////////////////////////////////////////////////// | 769 /////////////////////////////////////////////////////////////////////////////// |
| 787 | 770 |
| 788 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( | 771 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( |
| 789 GrColor color, const SkMatrix&
viewMatrix, | 772 GrColor color, const SkMatrix&
viewMatrix, |
| 790 GrTexture* texture, const GrTe
xtureParams& params, | 773 GrTexture* texture, const GrTe
xtureParams& params, |
| 791 GrTexture* gamma, const GrText
ureParams& gParams, | 774 GrTexture* gamma, const GrText
ureParams& gParams, |
| 792 SkColor textColor, | 775 SkColor textColor, |
| 793 uint32_t flags) | 776 uint32_t flags) |
| 794 : INHERITED(color, viewMatrix, SkMatrix::I()) | 777 : INHERITED(color, viewMatrix, SkMatrix::I()) |
| 795 , fTextureAccess(texture, params) | 778 , fTextureAccess(texture, params) |
| 796 , fGammaTextureAccess(gamma, gParams) | 779 , fGammaTextureAccess(gamma, gParams) |
| 797 , fTextColor(textColor) | 780 , fTextColor(textColor) |
| 798 , fFlags(flags & kLCD_DistanceFieldEffectMask){ | 781 , fFlags(flags & kLCD_DistanceFieldEffectMask){ |
| 799 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); | 782 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); |
| 800 this->initClassID<GrDistanceFieldLCDTextureEffect>(); | 783 this->initClassID<GrDistanceFieldLCDTextureEffect>(); |
| 801 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 784 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
| 802 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 785 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
| 803 kVec2f_GrVertexAttribT
ype)); | 786 kVec2s_GrVertexAttribT
ype)); |
| 804 this->addTextureAccess(&fTextureAccess); | 787 this->addTextureAccess(&fTextureAccess); |
| 805 this->addTextureAccess(&fGammaTextureAccess); | 788 this->addTextureAccess(&fGammaTextureAccess); |
| 806 } | 789 } |
| 807 | 790 |
| 808 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrGeometryProcessor& other
) const { | 791 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrGeometryProcessor& other
) const { |
| 809 const GrDistanceFieldLCDTextureEffect& cte = other.cast<GrDistanceFieldLCDTe
xtureEffect>(); | 792 const GrDistanceFieldLCDTextureEffect& cte = other.cast<GrDistanceFieldLCDTe
xtureEffect>(); |
| 810 return (fTextColor == cte.fTextColor && | 793 return (fTextColor == cte.fTextColor && |
| 811 fFlags == cte.fFlags); | 794 fFlags == cte.fFlags); |
| 812 } | 795 } |
| 813 | 796 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 861 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
| 879 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 862 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
| 880 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 863 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
| 881 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), | 864 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), |
| 882 GrProcessorUnitTest::TestMatr
ix(random), | 865 GrProcessorUnitTest::TestMatr
ix(random), |
| 883 textures[texIdx], params, | 866 textures[texIdx], params, |
| 884 textures[texIdx2], params2, | 867 textures[texIdx2], params2, |
| 885 textColor, | 868 textColor, |
| 886 flags); | 869 flags); |
| 887 } | 870 } |
| OLD | NEW |