wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrCoordTransform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrFragmentProcessor.h" |
| 10 | #include "src/gpu/GrProcessor.h" |
| 11 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 13 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 14 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 15 | void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman, |
| 16 | const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 17 | this->onSetData(pdman, processor); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 18 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 19 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 20 | SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, |
| 21 | EmitArgs& args, SkSL::String skslCoords) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 22 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 23 | while (childIndex >= (int) fFunctionNames.size()) { |
| 24 | fFunctionNames.emplace_back(); |
| 25 | } |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 26 | |
| 27 | // Subtle bug workaround: If an FP (this) has a child, and wishes to sample it, but does not |
| 28 | // want to *force* explicit coord sampling, then the obvious solution is to call it with |
| 29 | // invokeChild and no coords. However, if this FP is then adopted as a child of another FP that |
| 30 | // does want to sample with explicit coords, that property is propagated (recursively) to all |
| 31 | // children, and we need to supply explicit coords. So we propagate our own "_coords" (this is |
| 32 | // the name of our explicit coords parameter generated in the helper function). |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 33 | if (!args.fFp.coordTransformsApplyToLocalCoords() && skslCoords.length() == 0) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 34 | skslCoords = "_coords"; |
| 35 | } |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 36 | |
| 37 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 38 | |
| 39 | // Emit the child's helper function if this is the first time we've seen a call |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 40 | if (fFunctionNames[childIndex].size() == 0) { |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 41 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 42 | |
| 43 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 44 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 45 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 46 | EmitArgs childArgs(fragBuilder, |
| 47 | args.fUniformHandler, |
| 48 | args.fShaderCaps, |
| 49 | childProc, |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 50 | "_output", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 51 | "_input", |
| 52 | coordVars, |
| 53 | textureSamplers); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 54 | fFunctionNames[childIndex] = |
| 55 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 56 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 57 | fragBuilder->onAfterChildProcEmitCode(); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 58 | } |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 59 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 60 | // If the fragment processor is invoked with overridden coordinates, it must *always* be invoked |
| 61 | // with overridden coords. |
| 62 | SkASSERT(childProc.coordTransformsApplyToLocalCoords() == (skslCoords.length() == 0)); |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 63 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame^] | 64 | // Produce a string containing the call to the helper function |
| 65 | SkString result = SkStringPrintf("%s(%s", fFunctionNames[childIndex].c_str(), |
| 66 | inputColor ? inputColor : "half4(1)"); |
| 67 | if (skslCoords.length()) { |
| 68 | result.appendf(", %s", skslCoords.c_str()); |
| 69 | } |
| 70 | result.append(")"); |
| 71 | return result; |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 72 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 76 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 77 | for (int i = cnt - 1; i >= 0; --i) { |
| 78 | fFPStack.push_back(fps[i].get()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 79 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 83 | return *fFPStack.back(); |
| 84 | } |
| 85 | |
| 86 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 87 | return fFPStack.back(); |
| 88 | } |
| 89 | |
| 90 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 91 | SkASSERT(!fFPStack.empty()); |
| 92 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 93 | fFPStack.pop_back(); |
| 94 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 95 | fFPStack.push_back(back->childProcessor(i)); |
| 96 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 97 | return *this; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 98 | } |