blob: 18525e7c050a0305d7b17bfb09ef077ce5deae0a [file] [log] [blame]
egdaniel378092f2014-12-03 10:40:13 -08001/*
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 GrXferProcessor_DEFINED
9#define GrXferProcessor_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTypes.h"
Brian Salomon096b0912019-08-14 16:56:13 -040012#include "src/gpu/GrBlend.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrNonAtomicRef.h"
14#include "src/gpu/GrProcessor.h"
15#include "src/gpu/GrProcessorAnalysis.h"
egdaniel378092f2014-12-03 10:40:13 -080016
egdanielfa4cc8b2015-11-13 08:34:52 -080017class GrGLSLXferProcessor;
Hal Canaryce78bad2017-05-04 14:15:40 -040018class GrProcessorSet;
19class GrShaderCaps;
egdaniel95131432014-12-09 11:15:43 -080020
egdaniel378092f2014-12-03 10:40:13 -080021/**
cdalton9954bc32015-04-29 14:17:00 -070022 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
23 * required after a pixel has been written, before it can be safely read again.
24 */
25enum GrXferBarrierType {
bsalomoncb02b382015-08-12 11:14:50 -070026 kNone_GrXferBarrierType = 0, //<! No barrier is required
27 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
28 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
cdalton9954bc32015-04-29 14:17:00 -070029};
bsalomoncb02b382015-08-12 11:14:50 -070030/** Should be able to treat kNone as false in boolean expressions */
31GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
cdalton9954bc32015-04-29 14:17:00 -070032
33/**
egdaniel378092f2014-12-03 10:40:13 -080034 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
cdaltonedbb31f2015-06-08 12:14:44 -070035 * color, and for applying any coverage. It does this by emitting fragment shader code and
36 * controlling the fixed-function blend state. When dual-source blending is available, it may also
37 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
38 *
39 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
40 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
41 * provides shader code that blends the src and dst colors, and the base class applies coverage.
42 *
43 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
44 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
egdaniel378092f2014-12-03 10:40:13 -080045 *
46 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
47 * GrXPFactory once we have finalized the state of our draw.
48 */
Brian Salomond61c9d92017-04-10 10:54:25 -040049class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> {
egdaniel95131432014-12-09 11:15:43 -080050public:
51 /**
bsalomon6a44c6a2015-05-26 09:49:05 -070052 * A texture that contains the dst pixel values and an integer coord offset from device space
53 * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a
54 * GrXferProcessor for blending in the fragment shader.
55 */
Robert Phillipsbb581ce2017-05-29 15:05:15 -040056 class DstProxy {
bsalomon6a44c6a2015-05-26 09:49:05 -070057 public:
Robert Phillipsbb581ce2017-05-29 15:05:15 -040058 DstProxy() { fOffset.set(0, 0); }
bsalomon6a44c6a2015-05-26 09:49:05 -070059
Robert Phillipsbb581ce2017-05-29 15:05:15 -040060 DstProxy(const DstProxy& other) {
bsalomon6a44c6a2015-05-26 09:49:05 -070061 *this = other;
62 }
63
Robert Phillipsbb581ce2017-05-29 15:05:15 -040064 DstProxy(sk_sp<GrTextureProxy> proxy, const SkIPoint& offset)
65 : fProxy(std::move(proxy)) {
66 if (fProxy) {
67 fOffset = offset;
68 } else {
69 fOffset.set(0, 0);
70 }
71 }
bsalomon6a44c6a2015-05-26 09:49:05 -070072
Robert Phillipsbb581ce2017-05-29 15:05:15 -040073 DstProxy& operator=(const DstProxy& other) {
74 fProxy = other.fProxy;
bsalomon6a44c6a2015-05-26 09:49:05 -070075 fOffset = other.fOffset;
76 return *this;
77 }
78
Robert Phillipsbb581ce2017-05-29 15:05:15 -040079 bool operator==(const DstProxy& that) const {
80 return fProxy == that.fProxy && fOffset == that.fOffset;
Brian Salomon54d212e2017-03-21 14:22:38 -040081 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -040082 bool operator!=(const DstProxy& that) const { return !(*this == that); }
Brian Salomon54d212e2017-03-21 14:22:38 -040083
bsalomon6a44c6a2015-05-26 09:49:05 -070084 const SkIPoint& offset() const { return fOffset; }
85
86 void setOffset(const SkIPoint& offset) { fOffset = offset; }
87 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
88
Robert Phillipsbb581ce2017-05-29 15:05:15 -040089 GrTextureProxy* proxy() const { return fProxy.get(); }
Robert Phillips80bff5b2019-08-20 16:56:18 -040090 sk_sp<GrTextureProxy> refProxy() const { return fProxy; }
bsalomon6a44c6a2015-05-26 09:49:05 -070091
Robert Phillipsbb581ce2017-05-29 15:05:15 -040092 void setProxy(sk_sp<GrTextureProxy> proxy) {
93 fProxy = std::move(proxy);
94 if (!fProxy) {
Brian Salomon54d212e2017-03-21 14:22:38 -040095 fOffset = {0, 0};
96 }
bsalomon6a44c6a2015-05-26 09:49:05 -070097 }
98
99 private:
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400100 sk_sp<GrTextureProxy> fProxy;
101 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -0700102 };
103
104 /**
egdaniel57d3b032015-11-13 11:57:27 -0800105 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -0800106 * specific subclass's key.
Brian Salomon18dfa982017-04-03 16:57:43 -0400107 */
108 void getGLSLProcessorKey(const GrShaderCaps&,
109 GrProcessorKeyBuilder*,
110 const GrSurfaceOrigin* originIfDstTexture) const;
egdanielc2304142014-12-11 13:15:13 -0800111
112 /** Returns a new instance of the appropriate *GL* implementation class
113 for the given GrXferProcessor; caller is responsible for deleting
114 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800115 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800116
117 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400118 * Returns the barrier type, if any, that this XP will require. Note that the possibility
119 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
120 * considered by subclass overrides of this function.
cdalton9954bc32015-04-29 14:17:00 -0700121 */
Brian Salomon18dfa982017-04-03 16:57:43 -0400122 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
123 return kNone_GrXferBarrierType;
124 }
cdalton9954bc32015-04-29 14:17:00 -0700125
egdaniel95131432014-12-09 11:15:43 -0800126 struct BlendInfo {
bsalomonf7cc8772015-05-11 11:21:14 -0700127 SkDEBUGCODE(SkString dump() const;)
128
Chris Daltonbbb3f642019-07-24 12:25:08 -0400129 GrBlendEquation fEquation = kAdd_GrBlendEquation;
130 GrBlendCoeff fSrcBlend = kOne_GrBlendCoeff;
131 GrBlendCoeff fDstBlend = kZero_GrBlendCoeff;
132 SkPMColor4f fBlendConstant = SK_PMColor4fTRANSPARENT;
133 bool fWriteColor = true;
egdaniel95131432014-12-09 11:15:43 -0800134 };
135
Chris Daltonbbb3f642019-07-24 12:25:08 -0400136 inline BlendInfo getBlendInfo() const {
137 BlendInfo blendInfo;
138 if (!this->willReadDstColor()) {
139 this->onGetBlendInfo(&blendInfo);
140 } else if (this->dstReadUsesMixedSamples()) {
141 blendInfo.fDstBlend = kIS2A_GrBlendCoeff;
142 }
143 return blendInfo;
144 }
egdaniel95131432014-12-09 11:15:43 -0800145
egdaniel95131432014-12-09 11:15:43 -0800146 bool willReadDstColor() const { return fWillReadDstColor; }
147
bsalomon50785a32015-02-06 07:02:37 -0800148 /**
cdalton86ae0a92015-06-08 15:11:04 -0700149 * If we are performing a dst read, returns whether the base class will use mixed samples to
150 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
151 * for antialiasing and this returns false.
152 */
153 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
154
155 /**
egdanielc2304142014-12-11 13:15:13 -0800156 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
157 * source blending.
158 */
cdaltonedbb31f2015-06-08 12:14:44 -0700159 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800160
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400161 bool isLCD() const { return fIsLCD; }
162
egdanielc2304142014-12-11 13:15:13 -0800163 /** Returns true if this and other processor conservatively draw identically. It can only return
164 true when the two processor are of the same subclass (i.e. they return the same object from
165 from getFactory()).
166
167 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800168 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
169 */
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400170
egdanielc2304142014-12-11 13:15:13 -0800171 bool isEqual(const GrXferProcessor& that) const {
172 if (this->classID() != that.classID()) {
173 return false;
174 }
bsalomon50785a32015-02-06 07:02:37 -0800175 if (this->fWillReadDstColor != that.fWillReadDstColor) {
176 return false;
177 }
cdalton86ae0a92015-06-08 15:11:04 -0700178 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
179 return false;
180 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400181 if (fIsLCD != that.fIsLCD) {
182 return false;
183 }
egdanielc2304142014-12-11 13:15:13 -0800184 return this->onIsEqual(that);
185 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500186
egdaniel95131432014-12-09 11:15:43 -0800187protected:
Ethan Nicholasabff9562017-10-09 10:54:08 -0400188 GrXferProcessor(ClassID classID);
189 GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples,
190 GrProcessorAnalysisCoverage);
egdaniel95131432014-12-09 11:15:43 -0800191
egdaniel378092f2014-12-03 10:40:13 -0800192private:
bsalomon50785a32015-02-06 07:02:37 -0800193 /**
194 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
195 * processor's GL backend implementation.
196 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500197 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800198
cdaltonf4f2b442015-04-23 09:40:23 -0700199 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700200 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700201 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700202 * will not be called.
203 */
204 virtual bool onHasSecondaryOutput() const { return false; }
205
206 /**
207 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700208 * subclass. When using dst reads, the base class controls the fixed-function blend state and
209 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700210 */
211 virtual void onGetBlendInfo(BlendInfo*) const {}
212
egdanielc2304142014-12-11 13:15:13 -0800213 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800214
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400215 bool fWillReadDstColor;
216 bool fDstReadUsesMixedSamples;
217 bool fIsLCD;
egdaniel95131432014-12-09 11:15:43 -0800218
Ethan Nicholasabff9562017-10-09 10:54:08 -0400219 typedef GrProcessor INHERITED;
egdaniel378092f2014-12-03 10:40:13 -0800220};
221
222/**
223 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
224 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
225 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
226 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
227 * the draw.
228 *
229 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
230 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
231 * blend with the destination color.
Brian Salomona1633922017-01-09 11:46:10 -0500232 *
233 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
234 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
235 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
egdaniel378092f2014-12-03 10:40:13 -0800236 */
Brian Salomona1633922017-01-09 11:46:10 -0500237
238// In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
239// GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
240// since these objects have no need for destructors. However, GCC and clang throw a warning when a
241// class has virtual functions and a non-virtual destructor. We suppress that warning here and
242// for the subclasses.
Chris Dalton1ef80942017-12-04 12:01:30 -0700243#if defined(__GNUC__)
Brian Salomona1633922017-01-09 11:46:10 -0500244#pragma GCC diagnostic push
245#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
246#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700247#if defined(__clang__)
248#pragma clang diagnostic push
249#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
250#endif
Brian Salomona1633922017-01-09 11:46:10 -0500251class GrXPFactory {
egdaniel378092f2014-12-03 10:40:13 -0800252public:
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400253 typedef GrXferProcessor::DstProxy DstProxy;
Brian Salomon5be6c952017-01-20 19:06:29 +0000254
Brian Salomon31853842017-03-28 16:32:05 -0400255 enum class AnalysisProperties : unsigned {
256 kNone = 0x0,
Brian Salomon4fc77402017-03-30 16:48:26 -0400257 /**
258 * The fragment shader will require the destination color.
259 */
Brian Salomon31853842017-03-28 16:32:05 -0400260 kReadsDstInShader = 0x1,
Brian Salomon4fc77402017-03-30 16:48:26 -0400261 /**
262 * The op may apply coverage as alpha and still blend correctly.
263 */
Brian Osman605c6d52019-03-15 12:10:35 -0400264 kCompatibleWithCoverageAsAlpha = 0x2,
Brian Salomon4fc77402017-03-30 16:48:26 -0400265 /**
266 * The color input to the GrXferProcessor will be ignored.
267 */
268 kIgnoresInputColor = 0x4,
269 /**
Brian Salomon4fc77402017-03-30 16:48:26 -0400270 * The destination color will be provided to the fragment processor using a texture. This is
271 * additional information about the implementation of kReadsDstInShader.
272 */
273 kRequiresDstTexture = 0x10,
274 /**
Chris Dalton945ee652019-01-23 09:10:36 -0700275 * If set, each pixel can only be touched once during a draw (e.g., because we have a dst
276 * texture or because we need an xfer barrier).
Brian Salomon4fc77402017-03-30 16:48:26 -0400277 */
Chris Dalton945ee652019-01-23 09:10:36 -0700278 kRequiresNonOverlappingDraws = 0x20,
Brian Salomon31853842017-03-28 16:32:05 -0400279 };
280 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
egdaniel080e6732014-12-22 07:35:52 -0800281
Brian Salomond61c9d92017-04-10 10:54:25 -0400282 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
283 const GrProcessorAnalysisColor&,
284 GrProcessorAnalysisCoverage,
285 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400286 const GrCaps& caps,
287 GrClampType);
Brian Salomona076d872017-04-04 15:17:03 -0400288
Brian Salomon31853842017-03-28 16:32:05 -0400289 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
Brian Salomona811b122017-03-30 08:21:32 -0400290 const GrProcessorAnalysisColor&,
291 const GrProcessorAnalysisCoverage&,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400292 const GrCaps&,
293 GrClampType);
Brian Salomon54d212e2017-03-21 14:22:38 -0400294
egdaniel915187b2014-12-05 12:58:28 -0800295protected:
Brian Salomona1633922017-01-09 11:46:10 -0500296 constexpr GrXPFactory() {}
egdaniel915187b2014-12-05 12:58:28 -0800297
egdaniel378092f2014-12-03 10:40:13 -0800298private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400299 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
300 GrProcessorAnalysisCoverage,
301 bool hasMixedSamples,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400302 const GrCaps&,
303 GrClampType) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800304
bsalomon50785a32015-02-06 07:02:37 -0800305 /**
Brian Salomon31853842017-03-28 16:32:05 -0400306 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
307 * inferred by the base class based on kReadsDstInShader and the caps.
bsalomon50785a32015-02-06 07:02:37 -0800308 */
Brian Salomona811b122017-03-30 08:21:32 -0400309 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
310 const GrProcessorAnalysisCoverage&,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400311 const GrCaps&,
312 GrClampType) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800313};
Chris Dalton1ef80942017-12-04 12:01:30 -0700314#if defined(__GNUC__)
Brian Salomona1633922017-01-09 11:46:10 -0500315#pragma GCC diagnostic pop
316#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700317#if defined(__clang__)
318#pragma clang diagnostic pop
319#endif
egdaniel378092f2014-12-03 10:40:13 -0800320
Brian Salomon31853842017-03-28 16:32:05 -0400321GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties);
egdaniel378092f2014-12-03 10:40:13 -0800322
Brian Salomon31853842017-03-28 16:32:05 -0400323#endif