blob: 8fc3ec1d4b3fe9aed42f228a37065dfa8c99ccdd [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;
16class GrProcessorKeyBuilder;
bsalomon6251d172014-10-15 10:50:36 -070017
18/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
19 produce an output color. They may reference textures and uniforms. They may use
20 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
21 to the fragment being processed.
22 */
23class GrFragmentProcessor : public GrProcessor {
24public:
25 GrFragmentProcessor()
26 : INHERITED()
joshualitt290c09b2014-12-19 13:45:20 -080027 , fUsesLocalCoords(false) {}
bsalomon6251d172014-10-15 10:50:36 -070028
joshualitteb2a6762014-12-04 11:35:33 -080029 /** Returns a new instance of the appropriate *GL* implementation class
30 for the given GrFragmentProcessor; caller is responsible for deleting
31 the object. */
32 virtual GrGLFragmentProcessor* createGLInstance() const = 0;
33
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) {
41 fChildProcessors[i]->getGLProcessorKey(caps, b);
42 }
43 }
44
bsalomon6251d172014-10-15 10:50:36 -070045 int numTransforms() const { return fCoordTransforms.count(); }
46
wangyix4b3050b2015-08-04 07:59:37 -070047 int numTransformsIncludeChildProcs() const {
48 int numTransforms = fCoordTransforms.count();
49 for (int i = 0; i < fChildProcessors.count(); ++i) {
50 numTransforms += fChildProcessors[i]->numTransformsIncludeChildProcs();
51 }
52 return numTransforms;
53 }
54
bsalomon6251d172014-10-15 10:50:36 -070055 /** Returns the coordinate transformation at index. index must be valid according to
56 numTransforms(). */
57 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
58
joshualittabb52a12015-01-13 15:02:10 -080059 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
60 return fCoordTransforms;
61 }
62
wangyix4b3050b2015-08-04 07:59:37 -070063 int numChildProcessors() const { return fChildProcessors.count(); }
64
65 GrFragmentProcessor* childProcessor(int index) const { return fChildProcessors[index]; }
66
67 const SkTArray<GrFragmentProcessor*, false>& childProcessors() const {
68 return fChildProcessors;
69 }
70
71 int numTexturesIncludeChildProcs() const {
72 int numTextures = this->numTextures();
73 for (int i = 0; i < fChildProcessors.count(); ++i) {
74 numTextures += fChildProcessors[i]->numTexturesIncludeChildProcs();
75 }
76 return numTextures;
77 }
78
joshualitt290c09b2014-12-19 13:45:20 -080079 /** Do any of the coordtransforms for this processor require local coords? */
80 bool usesLocalCoords() const { return fUsesLocalCoords; }
81
joshualitteb2a6762014-12-04 11:35:33 -080082 /** Returns true if this and other processor conservatively draw identically. It can only return
83 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070084 from getFactory()).
85
joshualitteb2a6762014-12-04 11:35:33 -080086 A return value of true from isEqual() should not be used to test whether the processor would
87 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
bsalomon420d7e92014-10-16 09:18:09 -070088 bool isEqual(const GrFragmentProcessor& that) const {
joshualitteb2a6762014-12-04 11:35:33 -080089 if (this->classID() != that.classID() ||
bsalomon420d7e92014-10-16 09:18:09 -070090 !this->hasSameTransforms(that) ||
91 !this->hasSameTextureAccesses(that)) {
bsalomon6251d172014-10-15 10:50:36 -070092 return false;
93 }
bsalomon420d7e92014-10-16 09:18:09 -070094 return this->onIsEqual(that);
bsalomon6251d172014-10-15 10:50:36 -070095 }
96
joshualitt56995b52014-12-11 15:44:02 -080097 /**
98 * This function is used to perform optimizations. When called the invarientOuput param
99 * indicate whether the input components to this processor in the FS will have known values.
100 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
101 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
102 * inout to indicate known values of its output. A component of the color member only has
103 * meaning if the corresponding bit in validFlags is set.
104 */
105 void computeInvariantOutput(GrInvariantOutput* inout) const;
106
bsalomon6251d172014-10-15 10:50:36 -0700107protected:
108 /**
109 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700110 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
111 * in their FS code. The matrix expresses a transformation from local space. For a given
112 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
113 *
114 * When the transformation has perspective, the transformed coordinates will have
115 * 3 components. Otherwise they'll have 2.
116 *
117 * This must only be called from the constructor because GrProcessors are immutable. The
118 * processor subclass manages the lifetime of the transformations (this function only stores a
119 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
120 *
121 * A processor subclass that has multiple methods of construction should always add its coord
122 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
123 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700124 */
125 void addCoordTransform(const GrCoordTransform*);
126
127 /**
wangyix4b3050b2015-08-04 07:59:37 -0700128 * FragmentProcessor subclasses call this to register any child FragmentProcessors they have.
129 * This is for processors whose shader code will be composed of nested processors whose output
130 * colors will be combined somehow to produce its output color. Registering these child
131 * processors will allow the ProgramBuilder to automatically add their transformed coords and
132 * texture accesses and mangle their uniform and output color names and
133 */
134 void registerChildProcessor(GrFragmentProcessor* child);
135
136 /**
joshualitt56995b52014-12-11 15:44:02 -0800137 * Subclass implements this to support getConstantColorComponents(...).
138 */
139 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
140
bsalomon6251d172014-10-15 10:50:36 -0700141private:
wangyix4b3050b2015-08-04 07:59:37 -0700142 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
143 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
144 GrProcessorKeyBuilder* b) const = 0;
145
bsalomonde258cd2014-10-15 19:06:21 -0700146 /**
147 * Subclass implements this to support isEqual(). It will only be called if it is known that
148 * the two processors are of the same subclass (i.e. they return the same object from
149 * getFactory()). The processor subclass should not compare its coord transforms as that will
150 * be performed automatically in the non-virtual isEqual().
151 */
152 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
153
154 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700155
156 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
joshualitt290c09b2014-12-19 13:45:20 -0800157 bool fUsesLocalCoords;
wangyix4b3050b2015-08-04 07:59:37 -0700158 SkTArray<GrFragmentProcessor*, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700159
160 typedef GrProcessor INHERITED;
161};
162
bsalomon6251d172014-10-15 10:50:36 -0700163#endif