Chromium Code Reviews| 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 "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
| 10 #include "gl/GrGLSL.h" | 10 #include "gl/GrGLSL.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 virtual void emitCode(GrGLFullShaderBuilder* builder, | 28 virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 29 const GrDrawEffect& drawEffect, | 29 const GrDrawEffect& drawEffect, |
| 30 EffectKey key, | 30 EffectKey key, |
| 31 const char* outputColor, | 31 const char* outputColor, |
| 32 const char* inputColor, | 32 const char* inputColor, |
| 33 const TransformedCoordsArray&, | 33 const TransformedCoordsArray&, |
| 34 const TextureSamplerArray& samplers) SK_OVERRIDE { | 34 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV ertexAttribs()); | 35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV ertexAttribs()); |
| 36 | 36 |
| 37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva tives_GLSLFeature)); | 37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva tives_GLSLFeature)); |
| 38 const GrDistanceFieldTextureEffect& dfTexEffect = | |
| 39 drawEffect.castEffect<GrDistanceFi eldTextureEffect>(); | |
| 38 | 40 |
| 39 SkString fsCoordName; | 41 SkString fsCoordName; |
| 40 const char* vsCoordName; | 42 const char* vsCoordName; |
| 41 const char* fsCoordNamePtr; | 43 const char* fsCoordNamePtr; |
| 42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr); | 44 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC oordNamePtr); |
| 43 fsCoordName = fsCoordNamePtr; | 45 fsCoordName = fsCoordNamePtr; |
| 44 | 46 |
| 45 const char* attrName0 = | 47 const char* attrName0 = |
| 46 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str(); | 48 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0])->c_str(); |
| 47 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); | 49 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
| 48 | 50 |
| 49 const char* textureSizeUniName = NULL; | 51 const char* textureSizeUniName = NULL; |
| 50 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib ility, | 52 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib ility, |
| 51 kVec2f_GrSLType, "TextureSize", | 53 kVec2f_GrSLType, "TextureSize", |
| 52 &textureSizeUniName); | 54 &textureSizeUniName); |
| 53 | 55 |
| 54 builder->fsCodeAppend("\tvec4 texColor = "); | 56 builder->fsCodeAppend("\tvec4 texColor = "); |
| 55 builder->fsAppendTextureLookup(samplers[0], | 57 builder->fsAppendTextureLookup(samplers[0], |
| 56 fsCoordName.c_str(), | 58 fsCoordName.c_str(), |
| 57 kVec2f_GrSLType); | 59 kVec2f_GrSLType); |
| 58 builder->fsCodeAppend(";\n"); | 60 builder->fsCodeAppend(";\n"); |
| 59 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); | 61 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); |
| 60 | 62 |
| 61 // we adjust for the effect of the transformation on the distance by usi ng | 63 // we adjust for the effect of the transformation on the distance by usi ng |
| 62 // the length of the gradient of the texture coordinates. We use st coor dinates | 64 // the length of the gradient of the texture coordinates. We use st coor dinates |
| 63 // to ensure we're mapping 1:1 from texel space to pixel space. | 65 // to ensure we're mapping 1:1 from texel space to pixel space. |
| 64 builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), text ureSizeUniName); | 66 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); |
| 65 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); | 67 builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); |
| 66 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); | 68 builder->fsCodeAppend("\tfloat afwidth;\n"); |
| 67 builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n"); | 69 if (dfTexEffect.isUniformScale()) { |
| 68 builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jd y.x,\n"); | 70 // this gives us a smooth step across approximately one fragment |
| 69 builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jd y.y);\n"); | 71 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2) |
| 72 builder->fsCodeAppend("\tafwidth = 0.7071*dFdx(st.x);\n"); | |
| 73 } else { | |
| 74 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); | |
| 75 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); | |
| 70 | 76 |
| 71 // this gives us a smooth step across approximately one fragment | 77 builder->fsCodeAppend("\tvec2 uv_grad;\n"); |
| 72 // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) | 78 #ifdef SK_BUILD_FOR_ANDROID |
|
bsalomon
2014/03/26 13:18:58
Can we make this a runtime check based on Vendor o
jvanverth1
2014/03/26 16:24:00
Done.
| |
| 73 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n"); | 79 // this is to compensate for the Adreno, which likes to drop tiles o n division by 0 |
| 80 builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); | |
| 81 builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n"); | |
| 82 builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); | |
| 83 builder->fsCodeAppend("\t} else {\n"); | |
| 84 builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"); | |
| 85 builder->fsCodeAppend("\t}\n"); | |
| 86 #else | |
| 87 builder->fsCodeAppend("\tuv_grad = normalize(uv);\n"); | |
| 88 #endif | |
| 89 builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad. y*Jdy.x,\n"); | |
| 90 builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad. y*Jdy.y);\n"); | |
| 91 | |
| 92 // this gives us a smooth step across approximately one fragment | |
| 93 // (assuming a radius of the diagonal of the fragment, hence a facto r of sqrt(2)/2) | |
| 94 builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n"); | |
| 95 } | |
| 96 | |
| 74 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista nce);\n"); | 97 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista nce);\n"); |
| 75 | 98 |
| 76 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, | 99 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 77 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val") ).c_str()); | 100 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val") ).c_str()); |
| 78 } | 101 } |
| 79 | 102 |
| 80 virtual void setData(const GrGLUniformManager& uman, | 103 virtual void setData(const GrGLUniformManager& uman, |
| 81 const GrDrawEffect& drawEffect) SK_OVERRIDE { | 104 const GrDrawEffect& drawEffect) SK_OVERRIDE { |
| 82 SkASSERT(fTextureSizeUni.isValid()); | 105 SkASSERT(fTextureSizeUni.isValid()); |
| 83 const GrDistanceFieldTextureEffect& distanceFieldEffect = | 106 const GrDistanceFieldTextureEffect& distanceFieldEffect = |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 95 GrGLUniformManager::UniformHandle fTextureSizeUni; | 118 GrGLUniformManager::UniformHandle fTextureSizeUni; |
| 96 SkSize fTextureSize; | 119 SkSize fTextureSize; |
| 97 | 120 |
| 98 typedef GrGLVertexEffect INHERITED; | 121 typedef GrGLVertexEffect INHERITED; |
| 99 }; | 122 }; |
| 100 | 123 |
| 101 /////////////////////////////////////////////////////////////////////////////// | 124 /////////////////////////////////////////////////////////////////////////////// |
| 102 | 125 |
| 103 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, | 126 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
| 104 const GrTextureParams & params, | 127 const GrTextureParams & params, |
| 105 const SkISize& size) | 128 const SkISize& size, |
| 129 bool uniformScale) | |
| 106 : fTextureAccess(texture, params) | 130 : fTextureAccess(texture, params) |
| 107 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height( )))) { | 131 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height( )))) |
| 132 , fUniformScale(uniformScale) { | |
| 108 this->addTextureAccess(&fTextureAccess); | 133 this->addTextureAccess(&fTextureAccess); |
| 109 this->addVertexAttrib(kVec2f_GrSLType); | 134 this->addVertexAttrib(kVec2f_GrSLType); |
| 110 } | 135 } |
| 111 | 136 |
| 112 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { | 137 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
| 113 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other); | 138 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE ffect>(other); |
| 114 return fTextureAccess == cte.fTextureAccess; | 139 return fTextureAccess == cte.fTextureAccess; |
| 115 } | 140 } |
| 116 | 141 |
| 117 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, | 142 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 144 SkShader::kMirror_TileMode, | 169 SkShader::kMirror_TileMode, |
| 145 }; | 170 }; |
| 146 SkShader::TileMode tileModes[] = { | 171 SkShader::TileMode tileModes[] = { |
| 147 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 172 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 148 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 173 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 149 }; | 174 }; |
| 150 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : | 175 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil erp_FilterMode : |
| 151 GrTextureParams::kNon e_FilterMode); | 176 GrTextureParams::kNon e_FilterMode); |
| 152 SkISize size = SkISize::Make(1024, 2048); | 177 SkISize size = SkISize::Make(1024, 2048); |
| 153 | 178 |
| 154 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size); | 179 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size, random->nextBool()); |
| 155 } | 180 } |
| OLD | NEW |