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 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 30 | /** 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 38 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 39 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
| 40 | this->onGetGLProcessorKey(caps, b); |
| 41 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 42 | fChildProcessors[i].processor()->getGLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 46 | 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 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 52 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 53 | return fCoordTransforms; |
| 54 | } |
| 55 | |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 56 | void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const { |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 57 | if (!fCoordTransforms.empty()) { |
| 58 | outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin()); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 62 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 63 | |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 64 | const GrFragmentProcessor& childProcessor(int index) const { |
| 65 | return *fChildProcessors[index].processor(); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 66 | } |
| 67 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 68 | /** Do any of the coordtransforms for this processor require local coords? */ |
| 69 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 70 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 71 | /** 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 |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 73 | from getFactory()). |
| 74 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 75 | 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*/ |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 77 | bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 78 | if (this->classID() != that.classID() || |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 79 | !this->hasSameTextureAccesses(that)) { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 82 | if (ignoreCoordTransforms) { |
| 83 | if (this->numTransforms() != that.numTransforms()) { |
| 84 | return false; |
| 85 | } |
| 86 | } else if (!this->hasSameTransforms(that)) { |
| 87 | return false; |
| 88 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 89 | return this->onIsEqual(that); |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 92 | /** |
| 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 102 | protected: |
| 103 | /** |
| 104 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 105 | * 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. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 119 | */ |
| 120 | void addCoordTransform(const GrCoordTransform*); |
| 121 | |
| 122 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 123 | * FragmentProcessor subclasses call this from their constructor to register any child |
| 124 | * FragmentProcessors they have. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 125 | * 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 |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 127 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 128 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 129 | */ |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 130 | int registerChildProcessor(const GrFragmentProcessor* child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 133 | * Subclass implements this to support getConstantColorComponents(...). |
| 134 | */ |
| 135 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
| 136 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 137 | private: |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 138 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
| 139 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 140 | GrProcessorKeyBuilder* b) const = 0; |
| 141 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 142 | /** |
| 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 151 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 152 | bool fUsesLocalCoords; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame^] | 153 | |
| 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 162 | |
| 163 | typedef GrProcessor INHERITED; |
| 164 | }; |
| 165 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 166 | #endif |