blob: 78c427da8585c16122b2c4b4193dc9047b50a062 [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
egdaniel64c47282015-11-13 06:54:19 -08008#include "GrGLSLFragmentProcessor.h"
wangyixb1daa862015-08-18 11:29:31 -07009#include "GrFragmentProcessor.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070010#include "GrProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080012#include "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);
17 SkASSERT(fChildProcessors.count() == processor.numChildProcessors());
18 for (int i = 0; i < fChildProcessors.count(); ++i) {
19 fChildProcessors[i]->setData(pdman, processor.childProcessor(i));
20 }
21}
wangyix2a378432015-08-18 12:00:12 -070022
egdaniel64c47282015-11-13 06:54:19 -080023void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) {
bsalomon38ddbad2015-09-24 06:00:00 -070024 this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args);
25}
26
egdaniel64c47282015-11-13 06:54:19 -080027void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor,
egdaniel4ca2e602015-11-18 08:01:26 -080028 SkString* outputColor, EmitArgs& args) {
bsalomon38ddbad2015-09-24 06:00:00 -070029 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080030 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080031 outputColor->append(fragBuilder->getMangleString());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040032 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
bsalomon38ddbad2015-09-24 06:00:00 -070033 this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args);
34}
35
egdaniel64c47282015-11-13 06:54:19 -080036void GrGLSLFragmentProcessor::internalEmitChild(int childIndex, const char* inputColor,
37 const char* outputColor, EmitArgs& args) {
cdalton85285412016-02-18 12:37:07 -080038 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon38ddbad2015-09-24 06:00:00 -070039
egdaniel4ca2e602015-11-18 08:01:26 -080040 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
wangyix2a378432015-08-18 12:00:12 -070041
Michael Ludwig231de032018-08-30 14:33:01 -040042 // Prepare a mangled input color variable if the default is not used,
43 // inputName remains the empty string if no variable is needed.
44 SkString inputName;
45 if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) {
46 // The input name is based off of the current mangle string, and
47 // since this is called after onBeforeChildProcEmitCode(), it will be
48 // unique to the child processor (exactly what we want for its input).
49 inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str());
50 fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor);
51 }
52
wangyix2a378432015-08-18 12:00:12 -070053 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
54
wangyix2a378432015-08-18 12:00:12 -070055 // emit the code for the child in its own scope
egdaniel4ca2e602015-11-18 08:01:26 -080056 fragBuilder->codeAppend("{\n");
57 fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
58 fragBuilder->getMangleString().c_str(), childProc.name());
bsalomonb58a2b42016-09-26 06:55:02 -070059 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
60 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Michael Ludwig231de032018-08-30 14:33:01 -040061
62 // EmitArgs properly updates inputColor to half4(1) if it was null
egdaniel7ea439b2015-12-03 09:20:44 -080063 EmitArgs childArgs(fragBuilder,
64 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050065 args.fShaderCaps,
wangyix2a378432015-08-18 12:00:12 -070066 childProc,
wangyix54a6b1a2015-09-08 08:41:51 -070067 outputColor,
Michael Ludwig231de032018-08-30 14:33:01 -040068 inputName.size() > 0 ? inputName.c_str() : nullptr,
bsalomona624bf32016-09-20 09:12:47 -070069 coordVars,
Brian Salomon662ea4b2018-07-12 14:53:49 -040070 textureSamplers);
wangyix2a378432015-08-18 12:00:12 -070071 this->childProcessor(childIndex)->emitCode(childArgs);
egdaniel4ca2e602015-11-18 08:01:26 -080072 fragBuilder->codeAppend("}\n");
wangyix2a378432015-08-18 12:00:12 -070073
egdaniel4ca2e602015-11-18 08:01:26 -080074 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -070075}
bsalomona624bf32016-09-20 09:12:47 -070076
77//////////////////////////////////////////////////////////////////////////////
78
bsalomonb58a2b42016-09-26 06:55:02 -070079GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
80 if (fFPStack.empty()) {
81 return nullptr;
bsalomona624bf32016-09-20 09:12:47 -070082 }
bsalomonb58a2b42016-09-26 06:55:02 -070083 GrGLSLFragmentProcessor* back = fFPStack.back();
84 fFPStack.pop_back();
85 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
86 fFPStack.push_back(back->childProcessor(i));
87 }
88 return back;
bsalomona624bf32016-09-20 09:12:47 -070089}