blob: 8d2a4d42b1d1976f394679bc9f1bc7100e328338 [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
wangyixb1daa862015-08-18 11:29:31 -070030 GrGLFragmentProcessor* createGLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -080031
32 /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
33 in generated shader code. */
34 virtual const char* name() const = 0;
bsalomon6251d172014-10-15 10:50:36 -070035
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) {
wangyix58d890b2015-08-12 09:40:47 -070039 fChildProcessors[i].processor()->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070040 }
41 }
42
bsalomon6251d172014-10-15 10:50:36 -070043 int numTransforms() const { return fCoordTransforms.count(); }
44
45 /** Returns the coordinate transformation at index. index must be valid according to
46 numTransforms(). */
47 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
48
joshualittabb52a12015-01-13 15:02:10 -080049 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
50 return fCoordTransforms;
51 }
52
joshualitt2fe79232015-08-05 12:02:27 -070053 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070054 if (!fCoordTransforms.empty()) {
55 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070056 }
57 }
58
wangyix4b3050b2015-08-04 07:59:37 -070059 int numChildProcessors() const { return fChildProcessors.count(); }
60
wangyix58d890b2015-08-12 09:40:47 -070061 const GrFragmentProcessor& childProcessor(int index) const {
62 return *fChildProcessors[index].processor();
wangyix4b3050b2015-08-04 07:59:37 -070063 }
64
joshualitt290c09b2014-12-19 13:45:20 -080065 /** Do any of the coordtransforms for this processor require local coords? */
66 bool usesLocalCoords() const { return fUsesLocalCoords; }
67
joshualitteb2a6762014-12-04 11:35:33 -080068 /** Returns true if this and other processor conservatively draw identically. It can only return
69 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070070 from getFactory()).
71
joshualitteb2a6762014-12-04 11:35:33 -080072 A return value of true from isEqual() should not be used to test whether the processor would
73 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070074 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070075
joshualitt56995b52014-12-11 15:44:02 -080076 /**
77 * This function is used to perform optimizations. When called the invarientOuput param
78 * indicate whether the input components to this processor in the FS will have known values.
79 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
80 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
81 * inout to indicate known values of its output. A component of the color member only has
82 * meaning if the corresponding bit in validFlags is set.
83 */
84 void computeInvariantOutput(GrInvariantOutput* inout) const;
85
bsalomon6251d172014-10-15 10:50:36 -070086protected:
87 /**
88 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070089 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
90 * in their FS code. The matrix expresses a transformation from local space. For a given
91 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
92 *
93 * When the transformation has perspective, the transformed coordinates will have
94 * 3 components. Otherwise they'll have 2.
95 *
96 * This must only be called from the constructor because GrProcessors are immutable. The
97 * processor subclass manages the lifetime of the transformations (this function only stores a
98 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
99 *
100 * A processor subclass that has multiple methods of construction should always add its coord
101 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
102 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700103 */
104 void addCoordTransform(const GrCoordTransform*);
105
106 /**
wangyix58d890b2015-08-12 09:40:47 -0700107 * FragmentProcessor subclasses call this from their constructor to register any child
108 * FragmentProcessors they have.
wangyix4b3050b2015-08-04 07:59:37 -0700109 * This is for processors whose shader code will be composed of nested processors whose output
110 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700111 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
112 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700113 */
wangyix58d890b2015-08-12 09:40:47 -0700114 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700115
116 /**
joshualitt56995b52014-12-11 15:44:02 -0800117 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700118 *
119 * Note: it's up to the subclass implementation to do any recursive call to compute the child
120 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800121 */
122 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
123
bsalomon6251d172014-10-15 10:50:36 -0700124private:
wangyixb1daa862015-08-18 11:29:31 -0700125 /** Returns a new instance of the appropriate *GL* implementation class
126 for the given GrFragmentProcessor; caller is responsible for deleting
127 the object. */
128 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
129
wangyix4b3050b2015-08-04 07:59:37 -0700130 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
131 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
132 GrProcessorKeyBuilder* b) const = 0;
133
bsalomonde258cd2014-10-15 19:06:21 -0700134 /**
135 * Subclass implements this to support isEqual(). It will only be called if it is known that
136 * the two processors are of the same subclass (i.e. they return the same object from
137 * getFactory()). The processor subclass should not compare its coord transforms as that will
138 * be performed automatically in the non-virtual isEqual().
139 */
140 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
141
142 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700143
joshualitt290c09b2014-12-19 13:45:20 -0800144 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700145
146 /**
wangyix69ed1142015-08-18 07:24:29 -0700147 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
148 * proc's children. In other words, each proc stores all the transforms of its subtree as if
149 * they were collected using preorder traversal.
150 *
151 * Example:
152 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
153 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
154 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
155 *
156 * (A)
157 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
158 * / \
159 * / \
160 * (B) (D)
161 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
162 * / / \
163 * / / \
164 * (C) (E) (F)
165 * [c1] [e1,e2,e3] [f1,f2]
166 *
wangyix58d890b2015-08-12 09:40:47 -0700167 * The same goes for fTextureAccesses with textures.
168 */
169 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
170
171 SkTArray<GrFragmentStage, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700172
173 typedef GrProcessor INHERITED;
174};
175
bsalomon6251d172014-10-15 10:50:36 -0700176#endif