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 | 9 |
10 #include "gl/GrGLEffect.h" | 10 #include "gl/GrGLEffect.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(GrGLShaderBuilder* builder, |
74 const GrDrawEffect& drawEffect, | 74 const GrDrawEffect& drawEffect, |
75 EffectKey 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(GrGLShaderBuilder* builder, |
91 const GrDrawEffect& drawEffect, | 91 const GrDrawEffect& drawEffect, |
92 EffectKey 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 // Generate a random number based on the fragment position. For this | 97 // Generate a random number based on the fragment position. For this |
98 // random number generator, we use the "GLSL rand" function | 98 // random number generator, we use the "GLSL rand" function |
99 // that seems to be floating around on the internet. It works under | 99 // that seems to be floating around on the internet. It works under |
100 // the assumption that sin(<big number>) oscillates with high frequency | 100 // the assumption that sin(<big number>) oscillates with high frequency |
101 // and sampling it will generate "randomness". Since we're using this | 101 // and sampling it will generate "randomness". Since we're using this |
102 // for rendering and not cryptography it should be OK. | 102 // for rendering and not cryptography it should be OK. |
103 | 103 |
104 // For each channel c, add the random offset to the pixel to either bump | 104 // For each channel c, add the random offset to the pixel to either bump |
105 // it up or let it remain constant during quantization. | 105 // it up or let it remain constant during quantization. |
106 builder->fsCodeAppendf("\t\tfloat r = " | 106 builder->fsCodeAppendf("\t\tfloat r = " |
107 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 107 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
108 builder->fragmentPosition()); | 108 builder->fragmentPosition()); |
109 builder->fsCodeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 109 builder->fsCodeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
110 outputColor, GrGLSLExpr4(inputColor).c_str()); | 110 outputColor, GrGLSLExpr4(inputColor).c_str()); |
111 } | 111 } |
112 | 112 |
113 ////////////////////////////////////////////////////////////////////////////// | 113 ////////////////////////////////////////////////////////////////////////////// |
114 | 114 |
115 GrEffect* GrDitherEffect::Create() { return DitherEffect::Create(); } | 115 GrEffect* GrDitherEffect::Create() { return DitherEffect::Create(); } |
OLD | NEW |