blob: 298cccdb9574aec0e1761280799fe748124af867 [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) {
Brian Osman978693c2020-01-24 14:52:10 -050056 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
Ethan Nicholasd4efe682019-08-29 16:10:13 -040057
58 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
59 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050060
Ethan Nicholasd4efe682019-08-29 16:10:13 -040061 EmitArgs childArgs(fragBuilder,
62 args.fUniformHandler,
63 args.fShaderCaps,
64 childProc,
Brian Osman978693c2020-01-24 14:52:10 -050065 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040066 "_input",
67 coordVars,
68 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050069 fFunctionNames[childIndex] =
70 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000071
Brian Osman978693c2020-01-24 14:52:10 -050072 fragBuilder->onAfterChildProcEmitCode();
Ethan Nicholas6ad52892019-05-03 13:13:42 +000073 }
bsalomon38ddbad2015-09-24 06:00:00 -070074
Brian Osman978693c2020-01-24 14:52:10 -050075 // Produce a string containing the call to the helper function
76 SkString result = SkStringPrintf("%s(%s", fFunctionNames[childIndex].c_str(),
77 inputColor ? inputColor : "half4(1)");
78 if (skslCoords.length()) {
79 result.appendf(", %s", skslCoords.c_str());
80 }
81 result.append(")");
82 return result;
wangyix2a378432015-08-18 12:00:12 -070083}
bsalomona624bf32016-09-20 09:12:47 -070084
Ethan Nicholas58430122020-04-14 09:54:02 -040085SkString GrGLSLFragmentProcessor::invokeChildWithMatrix(int childIndex, const char* inputColor,
86 EmitArgs& args,
87 SkSL::String skslMatrix) {
88 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
89 while (childIndex >= (int) fFunctionNames.size()) {
90 fFunctionNames.emplace_back();
91 }
92
93 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
94
95 // Emit the child's helper function if this is the first time we've seen a call
96 if (fFunctionNames[childIndex].size() == 0) {
97 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
98
99 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
100 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
101
102 EmitArgs childArgs(fragBuilder,
103 args.fUniformHandler,
104 args.fShaderCaps,
105 childProc,
106 "_output",
107 "_input",
108 coordVars,
109 textureSamplers);
110 fFunctionNames[childIndex] =
111 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
112
113 fragBuilder->onAfterChildProcEmitCode();
114 }
115
116 // Produce a string containing the call to the helper function
117 return SkStringPrintf("%s(%s, %s)", fFunctionNames[childIndex].c_str(),
118 inputColor ? inputColor : "half4(1)",
119 skslMatrix.c_str());
120}
121
bsalomona624bf32016-09-20 09:12:47 -0700122//////////////////////////////////////////////////////////////////////////////
123
Brian Salomonc241b582019-11-27 08:57:17 -0500124GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
125 for (int i = cnt - 1; i >= 0; --i) {
126 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700127 }
Brian Salomonc241b582019-11-27 08:57:17 -0500128}
129
130GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
131 return *fFPStack.back();
132}
133
134GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
135 return fFPStack.back();
136}
137
138GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
139 SkASSERT(!fFPStack.empty());
140 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700141 fFPStack.pop_back();
142 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
143 fFPStack.push_back(back->childProcessor(i));
144 }
Brian Salomonc241b582019-11-27 08:57:17 -0500145 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700146}