blob: e5e97deccf053d3476285978b1a5a94038531885 [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::invokeChild(int childIndex, const char* inputColor, EmitArgs& args,
21 SkSL::String skslCoords) {
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040022 while (childIndex >= (int) fFunctionNames.size()) {
23 fFunctionNames.emplace_back();
24 }
Ethan Nicholasd4efe682019-08-29 16:10:13 -040025 this->internalInvokeChild(childIndex, inputColor, args.fOutputColor, args, skslCoords);
26}
27
28void GrGLSLFragmentProcessor::writeChildCall(GrGLSLFPFragmentBuilder* fragBuilder, int childIndex,
29 TransformedCoordVars coordVars, const char* inputColor,
30 const char* outputColor, EmitArgs& args,
31 SkSL::String skslCoords) {
32 std::vector<SkString> coordParams;
33 for (int i = 0; i < coordVars.count(); ++i) {
34 coordParams.push_back(fragBuilder->ensureCoords2D(coordVars[i].fVaryingPoint));
35 }
36 // if the fragment processor is invoked with overridden coordinates, it must *always* be invoked
37 // with overridden coords
38 SkASSERT(args.fFp.computeLocalCoordsInVertexShader() == (skslCoords.length() == 0));
39 fragBuilder->codeAppendf("%s = %s(%s", outputColor, fFunctionNames[childIndex].c_str(),
40 inputColor ? inputColor : "half4(1)");
41 if (skslCoords.length()) {
42 fragBuilder->codeAppendf(", %s", skslCoords.c_str());
43 }
44 fragBuilder->codeAppend(");\n");
bsalomon38ddbad2015-09-24 06:00:00 -070045}
46
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040047void GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040048 SkString* outputColor, EmitArgs& args,
49 SkSL::String skslCoords) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000050 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080051 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +000052 outputColor->append(fragBuilder->getMangleString());
53 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040054 while (childIndex >= (int) fFunctionNames.size()) {
55 fFunctionNames.emplace_back();
56 }
Ethan Nicholasd4efe682019-08-29 16:10:13 -040057 if (!args.fFp.computeLocalCoordsInVertexShader() && skslCoords.length() == 0) {
58 skslCoords = "_coords";
59 }
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040060 if (fFunctionNames[childIndex].size() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040061 this->internalInvokeChild(childIndex, inputColor, outputColor->c_str(), args, skslCoords);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040062 } else {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040063 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
64
65 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
66 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
67 EmitArgs childArgs(fragBuilder,
68 args.fUniformHandler,
69 args.fShaderCaps,
70 childProc,
71 outputColor->c_str(),
72 "_input",
73 coordVars,
74 textureSamplers);
75 this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor->c_str(),
76 childArgs, skslCoords);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040077 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +000078}
79
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040080void GrGLSLFragmentProcessor::internalInvokeChild(int childIndex, const char* inputColor,
Ethan Nicholasd4efe682019-08-29 16:10:13 -040081 const char* outputColor, EmitArgs& args,
82 SkSL::String skslCoords) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000083 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
84
85 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
86
87 // Prepare a mangled input color variable if the default is not used,
88 // inputName remains the empty string if no variable is needed.
89 SkString inputName;
90 if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) {
91 // The input name is based off of the current mangle string, and
92 // since this is called after onBeforeChildProcEmitCode(), it will be
93 // unique to the child processor (exactly what we want for its input).
94 inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str());
95 fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor);
96 }
bsalomon38ddbad2015-09-24 06:00:00 -070097
wangyix2a378432015-08-18 12:00:12 -070098 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
bsalomonb58a2b42016-09-26 06:55:02 -070099 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
100 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Michael Ludwig231de032018-08-30 14:33:01 -0400101
egdaniel7ea439b2015-12-03 09:20:44 -0800102 EmitArgs childArgs(fragBuilder,
103 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500104 args.fShaderCaps,
wangyix2a378432015-08-18 12:00:12 -0700105 childProc,
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000106 outputColor,
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400107 "_input",
bsalomona624bf32016-09-20 09:12:47 -0700108 coordVars,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400109 textureSamplers);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400110 fFunctionNames[childIndex] = fragBuilder->writeProcessorFunction(
111 this->childProcessor(childIndex),
112 childArgs);
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400113 this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor, childArgs,
114 skslCoords);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000115 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -0700116}
bsalomona624bf32016-09-20 09:12:47 -0700117
118//////////////////////////////////////////////////////////////////////////////
119
bsalomonb58a2b42016-09-26 06:55:02 -0700120GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
121 if (fFPStack.empty()) {
122 return nullptr;
bsalomona624bf32016-09-20 09:12:47 -0700123 }
bsalomonb58a2b42016-09-26 06:55:02 -0700124 GrGLSLFragmentProcessor* back = fFPStack.back();
125 fFPStack.pop_back();
126 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
127 fFPStack.push_back(back->childProcessor(i));
128 }
129 return back;
bsalomona624bf32016-09-20 09:12:47 -0700130}