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 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrCoordTransform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrFragmentProcessor.h" |
| 10 | #include "src/gpu/GrProcessor.h" |
| 11 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 13 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 14 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 15 | void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman, |
| 16 | const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 17 | this->onSetData(pdman, processor); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 18 | } |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 19 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 20 | void GrGLSLFragmentProcessor::writeChildCall(GrGLSLFPFragmentBuilder* fragBuilder, int childIndex, |
| 21 | TransformedCoordVars coordVars, const char* inputColor, |
| 22 | const char* outputColor, EmitArgs& args, |
| 23 | SkSL::String skslCoords) { |
| 24 | std::vector<SkString> coordParams; |
| 25 | for (int i = 0; i < coordVars.count(); ++i) { |
| 26 | coordParams.push_back(fragBuilder->ensureCoords2D(coordVars[i].fVaryingPoint)); |
| 27 | } |
| 28 | // if the fragment processor is invoked with overridden coordinates, it must *always* be invoked |
| 29 | // with overridden coords |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 30 | SkASSERT(args.fFp.coordTransformsApplyToLocalCoords() == (skslCoords.length() == 0)); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 31 | fragBuilder->codeAppendf("%s = %s(%s", outputColor, fFunctionNames[childIndex].c_str(), |
| 32 | inputColor ? inputColor : "half4(1)"); |
| 33 | if (skslCoords.length()) { |
| 34 | fragBuilder->codeAppendf(", %s", skslCoords.c_str()); |
| 35 | } |
| 36 | fragBuilder->codeAppend(");\n"); |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 39 | void GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 40 | SkString* outputColor, EmitArgs& args, |
| 41 | SkSL::String skslCoords) { |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 42 | SkASSERT(outputColor); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 43 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 44 | outputColor->append(fragBuilder->getMangleString()); |
| 45 | fragBuilder->codeAppendf("half4 %s;", outputColor->c_str()); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 46 | while (childIndex >= (int) fFunctionNames.size()) { |
| 47 | fFunctionNames.emplace_back(); |
| 48 | } |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 49 | if (!args.fFp.coordTransformsApplyToLocalCoords() && skslCoords.length() == 0) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 50 | skslCoords = "_coords"; |
| 51 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 52 | if (fFunctionNames[childIndex].size() == 0) { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 53 | this->internalInvokeChild(childIndex, inputColor, outputColor->c_str(), args, skslCoords); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 54 | } else { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 55 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 56 | |
| 57 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 58 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
| 59 | EmitArgs childArgs(fragBuilder, |
| 60 | args.fUniformHandler, |
| 61 | args.fShaderCaps, |
| 62 | childProc, |
| 63 | outputColor->c_str(), |
| 64 | "_input", |
| 65 | coordVars, |
| 66 | textureSamplers); |
| 67 | this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor->c_str(), |
| 68 | childArgs, skslCoords); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 69 | } |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 72 | void GrGLSLFragmentProcessor::internalInvokeChild(int childIndex, const char* inputColor, |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 73 | const char* outputColor, EmitArgs& args, |
| 74 | SkSL::String skslCoords) { |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 75 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 76 | |
| 77 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
| 78 | |
| 79 | // Prepare a mangled input color variable if the default is not used, |
| 80 | // inputName remains the empty string if no variable is needed. |
| 81 | SkString inputName; |
| 82 | if (inputColor&& strcmp("half4(1.0)", inputColor) != 0 && strcmp("half4(1)", inputColor) != 0) { |
| 83 | // The input name is based off of the current mangle string, and |
| 84 | // since this is called after onBeforeChildProcEmitCode(), it will be |
| 85 | // unique to the child processor (exactly what we want for its input). |
| 86 | inputName.appendf("_childInput%s", fragBuilder->getMangleString().c_str()); |
| 87 | fragBuilder->codeAppendf("half4 %s = %s;", inputName.c_str(), inputColor); |
| 88 | } |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 89 | |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 90 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 91 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 92 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 93 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 94 | EmitArgs childArgs(fragBuilder, |
| 95 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 96 | args.fShaderCaps, |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 97 | childProc, |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 98 | outputColor, |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 99 | "_input", |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 100 | coordVars, |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 101 | textureSamplers); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 102 | fFunctionNames[childIndex] = fragBuilder->writeProcessorFunction( |
| 103 | this->childProcessor(childIndex), |
| 104 | childArgs); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 105 | this->writeChildCall(fragBuilder, childIndex, coordVars, inputColor, outputColor, childArgs, |
| 106 | skslCoords); |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 107 | fragBuilder->onAfterChildProcEmitCode(); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 108 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 112 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 113 | for (int i = cnt - 1; i >= 0; --i) { |
| 114 | fFPStack.push_back(fps[i].get()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 115 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 119 | return *fFPStack.back(); |
| 120 | } |
| 121 | |
| 122 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 123 | return fFPStack.back(); |
| 124 | } |
| 125 | |
| 126 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 127 | SkASSERT(!fFPStack.empty()); |
| 128 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 129 | fFPStack.pop_back(); |
| 130 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 131 | fFPStack.push_back(back->childProcessor(i)); |
| 132 | } |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 133 | return *this; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 134 | } |