blob: a5e0d98c4d3fa490df3e04092d2fa9e11a85b970 [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:
26 GrFragmentProcessor()
27 : INHERITED()
wangyix93ab2542015-08-19 08:23:12 -070028 , fUsesLocalCoords(false)
29 , fNumTexturesExclChildren(0)
30 , fNumTransformsExclChildren(0) {}
bsalomon6251d172014-10-15 10:50:36 -070031
bsalomonac856c92015-08-27 06:30:17 -070032 ~GrFragmentProcessor() override;
33
wangyixb1daa862015-08-18 11:29:31 -070034 GrGLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080035
wangyix4b3050b2015-08-04 07:59:37 -070036 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
37 this->onGetGLProcessorKey(caps, b);
38 for (int i = 0; i < fChildProcessors.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -070039 fChildProcessors[i]->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070040 }
41 }
42
wangyix93ab2542015-08-19 08:23:12 -070043 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
44
45 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
46
bsalomon6251d172014-10-15 10:50:36 -070047 int numTransforms() const { return fCoordTransforms.count(); }
48
49 /** Returns the coordinate transformation at index. index must be valid according to
50 numTransforms(). */
51 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
52
joshualittabb52a12015-01-13 15:02:10 -080053 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
54 return fCoordTransforms;
55 }
56
joshualitt2fe79232015-08-05 12:02:27 -070057 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070058 if (!fCoordTransforms.empty()) {
59 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070060 }
61 }
62
wangyix4b3050b2015-08-04 07:59:37 -070063 int numChildProcessors() const { return fChildProcessors.count(); }
64
bsalomonac856c92015-08-27 06:30:17 -070065 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -070066
joshualitt290c09b2014-12-19 13:45:20 -080067 /** Do any of the coordtransforms for this processor require local coords? */
68 bool usesLocalCoords() const { return fUsesLocalCoords; }
69
joshualitteb2a6762014-12-04 11:35:33 -080070 /** Returns true if this and other processor conservatively draw identically. It can only return
71 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070072 from getFactory()).
73
joshualitteb2a6762014-12-04 11:35:33 -080074 A return value of true from isEqual() should not be used to test whether the processor would
75 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070076 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070077
joshualitt56995b52014-12-11 15:44:02 -080078 /**
79 * This function is used to perform optimizations. When called the invarientOuput param
80 * indicate whether the input components to this processor in the FS will have known values.
81 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
82 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
83 * inout to indicate known values of its output. A component of the color member only has
84 * meaning if the corresponding bit in validFlags is set.
85 */
bsalomonc21b09e2015-08-28 18:46:56 -070086 void computeInvariantOutput(GrInvariantOutput* inout) const {
87 this->onComputeInvariantOutput(inout);
88 }
joshualitt56995b52014-12-11 15:44:02 -080089
bsalomon6251d172014-10-15 10:50:36 -070090protected:
wangyix93ab2542015-08-19 08:23:12 -070091 void addTextureAccess(const GrTextureAccess* textureAccess) override;
92
bsalomon6251d172014-10-15 10:50:36 -070093 /**
94 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070095 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
96 * in their FS code. The matrix expresses a transformation from local space. For a given
97 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
98 *
99 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700100 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700101 *
102 * This must only be called from the constructor because GrProcessors are immutable. The
103 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700104 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700105 *
106 * A processor subclass that has multiple methods of construction should always add its coord
107 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
108 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700109 */
110 void addCoordTransform(const GrCoordTransform*);
111
112 /**
wangyix58d890b2015-08-12 09:40:47 -0700113 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700114 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
115 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700116 * This is for processors whose shader code will be composed of nested processors whose output
117 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700118 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
119 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700120 */
wangyix58d890b2015-08-12 09:40:47 -0700121 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700122
123 /**
joshualitt56995b52014-12-11 15:44:02 -0800124 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700125 *
126 * Note: it's up to the subclass implementation to do any recursive call to compute the child
127 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800128 */
129 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
130
bsalomon6251d172014-10-15 10:50:36 -0700131private:
bsalomon42048002015-08-27 16:43:48 -0700132 void notifyRefCntIsZero() const final;
133
wangyixb1daa862015-08-18 11:29:31 -0700134 /** Returns a new instance of the appropriate *GL* implementation class
135 for the given GrFragmentProcessor; caller is responsible for deleting
136 the object. */
137 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
138
wangyix4b3050b2015-08-04 07:59:37 -0700139 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
140 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
141 GrProcessorKeyBuilder* b) const = 0;
142
bsalomonde258cd2014-10-15 19:06:21 -0700143 /**
144 * Subclass implements this to support isEqual(). It will only be called if it is known that
145 * the two processors are of the same subclass (i.e. they return the same object from
146 * getFactory()). The processor subclass should not compare its coord transforms as that will
147 * be performed automatically in the non-virtual isEqual().
148 */
149 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
150
151 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700152
joshualitt290c09b2014-12-19 13:45:20 -0800153 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700154
155 /**
wangyix69ed1142015-08-18 07:24:29 -0700156 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
157 * proc's children. In other words, each proc stores all the transforms of its subtree as if
158 * they were collected using preorder traversal.
159 *
160 * Example:
161 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
162 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
163 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
164 *
165 * (A)
166 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
167 * / \
168 * / \
169 * (B) (D)
170 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
171 * / / \
172 * / / \
173 * (C) (E) (F)
174 * [c1] [e1,e2,e3] [f1,f2]
175 *
wangyix58d890b2015-08-12 09:40:47 -0700176 * The same goes for fTextureAccesses with textures.
177 */
178 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
179
wangyix93ab2542015-08-19 08:23:12 -0700180 int fNumTexturesExclChildren;
181 int fNumTransformsExclChildren;
182
bsalomonac856c92015-08-27 06:30:17 -0700183 // TODO: These must convert their processors to pending-execution refs when the parent is
184 // converted (do this automatically in GrProgramElement?).
185 SkTArray<const GrFragmentProcessor*, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700186
187 typedef GrProcessor INHERITED;
188};
189
bsalomon6251d172014-10-15 10:50:36 -0700190#endif