blob: 28778fd10acb714a8617c66e2ac1f72db3ea3074 [file] [log] [blame]
bsalomon6251d172014-10-15 10:50:36 -07001/*
2 * Copyright 2014 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
8#ifndef GrFragmentProcessor_DEFINED
9#define GrFragmentProcessor_DEFINED
10
11#include "GrProcessor.h"
12
13class GrCoordTransform;
14
15/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
16 produce an output color. They may reference textures and uniforms. They may use
17 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
18 to the fragment being processed.
19 */
20class GrFragmentProcessor : public GrProcessor {
21public:
22 GrFragmentProcessor()
23 : INHERITED()
24 , fWillReadDstColor(false)
25 , fWillUseInputColor(true) {}
26
27 virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0;
28
29 int numTransforms() const { return fCoordTransforms.count(); }
30
31 /** Returns the coordinate transformation at index. index must be valid according to
32 numTransforms(). */
33 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
34
35 /** Will this prceossor read the destination pixel value? */
36 bool willReadDstColor() const { return fWillReadDstColor; }
37
38 /** Will this prceossor read the source color value? */
39 bool willUseInputColor() const { return fWillUseInputColor; }
40
41 /** Returns true if this and other prceossor conservatively draw identically. It can only return
42 true when the two prceossor are of the same subclass (i.e. they return the same object from
43 from getFactory()).
44
45 A return value of true from isEqual() should not be used to test whether the prceossor would
46 generate the same shader code. To test for identical code generation use the prceossor' keys
47 computed by the GrBackendProcessorFactory. */
48 bool isEqual(const GrFragmentProcessor& other) const {
49 if (&this->getFactory() != &other.getFactory()) {
50 return false;
51 }
52 bool result = this->onIsEqual(other);
53#ifdef SK_DEBUG
54 if (result) {
55 this->assertTexturesEqual(other);
56 }
57#endif
58 return result;
59 }
60
61protected:
62 /**
63 * Fragment Processor subclasses call this from their constructor to register coordinate
64 * transformations. The processor subclass manages the lifetime of the transformations (this
65 * function only stores a pointer). The GrCoordTransform is typically a member field of the
66 * GrProcessor subclass. When the matrix has perspective, the transformed coordinates will have
67 * 3 components. Otherwise they'll have 2. This must only be called from the constructor because
68 * GrProcessors are immutable.
69 */
70 void addCoordTransform(const GrCoordTransform*);
71
72 /**
73 * If the prceossor subclass will read the destination pixel value then it must call this function
74 * from its constructor. Otherwise, when its generated backend-specific prceossor class attempts
75 * to generate code that reads the destination pixel it will fail.
76 */
77 void setWillReadDstColor() { fWillReadDstColor = true; }
78
79 /**
80 * If the prceossor will generate a result that does not depend on the input color value then it
81 * must call this function from its constructor. Otherwise, when its generated backend-specific
82 * code might fail during variable binding due to unused variables.
83 */
84 void setWillNotUseInputColor() { fWillUseInputColor = false; }
85
86private:
87 /** Subclass implements this to support isEqual(). It will only be called if it is known that
88 the two prceossor are of the same subclass (i.e. they return the same object from
89 getFactory()).*/
90 virtual bool onIsEqual(const GrFragmentProcessor& other) const = 0;
91
92 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
93 bool fWillReadDstColor;
94 bool fWillUseInputColor;
95
96 typedef GrProcessor INHERITED;
97};
98
99/**
100 * This creates an effect outside of the effect memory pool. The effect's destructor will be called
101 * at global destruction time. NAME will be the name of the created GrProcessor.
102 */
103#define GR_CREATE_STATIC_FRAGMENT_PROCESSOR(NAME, FP_CLASS, ARGS) \
104static SkAlignedSStorage<sizeof(FP_CLASS)> g_##NAME##_Storage; \
105static GrFragmentProcessor* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), FP_CLASS, ARGS); \
106static SkAutoTDestroy<GrFragmentProcessor> NAME##_ad(NAME);
107
108#endif