blob: 3a13123e675cfcc3024c2e6c2a28166420555c0b [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"
12
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040013inline 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 Nicholas9fb036f2017-07-05 16:19:09 -040020 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040021}
robertphillipsf7142e72016-04-18 07:20:05 -070022#include "glsl/GrGLSLFragmentProcessor.h"
23#include "glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040024#include "glsl/GrGLSLProgramBuilder.h"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050025#include "GrTexture.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040026#include "SkSLCPP.h"
27#include "SkSLUtil.h"
28class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor {
robertphillipsf7142e72016-04-18 07:20:05 -070029public:
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040030 GrGLSLAlphaThresholdFragmentProcessor() {}
31 void emitCode(EmitArgs& args) override {
32 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040033 const GrAlphaThresholdFragmentProcessor& _outer =
34 args.fFp.cast<GrAlphaThresholdFragmentProcessor>();
35 (void)_outer;
Ethan Nicholas82399462017-10-16 12:35:44 -040036 auto innerThreshold = _outer.innerThreshold();
37 (void)innerThreshold;
38 auto outerThreshold = _outer.outerThreshold();
39 (void)outerThreshold;
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040040 fInnerThresholdVar = args.fUniformHandler->addUniform(
Ethan Nicholasf7b88202017-09-18 14:10:39 -040041 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040042 fOuterThresholdVar = args.fUniformHandler->addUniform(
Ethan Nicholasf7b88202017-09-18 14:10:39 -040043 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
Brian Osman72a37be2017-08-15 09:19:53 -040044 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040045 fragBuilder->codeAppendf(
Brian Osmana4aa1332017-10-19 12:54:28 -040046 "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 Ludwig231de032018-08-30 14:33:01 -040051 args.fInputColor,
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040052 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
53 sk_TransformedCoords2D_0.c_str(),
54 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040055 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 Osman6fb592e2016-10-03 13:15:12 -040061 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040062
robertphillipsf7142e72016-04-18 07:20:05 -070063private:
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040064 void onSetData(const GrGLSLProgramDataManager& pdman,
65 const GrFragmentProcessor& _proc) override {
66 const GrAlphaThresholdFragmentProcessor& _outer =
67 _proc.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040068 {
Michael Ludwiga4275592018-08-31 10:52:47 -040069 pdman.set1f(fInnerThresholdVar, (_outer.innerThreshold()));
70 pdman.set1f(fOuterThresholdVar, (_outer.outerThreshold()));
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040071 }
Brian Osman6fb592e2016-10-03 13:15:12 -040072 }
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040073 UniformHandle fInnerThresholdVar;
74 UniformHandle fOuterThresholdVar;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040075};
76GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const {
77 return new GrGLSLAlphaThresholdFragmentProcessor();
robertphillipsf7142e72016-04-18 07:20:05 -070078}
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040079void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Brian Osmana4aa1332017-10-19 12:54:28 -040080 GrProcessorKeyBuilder* b) const {}
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040081bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
82 const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040083 (void)that;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040084 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 Nicholasf57c0d62017-07-31 11:18:22 -040089GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
90 const GrAlphaThresholdFragmentProcessor& src)
Ethan Nicholasabff9562017-10-09 10:54:08 -040091 : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags())
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040092 , fMask(src.fMask)
93 , fInnerThreshold(src.fInnerThreshold)
94 , fOuterThreshold(src.fOuterThreshold)
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040095 , fMaskCoordTransform(src.fMaskCoordTransform) {
Brian Salomonf7dcd762018-07-30 14:48:15 -040096 this->setTextureSamplerCnt(1);
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040097 this->addCoordTransform(&fMaskCoordTransform);
98}
Brian Salomonaff329b2017-08-11 09:40:37 -040099std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const {
100 return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this));
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400101}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400102const GrFragmentProcessor::TextureSampler& GrAlphaThresholdFragmentProcessor::onTextureSampler(
103 int index) const {
104 return IthTextureSampler(index, fMask);
105}
robertphillipsf7142e72016-04-18 07:20:05 -0700106GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor);
Hal Canary6f6961e2017-01-31 13:50:44 -0500107#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400108std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400109 GrProcessorTestData* testData) {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400110 sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700111 // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly.
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400112 float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
113 float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
robertphillipsf7142e72016-04-18 07:20:05 -0700114 const int kMaxWidth = 1000;
115 const int kMaxHeight = 1000;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400116 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);
robertphillipsf7142e72016-04-18 07:20:05 -0700120 SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
Brian Osmana4aa1332017-10-19 12:54:28 -0400121 return GrAlphaThresholdFragmentProcessor::Make(std::move(maskProxy), innerThresh, outerThresh,
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400122 bounds);
robertphillipsf7142e72016-04-18 07:20:05 -0700123}
Hal Canary6f6961e2017-01-31 13:50:44 -0500124#endif