blob: 9c9f05acdb8479b50ceab2b06f82a980ad95e8b9 [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;
16class GrProcessorKeyBuilder;
bsalomon6251d172014-10-15 10:50:36 -070017
18/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
19 produce an output color. They may reference textures and uniforms. They may use
20 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
21 to the fragment being processed.
22 */
23class GrFragmentProcessor : public GrProcessor {
24public:
25 GrFragmentProcessor()
26 : INHERITED()
wangyix93ab2542015-08-19 08:23:12 -070027 , fUsesLocalCoords(false)
28 , fNumTexturesExclChildren(0)
29 , fNumTransformsExclChildren(0) {}
bsalomon6251d172014-10-15 10:50:36 -070030
bsalomonac856c92015-08-27 06:30:17 -070031 ~GrFragmentProcessor() override;
32
wangyixb1daa862015-08-18 11:29:31 -070033 GrGLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080034
wangyix4b3050b2015-08-04 07:59:37 -070035 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
36 this->onGetGLProcessorKey(caps, b);
37 for (int i = 0; i < fChildProcessors.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -070038 fChildProcessors[i]->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070039 }
40 }
41
wangyix93ab2542015-08-19 08:23:12 -070042 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
43
44 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
45
bsalomon6251d172014-10-15 10:50:36 -070046 int numTransforms() const { return fCoordTransforms.count(); }
47
48 /** Returns the coordinate transformation at index. index must be valid according to
49 numTransforms(). */
50 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
51
joshualittabb52a12015-01-13 15:02:10 -080052 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
53 return fCoordTransforms;
54 }
55
joshualitt2fe79232015-08-05 12:02:27 -070056 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070057 if (!fCoordTransforms.empty()) {
58 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070059 }
60 }
61
wangyix4b3050b2015-08-04 07:59:37 -070062 int numChildProcessors() const { return fChildProcessors.count(); }
63
bsalomonac856c92015-08-27 06:30:17 -070064 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -070065
joshualitt290c09b2014-12-19 13:45:20 -080066 /** Do any of the coordtransforms for this processor require local coords? */
67 bool usesLocalCoords() const { return fUsesLocalCoords; }
68
joshualitteb2a6762014-12-04 11:35:33 -080069 /** Returns true if this and other processor conservatively draw identically. It can only return
70 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070071 from getFactory()).
72
joshualitteb2a6762014-12-04 11:35:33 -080073 A return value of true from isEqual() should not be used to test whether the processor would
74 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070075 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070076
joshualitt56995b52014-12-11 15:44:02 -080077 /**
78 * This function is used to perform optimizations. When called the invarientOuput param
79 * indicate whether the input components to this processor in the FS will have known values.
80 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
81 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
82 * inout to indicate known values of its output. A component of the color member only has
83 * meaning if the corresponding bit in validFlags is set.
84 */
85 void computeInvariantOutput(GrInvariantOutput* inout) const;
86
bsalomon6251d172014-10-15 10:50:36 -070087protected:
wangyix93ab2542015-08-19 08:23:12 -070088 void addTextureAccess(const GrTextureAccess* textureAccess) override;
89
bsalomon6251d172014-10-15 10:50:36 -070090 /**
91 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070092 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
93 * in their FS code. The matrix expresses a transformation from local space. For a given
94 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
95 *
96 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -070097 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -070098 *
99 * This must only be called from the constructor because GrProcessors are immutable. The
100 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700101 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700102 *
103 * A processor subclass that has multiple methods of construction should always add its coord
104 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
105 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700106 */
107 void addCoordTransform(const GrCoordTransform*);
108
109 /**
wangyix58d890b2015-08-12 09:40:47 -0700110 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700111 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
112 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700113 * This is for processors whose shader code will be composed of nested processors whose output
114 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700115 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
116 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700117 */
wangyix58d890b2015-08-12 09:40:47 -0700118 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700119
120 /**
joshualitt56995b52014-12-11 15:44:02 -0800121 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700122 *
123 * Note: it's up to the subclass implementation to do any recursive call to compute the child
124 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800125 */
126 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
127
bsalomon6251d172014-10-15 10:50:36 -0700128private:
bsalomon42048002015-08-27 16:43:48 -0700129 void notifyRefCntIsZero() const final;
130
wangyixb1daa862015-08-18 11:29:31 -0700131 /** Returns a new instance of the appropriate *GL* implementation class
132 for the given GrFragmentProcessor; caller is responsible for deleting
133 the object. */
134 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
135
wangyix4b3050b2015-08-04 07:59:37 -0700136 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
137 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
138 GrProcessorKeyBuilder* b) const = 0;
139
bsalomonde258cd2014-10-15 19:06:21 -0700140 /**
141 * Subclass implements this to support isEqual(). It will only be called if it is known that
142 * the two processors are of the same subclass (i.e. they return the same object from
143 * getFactory()). The processor subclass should not compare its coord transforms as that will
144 * be performed automatically in the non-virtual isEqual().
145 */
146 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
147
148 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700149
joshualitt290c09b2014-12-19 13:45:20 -0800150 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700151
152 /**
wangyix69ed1142015-08-18 07:24:29 -0700153 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
154 * proc's children. In other words, each proc stores all the transforms of its subtree as if
155 * they were collected using preorder traversal.
156 *
157 * Example:
158 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
159 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
160 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
161 *
162 * (A)
163 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
164 * / \
165 * / \
166 * (B) (D)
167 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
168 * / / \
169 * / / \
170 * (C) (E) (F)
171 * [c1] [e1,e2,e3] [f1,f2]
172 *
wangyix58d890b2015-08-12 09:40:47 -0700173 * The same goes for fTextureAccesses with textures.
174 */
175 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
176
wangyix93ab2542015-08-19 08:23:12 -0700177 int fNumTexturesExclChildren;
178 int fNumTransformsExclChildren;
179
bsalomonac856c92015-08-27 06:30:17 -0700180 // TODO: These must convert their processors to pending-execution refs when the parent is
181 // converted (do this automatically in GrProgramElement?).
182 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700183
184 typedef GrProcessor INHERITED;
185};
186
bsalomon6251d172014-10-15 10:50:36 -0700187#endif