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"
14 #include "glsl/GrGLSLProgramBuilder.h"
16 //////////////////////////////////////////////////////////////////////////////
18 class DitherEffect : public GrFragmentProcessor {
20 static GrFragmentProcessor* Create() {
21 return new DitherEffect;
24 virtual ~DitherEffect() {};
26 const char* name() const override { return "Dither"; }
30 this->initClassID<DitherEffect>();
31 this->setWillReadFragmentPosition();
34 GrGLSLFragmentProcessor* onCreateGLInstance() const override;
36 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
38 // All dither effects are equal
39 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
41 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
43 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
45 typedef GrFragmentProcessor INHERITED;
48 void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
49 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
52 //////////////////////////////////////////////////////////////////////////////
54 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect);
56 const GrFragmentProcessor* DitherEffect::TestCreate(GrProcessorTestData*) {
57 return DitherEffect::Create();
60 //////////////////////////////////////////////////////////////////////////////
62 class GLDitherEffect : public GrGLSLFragmentProcessor {
64 GLDitherEffect(const GrProcessor&);
66 virtual void emitCode(EmitArgs& args) override;
69 typedef GrGLSLFragmentProcessor INHERITED;
72 GLDitherEffect::GLDitherEffect(const GrProcessor&) {
75 void GLDitherEffect::emitCode(EmitArgs& args) {
76 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
77 // Generate a random number based on the fragment position. For this
78 // random number generator, we use the "GLSL rand" function
79 // that seems to be floating around on the internet. It works under
80 // the assumption that sin(<big number>) oscillates with high frequency
81 // and sampling it will generate "randomness". Since we're using this
82 // for rendering and not cryptography it should be OK.
84 // For each channel c, add the random offset to the pixel to either bump
85 // it up or let it remain constant during quantization.
86 fsBuilder->codeAppendf("\t\tfloat r = "
87 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
88 fsBuilder->fragmentPosition());
89 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
90 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_str());
93 //////////////////////////////////////////////////////////////////////////////
95 void DitherEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
96 GrProcessorKeyBuilder* b) const {
97 GLDitherEffect::GenKey(*this, caps, b);
100 GrGLSLFragmentProcessor* DitherEffect::onCreateGLInstance() const {
101 return new GLDitherEffect(*this);
104 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); }