OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrGLFragmentProcessor.h" | 8 #include "GrGLFragmentProcessor.h" |
9 #include "GrFragmentProcessor.h" | 9 #include "GrFragmentProcessor.h" |
| 10 #include "builders/GrGLFragmentShaderBuilder.h" |
| 11 #include "builders/GrGLProgramBuilder.h" |
10 | 12 |
11 void GrGLFragmentProcessor::setData(const GrGLProgramDataManager& pdman, | 13 void GrGLFragmentProcessor::setData(const GrGLProgramDataManager& pdman, |
12 const GrFragmentProcessor& processor) { | 14 const GrFragmentProcessor& processor) { |
13 this->onSetData(pdman, processor); | 15 this->onSetData(pdman, processor); |
14 SkASSERT(fChildProcessors.count() == processor.numChildProcessors()); | 16 SkASSERT(fChildProcessors.count() == processor.numChildProcessors()); |
15 for (int i = 0; i < fChildProcessors.count(); ++i) { | 17 for (int i = 0; i < fChildProcessors.count(); ++i) { |
16 fChildProcessors[i]->setData(pdman, processor.childProcessor(i)); | 18 fChildProcessors[i]->setData(pdman, processor.childProcessor(i)); |
17 } | 19 } |
18 } | 20 } |
| 21 |
| 22 void 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 h
as 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 descen
dants (i.e. preorder |
| 43 * traversal). Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transfor
ms 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' co
ords. 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 b
ackwards linear |
| 60 * search since we know that D's subtree has 6 transforms and B's subtree ha
s 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 1
0, not 1), so |
| 64 * we wouldn't know how many transforms to initially skip in A's array if us
ing 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[firstCo
ordAt]); |
| 78 } |
| 79 if (childProc.numTextures() > 0) { |
| 80 childSamplers.push_back_n(childProc.numTextures(), &args.fSamplers[first
SamplerAt]); |
| 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 } |
OLD | NEW |