blob: bbc2f2bb0704f69c3af8e80b49871195b22c21dc [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*/
joshualitt2fe79232015-08-05 12:02:27 -070077 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const {
joshualitteb2a6762014-12-04 11:35:33 -080078 if (this->classID() != that.classID() ||
bsalomon420d7e92014-10-16 09:18:09 -070079 !this->hasSameTextureAccesses(that)) {
bsalomon6251d172014-10-15 10:50:36 -070080 return false;
81 }
joshualitt2fe79232015-08-05 12:02:27 -070082 if (ignoreCoordTransforms) {
83 if (this->numTransforms() != that.numTransforms()) {
84 return false;
85 }
86 } else if (!this->hasSameTransforms(that)) {
87 return false;
88 }
bsalomon420d7e92014-10-16 09:18:09 -070089 return this->onIsEqual(that);
bsalomon6251d172014-10-15 10:50:36 -070090 }
91
joshualitt56995b52014-12-11 15:44:02 -080092 /**
93 * This function is used to perform optimizations. When called the invarientOuput param
94 * indicate whether the input components to this processor in the FS will have known values.
95 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
96 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
97 * inout to indicate known values of its output. A component of the color member only has
98 * meaning if the corresponding bit in validFlags is set.
99 */
100 void computeInvariantOutput(GrInvariantOutput* inout) const;
101
bsalomon6251d172014-10-15 10:50:36 -0700102protected:
103 /**
104 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700105 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
106 * in their FS code. The matrix expresses a transformation from local space. For a given
107 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
108 *
109 * When the transformation has perspective, the transformed coordinates will have
110 * 3 components. Otherwise they'll have 2.
111 *
112 * This must only be called from the constructor because GrProcessors are immutable. The
113 * processor subclass manages the lifetime of the transformations (this function only stores a
114 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
115 *
116 * A processor subclass that has multiple methods of construction should always add its coord
117 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
118 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700119 */
120 void addCoordTransform(const GrCoordTransform*);
121
122 /**
wangyix58d890b2015-08-12 09:40:47 -0700123 * FragmentProcessor subclasses call this from their constructor to register any child
124 * FragmentProcessors they have.
wangyix4b3050b2015-08-04 07:59:37 -0700125 * This is for processors whose shader code will be composed of nested processors whose output
126 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700127 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
128 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700129 */
wangyix58d890b2015-08-12 09:40:47 -0700130 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700131
132 /**
joshualitt56995b52014-12-11 15:44:02 -0800133 * Subclass implements this to support getConstantColorComponents(...).
134 */
135 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
136
bsalomon6251d172014-10-15 10:50:36 -0700137private:
wangyix4b3050b2015-08-04 07:59:37 -0700138 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
139 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
140 GrProcessorKeyBuilder* b) const = 0;
141
bsalomonde258cd2014-10-15 19:06:21 -0700142 /**
143 * Subclass implements this to support isEqual(). It will only be called if it is known that
144 * the two processors are of the same subclass (i.e. they return the same object from
145 * getFactory()). The processor subclass should not compare its coord transforms as that will
146 * be performed automatically in the non-virtual isEqual().
147 */
148 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
149
150 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700151
joshualitt290c09b2014-12-19 13:45:20 -0800152 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700153
154 /**
155 * This stores the transforms of this proc, followed by all the transforms of this proc's
156 * children. In other words, each proc stores all the transforms of its subtree.
157 * The same goes for fTextureAccesses with textures.
158 */
159 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
160
161 SkTArray<GrFragmentStage, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700162
163 typedef GrProcessor INHERITED;
164};
165
bsalomon6251d172014-10-15 10:50:36 -0700166#endif