blob: 1b8a8183bdf1809dc8dd994d46b9c5fd3eadde03 [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()
joshualitt290c09b2014-12-19 13:45:20 -080028 , fUsesLocalCoords(false) {}
bsalomon6251d172014-10-15 10:50:36 -070029
joshualitteb2a6762014-12-04 11:35:33 -080030 /** Returns a new instance of the appropriate *GL* implementation class
31 for the given GrFragmentProcessor; caller is responsible for deleting
32 the object. */
33 virtual GrGLFragmentProcessor* createGLInstance() const = 0;
34
35 /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
36 in generated shader code. */
37 virtual const char* name() const = 0;
bsalomon6251d172014-10-15 10:50:36 -070038
wangyix4b3050b2015-08-04 07:59:37 -070039 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
40 this->onGetGLProcessorKey(caps, b);
41 for (int i = 0; i < fChildProcessors.count(); ++i) {
wangyix58d890b2015-08-12 09:40:47 -070042 fChildProcessors[i].processor()->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070043 }
44 }
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
wangyix58d890b2015-08-12 09:40:47 -070064 const GrFragmentProcessor& childProcessor(int index) const {
65 return *fChildProcessors[index].processor();
wangyix4b3050b2015-08-04 07:59:37 -070066 }
67
joshualitt290c09b2014-12-19 13:45:20 -080068 /** Do any of the coordtransforms for this processor require local coords? */
69 bool usesLocalCoords() const { return fUsesLocalCoords; }
70
joshualitteb2a6762014-12-04 11:35:33 -080071 /** Returns true if this and other processor conservatively draw identically. It can only return
72 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070073 from getFactory()).
74
joshualitteb2a6762014-12-04 11:35:33 -080075 A return value of true from isEqual() should not be used to test whether the processor would
76 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070077 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070078
joshualitt56995b52014-12-11 15:44:02 -080079 /**
80 * This function is used to perform optimizations. When called the invarientOuput param
81 * indicate whether the input components to this processor in the FS will have known values.
82 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
83 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
84 * inout to indicate known values of its output. A component of the color member only has
85 * meaning if the corresponding bit in validFlags is set.
86 */
87 void computeInvariantOutput(GrInvariantOutput* inout) const;
88
bsalomon6251d172014-10-15 10:50:36 -070089protected:
90 /**
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
97 * 3 components. Otherwise they'll have 2.
98 *
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
101 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
102 *
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
111 * FragmentProcessors they have.
wangyix4b3050b2015-08-04 07:59:37 -0700112 * This is for processors whose shader code will be composed of nested processors whose output
113 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700114 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
115 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700116 */
wangyix58d890b2015-08-12 09:40:47 -0700117 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700118
119 /**
joshualitt56995b52014-12-11 15:44:02 -0800120 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700121 *
122 * Note: it's up to the subclass implementation to do any recursive call to compute the child
123 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800124 */
125 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
126
bsalomon6251d172014-10-15 10:50:36 -0700127private:
wangyix4b3050b2015-08-04 07:59:37 -0700128 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
129 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
130 GrProcessorKeyBuilder* b) const = 0;
131
bsalomonde258cd2014-10-15 19:06:21 -0700132 /**
133 * Subclass implements this to support isEqual(). It will only be called if it is known that
134 * the two processors are of the same subclass (i.e. they return the same object from
135 * getFactory()). The processor subclass should not compare its coord transforms as that will
136 * be performed automatically in the non-virtual isEqual().
137 */
138 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
139
140 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700141
joshualitt290c09b2014-12-19 13:45:20 -0800142 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700143
144 /**
wangyix69ed1142015-08-18 07:24:29 -0700145 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
146 * proc's children. In other words, each proc stores all the transforms of its subtree as if
147 * they were collected using preorder traversal.
148 *
149 * Example:
150 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
151 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
152 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
153 *
154 * (A)
155 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
156 * / \
157 * / \
158 * (B) (D)
159 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
160 * / / \
161 * / / \
162 * (C) (E) (F)
163 * [c1] [e1,e2,e3] [f1,f2]
164 *
wangyix58d890b2015-08-12 09:40:47 -0700165 * The same goes for fTextureAccesses with textures.
166 */
167 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
168
169 SkTArray<GrFragmentStage, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700170
171 typedef GrProcessor INHERITED;
172};
173
bsalomon6251d172014-10-15 10:50:36 -0700174#endif