blob: 3d42dfdf16ae5c7ada07d09a782ec01f7ea9bda0 [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
cdaltonee0175f2015-06-12 08:21:26 -070011#include "GrBlend.h"
Brian Salomond61c9d92017-04-10 10:54:25 -040012#include "GrNonAtomicRef.h"
egdanielc2304142014-12-11 13:15:13 -080013#include "GrProcessor.h"
Hal Canaryce78bad2017-05-04 14:15:40 -040014#include "GrProcessorAnalysis.h"
egdaniel378092f2014-12-03 10:40:13 -080015#include "GrTypes.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(); }
bsalomon6a44c6a2015-05-26 09:49:05 -070090
Robert Phillipsbb581ce2017-05-29 15:05:15 -040091 void setProxy(sk_sp<GrTextureProxy> proxy) {
92 fProxy = std::move(proxy);
93 if (!fProxy) {
Brian Salomon54d212e2017-03-21 14:22:38 -040094 fOffset = {0, 0};
95 }
bsalomon6a44c6a2015-05-26 09:49:05 -070096 }
97
Robert Phillipsbb581ce2017-05-29 15:05:15 -040098 bool instantiate(GrResourceProvider* resourceProvider) {
99 return SkToBool(fProxy->instantiate(resourceProvider));
100 }
101
bsalomon6a44c6a2015-05-26 09:49:05 -0700102 private:
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400103 sk_sp<GrTextureProxy> fProxy;
104 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -0700105 };
106
107 /**
egdaniel57d3b032015-11-13 11:57:27 -0800108 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -0800109 * specific subclass's key.
Brian Salomon18dfa982017-04-03 16:57:43 -0400110 */
111 void getGLSLProcessorKey(const GrShaderCaps&,
112 GrProcessorKeyBuilder*,
113 const GrSurfaceOrigin* originIfDstTexture) const;
egdanielc2304142014-12-11 13:15:13 -0800114
115 /** Returns a new instance of the appropriate *GL* implementation class
116 for the given GrXferProcessor; caller is responsible for deleting
117 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800118 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800119
120 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400121 * Returns the barrier type, if any, that this XP will require. Note that the possibility
122 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
123 * considered by subclass overrides of this function.
cdalton9954bc32015-04-29 14:17:00 -0700124 */
Brian Salomon18dfa982017-04-03 16:57:43 -0400125 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
126 return kNone_GrXferBarrierType;
127 }
cdalton9954bc32015-04-29 14:17:00 -0700128
egdaniel95131432014-12-09 11:15:43 -0800129 struct BlendInfo {
cdaltonf4f2b442015-04-23 09:40:23 -0700130 void reset() {
cdalton8917d622015-05-06 13:40:21 -0700131 fEquation = kAdd_GrBlendEquation;
cdaltonf4f2b442015-04-23 09:40:23 -0700132 fSrcBlend = kOne_GrBlendCoeff;
133 fDstBlend = kZero_GrBlendCoeff;
Brian Osman422f95b2018-11-05 16:49:04 -0500134 fBlendConstant = SK_PMColor4fTRANSPARENT;
cdaltonf4f2b442015-04-23 09:40:23 -0700135 fWriteColor = true;
136 }
egdaniel080e6732014-12-22 07:35:52 -0800137
bsalomonf7cc8772015-05-11 11:21:14 -0700138 SkDEBUGCODE(SkString dump() const;)
139
cdalton8917d622015-05-06 13:40:21 -0700140 GrBlendEquation fEquation;
141 GrBlendCoeff fSrcBlend;
142 GrBlendCoeff fDstBlend;
Brian Osman422f95b2018-11-05 16:49:04 -0500143 SkPMColor4f fBlendConstant;
cdalton8917d622015-05-06 13:40:21 -0700144 bool fWriteColor;
egdaniel95131432014-12-09 11:15:43 -0800145 };
146
cdaltonedbb31f2015-06-08 12:14:44 -0700147 void getBlendInfo(BlendInfo* blendInfo) const;
egdaniel95131432014-12-09 11:15:43 -0800148
egdaniel95131432014-12-09 11:15:43 -0800149 bool willReadDstColor() const { return fWillReadDstColor; }
150
bsalomon50785a32015-02-06 07:02:37 -0800151 /**
cdalton86ae0a92015-06-08 15:11:04 -0700152 * If we are performing a dst read, returns whether the base class will use mixed samples to
153 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
154 * for antialiasing and this returns false.
155 */
156 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
157
158 /**
egdanielc2304142014-12-11 13:15:13 -0800159 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
160 * source blending.
161 */
cdaltonedbb31f2015-06-08 12:14:44 -0700162 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800163
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400164 bool isLCD() const { return fIsLCD; }
165
egdanielc2304142014-12-11 13:15:13 -0800166 /** Returns true if this and other processor conservatively draw identically. It can only return
167 true when the two processor are of the same subclass (i.e. they return the same object from
168 from getFactory()).
169
170 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800171 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
172 */
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400173
egdanielc2304142014-12-11 13:15:13 -0800174 bool isEqual(const GrXferProcessor& that) const {
175 if (this->classID() != that.classID()) {
176 return false;
177 }
bsalomon50785a32015-02-06 07:02:37 -0800178 if (this->fWillReadDstColor != that.fWillReadDstColor) {
179 return false;
180 }
cdalton86ae0a92015-06-08 15:11:04 -0700181 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
182 return false;
183 }
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400184 if (fIsLCD != that.fIsLCD) {
185 return false;
186 }
egdanielc2304142014-12-11 13:15:13 -0800187 return this->onIsEqual(that);
188 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500189
egdaniel95131432014-12-09 11:15:43 -0800190protected:
Ethan Nicholasabff9562017-10-09 10:54:08 -0400191 GrXferProcessor(ClassID classID);
192 GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples,
193 GrProcessorAnalysisCoverage);
egdaniel95131432014-12-09 11:15:43 -0800194
egdaniel378092f2014-12-03 10:40:13 -0800195private:
bsalomon50785a32015-02-06 07:02:37 -0800196 /**
197 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
198 * processor's GL backend implementation.
199 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500200 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800201
cdaltonf4f2b442015-04-23 09:40:23 -0700202 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700203 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700204 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700205 * will not be called.
206 */
207 virtual bool onHasSecondaryOutput() const { return false; }
208
209 /**
210 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700211 * subclass. When using dst reads, the base class controls the fixed-function blend state and
212 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700213 */
214 virtual void onGetBlendInfo(BlendInfo*) const {}
215
egdanielc2304142014-12-11 13:15:13 -0800216 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800217
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400218 bool fWillReadDstColor;
219 bool fDstReadUsesMixedSamples;
220 bool fIsLCD;
egdaniel95131432014-12-09 11:15:43 -0800221
Ethan Nicholasabff9562017-10-09 10:54:08 -0400222 typedef GrProcessor INHERITED;
egdaniel378092f2014-12-03 10:40:13 -0800223};
224
225/**
226 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
227 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
228 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
229 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
230 * the draw.
231 *
232 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
233 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
234 * blend with the destination color.
Brian Salomona1633922017-01-09 11:46:10 -0500235 *
236 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
237 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
238 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
egdaniel378092f2014-12-03 10:40:13 -0800239 */
Brian Salomona1633922017-01-09 11:46:10 -0500240
241// In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
242// GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
243// since these objects have no need for destructors. However, GCC and clang throw a warning when a
244// class has virtual functions and a non-virtual destructor. We suppress that warning here and
245// for the subclasses.
Chris Dalton1ef80942017-12-04 12:01:30 -0700246#if defined(__GNUC__)
Brian Salomona1633922017-01-09 11:46:10 -0500247#pragma GCC diagnostic push
248#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
249#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700250#if defined(__clang__)
251#pragma clang diagnostic push
252#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
253#endif
Brian Salomona1633922017-01-09 11:46:10 -0500254class GrXPFactory {
egdaniel378092f2014-12-03 10:40:13 -0800255public:
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400256 typedef GrXferProcessor::DstProxy DstProxy;
Brian Salomon5be6c952017-01-20 19:06:29 +0000257
Brian Salomon31853842017-03-28 16:32:05 -0400258 enum class AnalysisProperties : unsigned {
259 kNone = 0x0,
Brian Salomon4fc77402017-03-30 16:48:26 -0400260 /**
261 * The fragment shader will require the destination color.
262 */
Brian Salomon31853842017-03-28 16:32:05 -0400263 kReadsDstInShader = 0x1,
Brian Salomon4fc77402017-03-30 16:48:26 -0400264 /**
265 * The op may apply coverage as alpha and still blend correctly.
266 */
267 kCompatibleWithAlphaAsCoverage = 0x2,
268 /**
269 * The color input to the GrXferProcessor will be ignored.
270 */
271 kIgnoresInputColor = 0x4,
272 /**
273 * If set overlapping stencil and cover operations can be replaced by a combined stencil
274 * followed by a combined cover.
275 */
276 kCanCombineOverlappedStencilAndCover = 0x8,
277 /**
278 * The destination color will be provided to the fragment processor using a texture. This is
279 * additional information about the implementation of kReadsDstInShader.
280 */
281 kRequiresDstTexture = 0x10,
282 /**
283 * If set overlapping draws may not be combined because a barrier must be inserted between
284 * them.
285 */
286 kRequiresBarrierBetweenOverlappingDraws = 0x20,
Brian Salomon31853842017-03-28 16:32:05 -0400287 };
288 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
egdaniel080e6732014-12-22 07:35:52 -0800289
Brian Salomond61c9d92017-04-10 10:54:25 -0400290 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
291 const GrProcessorAnalysisColor&,
292 GrProcessorAnalysisCoverage,
293 bool hasMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400294 const GrCaps& caps);
Brian Salomona076d872017-04-04 15:17:03 -0400295
Brian Salomon31853842017-03-28 16:32:05 -0400296 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
Brian Salomona811b122017-03-30 08:21:32 -0400297 const GrProcessorAnalysisColor&,
298 const GrProcessorAnalysisCoverage&,
Brian Osman532b3f92018-07-11 10:02:07 -0400299 const GrCaps&);
Brian Salomon54d212e2017-03-21 14:22:38 -0400300
egdaniel915187b2014-12-05 12:58:28 -0800301protected:
Brian Salomona1633922017-01-09 11:46:10 -0500302 constexpr GrXPFactory() {}
egdaniel915187b2014-12-05 12:58:28 -0800303
egdaniel378092f2014-12-03 10:40:13 -0800304private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400305 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
306 GrProcessorAnalysisCoverage,
307 bool hasMixedSamples,
Brian Osman532b3f92018-07-11 10:02:07 -0400308 const GrCaps&) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800309
bsalomon50785a32015-02-06 07:02:37 -0800310 /**
Brian Salomon31853842017-03-28 16:32:05 -0400311 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
312 * inferred by the base class based on kReadsDstInShader and the caps.
bsalomon50785a32015-02-06 07:02:37 -0800313 */
Brian Salomona811b122017-03-30 08:21:32 -0400314 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
315 const GrProcessorAnalysisCoverage&,
Brian Osman532b3f92018-07-11 10:02:07 -0400316 const GrCaps&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800317};
Chris Dalton1ef80942017-12-04 12:01:30 -0700318#if defined(__GNUC__)
Brian Salomona1633922017-01-09 11:46:10 -0500319#pragma GCC diagnostic pop
320#endif
Chris Dalton1ef80942017-12-04 12:01:30 -0700321#if defined(__clang__)
322#pragma clang diagnostic pop
323#endif
egdaniel378092f2014-12-03 10:40:13 -0800324
Brian Salomon31853842017-03-28 16:32:05 -0400325GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties);
egdaniel378092f2014-12-03 10:40:13 -0800326
Brian Salomon31853842017-03-28 16:32:05 -0400327#endif