bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 1 | /* |
| 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" |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 12 | #include "GrStagedProcessor.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 13 | |
| 14 | class GrCoordTransform; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 15 | class GrGLSLCaps; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 16 | class GrGLFragmentProcessor; |
| 17 | class GrProcessorKeyBuilder; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 18 | |
| 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 | */ |
| 24 | class GrFragmentProcessor : public GrProcessor { |
| 25 | public: |
| 26 | GrFragmentProcessor() |
| 27 | : INHERITED() |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 28 | , fUsesLocalCoords(false) {} |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 29 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 30 | GrGLFragmentProcessor* createGLInstance() const; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 31 | |
| 32 | /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded |
| 33 | in generated shader code. */ |
| 34 | virtual const char* name() const = 0; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 35 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 36 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
| 37 | this->onGetGLProcessorKey(caps, b); |
| 38 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 39 | fChildProcessors[i].processor()->getGLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 43 | 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 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 49 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 50 | return fCoordTransforms; |
| 51 | } |
| 52 | |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 53 | void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const { |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 54 | if (!fCoordTransforms.empty()) { |
| 55 | outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin()); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 59 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 60 | |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 61 | const GrFragmentProcessor& childProcessor(int index) const { |
| 62 | return *fChildProcessors[index].processor(); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 63 | } |
| 64 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 65 | /** Do any of the coordtransforms for this processor require local coords? */ |
| 66 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 67 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 68 | /** 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 |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 70 | from getFactory()). |
| 71 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 72 | 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*/ |
wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 74 | bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 75 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 76 | /** |
| 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 86 | protected: |
| 87 | /** |
| 88 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 89 | * 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. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 103 | */ |
| 104 | void addCoordTransform(const GrCoordTransform*); |
| 105 | |
| 106 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 107 | * FragmentProcessor subclasses call this from their constructor to register any child |
| 108 | * FragmentProcessors they have. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 109 | * 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 |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 111 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 112 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 113 | */ |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 114 | int registerChildProcessor(const GrFragmentProcessor* child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 117 | * Subclass implements this to support getConstantColorComponents(...). |
wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 118 | * |
| 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. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 121 | */ |
| 122 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
| 123 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 124 | private: |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 125 | /** 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 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 130 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
| 131 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 132 | GrProcessorKeyBuilder* b) const = 0; |
| 133 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 134 | /** |
| 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 143 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 144 | bool fUsesLocalCoords; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
wangyix | 69ed114 | 2015-08-18 07:24:29 -0700 | [diff] [blame] | 147 | * 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 | * |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 167 | * The same goes for fTextureAccesses with textures. |
| 168 | */ |
| 169 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 170 | |
| 171 | SkTArray<GrFragmentStage, false> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 172 | |
| 173 | typedef GrProcessor INHERITED; |
| 174 | }; |
| 175 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 176 | #endif |