blob: 90f50c2ef3825f89deedc40493b62b6306b42ebe [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
27 // Subtle bug workaround: If an FP (this) has a child, and wishes to sample it, but does not
28 // want to *force* explicit coord sampling, then the obvious solution is to call it with
29 // invokeChild and no coords. However, if this FP is then adopted as a child of another FP that
30 // does want to sample with explicit coords, that property is propagated (recursively) to all
31 // children, and we need to supply explicit coords. So we propagate our own "_coords" (this is
32 // the name of our explicit coords parameter generated in the helper function).
Brian Salomonb10a6622020-02-20 13:36:01 -050033 if (args.fFp.isSampledWithExplicitCoords() && skslCoords.length() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040034 skslCoords = "_coords";
35 }
Brian Osman978693c2020-01-24 14:52:10 -050036
37 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
38
Ethan Nicholas58430122020-04-14 09:54:02 -040039 // If the fragment processor is invoked with overridden coordinates, it must *always* be invoked
40 // with overridden coords.
41 SkASSERT(childProc.isSampledWithExplicitCoords() == !skslCoords.empty());
42
43 if (skslCoords.length() == 0) {
44 switch (childProc.sampleMatrix().fKind) {
45 case SkSL::SampleMatrix::Kind::kMixed:
46 case SkSL::SampleMatrix::Kind::kVariable:
47 skslCoords = "_matrix";
48 break;
49 default:
50 break;
51 }
52 }
53
Brian Osman978693c2020-01-24 14:52:10 -050054 // Emit the child's helper function if this is the first time we've seen a call
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040055 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040056 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
57 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050058
Ethan Nicholasd4efe682019-08-29 16:10:13 -040059 EmitArgs childArgs(fragBuilder,
60 args.fUniformHandler,
61 args.fShaderCaps,
62 childProc,
Brian Osman978693c2020-01-24 14:52:10 -050063 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040064 "_input",
65 coordVars,
66 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050067 fFunctionNames[childIndex] =
68 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000069 }
bsalomon38ddbad2015-09-24 06:00:00 -070070
Brian Osman978693c2020-01-24 14:52:10 -050071 // Produce a string containing the call to the helper function
72 SkString result = SkStringPrintf("%s(%s", fFunctionNames[childIndex].c_str(),
73 inputColor ? inputColor : "half4(1)");
74 if (skslCoords.length()) {
75 result.appendf(", %s", skslCoords.c_str());
76 }
77 result.append(")");
78 return result;
wangyix2a378432015-08-18 12:00:12 -070079}
bsalomona624bf32016-09-20 09:12:47 -070080
Ethan Nicholas58430122020-04-14 09:54:02 -040081SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
82 EmitArgs& args,
83 SkSL::String skslMatrix) {
84 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
85 while (childIndex >= (int) fFunctionNames.size()) {
86 fFunctionNames.emplace_back();
87 }
88
89 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
90
91 // Emit the child's helper function if this is the first time we've seen a call
92 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholas58430122020-04-14 09:54:02 -040093 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
94 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
95
96 EmitArgs childArgs(fragBuilder,
97 args.fUniformHandler,
98 args.fShaderCaps,
99 childProc,
100 "_output",
101 "_input",
102 coordVars,
103 textureSamplers);
104 fFunctionNames[childIndex] =
105 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas58430122020-04-14 09:54:02 -0400106 }
107
108 // Produce a string containing the call to the helper function
109 return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(),
110 inputColor ? inputColor : "half4(1)",
111 skslMatrix.c_str());
112}
113
bsalomona624bf32016-09-20 09:12:47 -0700114//////////////////////////////////////////////////////////////////////////////
115
Brian Salomonc241b582019-11-27 08:57:17 -0500116GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
117 for (int i = cnt - 1; i >= 0; --i) {
118 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700119 }
Brian Salomonc241b582019-11-27 08:57:17 -0500120}
121
122GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
123 return *fFPStack.back();
124}
125
126GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
127 return fFPStack.back();
128}
129
130GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
131 SkASSERT(!fFPStack.empty());
132 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700133 fFPStack.pop_back();
134 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
135 fFPStack.push_back(back->childProcessor(i));
136 }
Brian Salomonc241b582019-11-27 08:57:17 -0500137 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700138}