blob: dec2c96cfefc853ea00fd3f44588aeb5ce9fea7f [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
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040019void GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, EmitArgs& args) {
20 while (childIndex >= (int) fFunctionNames.size()) {
21 fFunctionNames.emplace_back();
22 }
23 this->internalInvokeChild(childIndex, inputColor, args.fOutputColor, args);
bsalomon38ddbad2015-09-24 06:00:00 -070024}
25
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040026void GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
27 SkString* outputColor, EmitArgs& args) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000028 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080029 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +000030 outputColor->append(fragBuilder->getMangleString());
31 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040032 while (childIndex >= (int) fFunctionNames.size()) {
33 fFunctionNames.emplace_back();
34 }
35 if (fFunctionNames[childIndex].size() == 0) {
36 this->internalInvokeChild(childIndex, inputColor, outputColor->c_str(), args);
37 } else {
38 fragBuilder->codeAppendf("%s = %s(%s);", outputColor->c_str(),
39 fFunctionNames[childIndex].c_str(),
40 inputColor ? inputColor : "half4(1)");
41 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +000042}
43
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040044void GrGLSLFragmentProcessor::internalInvokeChild(int childIndex, const char* inputColor,
45 const char* outputColor, EmitArgs& args) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +000046 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
47
48 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
49
50 // Prepare a mangled input color variable if the default is not used,
51 // inputName remains the empty string if no variable is needed.
52 SkString inputName;
53 if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) {
54 // The input name is based off of the current mangle string, and
55 // since this is called after onBeforeChildProcEmitCode(), it will be
56 // unique to the child processor (exactly what we want for its input).
57 inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str());
58 fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor);
59 }
bsalomon38ddbad2015-09-24 06:00:00 -070060
wangyix2a378432015-08-18 12:00:12 -070061 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
62
bsalomonb58a2b42016-09-26 06:55:02 -070063 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
64 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Michael Ludwig231de032018-08-30 14:33:01 -040065
egdaniel7ea439b2015-12-03 09:20:44 -080066 EmitArgs childArgs(fragBuilder,
67 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050068 args.fShaderCaps,
wangyix2a378432015-08-18 12:00:12 -070069 childProc,
Ethan Nicholas6ad52892019-05-03 13:13:42 +000070 outputColor,
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040071 "_input",
bsalomona624bf32016-09-20 09:12:47 -070072 coordVars,
Brian Salomon662ea4b2018-07-12 14:53:49 -040073 textureSamplers);
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040074 fFunctionNames[childIndex] = fragBuilder->writeProcessorFunction(
75 this->childProcessor(childIndex),
76 childArgs);
77 fragBuilder->codeAppendf("%s = %s(%s);\n", outputColor,
78 fFunctionNames[childIndex].c_str(),
79 inputName.size() > 0 ? inputName.c_str()
80 : "half4(1)");
Ethan Nicholas6ad52892019-05-03 13:13:42 +000081
82 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -070083}
bsalomona624bf32016-09-20 09:12:47 -070084
85//////////////////////////////////////////////////////////////////////////////
86
bsalomonb58a2b42016-09-26 06:55:02 -070087GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
88 if (fFPStack.empty()) {
89 return nullptr;
bsalomona624bf32016-09-20 09:12:47 -070090 }
bsalomonb58a2b42016-09-26 06:55:02 -070091 GrGLSLFragmentProcessor* back = fFPStack.back();
92 fFPStack.pop_back();
93 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
94 fFPStack.push_back(back->childProcessor(i));
95 }
96 return back;
bsalomona624bf32016-09-20 09:12:47 -070097}