Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Unified Diff: src/gpu/effects/GrDistanceFieldTextureEffect.cpp

Issue 149853002: Improved distance field sampling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: More clean up Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/gpu/GrTextStrike.h ('K') | « src/gpu/effects/GrDistanceFieldTextureEffect.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/gpu/effects/GrDistanceFieldTextureEffect.cpp
diff --git a/src/gpu/effects/GrDistanceFieldTextureEffect.cpp b/src/gpu/effects/GrDistanceFieldTextureEffect.cpp
index 8c38f9bebf919071af1c7a079deef5adbca983d2..8512cca78b0d89f45cdadb2332a5709123b43b32 100755
--- a/src/gpu/effects/GrDistanceFieldTextureEffect.cpp
+++ b/src/gpu/effects/GrDistanceFieldTextureEffect.cpp
@@ -13,13 +13,15 @@
#include "GrTBackendEffectFactory.h"
#include "GrTexture.h"
-// The distance field is constructed as unsigned char values, so that the zero value is at 128.
-// Hence our zero threshold is 128/255.
+// The distance field is constructed as unsigned char values, so that the zero value is at 128,
+// and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero threshold is 128/255.
+#define MULTIPLIER "7.96875"
#define THRESHOLD "0.50196078431"
class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect {
public:
- GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
+ GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory,
+ const GrDrawEffect& drawEffect)
: INHERITED (factory) {}
virtual void emitCode(GrGLFullShaderBuilder* builder,
@@ -29,28 +31,45 @@ public:
const char* inputColor,
const TransformedCoordsArray&,
const TextureSamplerArray& samplers) SK_OVERRIDE {
- SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs());
+ SkASSERT(2 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs());
SkString fsCoordName;
- const char* vsVaryingName;
- const char* fsVaryingNamePtr;
- builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr);
- fsCoordName = fsVaryingNamePtr;
-
- const char* attrName =
+ const char* vsCoordName;
+ const char* fsCoordNamePtr;
+ builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr);
+ fsCoordName = fsCoordNamePtr;
+
+ SkString fsSizeName;
+ const char* vsSizeName;
+ const char* fsSizeNamePtr;
+ builder->addVarying(kVec2f_GrSLType, "size", &vsSizeName, &fsSizeNamePtr);
+ fsSizeName = fsSizeNamePtr;
+
+ const char* attrName0 =
builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
- builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
+ builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0);
+ const char* attrName1 =
+ builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1])->c_str();
+ builder->vsCodeAppendf("\t%s = %s;\n", vsSizeName, attrName1);
builder->fsCodeAppend("\tvec4 texColor = ");
builder->fsAppendTextureLookup(samplers[0],
fsCoordName.c_str(),
kVec2f_GrSLType);
builder->fsCodeAppend(";\n");
- builder->fsCodeAppend("\tfloat distance = texColor.r;\n");
+ builder->fsCodeAppend("\tfloat distance = "MULTIPLIER"*(texColor.r - "THRESHOLD");\n");
+
+ builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), fsSizeName.c_str());
+ builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n");
+ builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n");
+ builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n");
+ builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jdy.x,\n");
+ builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jdy.y);\n");
+
// this gives us a smooth step across approximately one fragment
// (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2)
- builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(vec2(dFdx(distance), dFdy(distance)));\n");
- builder->fsCodeAppend("\tfloat val = smoothstep("THRESHOLD"-afwidth, "THRESHOLD"+afwidth, distance);\n");
+ builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n");
+ builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n");
builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
(GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str());
@@ -70,6 +89,7 @@ GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture,
: fTextureAccess(texture, params) {
this->addTextureAccess(&fTextureAccess);
this->addVertexAttrib(kVec2f_GrSLType);
+ this->addVertexAttrib(kVec2f_GrSLType);
}
bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const {
« src/gpu/GrTextStrike.h ('K') | « src/gpu/effects/GrDistanceFieldTextureEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698