blob: 4eb8f2a74dd0798a8e510108cc1d27a35e9845d2 [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"
13#include "GrProgramStageFactory.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
29 objects when they're stored by a GrGLProgramStage.)
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.comae4f96a2012-05-18 19:54:48 +000036 typedef GrProgramStageFactory::StageKey StageKey;
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.comae4f96a2012-05-18 19:54:48 +000045 /** This object, besides creating back-end-specific helper
46 objects, is used for run-time-type-identification. The factory should be
47 an instance of templated class, GrTProgramStageFactory. It is templated
bsalomon@google.coma469c282012-10-24 18:28:34 +000048 on the subclass of GrEffect. The subclass must have a nested type
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000049 (or typedef) named GLProgramStage which will be the subclass of
50 GrGLProgramStage created by the factory.
51
52 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000053 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000054 ...
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055 virtual const GrProgramStageFactory& getFactory() const
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000056 SK_OVERRIDE {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000057 return GrTProgramStageFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000058 }
59 ...
60 };
61 */
62 virtual const GrProgramStageFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000063
bsalomon@google.com6f261be2012-10-24 19:07:10 +000064 /** Returns true if the other effect will generate identical output.
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000065 Must only be called if the two are already known to be of the
66 same type (i.e. they return the same value from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +000067
68 Equality is not the same thing as equivalence.
69 To test for equivalence (that they will generate the same
70 shader code, but may have different uniforms), check equality
71 of the stageKey produced by the GrProgramStageFactory:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000072 a.getFactory().glStageKey(a) == b.getFactory().glStageKey(b).
73
74 The default implementation of this function returns true iff
75 the two stages have the same return value for numTextures() and
76 for texture() over all valid indicse.
77 */
bsalomon@google.coma469c282012-10-24 18:28:34 +000078 virtual bool isEqual(const GrEffect&) const;
tomhudson@google.com168e6342012-04-18 17:49:20 +000079
twiz@google.coma5e65ec2012-08-02 15:15:16 +000080 /** Human-meaningful string to identify this effect; may be embedded
81 in generated shader code. */
bsalomon@google.com289efe02012-05-21 20:57:59 +000082 const char* name() const { return this->getFactory().name(); }
83
bsalomon@google.come6e62d12012-10-04 14:38:48 +000084 int numTextures() const { return fNumTextures; }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000085
bsalomon@google.com6d003d12012-09-11 15:45:20 +000086 /** Returns the access pattern for the texture at index. index must be valid according to
87 numTextures(). */
88 virtual const GrTextureAccess& textureAccess(int index) const;
89
90 /** Shortcut for textureAccess(index).texture(); */
91 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000092
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000093 void* operator new(size_t size);
94 void operator delete(void* target);
95
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000096private:
bsalomon@google.come6e62d12012-10-04 14:38:48 +000097 int fNumTextures;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000098 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +000099};
100
101#endif