blob: 097925bb2a4b3d2af0905893f4073843bb8602db [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
wangyix4b3050b2015-08-04 07:59:37 -070034 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
35 this->onGetGLProcessorKey(caps, b);
36 for (int i = 0; i < fChildProcessors.count(); ++i) {
wangyix58d890b2015-08-12 09:40:47 -070037 fChildProcessors[i].processor()->getGLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -070038 }
39 }
40
wangyix93ab2542015-08-19 08:23:12 -070041 int numTexturesExclChildren() const { return fNumTexturesExclChildren; }
42
43 int numTransformsExclChildren() const { return fNumTransformsExclChildren; }
44
bsalomon6251d172014-10-15 10:50:36 -070045 int numTransforms() const { return fCoordTransforms.count(); }
46
47 /** Returns the coordinate transformation at index. index must be valid according to
48 numTransforms(). */
49 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
50
joshualittabb52a12015-01-13 15:02:10 -080051 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
52 return fCoordTransforms;
53 }
54
joshualitt2fe79232015-08-05 12:02:27 -070055 void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const {
wangyix58d890b2015-08-12 09:40:47 -070056 if (!fCoordTransforms.empty()) {
57 outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin());
joshualitt2fe79232015-08-05 12:02:27 -070058 }
59 }
60
wangyix4b3050b2015-08-04 07:59:37 -070061 int numChildProcessors() const { return fChildProcessors.count(); }
62
wangyix58d890b2015-08-12 09:40:47 -070063 const GrFragmentProcessor& childProcessor(int index) const {
64 return *fChildProcessors[index].processor();
wangyix4b3050b2015-08-04 07:59:37 -070065 }
66
joshualitt290c09b2014-12-19 13:45:20 -080067 /** Do any of the coordtransforms for this processor require local coords? */
68 bool usesLocalCoords() const { return fUsesLocalCoords; }
69
joshualitteb2a6762014-12-04 11:35:33 -080070 /** Returns true if this and other processor conservatively draw identically. It can only return
71 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070072 from getFactory()).
73
joshualitteb2a6762014-12-04 11:35:33 -080074 A return value of true from isEqual() should not be used to test whether the processor would
75 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
wangyix54017d72015-08-18 07:39:33 -070076 bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const;
bsalomon6251d172014-10-15 10:50:36 -070077
joshualitt56995b52014-12-11 15:44:02 -080078 /**
79 * This function is used to perform optimizations. When called the invarientOuput param
80 * indicate whether the input components to this processor in the FS will have known values.
81 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
82 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
83 * inout to indicate known values of its output. A component of the color member only has
84 * meaning if the corresponding bit in validFlags is set.
85 */
86 void computeInvariantOutput(GrInvariantOutput* inout) const;
87
bsalomon6251d172014-10-15 10:50:36 -070088protected:
wangyix93ab2542015-08-19 08:23:12 -070089 void addTextureAccess(const GrTextureAccess* textureAccess) override;
90
bsalomon6251d172014-10-15 10:50:36 -070091 /**
92 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070093 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
94 * in their FS code. The matrix expresses a transformation from local space. For a given
95 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
96 *
97 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -070098 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -070099 *
100 * This must only be called from the constructor because GrProcessors are immutable. The
101 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700102 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700103 *
104 * A processor subclass that has multiple methods of construction should always add its coord
105 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
106 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700107 */
108 void addCoordTransform(const GrCoordTransform*);
109
110 /**
wangyix58d890b2015-08-12 09:40:47 -0700111 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700112 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
113 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700114 * This is for processors whose shader code will be composed of nested processors whose output
115 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700116 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
117 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700118 */
wangyix58d890b2015-08-12 09:40:47 -0700119 int registerChildProcessor(const GrFragmentProcessor* child);
wangyix4b3050b2015-08-04 07:59:37 -0700120
121 /**
joshualitt56995b52014-12-11 15:44:02 -0800122 * Subclass implements this to support getConstantColorComponents(...).
wangyix54017d72015-08-18 07:39:33 -0700123 *
124 * Note: it's up to the subclass implementation to do any recursive call to compute the child
125 * procs' output invariants; computeInvariantOutput will not be recursive.
joshualitt56995b52014-12-11 15:44:02 -0800126 */
127 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
128
bsalomon6251d172014-10-15 10:50:36 -0700129private:
wangyixb1daa862015-08-18 11:29:31 -0700130 /** Returns a new instance of the appropriate *GL* implementation class
131 for the given GrFragmentProcessor; caller is responsible for deleting
132 the object. */
133 virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0;
134
wangyix4b3050b2015-08-04 07:59:37 -0700135 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
136 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
137 GrProcessorKeyBuilder* b) const = 0;
138
bsalomonde258cd2014-10-15 19:06:21 -0700139 /**
140 * Subclass implements this to support isEqual(). It will only be called if it is known that
141 * the two processors are of the same subclass (i.e. they return the same object from
142 * getFactory()). The processor subclass should not compare its coord transforms as that will
143 * be performed automatically in the non-virtual isEqual().
144 */
145 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
146
147 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700148
joshualitt290c09b2014-12-19 13:45:20 -0800149 bool fUsesLocalCoords;
wangyix58d890b2015-08-12 09:40:47 -0700150
151 /**
wangyix69ed1142015-08-18 07:24:29 -0700152 * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
153 * proc's children. In other words, each proc stores all the transforms of its subtree as if
154 * they were collected using preorder traversal.
155 *
156 * Example:
157 * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has
158 * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms
159 * respectively. The following shows what the fCoordTransforms array of each proc would contain:
160 *
161 * (A)
162 * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2]
163 * / \
164 * / \
165 * (B) (D)
166 * [b1,b2,c1] [d1,e1,e2,e3,f1,f2]
167 * / / \
168 * / / \
169 * (C) (E) (F)
170 * [c1] [e1,e2,e3] [f1,f2]
171 *
wangyix58d890b2015-08-12 09:40:47 -0700172 * The same goes for fTextureAccesses with textures.
173 */
174 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
175
wangyix93ab2542015-08-19 08:23:12 -0700176 int fNumTexturesExclChildren;
177 int fNumTransformsExclChildren;
178
wangyix58d890b2015-08-12 09:40:47 -0700179 SkTArray<GrFragmentStage, false> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700180
181 typedef GrProcessor INHERITED;
182};
183
bsalomon6251d172014-10-15 10:50:36 -0700184#endif