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