Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 9 | |
| 10 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 12 | |
| 13 | class GLSLBlurredEdgeFP : public GrGLSLFragmentProcessor { |
| 14 | public: |
| 15 | GLSLBlurredEdgeFP() {} |
| 16 | |
| 17 | void emitCode(EmitArgs& args) override { |
| 18 | |
| 19 | GrBlurredEdgeFP::Mode mode = args.fFp.cast<GrBlurredEdgeFP>().mode(); |
| 20 | |
| 21 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 22 | |
| 23 | fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); |
| 24 | if (!args.fGpImplementsDistanceVector) { |
| 25 | fragBuilder->codeAppendf("// assuming interpolant is set in vertex colors\n"); |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame^] | 26 | fragBuilder->codeAppendf("float factor = 1.0 - color.a;"); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 27 | } else { |
| 28 | fragBuilder->codeAppendf("// using distance to edge to compute interpolant\n"); |
| 29 | fragBuilder->codeAppend("float radius = color.r*256.0*64.0 + color.g*64.0;"); |
| 30 | fragBuilder->codeAppend("float pad = color.b*64.0;"); |
| 31 | |
| 32 | fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad)/radius, 0.0, 1.0);", |
| 33 | fragBuilder->distanceVectorName()); |
| 34 | } |
| 35 | switch (mode) { |
| 36 | case GrBlurredEdgeFP::kGaussian_Mode: |
| 37 | fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;"); |
| 38 | break; |
| 39 | case GrBlurredEdgeFP::kSmoothstep_Mode: |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame^] | 40 | fragBuilder->codeAppend("factor = smoothstep(1.0, 0.0, factor);"); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 41 | break; |
| 42 | } |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame^] | 43 | fragBuilder->codeAppendf("%s = vec4(factor);", args.fOutputColor); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 47 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 48 | const GrFragmentProcessor& proc) override {} |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 49 | |
| 50 | GrBlurredEdgeFP::Mode fMode; |
| 51 | }; |
| 52 | |
| 53 | GrGLSLFragmentProcessor* GrBlurredEdgeFP::onCreateGLSLInstance() const { |
| 54 | return new GLSLBlurredEdgeFP(); |
| 55 | } |
| 56 | |
| 57 | void GrBlurredEdgeFP::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 58 | GrProcessorKeyBuilder* b) const { |
| 59 | b->add32(fMode); |
| 60 | } |
| 61 | |
| 62 | bool GrBlurredEdgeFP::onIsEqual(const GrFragmentProcessor& other) const { |
| 63 | const GrBlurredEdgeFP& that = other.cast<GrBlurredEdgeFP>(); |
| 64 | return that.fMode == fMode; |
| 65 | } |
| 66 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 67 | |