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; |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 16 | class GrInvariantOutput; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 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: |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 26 | /** |
| 27 | * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to |
| 28 | * only consider the input color's alpha. However, there is a competing desire to have reusable |
| 29 | * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input |
| 30 | * color is considered. This function exists to filter the input color and pass it to a FP. It |
| 31 | * does so by returning a parent FP that multiplies the passed in FPs output by the parent's |
| 32 | * input alpha. The passed in FP will not receive an input color. |
| 33 | */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame^] | 34 | static const GrFragmentProcessor* MulOutputByInputAlpha(const GrFragmentProcessor*); |
| 35 | |
| 36 | /** |
| 37 | * Similar to the above but it modulates the output r,g,b of the child processor by the input |
| 38 | * rgb and then multiplies all the components by the input alpha. This effectively modulates |
| 39 | * the child processor's premul color by a unpremul'ed input and produces a premul output |
| 40 | */ |
| 41 | static const GrFragmentProcessor* MulOutputByInputUnpremulColor(const GrFragmentProcessor*); |
| 42 | |
| 43 | /** |
| 44 | * Returns a parent fragment processor that adopts the passed fragment processor as a child. The |
| 45 | * parent will ignore its input color and instead feed the passed in color as input to the |
| 46 | * child. |
| 47 | */ |
| 48 | static const GrFragmentProcessor* OverrideInput(const GrFragmentProcessor*, GrColor); |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 49 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 50 | GrFragmentProcessor() |
| 51 | : INHERITED() |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 52 | , fUsesLocalCoords(false) |
| 53 | , fNumTexturesExclChildren(0) |
| 54 | , fNumTransformsExclChildren(0) {} |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 55 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 56 | ~GrFragmentProcessor() override; |
| 57 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 58 | GrGLFragmentProcessor* createGLInstance() const; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 59 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 60 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
| 61 | this->onGetGLProcessorKey(caps, b); |
| 62 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 63 | fChildProcessors[i]->getGLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 67 | int numTexturesExclChildren() const { return fNumTexturesExclChildren; } |
| 68 | |
| 69 | int numTransformsExclChildren() const { return fNumTransformsExclChildren; } |
| 70 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 71 | int numTransforms() const { return fCoordTransforms.count(); } |
| 72 | |
| 73 | /** Returns the coordinate transformation at index. index must be valid according to |
| 74 | numTransforms(). */ |
| 75 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 76 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 77 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 78 | return fCoordTransforms; |
| 79 | } |
| 80 | |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 81 | void gatherCoordTransforms(SkTArray<const GrCoordTransform*, true>* outTransforms) const { |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 82 | if (!fCoordTransforms.empty()) { |
| 83 | outTransforms->push_back_n(fCoordTransforms.count(), fCoordTransforms.begin()); |
joshualitt | 2fe7923 | 2015-08-05 12:02:27 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 87 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 88 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 89 | const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 90 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 91 | /** Do any of the coordtransforms for this processor require local coords? */ |
| 92 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 93 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 94 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 95 | 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] | 96 | from getFactory()). |
| 97 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 98 | A return value of true from isEqual() should not be used to test whether the processor would |
| 99 | generate the same shader code. To test for identical code generation use getGLProcessorKey*/ |
wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 100 | bool isEqual(const GrFragmentProcessor& that, bool ignoreCoordTransforms) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 101 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 102 | /** |
| 103 | * This function is used to perform optimizations. When called the invarientOuput param |
| 104 | * indicate whether the input components to this processor in the FS will have known values. |
| 105 | * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent |
| 106 | * member indicates whether the input will be 1 or 4 bytes. The function updates the members of |
| 107 | * inout to indicate known values of its output. A component of the color member only has |
| 108 | * meaning if the corresponding bit in validFlags is set. |
| 109 | */ |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 110 | void computeInvariantOutput(GrInvariantOutput* inout) const { |
| 111 | this->onComputeInvariantOutput(inout); |
| 112 | } |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 113 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 114 | protected: |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 115 | void addTextureAccess(const GrTextureAccess* textureAccess) override; |
| 116 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 119 | * transformations. Coord transforms provide a mechanism for a processor to receive coordinates |
| 120 | * in their FS code. The matrix expresses a transformation from local space. For a given |
| 121 | * fragment the matrix will be applied to the local coordinate that maps to the fragment. |
| 122 | * |
| 123 | * When the transformation has perspective, the transformed coordinates will have |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 124 | * 3 components. Otherwise they'll have 2. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 125 | * |
| 126 | * This must only be called from the constructor because GrProcessors are immutable. The |
| 127 | * processor subclass manages the lifetime of the transformations (this function only stores a |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 128 | * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 129 | * |
| 130 | * A processor subclass that has multiple methods of construction should always add its coord |
| 131 | * transforms in a consistent order. The non-virtual implementation of isEqual() automatically |
| 132 | * compares transforms and will assume they line up across the two processor instances. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 133 | */ |
| 134 | void addCoordTransform(const GrCoordTransform*); |
| 135 | |
| 136 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 137 | * FragmentProcessor subclasses call this from their constructor to register any child |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 138 | * FragmentProcessors they have. This must be called AFTER all texture accesses and coord |
| 139 | * transforms have been added. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 140 | * This is for processors whose shader code will be composed of nested processors whose output |
| 141 | * colors will be combined somehow to produce its output color. Registering these child |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 142 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 143 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 144 | */ |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 145 | int registerChildProcessor(const GrFragmentProcessor* child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 148 | * Subclass implements this to support getConstantColorComponents(...). |
wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 149 | * |
| 150 | * Note: it's up to the subclass implementation to do any recursive call to compute the child |
| 151 | * procs' output invariants; computeInvariantOutput will not be recursive. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 152 | */ |
| 153 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
| 154 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 155 | private: |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 156 | void notifyRefCntIsZero() const final; |
| 157 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 158 | /** Returns a new instance of the appropriate *GL* implementation class |
| 159 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 160 | the object. */ |
| 161 | virtual GrGLFragmentProcessor* onCreateGLInstance() const = 0; |
| 162 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 163 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
| 164 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 165 | GrProcessorKeyBuilder* b) const = 0; |
| 166 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 169 | * the two processors are of the same subclass (i.e. they return the same object from |
| 170 | * getFactory()). The processor subclass should not compare its coord transforms as that will |
| 171 | * be performed automatically in the non-virtual isEqual(). |
| 172 | */ |
| 173 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 174 | |
| 175 | bool hasSameTransforms(const GrFragmentProcessor&) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 176 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 177 | bool fUsesLocalCoords; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
wangyix | 69ed114 | 2015-08-18 07:24:29 -0700 | [diff] [blame] | 180 | * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this |
| 181 | * proc's children. In other words, each proc stores all the transforms of its subtree as if |
| 182 | * they were collected using preorder traversal. |
| 183 | * |
| 184 | * Example: |
| 185 | * Suppose we have frag proc A, who has two children B and D. B has a child C, and D has |
| 186 | * two children E and F. Suppose procs A, B, C, D, E, F have 1, 2, 1, 1, 3, 2 transforms |
| 187 | * respectively. The following shows what the fCoordTransforms array of each proc would contain: |
| 188 | * |
| 189 | * (A) |
| 190 | * [a1,b1,b2,c1,d1,e1,e2,e3,f1,f2] |
| 191 | * / \ |
| 192 | * / \ |
| 193 | * (B) (D) |
| 194 | * [b1,b2,c1] [d1,e1,e2,e3,f1,f2] |
| 195 | * / / \ |
| 196 | * / / \ |
| 197 | * (C) (E) (F) |
| 198 | * [c1] [e1,e2,e3] [f1,f2] |
| 199 | * |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 200 | * The same goes for fTextureAccesses with textures. |
| 201 | */ |
| 202 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 203 | |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 204 | int fNumTexturesExclChildren; |
| 205 | int fNumTransformsExclChildren; |
| 206 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 207 | // TODO: These must convert their processors to pending-execution refs when the parent is |
| 208 | // converted (do this automatically in GrProgramElement?). |
| 209 | SkTArray<const GrFragmentProcessor*, true> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 210 | |
| 211 | typedef GrProcessor INHERITED; |
| 212 | }; |
| 213 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 214 | #endif |