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 | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 13 | #include "GrNoncopyable.h" |
| 14 | #include "GrRefCnt.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 15 | #include "GrTexture.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 16 | #include "GrTextureAccess.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 18 | class GrBackendEffectFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 19 | class GrContext; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 20 | class GrEffect; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 21 | class SkString; |
| 22 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 23 | /** |
| 24 | * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue |
| 25 | * new draw operations separately from ownership within a deferred drawing queue. When the |
| 26 | * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled |
| 27 | * in service of later draws. However, the deferred draw queue may still own direct references to |
| 28 | * the underlying GrEffect. |
| 29 | */ |
| 30 | class GrEffectRef : public SkRefCnt { |
| 31 | public: |
| 32 | SK_DECLARE_INST_COUNT(GrEffectRef); |
| 33 | |
| 34 | GrEffect* get() { return fEffect; } |
| 35 | const GrEffect* get() const { return fEffect; } |
| 36 | |
| 37 | void* operator new(size_t size); |
| 38 | void operator delete(void* target); |
| 39 | |
| 40 | private: |
bsalomon@google.com | 64287c5 | 2013-01-16 15:25:55 +0000 | [diff] [blame^] | 41 | friend class GrEffect; // to construct these |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 42 | |
| 43 | explicit GrEffectRef(GrEffect* effect); |
| 44 | |
| 45 | virtual ~GrEffectRef(); |
| 46 | |
| 47 | GrEffect* fEffect; |
| 48 | |
| 49 | typedef SkRefCnt INHERITED; |
| 50 | }; |
| 51 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 52 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 53 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 54 | Subclasses must have a function that produces a human-readable name: |
| 55 | static const char* Name(); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 56 | 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] | 57 | |
| 58 | GrEffect subclass objects should be created by factory functions that return GrEffectRef. |
| 59 | There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static |
| 60 | member function of a GrEffect subclass. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 61 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 62 | class GrEffect : public GrRefCnt { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 63 | public: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 64 | SK_DECLARE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 66 | virtual ~GrEffect(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Flags for getConstantColorComponents. They are defined so that the bit order reflects the |
| 70 | * GrColor shift order. |
| 71 | */ |
| 72 | enum ValidComponentFlags { |
| 73 | kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8), |
| 74 | kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8), |
| 75 | kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8), |
| 76 | kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8), |
| 77 | |
| 78 | kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag | |
| 79 | kB_ValidComponentFlag | kA_ValidComponentFlag) |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * This function is used to perform optimizations. When called the color and validFlags params |
| 84 | * indicate whether the input components to this effect in the FS will have known values. The |
| 85 | * function updates both params to indicate known values of its output. A component of the color |
| 86 | * param only has meaning if the corresponding bit in validFlags is set. |
| 87 | */ |
| 88 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 90 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 91 | identification. The factory should be an instance of templated class, |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 92 | 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] | 93 | a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created |
| 94 | by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 95 | |
| 96 | Example: |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 97 | class MyCustomEffect : public GrEffect { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 98 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 99 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 100 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 101 | } |
| 102 | ... |
| 103 | }; |
| 104 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 105 | virtual const GrBackendEffectFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 107 | /** Returns true if the other effect will generate identical output. |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 108 | Must only be called if the two are already known to be of the |
| 109 | same type (i.e. they return the same value from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 110 | |
| 111 | Equality is not the same thing as equivalence. |
| 112 | To test for equivalence (that they will generate the same |
| 113 | shader code, but may have different uniforms), check equality |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 114 | of the EffectKey produced by the GrBackendEffectFactory: |
| 115 | a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b). |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 116 | |
| 117 | The default implementation of this function returns true iff |
| 118 | the two stages have the same return value for numTextures() and |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 119 | for texture() over all valid indices. |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 120 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 121 | virtual bool isEqual(const GrEffect&) const; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 122 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 123 | /** Human-meaningful string to identify this effect; may be embedded |
| 124 | in generated shader code. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 125 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 126 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 127 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 128 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 129 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 130 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 131 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 132 | |
| 133 | /** Shortcut for textureAccess(index).texture(); */ |
| 134 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 136 | /** Useful for effects that want to insert a texture matrix that is implied by the texture |
| 137 | dimensions */ |
| 138 | static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
| 139 | GrAssert(NULL != texture); |
| 140 | SkMatrix mat; |
| 141 | mat.setIDiv(texture->width(), texture->height()); |
| 142 | return mat; |
| 143 | } |
| 144 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 145 | void* operator new(size_t size); |
| 146 | void operator delete(void* target); |
| 147 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 148 | protected: |
| 149 | /** |
| 150 | * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass |
| 151 | * manages the lifetime of the accesses (this function only stores a pointer). This must only be |
| 152 | * called from the constructor because GrEffects are supposed to be immutable. |
| 153 | */ |
| 154 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 155 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 156 | GrEffect() : fEffectPtr(NULL) {}; |
| 157 | |
| 158 | /** This should be called by GrEffect subclass factories */ |
| 159 | static GrEffectRef* CreateEffectPtr(GrEffect* effect) { |
| 160 | if (NULL == effect->fEffectPtr) { |
| 161 | effect->fEffectPtr = SkNEW_ARGS(GrEffectRef, (effect)); |
| 162 | } else { |
| 163 | effect->fEffectPtr->ref(); |
| 164 | GrCrash("This function should only be called once per effect currently."); |
| 165 | } |
| 166 | return effect->fEffectPtr; |
| 167 | } |
| 168 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 169 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 170 | void effectPtrDestroyed() { |
| 171 | fEffectPtr = NULL; |
| 172 | } |
| 173 | |
bsalomon@google.com | 64287c5 | 2013-01-16 15:25:55 +0000 | [diff] [blame^] | 174 | friend class GrEffectRef; // to call GrEffectRef destroyed |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 175 | |
| 176 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
| 177 | GrEffectRef* fEffectPtr; |
| 178 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 179 | typedef GrRefCnt INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 182 | inline GrEffectRef::GrEffectRef(GrEffect* effect) { |
| 183 | GrAssert(NULL != effect); |
| 184 | effect->ref(); |
| 185 | fEffect = effect; |
| 186 | } |
| 187 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 188 | #endif |