2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrEffect_DEFINED
9 #define GrEffect_DEFINED
12 #include "GrEffectUnitTest.h"
13 #include "GrTexture.h"
14 #include "GrTextureAccess.h"
15 #include "GrTypesPriv.h"
17 class GrBackendEffectFactory;
19 class GrCoordTransform;
24 /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
25 Ganesh shading pipeline.
26 Subclasses must have a function that produces a human-readable name:
27 static const char* Name();
28 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
30 Dynamically allocated GrEffects are managed by a per-thread memory pool. The ref count of an
31 effect must reach 0 before the thread terminates and the pool is destroyed. To create a static
32 effect use the macro GR_CREATE_STATIC_EFFECT declared below.
34 class GrEffect : public SkRefCnt {
36 SK_DECLARE_INST_COUNT(GrEffect)
41 * This function is used to perform optimizations. When called the color and validFlags params
42 * indicate whether the input components to this effect in the FS will have known values.
43 * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to
44 * indicate known values of its output. A component of the color param only has meaning if the
45 * corresponding bit in validFlags is set.
47 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
49 /** Will this effect read the source color value? */
50 bool willUseInputColor() const { return fWillUseInputColor; }
52 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
53 identification. The factory should be an instance of templated class,
54 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
55 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
59 class MyCustomEffect : public GrEffect {
61 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
62 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
67 virtual const GrBackendEffectFactory& getFactory() const = 0;
69 /** Returns true if this and other effect conservatively draw identically. It can only return
70 true when the two effects are of the same subclass (i.e. they return the same object from
73 A return value of true from isEqual() should not be used to test whether the effects would
74 generate the same shader code. To test for identical code generation use the effects' keys
75 computed by the GrBackendEffectFactory.
77 bool isEqual(const GrEffect& other) const {
78 if (&this->getFactory() != &other.getFactory()) {
81 bool result = this->onIsEqual(other);
84 this->assertEquality(other);
90 /** Human-meaningful string to identify this effect; may be embedded
91 in generated shader code. */
92 const char* name() const;
94 int numTransforms() const { return fCoordTransforms.count(); }
96 /** Returns the coordinate transformation at index. index must be valid according to
98 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
100 int numTextures() const { return fTextureAccesses.count(); }
102 /** Returns the access pattern for the texture at index. index must be valid according to
104 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
106 /** Shortcut for textureAccess(index).texture(); */
107 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
109 /** Will this effect read the destination pixel value? */
110 bool willReadDstColor() const { return fWillReadDstColor; }
112 /** Will this effect read the fragment position? */
113 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
115 /** Will this effect emit custom vertex shader code?
116 (To set this value the effect must inherit from GrVertexEffect.) */
117 bool hasVertexCode() const { return fHasVertexCode; }
119 int numVertexAttribs() const {
120 SkASSERT(0 == fVertexAttribTypes.count() || fHasVertexCode);
121 return fVertexAttribTypes.count();
124 GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
126 static const int kMaxVertexAttribs = 2;
128 /** Useful for effects that want to insert a texture matrix that is implied by the texture
130 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
131 SkASSERT(NULL != texture);
133 mat.setIDiv(texture->width(), texture->height());
137 void* operator new(size_t size);
138 void operator delete(void* target);
140 void* operator new(size_t size, void* placement) {
141 return ::operator new(size, placement);
143 void operator delete(void* target, void* placement) {
144 ::operator delete(target, placement);
149 * Subclasses call this from their constructor to register coordinate transformations. The
150 * effect subclass manages the lifetime of the transformations (this function only stores a
151 * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the
152 * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll
153 * have 2. This must only be called from the constructor because GrEffects are immutable.
155 void addCoordTransform(const GrCoordTransform* coordTransform);
158 * Subclasses call this from their constructor to register GrTextureAccesses. The effect
159 * subclass manages the lifetime of the accesses (this function only stores a pointer). The
160 * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be
161 * called from the constructor because GrEffects are immutable.
163 void addTextureAccess(const GrTextureAccess* textureAccess);
166 : fWillReadDstColor(false)
167 , fWillReadFragmentPosition(false)
168 , fWillUseInputColor(true)
169 , fHasVertexCode(false) {}
172 * Helper for down-casting to a GrEffect subclass
174 template <typename T> static const T& CastEffect(const GrEffect& effect) {
175 return *static_cast<const T*>(&effect);
179 * If the effect subclass will read the destination pixel value then it must call this function
180 * from its constructor. Otherwise, when its generated backend-specific effect class attempts
181 * to generate code that reads the destination pixel it will fail.
183 void setWillReadDstColor() { fWillReadDstColor = true; }
186 * If the effect will generate a backend-specific effect that will read the fragment position
187 * in the FS then it must call this method from its constructor. Otherwise, the request to
188 * access the fragment position will be denied.
190 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
193 * If the effect will generate a result that does not depend on the input color value then it must
194 * call this function from its constructor. Otherwise, when its generated backend-specific code
195 * might fail during variable binding due to unused variables.
197 void setWillNotUseInputColor() { fWillUseInputColor = false; }
200 SkDEBUGCODE(void assertEquality(const GrEffect& other) const;)
202 /** Subclass implements this to support isEqual(). It will only be called if it is known that
203 the two effects are of the same subclass (i.e. they return the same object from
205 virtual bool onIsEqual(const GrEffect& other) const = 0;
207 friend class GrVertexEffect; // to set fHasVertexCode and build fVertexAttribTypes.
209 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
210 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
211 SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
212 bool fWillReadDstColor;
213 bool fWillReadFragmentPosition;
214 bool fWillUseInputColor;
217 typedef SkRefCnt INHERITED;
221 * This creates an effect outside of the effect memory pool. The effect's destructor will be called
222 * at global destruction time. NAME will be the name of the created GrEffect.
224 #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \
225 static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \
226 static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \
227 static SkAutoTDestroy<GrEffect> NAME##_ad(NAME);