blob: 9b3b7b72a2bb8172725573d48a18ef9536bb2db5 [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 {
63 // The child's function just takes a color; we should only get here for a call to
64 // sample(color) without explicit coordinates, so assert that the child has no sample matrix
65 // and skslCoords is _coords (a const/uniform sample call would go through
66 // invokeChildWithMatrix, and if a child was sampled with sample(matrix) and sample(), it
67 // should have been flagged as variable and hit the branch above).
68 SkASSERT(skslCoords == args.fSampleCoord && childProc.sampleMatrix().isNoOp());
69 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);
81
Michael Ludwige88320b2020-06-24 09:04:56 -040082 // Since this is const/uniform, the provided sksl expression should exactly match the
83 // expression stored on the FP, or it should match the mangled uniform name.
84 if (skslMatrix.empty()) {
85 // Empty matrix expression replaces with the sampleMatrix expression stored on the FP, but
86 // that is only valid for const/uniform sampled FPs
87 SkASSERT(childProc.sampleMatrix().isConstUniform());
Brian Osman061a5cf2020-06-24 14:50:25 -040088 skslMatrix.assign(childProc.sampleMatrix().fExpression);
Michael Ludwige88320b2020-06-24 09:04:56 -040089 }
90
91 if (childProc.sampleMatrix().isConstUniform()) {
92 // Attempt to resolve the uniform name from the raw name that was stored in the sample
93 // matrix. Since this is const/uniform, the provided expression better match what was given
94 // to the FP.
95 SkASSERT(childProc.sampleMatrix().fExpression == skslMatrix);
96 GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
Brian Osman061a5cf2020-06-24 14:50:25 -040097 args.fFp, SkString(childProc.sampleMatrix().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
107 // equal to matrix * parent coords. However, if matrix is a constant/uniform AND the parent
108 // coords were produced by const/uniform transforms, then this expression is lifted to a vertex
109 // 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()) {
115 SkASSERT(!childProc.sampleMatrix().isNoOp());
116 // Only check perspective for this specific matrix transform, not the aggregate FP property.
117 // Any parent perspective will have already been applied when evaluated in the FS.
118 if (childProc.sampleMatrix().fHasPerspective) {
Brian Osman10b75412020-06-29 16:48:46 -0400119 return SkStringPrintf("%s(%s, proj((%s) * %s.xy1))", fFunctionNames[childIndex].c_str(),
120 inputColor ? inputColor : "half4(1)", skslMatrix.c_str(),
121 args.fSampleCoord);
Michael Ludwige88320b2020-06-24 09:04:56 -0400122 } else {
123 return SkStringPrintf("%s(%s, ((%s) * %s.xy1).xy)",
124 fFunctionNames[childIndex].c_str(),
125 inputColor ? inputColor : "half4(1)",
126 skslMatrix.c_str(), args.fSampleCoord);
127 }
128 } else {
129 // A variable matrix expression should mark the child as explicitly sampled. A no-op
130 // matrix should match sample(color), not sample(color, matrix).
131 SkASSERT(childProc.sampleMatrix().isConstUniform());
132
133 // Since this is const/uniform and not explicitly sampled, it's transform has been
134 // promoted to the vertex shader and the signature doesn't take a float2 coord.
135 return SkStringPrintf("%s(%s)", fFunctionNames[childIndex].c_str(),
136 inputColor ? inputColor : "half4(1)");
137 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400138}
139
bsalomona624bf32016-09-20 09:12:47 -0700140//////////////////////////////////////////////////////////////////////////////
141
Brian Salomonc241b582019-11-27 08:57:17 -0500142GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
143 for (int i = cnt - 1; i >= 0; --i) {
144 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700145 }
Brian Salomonc241b582019-11-27 08:57:17 -0500146}
147
148GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
149 return *fFPStack.back();
150}
151
152GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
153 return fFPStack.back();
154}
155
156GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
157 SkASSERT(!fFPStack.empty());
158 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700159 fFPStack.pop_back();
160 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
161 fFPStack.push_back(back->childProcessor(i));
162 }
Brian Salomonc241b582019-11-27 08:57:17 -0500163 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700164}