blob: 464230717645c9415cde2a52ab6c0bc479eb8372 [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 {
bsalomonde258cd2014-10-15 19:06:21 -070049 if (&this->getFactory() != &other.getFactory() || !this->hasSameTransforms(other)) {
bsalomon6251d172014-10-15 10:50:36 -070050 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
bsalomonde258cd2014-10-15 19:06:21 -070064 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
65 * in their FS code. The matrix expresses a transformation from local space. For a given
66 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
67 *
68 * When the transformation has perspective, the transformed coordinates will have
69 * 3 components. Otherwise they'll have 2.
70 *
71 * This must only be called from the constructor because GrProcessors are immutable. The
72 * processor subclass manages the lifetime of the transformations (this function only stores a
73 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
74 *
75 * A processor subclass that has multiple methods of construction should always add its coord
76 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
77 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -070078 */
79 void addCoordTransform(const GrCoordTransform*);
80
81 /**
bsalomon98b33eb2014-10-15 11:05:26 -070082 * If the prceossor subclass will read the destination pixel value then it must call this
83 * function from its constructor. Otherwise, when its generated backend-specific prceossor class
84 * attempts to generate code that reads the destination pixel it will fail.
bsalomon6251d172014-10-15 10:50:36 -070085 */
86 void setWillReadDstColor() { fWillReadDstColor = true; }
87
88 /**
89 * If the prceossor will generate a result that does not depend on the input color value then it
90 * must call this function from its constructor. Otherwise, when its generated backend-specific
91 * code might fail during variable binding due to unused variables.
92 */
93 void setWillNotUseInputColor() { fWillUseInputColor = false; }
94
95private:
bsalomonde258cd2014-10-15 19:06:21 -070096 /**
97 * Subclass implements this to support isEqual(). It will only be called if it is known that
98 * the two processors are of the same subclass (i.e. they return the same object from
99 * getFactory()). The processor subclass should not compare its coord transforms as that will
100 * be performed automatically in the non-virtual isEqual().
101 */
102 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
103
104 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700105
106 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
107 bool fWillReadDstColor;
108 bool fWillUseInputColor;
109
110 typedef GrProcessor INHERITED;
111};
112
bsalomon6251d172014-10-15 10:50:36 -0700113#endif