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