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; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 14 | class GrGLCaps; |
| 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() |
| 27 | , fWillReadDstColor(false) |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 28 | , fWillUseInputColor(true) |
| 29 | , fUsesLocalCoords(false) {} |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 30 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 31 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
| 32 | virtual void getGLProcessorKey(const GrGLCaps& caps, |
| 33 | GrProcessorKeyBuilder* b) const = 0; |
| 34 | |
| 35 | /** Returns a new instance of the appropriate *GL* implementation class |
| 36 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 37 | the object. */ |
| 38 | virtual GrGLFragmentProcessor* createGLInstance() const = 0; |
| 39 | |
| 40 | /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded |
| 41 | in generated shader code. */ |
| 42 | virtual const char* name() const = 0; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 43 | |
| 44 | int numTransforms() const { return fCoordTransforms.count(); } |
| 45 | |
| 46 | /** Returns the coordinate transformation at index. index must be valid according to |
| 47 | numTransforms(). */ |
| 48 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 49 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame^] | 50 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 51 | return fCoordTransforms; |
| 52 | } |
| 53 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 54 | /** Will this prceossor read the destination pixel value? */ |
| 55 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 56 | |
| 57 | /** Will this prceossor read the source color value? */ |
| 58 | bool willUseInputColor() const { return fWillUseInputColor; } |
| 59 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 60 | /** Do any of the coordtransforms for this processor require local coords? */ |
| 61 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 62 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 63 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 64 | 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] | 65 | from getFactory()). |
| 66 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 67 | A return value of true from isEqual() should not be used to test whether the processor would |
| 68 | generate the same shader code. To test for identical code generation use getGLProcessorKey*/ |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 69 | bool isEqual(const GrFragmentProcessor& that) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 70 | if (this->classID() != that.classID() || |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 71 | !this->hasSameTransforms(that) || |
| 72 | !this->hasSameTextureAccesses(that)) { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 75 | return this->onIsEqual(that); |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 78 | /** |
| 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 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 88 | protected: |
| 89 | /** |
| 90 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 91 | * transformations. Coord transforms provide a mechanism for a processor to receive coordinates |
| 92 | * in their FS code. The matrix expresses a transformation from local space. For a given |
| 93 | * fragment the matrix will be applied to the local coordinate that maps to the fragment. |
| 94 | * |
| 95 | * When the transformation has perspective, the transformed coordinates will have |
| 96 | * 3 components. Otherwise they'll have 2. |
| 97 | * |
| 98 | * This must only be called from the constructor because GrProcessors are immutable. The |
| 99 | * processor subclass manages the lifetime of the transformations (this function only stores a |
| 100 | * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. |
| 101 | * |
| 102 | * A processor subclass that has multiple methods of construction should always add its coord |
| 103 | * transforms in a consistent order. The non-virtual implementation of isEqual() automatically |
| 104 | * compares transforms and will assume they line up across the two processor instances. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 105 | */ |
| 106 | void addCoordTransform(const GrCoordTransform*); |
| 107 | |
| 108 | /** |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 109 | * If the prceossor subclass will read the destination pixel value then it must call this |
| 110 | * function from its constructor. Otherwise, when its generated backend-specific prceossor class |
| 111 | * attempts to generate code that reads the destination pixel it will fail. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 112 | */ |
| 113 | void setWillReadDstColor() { fWillReadDstColor = true; } |
| 114 | |
| 115 | /** |
| 116 | * If the prceossor will generate a result that does not depend on the input color value then it |
| 117 | * must call this function from its constructor. Otherwise, when its generated backend-specific |
| 118 | * code might fail during variable binding due to unused variables. |
| 119 | */ |
| 120 | void setWillNotUseInputColor() { fWillUseInputColor = false; } |
| 121 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Subclass implements this to support getConstantColorComponents(...). |
| 124 | */ |
| 125 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
| 126 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 127 | private: |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 130 | * the two processors are of the same subclass (i.e. they return the same object from |
| 131 | * getFactory()). The processor subclass should not compare its coord transforms as that will |
| 132 | * be performed automatically in the non-virtual isEqual(). |
| 133 | */ |
| 134 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 135 | |
| 136 | bool hasSameTransforms(const GrFragmentProcessor&) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 137 | |
| 138 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 139 | bool fWillReadDstColor; |
| 140 | bool fWillUseInputColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 141 | bool fUsesLocalCoords; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 142 | |
| 143 | typedef GrProcessor INHERITED; |
| 144 | }; |
| 145 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 146 | #endif |