blob: bfc6dd71c50f4a43aac44999f903c36bab76f2e5 [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"
egdaniel378092f2014-12-03 10:40:13 -080012#include "GrColor.h"
egdanielc2304142014-12-11 13:15:13 -080013#include "GrProcessor.h"
Brian Salomon5298dc82017-02-22 11:52:03 -050014#include "GrProcessorSet.h"
bsalomon50785a32015-02-06 07:02:37 -080015#include "GrTexture.h"
egdaniel378092f2014-12-03 10:40:13 -080016#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080017
jvanverthe9c0fc62015-04-29 11:18:05 -070018class GrShaderCaps;
egdanielfa4cc8b2015-11-13 08:34:52 -080019class GrGLSLXferProcessor;
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 */
egdanielc2304142014-12-11 13:15:13 -080049class GrXferProcessor : public GrProcessor {
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 */
56 class DstTexture {
57 public:
58 DstTexture() { fOffset.set(0, 0); }
59
60 DstTexture(const DstTexture& other) {
61 *this = other;
62 }
63
64 DstTexture(GrTexture* texture, const SkIPoint& offset)
Brian Salomon54d212e2017-03-21 14:22:38 -040065 : fTexture(SkSafeRef(texture)), fOffset(texture ? offset : SkIPoint{0, 0}) {}
bsalomon6a44c6a2015-05-26 09:49:05 -070066
67 DstTexture& operator=(const DstTexture& other) {
bungeman6bd52842016-10-27 09:30:08 -070068 fTexture = other.fTexture;
bsalomon6a44c6a2015-05-26 09:49:05 -070069 fOffset = other.fOffset;
70 return *this;
71 }
72
Brian Salomon54d212e2017-03-21 14:22:38 -040073 bool operator==(const DstTexture& that) const {
74 return fTexture == that.fTexture && fOffset == that.fOffset;
75 }
76 bool operator!=(const DstTexture& that) const { return !(*this == that); }
77
bsalomon6a44c6a2015-05-26 09:49:05 -070078 const SkIPoint& offset() const { return fOffset; }
79
80 void setOffset(const SkIPoint& offset) { fOffset = offset; }
81 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
82
83 GrTexture* texture() const { return fTexture.get(); }
84
bungeman6bd52842016-10-27 09:30:08 -070085 void setTexture(sk_sp<GrTexture> texture) {
86 fTexture = std::move(texture);
Brian Salomon54d212e2017-03-21 14:22:38 -040087 if (!fTexture) {
88 fOffset = {0, 0};
89 }
bsalomon6a44c6a2015-05-26 09:49:05 -070090 }
91
92 private:
bungeman6bd52842016-10-27 09:30:08 -070093 sk_sp<GrTexture> fTexture;
94 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -070095 };
96
97 /**
egdaniel57d3b032015-11-13 11:57:27 -080098 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -080099 * specific subclass's key.
Brian Salomon18dfa982017-04-03 16:57:43 -0400100 */
101 void getGLSLProcessorKey(const GrShaderCaps&,
102 GrProcessorKeyBuilder*,
103 const GrSurfaceOrigin* originIfDstTexture) const;
egdanielc2304142014-12-11 13:15:13 -0800104
105 /** Returns a new instance of the appropriate *GL* implementation class
106 for the given GrXferProcessor; caller is responsible for deleting
107 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800108 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800109
110 /**
Brian Salomon18dfa982017-04-03 16:57:43 -0400111 * Returns the barrier type, if any, that this XP will require. Note that the possibility
112 * that a kTexture type barrier is required is handled by the GrPipeline and need not be
113 * considered by subclass overrides of this function.
cdalton9954bc32015-04-29 14:17:00 -0700114 */
Brian Salomon18dfa982017-04-03 16:57:43 -0400115 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const {
116 return kNone_GrXferBarrierType;
117 }
cdalton9954bc32015-04-29 14:17:00 -0700118
egdaniel95131432014-12-09 11:15:43 -0800119 struct BlendInfo {
cdaltonf4f2b442015-04-23 09:40:23 -0700120 void reset() {
cdalton8917d622015-05-06 13:40:21 -0700121 fEquation = kAdd_GrBlendEquation;
cdaltonf4f2b442015-04-23 09:40:23 -0700122 fSrcBlend = kOne_GrBlendCoeff;
123 fDstBlend = kZero_GrBlendCoeff;
124 fBlendConstant = 0;
125 fWriteColor = true;
126 }
egdaniel080e6732014-12-22 07:35:52 -0800127
bsalomonf7cc8772015-05-11 11:21:14 -0700128 SkDEBUGCODE(SkString dump() const;)
129
cdalton8917d622015-05-06 13:40:21 -0700130 GrBlendEquation fEquation;
131 GrBlendCoeff fSrcBlend;
132 GrBlendCoeff fDstBlend;
133 GrColor fBlendConstant;
134 bool fWriteColor;
egdaniel95131432014-12-09 11:15:43 -0800135 };
136
cdaltonedbb31f2015-06-08 12:14:44 -0700137 void getBlendInfo(BlendInfo* blendInfo) const;
egdaniel95131432014-12-09 11:15:43 -0800138
egdaniel95131432014-12-09 11:15:43 -0800139 bool willReadDstColor() const { return fWillReadDstColor; }
140
bsalomon50785a32015-02-06 07:02:37 -0800141 /**
cdalton86ae0a92015-06-08 15:11:04 -0700142 * If we are performing a dst read, returns whether the base class will use mixed samples to
143 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
144 * for antialiasing and this returns false.
145 */
146 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
147
148 /**
egdanielc2304142014-12-11 13:15:13 -0800149 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
150 * source blending.
151 */
cdaltonedbb31f2015-06-08 12:14:44 -0700152 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800153
154 /** Returns true if this and other processor conservatively draw identically. It can only return
155 true when the two processor are of the same subclass (i.e. they return the same object from
156 from getFactory()).
157
158 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800159 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
160 */
egdanielc2304142014-12-11 13:15:13 -0800161
162 bool isEqual(const GrXferProcessor& that) const {
163 if (this->classID() != that.classID()) {
164 return false;
165 }
bsalomon50785a32015-02-06 07:02:37 -0800166 if (this->fWillReadDstColor != that.fWillReadDstColor) {
167 return false;
168 }
cdalton86ae0a92015-06-08 15:11:04 -0700169 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
170 return false;
171 }
egdanielc2304142014-12-11 13:15:13 -0800172 return this->onIsEqual(that);
173 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500174
egdaniel95131432014-12-09 11:15:43 -0800175protected:
bsalomon50785a32015-02-06 07:02:37 -0800176 GrXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400177 GrXferProcessor(bool willReadDstColor, bool hasMixedSamples);
egdaniel95131432014-12-09 11:15:43 -0800178
egdaniel378092f2014-12-03 10:40:13 -0800179private:
bsalomon42048002015-08-27 16:43:48 -0700180 void notifyRefCntIsZero() const final {}
181
bsalomon50785a32015-02-06 07:02:37 -0800182 /**
183 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
184 * processor's GL backend implementation.
185 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500186 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800187
cdaltonf4f2b442015-04-23 09:40:23 -0700188 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700189 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700190 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700191 * will not be called.
192 */
193 virtual bool onHasSecondaryOutput() const { return false; }
194
195 /**
196 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700197 * subclass. When using dst reads, the base class controls the fixed-function blend state and
198 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700199 */
200 virtual void onGetBlendInfo(BlendInfo*) const {}
201
egdanielc2304142014-12-11 13:15:13 -0800202 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800203
bsalomon50785a32015-02-06 07:02:37 -0800204 bool fWillReadDstColor;
cdalton86ae0a92015-06-08 15:11:04 -0700205 bool fDstReadUsesMixedSamples;
egdaniel95131432014-12-09 11:15:43 -0800206
egdaniel378092f2014-12-03 10:40:13 -0800207 typedef GrFragmentProcessor INHERITED;
208};
209
210/**
211 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
212 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
213 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
214 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
215 * the draw.
216 *
217 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
218 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
219 * blend with the destination color.
Brian Salomona1633922017-01-09 11:46:10 -0500220 *
221 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
222 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
223 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
egdaniel378092f2014-12-03 10:40:13 -0800224 */
Brian Salomona1633922017-01-09 11:46:10 -0500225
226// In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
227// GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
228// since these objects have no need for destructors. However, GCC and clang throw a warning when a
229// class has virtual functions and a non-virtual destructor. We suppress that warning here and
230// for the subclasses.
231#if defined(__GNUC__) || defined(__clang)
232#pragma GCC diagnostic push
233#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
234#endif
235class GrXPFactory {
egdaniel378092f2014-12-03 10:40:13 -0800236public:
bsalomon6a44c6a2015-05-26 09:49:05 -0700237 typedef GrXferProcessor::DstTexture DstTexture;
Brian Salomon5be6c952017-01-20 19:06:29 +0000238
Brian Salomon31853842017-03-28 16:32:05 -0400239 enum class AnalysisProperties : unsigned {
240 kNone = 0x0,
Brian Salomon4fc77402017-03-30 16:48:26 -0400241 /**
242 * The fragment shader will require the destination color.
243 */
Brian Salomon31853842017-03-28 16:32:05 -0400244 kReadsDstInShader = 0x1,
Brian Salomon4fc77402017-03-30 16:48:26 -0400245 /**
246 * The op may apply coverage as alpha and still blend correctly.
247 */
248 kCompatibleWithAlphaAsCoverage = 0x2,
249 /**
250 * The color input to the GrXferProcessor will be ignored.
251 */
252 kIgnoresInputColor = 0x4,
253 /**
254 * If set overlapping stencil and cover operations can be replaced by a combined stencil
255 * followed by a combined cover.
256 */
257 kCanCombineOverlappedStencilAndCover = 0x8,
258 /**
259 * The destination color will be provided to the fragment processor using a texture. This is
260 * additional information about the implementation of kReadsDstInShader.
261 */
262 kRequiresDstTexture = 0x10,
263 /**
264 * If set overlapping draws may not be combined because a barrier must be inserted between
265 * them.
266 */
267 kRequiresBarrierBetweenOverlappingDraws = 0x20,
Brian Salomon31853842017-03-28 16:32:05 -0400268 };
269 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties);
egdaniel080e6732014-12-22 07:35:52 -0800270
Brian Salomona076d872017-04-04 15:17:03 -0400271 static sk_sp<GrXferProcessor> MakeXferProcessor(const GrXPFactory*,
272 const GrProcessorAnalysisColor&,
273 GrProcessorAnalysisCoverage,
274 bool hasMixedSamples,
275 const GrCaps& caps);
276
Brian Salomon31853842017-03-28 16:32:05 -0400277 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*,
Brian Salomona811b122017-03-30 08:21:32 -0400278 const GrProcessorAnalysisColor&,
279 const GrProcessorAnalysisCoverage&,
Brian Salomon31853842017-03-28 16:32:05 -0400280 const GrCaps&);
Brian Salomon54d212e2017-03-21 14:22:38 -0400281
egdaniel915187b2014-12-05 12:58:28 -0800282protected:
Brian Salomona1633922017-01-09 11:46:10 -0500283 constexpr GrXPFactory() {}
egdaniel915187b2014-12-05 12:58:28 -0800284
egdaniel378092f2014-12-03 10:40:13 -0800285private:
Brian Salomona076d872017-04-04 15:17:03 -0400286 virtual sk_sp<GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&,
287 GrProcessorAnalysisCoverage,
288 bool hasMixedSamples,
289 const GrCaps&) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800290
bsalomon50785a32015-02-06 07:02:37 -0800291 /**
Brian Salomon31853842017-03-28 16:32:05 -0400292 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be
293 * inferred by the base class based on kReadsDstInShader and the caps.
bsalomon50785a32015-02-06 07:02:37 -0800294 */
Brian Salomona811b122017-03-30 08:21:32 -0400295 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&,
296 const GrProcessorAnalysisCoverage&,
Brian Salomon31853842017-03-28 16:32:05 -0400297 const GrCaps&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800298};
Brian Salomona1633922017-01-09 11:46:10 -0500299#if defined(__GNUC__) || defined(__clang)
300#pragma GCC diagnostic pop
301#endif
egdaniel378092f2014-12-03 10:40:13 -0800302
Brian Salomon31853842017-03-28 16:32:05 -0400303GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties);
egdaniel378092f2014-12-03 10:40:13 -0800304
Brian Salomon31853842017-03-28 16:32:05 -0400305#endif