OLD | NEW |
| (Empty) |
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 | |
8 #ifndef GrEffect_DEFINED | |
9 #define GrEffect_DEFINED | |
10 | |
11 #include "GrColor.h" | |
12 #include "GrEffectUnitTest.h" | |
13 #include "GrProgramElement.h" | |
14 #include "GrShaderVar.h" | |
15 #include "GrTextureAccess.h" | |
16 #include "GrTypesPriv.h" | |
17 #include "SkString.h" | |
18 | |
19 class GrBackendEffectFactory; | |
20 class GrContext; | |
21 class GrCoordTransform; | |
22 | |
23 /** Provides custom vertex shader, fragment shader, uniform data for a particula
r stage of the | |
24 Ganesh shading pipeline. | |
25 Subclasses must have a function that produces a human-readable name: | |
26 static const char* Name(); | |
27 GrEffect objects *must* be immutable: after being constructed, their fields
may not change. | |
28 | |
29 Dynamically allocated GrEffects 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 use the macro GR_CREATE_STATIC_EFFECT declared below. | |
32 */ | |
33 class GrEffect : public GrProgramElement { | |
34 public: | |
35 SK_DECLARE_INST_COUNT(GrEffect) | |
36 | |
37 virtual ~GrEffect(); | |
38 | |
39 /** | |
40 * 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 * 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 * corresponding bit in validFlags is set. | |
45 */ | |
46 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const = 0; | |
47 | |
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- | |
52 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 | |
54 a nested type (or typedef) named GLEffect which will be the subclass of
GrGLEffect created | |
55 by the factory. | |
56 | |
57 Example: | |
58 class MyCustomEffect : public GrEffect { | |
59 ... | |
60 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE
{ | |
61 return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); | |
62 } | |
63 ... | |
64 }; | |
65 */ | |
66 virtual const GrBackendEffectFactory& getFactory() const = 0; | |
67 | |
68 /** 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 | |
70 from getFactory()). | |
71 | |
72 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 | |
74 computed by the GrBackendEffectFactory. | |
75 */ | |
76 bool isEqual(const GrEffect& other) const { | |
77 if (&this->getFactory() != &other.getFactory()) { | |
78 return false; | |
79 } | |
80 bool result = this->onIsEqual(other); | |
81 #ifdef SK_DEBUG | |
82 if (result) { | |
83 this->assertEquality(other); | |
84 } | |
85 #endif | |
86 return result; | |
87 } | |
88 | |
89 /** Human-meaningful string to identify this effect; may be embedded | |
90 in generated shader code. */ | |
91 const char* name() const; | |
92 | |
93 int numTransforms() const { return fCoordTransforms.count(); } | |
94 | |
95 /** Returns the coordinate transformation at index. index must be valid acco
rding to | |
96 numTransforms(). */ | |
97 const GrCoordTransform& coordTransform(int index) const { return *fCoordTran
sforms[index]; } | |
98 | |
99 int numTextures() const { return fTextureAccesses.count(); } | |
100 | |
101 /** Returns the access pattern for the texture at index. index must be valid
according to | |
102 numTextures(). */ | |
103 const GrTextureAccess& textureAccess(int index) const { return *fTextureAcce
sses[index]; } | |
104 | |
105 /** Shortcut for textureAccess(index).texture(); */ | |
106 GrTexture* texture(int index) const { return this->textureAccess(index).getT
exture(); } | |
107 | |
108 /** Will this effect read the destination pixel value? */ | |
109 bool willReadDstColor() const { return fWillReadDstColor; } | |
110 | |
111 /** Will this effect read the fragment position? */ | |
112 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } | |
113 | |
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); | |
124 void operator delete(void* target); | |
125 | |
126 void* operator new(size_t size, void* placement) { | |
127 return ::operator new(size, placement); | |
128 } | |
129 void operator delete(void* target, void* placement) { | |
130 ::operator delete(target, placement); | |
131 } | |
132 | |
133 /** | |
134 * Helper for down-casting to a GrEffect subclass | |
135 */ | |
136 template <typename T> const T& cast() const { return *static_cast<const T*>(
this); } | |
137 | |
138 protected: | |
139 /** | |
140 * 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 | |
142 * pointer). The GrCoordTransform is typically a member field of the GrEffec
t subclass. When the | |
143 * matrix has perspective, the transformed coordinates will have 3 component
s. Otherwise they'll | |
144 * have 2. This must only be called from the constructor because GrEffects a
re immutable. | |
145 */ | |
146 void addCoordTransform(const GrCoordTransform* coordTransform); | |
147 | |
148 /** | |
149 * 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 | |
151 * GrTextureAccess is typically a member field of the GrEffect subclass. Thi
s must only be | |
152 * called from the constructor because GrEffects are immutable. | |
153 */ | |
154 void addTextureAccess(const GrTextureAccess* textureAccess); | |
155 | |
156 GrEffect() | |
157 : fWillReadDstColor(false) | |
158 , fWillReadFragmentPosition(false) | |
159 , fWillUseInputColor(true) | |
160 , fRequiresVertexShader(false) {} | |
161 | |
162 /** | |
163 * 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 | |
165 * to generate code that reads the destination pixel it will fail. | |
166 */ | |
167 void setWillReadDstColor() { fWillReadDstColor = true; } | |
168 | |
169 /** | |
170 * If the effect will generate a backend-specific effect that will read the
fragment position | |
171 * in the FS then it must call this method from its constructor. Otherwise,
the request to | |
172 * access the fragment position will be denied. | |
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 */ | |
181 void setWillNotUseInputColor() { fWillUseInputColor = false; } | |
182 | |
183 private: | |
184 SkDEBUGCODE(void assertEquality(const GrEffect& other) const;) | |
185 | |
186 /** Subclass implements this to support isEqual(). It will only be called if
it is known that | |
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 }; | |
203 | |
204 /** | |
205 * 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. | |
207 */ | |
208 #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS)
\ | |
209 static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage;
\ | |
210 static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLAS
S, ARGS); \ | |
211 static SkAutoTDestroy<GrEffect> NAME##_ad(NAME); | |
212 | |
213 #endif | |
OLD | NEW |