blob: eb84ee8ddc04db0f0e861deb0aefc6ac8b5c1ed3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
wangyixb1daa862015-08-18 11:29:31 -070013
egdaniel64c47282015-11-13 06:54:19 -080014void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman,
15 const GrFragmentProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -070016 this->onSetData(pdman, processor);
wangyixb1daa862015-08-18 11:29:31 -070017}
wangyix2a378432015-08-18 12:00:12 -070018
Brian Osmanac9d3f62020-06-25 11:07:30 -040019void GrGLSLFragmentProcessor::emitChildFunction(int childIndex, EmitArgs& args) {
John Stiles09dbeff2020-06-30 14:07:57 -040020 SkASSERT(childIndex >= 0);
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);
Ethan Nicholasd4efe682019-08-29 16:10:13 -040029 EmitArgs childArgs(fragBuilder,
30 args.fUniformHandler,
31 args.fShaderCaps,
Brian Osmanac9d3f62020-06-25 11:07:30 -040032 args.fFp.childProcessor(childIndex),
Brian Osman978693c2020-01-24 14:52:10 -050033 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040034 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -040035 "_coords",
Brian Salomond90b3d32020-07-09 12:04:31 -040036 coordVars);
Brian Osman978693c2020-01-24 14:52:10 -050037 fFunctionNames[childIndex] =
38 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000039 }
Brian Osmanac9d3f62020-06-25 11:07:30 -040040}
41
42SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
43 EmitArgs& args, SkSL::String skslCoords) {
John Stiles09dbeff2020-06-30 14:07:57 -040044 SkASSERT(childIndex >= 0);
Brian Osmanac9d3f62020-06-25 11:07:30 -040045 this->emitChildFunction(childIndex, args);
46
47 if (skslCoords.empty()) {
48 // Empty coords means passing through the coords of the parent
49 skslCoords = args.fSampleCoord;
50 }
51
52 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
bsalomon38ddbad2015-09-24 06:00:00 -070053
Michael Ludwige88320b2020-06-24 09:04:56 -040054 if (childProc.isSampledWithExplicitCoords()) {
55 // The child's function takes a half4 color and a float2 coordinate
56 return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(),
57 inputColor ? inputColor : "half4(1)",
58 skslCoords.c_str());
59 } else {
Brian Osman1298bc42020-06-30 13:39:35 -040060 // The child's function just takes a color. We should only get here for a call to sample
61 // without explicit coordinates. Assert that the child has no sample matrix and skslCoords
62 // is _coords (a uniform matrix sample call would go through invokeChildWithMatrix, and if
63 // a child was sampled with sample(matrix) and sample(), it should have been flagged as
64 // variable and hit the branch above).
65 SkASSERT(skslCoords == args.fSampleCoord && !childProc.sampleUsage().hasMatrix());
Michael Ludwige88320b2020-06-24 09:04:56 -040066 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
67 inputColor ? inputColor : "half4(1)");
Brian Osman978693c2020-01-24 14:52:10 -050068 }
wangyix2a378432015-08-18 12:00:12 -070069}
bsalomona624bf32016-09-20 09:12:47 -070070
Ethan Nicholas58430122020-04-14 09:54:02 -040071SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
72 EmitArgs& args,
73 SkSL::String skslMatrix) {
John Stiles09dbeff2020-06-30 14:07:57 -040074 SkASSERT(childIndex >= 0);
Brian Osmanac9d3f62020-06-25 11:07:30 -040075 this->emitChildFunction(childIndex, args);
Ethan Nicholas58430122020-04-14 09:54:02 -040076
77 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
Brian Osman1298bc42020-06-30 13:39:35 -040078 SkASSERT(childProc.sampleUsage().hasMatrix());
Ethan Nicholas58430122020-04-14 09:54:02 -040079
Brian Osman1298bc42020-06-30 13:39:35 -040080 // Since this is uniform, the provided sksl expression should exactly match the expression
81 // stored on the FP, or it should match the mangled uniform name.
Michael Ludwige88320b2020-06-24 09:04:56 -040082 if (skslMatrix.empty()) {
Brian Osman1298bc42020-06-30 13:39:35 -040083 // Empty matrix expression replaces with the sample matrix expression stored on the FP, but
84 // that is only valid for uniform sampled FPs
85 SkASSERT(childProc.sampleUsage().hasUniformMatrix());
86 skslMatrix.assign(childProc.sampleUsage().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -040087 }
88
Brian Osman1298bc42020-06-30 13:39:35 -040089 if (childProc.sampleUsage().hasUniformMatrix()) {
90 // Attempt to resolve the uniform name from the raw name stored in the sample usage.
91 // Since this is uniform, the provided expression better match what was given to the FP.
92 SkASSERT(childProc.sampleUsage().fExpression == skslMatrix);
Michael Ludwige88320b2020-06-24 09:04:56 -040093 GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
Brian Osman1298bc42020-06-30 13:39:35 -040094 args.fFp, SkString(childProc.sampleUsage().fExpression));
Michael Ludwige88320b2020-06-24 09:04:56 -040095 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
Brian Osman1298bc42020-06-30 13:39:35 -0400104 // equal to matrix * parent coords. However, if matrix is a uniform expression AND the parent
105 // coords were produced by uniform transforms, then this expression is lifted to a vertex
Michael Ludwige88320b2020-06-24 09:04:56 -0400106 // 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()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400112 // Only check perspective for this specific matrix transform, not the aggregate FP property.
113 // Any parent perspective will have already been applied when evaluated in the FS.
Brian Osman1298bc42020-06-30 13:39:35 -0400114 if (childProc.sampleUsage().fHasPerspective) {
Brian Osman10b75412020-06-29 16:48:46 -0400115 return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(),
116 inputColor ? inputColor : "half4(1)", skslMatrix.c_str(),
117 args.fSampleCoord);
Michael Ludwige88320b2020-06-24 09:04:56 -0400118 } else {
119 return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)",
120 fFunctionNames[childIndex].c_str(),
121 inputColor ? inputColor : "half4(1)",
122 skslMatrix.c_str(), args.fSampleCoord);
123 }
124 } else {
125 // A variable matrix expression should mark the child as explicitly sampled. A no-op
126 // matrix should match sample(color), not sample(color, matrix).
Brian Osman1298bc42020-06-30 13:39:35 -0400127 SkASSERT(childProc.sampleUsage().hasUniformMatrix());
Michael Ludwige88320b2020-06-24 09:04:56 -0400128
Brian Osman1298bc42020-06-30 13:39:35 -0400129 // Since this is uniform and not explicitly sampled, it's transform has been promoted to
130 // the vertex shader and the signature doesn't take a float2 coord.
Michael Ludwige88320b2020-06-24 09:04:56 -0400131 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
132 inputColor ? inputColor : "half4(1)");
133 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400134}
135
bsalomona624bf32016-09-20 09:12:47 -0700136//////////////////////////////////////////////////////////////////////////////
137
Brian Salomonc241b582019-11-27 08:57:17 -0500138GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
139 for (int i = cnt - 1; i >= 0; --i) {
140 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700141 }
Brian Salomonc241b582019-11-27 08:57:17 -0500142}
143
Brian Salomon61a70fb2020-07-08 19:02:54 -0400144GrGLSLFragmentProcessor::ParallelIter::ParallelIter(const GrFragmentProcessor& fp,
145 GrGLSLFragmentProcessor& glslFP)
146 : fpIter(fp), glslIter(glslFP) {}
147
148GrGLSLFragmentProcessor::ParallelIter& GrGLSLFragmentProcessor::ParallelIter::operator++() {
149 ++fpIter;
150 ++glslIter;
151 SkASSERT(static_cast<bool>(fpIter) == static_cast<bool>(glslIter));
152 return *this;
153}
154
155std::tuple<const GrFragmentProcessor&, GrGLSLFragmentProcessor&>
156GrGLSLFragmentProcessor::ParallelIter::operator*() const {
157 return {*fpIter, *glslIter};
158}
159
160bool GrGLSLFragmentProcessor::ParallelIter::operator==(const ParallelIterEnd& end) const {
161 SkASSERT(static_cast<bool>(fpIter) == static_cast<bool>(glslIter));
162 return !fpIter;
163}
164
Brian Salomonc241b582019-11-27 08:57:17 -0500165GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
166 return *fFPStack.back();
167}
168
169GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
170 return fFPStack.back();
171}
172
173GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
174 SkASSERT(!fFPStack.empty());
175 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700176 fFPStack.pop_back();
177 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
178 fFPStack.push_back(back->childProcessor(i));
179 }
Brian Salomonc241b582019-11-27 08:57:17 -0500180 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700181}
Brian Salomon61a70fb2020-07-08 19:02:54 -0400182
183GrGLSLFragmentProcessor::ParallelRange::ParallelRange(const GrFragmentProcessor& fp,
184 GrGLSLFragmentProcessor& glslFP)
185 : fInitialFP(fp), fInitialGLSLFP(glslFP) {}