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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrFragmentProcessor.h" |
| 9 | #include "src/gpu/GrProcessor.h" |
| 10 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 11 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 12 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 13 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman, |
| 15 | const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 16 | this->onSetData(pdman, processor); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 17 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 18 | |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 19 | void GrGLSLFragmentProcessor::emitChildFunction(int childIndex, EmitArgs& args) { |
John Stiles | 09dbeff | 2020-06-30 14:07:57 -0400 | [diff] [blame] | 20 | SkASSERT(childIndex >= 0); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 21 | SkASSERT(args.fFp.childProcessor(childIndex)); |
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 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 27 | // 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] | 28 | if (fFunctionNames[childIndex].size() == 0) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 29 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 30 | EmitArgs childArgs(fragBuilder, |
| 31 | args.fUniformHandler, |
| 32 | args.fShaderCaps, |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 33 | *args.fFp.childProcessor(childIndex), |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 34 | "_input", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 35 | "_coords", |
John Stiles | ad22300 | 2021-03-12 17:33:17 -0500 | [diff] [blame^] | 36 | coordVars); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 37 | fFunctionNames[childIndex] = |
| 38 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 39 | } |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, |
| 43 | EmitArgs& args, SkSL::String skslCoords) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 44 | if (!inputColor) { |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 45 | inputColor = args.fInputColor; |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 46 | } |
| 47 | |
John Stiles | 09dbeff | 2020-06-30 14:07:57 -0400 | [diff] [blame] | 48 | SkASSERT(childIndex >= 0); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 49 | const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex); |
| 50 | if (!childProc) { |
| 51 | return SkString(inputColor); |
| 52 | } |
| 53 | |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 54 | this->emitChildFunction(childIndex, args); |
| 55 | |
| 56 | if (skslCoords.empty()) { |
| 57 | // Empty coords means passing through the coords of the parent |
| 58 | skslCoords = args.fSampleCoord; |
| 59 | } |
| 60 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 61 | if (childProc->isSampledWithExplicitCoords()) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 62 | // The child's function takes a half4 color and a float2 coordinate |
| 63 | return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(), |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 64 | inputColor, skslCoords.c_str()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 65 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 66 | // The child's function just takes a color. We should only get here for a call to sample |
| 67 | // without explicit coordinates. Assert that the child has no sample matrix and skslCoords |
| 68 | // is _coords (a uniform matrix sample call would go through invokeChildWithMatrix, and if |
| 69 | // a child was sampled with sample(matrix) and sample(), it should have been flagged as |
| 70 | // variable and hit the branch above). |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 71 | SkASSERT(skslCoords == args.fSampleCoord && !childProc->sampleUsage().hasMatrix()); |
| 72 | return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(), inputColor); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 73 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 74 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 75 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 76 | SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor, |
| 77 | EmitArgs& args, |
| 78 | SkSL::String skslMatrix) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 79 | if (!inputColor) { |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 80 | inputColor = args.fInputColor; |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 81 | } |
| 82 | |
John Stiles | 09dbeff | 2020-06-30 14:07:57 -0400 | [diff] [blame] | 83 | SkASSERT(childIndex >= 0); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 84 | const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex); |
| 85 | if (!childProc) { |
| 86 | return SkString(inputColor); |
| 87 | } |
| 88 | |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 89 | this->emitChildFunction(childIndex, args); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 90 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 91 | SkASSERT(childProc->sampleUsage().hasMatrix()); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 92 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 93 | // Since this is uniform, the provided sksl expression should exactly match the expression |
| 94 | // stored on the FP, or it should match the mangled uniform name. |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 95 | if (skslMatrix.empty()) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 96 | // Empty matrix expression replaces with the sample matrix expression stored on the FP, but |
| 97 | // that is only valid for uniform sampled FPs |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 98 | SkASSERT(childProc->sampleUsage().hasUniformMatrix()); |
| 99 | skslMatrix.assign(childProc->sampleUsage().fExpression); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 102 | if (childProc->sampleUsage().hasUniformMatrix()) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 103 | // Attempt to resolve the uniform name from the raw name stored in the sample usage. |
| 104 | // Since this is uniform, the provided expression better match what was given to the FP. |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 105 | SkASSERT(childProc->sampleUsage().fExpression == skslMatrix); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 106 | GrShaderVar uniform = args.fUniformHandler->getUniformMapping( |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 107 | args.fFp, SkString(childProc->sampleUsage().fExpression)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 108 | if (uniform.getType() != kVoid_GrSLType) { |
| 109 | // Found the uniform, so replace the expression with the actual uniform name |
| 110 | SkASSERT(uniform.getType() == kFloat3x3_GrSLType); |
| 111 | skslMatrix = uniform.getName().c_str(); |
| 112 | } // else assume it's a constant expression |
| 113 | } |
| 114 | |
| 115 | // Produce a string containing the call to the helper function. sample(matrix) is special where |
| 116 | // the provided skslMatrix expression means that the child FP should be invoked with coords |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 117 | // equal to matrix * parent coords. However, if matrix is a uniform expression AND the parent |
| 118 | // coords were produced by uniform transforms, then this expression is lifted to a vertex |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 119 | // shader and is stored in a varying. In that case, childProc will not have a variable sample |
| 120 | // matrix and will not be sampled explicitly, so its function signature will not take in coords. |
| 121 | // |
| 122 | // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke |
| 123 | // the function. |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 124 | if (childProc->isSampledWithExplicitCoords()) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 125 | // Only check perspective for this specific matrix transform, not the aggregate FP property. |
| 126 | // Any parent perspective will have already been applied when evaluated in the FS. |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 127 | if (childProc->sampleUsage().fHasPerspective) { |
Brian Osman | 10b7541 | 2020-06-29 16:48:46 -0400 | [diff] [blame] | 128 | return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(), |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 129 | inputColor, skslMatrix.c_str(), args.fSampleCoord); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 130 | } else { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 131 | return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)", fFunctionNames[childIndex].c_str(), |
| 132 | inputColor, skslMatrix.c_str(), args.fSampleCoord); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 133 | } |
| 134 | } else { |
| 135 | // A variable matrix expression should mark the child as explicitly sampled. A no-op |
| 136 | // matrix should match sample(color), not sample(color, matrix). |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 137 | SkASSERT(childProc->sampleUsage().hasUniformMatrix()); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 138 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 139 | // Since this is uniform and not explicitly sampled, it's transform has been promoted to |
| 140 | // the vertex shader and the signature doesn't take a float2 coord. |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 141 | return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(), inputColor); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 142 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 143 | } |
| 144 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 145 | ////////////////////////////////////////////////////////////////////////////// |
| 146 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 147 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 148 | for (int i = cnt - 1; i >= 0; --i) { |
| 149 | fFPStack.push_back(fps[i].get()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 150 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Brian Salomon | 61a70fb | 2020-07-08 19:02:54 -0400 | [diff] [blame] | 153 | GrGLSLFragmentProcessor::ParallelIter::ParallelIter(const GrFragmentProcessor& fp, |
| 154 | GrGLSLFragmentProcessor& glslFP) |
| 155 | : fpIter(fp), glslIter(glslFP) {} |
| 156 | |
| 157 | GrGLSLFragmentProcessor::ParallelIter& GrGLSLFragmentProcessor::ParallelIter::operator++() { |
| 158 | ++fpIter; |
| 159 | ++glslIter; |
| 160 | SkASSERT(static_cast<bool>(fpIter) == static_cast<bool>(glslIter)); |
| 161 | return *this; |
| 162 | } |
| 163 | |
| 164 | std::tuple<const GrFragmentProcessor&, GrGLSLFragmentProcessor&> |
| 165 | GrGLSLFragmentProcessor::ParallelIter::operator*() const { |
| 166 | return {*fpIter, *glslIter}; |
| 167 | } |
| 168 | |
| 169 | bool GrGLSLFragmentProcessor::ParallelIter::operator==(const ParallelIterEnd& end) const { |
| 170 | SkASSERT(static_cast<bool>(fpIter) == static_cast<bool>(glslIter)); |
| 171 | return !fpIter; |
| 172 | } |
| 173 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 174 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 175 | return *fFPStack.back(); |
| 176 | } |
| 177 | |
| 178 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 179 | return fFPStack.back(); |
| 180 | } |
| 181 | |
| 182 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 183 | SkASSERT(!fFPStack.empty()); |
| 184 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 185 | fFPStack.pop_back(); |
| 186 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 187 | if (auto child = back->childProcessor(i)) { |
| 188 | fFPStack.push_back(child); |
| 189 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 190 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 191 | return *this; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 192 | } |
Brian Salomon | 61a70fb | 2020-07-08 19:02:54 -0400 | [diff] [blame] | 193 | |
| 194 | GrGLSLFragmentProcessor::ParallelRange::ParallelRange(const GrFragmentProcessor& fp, |
| 195 | GrGLSLFragmentProcessor& glslFP) |
| 196 | : fInitialFP(fp), fInitialGLSLFP(glslFP) {} |