blob: d7209ff5631d7abbc00404c0883ce400c6d9edfb [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.com396e61f2012-10-25 19:00:29 +000013#include "GrBackendEffectFactory.h"
bsalomon@google.com6f261be2012-10-24 19:07:10 +000014#include "GrEffectUnitTest.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000015#include "GrTextureAccess.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016
tomhudson@google.com168e6342012-04-18 17:49:20 +000017class GrContext;
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000018class GrTexture;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000019class SkString;
20
tomhudson@google.com168e6342012-04-18 17:49:20 +000021/** Provides custom vertex shader, fragment shader, uniform data for a
rmistry@google.comfbfcd562012-08-23 18:09:54 +000022 particular stage of the 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.coma469c282012-10-24 18:28:34 +000025 GrEffect objects *must* be immutable: after being constructed,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000026 their fields may not change. (Immutability isn't actually required
27 until they've been used in a draw call, but supporting that would require
28 setters and getters that could fail, copy-on-write, or deep copying of these
bsalomon@google.comd698f772012-10-25 13:22:00 +000029 objects when they're stored by a GrGLEffect.)
bsalomon@google.com289efe02012-05-21 20:57:59 +000030 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000031class GrEffect : public GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000032
33public:
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.com46fba0d2012-10-25 21:42:05 +000036 typedef GrBackendEffectFactory::EffectKey EffectKey;
tomhudson@google.com168e6342012-04-18 17:49:20 +000037
bsalomon@google.coma469c282012-10-24 18:28:34 +000038 explicit GrEffect(int numTextures);
39 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000040
41 /** If given an input texture that is/is not opaque, is this
bsalomon@google.com021fc732012-10-25 12:47:42 +000042 effect guaranteed to produce an opaque output? */
tomhudson@google.com168e6342012-04-18 17:49:20 +000043 virtual bool isOpaque(bool inputTextureIsOpaque) const;
44
bsalomon@google.com422e81a2012-10-25 14:11:03 +000045 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
46 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000047 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000048 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
49 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000050
51 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000052 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000053 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000054 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
55 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000056 }
57 ...
58 };
59 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +000060 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000061
bsalomon@google.com6f261be2012-10-24 19:07:10 +000062 /** Returns true if the other effect will generate identical output.
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000063 Must only be called if the two are already known to be of the
64 same type (i.e. they return the same value from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000065
66 Equality is not the same thing as equivalence.
67 To test for equivalence (that they will generate the same
68 shader code, but may have different uniforms), check equality
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000069 of the EffectKey produced by the GrBackendEffectFactory:
70 a.getFactory().glEffectKey(a) == b.getFactory().glEffectKey(b).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000071
72 The default implementation of this function returns true iff
73 the two stages have the same return value for numTextures() and
bsalomon@google.com422e81a2012-10-25 14:11:03 +000074 for texture() over all valid indices.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000075 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000076 virtual bool isEqual(const GrEffect&) const;
tomhudson@google.com168e6342012-04-18 17:49:20 +000077
twiz@google.coma5e65ec2012-08-02 15:15:16 +000078 /** Human-meaningful string to identify this effect; may be embedded
79 in generated shader code. */
bsalomon@google.com289efe02012-05-21 20:57:59 +000080 const char* name() const { return this->getFactory().name(); }
81
bsalomon@google.come6e62d12012-10-04 14:38:48 +000082 int numTextures() const { return fNumTextures; }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000083
bsalomon@google.com6d003d12012-09-11 15:45:20 +000084 /** Returns the access pattern for the texture at index. index must be valid according to
85 numTextures(). */
86 virtual const GrTextureAccess& textureAccess(int index) const;
87
88 /** Shortcut for textureAccess(index).texture(); */
89 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000090
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000091 void* operator new(size_t size);
92 void operator delete(void* target);
93
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000094private:
bsalomon@google.come6e62d12012-10-04 14:38:48 +000095 int fNumTextures;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000096 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +000097};
98
99#endif