blob: b12a34662adcd89384358813de7f917bfb7dec9d [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
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrRefCnt.h"
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000012#include "GrNoncopyable.h"
bsalomon@google.com6f261be2012-10-24 19:07:10 +000013#include "GrEffectUnitTest.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000014#include "GrTexture.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000015#include "GrTextureAccess.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;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000019class SkString;
20
bsalomon@google.com50db75c2013-01-11 13:54:30 +000021/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
22 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000023 Subclasses must have a function that produces a human-readable name:
24 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000025 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com289efe02012-05-21 20:57:59 +000026 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000027class GrEffect : public GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000028public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000029 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000030
bsalomon@google.com50db75c2013-01-11 13:54:30 +000031 GrEffect() {};
bsalomon@google.coma469c282012-10-24 18:28:34 +000032 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000033
bsalomon@google.com50db75c2013-01-11 13:54:30 +000034 /** If given an input texture that is/is not opaque, is this effect guaranteed to produce an
35 opaque output? */
tomhudson@google.com168e6342012-04-18 17:49:20 +000036 virtual bool isOpaque(bool inputTextureIsOpaque) const;
37
bsalomon@google.com422e81a2012-10-25 14:11:03 +000038 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
39 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000040 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000041 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
42 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000043
44 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000045 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000046 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000047 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
48 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000049 }
50 ...
51 };
52 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +000053 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000054
bsalomon@google.com6f261be2012-10-24 19:07:10 +000055 /** Returns true if the other effect will generate identical output.
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000056 Must only be called if the two are already known to be of the
57 same type (i.e. they return the same value from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000058
59 Equality is not the same thing as equivalence.
60 To test for equivalence (that they will generate the same
61 shader code, but may have different uniforms), check equality
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000062 of the EffectKey produced by the GrBackendEffectFactory:
63 a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000064
65 The default implementation of this function returns true iff
66 the two stages have the same return value for numTextures() and
bsalomon@google.com422e81a2012-10-25 14:11:03 +000067 for texture() over all valid indices.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000068 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000069 virtual bool isEqual(const GrEffect&) const;
tomhudson@google.com168e6342012-04-18 17:49:20 +000070
twiz@google.coma5e65ec2012-08-02 15:15:16 +000071 /** Human-meaningful string to identify this effect; may be embedded
72 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000073 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +000074
bsalomon@google.com50db75c2013-01-11 13:54:30 +000075 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000076
bsalomon@google.com6d003d12012-09-11 15:45:20 +000077 /** Returns the access pattern for the texture at index. index must be valid according to
78 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +000079 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000080
81 /** Shortcut for textureAccess(index).texture(); */
82 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000083
bsalomon@google.com34cccde2013-01-04 18:34:30 +000084 /** Useful for effects that want to insert a texture matrix that is implied by the texture
85 dimensions */
86 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
87 GrAssert(NULL != texture);
88 SkMatrix mat;
89 mat.setIDiv(texture->width(), texture->height());
90 return mat;
91 }
92
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000093 void* operator new(size_t size);
94 void operator delete(void* target);
95
bsalomon@google.com50db75c2013-01-11 13:54:30 +000096protected:
97 /**
98 * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
99 * manages the lifetime of the accesses (this function only stores a pointer). This must only be
100 * called from the constructor because GrEffects are supposed to be immutable.
101 */
102 void addTextureAccess(const GrTextureAccess* textureAccess);
103
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000104private:
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000105 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000106 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000107};
108
109#endif