blob: 3f308d7e75048df485d03aac3e9c148db80fd77f [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;
joshualitteb2a6762014-12-04 11:35:33 -080014class GrGLCaps;
15class GrGLFragmentProcessor;
16class GrProcessorKeyBuilder;
bsalomon6251d172014-10-15 10:50:36 -070017
18/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
19 produce an output color. They may reference textures and uniforms. They may use
20 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
21 to the fragment being processed.
22 */
23class GrFragmentProcessor : public GrProcessor {
24public:
25 GrFragmentProcessor()
26 : INHERITED()
27 , fWillReadDstColor(false)
28 , fWillUseInputColor(true) {}
29
joshualitteb2a6762014-12-04 11:35:33 -080030 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
31 virtual void getGLProcessorKey(const GrGLCaps& caps,
32 GrProcessorKeyBuilder* b) const = 0;
33
34 /** Returns a new instance of the appropriate *GL* implementation class
35 for the given GrFragmentProcessor; caller is responsible for deleting
36 the object. */
37 virtual GrGLFragmentProcessor* createGLInstance() const = 0;
38
39 /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
40 in generated shader code. */
41 virtual const char* name() const = 0;
bsalomon6251d172014-10-15 10:50:36 -070042
43 int numTransforms() const { return fCoordTransforms.count(); }
44
45 /** Returns the coordinate transformation at index. index must be valid according to
46 numTransforms(). */
47 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
48
49 /** Will this prceossor read the destination pixel value? */
50 bool willReadDstColor() const { return fWillReadDstColor; }
51
52 /** Will this prceossor read the source color value? */
53 bool willUseInputColor() const { return fWillUseInputColor; }
54
joshualitteb2a6762014-12-04 11:35:33 -080055 /** Returns true if this and other processor conservatively draw identically. It can only return
56 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070057 from getFactory()).
58
joshualitteb2a6762014-12-04 11:35:33 -080059 A return value of true from isEqual() should not be used to test whether the processor would
60 generate the same shader code. To test for identical code generation use getGLProcessorKey*/
bsalomon420d7e92014-10-16 09:18:09 -070061 bool isEqual(const GrFragmentProcessor& that) const {
joshualitteb2a6762014-12-04 11:35:33 -080062 if (this->classID() != that.classID() ||
bsalomon420d7e92014-10-16 09:18:09 -070063 !this->hasSameTransforms(that) ||
64 !this->hasSameTextureAccesses(that)) {
bsalomon6251d172014-10-15 10:50:36 -070065 return false;
66 }
bsalomon420d7e92014-10-16 09:18:09 -070067 return this->onIsEqual(that);
bsalomon6251d172014-10-15 10:50:36 -070068 }
69
70protected:
71 /**
72 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -070073 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
74 * in their FS code. The matrix expresses a transformation from local space. For a given
75 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
76 *
77 * When the transformation has perspective, the transformed coordinates will have
78 * 3 components. Otherwise they'll have 2.
79 *
80 * This must only be called from the constructor because GrProcessors are immutable. The
81 * processor subclass manages the lifetime of the transformations (this function only stores a
82 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
83 *
84 * A processor subclass that has multiple methods of construction should always add its coord
85 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
86 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -070087 */
88 void addCoordTransform(const GrCoordTransform*);
89
90 /**
bsalomon98b33eb2014-10-15 11:05:26 -070091 * If the prceossor subclass will read the destination pixel value then it must call this
92 * function from its constructor. Otherwise, when its generated backend-specific prceossor class
93 * attempts to generate code that reads the destination pixel it will fail.
bsalomon6251d172014-10-15 10:50:36 -070094 */
95 void setWillReadDstColor() { fWillReadDstColor = true; }
96
97 /**
98 * If the prceossor will generate a result that does not depend on the input color value then it
99 * must call this function from its constructor. Otherwise, when its generated backend-specific
100 * code might fail during variable binding due to unused variables.
101 */
102 void setWillNotUseInputColor() { fWillUseInputColor = false; }
103
104private:
bsalomonde258cd2014-10-15 19:06:21 -0700105 /**
106 * Subclass implements this to support isEqual(). It will only be called if it is known that
107 * the two processors are of the same subclass (i.e. they return the same object from
108 * getFactory()). The processor subclass should not compare its coord transforms as that will
109 * be performed automatically in the non-virtual isEqual().
110 */
111 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
112
113 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700114
115 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
116 bool fWillReadDstColor;
117 bool fWillUseInputColor;
118
119 typedef GrProcessor INHERITED;
120};
121
bsalomon6251d172014-10-15 10:50:36 -0700122#endif