| 1 |
/* |
| 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 |
#include "src/gpu/ganesh/effects/GrBicubicEffect.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkString.h" |
| 14 |
#include "include/private/SkSLSampleUsage.h" |
| 15 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 16 |
#include "src/base/SkRandom.h" |
| 17 |
#include "src/core/SkSLTypeShared.h" |
| 18 |
#include "src/gpu/KeyBuilder.h" |
| 19 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 20 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 21 |
#include "src/gpu/ganesh/effects/GrMatrixEffect.h" |
| 22 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 23 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 24 |
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" |
| 25 |
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" |
| 26 |
#include "src/shaders/SkImageShader.h" |
| 27 |
#include "src/sksl/SkSLString.h" |
| 28 |
|
| 29 |
#include <cmath> |
| 30 |
#include <cstdint> |
| 31 |
#include <string> |
| 32 |
#include <utility> |
| 33 |
|
| 34 |
class GrBicubicEffect::Impl : public ProgramImpl { |
| 35 |
public: |
| 36 |
void emitCode(EmitArgs&) override; |
| 37 |
|
| 38 |
private: |
| 39 |
void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
| 40 |
|
| 41 |
SkCubicResampler fKernel = {-1, -1}; |
| 42 |
UniformHandle fCoefficientUni; |
| 43 |
}; |
| 44 |
|
| 45 |
void GrBicubicEffect::Impl::emitCode(EmitArgs& args) { |
| 46 |
const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>(); |
| 47 |
|
| 48 |
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 49 |
|
| 50 |
const char* coeffs; |
| 51 |
fCoefficientUni = args.fUniformHandler->addUniform(&args.fFp, kFragment_GrShaderFlag, |
| 52 |
SkSLType::kHalf4x4, "coefficients", &coeffs); |
| 53 |
// We determine our fractional offset (f) within the texel. We then snap coord to a texel |
| 54 |
// center. The snap prevents cases where the starting coords are near a texel boundary and |
| 55 |
// offsets with imperfect precision would cause us to skip/double hit a texel. |
| 56 |
// The use of "texel" above is somewhat abstract as we're sampling a child processor. It is |
| 57 |
// assumed the child processor represents something akin to a nearest neighbor sampled texture. |
| 58 |
if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) { |
| 59 |
fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);", args.fSampleCoord); |
| 60 |
fragBuilder->codeAppend("half2 f = half2(fract(coord));"); |
| 61 |
fragBuilder->codeAppend("coord += 0.5 - f;"); |
| 62 |
fragBuilder->codeAppendf("half4 wx = %s * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);", |
| 63 |
coeffs); |
| 64 |
fragBuilder->codeAppendf("half4 wy = %s * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);", |
| 65 |
coeffs); |
| 66 |
fragBuilder->codeAppend("half4 rowColors[4];"); |
| 67 |
for (int y = 0; y < 4; ++y) { |
| 68 |
for (int x = 0; x < 4; ++x) { |
| 69 |
auto coord = SkSL::String::printf("coord + float2(%d, %d)", x - 1, y - 1); |
| 70 |
auto childStr = this->invokeChild(0, args, coord); |
| 71 |
fragBuilder->codeAppendf("rowColors[%d] = %s;", x, childStr.c_str()); |
| 72 |
} |
| 73 |
fragBuilder->codeAppendf( |
| 74 |
"half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + " |
| 75 |
"wx.w * rowColors[3];", |
| 76 |
y); |
| 77 |
} |
| 78 |
fragBuilder->codeAppend( |
| 79 |
"half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;"); |
| 80 |
} else { |
| 81 |
const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y"; |
| 82 |
fragBuilder->codeAppendf("float coord = %s.%s - 0.5;", args.fSampleCoord, d); |
| 83 |
fragBuilder->codeAppend("half f = half(fract(coord));"); |
| 84 |
fragBuilder->codeAppend("coord += 0.5 - f;"); |
| 85 |
fragBuilder->codeAppend("half f2 = f * f;"); |
| 86 |
fragBuilder->codeAppendf("half4 w = %s * half4(1.0, f, f2, f2 * f);", coeffs); |
| 87 |
fragBuilder->codeAppend("half4 c[4];"); |
| 88 |
for (int i = 0; i < 4; ++i) { |
| 89 |
std::string coord; |
| 90 |
if (bicubicEffect.fDirection == Direction::kX) { |
| 91 |
coord = SkSL::String::printf("float2(coord + %d, %s.y)", i - 1, args.fSampleCoord); |
| 92 |
} else { |
| 93 |
coord = SkSL::String::printf("float2(%s.x, coord + %d)", args.fSampleCoord, i - 1); |
| 94 |
} |
| 95 |
auto childStr = this->invokeChild(0, args, coord); |
| 96 |
fragBuilder->codeAppendf("c[%d] = %s;", i, childStr.c_str()); |
| 97 |
} |
| 98 |
fragBuilder->codeAppend( |
| 99 |
"half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;"); |
| 100 |
} |
| 101 |
// Bicubic can send colors out of range, so clamp to get them back in (source) gamut. |
| 102 |
// The kind of clamp we have to do depends on the alpha type. |
| 103 |
switch (bicubicEffect.fClamp) { |
| 104 |
case Clamp::kUnpremul: |
| 105 |
fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);"); |
| 106 |
break; |
| 107 |
case Clamp::kPremul: |
| 108 |
fragBuilder->codeAppend("bicubicColor.a = saturate(bicubicColor.a);"); |
| 109 |
fragBuilder->codeAppend( |
| 110 |
"bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));"); |
| 111 |
break; |
| 112 |
} |
| 113 |
fragBuilder->codeAppendf("return bicubicColor;"); |
| 114 |
} |
| 115 |
|
| 116 |
void GrBicubicEffect::Impl::onSetData(const GrGLSLProgramDataManager& pdm, |
| 117 |
const GrFragmentProcessor& fp) { |
| 118 |
auto& bicubicEffect = fp.cast<GrBicubicEffect>(); |
| 119 |
|
| 120 |
if (fKernel.B != bicubicEffect.fKernel.B || fKernel.C != bicubicEffect.fKernel.C) { |
| 121 |
fKernel = bicubicEffect.fKernel; |
| 122 |
pdm.setSkM44(fCoefficientUni, SkImageShader::CubicResamplerMatrix(fKernel.B, fKernel.C)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 127 |
SkAlphaType alphaType, |
| 128 |
const SkMatrix& matrix, |
| 129 |
SkCubicResampler kernel, |
| 130 |
Direction direction) { |
| 131 |
auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I()); |
| 132 |
auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 133 |
return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 134 |
new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 135 |
} |
| 136 |
|
| 137 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 138 |
SkAlphaType alphaType, |
| 139 |
const SkMatrix& matrix, |
| 140 |
const GrSamplerState::WrapMode wrapX, |
| 141 |
const GrSamplerState::WrapMode wrapY, |
| 142 |
SkCubicResampler kernel, |
| 143 |
Direction direction, |
| 144 |
const GrCaps& caps) { |
| 145 |
GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 146 |
std::unique_ptr<GrFragmentProcessor> fp; |
| 147 |
fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps); |
| 148 |
auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 149 |
return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 150 |
new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 151 |
} |
| 152 |
|
| 153 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 154 |
GrSurfaceProxyView view, |
| 155 |
SkAlphaType alphaType, |
| 156 |
const SkMatrix& matrix, |
| 157 |
const GrSamplerState::WrapMode wrapX, |
| 158 |
const GrSamplerState::WrapMode wrapY, |
| 159 |
const SkRect& subset, |
| 160 |
SkCubicResampler kernel, |
| 161 |
Direction direction, |
| 162 |
const GrCaps& caps) { |
| 163 |
GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 164 |
std::unique_ptr<GrFragmentProcessor> fp; |
| 165 |
fp = GrTextureEffect::MakeSubset( |
| 166 |
std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps); |
| 167 |
auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 168 |
return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 169 |
new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 170 |
} |
| 171 |
|
| 172 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 173 |
GrSurfaceProxyView view, |
| 174 |
SkAlphaType alphaType, |
| 175 |
const SkMatrix& matrix, |
| 176 |
const GrSamplerState::WrapMode wrapX, |
| 177 |
const GrSamplerState::WrapMode wrapY, |
| 178 |
const SkRect& subset, |
| 179 |
const SkRect& domain, |
| 180 |
SkCubicResampler kernel, |
| 181 |
Direction direction, |
| 182 |
const GrCaps& caps) { |
| 183 |
auto lowerBound = [](float x) { return std::floor(x - 1.5f) + 0.5f; }; |
| 184 |
auto upperBound = [](float x) { return std::floor(x + 1.5f) - 0.5f; }; |
| 185 |
SkRect expandedDomain { |
| 186 |
lowerBound(domain.fLeft) , |
| 187 |
upperBound(domain.fRight) , |
| 188 |
lowerBound(domain.fTop) , |
| 189 |
upperBound(domain.fBottom) |
| 190 |
}; |
| 191 |
GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 192 |
std::unique_ptr<GrFragmentProcessor> fp; |
| 193 |
fp = GrTextureEffect::MakeSubset( |
| 194 |
std::move(view), alphaType, SkMatrix::I(), sampler, subset, expandedDomain, caps); |
| 195 |
auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 196 |
return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 197 |
new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 198 |
} |
| 199 |
|
| 200 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 201 |
SkAlphaType alphaType, |
| 202 |
const SkMatrix& matrix, |
| 203 |
SkCubicResampler kernel, |
| 204 |
Direction direction) { |
| 205 |
auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 206 |
return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 207 |
new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 208 |
} |
| 209 |
|
| 210 |
GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp, |
| 211 |
SkCubicResampler kernel, |
| 212 |
Direction direction, |
| 213 |
Clamp clamp) |
| 214 |
: INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get())) |
| 215 |
, fKernel(kernel) |
| 216 |
, fDirection(direction) |
| 217 |
, fClamp(clamp) { |
| 218 |
this->setUsesSampleCoordsDirectly(); |
| 219 |
this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit()); |
| 220 |
} |
| 221 |
|
| 222 |
GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that) |
| 223 |
: INHERITED(that) |
| 224 |
, fKernel(that.fKernel) |
| 225 |
, fDirection(that.fDirection) |
| 226 |
, fClamp(that.fClamp) {} |
| 227 |
|
| 228 |
void GrBicubicEffect::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const { |
| 229 |
uint32_t key = (static_cast<uint32_t>(fDirection) << 0) | (static_cast<uint32_t>(fClamp) << 2); |
| 230 |
b->add32(key); |
| 231 |
} |
| 232 |
|
| 233 |
std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrBicubicEffect::onMakeProgramImpl() const { |
| 234 |
return std::make_unique<Impl>(); |
| 235 |
} |
| 236 |
|
| 237 |
bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 238 |
const auto& that = other.cast<GrBicubicEffect>(); |
| 239 |
return fDirection == that.fDirection && |
| 240 |
fClamp == that.fClamp && |
| 241 |
fKernel.B == that.fKernel.B && |
| 242 |
fKernel.C == that.fKernel.C; |
| 243 |
} |
| 244 |
|
| 245 |
SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const { |
| 246 |
return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 247 |
} |
| 248 |
|
| 249 |
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect) |
| 250 |
|
| 251 |
#if defined(GPU_TEST_UTILS) |
| 252 |
std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { |
| 253 |
Direction direction = Direction::kX; |
| 254 |
switch (d->fRandom->nextULessThan(3)) { |
| 255 |
case 0: |
| 256 |
direction = Direction::kX; |
| 257 |
break; |
| 258 |
case 1: |
| 259 |
direction = Direction::kY; |
| 260 |
break; |
| 261 |
case 2: |
| 262 |
direction = Direction::kXY; |
| 263 |
break; |
| 264 |
} |
| 265 |
auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::gMitchell |
| 266 |
: GrBicubicEffect::gCatmullRom; |
| 267 |
auto m = GrTest::TestMatrix(d->fRandom); |
| 268 |
switch (d->fRandom->nextULessThan(3)) { |
| 269 |
case 0: { |
| 270 |
auto [view, ct, at] = d->randomView(); |
| 271 |
GrSamplerState::WrapMode wm[2]; |
| 272 |
GrTest::TestWrapModes(d->fRandom, wm); |
| 273 |
|
| 274 |
if (d->fRandom->nextBool()) { |
| 275 |
SkRect subset; |
| 276 |
subset.fLeft = d->fRandom->nextSScalar1() * view.width(); |
| 277 |
subset.fTop = d->fRandom->nextSScalar1() * view.height(); |
| 278 |
subset.fRight = d->fRandom->nextSScalar1() * view.width(); |
| 279 |
subset.fBottom = d->fRandom->nextSScalar1() * view.height(); |
| 280 |
subset.sort(); |
| 281 |
return MakeSubset(std::move(view), |
| 282 |
at, |
| 283 |
m, |
| 284 |
wm[0], |
| 285 |
wm[1], |
| 286 |
subset, |
| 287 |
kernel, |
| 288 |
direction, |
| 289 |
*d->caps()); |
| 290 |
} |
| 291 |
return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps()); |
| 292 |
} |
| 293 |
case 1: { |
| 294 |
auto [view, ct, at] = d->randomView(); |
| 295 |
return Make(std::move(view), at, m, kernel, direction); |
| 296 |
} |
| 297 |
default: { |
| 298 |
SkAlphaType at; |
| 299 |
do { |
| 300 |
at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1)); |
| 301 |
} while (at == kUnknown_SkAlphaType); |
| 302 |
return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
#endif |