blob: 978ad3a6a661a5fd41d1d463a0b372996921060c [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
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.com8ea78d82012-10-24 20:11:30 +00008#ifndef GrEffect_DEFINED
9#define GrEffect_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
bsalomon@google.com371e1052013-01-11 21:08:55 +000011#include "GrColor.h"
bsalomon@google.com6f261be2012-10-24 19:07:10 +000012#include "GrEffectUnitTest.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000013#include "GrTexture.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000014#include "GrTextureAccess.h"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000015#include "GrTypesPriv.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000017class GrBackendEffectFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000018class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000019class GrCoordTransform;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000020class GrEffect;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000021class GrVertexEffect;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000022class SkString;
23
bsalomon@google.com50db75c2013-01-11 13:54:30 +000024/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
25 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000026 Subclasses must have a function that produces a human-readable name:
27 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000028 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000029
bsalomon97b9ab72014-07-08 06:52:35 -070030 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.
bsalomon@google.com289efe02012-05-21 20:57:59 +000033 */
bsalomon97b9ab72014-07-08 06:52:35 -070034class GrEffect : public SkRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000035public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000036 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000037
bsalomon@google.coma469c282012-10-24 18:28:34 +000038 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000039
bsalomon@google.com371e1052013-01-11 21:08:55 +000040 /**
bsalomon@google.com371e1052013-01-11 21:08:55 +000041 * This function is used to perform optimizations. When called the color and validFlags params
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000042 * 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.
bsalomon@google.com371e1052013-01-11 21:08:55 +000046 */
47 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000048
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000049 /** Will this effect read the source color value? */
50 bool willUseInputColor() const { return fWillUseInputColor; }
51
bsalomon@google.com422e81a2012-10-25 14:11:03 +000052 /** 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,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000054 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000055 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
56 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000057
58 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000059 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000060 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000061 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
62 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000063 }
64 ...
65 };
66 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +000067 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000068
bsalomon@google.com68b58c92013-01-17 16:50:08 +000069 /** 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
71 from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000072
bsalomon@google.com68b58c92013-01-17 16:50:08 +000073 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 EffectKey
75 computed by the GrBackendEffectFactory:
76 effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000077 */
bsalomon97b9ab72014-07-08 06:52:35 -070078 bool isEqual(const GrEffect& other) const {
79 if (&this->getFactory() != &other.getFactory()) {
80 return false;
81 }
82 bool result = this->onIsEqual(other);
83#ifdef SK_DEBUG
84 if (result) {
85 this->assertEquality(other);
86 }
87#endif
88 return result;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000089 }
tomhudson@google.com168e6342012-04-18 17:49:20 +000090
twiz@google.coma5e65ec2012-08-02 15:15:16 +000091 /** Human-meaningful string to identify this effect; may be embedded
92 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000093 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +000094
bsalomon@google.com77af6802013-10-02 13:04:56 +000095 int numTransforms() const { return fCoordTransforms.count(); }
96
97 /** Returns the coordinate transformation at index. index must be valid according to
98 numTransforms(). */
99 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
100
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000101 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000102
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000103 /** Returns the access pattern for the texture at index. index must be valid according to
104 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000105 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000106
107 /** Shortcut for textureAccess(index).texture(); */
108 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000109
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000110 /** Will this effect read the destination pixel value? */
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000111 bool willReadDstColor() const { return fWillReadDstColor; }
112
113 /** Will this effect read the fragment position? */
114 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000115
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000116 /** Will this effect emit custom vertex shader code?
117 (To set this value the effect must inherit from GrVertexEffect.) */
118 bool hasVertexCode() const { return fHasVertexCode; }
119
120 int numVertexAttribs() const {
121 SkASSERT(0 == fVertexAttribTypes.count() || fHasVertexCode);
122 return fVertexAttribTypes.count();
123 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000124
125 GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
126
127 static const int kMaxVertexAttribs = 2;
128
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000129 /** Useful for effects that want to insert a texture matrix that is implied by the texture
130 dimensions */
131 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000132 SkASSERT(NULL != texture);
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000133 SkMatrix mat;
134 mat.setIDiv(texture->width(), texture->height());
135 return mat;
136 }
137
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000138 void* operator new(size_t size);
139 void operator delete(void* target);
140
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000141 void* operator new(size_t size, void* placement) {
142 return ::operator new(size, placement);
143 }
144 void operator delete(void* target, void* placement) {
145 ::operator delete(target, placement);
146 }
147
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000148protected:
149 /**
bsalomon@google.com77af6802013-10-02 13:04:56 +0000150 * Subclasses call this from their constructor to register coordinate transformations. The
151 * effect subclass manages the lifetime of the transformations (this function only stores a
152 * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the
153 * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll
154 * have 2. This must only be called from the constructor because GrEffects are immutable.
155 */
156 void addCoordTransform(const GrCoordTransform* coordTransform);
157
158 /**
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000159 * Subclasses call this from their constructor to register GrTextureAccesses. The effect
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000160 * subclass manages the lifetime of the accesses (this function only stores a pointer). The
bsalomon@google.com77af6802013-10-02 13:04:56 +0000161 * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000162 * called from the constructor because GrEffects are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000163 */
164 void addTextureAccess(const GrTextureAccess* textureAccess);
165
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000166 GrEffect()
167 : fWillReadDstColor(false)
168 , fWillReadFragmentPosition(false)
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000169 , fWillUseInputColor(true)
bsalomon97b9ab72014-07-08 06:52:35 -0700170 , fHasVertexCode(false) {}
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000171
bsalomon@google.com6340a412013-01-22 19:55:59 +0000172 /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for
173 an example factory function. */
bsalomon97b9ab72014-07-08 06:52:35 -0700174 static GrEffect* CreateEffectRef(GrEffect* effect) {
175 return SkRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000176 }
177
bsalomon97b9ab72014-07-08 06:52:35 -0700178 static const GrEffect* CreateEffectRef(const GrEffect* effect) {
bsalomon@google.comca432082013-01-23 19:53:46 +0000179 return CreateEffectRef(const_cast<GrEffect*>(effect));
180 }
181
bsalomon@google.com6340a412013-01-22 19:55:59 +0000182 /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a
183 GrEffectRef. E.g.:
184
185 class EffectSubclass : public GrEffect {
186 public:
187 GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
188 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...)));
189 return CreateEffectRef(effect);
190 }
191 */
bsalomon97b9ab72014-07-08 06:52:35 -0700192 typedef SkAutoTUnref<GrEffect> AutoEffectUnref;
bsalomon@google.com6340a412013-01-22 19:55:59 +0000193
194 /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass
195 */
196 template <typename T>
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000197 static const T& CastEffect(const GrEffect& effectRef) {
198 return *static_cast<const T*>(&effectRef);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000199 }
200
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000201 /**
202 * If the effect subclass will read the destination pixel value then it must call this function
203 * from its constructor. Otherwise, when its generated backend-specific effect class attempts
204 * to generate code that reads the destination pixel it will fail.
205 */
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000206 void setWillReadDstColor() { fWillReadDstColor = true; }
207
208 /**
209 * If the effect will generate a backend-specific effect that will read the fragment position
210 * in the FS then it must call this method from its constructor. Otherwise, the request to
211 * access the fragment position will be denied.
212 */
213 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000214
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000215 /**
216 * If the effect will generate a result that does not depend on the input color value then it must
217 * call this function from its constructor. Otherwise, when its generated backend-specific code
218 * might fail during variable binding due to unused variables.
219 */
220 void setWillNotUseInputColor() { fWillUseInputColor = false; }
221
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000222private:
bsalomon@google.com77af6802013-10-02 13:04:56 +0000223 SkDEBUGCODE(void assertEquality(const GrEffect& other) const;)
224
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000225 /** Subclass implements this to support isEqual(). It will only be called if it is known that
bsalomon@google.com6340a412013-01-22 19:55:59 +0000226 the two effects are of the same subclass (i.e. they return the same object from
227 getFactory()).*/
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000228 virtual bool onIsEqual(const GrEffect& other) const = 0;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000229
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000230 friend class GrVertexEffect; // to set fHasVertexCode and build fVertexAttribTypes.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000231
bsalomon@google.com77af6802013-10-02 13:04:56 +0000232 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000233 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
234 SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000235 bool fWillReadDstColor;
236 bool fWillReadFragmentPosition;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000237 bool fWillUseInputColor;
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000238 bool fHasVertexCode;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000239
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000240 typedef SkRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000241};
242
bsalomon97b9ab72014-07-08 06:52:35 -0700243typedef GrEffect GrEffectRef;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000244
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000245/**
246 * This creates an effect outside of the effect memory pool. The effect's destructor will be called
bsalomon97b9ab72014-07-08 06:52:35 -0700247 * at global destruction time. NAME will be the name of the created GrEffect.
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000248 */
249#define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \
bsalomon97b9ab72014-07-08 06:52:35 -0700250static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \
251static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \
252static SkAutoTDestroy<GrEffect> NAME##_ad(NAME);
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000253
254
tomhudson@google.com168e6342012-04-18 17:49:20 +0000255#endif