blob: d2902c26ac30734cac52a979a3a0898cc8025c71 [file] [log] [blame]
wangyixb1daa862015-08-18 11:29:31 -07001/*
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 "GrGLFragmentProcessor.h"
9#include "GrFragmentProcessor.h"
wangyix2a378432015-08-18 12:00:12 -070010#include "builders/GrGLFragmentShaderBuilder.h"
11#include "builders/GrGLProgramBuilder.h"
wangyixb1daa862015-08-18 11:29:31 -070012
13void GrGLFragmentProcessor::setData(const GrGLProgramDataManager& pdman,
14 const GrFragmentProcessor& processor) {
15 this->onSetData(pdman, processor);
16 SkASSERT(fChildProcessors.count() == processor.numChildProcessors());
17 for (int i = 0; i < fChildProcessors.count(); ++i) {
18 fChildProcessors[i]->setData(pdman, processor.childProcessor(i));
19 }
20}
wangyix2a378432015-08-18 12:00:12 -070021
22void GrGLFragmentProcessor::emitChild(int childIndex, const char* inputColor,
23 SkString* outputColor, EmitArgs& args) {
24 GrGLFragmentBuilder* fb = args.fBuilder->getFragmentShaderBuilder();
25 fb->onBeforeChildProcEmitCode(); // call first so mangleString is updated
26
27 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
28
29 // Mangle the name of the outputColor
30 outputColor->set(args.fOutputColor);
31 outputColor->append(fb->getMangleStringThisLevel());
32
33 /*
34 * We now want to find the subset of coords and samplers that belong to the child and its
35 * descendants and put that into childCoords and childSamplers. To do so, we must do a
36 * backwards linear search on coords and samplers.
37 *
38 * Explanation:
39 * Each GrFragmentProcessor has a copy of all the transforms and textures of itself and
40 * all procs in its subtree. For example, suppose we have frag proc A, who has two children B
41 * and D. B has a child C, and D has two children E and F. Each frag proc's transforms array
42 * contains its own transforms, followed by the transforms of all its descendants (i.e. preorder
43 * traversal). Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms respectively.
44 *
45 * (A)
46 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
47 * / \
48 * / \
49 * (B) (D)
50 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
51 * / / \
52 * / / \
53 * (C) (E) (F)
54 * [c1] [e1,e2,e3] [f1,f2]
55 *
56 * So if we're inside proc A's emitCode, and A is about to call emitCode on proc B, we want the
57 * EmitArgs that's passed onto B to only contain its and its descendants' coords. The
58 * EmitArgs given to A would contain the transforms [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2], and we want
59 * to extract the subset [b1,b2,c1] to pass on to B. We can do this with a backwards linear
60 * search since we know that D's subtree has 6 transforms and B's subtree has 3 transforms (by
61 * calling D.numTextures() and B.numTextures()), so we know the start of B's transforms is 9
62 * from the end of A's transforms. We cannot do this with a forwards linear search since we
63 * don't know how many transforms belong to A (A.numTextures() will return 10, not 1), so
64 * we wouldn't know how many transforms to initially skip in A's array if using a forward linear
65 * search.
66 * Textures work the same way as transforms.
67 */
68 int firstCoordAt = args.fFp.numTransforms();
69 int firstSamplerAt = args.fFp.numTextures();
70 for (int i = args.fFp.numChildProcessors() - 1; i >= childIndex; --i) {
71 firstCoordAt -= args.fFp.childProcessor(i).numTransforms();
72 firstSamplerAt -= args.fFp.childProcessor(i).numTextures();
73 }
74 TransformedCoordsArray childCoords;
75 TextureSamplerArray childSamplers;
76 if (childProc.numTransforms() > 0) {
77 childCoords.push_back_n(childProc.numTransforms(), &args.fCoords[firstCoordAt]);
78 }
79 if (childProc.numTextures() > 0) {
80 childSamplers.push_back_n(childProc.numTextures(), &args.fSamplers[firstSamplerAt]);
81 }
82
83 // emit the code for the child in its own scope
84 fb->codeAppendf("vec4 %s;\n", outputColor->c_str());
85 fb->codeAppend("{\n");
86 fb->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
87 fb->getMangleString().c_str(), childProc.name());
88 EmitArgs childArgs(args.fBuilder,
89 childProc,
90 outputColor->c_str(),
91 inputColor,
92 childCoords,
93 childSamplers);
94 this->childProcessor(childIndex)->emitCode(childArgs);
95 fb->codeAppend("}\n");
96
97 fb->onAfterChildProcEmitCode();
98}