| 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 "gl/builders/GrGLProgramBuilder.h" | |
| 9 #include "GrDitherEffect.h" | 8 #include "GrDitherEffect.h" |
| 9 #include "GrFragmentProcessor.h" |
| 10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
| 11 #include "SkRect.h" |
| 11 #include "gl/GrGLProcessor.h" | 12 #include "gl/GrGLProcessor.h" |
| 12 #include "gl/GrGLSL.h" | 13 #include "gl/GrGLSL.h" |
| 13 #include "GrTBackendProcessorFactory.h" | 14 #include "gl/builders/GrGLProgramBuilder.h" |
| 14 | |
| 15 #include "SkRect.h" | |
| 16 | 15 |
| 17 ////////////////////////////////////////////////////////////////////////////// | 16 ////////////////////////////////////////////////////////////////////////////// |
| 18 | 17 |
| 19 class GLDitherEffect; | |
| 20 | |
| 21 class DitherEffect : public GrFragmentProcessor { | 18 class DitherEffect : public GrFragmentProcessor { |
| 22 public: | 19 public: |
| 23 static GrFragmentProcessor* Create() { | 20 static GrFragmentProcessor* Create() { |
| 24 GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ()) | 21 GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ()) |
| 25 return SkRef(gDitherEffect); | 22 return SkRef(gDitherEffect); |
| 26 } | 23 } |
| 27 | 24 |
| 28 virtual ~DitherEffect() {}; | 25 virtual ~DitherEffect() {}; |
| 29 static const char* Name() { return "Dither"; } | |
| 30 | 26 |
| 31 typedef GLDitherEffect GLProcessor; | 27 virtual const char* name() const SK_OVERRIDE { return "Dither"; } |
| 32 | 28 |
| 33 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { | 29 virtual void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) cons
t SK_OVERRIDE; |
| 34 return GrTBackendFragmentProcessorFactory<DitherEffect>::getInstance(); | 30 |
| 35 } | 31 virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE; |
| 36 | 32 |
| 37 private: | 33 private: |
| 38 DitherEffect() { | 34 DitherEffect() { |
| 35 this->initClassID<DitherEffect>(); |
| 39 this->setWillReadFragmentPosition(); | 36 this->setWillReadFragmentPosition(); |
| 40 } | 37 } |
| 41 | 38 |
| 42 // All dither effects are equal | 39 // All dither effects are equal |
| 43 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { retur
n true; } | 40 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { retur
n true; } |
| 44 | 41 |
| 45 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVE
RRIDE; | 42 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVE
RRIDE; |
| 46 | 43 |
| 47 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 44 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 48 | 45 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 61 GrContext*, | 58 GrContext*, |
| 62 const GrDrawTargetCaps&, | 59 const GrDrawTargetCaps&, |
| 63 GrTexture*[]) { | 60 GrTexture*[]) { |
| 64 return DitherEffect::Create(); | 61 return DitherEffect::Create(); |
| 65 } | 62 } |
| 66 | 63 |
| 67 ////////////////////////////////////////////////////////////////////////////// | 64 ////////////////////////////////////////////////////////////////////////////// |
| 68 | 65 |
| 69 class GLDitherEffect : public GrGLFragmentProcessor { | 66 class GLDitherEffect : public GrGLFragmentProcessor { |
| 70 public: | 67 public: |
| 71 GLDitherEffect(const GrBackendProcessorFactory&, const GrProcessor&); | 68 GLDitherEffect(const GrProcessor&); |
| 72 | 69 |
| 73 virtual void emitCode(GrGLFPBuilder* builder, | 70 virtual void emitCode(GrGLFPBuilder* builder, |
| 74 const GrFragmentProcessor& fp, | 71 const GrFragmentProcessor& fp, |
| 75 const char* outputColor, | 72 const char* outputColor, |
| 76 const char* inputColor, | 73 const char* inputColor, |
| 77 const TransformedCoordsArray&, | 74 const TransformedCoordsArray&, |
| 78 const TextureSamplerArray&) SK_OVERRIDE; | 75 const TextureSamplerArray&) SK_OVERRIDE; |
| 79 | 76 |
| 80 private: | 77 private: |
| 81 typedef GrGLFragmentProcessor INHERITED; | 78 typedef GrGLFragmentProcessor INHERITED; |
| 82 }; | 79 }; |
| 83 | 80 |
| 84 GLDitherEffect::GLDitherEffect(const GrBackendProcessorFactory& factory, | 81 GLDitherEffect::GLDitherEffect(const GrProcessor&) { |
| 85 const GrProcessor&) | |
| 86 : INHERITED (factory) { | |
| 87 } | 82 } |
| 88 | 83 |
| 89 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, | 84 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, |
| 90 const GrFragmentProcessor& fp, | 85 const GrFragmentProcessor& fp, |
| 91 const char* outputColor, | 86 const char* outputColor, |
| 92 const char* inputColor, | 87 const char* inputColor, |
| 93 const TransformedCoordsArray&, | 88 const TransformedCoordsArray&, |
| 94 const TextureSamplerArray& samplers) { | 89 const TextureSamplerArray& samplers) { |
| 95 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 90 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 96 // Generate a random number based on the fragment position. For this | 91 // Generate a random number based on the fragment position. For this |
| 97 // random number generator, we use the "GLSL rand" function | 92 // random number generator, we use the "GLSL rand" function |
| 98 // that seems to be floating around on the internet. It works under | 93 // that seems to be floating around on the internet. It works under |
| 99 // the assumption that sin(<big number>) oscillates with high frequency | 94 // the assumption that sin(<big number>) oscillates with high frequency |
| 100 // and sampling it will generate "randomness". Since we're using this | 95 // and sampling it will generate "randomness". Since we're using this |
| 101 // for rendering and not cryptography it should be OK. | 96 // for rendering and not cryptography it should be OK. |
| 102 | 97 |
| 103 // For each channel c, add the random offset to the pixel to either bump | 98 // For each channel c, add the random offset to the pixel to either bump |
| 104 // it up or let it remain constant during quantization. | 99 // it up or let it remain constant during quantization. |
| 105 fsBuilder->codeAppendf("\t\tfloat r = " | 100 fsBuilder->codeAppendf("\t\tfloat r = " |
| 106 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 101 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
| 107 fsBuilder->fragmentPosition()); | 102 fsBuilder->fragmentPosition()); |
| 108 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 103 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 109 outputColor, GrGLSLExpr4(inputColor).c_str()); | 104 outputColor, GrGLSLExpr4(inputColor).c_str()); |
| 110 } | 105 } |
| 111 | 106 |
| 112 ////////////////////////////////////////////////////////////////////////////// | 107 ////////////////////////////////////////////////////////////////////////////// |
| 113 | 108 |
| 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 |
| 114 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } | 118 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |