blob: f08d3656b7de5d9281cdf1f88defc7a73f0efc0e [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"
wangyix58d890b2015-08-12 09:40:47 -070012#include "GrStagedProcessor.h"
bsalomon6251d172014-10-15 10:50:36 -070013
14class GrCoordTransform;
jvanverthe9c0fc62015-04-29 11:18:05 -070015class GrGLSLCaps;
joshualitteb2a6762014-12-04 11:35:33 -080016class GrGLFragmentProcessor;
17class 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
wangyixb1daa862015-08-18 11:29:31 -070032 GrGLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080033
34 /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
35 in generated shader code. */
36 virtual const char* name() const = 0;
bsalomon6251d172014-10-15 10:50:36 -070037
wangyix4b3050b2015-08-04 07:59:37 -070038 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
39 this->onGetGLProcessorKey(caps, b);
40 for (int i = 0; i < fChildProcessors.count(); ++i) {
wangyix58d890b2015-08-12 09:40:47 -070041 fChildProcessors[i].processor()->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070042 }
43 }
44
wangyix93ab2542015-08-19 08:23:12 -070045 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
46
47 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
48
bsalomon6251d172014-10-15 10:50:36 -070049 int numTransforms() const { return fCoordTransforms.count(); }
50
51 /** Returns the coordinate transformation at index. index must be valid according to
52 numTransforms(). */
53 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
54
joshualittabb52a12015-01-13 15:02:10 -080055 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
56 return fCoordTransforms;
57 }
58
joshualitt2fe79232015-08-05 12:02:27 -070059 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070060 if (!fCoordTransforms.empty()) {
61 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070062 }
63 }
64
wangyix4b3050b2015-08-04 07:59:37 -070065 int numChildProcessors() const { return fChildProcessors.count(); }
66
wangyix58d890b2015-08-12 09:40:47 -070067 const GrFragmentProcessor& childProcessor(int index) const {
68 return *fChildProcessors[index].processor();
wangyix4b3050b2015-08-04 07:59:37 -070069 }
70
joshualitt290c09b2014-12-19 13:45:20 -080071 /** Do any of the coordtransforms for this processor require local coords? */
72 bool usesLocalCoords() const { return fUsesLocalCoords; }
73
joshualitteb2a6762014-12-04 11:35:33 -080074 /** Returns true if this and other processor conservatively draw identically. It can only return
75 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070076 from getFactory()).
77
joshualitteb2a6762014-12-04 11:35:33 -080078 A return value of true from isEqual() should not be used to test whether the processor would
79 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070080 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070081
joshualitt56995b52014-12-11 15:44:02 -080082 /**
83 * This function is used to perform optimizations. When called the invarientOuput param
84 * indicate whether the input components to this processor in the FS will have known values.
85 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
86 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
87 * inout to indicate known values of its output. A component of the color member only has
88 * meaning if the corresponding bit in validFlags is set.
89 */
90 void computeInvariantOutput(GrInvariantOutput* inout) const;
91
bsalomon6251d172014-10-15 10:50:36 -070092protected:
wangyix93ab2542015-08-19 08:23:12 -070093 void addTextureAccess(const GrTextureAccess* textureAccess) override;
94
bsalomon6251d172014-10-15 10:50:36 -070095 /**
96 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070097 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
98 * in their FS code. The matrix expresses a transformation from local space. For a given
99 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
100 *
101 * When the transformation has perspective, the transformed coordinates will have
102 * 3 components. Otherwise they'll have 2.
103 *
104 * This must only be called from the constructor because GrProcessors are immutable. The
105 * processor subclass manages the lifetime of the transformations (this function only stores a
106 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
107 *
108 * A processor subclass that has multiple methods of construction should always add its coord
109 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
110 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700111 */
112 void addCoordTransform(const GrCoordTransform*);
113
114 /**
wangyix58d890b2015-08-12 09:40:47 -0700115 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700116 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
117 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700118 * This is for processors whose shader code will be composed of nested processors whose output
119 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700120 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
121 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700122 */
wangyix58d890b2015-08-12 09:40:47 -0700123 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700124
125 /**
joshualitt56995b52014-12-11 15:44:02 -0800126 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700127 *
128 * Note: it's up to the subclass implementation to do any recursive call to compute the child
129 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800130 */
131 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
132
bsalomon6251d172014-10-15 10:50:36 -0700133private:
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
wangyix58d890b2015-08-12 09:40:47 -0700183 SkTArray<GrFragmentStage, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700184
185 typedef GrProcessor INHERITED;
186};
187
bsalomon6251d172014-10-15 10:50:36 -0700188#endif