bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 1 | /* |
| 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" |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 9 | #include "GrPipeline.h" |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 10 | #include "GrProcOptInfo.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 11 | #include "gl/GrGLCaps.h" |
| 12 | |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 13 | GrXferProcessor::GrXferProcessor() |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 14 | : fWillReadDstColor(false) |
| 15 | , fDstReadUsesMixedSamples(false) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 16 | , fDstTextureOffset() { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 17 | } |
| 18 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 19 | GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture, |
| 20 | bool willReadDstColor, |
| 21 | bool hasMixedSamples) |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 22 | : fWillReadDstColor(willReadDstColor) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 23 | , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples) |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 24 | , fDstTextureOffset() { |
| 25 | if (dstTexture && dstTexture->texture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 26 | SkASSERT(willReadDstColor); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 27 | fDstTexture.reset(dstTexture->texture()); |
| 28 | fDstTextureOffset = dstTexture->offset(); |
| 29 | this->addTextureAccess(&fDstTexture); |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 30 | this->setWillReadFragmentPosition(); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 34 | GrXferProcessor::OptFlags GrXferProcessor::getOptimizations( |
| 35 | const GrPipelineOptimizations& optimizations, |
| 36 | bool doesStencilWrite, |
| 37 | GrColor* overrideColor, |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 38 | const GrCaps& caps) const { |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 39 | GrXferProcessor::OptFlags flags = this->onGetOptimizations(optimizations, |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 40 | doesStencilWrite, |
| 41 | overrideColor, |
| 42 | caps); |
| 43 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 44 | if (this->willReadDstColor()) { |
| 45 | // When performing a dst read we handle coverage in the base class. |
| 46 | SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag)); |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 47 | if (optimizations.fCoveragePOI.isSolidWhite()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 48 | flags |= GrXferProcessor::kIgnoreCoverage_OptFlag; |
| 49 | } |
| 50 | } |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 51 | return flags; |
| 52 | } |
| 53 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 54 | bool GrXferProcessor::hasSecondaryOutput() const { |
| 55 | if (!this->willReadDstColor()) { |
| 56 | return this->onHasSecondaryOutput(); |
| 57 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 58 | return this->dstReadUsesMixedSamples(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const { |
| 62 | blendInfo->reset(); |
| 63 | if (!this->willReadDstColor()) { |
| 64 | this->onGetBlendInfo(blendInfo); |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 65 | } else if (this->dstReadUsesMixedSamples()) { |
| 66 | blendInfo->fDstBlend = kIS2A_GrBlendCoeff; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 70 | void GrXferProcessor::getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 71 | uint32_t key = this->willReadDstColor() ? 0x1 : 0x0; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 72 | if (key) { |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 73 | if (const GrTexture* dstTexture = this->getDstTexture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 74 | key |= 0x2; |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 75 | if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) { |
| 76 | key |= 0x4; |
| 77 | } |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 78 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 79 | if (this->dstReadUsesMixedSamples()) { |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 80 | key |= 0x8; |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 81 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 82 | } |
| 83 | b->add32(key); |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 84 | this->onGetGLSLProcessorKey(caps, b); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 87 | GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt, |
| 88 | const GrCaps& caps) const { |
| 89 | SkASSERT(rt); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 90 | if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) { |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 91 | // Texture barriers are required when a shader reads and renders to the same texture. |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 92 | SkASSERT(caps.textureBarrierSupport()); |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 93 | return kTexture_GrXferBarrierType; |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 94 | } |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 95 | return this->onXferBarrier(rt, caps); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 98 | #ifdef SK_DEBUG |
| 99 | static const char* equation_string(GrBlendEquation eq) { |
| 100 | switch (eq) { |
| 101 | case kAdd_GrBlendEquation: |
| 102 | return "add"; |
| 103 | case kSubtract_GrBlendEquation: |
| 104 | return "subtract"; |
| 105 | case kReverseSubtract_GrBlendEquation: |
| 106 | return "reverse_subtract"; |
| 107 | case kScreen_GrBlendEquation: |
| 108 | return "screen"; |
| 109 | case kOverlay_GrBlendEquation: |
| 110 | return "overlay"; |
| 111 | case kDarken_GrBlendEquation: |
| 112 | return "darken"; |
| 113 | case kLighten_GrBlendEquation: |
| 114 | return "lighten"; |
| 115 | case kColorDodge_GrBlendEquation: |
| 116 | return "color_dodge"; |
| 117 | case kColorBurn_GrBlendEquation: |
| 118 | return "color_burn"; |
| 119 | case kHardLight_GrBlendEquation: |
| 120 | return "hard_light"; |
| 121 | case kSoftLight_GrBlendEquation: |
| 122 | return "soft_light"; |
| 123 | case kDifference_GrBlendEquation: |
| 124 | return "difference"; |
| 125 | case kExclusion_GrBlendEquation: |
| 126 | return "exclusion"; |
| 127 | case kMultiply_GrBlendEquation: |
| 128 | return "multiply"; |
| 129 | case kHSLHue_GrBlendEquation: |
| 130 | return "hsl_hue"; |
| 131 | case kHSLSaturation_GrBlendEquation: |
| 132 | return "hsl_saturation"; |
| 133 | case kHSLColor_GrBlendEquation: |
| 134 | return "hsl_color"; |
| 135 | case kHSLLuminosity_GrBlendEquation: |
| 136 | return "hsl_luminosity"; |
| 137 | }; |
| 138 | return ""; |
| 139 | } |
| 140 | |
| 141 | static const char* coeff_string(GrBlendCoeff coeff) { |
| 142 | switch (coeff) { |
| 143 | case kZero_GrBlendCoeff: |
| 144 | return "zero"; |
| 145 | case kOne_GrBlendCoeff: |
| 146 | return "one"; |
| 147 | case kSC_GrBlendCoeff: |
| 148 | return "src_color"; |
| 149 | case kISC_GrBlendCoeff: |
| 150 | return "inv_src_color"; |
| 151 | case kDC_GrBlendCoeff: |
| 152 | return "dst_color"; |
| 153 | case kIDC_GrBlendCoeff: |
| 154 | return "inv_dst_color"; |
| 155 | case kSA_GrBlendCoeff: |
| 156 | return "src_alpha"; |
| 157 | case kISA_GrBlendCoeff: |
| 158 | return "inv_src_alpha"; |
| 159 | case kDA_GrBlendCoeff: |
| 160 | return "dst_alpha"; |
| 161 | case kIDA_GrBlendCoeff: |
| 162 | return "inv_dst_alpha"; |
| 163 | case kConstC_GrBlendCoeff: |
| 164 | return "const_color"; |
| 165 | case kIConstC_GrBlendCoeff: |
| 166 | return "inv_const_color"; |
| 167 | case kConstA_GrBlendCoeff: |
| 168 | return "const_alpha"; |
| 169 | case kIConstA_GrBlendCoeff: |
| 170 | return "inv_const_alpha"; |
| 171 | case kS2C_GrBlendCoeff: |
| 172 | return "src2_color"; |
| 173 | case kIS2C_GrBlendCoeff: |
| 174 | return "inv_src2_color"; |
| 175 | case kS2A_GrBlendCoeff: |
| 176 | return "src2_alpha"; |
| 177 | case kIS2A_GrBlendCoeff: |
| 178 | return "inv_src2_alpha"; |
| 179 | } |
| 180 | return ""; |
| 181 | } |
| 182 | |
| 183 | SkString GrXferProcessor::BlendInfo::dump() const { |
| 184 | SkString out; |
| 185 | out.printf("write_color(%d) equation(%s) src_coeff(%s) dst_coeff:(%s) const(0x%08x)", |
| 186 | fWriteColor, equation_string(fEquation), coeff_string(fSrcBlend), |
| 187 | coeff_string(fDstBlend), fBlendConstant); |
| 188 | return out; |
| 189 | } |
| 190 | #endif |
| 191 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 194 | GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineOptimizations& optimizations, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 195 | bool hasMixedSamples, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 196 | const DstTexture* dstTexture, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 197 | const GrCaps& caps) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 198 | #ifdef SK_DEBUG |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 199 | if (this->willReadDstColor(caps, optimizations)) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 200 | if (!caps.shaderCaps()->dstReadInShaderSupport()) { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 201 | SkASSERT(dstTexture && dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 202 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 203 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 204 | } |
| 205 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 206 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 207 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 208 | SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 209 | #endif |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 210 | return this->onCreateXferProcessor(caps, optimizations, hasMixedSamples, dstTexture); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 211 | } |
| 212 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 213 | bool GrXPFactory::willNeedDstTexture(const GrCaps& caps, |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 214 | const GrPipelineOptimizations& optimizations) const { |
| 215 | return (this->willReadDstColor(caps, optimizations) && |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 216 | !caps.shaderCaps()->dstReadInShaderSupport()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 217 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 218 | |
| 219 | bool GrXPFactory::willReadDstColor(const GrCaps& caps, |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 220 | const GrPipelineOptimizations& optimizations) const { |
| 221 | return optimizations.fOverrides.fUsePLSDstRead || this->onWillReadDstColor(caps, optimizations); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 222 | } |