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