blob: 428466212cd28780da74fbd8b2dc3b3e71c3e6da [file] [log] [blame]
bsalomon6251d172014-10-15 10:50:36 -07001/*
2 * Copyright 2014 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#ifndef GrFragmentProcessor_DEFINED
9#define GrFragmentProcessor_DEFINED
10
11#include "GrProcessor.h"
12
13class GrCoordTransform;
jvanverthe9c0fc62015-04-29 11:18:05 -070014class GrGLSLCaps;
egdaniel64c47282015-11-13 06:54:19 -080015class GrGLSLFragmentProcessor;
bsalomonc21b09e2015-08-28 18:46:56 -070016class GrInvariantOutput;
joshualitteb2a6762014-12-04 11:35:33 -080017class GrProcessorKeyBuilder;
bsalomon6251d172014-10-15 10:50:36 -070018
19/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
20 produce an output color. They may reference textures and uniforms. They may use
21 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
22 to the fragment being processed.
23 */
24class GrFragmentProcessor : public GrProcessor {
25public:
bsalomon87ba62e2015-09-22 06:41:59 -070026 /**
27 * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to
28 * only consider the input color's alpha. However, there is a competing desire to have reusable
29 * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input
30 * color is considered. This function exists to filter the input color and pass it to a FP. It
31 * does so by returning a parent FP that multiplies the passed in FPs output by the parent's
32 * input alpha. The passed in FP will not receive an input color.
33 */
bsalomonf1b7a1d2015-09-28 06:26:28 -070034 static const GrFragmentProcessor* MulOutputByInputAlpha(const GrFragmentProcessor*);
35
36 /**
37 * Similar to the above but it modulates the output r,g,b of the child processor by the input
38 * rgb and then multiplies all the components by the input alpha. This effectively modulates
39 * the child processor's premul color by a unpremul'ed input and produces a premul output
40 */
41 static const GrFragmentProcessor* MulOutputByInputUnpremulColor(const GrFragmentProcessor*);
42
43 /**
bsalomone25eea42015-09-29 06:38:55 -070044 * Returns a parent fragment processor that adopts the passed fragment processor as a child.
45 * The parent will ignore its input color and instead feed the passed in color as input to the
46 * child.
bsalomonf1b7a1d2015-09-28 06:26:28 -070047 */
48 static const GrFragmentProcessor* OverrideInput(const GrFragmentProcessor*, GrColor);
bsalomon87ba62e2015-09-22 06:41:59 -070049
bsalomone25eea42015-09-29 06:38:55 -070050 /**
51 * Returns a fragment processor that runs the passed in array of fragment processors in a
52 * series. The original input is passed to the first, the first's output is passed to the
53 * second, etc. The output of the returned processor is the output of the last processor of the
54 * series.
55 */
56 static const GrFragmentProcessor* RunInSeries(const GrFragmentProcessor*[], int cnt);
57
bsalomon6251d172014-10-15 10:50:36 -070058 GrFragmentProcessor()
59 : INHERITED()
wangyix93ab2542015-08-19 08:23:12 -070060 , fUsesLocalCoords(false)
61 , fNumTexturesExclChildren(0)
62 , fNumTransformsExclChildren(0) {}
bsalomon6251d172014-10-15 10:50:36 -070063
bsalomonac856c92015-08-27 06:30:17 -070064 ~GrFragmentProcessor() override;
65
egdaniel64c47282015-11-13 06:54:19 -080066 GrGLSLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080067
wangyix4b3050b2015-08-04 07:59:37 -070068 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
69 this->onGetGLProcessorKey(caps, b);
70 for (int i = 0; i < fChildProcessors.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -070071 fChildProcessors[i]->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070072 }
73 }
74
wangyix93ab2542015-08-19 08:23:12 -070075 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
76
77 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
78
bsalomon6251d172014-10-15 10:50:36 -070079 int numTransforms() const { return fCoordTransforms.count(); }
80
81 /** Returns the coordinate transformation at index. index must be valid according to
82 numTransforms(). */
83 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
84
joshualittabb52a12015-01-13 15:02:10 -080085 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
86 return fCoordTransforms;
87 }
88
joshualitt2fe79232015-08-05 12:02:27 -070089 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070090 if (!fCoordTransforms.empty()) {
91 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070092 }
93 }
94
wangyix4b3050b2015-08-04 07:59:37 -070095 int numChildProcessors() const { return fChildProcessors.count(); }
96
bsalomonac856c92015-08-27 06:30:17 -070097 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -070098
joshualitt290c09b2014-12-19 13:45:20 -080099 /** Do any of the coordtransforms for this processor require local coords? */
100 bool usesLocalCoords() const { return fUsesLocalCoords; }
101
joshualitteb2a6762014-12-04 11:35:33 -0800102 /** Returns true if this and other processor conservatively draw identically. It can only return
103 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -0700104 from getFactory()).
105
joshualitteb2a6762014-12-04 11:35:33 -0800106 A return value of true from isEqual() should not be used to test whether the processor would
107 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -0700108 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -0700109
joshualitt56995b52014-12-11 15:44:02 -0800110 /**
111 * This function is used to perform optimizations. When called the invarientOuput param
112 * indicate whether the input components to this processor in the FS will have known values.
113 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
114 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
115 * inout to indicate known values of its output. A component of the color member only has
116 * meaning if the corresponding bit in validFlags is set.
117 */
bsalomonc21b09e2015-08-28 18:46:56 -0700118 void computeInvariantOutput(GrInvariantOutput* inout) const {
119 this->onComputeInvariantOutput(inout);
120 }
joshualitt56995b52014-12-11 15:44:02 -0800121
bsalomon6251d172014-10-15 10:50:36 -0700122protected:
wangyix93ab2542015-08-19 08:23:12 -0700123 void addTextureAccess(const GrTextureAccess* textureAccess) override;
124
bsalomon6251d172014-10-15 10:50:36 -0700125 /**
126 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700127 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
128 * in their FS code. The matrix expresses a transformation from local space. For a given
129 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
130 *
131 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700132 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700133 *
134 * This must only be called from the constructor because GrProcessors are immutable. The
135 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700136 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700137 *
138 * A processor subclass that has multiple methods of construction should always add its coord
139 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
140 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700141 */
142 void addCoordTransform(const GrCoordTransform*);
143
144 /**
wangyix58d890b2015-08-12 09:40:47 -0700145 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700146 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
147 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700148 * This is for processors whose shader code will be composed of nested processors whose output
149 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700150 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
151 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700152 */
wangyix58d890b2015-08-12 09:40:47 -0700153 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700154
155 /**
joshualitt56995b52014-12-11 15:44:02 -0800156 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700157 *
158 * Note: it's up to the subclass implementation to do any recursive call to compute the child
159 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800160 */
161 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
162
bsalomon6251d172014-10-15 10:50:36 -0700163private:
bsalomon42048002015-08-27 16:43:48 -0700164 void notifyRefCntIsZero() const final;
165
wangyixb1daa862015-08-18 11:29:31 -0700166 /** Returns a new instance of the appropriate *GL* implementation class
167 for the given GrFragmentProcessor; caller is responsible for deleting
168 the object. */
egdaniel64c47282015-11-13 06:54:19 -0800169 virtual GrGLSLFragmentProcessor* onCreateGLInstance() const = 0;
wangyixb1daa862015-08-18 11:29:31 -0700170
wangyix4b3050b2015-08-04 07:59:37 -0700171 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
172 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
173 GrProcessorKeyBuilder* b) const = 0;
174
bsalomonde258cd2014-10-15 19:06:21 -0700175 /**
176 * Subclass implements this to support isEqual(). It will only be called if it is known that
177 * the two processors are of the same subclass (i.e. they return the same object from
178 * getFactory()). The processor subclass should not compare its coord transforms as that will
179 * be performed automatically in the non-virtual isEqual().
180 */
181 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
182
183 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700184
joshualitt290c09b2014-12-19 13:45:20 -0800185 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700186
187 /**
wangyix69ed1142015-08-18 07:24:29 -0700188 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
189 * proc's children. In other words, each proc stores all the transforms of its subtree as if
190 * they were collected using preorder traversal.
191 *
192 * Example:
193 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
194 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
195 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
196 *
197 * (A)
198 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
199 * / \
200 * / \
201 * (B) (D)
202 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
203 * / / \
204 * / / \
205 * (C) (E) (F)
206 * [c1] [e1,e2,e3] [f1,f2]
207 *
wangyix58d890b2015-08-12 09:40:47 -0700208 * The same goes for fTextureAccesses with textures.
209 */
210 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
211
wangyix93ab2542015-08-19 08:23:12 -0700212 int fNumTexturesExclChildren;
213 int fNumTransformsExclChildren;
214
bsalomonac856c92015-08-27 06:30:17 -0700215 // TODO: These must convert their processors to pending-execution refs when the parent is
216 // converted (do this automatically in GrProgramElement?).
217 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700218
219 typedef GrProcessor INHERITED;
220};
221
bsalomon6251d172014-10-15 10:50:36 -0700222#endif