blob: aebb7d35e87d7ce1c4b5223490cf73490ba34e1a [file] [log] [blame]
robertphillipsf7142e72016-04-18 07:20:05 -07001/*
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04002 * Copyright 2017 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 Nicholas9fb036f2017-07-05 16:19:09 -04008/*
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}
Brian Osman6fb592e2016-10-03 13:15:12 -040023#include "glsl/GrGLSLColorSpaceXformHelper.h"
robertphillipsf7142e72016-04-18 07:20:05 -070024#include "glsl/GrGLSLFragmentProcessor.h"
25#include "glsl/GrGLSLFragmentShaderBuilder.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040026#include "glsl/GrGLSLProgramBuilder.h"
27#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 Nicholas9fb036f2017-07-05 16:19:09 -040037 fColorSpaceHelper.emitCode(args.fUniformHandler, _outer.colorXform().get());
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040038 fInnerThresholdVar = args.fUniformHandler->addUniform(
Brian Salomon1d816b92017-08-17 11:07:59 -040039 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040040 fOuterThresholdVar = args.fUniformHandler->addUniform(
Brian Salomon1d816b92017-08-17 11:07:59 -040041 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
Brian Osman72a37be2017-08-15 09:19:53 -040042 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
43 SkString sk_TransformedCoords2D_1 = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040044 fragBuilder->codeAppendf(
Brian Salomon1d816b92017-08-17 11:07:59 -040045 "float4 _tmpVar1;float4 color = %stexture(%s, %s).%s%s;\nfloat4 mask_color = "
46 "texture(%s, %s).%s;\nif (mask_color.w < 0.5) {\n if (color.w > %s) {\n "
47 "float scale = %s / color.w;\n color.xyz *= scale;\n color.w = %s;\n "
48 " }\n} else if (color.w < %s) {\n float scale = %s / max(0.001, color.w);\n "
49 " color.xyz *= scale;\n color.w = %s;\n}\n%s = color;\n",
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040050 fColorSpaceHelper.isValid() ? "(_tmpVar1 = " : "",
51 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
52 sk_TransformedCoords2D_0.c_str(),
53 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
54 fColorSpaceHelper.isValid()
Brian Salomon1d816b92017-08-17 11:07:59 -040055 ? SkStringPrintf(", float4(clamp((%s * float4(_tmpVar1.rgb, 1.0)).rgb, "
56 "0.0, _tmpVar1.a), _tmpVar1.a))",
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040057 args.fUniformHandler->getUniformCStr(
58 fColorSpaceHelper.gamutXformUniform()))
59 .c_str()
60 : "",
61 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[1]).c_str(),
62 sk_TransformedCoords2D_1.c_str(),
63 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[1]).c_str(),
64 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
65 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
66 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
67 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
68 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
69 args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fOutputColor);
Brian Osman6fb592e2016-10-03 13:15:12 -040070 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040071
robertphillipsf7142e72016-04-18 07:20:05 -070072private:
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040073 void onSetData(const GrGLSLProgramDataManager& pdman,
74 const GrFragmentProcessor& _proc) override {
75 const GrAlphaThresholdFragmentProcessor& _outer =
76 _proc.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040077 {
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040078 if (fColorSpaceHelper.isValid()) {
79 fColorSpaceHelper.setData(pdman, _outer.colorXform().get());
80 }
81 pdman.set1f(fInnerThresholdVar, _outer.innerThreshold());
82 pdman.set1f(fOuterThresholdVar, _outer.outerThreshold());
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040083 }
Brian Osman6fb592e2016-10-03 13:15:12 -040084 }
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040085 UniformHandle fImageVar;
86 UniformHandle fMaskVar;
87 UniformHandle fInnerThresholdVar;
88 UniformHandle fOuterThresholdVar;
89 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
90};
91GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const {
92 return new GrGLSLAlphaThresholdFragmentProcessor();
robertphillipsf7142e72016-04-18 07:20:05 -070093}
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040094void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
95 GrProcessorKeyBuilder* b) const {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040096 b->add32(GrColorSpaceXform::XformKey(fColorXform.get()));
97}
98bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
99 const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400100 (void)that;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400101 if (fImage != that.fImage) return false;
102 if (fColorXform != that.fColorXform) return false;
103 if (fMask != that.fMask) return false;
104 if (fInnerThreshold != that.fInnerThreshold) return false;
105 if (fOuterThreshold != that.fOuterThreshold) return false;
106 return true;
107}
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400108GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
109 const GrAlphaThresholdFragmentProcessor& src)
110 : INHERITED(src.optimizationFlags())
111 , fImage(src.fImage)
112 , fColorXform(src.fColorXform)
113 , fMask(src.fMask)
114 , fInnerThreshold(src.fInnerThreshold)
115 , fOuterThreshold(src.fOuterThreshold)
116 , fImageCoordTransform(src.fImageCoordTransform)
117 , fMaskCoordTransform(src.fMaskCoordTransform) {
118 this->initClassID<GrAlphaThresholdFragmentProcessor>();
119 this->addTextureSampler(&fImage);
120 this->addTextureSampler(&fMask);
121 this->addCoordTransform(&fImageCoordTransform);
122 this->addCoordTransform(&fMaskCoordTransform);
123}
Brian Salomonaff329b2017-08-11 09:40:37 -0400124std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const {
125 return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this));
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400126}
robertphillipsf7142e72016-04-18 07:20:05 -0700127GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor);
Hal Canary6f6961e2017-01-31 13:50:44 -0500128#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400129std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400130 GrProcessorTestData* testData) {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400131 sk_sp<GrTextureProxy> bmpProxy = testData->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx);
132 sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx);
Ethan Nicholasc576e932017-09-07 15:44:01 -0400133 // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly.
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400134 float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
135 float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
robertphillipsf7142e72016-04-18 07:20:05 -0700136 const int kMaxWidth = 1000;
137 const int kMaxHeight = 1000;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400138 uint32_t width = testData->fRandom->nextULessThan(kMaxWidth);
139 uint32_t height = testData->fRandom->nextULessThan(kMaxHeight);
140 uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width);
141 uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height);
robertphillipsf7142e72016-04-18 07:20:05 -0700142 SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400143 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(testData->fRandom);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400144 return GrAlphaThresholdFragmentProcessor::Make(std::move(bmpProxy), colorSpaceXform,
145 std::move(maskProxy), innerThresh, outerThresh,
146 bounds);
robertphillipsf7142e72016-04-18 07:20:05 -0700147}
Hal Canary6f6961e2017-01-31 13:50:44 -0500148#endif
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400149#endif