blob: 5317c187e2e0784093955365f9b67c83d0ee42ea [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"
bsalomon50785a32015-02-06 07:02:37 -080014#include "GrTexture.h"
egdaniel378092f2014-12-03 10:40:13 -080015#include "GrTypes.h"
16#include "SkXfermode.h"
17
jvanverthe9c0fc62015-04-29 11:18:05 -070018class GrShaderCaps;
19class GrGLSLCaps;
egdanielfa4cc8b2015-11-13 08:34:52 -080020class GrGLSLXferProcessor;
egdaniel95131432014-12-09 11:15:43 -080021class GrProcOptInfo;
ethannicholasde4166a2015-11-30 08:57:38 -080022struct GrPipelineOptimizations;
egdaniel95131432014-12-09 11:15:43 -080023
egdaniel378092f2014-12-03 10:40:13 -080024/**
cdalton9954bc32015-04-29 14:17:00 -070025 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes
26 * required after a pixel has been written, before it can be safely read again.
27 */
28enum GrXferBarrierType {
bsalomoncb02b382015-08-12 11:14:50 -070029 kNone_GrXferBarrierType = 0, //<! No barrier is required
30 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
31 kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
cdalton9954bc32015-04-29 14:17:00 -070032};
bsalomoncb02b382015-08-12 11:14:50 -070033/** Should be able to treat kNone as false in boolean expressions */
34GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
cdalton9954bc32015-04-29 14:17:00 -070035
36/**
egdaniel378092f2014-12-03 10:40:13 -080037 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
cdaltonedbb31f2015-06-08 12:14:44 -070038 * color, and for applying any coverage. It does this by emitting fragment shader code and
39 * controlling the fixed-function blend state. When dual-source blending is available, it may also
40 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation:
41 *
42 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the
43 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only
44 * provides shader code that blends the src and dst colors, and the base class applies coverage.
45 *
46 * No dst read: When not performing a dst read, the subclass is given full control of the fixed-
47 * function blend state and/or secondary output, and is responsible to apply coverage on its own.
egdaniel378092f2014-12-03 10:40:13 -080048 *
49 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
50 * GrXPFactory once we have finalized the state of our draw.
51 */
egdanielc2304142014-12-11 13:15:13 -080052class GrXferProcessor : public GrProcessor {
egdaniel95131432014-12-09 11:15:43 -080053public:
54 /**
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)
68 : fTexture(SkSafeRef(texture))
69 , fOffset(offset) {
70 }
71
72 DstTexture& operator=(const DstTexture& other) {
bungeman6bd52842016-10-27 09:30:08 -070073 fTexture = other.fTexture;
bsalomon6a44c6a2015-05-26 09:49:05 -070074 fOffset = other.fOffset;
75 return *this;
76 }
77
78 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);
bsalomon6a44c6a2015-05-26 09:49:05 -070087 }
88
89 private:
bungeman6bd52842016-10-27 09:30:08 -070090 sk_sp<GrTexture> fTexture;
91 SkIPoint fOffset;
bsalomon6a44c6a2015-05-26 09:49:05 -070092 };
93
94 /**
egdaniel57d3b032015-11-13 11:57:27 -080095 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the
bsalomon50785a32015-02-06 07:02:37 -080096 * specific subclass's key.
97 */
egdaniel57d3b032015-11-13 11:57:27 -080098 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const;
egdanielc2304142014-12-11 13:15:13 -080099
100 /** Returns a new instance of the appropriate *GL* implementation class
101 for the given GrXferProcessor; caller is responsible for deleting
102 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800103 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
egdanielc2304142014-12-11 13:15:13 -0800104
105 /**
egdaniel95131432014-12-09 11:15:43 -0800106 * Optimizations for blending / coverage that an OptDrawState should apply to itself.
107 */
108 enum OptFlags {
109 /**
egdaniel95131432014-12-09 11:15:43 -0800110 * The draw can be skipped completely.
111 */
112 kSkipDraw_OptFlag = 0x1,
113 /**
joshualitt9b989322014-12-15 14:16:27 -0800114 * GrXferProcessor will ignore color, thus no need to provide
egdaniel95131432014-12-09 11:15:43 -0800115 */
joshualitt9b989322014-12-15 14:16:27 -0800116 kIgnoreColor_OptFlag = 0x2,
egdaniel95131432014-12-09 11:15:43 -0800117 /**
joshualitt9b989322014-12-15 14:16:27 -0800118 * GrXferProcessor will ignore coverage, thus no need to provide
egdaniel95131432014-12-09 11:15:43 -0800119 */
joshualitt9b989322014-12-15 14:16:27 -0800120 kIgnoreCoverage_OptFlag = 0x4,
egdaniel95131432014-12-09 11:15:43 -0800121 /**
egdaniel54160f32014-12-15 12:38:53 -0800122 * Clear color stages and override input color to that returned by getOptimizations
123 */
124 kOverrideColor_OptFlag = 0x8,
125 /**
egdanielf7c2d552015-02-13 12:11:00 -0800126 * Can tweak alpha for coverage. Currently this flag should only be used by a batch
127 */
128 kCanTweakAlphaForCoverage_OptFlag = 0x20,
egdaniel95131432014-12-09 11:15:43 -0800129 };
130
bsalomon7765a472015-07-08 11:26:37 -0700131 static const OptFlags kNone_OptFlags = (OptFlags)0;
132
egdaniel95131432014-12-09 11:15:43 -0800133 GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags);
134
135 /**
136 * Determines which optimizations (as described by the ptFlags above) can be performed by
137 * the draw with this xfer processor. If this function is called, the xfer processor may change
egdaniel54160f32014-12-15 12:38:53 -0800138 * its state to reflected the given blend optimizations. If the XP needs to see a specific input
139 * color to blend correctly, it will set the OverrideColor flag and the output parameter
140 * overrideColor will be the required value that should be passed into the XP.
egdaniel95131432014-12-09 11:15:43 -0800141 * A caller who calls this function on a XP is required to honor the returned OptFlags
egdaniel54160f32014-12-15 12:38:53 -0800142 * and color values for its draw.
egdaniel95131432014-12-09 11:15:43 -0800143 */
ethannicholasde4166a2015-11-30 08:57:38 -0800144 OptFlags getOptimizations(const GrPipelineOptimizations& optimizations,
egdanielc19cdc22015-05-10 08:45:18 -0700145 bool doesStencilWrite,
146 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800147 const GrCaps& caps) const;
egdaniel95131432014-12-09 11:15:43 -0800148
cdalton9954bc32015-04-29 14:17:00 -0700149 /**
150 * Returns whether this XP will require an Xfer barrier on the given rt. If true, outBarrierType
151 * is updated to contain the type of barrier needed.
152 */
bsalomoncb02b382015-08-12 11:14:50 -0700153 GrXferBarrierType xferBarrierType(const GrRenderTarget* rt, const GrCaps& caps) const;
cdalton9954bc32015-04-29 14:17:00 -0700154
egdaniel95131432014-12-09 11:15:43 -0800155 struct BlendInfo {
cdaltonf4f2b442015-04-23 09:40:23 -0700156 void reset() {
cdalton8917d622015-05-06 13:40:21 -0700157 fEquation = kAdd_GrBlendEquation;
cdaltonf4f2b442015-04-23 09:40:23 -0700158 fSrcBlend = kOne_GrBlendCoeff;
159 fDstBlend = kZero_GrBlendCoeff;
160 fBlendConstant = 0;
161 fWriteColor = true;
162 }
egdaniel080e6732014-12-22 07:35:52 -0800163
bsalomonf7cc8772015-05-11 11:21:14 -0700164 SkDEBUGCODE(SkString dump() const;)
165
cdalton8917d622015-05-06 13:40:21 -0700166 GrBlendEquation fEquation;
167 GrBlendCoeff fSrcBlend;
168 GrBlendCoeff fDstBlend;
169 GrColor fBlendConstant;
170 bool fWriteColor;
egdaniel95131432014-12-09 11:15:43 -0800171 };
172
cdaltonedbb31f2015-06-08 12:14:44 -0700173 void getBlendInfo(BlendInfo* blendInfo) const;
egdaniel95131432014-12-09 11:15:43 -0800174
egdaniel95131432014-12-09 11:15:43 -0800175 bool willReadDstColor() const { return fWillReadDstColor; }
176
bsalomon50785a32015-02-06 07:02:37 -0800177 /**
178 * Returns the texture to be used as the destination when reading the dst in the fragment
179 * shader. If the returned texture is NULL then the XP is either not reading the dst or we have
180 * extentions that support framebuffer fetching and thus don't need a copy of the dst texture.
181 */
bsalomon6a44c6a2015-05-26 09:49:05 -0700182 const GrTexture* getDstTexture() const { return fDstTexture.getTexture(); }
bsalomon50785a32015-02-06 07:02:37 -0800183
184 /**
bsalomon6a44c6a2015-05-26 09:49:05 -0700185 * Returns the offset in device coords to use when accessing the dst texture to get the dst
186 * pixel color in the shader. This value is only valid if getDstTexture() != NULL.
bsalomon50785a32015-02-06 07:02:37 -0800187 */
bsalomon6a44c6a2015-05-26 09:49:05 -0700188 const SkIPoint& dstTextureOffset() const {
189 SkASSERT(this->getDstTexture());
190 return fDstTextureOffset;
bsalomon50785a32015-02-06 07:02:37 -0800191 }
192
egdanielc19cdc22015-05-10 08:45:18 -0700193 /**
cdalton86ae0a92015-06-08 15:11:04 -0700194 * If we are performing a dst read, returns whether the base class will use mixed samples to
195 * antialias the shader's final output. If not doing a dst read, the subclass is responsible
196 * for antialiasing and this returns false.
197 */
198 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
199
200 /**
egdanielc2304142014-12-11 13:15:13 -0800201 * Returns whether or not this xferProcossor will set a secondary output to be used with dual
202 * source blending.
203 */
cdaltonedbb31f2015-06-08 12:14:44 -0700204 bool hasSecondaryOutput() const;
egdanielc2304142014-12-11 13:15:13 -0800205
206 /** Returns true if this and other processor conservatively draw identically. It can only return
207 true when the two processor are of the same subclass (i.e. they return the same object from
208 from getFactory()).
209
210 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800211 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
212 */
egdanielc2304142014-12-11 13:15:13 -0800213
214 bool isEqual(const GrXferProcessor& that) const {
215 if (this->classID() != that.classID()) {
216 return false;
217 }
bsalomon50785a32015-02-06 07:02:37 -0800218 if (this->fWillReadDstColor != that.fWillReadDstColor) {
219 return false;
220 }
bsalomon6a44c6a2015-05-26 09:49:05 -0700221 if (this->fDstTexture.getTexture() != that.fDstTexture.getTexture()) {
bsalomon50785a32015-02-06 07:02:37 -0800222 return false;
223 }
bsalomon6a44c6a2015-05-26 09:49:05 -0700224 if (this->fDstTextureOffset != that.fDstTextureOffset) {
bsalomon50785a32015-02-06 07:02:37 -0800225 return false;
226 }
cdalton86ae0a92015-06-08 15:11:04 -0700227 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
228 return false;
229 }
egdanielc2304142014-12-11 13:15:13 -0800230 return this->onIsEqual(that);
231 }
232
egdaniel95131432014-12-09 11:15:43 -0800233protected:
bsalomon50785a32015-02-06 07:02:37 -0800234 GrXferProcessor();
cdalton86ae0a92015-06-08 15:11:04 -0700235 GrXferProcessor(const DstTexture*, bool willReadDstColor, bool hasMixedSamples);
egdaniel95131432014-12-09 11:15:43 -0800236
egdaniel378092f2014-12-03 10:40:13 -0800237private:
bsalomon42048002015-08-27 16:43:48 -0700238 void notifyRefCntIsZero() const final {}
239
ethannicholasde4166a2015-11-30 08:57:38 -0800240 virtual OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
egdanielc19cdc22015-05-10 08:45:18 -0700241 bool doesStencilWrite,
242 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -0800243 const GrCaps& caps) const = 0;
egdanielc19cdc22015-05-10 08:45:18 -0700244
bsalomon50785a32015-02-06 07:02:37 -0800245 /**
246 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer
247 * processor's GL backend implementation.
248 */
egdaniel57d3b032015-11-13 11:57:27 -0800249 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
250 GrProcessorKeyBuilder* b) const = 0;
bsalomon50785a32015-02-06 07:02:37 -0800251
cdaltonf4f2b442015-04-23 09:40:23 -0700252 /**
bsalomoncb02b382015-08-12 11:14:50 -0700253 * Determines the type of barrier (if any) required by the subclass. Note that the possibility
254 * that a kTexture type barrier is required is handled by the base class and need not be
255 * considered by subclass overrides of this function.
cdalton8917d622015-05-06 13:40:21 -0700256 */
bsalomoncb02b382015-08-12 11:14:50 -0700257 virtual GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const {
258 return kNone_GrXferBarrierType;
cdalton8917d622015-05-06 13:40:21 -0700259 }
260
261 /**
cdaltonedbb31f2015-06-08 12:14:44 -0700262 * If we are not performing a dst read, returns whether the subclass will set a secondary
cdalton86ae0a92015-06-08 15:11:04 -0700263 * output. When using dst reads, the base class controls the secondary output and this method
cdaltonedbb31f2015-06-08 12:14:44 -0700264 * will not be called.
265 */
266 virtual bool onHasSecondaryOutput() const { return false; }
267
268 /**
269 * If we are not performing a dst read, retrieves the fixed-function blend state required by the
cdalton86ae0a92015-06-08 15:11:04 -0700270 * subclass. When using dst reads, the base class controls the fixed-function blend state and
271 * this method will not be called. The BlendInfo struct comes initialized to "no blending".
cdaltonf4f2b442015-04-23 09:40:23 -0700272 */
273 virtual void onGetBlendInfo(BlendInfo*) const {}
274
egdanielc2304142014-12-11 13:15:13 -0800275 virtual bool onIsEqual(const GrXferProcessor&) const = 0;
egdaniel378092f2014-12-03 10:40:13 -0800276
bsalomon50785a32015-02-06 07:02:37 -0800277 bool fWillReadDstColor;
cdalton86ae0a92015-06-08 15:11:04 -0700278 bool fDstReadUsesMixedSamples;
bsalomon6a44c6a2015-05-26 09:49:05 -0700279 SkIPoint fDstTextureOffset;
280 GrTextureAccess fDstTexture;
egdaniel95131432014-12-09 11:15:43 -0800281
egdaniel378092f2014-12-03 10:40:13 -0800282 typedef GrFragmentProcessor INHERITED;
283};
284
egdaniel95131432014-12-09 11:15:43 -0800285GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags);
286
bsalomon50785a32015-02-06 07:02:37 -0800287///////////////////////////////////////////////////////////////////////////////
288
egdaniel378092f2014-12-03 10:40:13 -0800289/**
290 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
291 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
292 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
293 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
294 * the draw.
295 *
296 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
297 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
298 * blend with the destination color.
299 */
egdanielc016fb82014-12-03 11:41:54 -0800300class GrXPFactory : public SkRefCnt {
egdaniel378092f2014-12-03 10:40:13 -0800301public:
bsalomon6a44c6a2015-05-26 09:49:05 -0700302 typedef GrXferProcessor::DstTexture DstTexture;
ethannicholasde4166a2015-11-30 08:57:38 -0800303 GrXferProcessor* createXferProcessor(const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700304 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700305 const DstTexture*,
bsalomon4b91f762015-05-19 09:29:46 -0700306 const GrCaps& caps) const;
cdalton1fa45722015-06-02 10:43:39 -0700307 /**
308 * Known color information after blending, but before accounting for any coverage.
309 */
310 struct InvariantBlendedColor {
311 bool fWillBlendWithDst;
312 GrColor fKnownColor;
313 GrColorComponentFlags fKnownColorFlags;
egdaniel9e4ecdc2014-12-18 12:44:55 -0800314 };
315
316 /**
cdalton1fa45722015-06-02 10:43:39 -0700317 * Returns information about the output color, produced by XPs from this factory, that will be
318 * known after blending. Note that we can conflate coverage and color, so the actual values
319 * written to pixels with partial coverage may not always seem consistent with the invariant
320 * information returned by this function.
egdaniel95131432014-12-09 11:15:43 -0800321 */
cdalton1fa45722015-06-02 10:43:39 -0700322 virtual void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
323 InvariantBlendedColor*) const = 0;
egdaniel95131432014-12-09 11:15:43 -0800324
cdalton3ccf2e72016-05-06 09:41:16 -0700325 bool willNeedDstTexture(const GrCaps& caps, const GrPipelineOptimizations& optimizations) const;
egdaniel080e6732014-12-22 07:35:52 -0800326
egdaniel915187b2014-12-05 12:58:28 -0800327 bool isEqual(const GrXPFactory& that) const {
328 if (this->classID() != that.classID()) {
329 return false;
330 }
331 return this->onIsEqual(that);
332 }
333
334 /**
335 * Helper for down-casting to a GrXPFactory subclass
336 */
337 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
338
339 uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; }
340
341protected:
342 GrXPFactory() : fClassID(kIllegalXPFClassID) {}
343
344 template <typename XPF_SUBCLASS> void initClassID() {
345 static uint32_t kClassID = GenClassID();
346 fClassID = kClassID;
347 }
348
349 uint32_t fClassID;
350
egdaniel378092f2014-12-03 10:40:13 -0800351private:
bsalomon4b91f762015-05-19 09:29:46 -0700352 virtual GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800353 const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700354 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700355 const DstTexture*) const = 0;
ethannicholas22793252016-01-30 09:59:10 -0800356
357 virtual bool onIsEqual(const GrXPFactory&) const = 0;
358
cdalton3ccf2e72016-05-06 09:41:16 -0700359 bool willReadDstColor(const GrCaps&, const GrPipelineOptimizations&) const;
bsalomon50785a32015-02-06 07:02:37 -0800360 /**
361 * Returns true if the XP generated by this factory will explicitly read dst in the fragment
362 * shader.
363 */
cdalton3ccf2e72016-05-06 09:41:16 -0700364 virtual bool onWillReadDstColor(const GrCaps&, const GrPipelineOptimizations&) const = 0;
egdaniel915187b2014-12-05 12:58:28 -0800365
366 static uint32_t GenClassID() {
367 // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The
368 // atomic inc returns the old value not the incremented value. So we add
369 // 1 to the returned value.
370 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1;
371 if (!id) {
372 SkFAIL("This should never wrap as it should only be called once for each GrXPFactory "
373 "subclass.");
374 }
375 return id;
376 }
377
378 enum {
379 kIllegalXPFClassID = 0,
380 };
381 static int32_t gCurrXPFClassID;
382
egdaniel378092f2014-12-03 10:40:13 -0800383 typedef GrProgramElement INHERITED;
384};
385
386#endif
387