wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 8 | #include "GrGLSLFragmentProcessor.h" |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 9 | #include "GrFragmentProcessor.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 10 | #include "GrProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLUniformHandler.h" |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 13 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman, |
| 15 | const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 16 | 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 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 22 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 23 | void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) { |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 24 | this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args); |
| 25 | } |
| 26 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 27 | void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 28 | SkString* outputColor, EmitArgs& args) { |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 29 | SkASSERT(outputColor); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 30 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 31 | outputColor->append(fragBuilder->getMangleString()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 32 | fragBuilder->codeAppendf("half4 %s;", outputColor->c_str()); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 33 | this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args); |
| 34 | } |
| 35 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 36 | void GrGLSLFragmentProcessor::internalEmitChild(int childIndex, const char* inputColor, |
| 37 | const char* outputColor, EmitArgs& args) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 38 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 39 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 40 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 41 | |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 42 | // 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 | |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 53 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 54 | |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 55 | // emit the code for the child in its own scope |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 56 | fragBuilder->codeAppend("{\n"); |
| 57 | fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex, |
| 58 | fragBuilder->getMangleString().c_str(), childProc.name()); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 59 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 60 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 61 | |
| 62 | // EmitArgs properly updates inputColor to half4(1) if it was null |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 63 | EmitArgs childArgs(fragBuilder, |
| 64 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 65 | args.fShaderCaps, |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 66 | childProc, |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 67 | outputColor, |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 68 | inputName.size() > 0 ? inputName.c_str() : nullptr, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 69 | coordVars, |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 70 | textureSamplers); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 71 | this->childProcessor(childIndex)->emitCode(childArgs); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 72 | fragBuilder->codeAppend("}\n"); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 73 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 74 | fragBuilder->onAfterChildProcEmitCode(); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 75 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 76 | |
| 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 79 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() { |
| 80 | if (fFPStack.empty()) { |
| 81 | return nullptr; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 82 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 83 | 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; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 89 | } |