blob: 35ce5fa4a214a5e0c061168e012743af2ea221dc [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 -080020class GrProcOptInfo;
21
egdaniel378092f2014-12-03 10:40:13 -080022/**
cdalton9954bc32015-04-29 14:17:00 -070023 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
24 * required after a pixel has been written, before it can be safely read again.
25 */
26enum GrXferBarrierType {
bsalomoncb02b382015-08-12 11:14:50 -070027 kNone_GrXferBarrierType = 0, //<! No barrier is required
28 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
29 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
cdalton9954bc32015-04-29 14:17:00 -070030};
bsalomoncb02b382015-08-12 11:14:50 -070031/** Should be able to treat kNone as false in boolean expressions */
32GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
cdalton9954bc32015-04-29 14:17:00 -070033
34/**
egdaniel378092f2014-12-03 10:40:13 -080035 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
cdaltonedbb31f2015-06-08 12:14:44 -070036 * color, and for applying any coverage. It does this by emitting fragment shader code and
37 * controlling the fixed-function blend state. When dual-source blending is available, it may also
38 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
39 *
40 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
41 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
42 * provides shader code that blends the src and dst colors, and the base class applies coverage.
43 *
44 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
45 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
egdaniel378092f2014-12-03 10:40:13 -080046 *
47 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
48 * GrXPFactory once we have finalized the state of our draw.
49 */
egdanielc2304142014-12-11 13:15:13 -080050class GrXferProcessor : public GrProcessor {
egdaniel95131432014-12-09 11:15:43 -080051public:
Brian Salomon5298dc82017-02-22 11:52:03 -050052 using FragmentProcessorAnalysis = GrProcessorSet::FragmentProcessorAnalysis;
53
egdaniel95131432014-12-09 11:15:43 -080054 /**
bsalomon6a44c6a2015-05-26 09:49:05 -070055 * A texture that contains the dst pixel values and an integer coord offset from device space
56 * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a
57 * GrXferProcessor for blending in the fragment shader.
58 */
59 class DstTexture {
60 public:
61 DstTexture() { fOffset.set(0, 0); }
62
63 DstTexture(const DstTexture& other) {
64 *this = other;
65 }
66
67 DstTexture(GrTexture* texture, const SkIPoint& offset)
Brian Salomon54d212e2017-03-21 14:22:38 -040068 : fTexture(SkSafeRef(texture)), fOffset(texture ? offset : SkIPoint{0, 0}) {}
bsalomon6a44c6a2015-05-26 09:49:05 -070069
70 DstTexture& operator=(const DstTexture& other) {
bungeman6bd52842016-10-27 09:30:08 -070071 fTexture = other.fTexture;
bsalomon6a44c6a2015-05-26 09:49:05 -070072 fOffset = other.fOffset;
73 return *this;
74 }
75
Brian Salomon54d212e2017-03-21 14:22:38 -040076 bool operator==(const DstTexture& that) const {
77 return fTexture == that.fTexture && fOffset == that.fOffset;
78 }
79 bool operator!=(const DstTexture& that) const { return !(*this == that); }
80
bsalomon6a44c6a2015-05-26 09:49:05 -070081 const SkIPoint& offset() const { return fOffset; }
82
83 void setOffset(const SkIPoint& offset) { fOffset = offset; }
84 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
85
86 GrTexture* texture() const { return fTexture.get(); }
87
bungeman6bd52842016-10-27 09:30:08 -070088 void setTexture(sk_sp<GrTexture> texture) {
89 fTexture = std::move(texture);
Brian Salomon54d212e2017-03-21 14:22:38 -040090 if (!fTexture) {
91 fOffset = {0, 0};
92 }
bsalomon6a44c6a2015-05-26 09:49:05 -070093 }
94
95 private:
bungeman6bd52842016-10-27 09:30:08 -070096 sk_sp<GrTexture> fTexture;
97 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -070098 };
99
100 /**
egdaniel57d3b032015-11-13 11:57:27 -0800101 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -0800102 * specific subclass's key.
103 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500104 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const;
egdanielc2304142014-12-11 13:15:13 -0800105
106 /** Returns a new instance of the appropriate *GL* implementation class
107 for the given GrXferProcessor; caller is responsible for deleting
108 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800109 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800110
111 /**
egdaniel95131432014-12-09 11:15:43 -0800112 * Optimizations for blending / coverage that an OptDrawState should apply to itself.
113 */
114 enum OptFlags {
115 /**
joshualitt9b989322014-12-15 14:16:27 -0800116 * GrXferProcessor will ignore color, thus no need to provide
egdaniel95131432014-12-09 11:15:43 -0800117 */
Brian Salomon02baee92017-02-24 10:37:15 -0500118 kIgnoreColor_OptFlag = 0x1,
egdaniel95131432014-12-09 11:15:43 -0800119 /**
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500120 * Can tweak alpha for coverage.
egdaniel54160f32014-12-15 12:38:53 -0800121 */
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500122 kCanTweakAlphaForCoverage_OptFlag = 0x2,
egdaniel95131432014-12-09 11:15:43 -0800123 };
124
bsalomon7765a472015-07-08 11:26:37 -0700125 static const OptFlags kNone_OptFlags = (OptFlags)0;
126
egdaniel95131432014-12-09 11:15:43 -0800127 GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags);
128
129 /**
130 * Determines which optimizations (as described by the ptFlags above) can be performed by
131 * the draw with this xfer processor. If this function is called, the xfer processor may change
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500132 * its state to reflected the given blend optimizations. Callers are required to honor the
133 * returned OptFlags.
egdaniel95131432014-12-09 11:15:43 -0800134 */
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500135 OptFlags getOptimizations(const FragmentProcessorAnalysis&) const;
egdaniel95131432014-12-09 11:15:43 -0800136
cdalton9954bc32015-04-29 14:17:00 -0700137 /**
138 * Returns whether this XP will require an Xfer barrier on the given rt. If true, outBarrierType
139 * is updated to contain the type of barrier needed.
140 */
bsalomoncb02b382015-08-12 11:14:50 -0700141 GrXferBarrierType xferBarrierType(const GrRenderTarget* rt, const GrCaps& caps) const;
cdalton9954bc32015-04-29 14:17:00 -0700142
egdaniel95131432014-12-09 11:15:43 -0800143 struct BlendInfo {
cdaltonf4f2b442015-04-23 09:40:23 -0700144 void reset() {
cdalton8917d622015-05-06 13:40:21 -0700145 fEquation = kAdd_GrBlendEquation;
cdaltonf4f2b442015-04-23 09:40:23 -0700146 fSrcBlend = kOne_GrBlendCoeff;
147 fDstBlend = kZero_GrBlendCoeff;
148 fBlendConstant = 0;
149 fWriteColor = true;
150 }
egdaniel080e6732014-12-22 07:35:52 -0800151
bsalomonf7cc8772015-05-11 11:21:14 -0700152 SkDEBUGCODE(SkString dump() const;)
153
cdalton8917d622015-05-06 13:40:21 -0700154 GrBlendEquation fEquation;
155 GrBlendCoeff fSrcBlend;
156 GrBlendCoeff fDstBlend;
157 GrColor fBlendConstant;
158 bool fWriteColor;
egdaniel95131432014-12-09 11:15:43 -0800159 };
160
cdaltonedbb31f2015-06-08 12:14:44 -0700161 void getBlendInfo(BlendInfo* blendInfo) const;
egdaniel95131432014-12-09 11:15:43 -0800162
egdaniel95131432014-12-09 11:15:43 -0800163 bool willReadDstColor() const { return fWillReadDstColor; }
164
bsalomon50785a32015-02-06 07:02:37 -0800165 /**
166 * Returns the texture to be used as the destination when reading the dst in the fragment
167 * shader. If the returned texture is NULL then the XP is either not reading the dst or we have
168 * extentions that support framebuffer fetching and thus don't need a copy of the dst texture.
169 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500170 const GrTexture* getDstTexture() const { return fDstTexture.texture(); }
bsalomon50785a32015-02-06 07:02:37 -0800171
172 /**
bsalomon6a44c6a2015-05-26 09:49:05 -0700173 * Returns the offset in device coords to use when accessing the dst texture to get the dst
174 * pixel color in the shader. This value is only valid if getDstTexture() != NULL.
bsalomon50785a32015-02-06 07:02:37 -0800175 */
bsalomon6a44c6a2015-05-26 09:49:05 -0700176 const SkIPoint& dstTextureOffset() const {
177 SkASSERT(this->getDstTexture());
178 return fDstTextureOffset;
bsalomon50785a32015-02-06 07:02:37 -0800179 }
180
egdanielc19cdc22015-05-10 08:45:18 -0700181 /**
cdalton86ae0a92015-06-08 15:11:04 -0700182 * If we are performing a dst read, returns whether the base class will use mixed samples to
183 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
184 * for antialiasing and this returns false.
185 */
186 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
187
188 /**
egdanielc2304142014-12-11 13:15:13 -0800189 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
190 * source blending.
191 */
cdaltonedbb31f2015-06-08 12:14:44 -0700192 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800193
194 /** Returns true if this and other processor conservatively draw identically. It can only return
195 true when the two processor are of the same subclass (i.e. they return the same object from
196 from getFactory()).
197
198 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800199 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
200 */
egdanielc2304142014-12-11 13:15:13 -0800201
202 bool isEqual(const GrXferProcessor& that) const {
203 if (this->classID() != that.classID()) {
204 return false;
205 }
bsalomon50785a32015-02-06 07:02:37 -0800206 if (this->fWillReadDstColor != that.fWillReadDstColor) {
207 return false;
208 }
Brian Salomondb4183d2016-11-17 12:48:40 -0500209 if (this->fDstTexture.texture() != that.fDstTexture.texture()) {
bsalomon50785a32015-02-06 07:02:37 -0800210 return false;
211 }
bsalomon6a44c6a2015-05-26 09:49:05 -0700212 if (this->fDstTextureOffset != that.fDstTextureOffset) {
bsalomon50785a32015-02-06 07:02:37 -0800213 return false;
214 }
cdalton86ae0a92015-06-08 15:11:04 -0700215 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
216 return false;
217 }
egdanielc2304142014-12-11 13:15:13 -0800218 return this->onIsEqual(that);
219 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500220
egdaniel95131432014-12-09 11:15:43 -0800221protected:
bsalomon50785a32015-02-06 07:02:37 -0800222 GrXferProcessor();
cdalton86ae0a92015-06-08 15:11:04 -0700223 GrXferProcessor(const DstTexture*, bool willReadDstColor, bool hasMixedSamples);
egdaniel95131432014-12-09 11:15:43 -0800224
egdaniel378092f2014-12-03 10:40:13 -0800225private:
bsalomon42048002015-08-27 16:43:48 -0700226 void notifyRefCntIsZero() const final {}
227
Brian Salomon1c10fdd2017-03-03 14:30:15 -0500228 virtual OptFlags onGetOptimizations(const FragmentProcessorAnalysis&) const = 0;
egdanielc19cdc22015-05-10 08:45:18 -0700229
bsalomon50785a32015-02-06 07:02:37 -0800230 /**
231 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
232 * processor's GL backend implementation.
233 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500234 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800235
cdaltonf4f2b442015-04-23 09:40:23 -0700236 /**
bsalomoncb02b382015-08-12 11:14:50 -0700237 * Determines the type of barrier (if any) required by the subclass. Note that the possibility
238 * that a kTexture type barrier is required is handled by the base class and need not be
239 * considered by subclass overrides of this function.
cdalton8917d622015-05-06 13:40:21 -0700240 */
bsalomoncb02b382015-08-12 11:14:50 -0700241 virtual GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const {
242 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700243 }
244
245 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700246 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700247 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700248 * will not be called.
249 */
250 virtual bool onHasSecondaryOutput() const { return false; }
251
252 /**
253 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700254 * subclass. When using dst reads, the base class controls the fixed-function blend state and
255 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700256 */
257 virtual void onGetBlendInfo(BlendInfo*) const {}
258
egdanielc2304142014-12-11 13:15:13 -0800259 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800260
bsalomon50785a32015-02-06 07:02:37 -0800261 bool fWillReadDstColor;
cdalton86ae0a92015-06-08 15:11:04 -0700262 bool fDstReadUsesMixedSamples;
bsalomon6a44c6a2015-05-26 09:49:05 -0700263 SkIPoint fDstTextureOffset;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500264 TextureSampler fDstTexture;
egdaniel95131432014-12-09 11:15:43 -0800265
egdaniel378092f2014-12-03 10:40:13 -0800266 typedef GrFragmentProcessor INHERITED;
267};
268
egdaniel95131432014-12-09 11:15:43 -0800269GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags);
270
bsalomon50785a32015-02-06 07:02:37 -0800271///////////////////////////////////////////////////////////////////////////////
272
egdaniel378092f2014-12-03 10:40:13 -0800273/**
274 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
275 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
276 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
277 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
278 * the draw.
279 *
280 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
281 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
282 * blend with the destination color.
Brian Salomona1633922017-01-09 11:46:10 -0500283 *
284 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers
285 * and expect the pointers to always be valid and for the factories to be reusable and thread safe.
286 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops.
egdaniel378092f2014-12-03 10:40:13 -0800287 */
Brian Salomona1633922017-01-09 11:46:10 -0500288
289// In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore
290// GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok
291// since these objects have no need for destructors. However, GCC and clang throw a warning when a
292// class has virtual functions and a non-virtual destructor. We suppress that warning here and
293// for the subclasses.
294#if defined(__GNUC__) || defined(__clang)
295#pragma GCC diagnostic push
296#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
297#endif
298class GrXPFactory {
egdaniel378092f2014-12-03 10:40:13 -0800299public:
Brian Salomon5298dc82017-02-22 11:52:03 -0500300 using FragmentProcessorAnalysis = GrProcessorSet::FragmentProcessorAnalysis;
301
bsalomon6a44c6a2015-05-26 09:49:05 -0700302 typedef GrXferProcessor::DstTexture DstTexture;
Brian Salomon5be6c952017-01-20 19:06:29 +0000303
Brian Salomon5298dc82017-02-22 11:52:03 -0500304 GrXferProcessor* createXferProcessor(const FragmentProcessorAnalysis&,
cdalton86ae0a92015-06-08 15:11:04 -0700305 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700306 const DstTexture*,
bsalomon4b91f762015-05-19 09:29:46 -0700307 const GrCaps& caps) const;
Brian Salomon9a514982017-02-14 10:28:22 -0500308
cdalton1fa45722015-06-02 10:43:39 -0700309 /**
Brian Salomon5298dc82017-02-22 11:52:03 -0500310 * This will return true if the xfer processor needs the dst color in the shader and the way
311 * that the color will be made available to the xfer processor is by sampling a texture.
312 */
313 static bool WillNeedDstTexture(const GrXPFactory*,
314 const GrCaps&,
315 const FragmentProcessorAnalysis&);
egdaniel080e6732014-12-22 07:35:52 -0800316
Brian Salomon780b41f2017-03-13 10:36:40 -0400317 static bool CompatibleWithCoverageAsAlpha(const GrXPFactory*, bool colorIsOpaque);
318
Brian Salomon54d212e2017-03-21 14:22:38 -0400319 /**
320 * This indicates whether the the xfer processor will produce the same bleneded color result
321 * if a series of overlapping stencil and cover operations are replaced by a series of stencil
322 * operations and a single cover. A uniform src color is assumed.
323 **/
324 static bool CanCombineOverlappedStencilAndCover(const GrXPFactory*, bool colorIsOpaque);
325
egdaniel915187b2014-12-05 12:58:28 -0800326protected:
Brian Salomona1633922017-01-09 11:46:10 -0500327 constexpr GrXPFactory() {}
egdaniel915187b2014-12-05 12:58:28 -0800328
egdaniel378092f2014-12-03 10:40:13 -0800329private:
bsalomon4b91f762015-05-19 09:29:46 -0700330 virtual GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
Brian Salomon5298dc82017-02-22 11:52:03 -0500331 const FragmentProcessorAnalysis&,
cdalton86ae0a92015-06-08 15:11:04 -0700332 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700333 const DstTexture*) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800334
bsalomon50785a32015-02-06 07:02:37 -0800335 /**
336 * Returns true if the XP generated by this factory will explicitly read dst in the fragment
Brian Salomon42c456f2017-03-06 11:29:48 -0500337 * shader.
bsalomon50785a32015-02-06 07:02:37 -0800338 */
Brian Salomon42c456f2017-03-06 11:29:48 -0500339 virtual bool willReadDstInShader(const GrCaps&, const FragmentProcessorAnalysis&) const = 0;
Brian Salomon780b41f2017-03-13 10:36:40 -0400340
341 virtual bool compatibleWithCoverageAsAlpha(bool colorIsOpaque) const = 0;
Brian Salomon54d212e2017-03-21 14:22:38 -0400342 virtual bool canCombineOverlappedStencilAndCover(bool colorIsOpaque) const { return false; }
egdaniel378092f2014-12-03 10:40:13 -0800343};
Brian Salomona1633922017-01-09 11:46:10 -0500344#if defined(__GNUC__) || defined(__clang)
345#pragma GCC diagnostic pop
346#endif
egdaniel378092f2014-12-03 10:40:13 -0800347
348#endif
349