blob: 4b0e1e12d0568a5d6a213a863012dcbc126eea58 [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;
joshualitteb2a6762014-12-04 11:35:33 -080015class GrGLFragmentProcessor;
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 */
34 static const GrFragmentProcessor* MulOuputByInputAlpha(const GrFragmentProcessor*);
35
bsalomon6251d172014-10-15 10:50:36 -070036 GrFragmentProcessor()
37 : INHERITED()
wangyix93ab2542015-08-19 08:23:12 -070038 , fUsesLocalCoords(false)
39 , fNumTexturesExclChildren(0)
40 , fNumTransformsExclChildren(0) {}
bsalomon6251d172014-10-15 10:50:36 -070041
bsalomonac856c92015-08-27 06:30:17 -070042 ~GrFragmentProcessor() override;
43
wangyixb1daa862015-08-18 11:29:31 -070044 GrGLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080045
wangyix4b3050b2015-08-04 07:59:37 -070046 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
47 this->onGetGLProcessorKey(caps, b);
48 for (int i = 0; i < fChildProcessors.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -070049 fChildProcessors[i]->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070050 }
51 }
52
wangyix93ab2542015-08-19 08:23:12 -070053 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
54
55 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
56
bsalomon6251d172014-10-15 10:50:36 -070057 int numTransforms() const { return fCoordTransforms.count(); }
58
59 /** Returns the coordinate transformation at index. index must be valid according to
60 numTransforms(). */
61 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
62
joshualittabb52a12015-01-13 15:02:10 -080063 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
64 return fCoordTransforms;
65 }
66
joshualitt2fe79232015-08-05 12:02:27 -070067 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070068 if (!fCoordTransforms.empty()) {
69 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070070 }
71 }
72
wangyix4b3050b2015-08-04 07:59:37 -070073 int numChildProcessors() const { return fChildProcessors.count(); }
74
bsalomonac856c92015-08-27 06:30:17 -070075 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -070076
joshualitt290c09b2014-12-19 13:45:20 -080077 /** Do any of the coordtransforms for this processor require local coords? */
78 bool usesLocalCoords() const { return fUsesLocalCoords; }
79
joshualitteb2a6762014-12-04 11:35:33 -080080 /** Returns true if this and other processor conservatively draw identically. It can only return
81 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070082 from getFactory()).
83
joshualitteb2a6762014-12-04 11:35:33 -080084 A return value of true from isEqual() should not be used to test whether the processor would
85 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070086 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070087
joshualitt56995b52014-12-11 15:44:02 -080088 /**
89 * This function is used to perform optimizations. When called the invarientOuput param
90 * indicate whether the input components to this processor in the FS will have known values.
91 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
92 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
93 * inout to indicate known values of its output. A component of the color member only has
94 * meaning if the corresponding bit in validFlags is set.
95 */
bsalomonc21b09e2015-08-28 18:46:56 -070096 void computeInvariantOutput(GrInvariantOutput* inout) const {
97 this->onComputeInvariantOutput(inout);
98 }
joshualitt56995b52014-12-11 15:44:02 -080099
bsalomon6251d172014-10-15 10:50:36 -0700100protected:
wangyix93ab2542015-08-19 08:23:12 -0700101 void addTextureAccess(const GrTextureAccess* textureAccess) override;
102
bsalomon6251d172014-10-15 10:50:36 -0700103 /**
104 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700105 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
106 * in their FS code. The matrix expresses a transformation from local space. For a given
107 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
108 *
109 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700110 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700111 *
112 * This must only be called from the constructor because GrProcessors are immutable. The
113 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700114 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700115 *
116 * A processor subclass that has multiple methods of construction should always add its coord
117 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
118 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700119 */
120 void addCoordTransform(const GrCoordTransform*);
121
122 /**
wangyix58d890b2015-08-12 09:40:47 -0700123 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700124 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
125 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700126 * This is for processors whose shader code will be composed of nested processors whose output
127 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700128 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
129 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700130 */
wangyix58d890b2015-08-12 09:40:47 -0700131 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700132
133 /**
joshualitt56995b52014-12-11 15:44:02 -0800134 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700135 *
136 * Note: it's up to the subclass implementation to do any recursive call to compute the child
137 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800138 */
139 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
140
bsalomon6251d172014-10-15 10:50:36 -0700141private:
bsalomon42048002015-08-27 16:43:48 -0700142 void notifyRefCntIsZero() const final;
143
wangyixb1daa862015-08-18 11:29:31 -0700144 /** Returns a new instance of the appropriate *GL* implementation class
145 for the given GrFragmentProcessor; caller is responsible for deleting
146 the object. */
147 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
148
wangyix4b3050b2015-08-04 07:59:37 -0700149 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
150 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
151 GrProcessorKeyBuilder* b) const = 0;
152
bsalomonde258cd2014-10-15 19:06:21 -0700153 /**
154 * Subclass implements this to support isEqual(). It will only be called if it is known that
155 * the two processors are of the same subclass (i.e. they return the same object from
156 * getFactory()). The processor subclass should not compare its coord transforms as that will
157 * be performed automatically in the non-virtual isEqual().
158 */
159 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
160
161 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700162
joshualitt290c09b2014-12-19 13:45:20 -0800163 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700164
165 /**
wangyix69ed1142015-08-18 07:24:29 -0700166 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
167 * proc's children. In other words, each proc stores all the transforms of its subtree as if
168 * they were collected using preorder traversal.
169 *
170 * Example:
171 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
172 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
173 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
174 *
175 * (A)
176 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
177 * / \
178 * / \
179 * (B) (D)
180 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
181 * / / \
182 * / / \
183 * (C) (E) (F)
184 * [c1] [e1,e2,e3] [f1,f2]
185 *
wangyix58d890b2015-08-12 09:40:47 -0700186 * The same goes for fTextureAccesses with textures.
187 */
188 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
189
wangyix93ab2542015-08-19 08:23:12 -0700190 int fNumTexturesExclChildren;
191 int fNumTransformsExclChildren;
192
bsalomonac856c92015-08-27 06:30:17 -0700193 // TODO: These must convert their processors to pending-execution refs when the parent is
194 // converted (do this automatically in GrProgramElement?).
195 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700196
197 typedef GrProcessor INHERITED;
198};
199
bsalomon6251d172014-10-15 10:50:36 -0700200#endif