OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrEffect_DEFINED | 8 #ifndef GrProcessor_DEFINED |
9 #define GrEffect_DEFINED | 9 #define GrProcessor_DEFINED |
10 | 10 |
11 #include "GrBackendProcessorFactory.h" | |
11 #include "GrColor.h" | 12 #include "GrColor.h" |
12 #include "GrEffectUnitTest.h" | 13 #include "GrProcessorUnitTest.h" |
13 #include "GrProgramElement.h" | 14 #include "GrProgramElement.h" |
14 #include "GrShaderVar.h" | 15 #include "GrShaderVar.h" |
15 #include "GrTextureAccess.h" | 16 #include "GrTextureAccess.h" |
16 #include "GrTypesPriv.h" | 17 #include "GrTypesPriv.h" |
17 #include "SkString.h" | 18 #include "SkString.h" |
18 | 19 |
19 class GrBackendEffectFactory; | 20 class GrBackendProcessorFactory; |
20 class GrContext; | 21 class GrContext; |
21 class GrCoordTransform; | 22 class GrCoordTransform; |
22 | 23 |
23 /** Provides custom vertex shader, fragment shader, uniform data for a particula r stage of the | 24 /** Provides custom vertex shader, fragment shader, uniform data for a particula r stage of the |
24 Ganesh shading pipeline. | 25 Ganesh shading pipeline. |
25 Subclasses must have a function that produces a human-readable name: | 26 Subclasses must have a function that produces a human-readable name: |
26 static const char* Name(); | 27 static const char* Name(); |
27 GrEffect objects *must* be immutable: after being constructed, their fields may not change. | 28 GrProcessor objects *must* be immutable: after being constructed, their fiel ds may not change. |
28 | 29 |
29 Dynamically allocated GrEffects are managed by a per-thread memory pool. The ref count of an | 30 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an |
30 effect must reach 0 before the thread terminates and the pool is destroyed. To create a static | 31 effect must reach 0 before the thread terminates and the pool is destroyed. To create a static |
31 effect use the macro GR_CREATE_STATIC_EFFECT declared below. | 32 effect use the macro GR_CREATE_STATIC_EFFECT declared below. |
32 */ | 33 */ |
33 class GrEffect : public GrProgramElement { | 34 class GrProcessor : public GrProgramElement { |
34 public: | 35 public: |
35 SK_DECLARE_INST_COUNT(GrEffect) | 36 SK_DECLARE_INST_COUNT(GrProcessor) |
36 | 37 |
37 virtual ~GrEffect(); | 38 virtual ~GrProcessor(); |
38 | 39 |
39 /** | 40 /** |
40 * This function is used to perform optimizations. When called the color and validFlags params | 41 * This function is used to perform optimizations. When called the color and validFlags params |
41 * indicate whether the input components to this effect in the FS will have known values. | 42 * indicate whether the input components to this effect in the FS will have known values. |
42 * validFlags is a bitfield of GrColorComponentFlags. The function updates b oth params to | 43 * validFlags is a bitfield of GrColorComponentFlags. The function updates b oth params to |
43 * indicate known values of its output. A component of the color param only has meaning if the | 44 * indicate known values of its output. A component of the color param only has meaning if the |
44 * corresponding bit in validFlags is set. | 45 * corresponding bit in validFlags is set. |
45 */ | 46 */ |
46 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const = 0; | 47 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const = 0; |
47 | 48 |
48 /** Will this effect read the source color value? */ | |
49 bool willUseInputColor() const { return fWillUseInputColor; } | |
50 | |
51 /** This object, besides creating back-end-specific helper objects, is used for run-time-type- | 49 /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
52 identification. The factory should be an instance of templated class, | 50 identification. The factory should be an instance of templated class, |
53 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. Th e subclass must have | 51 GrTBackendEffectFactory. It is templated on the subclass of GrProcessor. The subclass must |
54 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created | 52 have a nested type (or typedef) named GLProcessor which will be the subc lass of |
55 by the factory. | 53 GrGLProcessor created by the factory. |
56 | 54 |
57 Example: | 55 Example: |
58 class MyCustomEffect : public GrEffect { | 56 class MyCustomEffect : public GrProcessor { |
59 ... | 57 ... |
60 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 58 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
61 return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); | 59 return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
62 } | 60 } |
63 ... | 61 ... |
64 }; | 62 }; |
65 */ | 63 */ |
66 virtual const GrBackendEffectFactory& getFactory() const = 0; | 64 virtual const GrBackendProcessorFactory& getFactory() const = 0; |
67 | 65 |
68 /** Returns true if this and other effect conservatively draw identically. I t can only return | 66 /** Returns true if this and other effect conservatively draw identically. I t can only return |
69 true when the two effects are of the same subclass (i.e. they return the same object from | 67 true when the two effects are of the same subclass (i.e. they return the same object from |
70 from getFactory()). | 68 from getFactory()). |
71 | 69 |
72 A return value of true from isEqual() should not be used to test whether the effects would | 70 A return value of true from isEqual() should not be used to test whether the effects would |
73 generate the same shader code. To test for identical code generation use the effects' keys | 71 generate the same shader code. To test for identical code generation use the effects' keys |
74 computed by the GrBackendEffectFactory. | 72 computed by the GrBackendEffectFactory. |
75 */ | 73 */ |
76 bool isEqual(const GrEffect& other) const { | 74 bool isEqual(const GrProcessor& other) const { |
77 if (&this->getFactory() != &other.getFactory()) { | 75 if (&this->getFactory() != &other.getFactory()) { |
78 return false; | 76 return false; |
79 } | 77 } |
80 bool result = this->onIsEqual(other); | 78 bool result = this->onIsEqual(other); |
81 #ifdef SK_DEBUG | 79 #ifdef SK_DEBUG |
82 if (result) { | 80 if (result) { |
83 this->assertEquality(other); | 81 this->assertEquality(other); |
84 } | 82 } |
85 #endif | 83 #endif |
86 return result; | 84 return result; |
(...skipping 11 matching lines...) Expand all Loading... | |
98 | 96 |
99 int numTextures() const { return fTextureAccesses.count(); } | 97 int numTextures() const { return fTextureAccesses.count(); } |
100 | 98 |
101 /** Returns the access pattern for the texture at index. index must be valid according to | 99 /** Returns the access pattern for the texture at index. index must be valid according to |
102 numTextures(). */ | 100 numTextures(). */ |
103 const GrTextureAccess& textureAccess(int index) const { return *fTextureAcce sses[index]; } | 101 const GrTextureAccess& textureAccess(int index) const { return *fTextureAcce sses[index]; } |
104 | 102 |
105 /** Shortcut for textureAccess(index).texture(); */ | 103 /** Shortcut for textureAccess(index).texture(); */ |
106 GrTexture* texture(int index) const { return this->textureAccess(index).getT exture(); } | 104 GrTexture* texture(int index) const { return this->textureAccess(index).getT exture(); } |
107 | 105 |
108 /** Will this effect read the destination pixel value? */ | |
109 bool willReadDstColor() const { return fWillReadDstColor; } | |
110 | |
111 /** Will this effect read the fragment position? */ | 106 /** Will this effect read the fragment position? */ |
112 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } | 107 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } |
113 | 108 |
114 /** Will this effect emit custom vertex shader code? | |
115 (To set this value the effect must inherit from GrEffect.) */ | |
116 bool requiresVertexShader() const { return fRequiresVertexShader; } | |
117 | |
118 static const int kMaxVertexAttribs = 2; | |
119 typedef SkSTArray<kMaxVertexAttribs, GrShaderVar, true> VertexAttribArray; | |
120 | |
121 const VertexAttribArray& getVertexAttribs() const { return fVertexAttribs; } | |
122 | |
123 void* operator new(size_t size); | 109 void* operator new(size_t size); |
124 void operator delete(void* target); | 110 void operator delete(void* target); |
125 | 111 |
126 void* operator new(size_t size, void* placement) { | 112 void* operator new(size_t size, void* placement) { |
127 return ::operator new(size, placement); | 113 return ::operator new(size, placement); |
128 } | 114 } |
129 void operator delete(void* target, void* placement) { | 115 void operator delete(void* target, void* placement) { |
130 ::operator delete(target, placement); | 116 ::operator delete(target, placement); |
131 } | 117 } |
132 | 118 |
133 /** | 119 /** |
134 * Helper for down-casting to a GrEffect subclass | 120 * Helper for down-casting to a GrProcessor subclass |
135 */ | 121 */ |
136 template <typename T> const T& cast() const { return *static_cast<const T*>( this); } | 122 template <typename T> const T& cast() const { return *static_cast<const T*>( this); } |
137 | 123 |
138 protected: | 124 protected: |
139 /** | 125 /** |
140 * Subclasses call this from their constructor to register coordinate transf ormations. The | 126 * Subclasses call this from their constructor to register coordinate transf ormations. The |
141 * effect subclass manages the lifetime of the transformations (this functio n only stores a | 127 * effect subclass manages the lifetime of the transformations (this functio n only stores a |
142 * pointer). The GrCoordTransform is typically a member field of the GrEffec t subclass. When the | 128 * pointer). The GrCoordTransform is typically a member field of the GrProce ssor subclass. When |
143 * matrix has perspective, the transformed coordinates will have 3 component s. Otherwise they'll | 129 * the matrix has perspective, the transformed coordinates will have 3 compo nents. Otherwise |
144 * have 2. This must only be called from the constructor because GrEffects a re immutable. | 130 * they'll have 2. This must only be called from the constructor because GrP rocessors are |
131 * immutable. | |
145 */ | 132 */ |
146 void addCoordTransform(const GrCoordTransform* coordTransform); | 133 void addCoordTransform(const GrCoordTransform* coordTransform); |
147 | 134 |
148 /** | 135 /** |
149 * Subclasses call this from their constructor to register GrTextureAccesses . The effect | 136 * Subclasses call this from their constructor to register GrTextureAccesses . The effect |
150 * subclass manages the lifetime of the accesses (this function only stores a pointer). The | 137 * subclass manages the lifetime of the accesses (this function only stores a pointer). The |
151 * GrTextureAccess is typically a member field of the GrEffect subclass. Thi s must only be | 138 * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be |
152 * called from the constructor because GrEffects are immutable. | 139 * called from the constructor because GrProcessors are immutable. |
153 */ | 140 */ |
154 void addTextureAccess(const GrTextureAccess* textureAccess); | 141 void addTextureAccess(const GrTextureAccess* textureAccess); |
155 | 142 |
156 GrEffect() | 143 GrProcessor() |
157 : fWillReadDstColor(false) | 144 : fWillReadFragmentPosition(false) |
158 , fWillReadFragmentPosition(false) | |
159 , fWillUseInputColor(true) | |
160 , fRequiresVertexShader(false) {} | 145 , fRequiresVertexShader(false) {} |
161 | 146 |
162 /** | 147 /** |
148 * If the effect will generate a backend-specific effect that will read the fragment position | |
149 * in the FS then it must call this method from its constructor. Otherwise, the request to | |
150 * access the fragment position will be denied. | |
151 */ | |
152 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } | |
153 | |
154 private: | |
155 SkDEBUGCODE(void assertEquality(const GrProcessor& other) const;) | |
156 | |
157 /** Subclass implements this to support isEqual(). It will only be called if it is known that | |
158 the two effects are of the same subclass (i.e. they return the same obje ct from | |
159 getFactory()).*/ | |
160 virtual bool onIsEqual(const GrProcessor& other) const = 0; | |
161 | |
162 friend class GrGeometryProcessor; // to set fRequiresVertexShader and build fVertexAttribTypes. | |
163 | |
164 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; | |
165 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; | |
166 bool fWillReadFragmentPosition; | |
167 bool fRequiresVertexShader; | |
168 | |
169 typedef GrProgramElement INHERITED; | |
170 }; | |
171 | |
172 class GrFragmentProcessor : public GrProcessor { | |
bsalomon
2014/09/22 14:56:38
Either for this land or in a future CL can you put
joshua.litt
2014/09/22 18:14:44
I will do it in a future CL because it means updat
| |
173 public: | |
174 GrFragmentProcessor() | |
175 : INHERITED() | |
176 , fWillReadDstColor(false) | |
177 , fWillUseInputColor(true) {} | |
178 | |
179 virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0; | |
180 | |
181 /** Will this effect read the destination pixel value? */ | |
182 bool willReadDstColor() const { return fWillReadDstColor; } | |
183 | |
184 /** Will this effect read the source color value? */ | |
185 bool willUseInputColor() const { return fWillUseInputColor; } | |
186 | |
187 protected: | |
188 /** | |
163 * If the effect subclass will read the destination pixel value then it must call this function | 189 * If the effect subclass will read the destination pixel value then it must call this function |
164 * from its constructor. Otherwise, when its generated backend-specific effe ct class attempts | 190 * from its constructor. Otherwise, when its generated backend-specific effe ct class attempts |
165 * to generate code that reads the destination pixel it will fail. | 191 * to generate code that reads the destination pixel it will fail. |
166 */ | 192 */ |
167 void setWillReadDstColor() { fWillReadDstColor = true; } | 193 void setWillReadDstColor() { fWillReadDstColor = true; } |
168 | 194 |
169 /** | 195 /** |
170 * If the effect will generate a backend-specific effect that will read the fragment position | 196 * If the effect will generate a result that does not depend on the input co lor value then it |
171 * in the FS then it must call this method from its constructor. Otherwise, the request to | 197 * must call this function from its constructor. Otherwise, when its generat ed backend-specific |
172 * access the fragment position will be denied. | 198 * code might fail during variable binding due to unused variables. |
173 */ | |
174 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } | |
175 | |
176 /** | |
177 * If the effect will generate a result that does not depend on the input co lor value then it must | |
178 * call this function from its constructor. Otherwise, when its generated ba ckend-specific code | |
179 * might fail during variable binding due to unused variables. | |
180 */ | 199 */ |
181 void setWillNotUseInputColor() { fWillUseInputColor = false; } | 200 void setWillNotUseInputColor() { fWillUseInputColor = false; } |
182 | 201 |
183 private: | 202 private: |
184 SkDEBUGCODE(void assertEquality(const GrEffect& other) const;) | 203 bool fWillReadDstColor; |
204 bool fWillUseInputColor; | |
185 | 205 |
186 /** Subclass implements this to support isEqual(). It will only be called if it is known that | 206 typedef GrProcessor INHERITED; |
187 the two effects are of the same subclass (i.e. they return the same obje ct from | |
188 getFactory()).*/ | |
189 virtual bool onIsEqual(const GrEffect& other) const = 0; | |
190 | |
191 friend class GrGeometryProcessor; // to set fRequiresVertexShader and build fVertexAttribTypes. | |
192 | |
193 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; | |
194 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; | |
195 VertexAttribArray fVertexAttribs; | |
196 bool fWillReadDstColor; | |
197 bool fWillReadFragmentPosition; | |
198 bool fWillUseInputColor; | |
199 bool fRequiresVertexShader; | |
200 | |
201 typedef GrProgramElement INHERITED; | |
202 }; | 207 }; |
203 | 208 |
204 /** | 209 /** |
205 * This creates an effect outside of the effect memory pool. The effect's destru ctor will be called | 210 * This creates an effect outside of the effect memory pool. The effect's destru ctor will be called |
206 * at global destruction time. NAME will be the name of the created GrEffect. | 211 * at global destruction time. NAME will be the name of the created GrProcessor. |
207 */ | 212 */ |
208 #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \ | 213 #define GR_CREATE_STATIC_FRAGMENT_PROCESSOR(NAME, EFFECT_CLASS, ARGS) \ |
209 static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \ | 214 static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \ |
210 static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLAS S, ARGS); \ | 215 static GrFragmentProcessor* \ |
211 static SkAutoTDestroy<GrEffect> NAME##_ad(NAME); | 216 NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \ |
217 static SkAutoTDestroy<GrFragmentProcessor> NAME##_ad(NAME); | |
212 | 218 |
213 #endif | 219 #endif |
OLD | NEW |