blob: 5d280d466f4dc26066796a53b1f688b78e7054ee [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 Osman978693c2020-01-24 14:52:10 -050020SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
21 EmitArgs& args, SkSL::String skslCoords) {
cdalton85285412016-02-18 12:37:07 -080022 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040023 while (childIndex >= (int) fFunctionNames.size()) {
24 fFunctionNames.emplace_back();
25 }
Brian Osman978693c2020-01-24 14:52:10 -050026
Michael Ludwige88320b2020-06-24 09:04:56 -040027 if (skslCoords.empty()) {
28 // Empty coords means passing through the coords of the parent
29 skslCoords = args.fSampleCoord;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040030 }
Brian Osman978693c2020-01-24 14:52:10 -050031
32 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
33
34 // Emit the child's helper function if this is the first time we've seen a call
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040035 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040036 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
37 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050038
Ethan Nicholasd4efe682019-08-29 16:10:13 -040039 EmitArgs childArgs(fragBuilder,
40 args.fUniformHandler,
41 args.fShaderCaps,
42 childProc,
Brian Osman978693c2020-01-24 14:52:10 -050043 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040044 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -040045 "_coords",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040046 coordVars,
47 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050048 fFunctionNames[childIndex] =
49 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000050 }
bsalomon38ddbad2015-09-24 06:00:00 -070051
Michael Ludwige88320b2020-06-24 09:04:56 -040052 if (childProc.isSampledWithExplicitCoords()) {
53 // The child's function takes a half4 color and a float2 coordinate
54 return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(),
55 inputColor ? inputColor : "half4(1)",
56 skslCoords.c_str());
57 } else {
58 // The child's function just takes a color; we should only get here for a call to
59 // sample(color) without explicit coordinates, so assert that the child has no sample matrix
60 // and skslCoords is _coords (a const/uniform sample call would go through
61 // invokeChildWithMatrix, and if a child was sampled with sample(matrix) and sample(), it
62 // should have been flagged as variable and hit the branch above).
63 SkASSERT(skslCoords == args.fSampleCoord && childProc.sampleMatrix().isNoOp());
64 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
65 inputColor ? inputColor : "half4(1)");
Brian Osman978693c2020-01-24 14:52:10 -050066 }
wangyix2a378432015-08-18 12:00:12 -070067}
bsalomona624bf32016-09-20 09:12:47 -070068
Ethan Nicholas58430122020-04-14 09:54:02 -040069SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
70 EmitArgs& args,
71 SkSL::String skslMatrix) {
72 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
73 while (childIndex >= (int) fFunctionNames.size()) {
74 fFunctionNames.emplace_back();
75 }
76
77 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
78
79 // Emit the child's helper function if this is the first time we've seen a call
80 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholas58430122020-04-14 09:54:02 -040081 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
82 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
83
84 EmitArgs childArgs(fragBuilder,
85 args.fUniformHandler,
86 args.fShaderCaps,
87 childProc,
88 "_output",
89 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -040090 "_coords",
Ethan Nicholas58430122020-04-14 09:54:02 -040091 coordVars,
92 textureSamplers);
93 fFunctionNames[childIndex] =
94 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas58430122020-04-14 09:54:02 -040095 }
96
Michael Ludwige88320b2020-06-24 09:04:56 -040097 // Since this is const/uniform, the provided sksl expression should exactly match the
98 // expression stored on the FP, or it should match the mangled uniform name.
99 if (skslMatrix.empty()) {
100 // Empty matrix expression replaces with the sampleMatrix expression stored on the FP, but
101 // that is only valid for const/uniform sampled FPs
102 SkASSERT(childProc.sampleMatrix().isConstUniform());
Brian Osman061a5cf2020-06-24 14:50:25 -0400103 skslMatrix.assign(childProc.sampleMatrix().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -0400104 }
105
106 if (childProc.sampleMatrix().isConstUniform()) {
107 // Attempt to resolve the uniform name from the raw name that was stored in the sample
108 // matrix. Since this is const/uniform, the provided expression better match what was given
109 // to the FP.
110 SkASSERT(childProc.sampleMatrix().fExpression == skslMatrix);
111 GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
Brian Osman061a5cf2020-06-24 14:50:25 -0400112 args.fFp, SkString(childProc.sampleMatrix().fExpression));
Michael Ludwige88320b2020-06-24 09:04:56 -0400113 if (uniform.getType() != kVoid_GrSLType) {
114 // Found the uniform, so replace the expression with the actual uniform name
115 SkASSERT(uniform.getType() == kFloat3x3_GrSLType);
116 skslMatrix = uniform.getName().c_str();
117 } // else assume it's a constant expression
118 }
119
120 // Produce a string containing the call to the helper function. sample(matrix) is special where
121 // the provided skslMatrix expression means that the child FP should be invoked with coords
122 // equal to matrix * parent coords. However, if matrix is a constant/uniform AND the parent
123 // coords were produced by const/uniform transforms, then this expression is lifted to a vertex
124 // shader and is stored in a varying. In that case, childProc will not have a variable sample
125 // matrix and will not be sampled explicitly, so its function signature will not take in coords.
126 //
127 // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke
128 // the function.
129 if (childProc.isSampledWithExplicitCoords()) {
130 SkASSERT(!childProc.sampleMatrix().isNoOp());
131 // Only check perspective for this specific matrix transform, not the aggregate FP property.
132 // Any parent perspective will have already been applied when evaluated in the FS.
133 if (childProc.sampleMatrix().fHasPerspective) {
134 SkString coords3 = fragBuilder->newTmpVarName("coords3");
135 fragBuilder->codeAppendf("float3 %s = (%s) * %s.xy1;\n",
136 coords3.c_str(), skslMatrix.c_str(), args.fSampleCoord);
137 return SkStringPrintf("%s(%s, %s.xy / %s.z)",
138 fFunctionNames[childIndex].c_str(),
139 inputColor ? inputColor : "half4(1)",
140 coords3.c_str(), coords3.c_str());
141 } else {
142 return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)",
143 fFunctionNames[childIndex].c_str(),
144 inputColor ? inputColor : "half4(1)",
145 skslMatrix.c_str(), args.fSampleCoord);
146 }
147 } else {
148 // A variable matrix expression should mark the child as explicitly sampled. A no-op
149 // matrix should match sample(color), not sample(color, matrix).
150 SkASSERT(childProc.sampleMatrix().isConstUniform());
151
152 // Since this is const/uniform and not explicitly sampled, it's transform has been
153 // promoted to the vertex shader and the signature doesn't take a float2 coord.
154 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
155 inputColor ? inputColor : "half4(1)");
156 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400157}
158
bsalomona624bf32016-09-20 09:12:47 -0700159//////////////////////////////////////////////////////////////////////////////
160
Brian Salomonc241b582019-11-27 08:57:17 -0500161GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
162 for (int i = cnt - 1; i >= 0; --i) {
163 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700164 }
Brian Salomonc241b582019-11-27 08:57:17 -0500165}
166
167GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
168 return *fFPStack.back();
169}
170
171GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
172 return fFPStack.back();
173}
174
175GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
176 SkASSERT(!fFPStack.empty());
177 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700178 fFPStack.pop_back();
179 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
180 fFPStack.push_back(back->childProcessor(i));
181 }
Brian Salomonc241b582019-11-27 08:57:17 -0500182 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700183}