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" |
| 12 | |
| 13 | class GrCoordTransform; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 14 | class GrGLSLCaps; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 15 | class GrGLFragmentProcessor; |
| 16 | class GrProcessorKeyBuilder; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 17 | |
| 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 | */ |
| 23 | class GrFragmentProcessor : public GrProcessor { |
| 24 | public: |
| 25 | GrFragmentProcessor() |
| 26 | : INHERITED() |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 27 | , fUsesLocalCoords(false) {} |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 28 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 29 | /** 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 37 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 38 | 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 45 | int numTransforms() const { return fCoordTransforms.count(); } |
| 46 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 47 | 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 55 | /** 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 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 59 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 60 | return fCoordTransforms; |
| 61 | } |
| 62 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 63 | 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 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 79 | /** Do any of the coordtransforms for this processor require local coords? */ |
| 80 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 81 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 82 | /** 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 |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 84 | from getFactory()). |
| 85 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 86 | 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*/ |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 88 | bool isEqual(const GrFragmentProcessor& that) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 89 | if (this->classID() != that.classID() || |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 90 | !this->hasSameTransforms(that) || |
| 91 | !this->hasSameTextureAccesses(that)) { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 94 | return this->onIsEqual(that); |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 97 | /** |
| 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 107 | protected: |
| 108 | /** |
| 109 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 110 | * 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. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 124 | */ |
| 125 | void addCoordTransform(const GrCoordTransform*); |
| 126 | |
| 127 | /** |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 128 | * 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 | /** |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 137 | * Subclass implements this to support getConstantColorComponents(...). |
| 138 | */ |
| 139 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
| 140 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 141 | private: |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 142 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
| 143 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 144 | GrProcessorKeyBuilder* b) const = 0; |
| 145 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 146 | /** |
| 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; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 155 | |
| 156 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 157 | bool fUsesLocalCoords; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame^] | 158 | SkTArray<GrFragmentProcessor*, false> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 159 | |
| 160 | typedef GrProcessor INHERITED; |
| 161 | }; |
| 162 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 163 | #endif |