blob: 2dbead1c224aebe8bdb196289e9d468587b2c60c [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
joshualittb0a8a372014-09-23 09:50:21 -07008#ifndef GrProcessor_DEFINED
9#define GrProcessor_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrBackendProcessorFactory.h"
bsalomon@google.com371e1052013-01-11 21:08:55 +000012#include "GrColor.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrProcessorUnitTest.h"
bsalomon95740982014-09-04 13:12:37 -070014#include "GrProgramElement.h"
joshualitt249af152014-09-15 11:41:13 -070015#include "GrShaderVar.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000016#include "GrTextureAccess.h"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000017#include "GrTypesPriv.h"
joshualitt249af152014-09-15 11:41:13 -070018#include "SkString.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000019
joshualittb0a8a372014-09-23 09:50:21 -070020class GrBackendProcessorFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000021class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000022class GrCoordTransform;
bsalomon95740982014-09-04 13:12:37 -070023
bsalomon@google.com50db75c2013-01-11 13:54:30 +000024/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
25 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000026 Subclasses must have a function that produces a human-readable name:
27 static const char* Name();
joshualittb0a8a372014-09-23 09:50:21 -070028 GrProcessor objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000029
joshualittb0a8a372014-09-23 09:50:21 -070030 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
bsalomon97b9ab72014-07-08 06:52:35 -070031 effect must reach 0 before the thread terminates and the pool is destroyed. To create a static
32 effect use the macro GR_CREATE_STATIC_EFFECT declared below.
bsalomon@google.com289efe02012-05-21 20:57:59 +000033 */
joshualittb0a8a372014-09-23 09:50:21 -070034class GrProcessor : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000035public:
joshualittb0a8a372014-09-23 09:50:21 -070036 SK_DECLARE_INST_COUNT(GrProcessor)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000037
joshualittb0a8a372014-09-23 09:50:21 -070038 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000039
egdaniel1a8ecdf2014-10-03 06:24:12 -070040 struct InvariantOutput{
41 GrColor fColor;
42 uint32_t fValidFlags;
43 bool fIsSingleComponent;
44
45 bool isOpaque() const {
46 return ((fValidFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(fColor));
47 }
48
49 bool isSolidWhite() const {
50 return (fValidFlags == kRGBA_GrColorComponentFlags && 0xFFFFFFFF == fColor);
51 }
52
53 /**
54 * If isSingleComponent is true, then the flag values for r, g, b, and a must all be the
55 * same. If the flags are all set then all color components must be equal.
56 */
57 SkDEBUGCODE(void validate() const;)
58
59 private:
60 SkDEBUGCODE(bool colorComponentsAllEqual() const;)
61
62 /**
63 * If alpha is valid, check that any valid R,G,B values are <= A
64 */
65 SkDEBUGCODE(bool validPreMulColor() const;)
66 };
67
bsalomon@google.com371e1052013-01-11 21:08:55 +000068 /**
egdaniel1a8ecdf2014-10-03 06:24:12 -070069 * This function is used to perform optimizations. When called the invarientOuput param
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000070 * indicate whether the input components to this effect in the FS will have known values.
egdaniel1a8ecdf2014-10-03 06:24:12 -070071 * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
72 * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
73 * inout to indicate known values of its output. A component of the color member only has
74 * meaning if the corresponding bit in validFlags is set.
bsalomon@google.com371e1052013-01-11 21:08:55 +000075 */
egdaniel1a8ecdf2014-10-03 06:24:12 -070076 void computeInvariantOutput(InvariantOutput* inout) const {
77 this->onComputeInvariantOutput(inout);
78#ifdef SK_DEBUG
79 inout->validate();
80#endif
81 }
tomhudson@google.com168e6342012-04-18 17:49:20 +000082
bsalomon@google.com422e81a2012-10-25 14:11:03 +000083 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
84 identification. The factory should be an instance of templated class,
joshualittb0a8a372014-09-23 09:50:21 -070085 GrTBackendEffectFactory. It is templated on the subclass of GrProcessor. The subclass must
86 have a nested type (or typedef) named GLProcessor which will be the subclass of
87 GrGLProcessor created by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000088
89 Example:
joshualittb0a8a372014-09-23 09:50:21 -070090 class MyCustomEffect : public GrProcessor {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000091 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +000092 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
93 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000094 }
95 ...
96 };
97 */
joshualittb0a8a372014-09-23 09:50:21 -070098 virtual const GrBackendProcessorFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000099
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000100 /** Returns true if this and other effect conservatively draw identically. It can only return
101 true when the two effects are of the same subclass (i.e. they return the same object from
102 from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000103
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000104 A return value of true from isEqual() should not be used to test whether the effects would
bsalomon63e99f72014-07-21 08:03:14 -0700105 generate the same shader code. To test for identical code generation use the effects' keys
106 computed by the GrBackendEffectFactory.
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000107 */
joshualittb0a8a372014-09-23 09:50:21 -0700108 bool isEqual(const GrProcessor& other) const {
bsalomon97b9ab72014-07-08 06:52:35 -0700109 if (&this->getFactory() != &other.getFactory()) {
110 return false;
111 }
112 bool result = this->onIsEqual(other);
113#ifdef SK_DEBUG
114 if (result) {
115 this->assertEquality(other);
116 }
117#endif
118 return result;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000119 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000120
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000121 /** Human-meaningful string to identify this effect; may be embedded
122 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000123 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +0000124
bsalomon@google.com77af6802013-10-02 13:04:56 +0000125 int numTransforms() const { return fCoordTransforms.count(); }
126
127 /** Returns the coordinate transformation at index. index must be valid according to
128 numTransforms(). */
129 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
130
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000131 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000132
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000133 /** Returns the access pattern for the texture at index. index must be valid according to
134 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000135 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000136
137 /** Shortcut for textureAccess(index).texture(); */
138 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000139
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000140 /** Will this effect read the fragment position? */
141 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000142
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000143 void* operator new(size_t size);
144 void operator delete(void* target);
145
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000146 void* operator new(size_t size, void* placement) {
147 return ::operator new(size, placement);
148 }
149 void operator delete(void* target, void* placement) {
150 ::operator delete(target, placement);
151 }
152
joshualitt49586be2014-09-16 08:21:41 -0700153 /**
joshualittb0a8a372014-09-23 09:50:21 -0700154 * Helper for down-casting to a GrProcessor subclass
joshualitt49586be2014-09-16 08:21:41 -0700155 */
156 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
157
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000158protected:
159 /**
bsalomon@google.com77af6802013-10-02 13:04:56 +0000160 * Subclasses call this from their constructor to register coordinate transformations. The
161 * effect subclass manages the lifetime of the transformations (this function only stores a
joshualittb0a8a372014-09-23 09:50:21 -0700162 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. When
163 * the matrix has perspective, the transformed coordinates will have 3 components. Otherwise
164 * they'll have 2. This must only be called from the constructor because GrProcessors are
165 * immutable.
bsalomon@google.com77af6802013-10-02 13:04:56 +0000166 */
167 void addCoordTransform(const GrCoordTransform* coordTransform);
168
169 /**
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000170 * Subclasses call this from their constructor to register GrTextureAccesses. The effect
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000171 * subclass manages the lifetime of the accesses (this function only stores a pointer). The
joshualittb0a8a372014-09-23 09:50:21 -0700172 * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be
173 * called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000174 */
175 void addTextureAccess(const GrTextureAccess* textureAccess);
176
joshualittb0a8a372014-09-23 09:50:21 -0700177 GrProcessor()
178 : fWillReadFragmentPosition(false) {}
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000179
180 /**
181 * If the effect will generate a backend-specific effect that will read the fragment position
182 * in the FS then it must call this method from its constructor. Otherwise, the request to
183 * access the fragment position will be denied.
184 */
185 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000186
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000187private:
joshualittb0a8a372014-09-23 09:50:21 -0700188 SkDEBUGCODE(void assertEquality(const GrProcessor& other) const;)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000189
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000190 /** Subclass implements this to support isEqual(). It will only be called if it is known that
bsalomon@google.com6340a412013-01-22 19:55:59 +0000191 the two effects are of the same subclass (i.e. they return the same object from
192 getFactory()).*/
joshualittb0a8a372014-09-23 09:50:21 -0700193 virtual bool onIsEqual(const GrProcessor& other) const = 0;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000194
egdaniel1a8ecdf2014-10-03 06:24:12 -0700195 /**
196 * Subclass implements this to support getConstantColorComponents(...).
197 */
198 virtual void onComputeInvariantOutput(InvariantOutput* inout) const = 0;
joshualitt249af152014-09-15 11:41:13 -0700199 friend class GrGeometryProcessor; // to set fRequiresVertexShader and build fVertexAttribTypes.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000200
bsalomon@google.com77af6802013-10-02 13:04:56 +0000201 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000202 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000203 bool fWillReadFragmentPosition;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000204
bsalomon95740982014-09-04 13:12:37 -0700205 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000206};
207
joshualittb0a8a372014-09-23 09:50:21 -0700208class GrFragmentProcessor : public GrProcessor {
209public:
210 GrFragmentProcessor()
211 : INHERITED()
212 , fWillReadDstColor(false)
213 , fWillUseInputColor(true) {}
214
215 virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0;
216
217 /** Will this effect read the destination pixel value? */
218 bool willReadDstColor() const { return fWillReadDstColor; }
219
220 /** Will this effect read the source color value? */
221 bool willUseInputColor() const { return fWillUseInputColor; }
222
223protected:
224 /**
225 * If the effect subclass will read the destination pixel value then it must call this function
226 * from its constructor. Otherwise, when its generated backend-specific effect class attempts
227 * to generate code that reads the destination pixel it will fail.
228 */
229 void setWillReadDstColor() { fWillReadDstColor = true; }
230
231 /**
232 * If the effect will generate a result that does not depend on the input color value then it
233 * must call this function from its constructor. Otherwise, when its generated backend-specific
234 * code might fail during variable binding due to unused variables.
235 */
236 void setWillNotUseInputColor() { fWillUseInputColor = false; }
237
238private:
239 bool fWillReadDstColor;
240 bool fWillUseInputColor;
241
242 typedef GrProcessor INHERITED;
243};
244
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000245/**
246 * This creates an effect outside of the effect memory pool. The effect's destructor will be called
joshualittb0a8a372014-09-23 09:50:21 -0700247 * at global destruction time. NAME will be the name of the created GrProcessor.
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000248 */
joshualittb0a8a372014-09-23 09:50:21 -0700249#define GR_CREATE_STATIC_FRAGMENT_PROCESSOR(NAME, EFFECT_CLASS, ARGS) \
bsalomon97b9ab72014-07-08 06:52:35 -0700250static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \
joshualittb0a8a372014-09-23 09:50:21 -0700251static GrFragmentProcessor* \
252NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \
253static SkAutoTDestroy<GrFragmentProcessor> NAME##_ad(NAME);
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000254
tomhudson@google.com168e6342012-04-18 17:49:20 +0000255#endif