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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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" |
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); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 17 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 18 | |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 19 | void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) { |
| 20 | this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 23 | void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, |
| 24 | SkString* outputColor, EmitArgs& args) { |
| 25 | SkASSERT(outputColor); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 26 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 27 | outputColor->append(fragBuilder->getMangleString()); |
| 28 | fragBuilder->codeAppendf("half4 %s;", outputColor->c_str()); |
| 29 | this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args); |
| 30 | } |
| 31 | |
| 32 | void 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 | } |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 48 | |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 49 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 50 | |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 51 | // 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()); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 55 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 56 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 57 | |
| 58 | // EmitArgs properly updates inputColor to half4(1) if it was null |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 59 | EmitArgs childArgs(fragBuilder, |
| 60 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 61 | args.fShaderCaps, |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 62 | childProc, |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 63 | outputColor, |
| 64 | inputName.size() > 0 ? inputName.c_str() : nullptr, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 65 | coordVars, |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 66 | textureSamplers); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 67 | this->childProcessor(childIndex)->emitCode(childArgs); |
| 68 | fragBuilder->codeAppend("}\n"); |
| 69 | |
| 70 | fragBuilder->onAfterChildProcEmitCode(); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 71 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 72 | |
| 73 | ////////////////////////////////////////////////////////////////////////////// |
| 74 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 75 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() { |
| 76 | if (fFPStack.empty()) { |
| 77 | return nullptr; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 78 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 79 | 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; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 85 | } |