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 "GrGLSLFragmentProcessor.h"
9 #include "GrFragmentProcessor.h"
10 #include "GrProcessor.h"
11 #include "glsl/GrGLSLFragmentShaderBuilder.h"
12 #include "glsl/GrGLSLUniformHandler.h"
13
setData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)14 void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman,
15 const GrFragmentProcessor& processor) {
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 }
22
emitChild(int childIndex,const char * inputColor,EmitArgs & args)23 void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) {
24 this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args);
25 }
26
emitChild(int childIndex,const char * inputColor,SkString * outputColor,EmitArgs & args)27 void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor,
28 SkString* outputColor, EmitArgs& args) {
29 SkASSERT(outputColor);
30 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
31 outputColor->append(fragBuilder->getMangleString());
32 fragBuilder->codeAppendf("half4 %s;", outputColor->c_str());
33 this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args);
34 }
35
internalEmitChild(int childIndex,const char * inputColor,const char * outputColor,EmitArgs & args)36 void GrGLSLFragmentProcessor::internalEmitChild(int childIndex, const char* inputColor,
37 const char* outputColor, EmitArgs& args) {
38 SkASSERT(inputColor);
39 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
40
41 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
42
43 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
44
45 // emit the code for the child in its own scope
46 fragBuilder->codeAppend("{\n");
47 fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
48 fragBuilder->getMangleString().c_str(), childProc.name());
49 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
50 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
51 TexelBuffers texelBuffers = args.fTexelBuffers.childInputs(childIndex);
52 EmitArgs childArgs(fragBuilder,
53 args.fUniformHandler,
54 args.fShaderCaps,
55 childProc,
56 outputColor,
57 inputColor,
58 coordVars,
59 textureSamplers,
60 texelBuffers);
61 this->childProcessor(childIndex)->emitCode(childArgs);
62 fragBuilder->codeAppend("}\n");
63
64 fragBuilder->onAfterChildProcEmitCode();
65 }
66
67 //////////////////////////////////////////////////////////////////////////////
68
next()69 GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::next() {
70 if (fFPStack.empty()) {
71 return nullptr;
72 }
73 GrGLSLFragmentProcessor* back = fFPStack.back();
74 fFPStack.pop_back();
75 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
76 fFPStack.push_back(back->childProcessor(i));
77 }
78 return back;
79 }
80