blob: fab78cea9d45b9b1da74bae77c85dee1ffb10e29 [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(
39 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
40 fOuterThresholdVar = args.fUniformHandler->addUniform(
41 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
42 SkSL::String sk_TransformedCoords2D_0 =
43 fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
44 SkSL::String sk_TransformedCoords2D_1 =
45 fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
46 fragBuilder->codeAppendf(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040047 "float4 _tmpVar1;float4 color = %stexture(%s, %s).%s%s;\nfloat4 mask_color = "
48 "texture(%s, %s).%s;\nif (mask_color.w < 0.5) {\n if (color.w > %s) {\n "
49 "float scale = %s / color.w;\n color.xyz *= scale;\n color.w = %s;\n "
50 " }\n} else if (color.w < %s) {\n float scale = %s / max(0.001, color.w);\n "
51 " color.xyz *= scale;\n color.w = %s;\n}\n%s = color;\n",
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040052 fColorSpaceHelper.isValid() ? "(_tmpVar1 = " : "",
53 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
54 sk_TransformedCoords2D_0.c_str(),
55 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
56 fColorSpaceHelper.isValid()
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040057 ? SkStringPrintf(", float4(clamp((%s * float4(_tmpVar1.rgb, 1.0)).rgb, "
58 "0.0, _tmpVar1.a), _tmpVar1.a))",
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040059 args.fUniformHandler->getUniformCStr(
60 fColorSpaceHelper.gamutXformUniform()))
61 .c_str()
62 : "",
63 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[1]).c_str(),
64 sk_TransformedCoords2D_1.c_str(),
65 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[1]).c_str(),
66 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
67 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
68 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
69 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
70 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
71 args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fOutputColor);
Brian Osman6fb592e2016-10-03 13:15:12 -040072 }
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040073
robertphillipsf7142e72016-04-18 07:20:05 -070074private:
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040075 void onSetData(const GrGLSLProgramDataManager& pdman,
76 const GrFragmentProcessor& _proc) override {
77 const GrAlphaThresholdFragmentProcessor& _outer =
78 _proc.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040079 {
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040080 if (fColorSpaceHelper.isValid()) {
81 fColorSpaceHelper.setData(pdman, _outer.colorXform().get());
82 }
83 pdman.set1f(fInnerThresholdVar, _outer.innerThreshold());
84 pdman.set1f(fOuterThresholdVar, _outer.outerThreshold());
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040085 }
Brian Osman6fb592e2016-10-03 13:15:12 -040086 }
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040087 UniformHandle fImageVar;
88 UniformHandle fMaskVar;
89 UniformHandle fInnerThresholdVar;
90 UniformHandle fOuterThresholdVar;
91 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
92};
93GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const {
94 return new GrGLSLAlphaThresholdFragmentProcessor();
robertphillipsf7142e72016-04-18 07:20:05 -070095}
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -040096void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
97 GrProcessorKeyBuilder* b) const {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040098 b->add32(GrColorSpaceXform::XformKey(fColorXform.get()));
99}
100bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
101 const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>();
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400102 (void)that;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400103 if (fImage != that.fImage) return false;
104 if (fColorXform != that.fColorXform) return false;
105 if (fMask != that.fMask) return false;
106 if (fInnerThreshold != that.fInnerThreshold) return false;
107 if (fOuterThreshold != that.fOuterThreshold) return false;
108 return true;
109}
robertphillipsf7142e72016-04-18 07:20:05 -0700110GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor);
Hal Canary6f6961e2017-01-31 13:50:44 -0500111#if GR_TEST_UTILS
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400112sk_sp<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(
113 GrProcessorTestData* testData) {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400114 sk_sp<GrTextureProxy> bmpProxy = testData->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx);
115 sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400116
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400117 float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
118 float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
robertphillipsf7142e72016-04-18 07:20:05 -0700119 const int kMaxWidth = 1000;
120 const int kMaxHeight = 1000;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400121 uint32_t width = testData->fRandom->nextULessThan(kMaxWidth);
122 uint32_t height = testData->fRandom->nextULessThan(kMaxHeight);
123 uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width);
124 uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height);
robertphillipsf7142e72016-04-18 07:20:05 -0700125 SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
Ethan Nicholas9fb036f2017-07-05 16:19:09 -0400126 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(testData->fRandom);
Ethan Nicholasb7e8c3b2017-07-19 13:54:20 -0400127 return GrAlphaThresholdFragmentProcessor::Make(std::move(bmpProxy), colorSpaceXform,
128 std::move(maskProxy), innerThresh, outerThresh,
129 bounds);
robertphillipsf7142e72016-04-18 07:20:05 -0700130}
Hal Canary6f6961e2017-01-31 13:50:44 -0500131#endif
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400132#endif