blob: e4d9151ccb82bc51a3a2b67297edac2c71bbdd68 [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:
wangyixb1daa862015-08-18 11:29:31 -0700129 /** Returns a new instance of the appropriate *GL* implementation class
130 for the given GrFragmentProcessor; caller is responsible for deleting
131 the object. */
132 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
133
wangyix4b3050b2015-08-04 07:59:37 -0700134 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
135 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
136 GrProcessorKeyBuilder* b) const = 0;
137
bsalomonde258cd2014-10-15 19:06:21 -0700138 /**
139 * Subclass implements this to support isEqual(). It will only be called if it is known that
140 * the two processors are of the same subclass (i.e. they return the same object from
141 * getFactory()). The processor subclass should not compare its coord transforms as that will
142 * be performed automatically in the non-virtual isEqual().
143 */
144 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
145
146 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700147
joshualitt290c09b2014-12-19 13:45:20 -0800148 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700149
150 /**
wangyix69ed1142015-08-18 07:24:29 -0700151 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
152 * proc's children. In other words, each proc stores all the transforms of its subtree as if
153 * they were collected using preorder traversal.
154 *
155 * Example:
156 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
157 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
158 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
159 *
160 * (A)
161 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
162 * / \
163 * / \
164 * (B) (D)
165 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
166 * / / \
167 * / / \
168 * (C) (E) (F)
169 * [c1] [e1,e2,e3] [f1,f2]
170 *
wangyix58d890b2015-08-12 09:40:47 -0700171 * The same goes for fTextureAccesses with textures.
172 */
173 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
174
wangyix93ab2542015-08-19 08:23:12 -0700175 int fNumTexturesExclChildren;
176 int fNumTransformsExclChildren;
177
bsalomonac856c92015-08-27 06:30:17 -0700178 // TODO: These must convert their processors to pending-execution refs when the parent is
179 // converted (do this automatically in GrProgramElement?).
180 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700181
182 typedef GrProcessor INHERITED;
183};
184
bsalomon6251d172014-10-15 10:50:36 -0700185#endif