Index: src/gpu/effects/GrDitherEffect.cpp |
diff --git a/src/gpu/effects/GrDitherEffect.cpp b/src/gpu/effects/GrDitherEffect.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..002a60865c2a82eea335eb8e2822ddad3c7d9b42 |
--- /dev/null |
+++ b/src/gpu/effects/GrDitherEffect.cpp |
@@ -0,0 +1,126 @@ |
+/* |
+ * Copyright 2014 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "GrDitherEffect.h" |
+ |
+#include "gl/GrGLEffect.h" |
+#include "gl/GrGLSL.h" |
+#include "GrTBackendEffectFactory.h" |
+ |
+#include "SkRect.h" |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+class GLDitherEffect; |
+ |
+class DitherEffect : public GrEffect { |
+public: |
+ static GrEffectRef* Create() { |
+ return CreateEffectRef(AutoEffectUnref(SkNEW(DitherEffect))); |
+ } |
+ |
+ virtual ~DitherEffect() {}; |
+ static const char* Name() { return "Dither"; } |
+ |
+ typedef GLDitherEffect GLEffect; |
+ |
+ virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
+ |
+ virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
+ return GrTBackendEffectFactory<DitherEffect>::getInstance(); |
+ } |
+ |
+private: |
+ DitherEffect() { |
+ this->setWillReadFragmentPosition(); |
+ } |
+ |
+ // All dither effects are equal |
+ virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE { return true; } |
+ |
+ GR_DECLARE_EFFECT_TEST; |
+ |
+ typedef GrEffect INHERITED; |
+}; |
+ |
+void DitherEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
+ *validFlags = 0; |
+} |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+GR_DEFINE_EFFECT_TEST(DitherEffect); |
+ |
+GrEffectRef* DitherEffect::TestCreate(SkRandom*, |
+ GrContext*, |
+ const GrDrawTargetCaps&, |
+ GrTexture*[]) { |
+ return DitherEffect::Create(); |
+} |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+class GLDitherEffect : public GrGLEffect { |
+public: |
+ GLDitherEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
+ |
+ virtual void emitCode(GrGLShaderBuilder* builder, |
+ const GrDrawEffect& drawEffect, |
+ EffectKey key, |
+ const char* outputColor, |
+ const char* inputColor, |
+ const TransformedCoordsArray&, |
+ const TextureSamplerArray&) SK_OVERRIDE; |
+ |
+private: |
+ typedef GrGLEffect INHERITED; |
+}; |
+ |
+GLDitherEffect::GLDitherEffect(const GrBackendEffectFactory& factory, |
+ const GrDrawEffect& drawEffect) |
+ : INHERITED (factory) { |
+} |
+ |
robertphillips
2014/06/11 14:15:50
// for each channel c:
// 1. Compute quantized col
krajcevski
2014/06/11 22:33:18
Done.
|
+void GLDitherEffect::emitCode(GrGLShaderBuilder* builder, |
+ const GrDrawEffect& drawEffect, |
+ EffectKey key, |
+ const char* outputColor, |
+ const char* inputColor, |
+ const TransformedCoordsArray&, |
+ const TextureSamplerArray& samplers) { |
+ // Generate a random number based on the fragment position. |
+ builder->fsCodeAppendf("\t\tfloat r = " |
robertphillips
2014/06/11 14:14:32
magic numbers?
egdaniel
2014/06/11 14:26:24
Probably should add a comment to describe it, but
krajcevski
2014/06/11 22:33:18
I added a comment for this.
|
+ "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n", |
+ builder->fragmentPosition()); |
+ |
+ // For each channel, weight the neighboring pixel values based on thier |
+ // distance from the input channel |
robertphillips
2014/06/11 14:14:32
kSuffixes ?
krajcevski
2014/06/11 22:33:18
Done.
|
+ static const char *suffixes[4] = { "r", "g", "b", "a" }; |
+ for (int i = 0; i < 4; ++i) { |
+ builder->fsCodeAppendf("\t\tfloat dithered_%s;\n", suffixes[i]); |
+ builder->fsCodeAppendf("\t\t{\n"); |
+ builder->fsCodeAppendf("\t\t\tfloat channel = 255.0f * %s.%s;\n", |
+ inputColor, suffixes[i]); |
+ builder->fsCodeAppendf("\t\t\tfloat low = floor(channel);\n"); |
+ builder->fsCodeAppendf("\t\t\tfloat c = channel-low;\n"); |
+ builder->fsCodeAppendf("\t\t\tfloat high = low + 1;\n"); |
+ builder->fsCodeAppendf("\t\t\tfloat t = float(c > r);\n"); |
+ builder->fsCodeAppendf("\t\t\tdithered_%s = (low*(1-t) + high*t)/255.0f;\n", suffixes[i]); |
+ builder->fsCodeAppendf("\t\t}\n"); |
+ } |
+ |
+ builder->fsCodeAppendf("\t\t%s = vec4(", outputColor); |
+ for (int i = 0; i < 4; ++i) { |
+ builder->fsCodeAppendf("dithered_%s%s", suffixes[i], (i < 3)? ", " : ");\n"); |
+ } |
+} |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+GrEffectRef* GrDitherEffect::Create() { |
+ return DitherEffect::Create(); |
+} |