blob: 7a0858c30188c29ef1aeca8bb059b85b9658048e [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) {
cdalton85285412016-02-18 12:37:07 -080020 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040021 while (childIndex >= (int) fFunctionNames.size()) {
22 fFunctionNames.emplace_back();
23 }
Brian Osman978693c2020-01-24 14:52:10 -050024
Brian Osman978693c2020-01-24 14:52:10 -050025 // Emit the child's helper function if this is the first time we've seen a call
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040026 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040027 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
28 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050029
Ethan Nicholasd4efe682019-08-29 16:10:13 -040030 EmitArgs childArgs(fragBuilder,
31 args.fUniformHandler,
32 args.fShaderCaps,
Brian Osmanac9d3f62020-06-25 11:07:30 -040033 args.fFp.childProcessor(childIndex),
Brian Osman978693c2020-01-24 14:52:10 -050034 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040035 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -040036 "_coords",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040037 coordVars,
38 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050039 fFunctionNames[childIndex] =
40 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000041 }
Brian Osmanac9d3f62020-06-25 11:07:30 -040042}
43
44SkString 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);
bsalomon38ddbad2015-09-24 06:00:00 -070054
Michael Ludwige88320b2020-06-24 09:04:56 -040055 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 Osman978693c2020-01-24 14:52:10 -050069 }
wangyix2a378432015-08-18 12:00:12 -070070}
bsalomona624bf32016-09-20 09:12:47 -070071
Ethan Nicholas58430122020-04-14 09:54:02 -040072SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
73 EmitArgs& args,
74 SkSL::String skslMatrix) {
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);
78
Michael Ludwige88320b2020-06-24 09:04:56 -040079 // 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 Osman061a5cf2020-06-24 14:50:25 -040085 skslMatrix.assign(childProc.sampleMatrix().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -040086 }
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 Osman061a5cf2020-06-24 14:50:25 -040094 args.fFp, SkString(childProc.sampleMatrix().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
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 Osman10b75412020-06-29 16:48:46 -0400116 return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(),
117 inputColor ? inputColor : "half4(1)", skslMatrix.c_str(),
118 args.fSampleCoord);
Michael Ludwige88320b2020-06-24 09:04:56 -0400119 } 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 Nicholas58430122020-04-14 09:54:02 -0400135}
136
bsalomona624bf32016-09-20 09:12:47 -0700137//////////////////////////////////////////////////////////////////////////////
138
Brian Salomonc241b582019-11-27 08:57:17 -0500139GrGLSLFragmentProcessor::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());
bsalomona624bf32016-09-20 09:12:47 -0700142 }
Brian Salomonc241b582019-11-27 08:57:17 -0500143}
144
145GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
146 return *fFPStack.back();
147}
148
149GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
150 return fFPStack.back();
151}
152
153GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
154 SkASSERT(!fFPStack.empty());
155 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700156 fFPStack.pop_back();
157 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
158 fFPStack.push_back(back->childProcessor(i));
159 }
Brian Salomonc241b582019-11-27 08:57:17 -0500160 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700161}