| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrDitherEffect.h" | 8 #include "GrDitherEffect.h" |
| 9 #include "GrFragmentProcessor.h" | 9 #include "GrFragmentProcessor.h" |
| 10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect); | 53 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect); |
| 54 | 54 |
| 55 const GrFragmentProcessor* DitherEffect::TestCreate(GrProcessorTestData*) { | 55 const GrFragmentProcessor* DitherEffect::TestCreate(GrProcessorTestData*) { |
| 56 return DitherEffect::Create(); | 56 return DitherEffect::Create(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 ////////////////////////////////////////////////////////////////////////////// | 59 ////////////////////////////////////////////////////////////////////////////// |
| 60 | 60 |
| 61 class GLDitherEffect : public GrGLSLFragmentProcessor { | 61 class GLDitherEffect : public GrGLSLFragmentProcessor { |
| 62 public: | 62 public: |
| 63 GLDitherEffect(const GrProcessor&); | 63 void emitCode(EmitArgs& args) override; |
| 64 | |
| 65 virtual void emitCode(EmitArgs& args) override; | |
| 66 | 64 |
| 67 private: | 65 private: |
| 68 typedef GrGLSLFragmentProcessor INHERITED; | 66 typedef GrGLSLFragmentProcessor INHERITED; |
| 69 }; | 67 }; |
| 70 | 68 |
| 71 GLDitherEffect::GLDitherEffect(const GrProcessor&) { | |
| 72 } | |
| 73 | |
| 74 void GLDitherEffect::emitCode(EmitArgs& args) { | 69 void GLDitherEffect::emitCode(EmitArgs& args) { |
| 75 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 70 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 76 // Generate a random number based on the fragment position. For this | 71 // Generate a random number based on the fragment position. For this |
| 77 // random number generator, we use the "GLSL rand" function | 72 // random number generator, we use the "GLSL rand" function |
| 78 // that seems to be floating around on the internet. It works under | 73 // that seems to be floating around on the internet. It works under |
| 79 // the assumption that sin(<big number>) oscillates with high frequency | 74 // the assumption that sin(<big number>) oscillates with high frequency |
| 80 // and sampling it will generate "randomness". Since we're using this | 75 // and sampling it will generate "randomness". Since we're using this |
| 81 // for rendering and not cryptography it should be OK. | 76 // for rendering and not cryptography it should be OK. |
| 82 | 77 |
| 83 // For each channel c, add the random offset to the pixel to either bump | 78 // For each channel c, add the random offset to the pixel to either bump |
| 84 // it up or let it remain constant during quantization. | 79 // it up or let it remain constant during quantization. |
| 85 fragBuilder->codeAppendf("\t\tfloat r = " | 80 fragBuilder->codeAppendf("\t\tfloat r = " |
| 86 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 4375
8.5453);\n", | 81 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 4375
8.5453);\n", |
| 87 fragBuilder->fragmentPosition()); | 82 fragBuilder->fragmentPosition()); |
| 88 fragBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 83 fragBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 89 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_
str()); | 84 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_
str()); |
| 90 } | 85 } |
| 91 | 86 |
| 92 ////////////////////////////////////////////////////////////////////////////// | 87 ////////////////////////////////////////////////////////////////////////////// |
| 93 | 88 |
| 94 void DitherEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 89 void DitherEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 95 GrProcessorKeyBuilder* b) const { | 90 GrProcessorKeyBuilder* b) const { |
| 96 GLDitherEffect::GenKey(*this, caps, b); | 91 GLDitherEffect::GenKey(*this, caps, b); |
| 97 } | 92 } |
| 98 | 93 |
| 99 GrGLSLFragmentProcessor* DitherEffect::onCreateGLSLInstance() const { | 94 GrGLSLFragmentProcessor* DitherEffect::onCreateGLSLInstance() const { |
| 100 return new GLDitherEffect(*this); | 95 return new GLDitherEffect; |
| 101 } | 96 } |
| 102 | 97 |
| 103 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } | 98 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |