blob: b7a6d17af9527ed0a11c3d5d58376a6cfdc62702 [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"
bsalomon95740982014-09-04 13:12:37 -070013#include "GrProgramElement.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;
bsalomon95740982014-09-04 13:12:37 -070020
twiz@google.coma5e65ec2012-08-02 15:15:16 +000021
bsalomon@google.com50db75c2013-01-11 13:54:30 +000022/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
23 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000024 Subclasses must have a function that produces a human-readable name:
25 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000026 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000027
bsalomon97b9ab72014-07-08 06:52:35 -070028 Dynamically allocated GrEffects are managed by a per-thread memory pool. The ref count of an
29 effect must reach 0 before the thread terminates and the pool is destroyed. To create a static
30 effect use the macro GR_CREATE_STATIC_EFFECT declared below.
bsalomon@google.com289efe02012-05-21 20:57:59 +000031 */
bsalomon95740982014-09-04 13:12:37 -070032class GrEffect : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000033public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000034 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000035
bsalomon@google.coma469c282012-10-24 18:28:34 +000036 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000037
bsalomon@google.com371e1052013-01-11 21:08:55 +000038 /**
bsalomon@google.com371e1052013-01-11 21:08:55 +000039 * This function is used to perform optimizations. When called the color and validFlags params
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000040 * indicate whether the input components to this effect in the FS will have known values.
41 * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to
42 * indicate known values of its output. A component of the color param only has meaning if the
43 * corresponding bit in validFlags is set.
bsalomon@google.com371e1052013-01-11 21:08:55 +000044 */
45 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000046
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000047 /** Will this effect read the source color value? */
48 bool willUseInputColor() const { return fWillUseInputColor; }
49
bsalomon@google.com422e81a2012-10-25 14:11:03 +000050 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
51 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000052 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000053 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
54 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000055
56 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000057 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000058 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000059 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
60 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000061 }
62 ...
63 };
64 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +000065 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000066
bsalomon@google.com68b58c92013-01-17 16:50:08 +000067 /** Returns true if this and other effect conservatively draw identically. It can only return
68 true when the two effects are of the same subclass (i.e. they return the same object from
69 from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000070
bsalomon@google.com68b58c92013-01-17 16:50:08 +000071 A return value of true from isEqual() should not be used to test whether the effects would
bsalomon63e99f72014-07-21 08:03:14 -070072 generate the same shader code. To test for identical code generation use the effects' keys
73 computed by the GrBackendEffectFactory.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000074 */
bsalomon97b9ab72014-07-08 06:52:35 -070075 bool isEqual(const GrEffect& other) const {
76 if (&this->getFactory() != &other.getFactory()) {
77 return false;
78 }
79 bool result = this->onIsEqual(other);
80#ifdef SK_DEBUG
81 if (result) {
82 this->assertEquality(other);
83 }
84#endif
85 return result;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000086 }
tomhudson@google.com168e6342012-04-18 17:49:20 +000087
twiz@google.coma5e65ec2012-08-02 15:15:16 +000088 /** Human-meaningful string to identify this effect; may be embedded
89 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000090 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +000091
bsalomon@google.com77af6802013-10-02 13:04:56 +000092 int numTransforms() const { return fCoordTransforms.count(); }
93
94 /** Returns the coordinate transformation at index. index must be valid according to
95 numTransforms(). */
96 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
97
bsalomon@google.com50db75c2013-01-11 13:54:30 +000098 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000099
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000100 /** Returns the access pattern for the texture at index. index must be valid according to
101 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000102 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000103
104 /** Shortcut for textureAccess(index).texture(); */
105 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000106
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000107 /** Will this effect read the destination pixel value? */
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000108 bool willReadDstColor() const { return fWillReadDstColor; }
109
110 /** Will this effect read the fragment position? */
111 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000112
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000113 /** Will this effect emit custom vertex shader code?
joshualitt30ba4362014-08-21 20:18:45 -0700114 (To set this value the effect must inherit from GrEffect.) */
kkinnunenec56e452014-08-25 22:21:16 -0700115 bool requiresVertexShader() const { return fRequiresVertexShader; }
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000116
117 int numVertexAttribs() const {
kkinnunenec56e452014-08-25 22:21:16 -0700118 SkASSERT(0 == fVertexAttribTypes.count() || fRequiresVertexShader);
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000119 return fVertexAttribTypes.count();
120 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000121
122 GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
123
124 static const int kMaxVertexAttribs = 2;
125
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000126 void* operator new(size_t size);
127 void operator delete(void* target);
128
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000129 void* operator new(size_t size, void* placement) {
130 return ::operator new(size, placement);
131 }
132 void operator delete(void* target, void* placement) {
133 ::operator delete(target, placement);
134 }
135
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000136protected:
137 /**
bsalomon@google.com77af6802013-10-02 13:04:56 +0000138 * Subclasses call this from their constructor to register coordinate transformations. The
139 * effect subclass manages the lifetime of the transformations (this function only stores a
140 * pointer). The GrCoordTransform is typically a member field of the GrEffect subclass. When the
141 * matrix has perspective, the transformed coordinates will have 3 components. Otherwise they'll
142 * have 2. This must only be called from the constructor because GrEffects are immutable.
143 */
144 void addCoordTransform(const GrCoordTransform* coordTransform);
145
146 /**
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000147 * Subclasses call this from their constructor to register GrTextureAccesses. The effect
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000148 * subclass manages the lifetime of the accesses (this function only stores a pointer). The
bsalomon@google.com77af6802013-10-02 13:04:56 +0000149 * GrTextureAccess is typically a member field of the GrEffect subclass. This must only be
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000150 * called from the constructor because GrEffects are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000151 */
152 void addTextureAccess(const GrTextureAccess* textureAccess);
153
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000154 GrEffect()
155 : fWillReadDstColor(false)
156 , fWillReadFragmentPosition(false)
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000157 , fWillUseInputColor(true)
kkinnunenec56e452014-08-25 22:21:16 -0700158 , fRequiresVertexShader(false) {}
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000159
bsalomon0139ae32014-07-08 10:30:37 -0700160 /**
161 * Helper for down-casting to a GrEffect subclass
bsalomon@google.com6340a412013-01-22 19:55:59 +0000162 */
bsalomon0139ae32014-07-08 10:30:37 -0700163 template <typename T> static const T& CastEffect(const GrEffect& effect) {
164 return *static_cast<const T*>(&effect);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000165 }
166
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000167 /**
168 * If the effect subclass will read the destination pixel value then it must call this function
169 * from its constructor. Otherwise, when its generated backend-specific effect class attempts
170 * to generate code that reads the destination pixel it will fail.
171 */
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000172 void setWillReadDstColor() { fWillReadDstColor = true; }
173
174 /**
175 * If the effect will generate a backend-specific effect that will read the fragment position
176 * in the FS then it must call this method from its constructor. Otherwise, the request to
177 * access the fragment position will be denied.
178 */
179 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000180
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000181 /**
182 * If the effect will generate a result that does not depend on the input color value then it must
183 * call this function from its constructor. Otherwise, when its generated backend-specific code
184 * might fail during variable binding due to unused variables.
185 */
186 void setWillNotUseInputColor() { fWillUseInputColor = false; }
187
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000188private:
bsalomon@google.com77af6802013-10-02 13:04:56 +0000189 SkDEBUGCODE(void assertEquality(const GrEffect& other) const;)
190
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000191 /** Subclass implements this to support isEqual(). It will only be called if it is known that
bsalomon@google.com6340a412013-01-22 19:55:59 +0000192 the two effects are of the same subclass (i.e. they return the same object from
193 getFactory()).*/
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000194 virtual bool onIsEqual(const GrEffect& other) const = 0;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000195
kkinnunenec56e452014-08-25 22:21:16 -0700196 friend class GrVertexEffect; // to set fRequiresVertexShader and build fVertexAttribTypes.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000197
bsalomon@google.com77af6802013-10-02 13:04:56 +0000198 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000199 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
200 SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000201 bool fWillReadDstColor;
202 bool fWillReadFragmentPosition;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000203 bool fWillUseInputColor;
kkinnunenec56e452014-08-25 22:21:16 -0700204 bool fRequiresVertexShader;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000205
bsalomon95740982014-09-04 13:12:37 -0700206 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000207};
208
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000209/**
210 * This creates an effect outside of the effect memory pool. The effect's destructor will be called
bsalomon97b9ab72014-07-08 06:52:35 -0700211 * at global destruction time. NAME will be the name of the created GrEffect.
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000212 */
213#define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \
bsalomon97b9ab72014-07-08 06:52:35 -0700214static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \
215static GrEffect* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \
216static SkAutoTDestroy<GrEffect> NAME##_ad(NAME);
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000217
tomhudson@google.com168e6342012-04-18 17:49:20 +0000218#endif