| 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 | |
| 8 | #include "src/gpu/GrCoordTransform.h" |
| 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" |
| 14 | |
| 15 | void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman, |
| 16 | const GrFragmentProcessor& processor) { |
| 17 | this->onSetData(pdman, processor); |
| 18 | } |
| 19 | |
| 20 | SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor, |
| 21 | EmitArgs& args, SkSL::String skslCoords) { |
| 22 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 23 | while (childIndex >= (int) fFunctionNames.size()) { |
| 24 | fFunctionNames.emplace_back(); |
| 25 | } |
| 26 | |
| 27 | // Subtle bug workaround: If an FP (this) has a child, and wishes to sample it, but does not |
| 28 | // want to *force* explicit coord sampling, then the obvious solution is to call it with |
| 29 | // invokeChild and no coords. However, if this FP is then adopted as a child of another FP that |
| 30 | // does want to sample with explicit coords, that property is propagated (recursively) to all |
| 31 | // children, and we need to supply explicit coords. So we propagate our own "_coords" (this is |
| 32 | // the name of our explicit coords parameter generated in the helper function). |
| 33 | if (args.fFp.isSampledWithExplicitCoords() && skslCoords.length() == 0) { |
| 34 | skslCoords = "_coords" ; |
| 35 | } |
| 36 | |
| 37 | const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex); |
| 38 | |
| 39 | // Emit the child's helper function if this is the first time we've seen a call |
| 40 | if (fFunctionNames[childIndex].size() == 0) { |
| 41 | fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated |
| 42 | |
| 43 | TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex); |
| 44 | TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex); |
| 45 | |
| 46 | EmitArgs childArgs(fragBuilder, |
| 47 | args.fUniformHandler, |
| 48 | args.fShaderCaps, |
| 49 | childProc, |
| 50 | "_output" , |
| 51 | "_input" , |
| 52 | coordVars, |
| 53 | textureSamplers); |
| 54 | fFunctionNames[childIndex] = |
| 55 | fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs); |
| 56 | |
| 57 | fragBuilder->onAfterChildProcEmitCode(); |
| 58 | } |
| 59 | |
| 60 | // If the fragment processor is invoked with overridden coordinates, it must *always* be invoked |
| 61 | // with overridden coords. |
| 62 | SkASSERT(childProc.isSampledWithExplicitCoords() == !skslCoords.empty()); |
| 63 | |
| 64 | // Produce a string containing the call to the helper function |
| 65 | SkString result = SkStringPrintf("%s(%s" , fFunctionNames[childIndex].c_str(), |
| 66 | inputColor ? inputColor : "half4(1)" ); |
| 67 | if (skslCoords.length()) { |
| 68 | result.appendf(", %s" , skslCoords.c_str()); |
| 69 | } |
| 70 | result.append(")" ); |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) { |
| 77 | for (int i = cnt - 1; i >= 0; --i) { |
| 78 | fFPStack.push_back(fps[i].get()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const { |
| 83 | return *fFPStack.back(); |
| 84 | } |
| 85 | |
| 86 | GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const { |
| 87 | return fFPStack.back(); |
| 88 | } |
| 89 | |
| 90 | GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() { |
| 91 | SkASSERT(!fFPStack.empty()); |
| 92 | const GrGLSLFragmentProcessor* back = fFPStack.back(); |
| 93 | fFPStack.pop_back(); |
| 94 | for (int i = back->numChildProcessors() - 1; i >= 0; --i) { |
| 95 | fFPStack.push_back(back->childProcessor(i)); |
| 96 | } |
| 97 | return *this; |
| 98 | } |
| 99 | |