blob: 99a26eed96babbf4c1884c76201e575dbf07031d [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;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000020class GrEffect;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000021class SkString;
22
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000023/**
24 * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue
25 * new draw operations separately from ownership within a deferred drawing queue. When the
26 * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled
27 * in service of later draws. However, the deferred draw queue may still own direct references to
28 * the underlying GrEffect.
29 */
30class GrEffectRef : public SkRefCnt {
31public:
32 SK_DECLARE_INST_COUNT(GrEffectRef);
33
34 GrEffect* get() { return fEffect; }
35 const GrEffect* get() const { return fEffect; }
36
37 void* operator new(size_t size);
38 void operator delete(void* target);
39
40private:
bsalomon@google.com64287c52013-01-16 15:25:55 +000041 friend class GrEffect; // to construct these
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000042
43 explicit GrEffectRef(GrEffect* effect);
44
45 virtual ~GrEffectRef();
46
47 GrEffect* fEffect;
48
49 typedef SkRefCnt INHERITED;
50};
51
bsalomon@google.com50db75c2013-01-11 13:54:30 +000052/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
53 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000054 Subclasses must have a function that produces a human-readable name:
55 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000056 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000057
58 GrEffect subclass objects should be created by factory functions that return GrEffectRef.
59 There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
60 member function of a GrEffect subclass.
bsalomon@google.com289efe02012-05-21 20:57:59 +000061 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000062class GrEffect : public GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000063public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000064 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000065
bsalomon@google.coma469c282012-10-24 18:28:34 +000066 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000067
bsalomon@google.com371e1052013-01-11 21:08:55 +000068 /**
69 * Flags for getConstantColorComponents. They are defined so that the bit order reflects the
70 * GrColor shift order.
71 */
72 enum ValidComponentFlags {
73 kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8),
74 kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8),
75 kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8),
76 kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8),
77
78 kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag |
79 kB_ValidComponentFlag | kA_ValidComponentFlag)
80 };
81
82 /**
83 * This function is used to perform optimizations. When called the color and validFlags params
84 * indicate whether the input components to this effect in the FS will have known values. The
85 * function updates both params to indicate known values of its output. A component of the color
86 * param only has meaning if the corresponding bit in validFlags is set.
87 */
88 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000089
bsalomon@google.com422e81a2012-10-25 14:11:03 +000090 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
91 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000092 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000093 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
94 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000095
96 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000097 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000098 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000099 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
100 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000101 }
102 ...
103 };
104 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000105 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +0000106
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000107 /** Returns true if the other effect will generate identical output.
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000108 Must only be called if the two are already known to be of the
109 same type (i.e. they return the same value from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000110
111 Equality is not the same thing as equivalence.
112 To test for equivalence (that they will generate the same
113 shader code, but may have different uniforms), check equality
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000114 of the EffectKey produced by the GrBackendEffectFactory:
115 a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000116
117 The default implementation of this function returns true iff
118 the two stages have the same return value for numTextures() and
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000119 for texture() over all valid indices.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000120 */
bsalomon@google.coma469c282012-10-24 18:28:34 +0000121 virtual bool isEqual(const GrEffect&) const;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000122
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000123 /** Human-meaningful string to identify this effect; may be embedded
124 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000125 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +0000126
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000127 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000128
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000129 /** Returns the access pattern for the texture at index. index must be valid according to
130 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000131 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000132
133 /** Shortcut for textureAccess(index).texture(); */
134 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000135
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000136 /** Useful for effects that want to insert a texture matrix that is implied by the texture
137 dimensions */
138 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
139 GrAssert(NULL != texture);
140 SkMatrix mat;
141 mat.setIDiv(texture->width(), texture->height());
142 return mat;
143 }
144
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000145 void* operator new(size_t size);
146 void operator delete(void* target);
147
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000148protected:
149 /**
150 * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
151 * manages the lifetime of the accesses (this function only stores a pointer). This must only be
152 * called from the constructor because GrEffects are supposed to be immutable.
153 */
154 void addTextureAccess(const GrTextureAccess* textureAccess);
155
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000156 GrEffect() : fEffectPtr(NULL) {};
157
158 /** This should be called by GrEffect subclass factories */
159 static GrEffectRef* CreateEffectPtr(GrEffect* effect) {
160 if (NULL == effect->fEffectPtr) {
161 effect->fEffectPtr = SkNEW_ARGS(GrEffectRef, (effect));
162 } else {
163 effect->fEffectPtr->ref();
164 GrCrash("This function should only be called once per effect currently.");
165 }
166 return effect->fEffectPtr;
167 }
168
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000169private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000170 void effectPtrDestroyed() {
171 fEffectPtr = NULL;
172 }
173
bsalomon@google.com64287c52013-01-16 15:25:55 +0000174 friend class GrEffectRef; // to call GrEffectRef destroyed
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000175
176 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
177 GrEffectRef* fEffectPtr;
178
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000179 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000180};
181
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000182inline GrEffectRef::GrEffectRef(GrEffect* effect) {
183 GrAssert(NULL != effect);
184 effect->ref();
185 fEffect = effect;
186}
187
tomhudson@google.com168e6342012-04-18 17:49:20 +0000188#endif