blob: eb29003f5ae9f3fcf8b46a8c15071176072325b1 [file] [log] [blame]
wangyixb1daa862015-08-18 11:29:31 -07001/*
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 Nicholasd4efe682019-08-29 16:10:13 -04008#include "src/gpu/GrCoordTransform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#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"
wangyixb1daa862015-08-18 11:29:31 -070014
egdaniel64c47282015-11-13 06:54:19 -080015void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman,
16 const GrFragmentProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -070017 this->onSetData(pdman, processor);
wangyixb1daa862015-08-18 11:29:31 -070018}
wangyix2a378432015-08-18 12:00:12 -070019
Brian Osmanac9d3f62020-06-25 11:07:30 -040020void GrGLSLFragmentProcessor::emitChildFunction(int childIndex, EmitArgs& args) {
cdalton85285412016-02-18 12:37:07 -080021 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040022 while (childIndex >= (int) fFunctionNames.size()) {
23 fFunctionNames.emplace_back();
24 }
Brian Osman978693c2020-01-24 14:52:10 -050025
Brian Osman978693c2020-01-24 14:52:10 -050026 // Emit the child's helper function if this is the first time we've seen a call
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040027 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040028 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
29 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050030
Ethan Nicholasd4efe682019-08-29 16:10:13 -040031 EmitArgs childArgs(fragBuilder,
32 args.fUniformHandler,
33 args.fShaderCaps,
Brian Osmanac9d3f62020-06-25 11:07:30 -040034 args.fFp.childProcessor(childIndex),
Brian Osman978693c2020-01-24 14:52:10 -050035 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040036 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -040037 "_coords",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040038 coordVars,
39 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050040 fFunctionNames[childIndex] =
41 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000042 }
Brian Osmanac9d3f62020-06-25 11:07:30 -040043}
44
45SkString 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);
bsalomon38ddbad2015-09-24 06:00:00 -070055
Michael Ludwige88320b2020-06-24 09:04:56 -040056 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 Osman978693c2020-01-24 14:52:10 -050070 }
wangyix2a378432015-08-18 12:00:12 -070071}
bsalomona624bf32016-09-20 09:12:47 -070072
Ethan Nicholas58430122020-04-14 09:54:02 -040073SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
74 EmitArgs& args,
75 SkSL::String skslMatrix) {
Brian Osmanac9d3f62020-06-25 11:07:30 -040076 this->emitChildFunction(childIndex, args);
Ethan Nicholas58430122020-04-14 09:54:02 -040077
78 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
79
Michael Ludwige88320b2020-06-24 09:04:56 -040080 // 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 Osman061a5cf2020-06-24 14:50:25 -040086 skslMatrix.assign(childProc.sampleMatrix().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -040087 }
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 Osman061a5cf2020-06-24 14:50:25 -040095 args.fFp, SkString(childProc.sampleMatrix().fExpression));
Michael Ludwige88320b2020-06-24 09:04:56 -040096 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 Osmanac9d3f62020-06-25 11:07:30 -0400117 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 Ludwige88320b2020-06-24 09:04:56 -0400123 } 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 Nicholas58430122020-04-14 09:54:02 -0400139}
140
bsalomona624bf32016-09-20 09:12:47 -0700141//////////////////////////////////////////////////////////////////////////////
142
Brian Salomonc241b582019-11-27 08:57:17 -0500143GrGLSLFragmentProcessor::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());
bsalomona624bf32016-09-20 09:12:47 -0700146 }
Brian Salomonc241b582019-11-27 08:57:17 -0500147}
148
149GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
150 return *fFPStack.back();
151}
152
153GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
154 return fFPStack.back();
155}
156
157GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
158 SkASSERT(!fFPStack.empty());
159 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700160 fFPStack.pop_back();
161 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
162 fFPStack.push_back(back->childProcessor(i));
163 }
Brian Salomonc241b582019-11-27 08:57:17 -0500164 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700165}