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; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 20 | class SkString; |
| 21 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 22 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 23 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 24 | Subclasses must have a function that produces a human-readable name: |
| 25 | static const char* Name(); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 26 | 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] | 27 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 28 | class GrEffect : public GrRefCnt { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 29 | public: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 32 | GrEffect() {}; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 33 | virtual ~GrEffect(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame^] | 35 | /** |
| 36 | * Flags for getConstantColorComponents. They are defined so that the bit order reflects the |
| 37 | * GrColor shift order. |
| 38 | */ |
| 39 | enum ValidComponentFlags { |
| 40 | kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8), |
| 41 | kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8), |
| 42 | kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8), |
| 43 | kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8), |
| 44 | |
| 45 | kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag | |
| 46 | kB_ValidComponentFlag | kA_ValidComponentFlag) |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * This function is used to perform optimizations. When called the color and validFlags params |
| 51 | * indicate whether the input components to this effect in the FS will have known values. The |
| 52 | * function updates both params to indicate known values of its output. A component of the color |
| 53 | * param only has meaning if the corresponding bit in validFlags is set. |
| 54 | */ |
| 55 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 57 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 58 | identification. The factory should be an instance of templated class, |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 59 | 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] | 60 | a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created |
| 61 | by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 62 | |
| 63 | Example: |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 64 | class MyCustomEffect : public GrEffect { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 65 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 66 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 67 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 68 | } |
| 69 | ... |
| 70 | }; |
| 71 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 72 | virtual const GrBackendEffectFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 74 | /** Returns true if the other effect will generate identical output. |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 75 | Must only be called if the two are already known to be of the |
| 76 | same type (i.e. they return the same value from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 77 | |
| 78 | Equality is not the same thing as equivalence. |
| 79 | To test for equivalence (that they will generate the same |
| 80 | shader code, but may have different uniforms), check equality |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 81 | of the EffectKey produced by the GrBackendEffectFactory: |
| 82 | a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b). |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 83 | |
| 84 | The default implementation of this function returns true iff |
| 85 | the two stages have the same return value for numTextures() and |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 86 | for texture() over all valid indices. |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 87 | */ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 88 | virtual bool isEqual(const GrEffect&) const; |
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 | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 94 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 96 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 97 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 98 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 99 | |
| 100 | /** Shortcut for textureAccess(index).texture(); */ |
| 101 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 103 | /** Useful for effects that want to insert a texture matrix that is implied by the texture |
| 104 | dimensions */ |
| 105 | static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
| 106 | GrAssert(NULL != texture); |
| 107 | SkMatrix mat; |
| 108 | mat.setIDiv(texture->width(), texture->height()); |
| 109 | return mat; |
| 110 | } |
| 111 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 112 | void* operator new(size_t size); |
| 113 | void operator delete(void* target); |
| 114 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 115 | protected: |
| 116 | /** |
| 117 | * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass |
| 118 | * manages the lifetime of the accesses (this function only stores a pointer). This must only be |
| 119 | * called from the constructor because GrEffects are supposed to be immutable. |
| 120 | */ |
| 121 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 122 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 123 | private: |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 124 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 125 | typedef GrRefCnt INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | #endif |