blob: b0ef6b5845e1d82d89e343e79f3fe36da18c0949 [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
egdaniel57d3b032015-11-13 11:57:27 -080066 GrGLSLFragmentProcessor* createGLSLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080067
egdaniel57d3b032015-11-13 11:57:27 -080068 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
69 this->onGetGLSLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070070 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080071 fChildProcessors[i]->getGLSLProcessorKey(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
egdaniel57d3b032015-11-13 11:57:27 -0800107 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
108 */
wangyix54017d72015-08-18 07:39:33 -0700109 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -0700110
joshualitt56995b52014-12-11 15:44:02 -0800111 /**
112 * This function is used to perform optimizations. When called the invarientOuput param
113 * indicate whether the input components to this processor in the FS will have known values.
114 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
115 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
116 * inout to indicate known values of its output. A component of the color member only has
117 * meaning if the corresponding bit in validFlags is set.
118 */
bsalomonc21b09e2015-08-28 18:46:56 -0700119 void computeInvariantOutput(GrInvariantOutput* inout) const {
120 this->onComputeInvariantOutput(inout);
121 }
joshualitt56995b52014-12-11 15:44:02 -0800122
bsalomon6251d172014-10-15 10:50:36 -0700123protected:
wangyix93ab2542015-08-19 08:23:12 -0700124 void addTextureAccess(const GrTextureAccess* textureAccess) override;
125
bsalomon6251d172014-10-15 10:50:36 -0700126 /**
127 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700128 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
129 * in their FS code. The matrix expresses a transformation from local space. For a given
130 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
131 *
132 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700133 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700134 *
135 * This must only be called from the constructor because GrProcessors are immutable. The
136 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700137 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700138 *
139 * A processor subclass that has multiple methods of construction should always add its coord
140 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
141 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700142 */
143 void addCoordTransform(const GrCoordTransform*);
144
145 /**
wangyix58d890b2015-08-12 09:40:47 -0700146 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700147 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
148 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700149 * This is for processors whose shader code will be composed of nested processors whose output
150 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700151 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
152 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700153 */
wangyix58d890b2015-08-12 09:40:47 -0700154 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700155
156 /**
joshualitt56995b52014-12-11 15:44:02 -0800157 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700158 *
159 * Note: it's up to the subclass implementation to do any recursive call to compute the child
160 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800161 */
162 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
163
bsalomon6251d172014-10-15 10:50:36 -0700164private:
bsalomon42048002015-08-27 16:43:48 -0700165 void notifyRefCntIsZero() const final;
166
wangyixb1daa862015-08-18 11:29:31 -0700167 /** Returns a new instance of the appropriate *GL* implementation class
168 for the given GrFragmentProcessor; caller is responsible for deleting
169 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800170 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0;
wangyixb1daa862015-08-18 11:29:31 -0700171
wangyix4b3050b2015-08-04 07:59:37 -0700172 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
egdaniel57d3b032015-11-13 11:57:27 -0800173 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
174 GrProcessorKeyBuilder* b) const = 0;
wangyix4b3050b2015-08-04 07:59:37 -0700175
bsalomonde258cd2014-10-15 19:06:21 -0700176 /**
177 * Subclass implements this to support isEqual(). It will only be called if it is known that
178 * the two processors are of the same subclass (i.e. they return the same object from
179 * getFactory()). The processor subclass should not compare its coord transforms as that will
180 * be performed automatically in the non-virtual isEqual().
181 */
182 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
183
184 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700185
joshualitt290c09b2014-12-19 13:45:20 -0800186 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700187
188 /**
wangyix69ed1142015-08-18 07:24:29 -0700189 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
190 * proc's children. In other words, each proc stores all the transforms of its subtree as if
191 * they were collected using preorder traversal.
192 *
193 * Example:
194 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
195 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
196 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
197 *
198 * (A)
199 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
200 * / \
201 * / \
202 * (B) (D)
203 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
204 * / / \
205 * / / \
206 * (C) (E) (F)
207 * [c1] [e1,e2,e3] [f1,f2]
208 *
wangyix58d890b2015-08-12 09:40:47 -0700209 * The same goes for fTextureAccesses with textures.
210 */
211 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
212
wangyix93ab2542015-08-19 08:23:12 -0700213 int fNumTexturesExclChildren;
214 int fNumTransformsExclChildren;
215
bsalomonac856c92015-08-27 06:30:17 -0700216 // TODO: These must convert their processors to pending-execution refs when the parent is
217 // converted (do this automatically in GrProgramElement?).
218 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700219
220 typedef GrProcessor INHERITED;
221};
222
bsalomon6251d172014-10-15 10:50:36 -0700223#endif