| 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..ab6cef09017e31f9042af490ec02fbd135465df3
|
| --- /dev/null
|
| +++ b/src/gpu/effects/GrDitherEffect.cpp
|
| @@ -0,0 +1,147 @@
|
| +/*
|
| + * 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) {
|
| +}
|
| +
|
| +void GLDitherEffect::emitCode(GrGLShaderBuilder* builder,
|
| + const GrDrawEffect& drawEffect,
|
| + EffectKey key,
|
| + const char* outputColor,
|
| + const char* inputColor,
|
| + const TransformedCoordsArray&,
|
| + const TextureSamplerArray& samplers) {
|
| +#if GR_DITHER_RANDOM
|
| + // Generate a random number based on the fragment position. For this
|
| + // random number generator, we use the "GLSL rand" function
|
| + // that seems to be floating around on the internet. It works under
|
| + // the assumption that sin(<big number>) oscillates with high frequency
|
| + // and sampling it will generate "randomness". Since we're using this
|
| + // for rendering and not cryptography it should be OK.
|
| +
|
| + // for each channel c:
|
| + // 1. Compute quantized colors [low, high] that c is between
|
| + // 2. Pick high by flipping a coin weighted by (c - low)
|
| + builder->fsCodeAppendf("\t\tfloat r = "
|
| + "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
|
| + static const char *kSuffixes[] = { "r", "g", "b" };
|
| + static const int kNumSuffixes = SK_ARRAY_COUNT(kSuffixes);
|
| + for (int i = 0; i < kNumSuffixes; ++i) {
|
| + builder->fsCodeAppendf("\t\tfloat dithered_%s;\n", kSuffixes[i]);
|
| + builder->fsCodeAppendf("\t\t{\n");
|
| + builder->fsCodeAppendf("\t\t\tfloat channel = 255.0f * %s.%s;\n",
|
| + inputColor, kSuffixes[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", kSuffixes[i]);
|
| + builder->fsCodeAppendf("\t\t}\n");
|
| + }
|
| +
|
| + builder->fsCodeAppendf("\t\t%s = vec4(", outputColor);
|
| + for (int i = 0; i < kNumSuffixes; ++i) {
|
| + builder->fsCodeAppendf("dithered_%s%s", kSuffixes[i], (i < kNumSuffixes)? ", " : "");
|
| + }
|
| + builder->fsCodeAppendf("%s.a);\n", inputColor);
|
| +#else
|
| + builder->fsCodeAppendf("\t\tfloat dither[4] = float[4](\n");
|
| + builder->fsCodeAppendf("\t\t\t0.000784314, 0.00235294, 0.00313725, 0.00156863);\n");
|
| + builder->fsCodeAppendf("\t\tint x = int(%s.x + 0.5) & 1;\n", builder->fragmentPosition());
|
| + builder->fsCodeAppendf("\t\tint y = int(%s.y + 0.5) & 1;\n", builder->fragmentPosition());
|
| + builder->fsCodeAppendf("\t\tfloat d = dither[y*2+x];\n");
|
| + builder->fsCodeAppendf("\t\t%s = vec4(d, d, d, 0) + %s;\n",
|
| + outputColor, inputColor);
|
| +#endif
|
| +}
|
| +
|
| +//////////////////////////////////////////////////////////////////////////////
|
| +
|
| +GrEffectRef* GrDitherEffect::Create() {
|
| + return DitherEffect::Create();
|
| +}
|
|
|