robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrAlphaThresholdFragmentProcessor.fp; do not modify. |
| 10 | **************************************************************************************************/ |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 11 | #include "GrAlphaThresholdFragmentProcessor.h" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 13 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 14 | inline GrFragmentProcessor::OptimizationFlags GrAlphaThresholdFragmentProcessor::optFlags( |
| 15 | float outerThreshold) { |
| 16 | if (outerThreshold >= 1.0) { |
| 17 | return kPreservesOpaqueInput_OptimizationFlag | |
| 18 | kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 19 | } else { |
| 20 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 21 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 22 | } |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 23 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 24 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 25 | #include "glsl/GrGLSLProgramBuilder.h" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 26 | #include "GrTexture.h" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 27 | #include "SkSLCPP.h" |
| 28 | #include "SkSLUtil.h" |
| 29 | class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor { |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 30 | public: |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 31 | GrGLSLAlphaThresholdFragmentProcessor() {} |
| 32 | void emitCode(EmitArgs& args) override { |
| 33 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 34 | const GrAlphaThresholdFragmentProcessor& _outer = |
| 35 | args.fFp.cast<GrAlphaThresholdFragmentProcessor>(); |
| 36 | (void)_outer; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 37 | auto innerThreshold = _outer.innerThreshold(); |
| 38 | (void)innerThreshold; |
| 39 | auto outerThreshold = _outer.outerThreshold(); |
| 40 | (void)outerThreshold; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 41 | fInnerThresholdVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 42 | kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "innerThreshold"); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 43 | fOuterThresholdVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 44 | kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "outerThreshold"); |
Brian Osman | 72a37be | 2017-08-15 09:19:53 -0400 | [diff] [blame] | 45 | SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 46 | fragBuilder->codeAppendf( |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 47 | "half4 color = %s;\nhalf4 mask_color = texture(%s, %s).%s;\nif " |
| 48 | "(float(mask_color.w) < 0.5) {\n if (color.w > %s) {\n half scale = %s / " |
| 49 | "color.w;\n color.xyz *= scale;\n color.w = %s;\n }\n} else if " |
| 50 | "(color.w < %s) {\n half scale = float(%s) / max(0.001, float(color.w));\n " |
| 51 | "color.xyz *= scale;\n color.w = %s;\n}\n%s = color;\n", |
| 52 | args.fInputColor ? args.fInputColor : "half4(1)", |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 53 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), |
| 54 | sk_TransformedCoords2D_0.c_str(), |
| 55 | fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 56 | args.fUniformHandler->getUniformCStr(fOuterThresholdVar), |
| 57 | args.fUniformHandler->getUniformCStr(fOuterThresholdVar), |
| 58 | args.fUniformHandler->getUniformCStr(fOuterThresholdVar), |
| 59 | args.fUniformHandler->getUniformCStr(fInnerThresholdVar), |
| 60 | args.fUniformHandler->getUniformCStr(fInnerThresholdVar), |
| 61 | args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fOutputColor); |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 62 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 63 | |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 64 | private: |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 65 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 66 | const GrFragmentProcessor& _proc) override { |
| 67 | const GrAlphaThresholdFragmentProcessor& _outer = |
| 68 | _proc.cast<GrAlphaThresholdFragmentProcessor>(); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 69 | { |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 70 | pdman.set1f(fInnerThresholdVar, _outer.innerThreshold()); |
| 71 | pdman.set1f(fOuterThresholdVar, _outer.outerThreshold()); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 72 | } |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 73 | } |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 74 | UniformHandle fInnerThresholdVar; |
| 75 | UniformHandle fOuterThresholdVar; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 76 | }; |
| 77 | GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const { |
| 78 | return new GrGLSLAlphaThresholdFragmentProcessor(); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 79 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 80 | void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 81 | GrProcessorKeyBuilder* b) const {} |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 82 | bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const { |
| 83 | const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 84 | (void)that; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 85 | if (fMask != that.fMask) return false; |
| 86 | if (fInnerThreshold != that.fInnerThreshold) return false; |
| 87 | if (fOuterThreshold != that.fOuterThreshold) return false; |
| 88 | return true; |
| 89 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 90 | GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor( |
| 91 | const GrAlphaThresholdFragmentProcessor& src) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 92 | : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags()) |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 93 | , fMask(src.fMask) |
| 94 | , fInnerThreshold(src.fInnerThreshold) |
| 95 | , fOuterThreshold(src.fOuterThreshold) |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 96 | , fMaskCoordTransform(src.fMaskCoordTransform) { |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 97 | this->addTextureSampler(&fMask); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 98 | this->addCoordTransform(&fMaskCoordTransform); |
| 99 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 100 | std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const { |
| 101 | return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this)); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 102 | } |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 103 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor); |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 104 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 105 | std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate( |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 106 | GrProcessorTestData* testData) { |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 107 | sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 108 | // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly. |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 109 | float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 110 | float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 111 | const int kMaxWidth = 1000; |
| 112 | const int kMaxHeight = 1000; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 113 | uint32_t width = testData->fRandom->nextULessThan(kMaxWidth); |
| 114 | uint32_t height = testData->fRandom->nextULessThan(kMaxHeight); |
| 115 | uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width); |
| 116 | uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 117 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 118 | return GrAlphaThresholdFragmentProcessor::Make(std::move(maskProxy), innerThresh, outerThresh, |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 119 | bounds); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 120 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 121 | #endif |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 122 | #endif |