blob: ac3d734c90b9852a42dae4cb1e92e67e3611806b [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
egdaniel64c47282015-11-13 06:54:19 -08008#include "GrGLSLFragmentProcessor.h"
wangyixb1daa862015-08-18 11:29:31 -07009#include "GrFragmentProcessor.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070010#include "GrProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080012#include "glsl/GrGLSLUniformHandler.h"
wangyixb1daa862015-08-18 11:29:31 -070013
egdaniel64c47282015-11-13 06:54:19 -080014void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman,
15 const GrFragmentProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -070016 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}
wangyix2a378432015-08-18 12:00:12 -070022
egdaniel64c47282015-11-13 06:54:19 -080023void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor, EmitArgs& args) {
bsalomon38ddbad2015-09-24 06:00:00 -070024 this->internalEmitChild(childIndex, inputColor, args.fOutputColor, args);
25}
26
egdaniel64c47282015-11-13 06:54:19 -080027void GrGLSLFragmentProcessor::emitChild(int childIndex, const char* inputColor,
egdaniel4ca2e602015-11-18 08:01:26 -080028 SkString* outputColor, EmitArgs& args) {
bsalomon38ddbad2015-09-24 06:00:00 -070029
30 SkASSERT(outputColor);
cdalton85285412016-02-18 12:37:07 -080031 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080032 outputColor->append(fragBuilder->getMangleString());
33 fragBuilder->codeAppendf("vec4 %s;", outputColor->c_str());
bsalomon38ddbad2015-09-24 06:00:00 -070034 this->internalEmitChild(childIndex, inputColor, outputColor->c_str(), args);
35}
36
egdaniel64c47282015-11-13 06:54:19 -080037void GrGLSLFragmentProcessor::internalEmitChild(int childIndex, const char* inputColor,
38 const char* outputColor, EmitArgs& args) {
cdalton85285412016-02-18 12:37:07 -080039 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon38ddbad2015-09-24 06:00:00 -070040
egdaniel4ca2e602015-11-18 08:01:26 -080041 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
wangyix2a378432015-08-18 12:00:12 -070042
43 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
44
wangyix2a378432015-08-18 12:00:12 -070045 /*
46 * We now want to find the subset of coords and samplers that belong to the child and its
wangyix93ab2542015-08-19 08:23:12 -070047 * descendants and put that into childCoords and childSamplers. To do so, we'll do a forwards
48 * linear search.
wangyix2a378432015-08-18 12:00:12 -070049 *
50 * Explanation:
51 * Each GrFragmentProcessor has a copy of all the transforms and textures of itself and
52 * all procs in its subtree. For example, suppose we have frag proc A, who has two children B
53 * and D. B has a child C, and D has two children E and F. Each frag proc's transforms array
54 * contains its own transforms, followed by the transforms of all its descendants (i.e. preorder
55 * traversal). Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms respectively.
56 *
57 * (A)
58 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
59 * / \
60 * / \
61 * (B) (D)
62 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
63 * / / \
64 * / / \
65 * (C) (E) (F)
66 * [c1] [e1,e2,e3] [f1,f2]
67 *
wangyix93ab2542015-08-19 08:23:12 -070068 * So if we're inside proc A's emitCode, and A is about to call emitCode on proc D, we want the
69 * EmitArgs that's passed onto D to only contain its and its descendants' coords. The
wangyix2a378432015-08-18 12:00:12 -070070 * EmitArgs given to A would contain the transforms [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2], and we want
wangyix93ab2542015-08-19 08:23:12 -070071 * to extract the subset [d1,e1,e2,e3,f1,f2] to pass on to D. We can do this with a linear
72 * search since we know that A has 1 transform (using A.numTransformsExclChildren()), and B's
73 * subtree has 3 transforms (using B.numTransforms()), so we know the start of D's transforms is
74 * 4 after the start of A's transforms.
wangyix2a378432015-08-18 12:00:12 -070075 * Textures work the same way as transforms.
76 */
wangyix93ab2542015-08-19 08:23:12 -070077 int firstCoordAt = args.fFp.numTransformsExclChildren();
78 int firstSamplerAt = args.fFp.numTexturesExclChildren();
79 for (int i = 0; i < childIndex; ++i) {
80 firstCoordAt += args.fFp.childProcessor(i).numTransforms();
81 firstSamplerAt += args.fFp.childProcessor(i).numTextures();
wangyix2a378432015-08-18 12:00:12 -070082 }
egdaniel7dc4bd02015-10-29 07:57:01 -070083 GrGLSLTransformedCoordsArray childCoords;
wangyix2a378432015-08-18 12:00:12 -070084 TextureSamplerArray childSamplers;
85 if (childProc.numTransforms() > 0) {
86 childCoords.push_back_n(childProc.numTransforms(), &args.fCoords[firstCoordAt]);
87 }
88 if (childProc.numTextures() > 0) {
89 childSamplers.push_back_n(childProc.numTextures(), &args.fSamplers[firstSamplerAt]);
90 }
91
92 // emit the code for the child in its own scope
egdaniel4ca2e602015-11-18 08:01:26 -080093 fragBuilder->codeAppend("{\n");
94 fragBuilder->codeAppendf("// Child Index %d (mangle: %s): %s\n", childIndex,
95 fragBuilder->getMangleString().c_str(), childProc.name());
egdaniel7ea439b2015-12-03 09:20:44 -080096 EmitArgs childArgs(fragBuilder,
97 args.fUniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -080098 args.fGLSLCaps,
wangyix2a378432015-08-18 12:00:12 -070099 childProc,
wangyix54a6b1a2015-09-08 08:41:51 -0700100 outputColor,
wangyix2a378432015-08-18 12:00:12 -0700101 inputColor,
102 childCoords,
103 childSamplers);
104 this->childProcessor(childIndex)->emitCode(childArgs);
egdaniel4ca2e602015-11-18 08:01:26 -0800105 fragBuilder->codeAppend("}\n");
wangyix2a378432015-08-18 12:00:12 -0700106
egdaniel4ca2e602015-11-18 08:01:26 -0800107 fragBuilder->onAfterChildProcEmitCode();
wangyix2a378432015-08-18 12:00:12 -0700108}