krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrDitherEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 9 | #include "GrFragmentProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 11 | #include "SkRect.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "gl/GrGLProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 13 | #include "gl/GrGLSL.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 14 | #include "gl/builders/GrGLProgramBuilder.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 15 | |
| 16 | ////////////////////////////////////////////////////////////////////////////// |
| 17 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | class DitherEffect : public GrFragmentProcessor { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 19 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | static GrFragmentProcessor* Create() { |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 21 | GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ()) |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 22 | return SkRef(gDitherEffect); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | virtual ~DitherEffect() {}; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 26 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 27 | virtual const char* name() const SK_OVERRIDE { return "Dither"; } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 28 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 29 | virtual void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE; |
| 30 | |
| 31 | virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | DitherEffect() { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 35 | this->initClassID<DitherEffect>(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 36 | this->setWillReadFragmentPosition(); |
| 37 | } |
| 38 | |
| 39 | // All dither effects are equal |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 40 | virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 41 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 42 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 43 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 44 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 45 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 46 | typedef GrFragmentProcessor INHERITED; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 49 | void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
| 50 | inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ////////////////////////////////////////////////////////////////////////////// |
| 54 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 56 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 57 | GrFragmentProcessor* DitherEffect::TestCreate(SkRandom*, |
| 58 | GrContext*, |
| 59 | const GrDrawTargetCaps&, |
| 60 | GrTexture*[]) { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 61 | return DitherEffect::Create(); |
| 62 | } |
| 63 | |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 66 | class GLDitherEffect : public GrGLFragmentProcessor { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 67 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 68 | GLDitherEffect(const GrProcessor&); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 69 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 70 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 71 | const GrFragmentProcessor& fp, |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 72 | const char* outputColor, |
| 73 | const char* inputColor, |
| 74 | const TransformedCoordsArray&, |
| 75 | const TextureSamplerArray&) SK_OVERRIDE; |
| 76 | |
| 77 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 78 | typedef GrGLFragmentProcessor INHERITED; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 81 | GLDitherEffect::GLDitherEffect(const GrProcessor&) { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 82 | } |
| 83 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 84 | void GLDitherEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | const GrFragmentProcessor& fp, |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 86 | const char* outputColor, |
| 87 | const char* inputColor, |
| 88 | const TransformedCoordsArray&, |
| 89 | const TextureSamplerArray& samplers) { |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 90 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 91 | // Generate a random number based on the fragment position. For this |
| 92 | // random number generator, we use the "GLSL rand" function |
| 93 | // that seems to be floating around on the internet. It works under |
| 94 | // the assumption that sin(<big number>) oscillates with high frequency |
| 95 | // and sampling it will generate "randomness". Since we're using this |
| 96 | // for rendering and not cryptography it should be OK. |
| 97 | |
| 98 | // For each channel c, add the random offset to the pixel to either bump |
| 99 | // it up or let it remain constant during quantization. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 100 | fsBuilder->codeAppendf("\t\tfloat r = " |
bsalomon | 2290000 | 2014-06-24 11:16:52 -0700 | [diff] [blame] | 101 | "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n", |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | fsBuilder->fragmentPosition()); |
| 103 | fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 104 | outputColor, GrGLSLExpr4(inputColor).c_str()); |
| 105 | } |
| 106 | |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 109 | void DitherEffect::getGLProcessorKey(const GrGLCaps& caps, |
| 110 | GrProcessorKeyBuilder* b) const { |
| 111 | GLDitherEffect::GenKey(*this, caps, b); |
| 112 | } |
| 113 | |
| 114 | GrGLFragmentProcessor* DitherEffect::createGLInstance() const { |
| 115 | return SkNEW_ARGS(GLDitherEffect, (*this)); |
| 116 | } |
| 117 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 118 | GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |