blob: ad9b5ba92d3b9aaf90a545a3ecb6081ea4a5022e [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);
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) {
John Stiles09dbeff2020-06-30 14:07:57 -040047 SkASSERT(childIndex >= 0);
Brian Osmanac9d3f62020-06-25 11:07:30 -040048 this->emitChildFunction(childIndex, args);
49
50 if (skslCoords.empty()) {
51 // Empty coords means passing through the coords of the parent
52 skslCoords = args.fSampleCoord;
53 }
54
55 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
bsalomon38ddbad2015-09-24 06:00:00 -070056
Michael Ludwige88320b2020-06-24 09:04:56 -040057 if (childProc.isSampledWithExplicitCoords()) {
58 // The child's function takes a half4 color and a float2 coordinate
59 return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(),
60 inputColor ? inputColor : "half4(1)",
61 skslCoords.c_str());
62 } else {
Brian Osman1298bc42020-06-30 13:39:35 -040063 // The child's function just takes a color. We should only get here for a call to sample
64 // without explicit coordinates. Assert that the child has no sample matrix and skslCoords
65 // is _coords (a uniform matrix sample call would go through invokeChildWithMatrix, and if
66 // a child was sampled with sample(matrix) and sample(), it should have been flagged as
67 // variable and hit the branch above).
68 SkASSERT(skslCoords == args.fSampleCoord && !childProc.sampleUsage().hasMatrix());
Michael Ludwige88320b2020-06-24 09:04:56 -040069 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
70 inputColor ? inputColor : "half4(1)");
Brian Osman978693c2020-01-24 14:52:10 -050071 }
wangyix2a378432015-08-18 12:00:12 -070072}
bsalomona624bf32016-09-20 09:12:47 -070073
Ethan Nicholas58430122020-04-14 09:54:02 -040074SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
75 EmitArgs& args,
76 SkSL::String skslMatrix) {
John Stiles09dbeff2020-06-30 14:07:57 -040077 SkASSERT(childIndex >= 0);
Brian Osmanac9d3f62020-06-25 11:07:30 -040078 this->emitChildFunction(childIndex, args);
Ethan Nicholas58430122020-04-14 09:54:02 -040079
80 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
Brian Osman1298bc42020-06-30 13:39:35 -040081 SkASSERT(childProc.sampleUsage().hasMatrix());
Ethan Nicholas58430122020-04-14 09:54:02 -040082
Brian Osman1298bc42020-06-30 13:39:35 -040083 // Since this is uniform, the provided sksl expression should exactly match the expression
84 // stored on the FP, or it should match the mangled uniform name.
Michael Ludwige88320b2020-06-24 09:04:56 -040085 if (skslMatrix.empty()) {
Brian Osman1298bc42020-06-30 13:39:35 -040086 // Empty matrix expression replaces with the sample matrix expression stored on the FP, but
87 // that is only valid for uniform sampled FPs
88 SkASSERT(childProc.sampleUsage().hasUniformMatrix());
89 skslMatrix.assign(childProc.sampleUsage().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -040090 }
91
Brian Osman1298bc42020-06-30 13:39:35 -040092 if (childProc.sampleUsage().hasUniformMatrix()) {
93 // Attempt to resolve the uniform name from the raw name stored in the sample usage.
94 // Since this is uniform, the provided expression better match what was given to the FP.
95 SkASSERT(childProc.sampleUsage().fExpression == skslMatrix);
Michael Ludwige88320b2020-06-24 09:04:56 -040096 GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
Brian Osman1298bc42020-06-30 13:39:35 -040097 args.fFp, SkString(childProc.sampleUsage().fExpression));
Michael Ludwige88320b2020-06-24 09:04:56 -040098 if (uniform.getType() != kVoid_GrSLType) {
99 // Found the uniform, so replace the expression with the actual uniform name
100 SkASSERT(uniform.getType() == kFloat3x3_GrSLType);
101 skslMatrix = uniform.getName().c_str();
102 } // else assume it's a constant expression
103 }
104
105 // Produce a string containing the call to the helper function. sample(matrix) is special where
106 // the provided skslMatrix expression means that the child FP should be invoked with coords
Brian Osman1298bc42020-06-30 13:39:35 -0400107 // equal to matrix * parent coords. However, if matrix is a uniform expression AND the parent
108 // coords were produced by uniform transforms, then this expression is lifted to a vertex
Michael Ludwige88320b2020-06-24 09:04:56 -0400109 // shader and is stored in a varying. In that case, childProc will not have a variable sample
110 // matrix and will not be sampled explicitly, so its function signature will not take in coords.
111 //
112 // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke
113 // the function.
114 if (childProc.isSampledWithExplicitCoords()) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400115 // Only check perspective for this specific matrix transform, not the aggregate FP property.
116 // Any parent perspective will have already been applied when evaluated in the FS.
Brian Osman1298bc42020-06-30 13:39:35 -0400117 if (childProc.sampleUsage().fHasPerspective) {
Brian Osman10b75412020-06-29 16:48:46 -0400118 return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(),
119 inputColor ? inputColor : "half4(1)", skslMatrix.c_str(),
120 args.fSampleCoord);
Michael Ludwige88320b2020-06-24 09:04:56 -0400121 } else {
122 return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)",
123 fFunctionNames[childIndex].c_str(),
124 inputColor ? inputColor : "half4(1)",
125 skslMatrix.c_str(), args.fSampleCoord);
126 }
127 } else {
128 // A variable matrix expression should mark the child as explicitly sampled. A no-op
129 // matrix should match sample(color), not sample(color, matrix).
Brian Osman1298bc42020-06-30 13:39:35 -0400130 SkASSERT(childProc.sampleUsage().hasUniformMatrix());
Michael Ludwige88320b2020-06-24 09:04:56 -0400131
Brian Osman1298bc42020-06-30 13:39:35 -0400132 // Since this is uniform and not explicitly sampled, it's transform has been promoted to
133 // the vertex shader and the signature doesn't take a float2 coord.
Michael Ludwige88320b2020-06-24 09:04:56 -0400134 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
135 inputColor ? inputColor : "half4(1)");
136 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400137}
138
bsalomona624bf32016-09-20 09:12:47 -0700139//////////////////////////////////////////////////////////////////////////////
140
Brian Salomonc241b582019-11-27 08:57:17 -0500141GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
142 for (int i = cnt - 1; i >= 0; --i) {
143 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700144 }
Brian Salomonc241b582019-11-27 08:57:17 -0500145}
146
147GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
148 return *fFPStack.back();
149}
150
151GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
152 return fFPStack.back();
153}
154
155GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
156 SkASSERT(!fFPStack.empty());
157 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700158 fFPStack.pop_back();
159 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
160 fFPStack.push_back(back->childProcessor(i));
161 }
Brian Salomonc241b582019-11-27 08:57:17 -0500162 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700163}