tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 8 | #ifndef GrEffect_DEFINED |
| 9 | #define GrEffect_DEFINED |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 11 | #include "GrColor.h" |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 12 | #include "GrEffectUnitTest.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 14 | #include "GrTextureAccess.h" |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 15 | #include "GrTypesPriv.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 17 | class GrBackendEffectFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 18 | class GrContext; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 19 | class GrCoordTransform; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 20 | class GrEffect; |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 21 | class GrVertexEffect; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 22 | class SkString; |
| 23 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 24 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 25 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 26 | Subclasses must have a function that produces a human-readable name: |
| 27 | static const char* Name(); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 28 | GrEffect objects *must* be immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 29 | |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 30 | Dynamically allocated GrEffects are managed by a per-thread memory pool. The ref count of an |
| 31 | effect must reach 0 before the thread terminates and the pool is destroyed. To create a static |
| 32 | effect use the macro GR_CREATE_STATIC_EFFECT declared below. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 33 | */ |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 34 | class GrEffect : public SkRefCnt { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 35 | public: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 36 | SK_DECLARE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 38 | virtual ~GrEffect(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 40 | /** |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 41 | * This function is used to perform optimizations. When called the color and validFlags params |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 42 | * indicate whether the input components to this effect in the FS will have known values. |
| 43 | * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to |
| 44 | * indicate known values of its output. A component of the color param only has meaning if the |
| 45 | * corresponding bit in validFlags is set. |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 46 | */ |
| 47 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 49 | /** Will this effect read the source color value? */ |
| 50 | bool willUseInputColor() const { return fWillUseInputColor; } |
| 51 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 52 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 53 | identification. The factory should be an instance of templated class, |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 54 | GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 55 | a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created |
| 56 | by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 57 | |
| 58 | Example: |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 59 | class MyCustomEffect : public GrEffect { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 60 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 61 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 62 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 63 | } |
| 64 | ... |
| 65 | }; |
| 66 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 67 | virtual const GrBackendEffectFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 68 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 69 | /** Returns true if this and other effect conservatively draw identically. It can only return |
| 70 | true when the two effects are of the same subclass (i.e. they return the same object from |
| 71 | from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 73 | A return value of true from isEqual() should not be used to test whether the effects would |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 74 | generate the same shader code. To test for identical code generation use the effects' keys |
| 75 | computed by the GrBackendEffectFactory. |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 76 | */ |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 77 | bool isEqual(const GrEffect& other) const { |
| 78 | if (&this->getFactory() != &other.getFactory()) { |
| 79 | return false; |
| 80 | } |
| 81 | bool result = this->onIsEqual(other); |
| 82 | #ifdef SK_DEBUG |
| 83 | if (result) { |
| 84 | this->assertEquality(other); |
| 85 | } |
| 86 | #endif |
| 87 | return result; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 88 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 89 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 90 | /** Human-meaningful string to identify this effect; may be embedded |
| 91 | in generated shader code. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 92 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 94 | int numTransforms() const { return fCoordTransforms.count(); } |
| 95 | |
| 96 | /** Returns the coordinate transformation at index. index must be valid according to |
| 97 | numTransforms(). */ |
| 98 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 99 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 100 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 102 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 103 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 104 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 105 | |
| 106 | /** Shortcut for textureAccess(index).texture(); */ |
| 107 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 109 | /** Will this effect read the destination pixel value? */ |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 110 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 111 | |
| 112 | /** Will this effect read the fragment position? */ |
| 113 | bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 114 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 115 | /** Will this effect emit custom vertex shader code? |
| 116 | (To set this value the effect must inherit from GrVertexEffect.) */ |
| 117 | bool hasVertexCode() const { return fHasVertexCode; } |
| 118 | |
| 119 | int numVertexAttribs() const { |
| 120 | SkASSERT(0 == fVertexAttribTypes.count() || fHasVertexCode); |
| 121 | return fVertexAttribTypes.count(); |
| 122 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 123 | |
| 124 | GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; } |
| 125 | |
| 126 | static const int kMaxVertexAttribs = 2; |
| 127 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 128 | /** Useful for effects that want to insert a texture matrix that is implied by the texture |
| 129 | dimensions */ |
| 130 | static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 131 | SkASSERT(NULL != texture); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 132 | SkMatrix mat; |
| 133 | mat.setIDiv(texture->width(), texture->height()); |
| 134 | return mat; |
| 135 | } |
| 136 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 137 | void* operator new(size_t size); |
| 138 | void operator delete(void* target); |
| 139 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 140 | void* operator new(size_t size, void* placement) { |
| 141 | return ::operator new(size, placement); |
| 142 | } |
| 143 | void operator delete(void* target, void* placement) { |
| 144 | ::operator delete(target, placement); |
| 145 | } |
| 146 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 147 | protected: |
| 148 | /** |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 149 | * Subclasses call this from their constructor to register coordinate transformations. The |
| 150 | * effect subclass manages the lifetime of the transformations (this function only stores a |
| 151 | * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the |
| 152 | * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll |
| 153 | * have 2. This must only be called from the constructor because GrEffects are immutable. |
| 154 | */ |
| 155 | void addCoordTransform(const GrCoordTransform* coordTransform); |
| 156 | |
| 157 | /** |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 158 | * Subclasses call this from their constructor to register GrTextureAccesses. The effect |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 159 | * subclass manages the lifetime of the accesses (this function only stores a pointer). The |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 160 | * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 161 | * called from the constructor because GrEffects are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 162 | */ |
| 163 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 164 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 165 | GrEffect() |
| 166 | : fWillReadDstColor(false) |
| 167 | , fWillReadFragmentPosition(false) |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 168 | , fWillUseInputColor(true) |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 169 | , fHasVertexCode(false) {} |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 170 | |
bsalomon | 0139ae3 | 2014-07-08 10:30:37 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Helper for down-casting to a GrEffect subclass |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 173 | */ |
bsalomon | 0139ae3 | 2014-07-08 10:30:37 -0700 | [diff] [blame] | 174 | template <typename T> static const T& CastEffect(const GrEffect& effect) { |
| 175 | return *static_cast<const T*>(&effect); |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 178 | /** |
| 179 | * If the effect subclass will read the destination pixel value then it must call this function |
| 180 | * from its constructor. Otherwise, when its generated backend-specific effect class attempts |
| 181 | * to generate code that reads the destination pixel it will fail. |
| 182 | */ |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 183 | void setWillReadDstColor() { fWillReadDstColor = true; } |
| 184 | |
| 185 | /** |
| 186 | * If the effect will generate a backend-specific effect that will read the fragment position |
| 187 | * in the FS then it must call this method from its constructor. Otherwise, the request to |
| 188 | * access the fragment position will be denied. |
| 189 | */ |
| 190 | void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 191 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 192 | /** |
| 193 | * If the effect will generate a result that does not depend on the input color value then it must |
| 194 | * call this function from its constructor. Otherwise, when its generated backend-specific code |
| 195 | * might fail during variable binding due to unused variables. |
| 196 | */ |
| 197 | void setWillNotUseInputColor() { fWillUseInputColor = false; } |
| 198 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 199 | private: |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 200 | SkDEBUGCODE(void assertEquality(const GrEffect& other) const;) |
| 201 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 202 | /** Subclass implements this to support isEqual(). It will only be called if it is known that |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 203 | the two effects are of the same subclass (i.e. they return the same object from |
| 204 | getFactory()).*/ |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 205 | virtual bool onIsEqual(const GrEffect& other) const = 0; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 206 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 207 | friend class GrVertexEffect; // to set fHasVertexCode and build fVertexAttribTypes. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 208 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 209 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 210 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
| 211 | SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 212 | bool fWillReadDstColor; |
| 213 | bool fWillReadFragmentPosition; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 214 | bool fWillUseInputColor; |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 215 | bool fHasVertexCode; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 216 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 217 | typedef SkRefCnt INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 220 | /** |
| 221 | * This creates an effect outside of the effect memory pool. The effect's destructor will be called |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 222 | * at global destruction time. NAME will be the name of the created GrEffect. |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 223 | */ |
| 224 | #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \ |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 225 | static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \ |
| 226 | static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \ |
| 227 | static SkAutoTDestroy<GrEffect> NAME##_ad(NAME); |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 228 | |
| 229 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 230 | #endif |