| 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" |
| 8 #include "GrDitherEffect.h" | 9 #include "GrDitherEffect.h" |
| 9 | 10 |
| 10 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLEffect.h" |
| 11 #include "gl/GrGLShaderBuilder.h" | |
| 12 #include "gl/GrGLSL.h" | 12 #include "gl/GrGLSL.h" |
| 13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
| 14 | 14 |
| 15 #include "SkRect.h" | 15 #include "SkRect.h" |
| 16 | 16 |
| 17 ////////////////////////////////////////////////////////////////////////////// | 17 ////////////////////////////////////////////////////////////////////////////// |
| 18 | 18 |
| 19 class GLDitherEffect; | 19 class GLDitherEffect; |
| 20 | 20 |
| 21 class DitherEffect : public GrEffect { | 21 class DitherEffect : public GrEffect { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 GrTexture*[]) { | 63 GrTexture*[]) { |
| 64 return DitherEffect::Create(); | 64 return DitherEffect::Create(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 ////////////////////////////////////////////////////////////////////////////// | 67 ////////////////////////////////////////////////////////////////////////////// |
| 68 | 68 |
| 69 class GLDitherEffect : public GrGLEffect { | 69 class GLDitherEffect : public GrGLEffect { |
| 70 public: | 70 public: |
| 71 GLDitherEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 71 GLDitherEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 72 | 72 |
| 73 virtual void emitCode(GrGLShaderBuilder* builder, | 73 virtual void emitCode(GrGLProgramBuilder* builder, |
| 74 const GrDrawEffect& drawEffect, | 74 const GrDrawEffect& drawEffect, |
| 75 const GrEffectKey& key, | 75 const GrEffectKey& key, |
| 76 const char* outputColor, | 76 const char* outputColor, |
| 77 const char* inputColor, | 77 const char* inputColor, |
| 78 const TransformedCoordsArray&, | 78 const TransformedCoordsArray&, |
| 79 const TextureSamplerArray&) SK_OVERRIDE; | 79 const TextureSamplerArray&) SK_OVERRIDE; |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 typedef GrGLEffect INHERITED; | 82 typedef GrGLEffect INHERITED; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 GLDitherEffect::GLDitherEffect(const GrBackendEffectFactory& factory, | 85 GLDitherEffect::GLDitherEffect(const GrBackendEffectFactory& factory, |
| 86 const GrDrawEffect& drawEffect) | 86 const GrDrawEffect& drawEffect) |
| 87 : INHERITED (factory) { | 87 : INHERITED (factory) { |
| 88 } | 88 } |
| 89 | 89 |
| 90 void GLDitherEffect::emitCode(GrGLShaderBuilder* builder, | 90 void GLDitherEffect::emitCode(GrGLProgramBuilder* builder, |
| 91 const GrDrawEffect& drawEffect, | 91 const GrDrawEffect& drawEffect, |
| 92 const GrEffectKey& key, | 92 const GrEffectKey& key, |
| 93 const char* outputColor, | 93 const char* outputColor, |
| 94 const char* inputColor, | 94 const char* inputColor, |
| 95 const TransformedCoordsArray&, | 95 const TransformedCoordsArray&, |
| 96 const TextureSamplerArray& samplers) { | 96 const TextureSamplerArray& samplers) { |
| 97 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 97 // Generate a random number based on the fragment position. For this | 98 // Generate a random number based on the fragment position. For this |
| 98 // random number generator, we use the "GLSL rand" function | 99 // random number generator, we use the "GLSL rand" function |
| 99 // that seems to be floating around on the internet. It works under | 100 // that seems to be floating around on the internet. It works under |
| 100 // the assumption that sin(<big number>) oscillates with high frequency | 101 // the assumption that sin(<big number>) oscillates with high frequency |
| 101 // and sampling it will generate "randomness". Since we're using this | 102 // and sampling it will generate "randomness". Since we're using this |
| 102 // for rendering and not cryptography it should be OK. | 103 // for rendering and not cryptography it should be OK. |
| 103 | 104 |
| 104 // For each channel c, add the random offset to the pixel to either bump | 105 // For each channel c, add the random offset to the pixel to either bump |
| 105 // it up or let it remain constant during quantization. | 106 // it up or let it remain constant during quantization. |
| 106 builder->fsCodeAppendf("\t\tfloat r = " | 107 fsBuilder->codeAppendf("\t\tfloat r = " |
| 107 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 108 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
| 108 builder->fragmentPosition()); | 109 fsBuilder->fragmentPosition()); |
| 109 builder->fsCodeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 110 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 110 outputColor, GrGLSLExpr4(inputColor).c_str()); | 111 outputColor, GrGLSLExpr4(inputColor).c_str()); |
| 111 } | 112 } |
| 112 | 113 |
| 113 ////////////////////////////////////////////////////////////////////////////// | 114 ////////////////////////////////////////////////////////////////////////////// |
| 114 | 115 |
| 115 GrEffect* GrDitherEffect::Create() { return DitherEffect::Create(); } | 116 GrEffect* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |