blob: 41018604af5d7e573da58e9e2b44858cfef105d6 [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 Nicholas6ad52892019-05-03 13:13:42 +000019void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) {
20 this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args);
bsalomon38ddbad2015-09-24 06:00:00 -070021}
22
Ethan Nicholas6ad52892019-05-03 13:13:42 +000023void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor,
24 SkString* outputColor, EmitArgs& args) {
25 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080026 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholas6ad52892019-05-03 13:13:42 +000027 outputColor->append(fragBuilder->getMangleString());
28 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
29 this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args);
30}
31
32void GrGLSLFragmentProcessor::internalEmitChild(int childIndex, const char* inputColor,
33 const char* outputColor, EmitArgs& args) {
34 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
35
36 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
37
38 // Prepare a mangled input color variable if the default is not used,
39 // inputName remains the empty string if no variable is needed.
40 SkString inputName;
41 if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) {
42 // The input name is based off of the current mangle string, and
43 // since this is called after onBeforeChildProcEmitCode(), it will be
44 // unique to the child processor (exactly what we want for its input).
45 inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str());
46 fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor);
47 }
bsalomon38ddbad2015-09-24 06:00:00 -070048
wangyix2a378432015-08-18 12:00:12 -070049 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
50
Ethan Nicholas6ad52892019-05-03 13:13:42 +000051 // emit the code for the child in its own scope
52 fragBuilder->codeAppend("{\n");
53 fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
54 fragBuilder->getMangleString().c_str(), childProc.name());
bsalomonb58a2b42016-09-26 06:55:02 -070055 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
56 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Michael Ludwig231de032018-08-30 14:33:01 -040057
58 // EmitArgs properly updates inputColor to half4(1) if it was null
egdaniel7ea439b2015-12-03 09:20:44 -080059 EmitArgs childArgs(fragBuilder,
60 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050061 args.fShaderCaps,
wangyix2a378432015-08-18 12:00:12 -070062 childProc,
Ethan Nicholas6ad52892019-05-03 13:13:42 +000063 outputColor,
64 inputName.size() > 0 ? inputName.c_str() : nullptr,
bsalomona624bf32016-09-20 09:12:47 -070065 coordVars,
Brian Salomon662ea4b2018-07-12 14:53:49 -040066 textureSamplers);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000067 this->childProcessor(childIndex)->emitCode(childArgs);
68 fragBuilder->codeAppend("}\n");
69
70 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -070071}
bsalomona624bf32016-09-20 09:12:47 -070072
73//////////////////////////////////////////////////////////////////////////////
74
bsalomonb58a2b42016-09-26 06:55:02 -070075GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
76 if (fFPStack.empty()) {
77 return nullptr;
bsalomona624bf32016-09-20 09:12:47 -070078 }
bsalomonb58a2b42016-09-26 06:55:02 -070079 GrGLSLFragmentProcessor* back = fFPStack.back();
80 fFPStack.pop_back();
81 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
82 fFPStack.push_back(back->childProcessor(i));
83 }
84 return back;
bsalomona624bf32016-09-20 09:12:47 -070085}