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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 GLDitherEffect::GLDitherEffect(const GrProcessor&) { | 81 GLDitherEffect::GLDitherEffect(const GrProcessor&) { |
82 } | 82 } |
83 | 83 |
84 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, | 84 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, |
85 const GrFragmentProcessor& fp, | 85 const GrFragmentProcessor& fp, |
86 const char* outputColor, | 86 const char* outputColor, |
87 const char* inputColor, | 87 const char* inputColor, |
88 const TransformedCoordsArray&, | 88 const TransformedCoordsArray&, |
89 const TextureSamplerArray& samplers) { | 89 const TextureSamplerArray& samplers) { |
90 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 90 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
91 // Generate a random number based on the fragment position. For this | 91 // Generate a random number based on the fragment position. For this |
92 // random number generator, we use the "GLSL rand" function | 92 // random number generator, we use the "GLSL rand" function |
93 // 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 |
94 // the assumption that sin(<big number>) oscillates with high frequency | 94 // the assumption that sin(<big number>) oscillates with high frequency |
95 // and sampling it will generate "randomness". Since we're using this | 95 // and sampling it will generate "randomness". Since we're using this |
96 // for rendering and not cryptography it should be OK. | 96 // for rendering and not cryptography it should be OK. |
97 | 97 |
98 // 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 |
99 // it up or let it remain constant during quantization. | 99 // it up or let it remain constant during quantization. |
100 fsBuilder->codeAppendf("\t\tfloat r = " | 100 fsBuilder->codeAppendf("\t\tfloat r = " |
101 "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", |
102 fsBuilder->fragmentPosition()); | 102 fsBuilder->fragmentPosition()); |
103 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", |
104 outputColor, GrGLSLExpr4(inputColor).c_str()); | 104 outputColor, GrGLSLExpr4(inputColor).c_str()); |
105 } | 105 } |
106 | 106 |
107 ////////////////////////////////////////////////////////////////////////////// | 107 ////////////////////////////////////////////////////////////////////////////// |
108 | 108 |
109 void DitherEffect::getGLProcessorKey(const GrGLSLCaps& caps, | 109 void DitherEffect::getGLProcessorKey(const GrGLSLCaps& caps, |
110 GrProcessorKeyBuilder* b) const { | 110 GrProcessorKeyBuilder* b) const { |
111 GLDitherEffect::GenKey(*this, caps, b); | 111 GLDitherEffect::GenKey(*this, caps, b); |
112 } | 112 } |
113 | 113 |
114 GrGLFragmentProcessor* DitherEffect::createGLInstance() const { | 114 GrGLFragmentProcessor* DitherEffect::createGLInstance() const { |
115 return SkNEW_ARGS(GLDitherEffect, (*this)); | 115 return SkNEW_ARGS(GLDitherEffect, (*this)); |
116 } | 116 } |
117 | 117 |
118 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } | 118 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
OLD | NEW |