robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 2 | * Copyright 2017 Google Inc. |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 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 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 8 | /* |
| 9 | * This file was autogenerated from GrAlphaThresholdFragmentProcessor.fp; do not modify. |
| 10 | */ |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 11 | #include "GrAlphaThresholdFragmentProcessor.h" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 13 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 14 | inline 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; |
| 21 | } |
| 22 | } |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 23 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 25 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramBuilder.h" |
| 27 | #include "SkSLCPP.h" |
| 28 | #include "SkSLUtil.h" |
| 29 | class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor { |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 30 | public: |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 31 | GrGLSLAlphaThresholdFragmentProcessor() {} |
| 32 | void emitCode(EmitArgs& args) override { |
| 33 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 34 | const GrAlphaThresholdFragmentProcessor& _outer = args.fFp.cast<GrAlphaThresholdFragmentProcessor>(); |
| 35 | (void) _outer; |
| 36 | fColorSpaceHelper.emitCode(args.fUniformHandler, _outer.colorXform().get()); |
| 37 | fInnerThresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "innerThreshold"); |
| 38 | fOuterThresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "outerThreshold"); |
| 39 | SkSL::String sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 40 | SkSL::String sk_TransformedCoords2D_1 = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 41 | fragBuilder->codeAppendf("vec4 _tmp0;\nvec4 color = (_tmp0 = texture(%s, %s).%s , %s != mat4(1.0) ? vec4(clamp((%s * vec4(_tmp0.xyz, 1.0)).xyz, 0.0, _tmp0.w), _tmp0.w) : _tmp0);\nvec4 mask_color = texture(%s, %s).%s;\nif (mask_color.w < 0.5) {\n if (color.w > %s) {\n float scale = %s / color.w;\n color.xyz *= scale;\n color.w = %s;\n }\n} else if (color.w < %s) {\n float scale = %s / max(0.001, color.w);\n color.xyz *= scale;\n color.w = %s;\n}\n%s = color;\n", fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), sk_TransformedCoords2D_0.c_str(), fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), fColorSpaceHelper.isValid() ? args.fUniformHandler->getUniformCStr(fColorSpaceHelper.gamutXformUniform()) : "mat4(1.0)", fColorSpaceHelper.isValid() ? args.fUniformHandler->getUniformCStr(fColorSpaceHelper.gamutXformUniform()) : "mat4(1.0)", fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[1]).c_str(), sk_TransformedCoords2D_1.c_str(), fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[1]).c_str(), args.fUniformHandler->getUniformCStr(fOuterThresholdVar), args.fUniformHandler->getUniformCStr(fOuterThresholdVar), args.fUniformHandler->getUniformCStr(fOuterThresholdVar), args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fOutputColor); |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 42 | } |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 43 | private: |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 44 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& _proc) override { |
| 45 | const GrAlphaThresholdFragmentProcessor& _outer = _proc.cast<GrAlphaThresholdFragmentProcessor>(); |
| 46 | { |
| 47 | if (fColorSpaceHelper.isValid()) { |
| 48 | fColorSpaceHelper.setData(pdman, _outer.colorXform().get()); |
| 49 | } |
| 50 | pdman.set1f(fInnerThresholdVar, _outer.innerThreshold()); |
| 51 | pdman.set1f(fOuterThresholdVar, _outer.outerThreshold()); |
| 52 | } |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 53 | } |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 54 | UniformHandle fImageVar; |
| 55 | UniformHandle fMaskVar; |
| 56 | UniformHandle fInnerThresholdVar; |
| 57 | UniformHandle fOuterThresholdVar; |
| 58 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
| 59 | }; |
| 60 | GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const { |
| 61 | return new GrGLSLAlphaThresholdFragmentProcessor(); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 62 | } |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 63 | void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
| 64 | b->add32(GrColorSpaceXform::XformKey(fColorXform.get())); |
| 65 | } |
| 66 | bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const { |
| 67 | const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>(); |
| 68 | (void) that; |
| 69 | if (fImage != that.fImage) return false; |
| 70 | if (fColorXform != that.fColorXform) return false; |
| 71 | if (fMask != that.fMask) return false; |
| 72 | if (fInnerThreshold != that.fInnerThreshold) return false; |
| 73 | if (fOuterThreshold != that.fOuterThreshold) return false; |
| 74 | return true; |
| 75 | } |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 76 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor); |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 77 | #if GR_TEST_UTILS |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 78 | sk_sp<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(GrProcessorTestData* testData) { |
| 79 | |
| 80 | sk_sp<GrTextureProxy> bmpProxy = testData->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx); |
| 81 | sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx); |
| 82 | |
| 83 | float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 84 | float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 85 | const int kMaxWidth = 1000; |
| 86 | const int kMaxHeight = 1000; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 87 | uint32_t width = testData->fRandom->nextULessThan(kMaxWidth); |
| 88 | uint32_t height = testData->fRandom->nextULessThan(kMaxHeight); |
| 89 | uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width); |
| 90 | uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 91 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 92 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(testData->fRandom); |
| 93 | return GrAlphaThresholdFragmentProcessor::Make( |
| 94 | std::move(bmpProxy), |
| 95 | colorSpaceXform, |
| 96 | std::move(maskProxy), |
| 97 | innerThresh, outerThresh, |
| 98 | bounds); |
robertphillips | f7142e7 | 2016-04-18 07:20:05 -0700 | [diff] [blame] | 99 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 100 | #endif |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 101 | #endif |