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) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 20 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 21 | while (childIndex >= (int) fFunctionNames.size()) { |
| 22 | fFunctionNames.emplace_back(); |
| 23 | } |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 24 | |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 25 | // 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] | 26 | if (fFunctionNames[childIndex].size() == 0) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 27 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 28 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 29 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 30 | EmitArgs childArgs(fragBuilder, |
| 31 | args.fUniformHandler, |
| 32 | args.fShaderCaps, |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 33 | args.fFp.childProcessor(childIndex), |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 34 | "_output", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 35 | "_input", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 36 | "_coords", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 37 | coordVars, |
| 38 | textureSamplers); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 39 | fFunctionNames[childIndex] = |
| 40 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 41 | } |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, |
| 45 | EmitArgs& args, SkSL::String skslCoords) { |
| 46 | this->emitChildFunction(childIndex, args); |
| 47 | |
| 48 | if (skslCoords.empty()) { |
| 49 | // Empty coords means passing through the coords of the parent |
| 50 | skslCoords = args.fSampleCoord; |
| 51 | } |
| 52 | |
| 53 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 54 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 55 | if (childProc.isSampledWithExplicitCoords()) { |
| 56 | // The child's function takes a half4 color and a float2 coordinate |
| 57 | return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(), |
| 58 | inputColor ? inputColor : "half4(1)", |
| 59 | skslCoords.c_str()); |
| 60 | } else { |
| 61 | // The child's function just takes a color; we should only get here for a call to |
| 62 | // sample(color) without explicit coordinates, so assert that the child has no sample matrix |
| 63 | // and skslCoords is _coords (a const/uniform sample call would go through |
| 64 | // invokeChildWithMatrix, and if a child was sampled with sample(matrix) and sample(), it |
| 65 | // should have been flagged as variable and hit the branch above). |
| 66 | SkASSERT(skslCoords == args.fSampleCoord && childProc.sampleMatrix().isNoOp()); |
| 67 | return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(), |
| 68 | inputColor ? inputColor : "half4(1)"); |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 69 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 70 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 71 | |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 72 | SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor, |
| 73 | EmitArgs& args, |
| 74 | SkSL::String skslMatrix) { |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 75 | this->emitChildFunction(childIndex, args); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 76 | |
| 77 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 78 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 79 | // Since this is const/uniform, the provided sksl expression should exactly match the |
| 80 | // expression stored on the FP, or it should match the mangled uniform name. |
| 81 | if (skslMatrix.empty()) { |
| 82 | // Empty matrix expression replaces with the sampleMatrix expression stored on the FP, but |
| 83 | // that is only valid for const/uniform sampled FPs |
| 84 | SkASSERT(childProc.sampleMatrix().isConstUniform()); |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame] | 85 | skslMatrix.assign(childProc.sampleMatrix().fExpression); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (childProc.sampleMatrix().isConstUniform()) { |
| 89 | // Attempt to resolve the uniform name from the raw name that was stored in the sample |
| 90 | // matrix. Since this is const/uniform, the provided expression better match what was given |
| 91 | // to the FP. |
| 92 | SkASSERT(childProc.sampleMatrix().fExpression == skslMatrix); |
| 93 | GrShaderVar uniform = args.fUniformHandler->getUniformMapping( |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame] | 94 | args.fFp, SkString(childProc.sampleMatrix().fExpression)); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 95 | if (uniform.getType() != kVoid_GrSLType) { |
| 96 | // Found the uniform, so replace the expression with the actual uniform name |
| 97 | SkASSERT(uniform.getType() == kFloat3x3_GrSLType); |
| 98 | skslMatrix = uniform.getName().c_str(); |
| 99 | } // else assume it's a constant expression |
| 100 | } |
| 101 | |
| 102 | // Produce a string containing the call to the helper function. sample(matrix) is special where |
| 103 | // the provided skslMatrix expression means that the child FP should be invoked with coords |
| 104 | // equal to matrix * parent coords. However, if matrix is a constant/uniform AND the parent |
| 105 | // coords were produced by const/uniform transforms, then this expression is lifted to a vertex |
| 106 | // shader and is stored in a varying. In that case, childProc will not have a variable sample |
| 107 | // matrix and will not be sampled explicitly, so its function signature will not take in coords. |
| 108 | // |
| 109 | // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke |
| 110 | // the function. |
| 111 | if (childProc.isSampledWithExplicitCoords()) { |
| 112 | SkASSERT(!childProc.sampleMatrix().isNoOp()); |
| 113 | // Only check perspective for this specific matrix transform, not the aggregate FP property. |
| 114 | // Any parent perspective will have already been applied when evaluated in the FS. |
| 115 | if (childProc.sampleMatrix().fHasPerspective) { |
Brian Osman | 10b7541 | 2020-06-29 16:48:46 -0400 | [diff] [blame] | 116 | return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(), |
| 117 | inputColor ? inputColor : "half4(1)", skslMatrix.c_str(), |
| 118 | args.fSampleCoord); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 119 | } else { |
| 120 | return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)", |
| 121 | fFunctionNames[childIndex].c_str(), |
| 122 | inputColor ? inputColor : "half4(1)", |
| 123 | skslMatrix.c_str(), args.fSampleCoord); |
| 124 | } |
| 125 | } else { |
| 126 | // A variable matrix expression should mark the child as explicitly sampled. A no-op |
| 127 | // matrix should match sample(color), not sample(color, matrix). |
| 128 | SkASSERT(childProc.sampleMatrix().isConstUniform()); |
| 129 | |
| 130 | // Since this is const/uniform and not explicitly sampled, it's transform has been |
| 131 | // promoted to the vertex shader and the signature doesn't take a float2 coord. |
| 132 | return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(), |
| 133 | inputColor ? inputColor : "half4(1)"); |
| 134 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 135 | } |
| 136 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 139 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 140 | for (int i = cnt - 1; i >= 0; --i) { |
| 141 | fFPStack.push_back(fps[i].get()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 142 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 146 | return *fFPStack.back(); |
| 147 | } |
| 148 | |
| 149 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 150 | return fFPStack.back(); |
| 151 | } |
| 152 | |
| 153 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 154 | SkASSERT(!fFPStack.empty()); |
| 155 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 156 | fFPStack.pop_back(); |
| 157 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 158 | fFPStack.push_back(back->childProcessor(i)); |
| 159 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 160 | return *this; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 161 | } |