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 | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 33 | if (args.fFp.isSampledWithExplicitCoords() && 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 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame^] | 39 | // If the fragment processor is invoked with overridden coordinates, it must *always* be invoked |
| 40 | // with overridden coords. |
| 41 | SkASSERT(childProc.isSampledWithExplicitCoords() == !skslCoords.empty()); |
| 42 | |
| 43 | if (skslCoords.length() == 0) { |
| 44 | switch (childProc.sampleMatrix().fKind) { |
| 45 | case SkSL::SampleMatrix::Kind::kMixed: |
| 46 | case SkSL::SampleMatrix::Kind::kVariable: |
| 47 | skslCoords = "_matrix"; |
| 48 | break; |
| 49 | default: |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 54 | // 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] | 55 | if (fFunctionNames[childIndex].size() == 0) { |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 56 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 57 | |
| 58 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 59 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 60 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 61 | EmitArgs childArgs(fragBuilder, |
| 62 | args.fUniformHandler, |
| 63 | args.fShaderCaps, |
| 64 | childProc, |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 65 | "_output", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 66 | "_input", |
| 67 | coordVars, |
| 68 | textureSamplers); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 69 | fFunctionNames[childIndex] = |
| 70 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 71 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 72 | fragBuilder->onAfterChildProcEmitCode(); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 73 | } |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 74 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 75 | // Produce a string containing the call to the helper function |
| 76 | SkString result = SkStringPrintf("%s(%s", fFunctionNames[childIndex].c_str(), |
| 77 | inputColor ? inputColor : "half4(1)"); |
| 78 | if (skslCoords.length()) { |
| 79 | result.appendf(", %s", skslCoords.c_str()); |
| 80 | } |
| 81 | result.append(")"); |
| 82 | return result; |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 83 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 84 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame^] | 85 | SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor, |
| 86 | EmitArgs& args, |
| 87 | SkSL::String skslMatrix) { |
| 88 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 89 | while (childIndex >= (int) fFunctionNames.size()) { |
| 90 | fFunctionNames.emplace_back(); |
| 91 | } |
| 92 | |
| 93 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 94 | |
| 95 | // Emit the child's helper function if this is the first time we've seen a call |
| 96 | if (fFunctionNames[childIndex].size() == 0) { |
| 97 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
| 98 | |
| 99 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 100 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
| 101 | |
| 102 | EmitArgs childArgs(fragBuilder, |
| 103 | args.fUniformHandler, |
| 104 | args.fShaderCaps, |
| 105 | childProc, |
| 106 | "_output", |
| 107 | "_input", |
| 108 | coordVars, |
| 109 | textureSamplers); |
| 110 | fFunctionNames[childIndex] = |
| 111 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
| 112 | |
| 113 | fragBuilder->onAfterChildProcEmitCode(); |
| 114 | } |
| 115 | |
| 116 | // Produce a string containing the call to the helper function |
| 117 | return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(), |
| 118 | inputColor ? inputColor : "half4(1)", |
| 119 | skslMatrix.c_str()); |
| 120 | } |
| 121 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 122 | ////////////////////////////////////////////////////////////////////////////// |
| 123 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 124 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 125 | for (int i = cnt - 1; i >= 0; --i) { |
| 126 | fFPStack.push_back(fps[i].get()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 127 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 131 | return *fFPStack.back(); |
| 132 | } |
| 133 | |
| 134 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 135 | return fFPStack.back(); |
| 136 | } |
| 137 | |
| 138 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 139 | SkASSERT(!fFPStack.empty()); |
| 140 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 141 | fFPStack.pop_back(); |
| 142 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 143 | fFPStack.push_back(back->childProcessor(i)); |
| 144 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 145 | return *this; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 146 | } |