blob: f174ff5285ffb83b084735af4fb81d2af3053b4c [file] [log] [blame]
robertphillipsf7142e72016-04-18 07:20:05 -07001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
robertphillipsf7142e72016-04-18 07:20:05 -07003 *
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 Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrAlphaThresholdFragmentProcessor.fp; do not modify.
10 **************************************************************************************************/
robertphillipsf7142e72016-04-18 07:20:05 -070011#include "GrAlphaThresholdFragmentProcessor.h"
Ethan Nicholasceb4d482017-07-10 15:40:20 -040012#if SK_SUPPORT_GPU
robertphillipsf7142e72016-04-18 07:20:05 -070013
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040014inline 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 Nicholas9fb036f2017-07-05 16:19:09 -040021 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040022}
robertphillipsf7142e72016-04-18 07:20:05 -070023#include "glsl/GrGLSLFragmentProcessor.h"
24#include "glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040025#include "glsl/GrGLSLProgramBuilder.h"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050026#include "GrTexture.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040027#include "SkSLCPP.h"
28#include "SkSLUtil.h"
29class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor {
robertphillipsf7142e72016-04-18 07:20:05 -070030public:
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040031 GrGLSLAlphaThresholdFragmentProcessor() {}
32 void emitCode(EmitArgs& args) override {
33 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040034 const GrAlphaThresholdFragmentProcessor& _outer =
35 args.fFp.cast<GrAlphaThresholdFragmentProcessor>();
36 (void)_outer;
Ethan Nicholas82399462017-10-16 12:35:44 -040037 auto innerThreshold = _outer.innerThreshold();
38 (void)innerThreshold;
39 auto outerThreshold = _outer.outerThreshold();
40 (void)outerThreshold;
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040041 fInnerThresholdVar = args.fUniformHandler->addUniform(
Ethan Nicholasf7b88202017-09-18 14:10:39 -040042 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040043 fOuterThresholdVar = args.fUniformHandler->addUniform(
Ethan Nicholasf7b88202017-09-18 14:10:39 -040044 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
Brian Osman72a37be2017-08-15 09:19:53 -040045 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040046 fragBuilder->codeAppendf(
Brian Osmana4aa1332017-10-19 12:54:28 -040047 "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 Nicholasb7e8c3b2017-07-19 13:54:20 -040053 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
54 sk_TransformedCoords2D_0.c_str(),
55 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040056 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 Osman6fb592e2016-10-03 13:15:12 -040062 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040063
robertphillipsf7142e72016-04-18 07:20:05 -070064private:
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040065 void onSetData(const GrGLSLProgramDataManager& pdman,
66 const GrFragmentProcessor& _proc) override {
67 const GrAlphaThresholdFragmentProcessor& _outer =
68 _proc.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040069 {
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040070 pdman.set1f(fInnerThresholdVar, _outer.innerThreshold());
71 pdman.set1f(fOuterThresholdVar, _outer.outerThreshold());
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040072 }
Brian Osman6fb592e2016-10-03 13:15:12 -040073 }
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040074 UniformHandle fInnerThresholdVar;
75 UniformHandle fOuterThresholdVar;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040076};
77GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const {
78 return new GrGLSLAlphaThresholdFragmentProcessor();
robertphillipsf7142e72016-04-18 07:20:05 -070079}
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040080void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Brian Osmana4aa1332017-10-19 12:54:28 -040081 GrProcessorKeyBuilder* b) const {}
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040082bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
83 const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040084 (void)that;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040085 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 Nicholasf57c0d62017-07-31 11:18:22 -040090GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
91 const GrAlphaThresholdFragmentProcessor& src)
Ethan Nicholasabff9562017-10-09 10:54:08 -040092 : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags())
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040093 , fMask(src.fMask)
94 , fInnerThreshold(src.fInnerThreshold)
95 , fOuterThreshold(src.fOuterThreshold)
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040096 , fMaskCoordTransform(src.fMaskCoordTransform) {
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040097 this->addTextureSampler(&fMask);
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040098 this->addCoordTransform(&fMaskCoordTransform);
99}
Brian Salomonaff329b2017-08-11 09:40:37 -0400100std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const {
101 return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this));
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400102}
robertphillipsf7142e72016-04-18 07:20:05 -0700103GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor);
Hal Canary6f6961e2017-01-31 13:50:44 -0500104#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400105std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400106 GrProcessorTestData* testData) {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400107 sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700108 // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly.
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400109 float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
110 float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
robertphillipsf7142e72016-04-18 07:20:05 -0700111 const int kMaxWidth = 1000;
112 const int kMaxHeight = 1000;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400113 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);
robertphillipsf7142e72016-04-18 07:20:05 -0700117 SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
Brian Osmana4aa1332017-10-19 12:54:28 -0400118 return GrAlphaThresholdFragmentProcessor::Make(std::move(maskProxy), innerThresh, outerThresh,
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400119 bounds);
robertphillipsf7142e72016-04-18 07:20:05 -0700120}
Hal Canary6f6961e2017-01-31 13:50:44 -0500121#endif
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400122#endif