bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +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 | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 8 | #ifndef GrBackendEffectFactory_DEFINED |
| 9 | #define GrBackendEffectFactory_DEFINED |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 10 | |
| 11 | #include "GrTypes.h" |
bsalomon@google.com | 8e520fc | 2012-05-18 20:06:45 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 13 | #include "GrNoncopyable.h" |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 15 | /** Given a GrEffect of a particular type, creates the corresponding |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 16 | graphics-backend-specific GrGLEffect. Also tracks equivalence |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 17 | of shaders generated via a key. |
| 18 | */ |
| 19 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 20 | class GrEffect; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 21 | class GrGLEffect; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 22 | class GrGLCaps; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 24 | class GrBackendEffectFactory : public GrNoncopyable { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 25 | public: |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 26 | typedef uint32_t StageKey; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 27 | enum { |
| 28 | kProgramStageKeyBits = 10, |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 29 | kTexturingStageKeyBits = 6 |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 32 | virtual StageKey glStageKey(const GrEffect&, const GrGLCaps&) const = 0; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 33 | virtual GrGLEffect* createGLInstance(const GrEffect&) const = 0; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 35 | bool operator ==(const GrBackendEffectFactory& b) const { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 36 | return fEffectClassID == b.fEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 37 | } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 38 | bool operator !=(const GrBackendEffectFactory& b) const { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 39 | return !(*this == b); |
| 40 | } |
| 41 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 42 | virtual const char* name() const = 0; |
| 43 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 44 | protected: |
| 45 | enum { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 46 | kIllegalEffectClassID = 0, |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 49 | GrBackendEffectFactory() { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 50 | fEffectClassID = kIllegalEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static StageKey GenID() { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 54 | // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 55 | // atomic inc returns the old value not the incremented value. So we add |
| 56 | // 1 to the returned value. |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 57 | int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 58 | GrAssert(id < (1 << (8 * sizeof(StageKey) - kProgramStageKeyBits))); |
| 59 | return id; |
| 60 | } |
| 61 | |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 62 | StageKey fEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 65 | static int32_t fCurrEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 68 | template <typename EffectClass> |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 69 | class GrTBackendEffectFactory : public GrBackendEffectFactory { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 70 | |
| 71 | public: |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 72 | typedef typename EffectClass::GLEffect GLEffect; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 74 | /** Returns a human-readable name that is accessible via GrEffect or |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 75 | GrGLEffect and is consistent between the two of them. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 76 | */ |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 77 | virtual const char* name() const SK_OVERRIDE { return EffectClass::Name(); } |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 78 | |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 79 | /** Returns a value that identifies the GLSL shader code generated by |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 80 | a GrEffect. This enables caching of generated shaders. Part of the |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 81 | id identifies the GrEffect subclass. The remainder is based |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 82 | on the aspects of the GrEffect object's configuration that affect |
bsalomon@google.com | 16fd21b | 2012-05-21 21:18:13 +0000 | [diff] [blame] | 83 | GLSL code generation. */ |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 84 | virtual StageKey glStageKey(const GrEffect& effect, const GrGLCaps& caps) const SK_OVERRIDE { |
| 85 | GrAssert(kIllegalEffectClassID != fEffectClassID); |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 86 | StageKey stageID = GLEffect::GenKey(effect, caps); |
| 87 | StageKey textureKey = GLEffect::GenTextureKey(effect, caps); |
bsalomon@google.com | 7c4d06d | 2012-05-21 15:31:00 +0000 | [diff] [blame] | 88 | #if GR_DEBUG |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 89 | static const StageKey kIllegalIDMask = (uint16_t) (~((1U << kProgramStageKeyBits) - 1)); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 90 | GrAssert(!(kIllegalIDMask & stageID)); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 91 | |
| 92 | static const StageKey kIllegalTextureKeyMask = |
| 93 | (uint16_t) (~((1U << kTexturingStageKeyBits) - 1)); |
| 94 | GrAssert(!(kIllegalTextureKeyMask & textureKey)); |
bsalomon@google.com | 7c4d06d | 2012-05-21 15:31:00 +0000 | [diff] [blame] | 95 | #endif |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 96 | return fEffectClassID | (textureKey << kProgramStageKeyBits) | stageID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** Returns a new instance of the appropriate *GL* implementation class |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 100 | for the given GrEffect; caller is responsible for deleting |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 101 | the object. */ |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 102 | virtual GLEffect* createGLInstance(const GrEffect& effect) const SK_OVERRIDE { |
| 103 | return SkNEW_ARGS(GLEffect, (*this, effect)); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 104 | } |
| 105 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 106 | /** This class is a singleton. This function returns the single instance. |
| 107 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 108 | static const GrBackendEffectFactory& getInstance() { |
| 109 | static SkAlignedSTStorage<1, GrTBackendEffectFactory> gInstanceMem; |
| 110 | static const GrTBackendEffectFactory* gInstance; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 111 | if (!gInstance) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 112 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 113 | GrTBackendEffectFactory); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 114 | } |
| 115 | return *gInstance; |
| 116 | } |
| 117 | |
| 118 | protected: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 119 | GrTBackendEffectFactory() { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 120 | fEffectClassID = GenID() << (kProgramStageKeyBits + kTexturingStageKeyBits) ; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
| 124 | #endif |