2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLSLFragmentProcessor_DEFINED
9 #define GrGLSLFragmentProcessor_DEFINED
11 #include "glsl/GrGLSLProcessorTypes.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
13 #include "glsl/GrGLSLTextureSampler.h"
16 class GrProcessorKeyBuilder;
17 class GrGLSLFPBuilder;
18 class GrGLSLFragmentBuilder;
21 class GrGLSLFragmentProcessor {
23 GrGLSLFragmentProcessor() {}
25 virtual ~GrGLSLFragmentProcessor() {
26 for (int i = 0; i < fChildProcessors.count(); ++i) {
27 delete fChildProcessors[i];
31 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
32 typedef GrGLSLTextureSampler::TextureSamplerArray TextureSamplerArray;
34 /** Called when the program stage should insert its code into the shaders. The code in each
35 shader will be in its own block ({}) and so locally scoped names will not collide across
38 @param builder Interface used to emit code in the shaders.
39 @param processor The processor that generated this program stage.
40 @param key The key that was computed by GenKey() from the generating GrProcessor.
41 @param outputColor A predefined vec4 in the FS in which the stage should place its output
43 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be
44 nullptr in which case the implied input is solid white (all ones).
45 TODO: Better system for communicating optimization info (e.g. input
46 color is solid white, trans black, known to be opaque, etc.) that allows
47 the processor to communicate back similar known info about its output.
48 @param samplers Contains one entry for each GrTextureAccess of the GrProcessor. These
49 can be passed to the builder to emit texture reads in the generated
54 EmitArgs(GrGLSLFPBuilder* builder,
55 GrGLSLFragmentBuilder* fragBuilder,
56 const GrGLSLCaps* caps,
57 const GrFragmentProcessor& fp,
58 const char* outputColor,
59 const char* inputColor,
60 const GrGLSLTransformedCoordsArray& coords,
61 const TextureSamplerArray& samplers)
63 , fFragBuilder(fragBuilder)
66 , fOutputColor(outputColor)
67 , fInputColor(inputColor)
69 , fSamplers(samplers) {}
70 GrGLSLFPBuilder* fBuilder;
71 GrGLSLFragmentBuilder* fFragBuilder;
72 const GrGLSLCaps* fGLSLCaps;
73 const GrFragmentProcessor& fFp;
74 const char* fOutputColor;
75 const char* fInputColor;
76 const GrGLSLTransformedCoordsArray& fCoords;
77 const TextureSamplerArray& fSamplers;
80 virtual void emitCode(EmitArgs&) = 0;
82 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
84 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
86 int numChildProcessors() const { return fChildProcessors.count(); }
88 GrGLSLFragmentProcessor* childProcessor(int index) const {
89 return fChildProcessors[index];
92 /** Will emit the code of a child proc in its own scope. Pass in the parent's EmitArgs and
93 * emitChild will automatically extract the coords and samplers of that child and pass them
94 * on to the child's emitCode(). Also, any uniforms or functions emitted by the child will
95 * have their names mangled to prevent redefinitions. The output color name is also mangled
96 * therefore in an in/out param. It will be declared in mangled form by emitChild(). It is
97 * legal to pass nullptr as inputColor, since all fragment processors are required to work
98 * without an input color.
100 void emitChild(int childIndex, const char* inputColor, SkString* outputColor,
101 EmitArgs& parentArgs);
103 /** Variation that uses the parent's output color variable to hold the child's output.*/
104 void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs);
107 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
108 the same stage key; this function reads data from a GrFragmentProcessor and uploads any
109 uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
110 parameter is guaranteed to be of the same type that created this GrGLSLFragmentProcessor and
111 to have an identical processor key as the one that created this GrGLSLFragmentProcessor. */
112 // TODO update this to pass in GrFragmentProcessor
113 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {}
116 void internalEmitChild(int, const char*, const char*, EmitArgs&);
118 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
120 friend class GrFragmentProcessor;