2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrDitherEffect.h"
9 #include "GrFragmentProcessor.h"
10 #include "GrInvariantOutput.h"
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
15 //////////////////////////////////////////////////////////////////////////////
17 class DitherEffect : public GrFragmentProcessor {
19 static GrFragmentProcessor* Create() {
20 return new DitherEffect;
23 virtual ~DitherEffect() {};
25 const char* name() const override { return "Dither"; }
29 this->initClassID<DitherEffect>();
30 this->setWillReadFragmentPosition();
33 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
35 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
37 // All dither effects are equal
38 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
40 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
42 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
44 typedef GrFragmentProcessor INHERITED;
47 void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
48 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
51 //////////////////////////////////////////////////////////////////////////////
53 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect);
55 const GrFragmentProcessor* DitherEffect::TestCreate(GrProcessorTestData*) {
56 return DitherEffect::Create();
59 //////////////////////////////////////////////////////////////////////////////
61 class GLDitherEffect : public GrGLSLFragmentProcessor {
63 GLDitherEffect(const GrProcessor&);
65 virtual void emitCode(EmitArgs& args) override;
68 typedef GrGLSLFragmentProcessor INHERITED;
71 GLDitherEffect::GLDitherEffect(const GrProcessor&) {
74 void GLDitherEffect::emitCode(EmitArgs& args) {
75 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
76 // Generate a random number based on the fragment position. For this
77 // random number generator, we use the "GLSL rand" function
78 // that seems to be floating around on the internet. It works under
79 // the assumption that sin(<big number>) oscillates with high frequency
80 // and sampling it will generate "randomness". Since we're using this
81 // for rendering and not cryptography it should be OK.
83 // For each channel c, add the random offset to the pixel to either bump
84 // it up or let it remain constant during quantization.
85 fragBuilder->codeAppendf("\t\tfloat r = "
86 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
87 fragBuilder->fragmentPosition());
88 fragBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
89 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_str());
92 //////////////////////////////////////////////////////////////////////////////
94 void DitherEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
95 GrProcessorKeyBuilder* b) const {
96 GLDitherEffect::GenKey(*this, caps, b);
99 GrGLSLFragmentProcessor* DitherEffect::onCreateGLSLInstance() const {
100 return new GLDitherEffect(*this);
103 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); }