blob: 1d031f7d73b49497e97f1f932f7c1b5ad441ff2e [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
Ethan Nicholasd4efe682019-08-29 16:10:13 -040020void GrGLSLFragmentProcessor::writeChildCall(GrGLSLFPFragmentBuilder* fragBuilder, int childIndex,
21 TransformedCoordVars coordVars, const char* inputColor,
22 const char* outputColor, EmitArgs& args,
23 SkSL::String skslCoords) {
24 std::vector<SkString> coordParams;
25 for (int i = 0; i < coordVars.count(); ++i) {
26 coordParams.push_back(fragBuilder->ensureCoords2D(coordVars[i].fVaryingPoint));
27 }
28 // if the fragment processor is invoked with overridden coordinates, it must *always* be invoked
29 // with overridden coords
Brian Salomon7d8b3972019-11-26 22:34:44 -050030 SkASSERT(args.fFp.coordTransformsApplyToLocalCoords() == (skslCoords.length() == 0));
Ethan Nicholasd4efe682019-08-29 16:10:13 -040031 fragBuilder->codeAppendf("%s = %s(%s", outputColor, fFunctionNames[childIndex].c_str(),
32 inputColor ? inputColor : "half4(1)");
33 if (skslCoords.length()) {
34 fragBuilder->codeAppendf(", %s", skslCoords.c_str());
35 }
36 fragBuilder->codeAppend(");\n");
bsalomon38ddbad2015-09-24 06:00:00 -070037}
38
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040039void GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040040 SkString* outputColor, EmitArgs& args,
41 SkSL::String skslCoords) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000042 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080043 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +000044 outputColor->append(fragBuilder->getMangleString());
45 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040046 while (childIndex >= (int) fFunctionNames.size()) {
47 fFunctionNames.emplace_back();
48 }
Brian Salomon7d8b3972019-11-26 22:34:44 -050049 if (!args.fFp.coordTransformsApplyToLocalCoords() && skslCoords.length() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040050 skslCoords = "_coords";
51 }
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040052 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040053 this->internalInvokeChild(childIndex, inputColor, outputColor->c_str(), args, skslCoords);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040054 } else {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040055 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
56
57 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
58 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
59 EmitArgs childArgs(fragBuilder,
60 args.fUniformHandler,
61 args.fShaderCaps,
62 childProc,
63 outputColor->c_str(),
64 "_input",
65 coordVars,
66 textureSamplers);
67 this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor->c_str(),
68 childArgs, skslCoords);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040069 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +000070}
71
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040072void GrGLSLFragmentProcessor::internalInvokeChild(int childIndex, const char* inputColor,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040073 const char* outputColor, EmitArgs& args,
74 SkSL::String skslCoords) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000075 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
76
77 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
78
79 // Prepare a mangled input color variable if the default is not used,
80 // inputName remains the empty string if no variable is needed.
81 SkString inputName;
82 if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) {
83 // The input name is based off of the current mangle string, and
84 // since this is called after onBeforeChildProcEmitCode(), it will be
85 // unique to the child processor (exactly what we want for its input).
86 inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str());
87 fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor);
88 }
bsalomon38ddbad2015-09-24 06:00:00 -070089
wangyix2a378432015-08-18 12:00:12 -070090 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
bsalomonb58a2b42016-09-26 06:55:02 -070091 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
92 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Michael Ludwig231de032018-08-30 14:33:01 -040093
egdaniel7ea439b2015-12-03 09:20:44 -080094 EmitArgs childArgs(fragBuilder,
95 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050096 args.fShaderCaps,
wangyix2a378432015-08-18 12:00:12 -070097 childProc,
Ethan Nicholas6ad52892019-05-03 13:13:42 +000098 outputColor,
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040099 "_input",
bsalomona624bf32016-09-20 09:12:47 -0700100 coordVars,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400101 textureSamplers);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400102 fFunctionNames[childIndex] = fragBuilder->writeProcessorFunction(
103 this->childProcessor(childIndex),
104 childArgs);
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400105 this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor, childArgs,
106 skslCoords);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000107 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -0700108}
bsalomona624bf32016-09-20 09:12:47 -0700109
110//////////////////////////////////////////////////////////////////////////////
111
Brian Salomonc241b582019-11-27 08:57:17 -0500112GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
113 for (int i = cnt - 1; i >= 0; --i) {
114 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -0700115 }
Brian Salomonc241b582019-11-27 08:57:17 -0500116}
117
118GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
119 return *fFPStack.back();
120}
121
122GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
123 return fFPStack.back();
124}
125
126GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
127 SkASSERT(!fFPStack.empty());
128 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -0700129 fFPStack.pop_back();
130 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
131 fFPStack.push_back(back->childProcessor(i));
132 }
Brian Salomonc241b582019-11-27 08:57:17 -0500133 return *this;
bsalomona624bf32016-09-20 09:12:47 -0700134}