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