| 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" |
| 11 #include "SkRect.h" | 11 #include "SkRect.h" |
| 12 #include "gl/GrGLFragmentProcessor.h" | 12 #include "gl/GrGLFragmentProcessor.h" |
| 13 #include "gl/builders/GrGLProgramBuilder.h" | 13 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 #include "glsl/GrGLSLProgramBuilder.h" |
| 14 | 15 |
| 15 ////////////////////////////////////////////////////////////////////////////// | 16 ////////////////////////////////////////////////////////////////////////////// |
| 16 | 17 |
| 17 class DitherEffect : public GrFragmentProcessor { | 18 class DitherEffect : public GrFragmentProcessor { |
| 18 public: | 19 public: |
| 19 static GrFragmentProcessor* Create() { | 20 static GrFragmentProcessor* Create() { |
| 20 return new DitherEffect; | 21 return new DitherEffect; |
| 21 } | 22 } |
| 22 | 23 |
| 23 virtual ~DitherEffect() {}; | 24 virtual ~DitherEffect() {}; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 virtual void emitCode(EmitArgs& args) override; | 66 virtual void emitCode(EmitArgs& args) override; |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 typedef GrGLFragmentProcessor INHERITED; | 69 typedef GrGLFragmentProcessor INHERITED; |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 GLDitherEffect::GLDitherEffect(const GrProcessor&) { | 72 GLDitherEffect::GLDitherEffect(const GrProcessor&) { |
| 72 } | 73 } |
| 73 | 74 |
| 74 void GLDitherEffect::emitCode(EmitArgs& args) { | 75 void GLDitherEffect::emitCode(EmitArgs& args) { |
| 75 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 76 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; |
| 76 // Generate a random number based on the fragment position. For this | 77 // Generate a random number based on the fragment position. For this |
| 77 // random number generator, we use the "GLSL rand" function | 78 // random number generator, we use the "GLSL rand" function |
| 78 // that seems to be floating around on the internet. It works under | 79 // that seems to be floating around on the internet. It works under |
| 79 // the assumption that sin(<big number>) oscillates with high frequency | 80 // the assumption that sin(<big number>) oscillates with high frequency |
| 80 // and sampling it will generate "randomness". Since we're using this | 81 // and sampling it will generate "randomness". Since we're using this |
| 81 // for rendering and not cryptography it should be OK. | 82 // for rendering and not cryptography it should be OK. |
| 82 | 83 |
| 83 // For each channel c, add the random offset to the pixel to either bump | 84 // 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 // it up or let it remain constant during quantization. |
| 85 fsBuilder->codeAppendf("\t\tfloat r = " | 86 fsBuilder->codeAppendf("\t\tfloat r = " |
| 86 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 87 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
| 87 fsBuilder->fragmentPosition()); | 88 fsBuilder->fragmentPosition()); |
| 88 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 89 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 89 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_st
r()); | 90 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_st
r()); |
| 90 } | 91 } |
| 91 | 92 |
| 92 ////////////////////////////////////////////////////////////////////////////// | 93 ////////////////////////////////////////////////////////////////////////////// |
| 93 | 94 |
| 94 void DitherEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, | 95 void DitherEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 95 GrProcessorKeyBuilder* b) const { | 96 GrProcessorKeyBuilder* b) const { |
| 96 GLDitherEffect::GenKey(*this, caps, b); | 97 GLDitherEffect::GenKey(*this, caps, b); |
| 97 } | 98 } |
| 98 | 99 |
| 99 GrGLFragmentProcessor* DitherEffect::onCreateGLInstance() const { | 100 GrGLFragmentProcessor* DitherEffect::onCreateGLInstance() const { |
| 100 return new GLDitherEffect(*this); | 101 return new GLDitherEffect(*this); |
| 101 } | 102 } |
| 102 | 103 |
| 103 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } | 104 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |