| 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; | 179 #ifdef SK_GAMMA_APPLY_TO_A8 |
| 188 SkISize fTextureSize; | |
| 189 UniformHandle fLuminanceUni; | 180 UniformHandle fLuminanceUni; |
| 190 #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 |
| 199 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrColor color, | 189 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrColor color, |
| 200 const SkMatrix& viewM
atrix, | 190 const SkMatrix& viewM
atrix, |
| (...skipping 14 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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 GrGPInput fInputColorType; | 555 GrGPInput fInputColorType; |
| 566 GrColor fColor; | 556 GrColor fColor; |
| 567 bool fUsesLocalCoords; | 557 bool fUsesLocalCoords; |
| 568 }; | 558 }; |
| 569 | 559 |
| 570 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { | 560 class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
| 571 public: | 561 public: |
| 572 GrGLDistanceFieldLCDTextureEffect(const GrGeometryProcessor&, | 562 GrGLDistanceFieldLCDTextureEffect(const GrGeometryProcessor&, |
| 573 const GrBatchTracker&) | 563 const GrBatchTracker&) |
| 574 : fColor(GrColor_ILLEGAL) | 564 : fColor(GrColor_ILLEGAL) |
| 575 , fTextureSize(SkISize::Make(-1,-1)) | |
| 576 , fTextColor(GrColor_ILLEGAL) {} | 565 , fTextColor(GrColor_ILLEGAL) {} |
| 577 | 566 |
| 578 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ | 567 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
| 579 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 568 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 580 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); | 569 args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); |
| 581 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); | 570 const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldL
CDBatchTracker>(); |
| 582 GrGLGPBuilder* pb = args.fPB; | 571 GrGLGPBuilder* pb = args.fPB; |
| 583 | 572 |
| 584 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); | 573 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 585 | 574 |
| 586 // emit attributes | 575 // emit attributes |
| 587 vsBuilder->emitAttributes(dfTexEffect); | 576 vsBuilder->emitAttributes(dfTexEffect); |
| 588 | 577 |
| 589 GrGLVertToFrag v(kVec2f_GrSLType); | 578 GrGLVertToFrag st(kVec2f_GrSLType); |
| 590 args.fPB->addVarying("TextureCoords", &v); | 579 args.fPB->addVarying("IntTextureCoords", &st); |
| 591 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoord
s()->fName); | 580 vsBuilder->codeAppendf("%s = %s;", st.vsOut(), dfTexEffect.inTextureCoor
ds()->fName); |
| 592 | 581 |
| 582 GrGLVertToFrag uv(kVec2f_GrSLType); |
| 583 args.fPB->addVarying("TextureCoords", &uv); |
| 584 // this is only used with text, so our texture bounds always match the g
lyph atlas |
| 585 vsBuilder->codeAppendf("%s = vec2(" GR_FONT_ATLAS_RECIP_WIDTH ", " |
| 586 GR_FONT_ATLAS_RECIP_HEIGHT ")*%s;", uv.vsOut(), |
| 587 dfTexEffect.inTextureCoords()->fName); |
| 588 |
| 593 // setup pass through color | 589 // setup pass through color |
| 594 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, | 590 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor
, NULL, |
| 595 &fColorUniform); | 591 &fColorUniform); |
| 596 | 592 |
| 597 // Setup position | 593 // Setup position |
| 598 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); | 594 this->setupPosition(pb, gpArgs, dfTexEffect.inPosition()->fName, dfTexEf
fect.viewMatrix()); |
| 599 | 595 |
| 600 // emit transforms | 596 // emit transforms |
| 601 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, | 597 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosit
ion()->fName, |
| 602 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); | 598 dfTexEffect.localMatrix(), args.fTransformsIn, args
.fTransformsOut); |
| 603 | 599 |
| 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(); | 600 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
| 611 | 601 |
| 612 SkAssertResult(fsBuilder->enableFeature( | 602 SkAssertResult(fsBuilder->enableFeature( |
| 613 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); | 603 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 614 | 604 |
| 615 // create LCD offset adjusted by inverse of transform | 605 // create LCD offset adjusted by inverse of transform |
| 616 // Use highp to work around aliasing issues | 606 // Use highp to work around aliasing issues |
| 617 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 607 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 618 pb->ctxInfo().stand
ard())); | 608 pb->ctxInfo().stand
ard())); |
| 619 fsBuilder->codeAppendf("vec2 uv = %s;\n", v.fsIn()); | 609 fsBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
| 620 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, | 610 fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision
, |
| 621 pb->ctxInfo().stand
ard())); | 611 pb->ctxInfo().stand
ard())); |
| 622 fsBuilder->codeAppendf("vec2 st = uv*%s.xy;\n", textureSizeUniName); | 612 fsBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); |
| 623 bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_Distance
FieldEffectMask); | 613 bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_Distance
FieldEffectMask); |
| 614 |
| 615 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
| 616 fsBuilder->codeAppend("float delta = -" GR_FONT_ATLAS_LCD_DELTA ";\n
"); |
| 617 } else { |
| 618 fsBuilder->codeAppend("float delta = " GR_FONT_ATLAS_LCD_DELTA ";\n"
); |
| 619 } |
| 624 if (isUniformScale) { | 620 if (isUniformScale) { |
| 625 fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); | 621 fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); |
| 626 fsBuilder->codeAppendf("\tvec2 offset = vec2(dx*%s.z, 0.0);\n", text
ureSizeUniName); | 622 fsBuilder->codeAppend("\tvec2 offset = vec2(dx*delta, 0.0);\n"); |
| 627 } else { | 623 } else { |
| 628 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); | 624 fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 629 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); | 625 fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
| 630 fsBuilder->codeAppendf("\tvec2 offset = %s.z*Jdx;\n", textureSizeUni
Name); | 626 fsBuilder->codeAppend("\tvec2 offset = delta*Jdx;\n"); |
| 631 } | 627 } |
| 632 | 628 |
| 633 // green is distance to uv center | 629 // green is distance to uv center |
| 634 fsBuilder->codeAppend("\tvec4 texColor = "); | 630 fsBuilder->codeAppend("\tvec4 texColor = "); |
| 635 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; | 631 fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType)
; |
| 636 fsBuilder->codeAppend(";\n"); | 632 fsBuilder->codeAppend(";\n"); |
| 637 fsBuilder->codeAppend("\tvec3 distance;\n"); | 633 fsBuilder->codeAppend("\tvec3 distance;\n"); |
| 638 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); | 634 fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); |
| 639 // red is distance to left offset | 635 // red is distance to left offset |
| 640 fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); | 636 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"); | 677 fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad.
y*Jdy.y);\n"); |
| 682 | 678 |
| 683 // this gives us a smooth step across approximately one fragment | 679 // this gives us a smooth step across approximately one fragment |
| 684 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*leng
th(grad);\n"); | 680 fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*leng
th(grad);\n"); |
| 685 } | 681 } |
| 686 | 682 |
| 687 fsBuilder->codeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3
(afwidth), distance), 1.0);\n"); | 683 fsBuilder->codeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3
(afwidth), distance), 1.0);\n"); |
| 688 | 684 |
| 689 // adjust based on gamma | 685 // adjust based on gamma |
| 690 const char* textColorUniName = NULL; | 686 const char* textColorUniName = NULL; |
| 691 // width, height, 1/(3*width) | |
| 692 fTextColorUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 687 fTextColorUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 693 kVec3f_GrSLType, kDefault_GrSLPreci
sion, | 688 kVec3f_GrSLType, kDefault_GrSLPreci
sion, |
| 694 "TextColor", &textColorUniName); | 689 "TextColor", &textColorUniName); |
| 695 | 690 |
| 696 fsBuilder->codeAppendf("\tuv = vec2(val.x, %s.x);\n", textColorUniName); | 691 fsBuilder->codeAppendf("\tuv = vec2(val.x, %s.x);\n", textColorUniName); |
| 697 fsBuilder->codeAppend("float gammaColor = "); | 692 fsBuilder->codeAppend("float gammaColor = "); |
| 698 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 693 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 699 fsBuilder->codeAppend(".r;\n"); | 694 fsBuilder->codeAppend(".r;\n"); |
| 700 fsBuilder->codeAppend("\tval.x = gammaColor;\n"); | 695 fsBuilder->codeAppend("\tval.x = gammaColor;\n"); |
| 701 | 696 |
| 702 fsBuilder->codeAppendf("\tuv = vec2(val.y, %s.y);\n", textColorUniName); | 697 fsBuilder->codeAppendf("\tuv = vec2(val.y, %s.y);\n", textColorUniName); |
| 703 fsBuilder->codeAppend("\tgammaColor = "); | 698 fsBuilder->codeAppend("\tgammaColor = "); |
| 704 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 699 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 705 fsBuilder->codeAppend(".r;\n"); | 700 fsBuilder->codeAppend(".r;\n"); |
| 706 fsBuilder->codeAppend("\tval.y = gammaColor;\n"); | 701 fsBuilder->codeAppend("\tval.y = gammaColor;\n"); |
| 707 | 702 |
| 708 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); | 703 fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); |
| 709 fsBuilder->codeAppend("\tgammaColor = "); | 704 fsBuilder->codeAppend("\tgammaColor = "); |
| 710 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; | 705 fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType)
; |
| 711 fsBuilder->codeAppend(".r;\n"); | 706 fsBuilder->codeAppend(".r;\n"); |
| 712 fsBuilder->codeAppend("\tval.z = gammaColor;\n"); | 707 fsBuilder->codeAppend("\tval.z = gammaColor;\n"); |
| 713 | 708 |
| 714 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); | 709 fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
| 715 } | 710 } |
| 716 | 711 |
| 717 virtual void setData(const GrGLProgramDataManager& pdman, | 712 virtual void setData(const GrGLProgramDataManager& pdman, |
| 718 const GrPrimitiveProcessor& processor, | 713 const GrPrimitiveProcessor& processor, |
| 719 const GrBatchTracker& bt) SK_OVERRIDE { | 714 const GrBatchTracker& bt) SK_OVERRIDE { |
| 720 SkASSERT(fTextureSizeUni.isValid()); | |
| 721 SkASSERT(fTextColorUni.isValid()); | 715 SkASSERT(fTextColorUni.isValid()); |
| 722 | 716 |
| 723 const GrDistanceFieldLCDTextureEffect& dfTexEffect = | 717 const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
| 724 processor.cast<GrDistanceFieldLCDTextureEffect>(); | 718 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(); | 719 GrColor textColor = dfTexEffect.getTextColor(); |
| 740 if (textColor != fTextColor) { | 720 if (textColor != fTextColor) { |
| 741 static const float ONE_OVER_255 = 1.f / 255.f; | 721 static const float ONE_OVER_255 = 1.f / 255.f; |
| 742 pdman.set3f(fTextColorUni, | 722 pdman.set3f(fTextColorUni, |
| 743 GrColorUnpackR(textColor) * ONE_OVER_255, | 723 GrColorUnpackR(textColor) * ONE_OVER_255, |
| 744 GrColorUnpackG(textColor) * ONE_OVER_255, | 724 GrColorUnpackG(textColor) * ONE_OVER_255, |
| 745 GrColorUnpackB(textColor) * ONE_OVER_255); | 725 GrColorUnpackB(textColor) * ONE_OVER_255); |
| 746 fTextColor = textColor; | 726 fTextColor = textColor; |
| 747 } | 727 } |
| 748 | 728 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 768 uint32_t key = dfTexEffect.getFlags(); | 748 uint32_t key = dfTexEffect.getFlags(); |
| 769 key |= local.fInputColorType << 16; | 749 key |= local.fInputColorType << 16; |
| 770 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; | 750 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1
<< 24: 0x0; |
| 771 key |= ComputePosKey(gp.viewMatrix()) << 25; | 751 key |= ComputePosKey(gp.viewMatrix()) << 25; |
| 772 b->add32(key); | 752 b->add32(key); |
| 773 } | 753 } |
| 774 | 754 |
| 775 private: | 755 private: |
| 776 GrColor fColor; | 756 GrColor fColor; |
| 777 UniformHandle fColorUniform; | 757 UniformHandle fColorUniform; |
| 778 UniformHandle fTextureSizeUni; | |
| 779 SkISize fTextureSize; | |
| 780 UniformHandle fTextColorUni; | 758 UniformHandle fTextColorUni; |
| 781 SkColor fTextColor; | 759 SkColor fTextColor; |
| 782 | 760 |
| 783 typedef GrGLGeometryProcessor INHERITED; | 761 typedef GrGLGeometryProcessor INHERITED; |
| 784 }; | 762 }; |
| 785 | 763 |
| 786 /////////////////////////////////////////////////////////////////////////////// | 764 /////////////////////////////////////////////////////////////////////////////// |
| 787 | 765 |
| 788 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( | 766 GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( |
| 789 GrColor color, const SkMatrix&
viewMatrix, | 767 GrColor color, const SkMatrix&
viewMatrix, |
| 790 GrTexture* texture, const GrTe
xtureParams& params, | 768 GrTexture* texture, const GrTe
xtureParams& params, |
| 791 GrTexture* gamma, const GrText
ureParams& gParams, | 769 GrTexture* gamma, const GrText
ureParams& gParams, |
| 792 SkColor textColor, | 770 SkColor textColor, |
| 793 uint32_t flags) | 771 uint32_t flags) |
| 794 : INHERITED(color, viewMatrix, SkMatrix::I()) | 772 : INHERITED(color, viewMatrix, SkMatrix::I()) |
| 795 , fTextureAccess(texture, params) | 773 , fTextureAccess(texture, params) |
| 796 , fGammaTextureAccess(gamma, gParams) | 774 , fGammaTextureAccess(gamma, gParams) |
| 797 , fTextColor(textColor) | 775 , fTextColor(textColor) |
| 798 , fFlags(flags & kLCD_DistanceFieldEffectMask){ | 776 , fFlags(flags & kLCD_DistanceFieldEffectMask){ |
| 799 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); | 777 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_Distan
ceFieldEffectFlag)); |
| 800 this->initClassID<GrDistanceFieldLCDTextureEffect>(); | 778 this->initClassID<GrDistanceFieldLCDTextureEffect>(); |
| 801 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); | 779 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertex
AttribType)); |
| 802 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", | 780 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
| 803 kVec2f_GrVertexAttribT
ype)); | 781 kVec2s_GrVertexAttribT
ype)); |
| 804 this->addTextureAccess(&fTextureAccess); | 782 this->addTextureAccess(&fTextureAccess); |
| 805 this->addTextureAccess(&fGammaTextureAccess); | 783 this->addTextureAccess(&fGammaTextureAccess); |
| 806 } | 784 } |
| 807 | 785 |
| 808 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrGeometryProcessor& other
) const { | 786 bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrGeometryProcessor& other
) const { |
| 809 const GrDistanceFieldLCDTextureEffect& cte = other.cast<GrDistanceFieldLCDTe
xtureEffect>(); | 787 const GrDistanceFieldLCDTextureEffect& cte = other.cast<GrDistanceFieldLCDTe
xtureEffect>(); |
| 810 return (fTextColor == cte.fTextColor && | 788 return (fTextColor == cte.fTextColor && |
| 811 fFlags == cte.fFlags); | 789 fFlags == cte.fFlags); |
| 812 } | 790 } |
| 813 | 791 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 856 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
| 879 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 857 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
| 880 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 858 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
| 881 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), | 859 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), |
| 882 GrProcessorUnitTest::TestMatr
ix(random), | 860 GrProcessorUnitTest::TestMatr
ix(random), |
| 883 textures[texIdx], params, | 861 textures[texIdx], params, |
| 884 textures[texIdx2], params2, | 862 textures[texIdx2], params2, |
| 885 textColor, | 863 textColor, |
| 886 flags); | 864 flags); |
| 887 } | 865 } |
| OLD | NEW |