| 1 | /* |
| 2 | * Copyright 2018 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 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrAlphaThresholdFragmentProcessor.fp; do not modify. |
| 10 | **************************************************************************************************/ |
| 11 | #include "GrAlphaThresholdFragmentProcessor.h" |
| 12 | |
| 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; |
| 20 | } |
| 21 | } |
| 22 | #include "src/gpu/GrTexture.h" |
| 23 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 24 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 25 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 26 | #include "src/sksl/SkSLCPP.h" |
| 27 | #include "src/sksl/SkSLUtil.h" |
| 28 | class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor { |
| 29 | public: |
| 30 | GrGLSLAlphaThresholdFragmentProcessor() {} |
| 31 | void emitCode(EmitArgs& args) override { |
| 32 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 33 | const GrAlphaThresholdFragmentProcessor& _outer = |
| 34 | args.fFp.cast<GrAlphaThresholdFragmentProcessor>(); |
| 35 | (void)_outer; |
| 36 | auto innerThreshold = _outer.innerThreshold; |
| 37 | (void)innerThreshold; |
| 38 | auto outerThreshold = _outer.outerThreshold; |
| 39 | (void)outerThreshold; |
| 40 | innerThresholdVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 41 | kHalf_GrSLType, "innerThreshold" ); |
| 42 | outerThresholdVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 43 | kHalf_GrSLType, "outerThreshold" ); |
| 44 | SkString sk_TransformedCoords2D_0 = |
| 45 | fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint); |
| 46 | fragBuilder->codeAppendf( |
| 47 | "half4 color = %s;\nhalf4 mask_color = sample(%s, %s).%s;\nif (mask_color.w < 0.5) " |
| 48 | "{\n if (color.w > %s) {\n half scale = %s / color.w;\n color.xyz " |
| 49 | "*= scale;\n color.w = %s;\n }\n} else if (color.w < %s) {\n half " |
| 50 | "scale = %s / max(0.0010000000474974513, color.w);\n color.xyz *= scale;\n " |
| 51 | "color.w = %s;\n}\n%s = color;\n" , |
| 52 | args.fInputColor, |
| 53 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]), |
| 54 | sk_TransformedCoords2D_0.c_str(), |
| 55 | fragBuilder->getProgramBuilder() |
| 56 | ->samplerSwizzle(args.fTexSamplers[0]) |
| 57 | .asString() |
| 58 | .c_str(), |
| 59 | args.fUniformHandler->getUniformCStr(outerThresholdVar), |
| 60 | args.fUniformHandler->getUniformCStr(outerThresholdVar), |
| 61 | args.fUniformHandler->getUniformCStr(outerThresholdVar), |
| 62 | args.fUniformHandler->getUniformCStr(innerThresholdVar), |
| 63 | args.fUniformHandler->getUniformCStr(innerThresholdVar), |
| 64 | args.fUniformHandler->getUniformCStr(innerThresholdVar), args.fOutputColor); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 69 | const GrFragmentProcessor& _proc) override { |
| 70 | const GrAlphaThresholdFragmentProcessor& _outer = |
| 71 | _proc.cast<GrAlphaThresholdFragmentProcessor>(); |
| 72 | { |
| 73 | pdman.set1f(innerThresholdVar, (_outer.innerThreshold)); |
| 74 | pdman.set1f(outerThresholdVar, (_outer.outerThreshold)); |
| 75 | } |
| 76 | } |
| 77 | UniformHandle innerThresholdVar; |
| 78 | UniformHandle outerThresholdVar; |
| 79 | }; |
| 80 | GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const { |
| 81 | return new GrGLSLAlphaThresholdFragmentProcessor(); |
| 82 | } |
| 83 | void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 84 | GrProcessorKeyBuilder* b) const {} |
| 85 | bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const { |
| 86 | const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>(); |
| 87 | (void)that; |
| 88 | if (mask != that.mask) return false; |
| 89 | if (innerThreshold != that.innerThreshold) return false; |
| 90 | if (outerThreshold != that.outerThreshold) return false; |
| 91 | return true; |
| 92 | } |
| 93 | GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor( |
| 94 | const GrAlphaThresholdFragmentProcessor& src) |
| 95 | : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags()) |
| 96 | , maskCoordTransform(src.maskCoordTransform) |
| 97 | , mask(src.mask) |
| 98 | , innerThreshold(src.innerThreshold) |
| 99 | , outerThreshold(src.outerThreshold) { |
| 100 | this->setTextureSamplerCnt(1); |
| 101 | this->addCoordTransform(&maskCoordTransform); |
| 102 | } |
| 103 | std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const { |
| 104 | return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this)); |
| 105 | } |
| 106 | const GrFragmentProcessor::TextureSampler& GrAlphaThresholdFragmentProcessor::onTextureSampler( |
| 107 | int index) const { |
| 108 | return IthTextureSampler(index, mask); |
| 109 | } |
| 110 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor); |
| 111 | #if GR_TEST_UTILS |
| 112 | std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate( |
| 113 | GrProcessorTestData* testData) { |
| 114 | auto[maskView, ct, at] = testData->randomAlphaOnlyView(); |
| 115 | // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly. |
| 116 | float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 117 | float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 118 | const int kMaxWidth = 1000; |
| 119 | const int kMaxHeight = 1000; |
| 120 | uint32_t width = testData->fRandom->nextULessThan(kMaxWidth); |
| 121 | uint32_t height = testData->fRandom->nextULessThan(kMaxHeight); |
| 122 | uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width); |
| 123 | uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height); |
| 124 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
| 125 | |
| 126 | return GrAlphaThresholdFragmentProcessor::Make(std::move(maskView), innerThresh, outerThresh, |
| 127 | bounds); |
| 128 | } |
| 129 | #endif |
| 130 | |