aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/effects')
-rw-r--r--src/gpu/effects/GrEdgeEffect.cpp138
-rw-r--r--src/gpu/effects/GrEdgeEffect.h85
2 files changed, 223 insertions, 0 deletions
diff --git a/src/gpu/effects/GrEdgeEffect.cpp b/src/gpu/effects/GrEdgeEffect.cpp
new file mode 100644
index 0000000000..f02fa35ae0
--- /dev/null
+++ b/src/gpu/effects/GrEdgeEffect.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrEdgeEffect.h"
+#include "gl/GrGLEffect.h"
+#include "gl/GrGLSL.h"
+#include "GrTBackendEffectFactory.h"
+
+class GrGLEdgeEffect : public GrGLEffect {
+public:
+ GrGLEdgeEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
+ : INHERITED (factory) {}
+
+ virtual void emitCode(GrGLShaderBuilder* builder,
+ const GrDrawEffect& drawEffect,
+ EffectKey key,
+ const char* outputColor,
+ const char* inputColor,
+ const TextureSamplerArray& samplers) SK_OVERRIDE {
+ const GrEdgeEffect& edgeEffect = drawEffect.castEffect<GrEdgeEffect>();
+ GrEdgeEffect::EdgeType type = edgeEffect.edgeType();
+
+ const char *vsName, *fsName;
+ const SkString* attrName =
+ builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
+ builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
+
+ switch (type) {
+ case GrEdgeEffect::kHairLine_EdgeType:
+ builder->addVarying(kVec4f_GrSLType, "HairEdge", &vsName, &fsName);
+
+ builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n",
+ builder->fragmentPosition(), fsName);
+ builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
+ break;
+ case GrEdgeEffect::kQuad_EdgeType:
+ GrAssert(builder->ctxInfo().caps()->shaderDerivativeSupport());
+ builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
+
+ // keep the derivative instructions outside the conditional
+ builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
+ builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
+ builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
+ // today we know z and w are in device space. We could use derivatives
+ builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
+ fsName);
+ builder->fsCodeAppendf ("\t\t} else {\n");
+ builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
+ "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
+ fsName, fsName);
+ builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
+ fsName);
+ builder->fsCodeAppendf("\t\t\tedgeAlpha = "
+ "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
+ if (kES2_GrGLBinding == builder->ctxInfo().binding()) {
+ builder->fHeader.append("#extension GL_OES_standard_derivatives: enable\n");
+ }
+ break;
+ case GrEdgeEffect::kHairQuad_EdgeType:
+ GrAssert(builder->ctxInfo().caps()->shaderDerivativeSupport());
+ builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName);
+
+ builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
+ builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
+ builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
+ "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
+ fsName, fsName);
+ builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
+ fsName);
+ builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
+ builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
+ if (kES2_GrGLBinding == builder->ctxInfo().binding()) {
+ builder->fHeader.append("#extension GL_OES_standard_derivatives: enable\n");
+ }
+ break;
+ };
+
+ SkString modulate;
+ GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha");
+ builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
+
+ builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
+ }
+
+ static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
+ const GrEdgeEffect& QuadEffect = drawEffect.castEffect<GrEdgeEffect>();
+
+ return QuadEffect.edgeType();
+ }
+
+ virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {
+ }
+
+private:
+ typedef GrGLEffect INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+GrEdgeEffect::GrEdgeEffect(EdgeType edgeType) : GrEffect() {
+ if (edgeType == kQuad_EdgeType) {
+ this->addVertexAttrib(kVec4f_GrSLType);
+ } else {
+ this->addVertexAttrib(kVec4f_GrSLType); // TODO: use different vec sizes for differnt edge
+ // types.
+ }
+ fEdgeType = edgeType;
+}
+
+void GrEdgeEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
+ *validFlags = 0;
+}
+
+const GrBackendEffectFactory& GrEdgeEffect::getFactory() const {
+ return GrTBackendEffectFactory<GrEdgeEffect>::getInstance();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_EFFECT_TEST(GrEdgeEffect);
+
+GrEffectRef* GrEdgeEffect::TestCreate(SkMWCRandom* random,
+ GrContext*,
+ const GrDrawTargetCaps& caps,
+ GrTexture*[]) {
+ // Only kHairLine works without derivative instructions.
+ EdgeType edgeType;
+ if (caps.shaderDerivativeSupport()) {
+ edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCount));
+ } else {
+ edgeType = kHairLine_EdgeType;
+ }
+ return GrEdgeEffect::Create(edgeType);
+}
diff --git a/src/gpu/effects/GrEdgeEffect.h b/src/gpu/effects/GrEdgeEffect.h
new file mode 100644
index 0000000000..c6238a2e09
--- /dev/null
+++ b/src/gpu/effects/GrEdgeEffect.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrEdgeEffect_DEFINED
+#define GrEdgeEffect_DEFINED
+
+#include "GrEffect.h"
+
+class GrGLEdgeEffect;
+
+/**
+ * The output of this effect is one of three different edge types: hairlines, quads,
+ * and hairline quads.
+ */
+
+class GrEdgeEffect : public GrEffect {
+public:
+ enum EdgeType {
+ /* 1-pixel wide line
+ 2D implicit device coord line eq (a*x + b*y +c = 0). 4th component unused. */
+ kHairLine_EdgeType = 0,
+ /* Quadratic specified by 0=u^2-v canonical coords. u and v are the first
+ two components of the vertex attribute. Coverage is based on signed
+ distance with negative being inside, positive outside. The edge is specified in
+ window space (y-down). If either the third or fourth component of the interpolated
+ vertex coord is > 0 then the pixel is considered outside the edge. This is used to
+ attempt to trim to a portion of the infinite quad. Requires shader derivative
+ instruction support. */
+ kQuad_EdgeType,
+ /* Similar to above but for hairline quadratics. Uses unsigned distance.
+ Coverage is min(0, 1-distance). 3rd & 4th component unused. Requires
+ shader derivative instruction support. */
+ kHairQuad_EdgeType,
+
+ kLast_EdgeType = kHairQuad_EdgeType
+ };
+ static const int kEdgeTypeCount = kLast_EdgeType + 1;
+
+ static GrEffectRef* Create(EdgeType type) {
+ // we go through this so we only have one copy of each effect
+ static GrEffectRef* gEdgeEffectRef[kEdgeTypeCount] = {
+ CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrEdgeEffect, (kHairLine_EdgeType)))),
+ CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrEdgeEffect, (kQuad_EdgeType)))),
+ CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrEdgeEffect, (kHairQuad_EdgeType)))),
+ };
+ static SkAutoTUnref<GrEffectRef> gUnref0(gEdgeEffectRef[0]);
+ static SkAutoTUnref<GrEffectRef> gUnref1(gEdgeEffectRef[1]);
+ static SkAutoTUnref<GrEffectRef> gUnref2(gEdgeEffectRef[2]);
+
+ gEdgeEffectRef[type]->ref();
+ return gEdgeEffectRef[type];
+ }
+
+ virtual ~GrEdgeEffect() {}
+
+ static const char* Name() { return "Edge"; }
+
+ virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
+
+ typedef GrGLEdgeEffect GLEffect;
+
+ virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
+
+ EdgeType edgeType() const { return fEdgeType; }
+
+private:
+ GrEdgeEffect(EdgeType edgeType);
+
+ virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
+ const GrEdgeEffect& qee = CastEffect<GrEdgeEffect>(other);
+ return qee.fEdgeType == fEdgeType;
+ }
+
+ EdgeType fEdgeType;
+
+ GR_DECLARE_EFFECT_TEST;
+
+ typedef GrEffect INHERITED;
+};
+
+#endif