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 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 11 | #include "GrRefCnt.h" |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 12 | #include "GrNoncopyable.h" |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 13 | #include "GrEffectUnitTest.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 15 | #include "GrTextureAccess.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; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 19 | class SkString; |
| 20 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 21 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 22 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 23 | Subclasses must have a function that produces a human-readable name: |
| 24 | static const char* Name(); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 25 | GrEffect objects *must* be immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 26 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 27 | class GrEffect : public GrRefCnt { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 28 | public: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 29 | SK_DECLARE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 31 | GrEffect() {}; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 32 | virtual ~GrEffect(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 34 | /** If given an input texture that is/is not opaque, is this effect guaranteed to produce an |
| 35 | opaque output? */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 36 | virtual bool isOpaque(bool inputTextureIsOpaque) const; |
| 37 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 38 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 39 | identification. The factory should be an instance of templated class, |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 40 | 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] | 41 | a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created |
| 42 | by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 43 | |
| 44 | Example: |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 45 | class MyCustomEffect : public GrEffect { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 46 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 47 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 48 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 49 | } |
| 50 | ... |
| 51 | }; |
| 52 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 53 | virtual const GrBackendEffectFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 55 | /** Returns true if the other effect will generate identical output. |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 56 | Must only be called if the two are already known to be of the |
| 57 | same type (i.e. they return the same value from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 58 | |
| 59 | Equality is not the same thing as equivalence. |
| 60 | To test for equivalence (that they will generate the same |
| 61 | shader code, but may have different uniforms), check equality |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 62 | of the EffectKey produced by the GrBackendEffectFactory: |
| 63 | a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b). |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 64 | |
| 65 | The default implementation of this function returns true iff |
| 66 | the two stages have the same return value for numTextures() and |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 67 | for texture() over all valid indices. |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 68 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 69 | virtual bool isEqual(const GrEffect&) const; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 70 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 71 | /** Human-meaningful string to identify this effect; may be embedded |
| 72 | in generated shader code. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 73 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 75 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 77 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 78 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 79 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 80 | |
| 81 | /** Shortcut for textureAccess(index).texture(); */ |
| 82 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 84 | /** Useful for effects that want to insert a texture matrix that is implied by the texture |
| 85 | dimensions */ |
| 86 | static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
| 87 | GrAssert(NULL != texture); |
| 88 | SkMatrix mat; |
| 89 | mat.setIDiv(texture->width(), texture->height()); |
| 90 | return mat; |
| 91 | } |
| 92 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 93 | void* operator new(size_t size); |
| 94 | void operator delete(void* target); |
| 95 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 96 | protected: |
| 97 | /** |
| 98 | * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass |
| 99 | * manages the lifetime of the accesses (this function only stores a pointer). This must only be |
| 100 | * called from the constructor because GrEffects are supposed to be immutable. |
| 101 | */ |
| 102 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 103 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 104 | private: |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 105 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 106 | typedef GrRefCnt INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | #endif |