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