blob: ebf949285bd11b19ad20a1b3a75d990f97053fc5 [file] [log] [blame]
bsalomon50785a32015-02-06 07:02:37 -08001/*
2 * Copyright 2015 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#include "GrXferProcessor.h"
ethannicholasde4166a2015-11-30 08:57:38 -08009#include "GrPipeline.h"
cdaltonedbb31f2015-06-08 12:14:44 -070010#include "GrProcOptInfo.h"
bsalomon50785a32015-02-06 07:02:37 -080011#include "gl/GrGLCaps.h"
12
egdanielc19cdc22015-05-10 08:45:18 -070013GrXferProcessor::GrXferProcessor()
cdalton86ae0a92015-06-08 15:11:04 -070014 : fWillReadDstColor(false)
15 , fDstReadUsesMixedSamples(false)
cdalton86ae0a92015-06-08 15:11:04 -070016 , fDstTextureOffset() {
bsalomon50785a32015-02-06 07:02:37 -080017}
18
cdalton86ae0a92015-06-08 15:11:04 -070019GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture,
20 bool willReadDstColor,
21 bool hasMixedSamples)
bsalomon50785a32015-02-06 07:02:37 -080022 : fWillReadDstColor(willReadDstColor)
cdalton86ae0a92015-06-08 15:11:04 -070023 , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples)
bsalomon6a44c6a2015-05-26 09:49:05 -070024 , fDstTextureOffset() {
25 if (dstTexture && dstTexture->texture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070026 SkASSERT(willReadDstColor);
bsalomon6a44c6a2015-05-26 09:49:05 -070027 fDstTexture.reset(dstTexture->texture());
28 fDstTextureOffset = dstTexture->offset();
Brian Salomon0bbecb22016-11-17 11:38:22 -050029 this->addTextureSampler(&fDstTexture);
egdaniel060a52c2015-04-07 07:31:11 -070030 this->setWillReadFragmentPosition();
bsalomon50785a32015-02-06 07:02:37 -080031 }
32}
33
ethannicholasde4166a2015-11-30 08:57:38 -080034GrXferProcessor::OptFlags GrXferProcessor::getOptimizations(
35 const GrPipelineOptimizations& optimizations,
36 bool doesStencilWrite,
37 GrColor* overrideColor,
egdaniel56cf6dc2015-11-30 10:15:58 -080038 const GrCaps& caps) const {
ethannicholasde4166a2015-11-30 08:57:38 -080039 GrXferProcessor::OptFlags flags = this->onGetOptimizations(optimizations,
egdanielc19cdc22015-05-10 08:45:18 -070040 doesStencilWrite,
41 overrideColor,
42 caps);
43
cdaltonedbb31f2015-06-08 12:14:44 -070044 if (this->willReadDstColor()) {
45 // When performing a dst read we handle coverage in the base class.
46 SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag));
ethannicholasde4166a2015-11-30 08:57:38 -080047 if (optimizations.fCoveragePOI.isSolidWhite()) {
cdaltonedbb31f2015-06-08 12:14:44 -070048 flags |= GrXferProcessor::kIgnoreCoverage_OptFlag;
49 }
50 }
egdanielc19cdc22015-05-10 08:45:18 -070051 return flags;
52}
53
cdaltonedbb31f2015-06-08 12:14:44 -070054bool GrXferProcessor::hasSecondaryOutput() const {
55 if (!this->willReadDstColor()) {
56 return this->onHasSecondaryOutput();
57 }
cdalton86ae0a92015-06-08 15:11:04 -070058 return this->dstReadUsesMixedSamples();
cdaltonedbb31f2015-06-08 12:14:44 -070059}
60
61void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const {
62 blendInfo->reset();
63 if (!this->willReadDstColor()) {
64 this->onGetBlendInfo(blendInfo);
cdalton86ae0a92015-06-08 15:11:04 -070065 } else if (this->dstReadUsesMixedSamples()) {
66 blendInfo->fDstBlend = kIS2A_GrBlendCoeff;
cdaltonedbb31f2015-06-08 12:14:44 -070067 }
68}
69
Brian Salomon94efbf52016-11-29 13:43:05 -050070void GrXferProcessor::getGLSLProcessorKey(const GrShaderCaps& caps,
71 GrProcessorKeyBuilder* b) const {
bsalomon50785a32015-02-06 07:02:37 -080072 uint32_t key = this->willReadDstColor() ? 0x1 : 0x0;
cdaltonedbb31f2015-06-08 12:14:44 -070073 if (key) {
cdalton827bae12015-06-08 13:43:33 -070074 if (const GrTexture* dstTexture = this->getDstTexture()) {
cdaltonedbb31f2015-06-08 12:14:44 -070075 key |= 0x2;
cdalton827bae12015-06-08 13:43:33 -070076 if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) {
77 key |= 0x4;
78 }
cdaltonedbb31f2015-06-08 12:14:44 -070079 }
cdalton86ae0a92015-06-08 15:11:04 -070080 if (this->dstReadUsesMixedSamples()) {
egdaniel56cf6dc2015-11-30 10:15:58 -080081 key |= 0x8;
cdalton86ae0a92015-06-08 15:11:04 -070082 }
bsalomon50785a32015-02-06 07:02:37 -080083 }
84 b->add32(key);
egdaniel57d3b032015-11-13 11:57:27 -080085 this->onGetGLSLProcessorKey(caps, b);
bsalomon50785a32015-02-06 07:02:37 -080086}
87
bsalomoncb02b382015-08-12 11:14:50 -070088GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt,
89 const GrCaps& caps) const {
90 SkASSERT(rt);
bsalomon6a44c6a2015-05-26 09:49:05 -070091 if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) {
cdalton9954bc32015-04-29 14:17:00 -070092 // Texture barriers are required when a shader reads and renders to the same texture.
cdalton9954bc32015-04-29 14:17:00 -070093 SkASSERT(caps.textureBarrierSupport());
bsalomoncb02b382015-08-12 11:14:50 -070094 return kTexture_GrXferBarrierType;
cdalton9954bc32015-04-29 14:17:00 -070095 }
bsalomoncb02b382015-08-12 11:14:50 -070096 return this->onXferBarrier(rt, caps);
cdalton9954bc32015-04-29 14:17:00 -070097}
98
bsalomonf7cc8772015-05-11 11:21:14 -070099#ifdef SK_DEBUG
100static const char* equation_string(GrBlendEquation eq) {
101 switch (eq) {
102 case kAdd_GrBlendEquation:
103 return "add";
104 case kSubtract_GrBlendEquation:
105 return "subtract";
106 case kReverseSubtract_GrBlendEquation:
107 return "reverse_subtract";
108 case kScreen_GrBlendEquation:
109 return "screen";
110 case kOverlay_GrBlendEquation:
111 return "overlay";
112 case kDarken_GrBlendEquation:
113 return "darken";
114 case kLighten_GrBlendEquation:
115 return "lighten";
116 case kColorDodge_GrBlendEquation:
117 return "color_dodge";
118 case kColorBurn_GrBlendEquation:
119 return "color_burn";
120 case kHardLight_GrBlendEquation:
121 return "hard_light";
122 case kSoftLight_GrBlendEquation:
123 return "soft_light";
124 case kDifference_GrBlendEquation:
125 return "difference";
126 case kExclusion_GrBlendEquation:
127 return "exclusion";
128 case kMultiply_GrBlendEquation:
129 return "multiply";
130 case kHSLHue_GrBlendEquation:
131 return "hsl_hue";
132 case kHSLSaturation_GrBlendEquation:
133 return "hsl_saturation";
134 case kHSLColor_GrBlendEquation:
135 return "hsl_color";
136 case kHSLLuminosity_GrBlendEquation:
137 return "hsl_luminosity";
138 };
139 return "";
140}
141
142static const char* coeff_string(GrBlendCoeff coeff) {
143 switch (coeff) {
144 case kZero_GrBlendCoeff:
145 return "zero";
146 case kOne_GrBlendCoeff:
147 return "one";
148 case kSC_GrBlendCoeff:
149 return "src_color";
150 case kISC_GrBlendCoeff:
151 return "inv_src_color";
152 case kDC_GrBlendCoeff:
153 return "dst_color";
154 case kIDC_GrBlendCoeff:
155 return "inv_dst_color";
156 case kSA_GrBlendCoeff:
157 return "src_alpha";
158 case kISA_GrBlendCoeff:
159 return "inv_src_alpha";
160 case kDA_GrBlendCoeff:
161 return "dst_alpha";
162 case kIDA_GrBlendCoeff:
163 return "inv_dst_alpha";
164 case kConstC_GrBlendCoeff:
165 return "const_color";
166 case kIConstC_GrBlendCoeff:
167 return "inv_const_color";
168 case kConstA_GrBlendCoeff:
169 return "const_alpha";
170 case kIConstA_GrBlendCoeff:
171 return "inv_const_alpha";
172 case kS2C_GrBlendCoeff:
173 return "src2_color";
174 case kIS2C_GrBlendCoeff:
175 return "inv_src2_color";
176 case kS2A_GrBlendCoeff:
177 return "src2_alpha";
178 case kIS2A_GrBlendCoeff:
179 return "inv_src2_alpha";
180 }
181 return "";
182}
183
184SkString GrXferProcessor::BlendInfo::dump() const {
185 SkString out;
186 out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)",
187 fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend),
188 coeff_string(fDstBlend), fBlendConstant);
189 return out;
190}
191#endif
192
bsalomon50785a32015-02-06 07:02:37 -0800193///////////////////////////////////////////////////////////////////////////////
194
ethannicholasde4166a2015-11-30 08:57:38 -0800195GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineOptimizations& optimizations,
cdalton86ae0a92015-06-08 15:11:04 -0700196 bool hasMixedSamples,
bsalomon6a44c6a2015-05-26 09:49:05 -0700197 const DstTexture* dstTexture,
bsalomon4b91f762015-05-19 09:29:46 -0700198 const GrCaps& caps) const {
bsalomon50785a32015-02-06 07:02:37 -0800199#ifdef SK_DEBUG
cdalton3ccf2e72016-05-06 09:41:16 -0700200 if (this->willReadDstColor(caps, optimizations)) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700201 if (!caps.shaderCaps()->dstReadInShaderSupport()) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700202 SkASSERT(dstTexture && dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800203 } else {
halcanary9d524f22016-03-29 09:03:52 -0700204 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800205 }
206 } else {
halcanary9d524f22016-03-29 09:03:52 -0700207 SkASSERT(!dstTexture || !dstTexture->texture());
bsalomon50785a32015-02-06 07:02:37 -0800208 }
cdalton86ae0a92015-06-08 15:11:04 -0700209 SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport());
bsalomon50785a32015-02-06 07:02:37 -0800210#endif
ethannicholasde4166a2015-11-30 08:57:38 -0800211 return this->onCreateXferProcessor(caps, optimizations, hasMixedSamples, dstTexture);
bsalomon50785a32015-02-06 07:02:37 -0800212}
213
cdalton86ae0a92015-06-08 15:11:04 -0700214bool GrXPFactory::willNeedDstTexture(const GrCaps& caps,
cdalton3ccf2e72016-05-06 09:41:16 -0700215 const GrPipelineOptimizations& optimizations) const {
216 return (this->willReadDstColor(caps, optimizations) &&
cdalton86ae0a92015-06-08 15:11:04 -0700217 !caps.shaderCaps()->dstReadInShaderSupport());
bsalomon50785a32015-02-06 07:02:37 -0800218}
ethannicholas22793252016-01-30 09:59:10 -0800219
220bool GrXPFactory::willReadDstColor(const GrCaps& caps,
cdalton3ccf2e72016-05-06 09:41:16 -0700221 const GrPipelineOptimizations& optimizations) const {
222 return optimizations.fOverrides.fUsePLSDstRead || this->onWillReadDstColor(caps, optimizations);
ethannicholas22793252016-01-30 09:59:10 -0800223}