blob: 5fca4b23446ddb267671b3e40c6581f2f3cd227a [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.com371e1052013-01-11 21:08:55 +000013#include "GrNoncopyable.h"
14#include "GrRefCnt.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000015#include "GrTexture.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000016#include "GrTextureAccess.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000017
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000018class GrBackendEffectFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000019class GrContext;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000020class SkString;
21
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.com289efe02012-05-21 20:57:59 +000027 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000028class GrEffect : public GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000029public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000030 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000031
bsalomon@google.com50db75c2013-01-11 13:54:30 +000032 GrEffect() {};
bsalomon@google.coma469c282012-10-24 18:28:34 +000033 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000034
bsalomon@google.com371e1052013-01-11 21:08:55 +000035 /**
36 * Flags for getConstantColorComponents. They are defined so that the bit order reflects the
37 * GrColor shift order.
38 */
39 enum ValidComponentFlags {
40 kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8),
41 kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8),
42 kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8),
43 kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8),
44
45 kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag |
46 kB_ValidComponentFlag | kA_ValidComponentFlag)
47 };
48
49 /**
50 * This function is used to perform optimizations. When called the color and validFlags params
51 * indicate whether the input components to this effect in the FS will have known values. The
52 * function updates both params to indicate known values of its output. A component of the color
53 * param only has meaning if the corresponding bit in validFlags is set.
54 */
55 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000056
bsalomon@google.com422e81a2012-10-25 14:11:03 +000057 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
58 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000059 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000060 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
61 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000062
63 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000064 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000065 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000066 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
67 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000068 }
69 ...
70 };
71 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +000072 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000073
bsalomon@google.com6f261be2012-10-24 19:07:10 +000074 /** Returns true if the other effect will generate identical output.
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000075 Must only be called if the two are already known to be of the
76 same type (i.e. they return the same value from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000077
78 Equality is not the same thing as equivalence.
79 To test for equivalence (that they will generate the same
80 shader code, but may have different uniforms), check equality
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000081 of the EffectKey produced by the GrBackendEffectFactory:
82 a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000083
84 The default implementation of this function returns true iff
85 the two stages have the same return value for numTextures() and
bsalomon@google.com422e81a2012-10-25 14:11:03 +000086 for texture() over all valid indices.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000087 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000088 virtual bool isEqual(const GrEffect&) const;
tomhudson@google.com168e6342012-04-18 17:49:20 +000089
twiz@google.coma5e65ec2012-08-02 15:15:16 +000090 /** Human-meaningful string to identify this effect; may be embedded
91 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000092 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +000093
bsalomon@google.com50db75c2013-01-11 13:54:30 +000094 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000095
bsalomon@google.com6d003d12012-09-11 15:45:20 +000096 /** Returns the access pattern for the texture at index. index must be valid according to
97 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +000098 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000099
100 /** Shortcut for textureAccess(index).texture(); */
101 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000102
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000103 /** Useful for effects that want to insert a texture matrix that is implied by the texture
104 dimensions */
105 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
106 GrAssert(NULL != texture);
107 SkMatrix mat;
108 mat.setIDiv(texture->width(), texture->height());
109 return mat;
110 }
111
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000112 void* operator new(size_t size);
113 void operator delete(void* target);
114
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000115protected:
116 /**
117 * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
118 * manages the lifetime of the accesses (this function only stores a pointer). This must only be
119 * called from the constructor because GrEffects are supposed to be immutable.
120 */
121 void addTextureAccess(const GrTextureAccess* textureAccess);
122
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000123private:
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000124 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000126};
127
128#endif