blob: 4b56fbf7287257214e13c26d1c665ea3ec9a2fbb [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 */
bungeman06ca8ec2016-06-09 08:01:03 -070034 static sk_sp<GrFragmentProcessor> MulOutputByInputAlpha(sk_sp<GrFragmentProcessor>);
bsalomonf1b7a1d2015-09-28 06:26:28 -070035
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 */
bungeman06ca8ec2016-06-09 08:01:03 -070041 static sk_sp<GrFragmentProcessor> MulOutputByInputUnpremulColor(sk_sp<GrFragmentProcessor>);
bsalomonf1b7a1d2015-09-28 06:26:28 -070042
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 */
bungeman06ca8ec2016-06-09 08:01:03 -070048 static sk_sp<GrFragmentProcessor> OverrideInput(sk_sp<GrFragmentProcessor>, GrColor);
bsalomon87ba62e2015-09-22 06:41:59 -070049
bsalomone25eea42015-09-29 06:38:55 -070050 /**
dvonbeckc526da92016-07-20 11:20:30 -070051 * Returns a fragment processor that premuls the input before calling the passed in fragment
52 * processor.
53 */
54 static sk_sp<GrFragmentProcessor> PremulInput(sk_sp<GrFragmentProcessor>);
55
56 /**
bsalomone25eea42015-09-29 06:38:55 -070057 * Returns a fragment processor that runs the passed in array of fragment processors in a
58 * series. The original input is passed to the first, the first's output is passed to the
59 * second, etc. The output of the returned processor is the output of the last processor of the
60 * series.
bungeman06ca8ec2016-06-09 08:01:03 -070061 *
62 * The array elements with be moved.
bsalomone25eea42015-09-29 06:38:55 -070063 */
bungeman06ca8ec2016-06-09 08:01:03 -070064 static sk_sp<GrFragmentProcessor> RunInSeries(sk_sp<GrFragmentProcessor>*, int cnt);
bsalomone25eea42015-09-29 06:38:55 -070065
bsalomon6251d172014-10-15 10:50:36 -070066 GrFragmentProcessor()
67 : INHERITED()
wangyix93ab2542015-08-19 08:23:12 -070068 , fUsesLocalCoords(false)
69 , fNumTexturesExclChildren(0)
cdalton74b8d322016-04-11 14:47:28 -070070 , fNumBuffersExclChildren(0)
wangyix93ab2542015-08-19 08:23:12 -070071 , fNumTransformsExclChildren(0) {}
bsalomon6251d172014-10-15 10:50:36 -070072
bsalomonac856c92015-08-27 06:30:17 -070073 ~GrFragmentProcessor() override;
74
egdaniel57d3b032015-11-13 11:57:27 -080075 GrGLSLFragmentProcessor* createGLSLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080076
egdaniel57d3b032015-11-13 11:57:27 -080077 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
78 this->onGetGLSLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070079 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -080080 fChildProcessors[i]->getGLSLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070081 }
82 }
83
wangyix93ab2542015-08-19 08:23:12 -070084 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
85
cdalton74b8d322016-04-11 14:47:28 -070086 int numBuffersExclChildren() const { return fNumBuffersExclChildren; }
87
wangyix93ab2542015-08-19 08:23:12 -070088 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
89
bsalomon6251d172014-10-15 10:50:36 -070090 int numTransforms() const { return fCoordTransforms.count(); }
91
92 /** Returns the coordinate transformation at index. index must be valid according to
93 numTransforms(). */
94 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
95
joshualittabb52a12015-01-13 15:02:10 -080096 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
97 return fCoordTransforms;
98 }
99
joshualitt2fe79232015-08-05 12:02:27 -0700100 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -0700101 if (!fCoordTransforms.empty()) {
102 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -0700103 }
104 }
105
wangyix4b3050b2015-08-04 07:59:37 -0700106 int numChildProcessors() const { return fChildProcessors.count(); }
107
bsalomonac856c92015-08-27 06:30:17 -0700108 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -0700109
joshualitt290c09b2014-12-19 13:45:20 -0800110 /** Do any of the coordtransforms for this processor require local coords? */
111 bool usesLocalCoords() const { return fUsesLocalCoords; }
112
joshualitteb2a6762014-12-04 11:35:33 -0800113 /** Returns true if this and other processor conservatively draw identically. It can only return
114 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -0700115 from getFactory()).
116
joshualitteb2a6762014-12-04 11:35:33 -0800117 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800118 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
119 */
wangyix54017d72015-08-18 07:39:33 -0700120 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -0700121
joshualitt56995b52014-12-11 15:44:02 -0800122 /**
123 * This function is used to perform optimizations. When called the invarientOuput param
124 * indicate whether the input components to this processor in the FS will have known values.
125 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
126 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
127 * inout to indicate known values of its output. A component of the color member only has
128 * meaning if the corresponding bit in validFlags is set.
129 */
bsalomonc21b09e2015-08-28 18:46:56 -0700130 void computeInvariantOutput(GrInvariantOutput* inout) const {
131 this->onComputeInvariantOutput(inout);
132 }
joshualitt56995b52014-12-11 15:44:02 -0800133
bsalomon6251d172014-10-15 10:50:36 -0700134protected:
wangyix93ab2542015-08-19 08:23:12 -0700135 void addTextureAccess(const GrTextureAccess* textureAccess) override;
cdalton74b8d322016-04-11 14:47:28 -0700136 void addBufferAccess(const GrBufferAccess*) override;
wangyix93ab2542015-08-19 08:23:12 -0700137
bsalomon6251d172014-10-15 10:50:36 -0700138 /**
139 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700140 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
141 * in their FS code. The matrix expresses a transformation from local space. For a given
142 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
143 *
144 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700145 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700146 *
147 * This must only be called from the constructor because GrProcessors are immutable. The
148 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700149 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700150 *
151 * A processor subclass that has multiple methods of construction should always add its coord
152 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
153 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700154 */
155 void addCoordTransform(const GrCoordTransform*);
156
157 /**
wangyix58d890b2015-08-12 09:40:47 -0700158 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700159 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
160 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700161 * This is for processors whose shader code will be composed of nested processors whose output
162 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700163 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
164 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700165 */
bungeman06ca8ec2016-06-09 08:01:03 -0700166 int registerChildProcessor(sk_sp<GrFragmentProcessor> child);
wangyix4b3050b2015-08-04 07:59:37 -0700167
168 /**
joshualitt56995b52014-12-11 15:44:02 -0800169 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700170 *
171 * Note: it's up to the subclass implementation to do any recursive call to compute the child
172 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800173 */
174 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
175
bsalomon6251d172014-10-15 10:50:36 -0700176private:
bsalomon42048002015-08-27 16:43:48 -0700177 void notifyRefCntIsZero() const final;
178
wangyixb1daa862015-08-18 11:29:31 -0700179 /** Returns a new instance of the appropriate *GL* implementation class
180 for the given GrFragmentProcessor; caller is responsible for deleting
181 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800182 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0;
wangyixb1daa862015-08-18 11:29:31 -0700183
wangyix4b3050b2015-08-04 07:59:37 -0700184 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
egdaniel57d3b032015-11-13 11:57:27 -0800185 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
186 GrProcessorKeyBuilder* b) const = 0;
wangyix4b3050b2015-08-04 07:59:37 -0700187
bsalomonde258cd2014-10-15 19:06:21 -0700188 /**
189 * Subclass implements this to support isEqual(). It will only be called if it is known that
190 * the two processors are of the same subclass (i.e. they return the same object from
191 * getFactory()). The processor subclass should not compare its coord transforms as that will
192 * be performed automatically in the non-virtual isEqual().
193 */
194 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
195
196 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700197
bungeman06ca8ec2016-06-09 08:01:03 -0700198 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700199
200 /**
wangyix69ed1142015-08-18 07:24:29 -0700201 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
202 * proc's children. In other words, each proc stores all the transforms of its subtree as if
203 * they were collected using preorder traversal.
204 *
205 * Example:
206 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
207 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
208 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
209 *
210 * (A)
211 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
212 * / \
213 * / \
214 * (B) (D)
215 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
216 * / / \
217 * / / \
218 * (C) (E) (F)
219 * [c1] [e1,e2,e3] [f1,f2]
220 *
wangyix58d890b2015-08-12 09:40:47 -0700221 * The same goes for fTextureAccesses with textures.
222 */
bungeman06ca8ec2016-06-09 08:01:03 -0700223 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
224 int fNumTexturesExclChildren;
225 int fNumBuffersExclChildren;
226 int fNumTransformsExclChildren;
227
228 /**
229 * This is not SkSTArray<1, sk_sp<GrFragmentProcessor>> because this class holds strong
230 * references until notifyRefCntIsZero and then it holds pending executions.
231 */
232 SkSTArray<1, GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700233
234 typedef GrProcessor INHERITED;
235};
236
bsalomon6251d172014-10-15 10:50:36 -0700237#endif