| 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 "src/gpu/ganesh/GrXferProcessor.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkString.h" |
| 12 |
#include "include/gpu/ganesh/GrTypes.h" |
| 13 |
#include "src/gpu/KeyBuilder.h" |
| 14 |
#include "src/gpu/ganesh/GrCaps.h" |
| 15 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 16 |
#include "src/gpu/ganesh/effects/GrCustomXfermode.h" |
| 17 |
#include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h" |
| 18 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 19 |
|
| 20 |
#include <cstdint> |
| 21 |
|
| 22 |
enum class GrClampType; |
| 23 |
|
| 24 |
GrXferProcessor::GrXferProcessor(ClassID classID) |
| 25 |
: INHERITED(classID) |
| 26 |
, fWillReadDstColor(false) |
| 27 |
, fIsLCD(false) {} |
| 28 |
|
| 29 |
GrXferProcessor::GrXferProcessor(ClassID classID, bool willReadDstColor, |
| 30 |
GrProcessorAnalysisCoverage coverage) |
| 31 |
: INHERITED(classID) |
| 32 |
, fWillReadDstColor(willReadDstColor) |
| 33 |
, fIsLCD(GrProcessorAnalysisCoverage::kLCD == coverage) {} |
| 34 |
|
| 35 |
bool GrXferProcessor::hasSecondaryOutput() const { |
| 36 |
if (!this->willReadDstColor()) { |
| 37 |
return this->onHasSecondaryOutput(); |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
void GrXferProcessor::addToKey(const GrShaderCaps& caps, |
| 43 |
skgpu::KeyBuilder* b) const { |
| 44 |
b->addBool(this->willReadDstColor(), "willReadDstColor"); |
| 45 |
b->addBool(fIsLCD, "isLCD"); |
| 46 |
this->onAddToKey(caps, b); |
| 47 |
} |
| 48 |
|
| 49 |
/////////////////////////////////////////////////////////////////////////////// |
| 50 |
|
| 51 |
GrXPFactory::AnalysisProperties GrXPFactory::GetAnalysisProperties( |
| 52 |
const GrXPFactory* factory, |
| 53 |
const GrProcessorAnalysisColor& color, |
| 54 |
const GrProcessorAnalysisCoverage& coverage, |
| 55 |
const GrCaps& caps, |
| 56 |
GrClampType clampType) { |
| 57 |
AnalysisProperties result; |
| 58 |
if (factory) { |
| 59 |
result = factory->analysisProperties(color, coverage, caps, clampType); |
| 60 |
} else { |
| 61 |
result = GrPorterDuffXPFactory::SrcOverAnalysisProperties(color, coverage, caps, clampType); |
| 62 |
} |
| 63 |
if (coverage == GrProcessorAnalysisCoverage::kNone) { |
| 64 |
result |= AnalysisProperties::kCompatibleWithCoverageAsAlpha; |
| 65 |
} |
| 66 |
SkASSERT(!(result & AnalysisProperties::kRequiresDstTexture)); |
| 67 |
if ((result & AnalysisProperties::kReadsDstInShader) && |
| 68 |
!caps.shaderCaps()->fDstReadInShaderSupport) { |
| 69 |
result |= AnalysisProperties::kRequiresDstTexture | |
| 70 |
AnalysisProperties::kRequiresNonOverlappingDraws; |
| 71 |
} |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
sk_sp<const GrXferProcessor> GrXPFactory::MakeXferProcessor(const GrXPFactory* factory, |
| 76 |
const GrProcessorAnalysisColor& color, |
| 77 |
GrProcessorAnalysisCoverage coverage, |
| 78 |
const GrCaps& caps, |
| 79 |
GrClampType clampType) { |
| 80 |
if (factory) { |
| 81 |
return factory->makeXferProcessor(color, coverage, caps, clampType); |
| 82 |
} else { |
| 83 |
return GrPorterDuffXPFactory::MakeSrcOverXferProcessor(color, coverage, caps); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
const GrXPFactory* GrXPFactory::FromBlendMode(SkBlendMode mode) { |
| 88 |
if (SkBlendMode_AsCoeff(mode, nullptr, nullptr)) { |
| 89 |
const GrXPFactory* result = GrPorterDuffXPFactory::Get(mode); |
| 90 |
SkASSERT(result); |
| 91 |
return result; |
| 92 |
} |
| 93 |
|
| 94 |
SkASSERT(GrCustomXfermode::IsSupportedMode(mode)); |
| 95 |
return GrCustomXfermode::Get(mode); |
| 96 |
} |
| 97 |
|
| 98 |
////////////////////////////////////////////////////////////////////////////// |
| 99 |
|
| 100 |
using ProgramImpl = GrXferProcessor::ProgramImpl; |
| 101 |
|
| 102 |
// This is only called for cases where we are doing LCD coverage and not using in shader blending. |
| 103 |
// For these cases we assume the the src alpha is 1, thus we can just use the max for the alpha |
| 104 |
// coverage since src alpha will always be greater than or equal to dst alpha. |
| 105 |
static void adjust_for_lcd_coverage(GrGLSLXPFragmentBuilder* fragBuilder, |
| 106 |
const char* srcCoverage, |
| 107 |
const GrXferProcessor& proc) { |
| 108 |
if (srcCoverage && proc.isLCD()) { |
| 109 |
fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);", |
| 110 |
srcCoverage, |
| 111 |
srcCoverage, |
| 112 |
srcCoverage, |
| 113 |
srcCoverage); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void ProgramImpl::emitCode(const EmitArgs& args) { |
| 118 |
if (!args.fXP.willReadDstColor()) { |
| 119 |
adjust_for_lcd_coverage(args.fXPFragBuilder, args.fInputCoverage, args.fXP); |
| 120 |
this->emitOutputsForBlendState(args); |
| 121 |
} else { |
| 122 |
GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
| 123 |
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 124 |
const char* dstColor = fragBuilder->dstColor(); |
| 125 |
|
| 126 |
bool needsLocalOutColor = false; |
| 127 |
|
| 128 |
if (args.fDstTextureSamplerHandle.isValid()) { |
| 129 |
if (args.fInputCoverage) { |
| 130 |
// We don't think any shaders actually output negative coverage, but just as a |
| 131 |
// safety check for floating point precision errors, we compare with <= here. We |
| 132 |
// just check the RGB values of the coverage, since the alpha may not have been set |
| 133 |
// when using LCD. If we are using single-channel coverage, alpha will be equal to |
| 134 |
// RGB anyway. |
| 135 |
// |
| 136 |
// The discard here also helps for batching text-draws together, which need to read |
| 137 |
// from a dst copy for blends. However, this only helps the case where the outer |
| 138 |
// bounding boxes of each letter overlap and not two actually parts of the text. |
| 139 |
fragBuilder->codeAppendf("if (all(lessThanEqual(%s.rgb, half3(0)))) {" |
| 140 |
" discard;" |
| 141 |
"}", |
| 142 |
args.fInputCoverage); |
| 143 |
} |
| 144 |
} else { |
| 145 |
needsLocalOutColor = args.fShaderCaps->fRequiresLocalOutputColorForFBFetch; |
| 146 |
} |
| 147 |
|
| 148 |
const char* outColor = "_localColorOut"; |
| 149 |
if (!needsLocalOutColor) { |
| 150 |
outColor = args.fOutputPrimary; |
| 151 |
} else { |
| 152 |
fragBuilder->codeAppendf("half4 %s;", outColor); |
| 153 |
} |
| 154 |
|
| 155 |
this->emitBlendCodeForDstRead(fragBuilder, |
| 156 |
uniformHandler, |
| 157 |
args.fInputColor, |
| 158 |
args.fInputCoverage, |
| 159 |
dstColor, |
| 160 |
outColor, |
| 161 |
args.fOutputSecondary, |
| 162 |
args.fXP); |
| 163 |
if (needsLocalOutColor) { |
| 164 |
fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, outColor); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Swizzle the fragment shader outputs if necessary. |
| 169 |
this->emitWriteSwizzle(args.fXPFragBuilder, |
| 170 |
args.fWriteSwizzle, |
| 171 |
args.fOutputPrimary, |
| 172 |
args.fOutputSecondary); |
| 173 |
} |
| 174 |
|
| 175 |
void ProgramImpl::emitWriteSwizzle(GrGLSLXPFragmentBuilder* x, |
| 176 |
const skgpu::Swizzle& swizzle, |
| 177 |
const char* outColor, |
| 178 |
const char* outColorSecondary) const { |
| 179 |
if (skgpu::Swizzle::RGBA() != swizzle) { |
| 180 |
x->codeAppendf("%s = %s.%s;", outColor, outColor, swizzle.asString().c_str()); |
| 181 |
if (outColorSecondary) { |
| 182 |
x->codeAppendf("%s = %s.%s;", |
| 183 |
outColorSecondary, |
| 184 |
outColorSecondary, |
| 185 |
swizzle.asString().c_str()); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void ProgramImpl::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp) { |
| 191 |
this->onSetData(pdm, xp); |
| 192 |
} |
| 193 |
|
| 194 |
void ProgramImpl::DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder, |
| 195 |
const char* srcCoverage, |
| 196 |
const char* dstColor, |
| 197 |
const char* outColor, |
| 198 |
const char* outColorSecondary, |
| 199 |
const GrXferProcessor& proc) { |
| 200 |
if (srcCoverage) { |
| 201 |
if (proc.isLCD()) { |
| 202 |
fragBuilder->codeAppendf("half3 lerpRGB = mix(%s.aaa, %s.aaa, %s.rgb);", |
| 203 |
dstColor, |
| 204 |
outColor, |
| 205 |
srcCoverage); |
| 206 |
} |
| 207 |
fragBuilder->codeAppendf("%s = %s * %s + (half4(1.0) - %s) * %s;", |
| 208 |
outColor, |
| 209 |
srcCoverage, |
| 210 |
outColor, |
| 211 |
srcCoverage, |
| 212 |
dstColor); |
| 213 |
if (proc.isLCD()) { |
| 214 |
fragBuilder->codeAppendf("%s.a = max(max(lerpRGB.r, lerpRGB.b), lerpRGB.g);", outColor); |
| 215 |
} |
| 216 |
} |
| 217 |
} |