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 | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 10 | #include "GrPipelineBuilder.h" |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 11 | #include "GrProcOptInfo.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 12 | #include "gl/GrGLCaps.h" |
| 13 | |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 14 | GrXferProcessor::GrXferProcessor() |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 15 | : fWillReadDstColor(false) |
| 16 | , fDstReadUsesMixedSamples(false) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 17 | , fDstTextureOffset() { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 18 | } |
| 19 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 20 | GrXferProcessor::GrXferProcessor(const DstTexture* dstTexture, |
| 21 | bool willReadDstColor, |
| 22 | bool hasMixedSamples) |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 23 | : fWillReadDstColor(willReadDstColor) |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 24 | , fDstReadUsesMixedSamples(willReadDstColor && hasMixedSamples) |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 25 | , fDstTextureOffset() { |
| 26 | if (dstTexture && dstTexture->texture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 27 | SkASSERT(willReadDstColor); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 28 | fDstTexture.reset(dstTexture->texture()); |
| 29 | fDstTextureOffset = dstTexture->offset(); |
| 30 | this->addTextureAccess(&fDstTexture); |
egdaniel | 060a52c | 2015-04-07 07:31:11 -0700 | [diff] [blame] | 31 | this->setWillReadFragmentPosition(); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 35 | GrXferProcessor::OptFlags GrXferProcessor::getOptimizations( |
| 36 | const GrPipelineOptimizations& optimizations, |
| 37 | bool doesStencilWrite, |
| 38 | GrColor* overrideColor, |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 39 | const GrCaps& caps) const { |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 40 | GrXferProcessor::OptFlags flags = this->onGetOptimizations(optimizations, |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 41 | doesStencilWrite, |
| 42 | overrideColor, |
| 43 | caps); |
| 44 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 45 | if (this->willReadDstColor()) { |
| 46 | // When performing a dst read we handle coverage in the base class. |
| 47 | SkASSERT(!(flags & GrXferProcessor::kIgnoreCoverage_OptFlag)); |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 48 | if (optimizations.fCoveragePOI.isSolidWhite()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 49 | flags |= GrXferProcessor::kIgnoreCoverage_OptFlag; |
| 50 | } |
| 51 | } |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 52 | return flags; |
| 53 | } |
| 54 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 55 | bool GrXferProcessor::hasSecondaryOutput() const { |
| 56 | if (!this->willReadDstColor()) { |
| 57 | return this->onHasSecondaryOutput(); |
| 58 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 59 | return this->dstReadUsesMixedSamples(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void GrXferProcessor::getBlendInfo(BlendInfo* blendInfo) const { |
| 63 | blendInfo->reset(); |
| 64 | if (!this->willReadDstColor()) { |
| 65 | this->onGetBlendInfo(blendInfo); |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 66 | } else if (this->dstReadUsesMixedSamples()) { |
| 67 | blendInfo->fDstBlend = kIS2A_GrBlendCoeff; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 71 | void GrXferProcessor::getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 72 | uint32_t key = this->willReadDstColor() ? 0x1 : 0x0; |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 73 | if (key) { |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 74 | if (const GrTexture* dstTexture = this->getDstTexture()) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 75 | key |= 0x2; |
cdalton | 827bae1 | 2015-06-08 13:43:33 -0700 | [diff] [blame] | 76 | if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) { |
| 77 | key |= 0x4; |
| 78 | } |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 79 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 80 | if (this->dstReadUsesMixedSamples()) { |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 81 | key |= 0x8; |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 82 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 83 | } |
| 84 | b->add32(key); |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 85 | this->onGetGLSLProcessorKey(caps, b); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 88 | GrXferBarrierType GrXferProcessor::xferBarrierType(const GrRenderTarget* rt, |
| 89 | const GrCaps& caps) const { |
| 90 | SkASSERT(rt); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 91 | if (static_cast<const GrSurface*>(rt) == this->getDstTexture()) { |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 92 | // 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] | 93 | SkASSERT(caps.textureBarrierSupport()); |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 94 | return kTexture_GrXferBarrierType; |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 95 | } |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 96 | return this->onXferBarrier(rt, caps); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 99 | #ifdef SK_DEBUG |
| 100 | static 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 | |
| 142 | static 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 | |
| 184 | SkString 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 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 195 | GrXferProcessor* GrXPFactory::createXferProcessor(const GrPipelineOptimizations& optimizations, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 196 | bool hasMixedSamples, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 197 | const DstTexture* dstTexture, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 198 | const GrCaps& caps) const { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 199 | #ifdef SK_DEBUG |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame^] | 200 | if (this->willReadDstColor(caps, optimizations)) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 201 | if (!caps.shaderCaps()->dstReadInShaderSupport()) { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 202 | SkASSERT(dstTexture && dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 203 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 204 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 205 | } |
| 206 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 207 | SkASSERT(!dstTexture || !dstTexture->texture()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 208 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 209 | SkASSERT(!hasMixedSamples || caps.shaderCaps()->dualSourceBlendingSupport()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 210 | #endif |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 211 | return this->onCreateXferProcessor(caps, optimizations, hasMixedSamples, dstTexture); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 212 | } |
| 213 | |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 214 | bool GrXPFactory::willNeedDstTexture(const GrCaps& caps, |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame^] | 215 | const GrPipelineOptimizations& optimizations) const { |
| 216 | return (this->willReadDstColor(caps, optimizations) && |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 217 | !caps.shaderCaps()->dstReadInShaderSupport()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 218 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 219 | |
| 220 | bool GrXPFactory::willReadDstColor(const GrCaps& caps, |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame^] | 221 | const GrPipelineOptimizations& optimizations) const { |
| 222 | return optimizations.fOverrides.fUsePLSDstRead || this->onWillReadDstColor(caps, optimizations); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 223 | } |