| 1 |
/* |
| 2 |
* Copyright 2019 Google LLC. |
| 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkFont.h" |
| 11 |
#include "include/effects/SkRuntimeEffect.h" |
| 12 |
#include "src/core/SkCanvasPriv.h" |
| 13 |
#include "src/gpu/ganesh/GrCanvas.h" |
| 14 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 15 |
#include "src/gpu/ganesh/GrPaint.h" |
| 16 |
#include "src/gpu/ganesh/SkGr.h" |
| 17 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 18 |
#include "src/gpu/ganesh/effects/GrMatrixEffect.h" |
| 19 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 20 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 21 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
#include "tools/fonts/FontToolUtils.h" |
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
// Samples child with a uniform matrix (functionally identical to GrMatrixEffect) |
| 28 |
// Scales along Y |
| 29 |
class UniformMatrixEffect : public GrFragmentProcessor { |
| 30 |
public: |
| 31 |
inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 4; |
| 32 |
|
| 33 |
UniformMatrixEffect(std::unique_ptr<GrFragmentProcessor> child) |
| 34 |
: GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) { |
| 35 |
this->registerChild(std::move(child), |
| 36 |
SkSL::SampleUsage::UniformMatrix(/*hasPerspective=*/false)); |
| 37 |
} |
| 38 |
|
| 39 |
const char* name() const override { return "UniformMatrixEffect"; } |
| 40 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
| 41 |
bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
| 42 |
std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; } |
| 43 |
|
| 44 |
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
| 45 |
class Impl : public ProgramImpl { |
| 46 |
public: |
| 47 |
void emitCode(EmitArgs& args) override { |
| 48 |
fMatrixVar = |
| 49 |
args.fUniformHandler->addUniform(&args.fFp, |
| 50 |
kFragment_GrShaderFlag, |
| 51 |
SkSLType::kFloat3x3, |
| 52 |
SkSL::SampleUsage::MatrixUniformName()); |
| 53 |
SkString sample = this->invokeChildWithMatrix(0, args); |
| 54 |
args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
| 55 |
} |
| 56 |
|
| 57 |
private: |
| 58 |
void onSetData(const GrGLSLProgramDataManager& pdman, |
| 59 |
const GrFragmentProcessor& proc) override { |
| 60 |
pdman.setSkMatrix(fMatrixVar, SkMatrix::Scale(1, 0.5f)); |
| 61 |
} |
| 62 |
UniformHandle fMatrixVar; |
| 63 |
}; |
| 64 |
return std::make_unique<Impl>(); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
// Samples child with explicit coords |
| 69 |
// Translates along Y |
| 70 |
class ExplicitCoordEffect : public GrFragmentProcessor { |
| 71 |
public: |
| 72 |
inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 6; |
| 73 |
|
| 74 |
ExplicitCoordEffect(std::unique_ptr<GrFragmentProcessor> child) |
| 75 |
: GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) { |
| 76 |
this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
| 77 |
this->setUsesSampleCoordsDirectly(); |
| 78 |
} |
| 79 |
|
| 80 |
const char* name() const override { return "ExplicitCoordEffect"; } |
| 81 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
| 82 |
bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
| 83 |
std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; } |
| 84 |
|
| 85 |
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
| 86 |
class Impl : public ProgramImpl { |
| 87 |
public: |
| 88 |
void emitCode(EmitArgs& args) override { |
| 89 |
args.fFragBuilder->codeAppendf("float2 coord = %s + float2(0, 8);", |
| 90 |
args.fSampleCoord); |
| 91 |
SkString sample = this->invokeChild(0, args, "coord"); |
| 92 |
args.fFragBuilder->codeAppendf("return %s;\n", sample.c_str()); |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
return std::make_unique<Impl>(); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
// Generates test pattern |
| 101 |
class TestPatternEffect : public GrFragmentProcessor { |
| 102 |
public: |
| 103 |
inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 7; |
| 104 |
|
| 105 |
TestPatternEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) { |
| 106 |
this->setUsesSampleCoordsDirectly(); |
| 107 |
} |
| 108 |
|
| 109 |
const char* name() const override { return "TestPatternEffect"; } |
| 110 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
| 111 |
bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; } |
| 112 |
std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; } |
| 113 |
|
| 114 |
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
| 115 |
class Impl : public ProgramImpl { |
| 116 |
public: |
| 117 |
void emitCode(EmitArgs& args) override { |
| 118 |
auto fb = args.fFragBuilder; |
| 119 |
fb->codeAppendf("float2 coord = %s / 64.0;", args.fSampleCoord); |
| 120 |
fb->codeAppendf("coord = floor(coord * 4) / 3;"); |
| 121 |
fb->codeAppendf("return half2(coord).rg01;\n"); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
return std::make_unique<Impl>(); |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
SkBitmap make_test_bitmap() { |
| 130 |
SkBitmap bitmap; |
| 131 |
bitmap.allocN32Pixels(64, 64); |
| 132 |
SkCanvas canvas(bitmap); |
| 133 |
|
| 134 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 135 |
const char* alpha = "ABCDEFGHIJKLMNOP"; |
| 136 |
|
| 137 |
for (int i = 0; i < 16; ++i) { |
| 138 |
int tx = i % 4, |
| 139 |
ty = i / 4; |
| 140 |
int x = tx * 16, |
| 141 |
y = ty * 16; |
| 142 |
SkPaint paint; |
| 143 |
paint.setColor4f({ tx / 3.0f, ty / 3.0f, 0.0f, 1.0f }); |
| 144 |
canvas.drawRect(SkRect::MakeXYWH(x, y, 16, 16), paint); |
| 145 |
paint.setColor4f({ (3-tx) / 3.0f, (3-ty)/3.0f, 1.0f, 1.0f }); |
| 146 |
canvas.drawSimpleText(alpha + i, 1, SkTextEncoding::kUTF8, x + 3, y + 13, font, paint); |
| 147 |
} |
| 148 |
|
| 149 |
return bitmap; |
| 150 |
} |
| 151 |
|
| 152 |
enum EffectType { |
| 153 |
kUniform, |
| 154 |
kExplicit, |
| 155 |
kDevice, |
| 156 |
}; |
| 157 |
|
| 158 |
static std::unique_ptr<GrFragmentProcessor> wrap(std::unique_ptr<GrFragmentProcessor> fp, |
| 159 |
EffectType effectType, |
| 160 |
int drawX, int drawY) { |
| 161 |
switch (effectType) { |
| 162 |
case kUniform: |
| 163 |
return std::make_unique<UniformMatrixEffect>(std::move(fp)); |
| 164 |
case kExplicit: |
| 165 |
return std::make_unique<ExplicitCoordEffect>(std::move(fp)); |
| 166 |
case kDevice: |
| 167 |
// Subtract out upper-left corner of draw so that device is effectively identity. |
| 168 |
fp = GrMatrixEffect::Make(SkMatrix::Translate(-drawX, -drawY), std::move(fp)); |
| 169 |
return GrFragmentProcessor::DeviceSpace(std::move(fp)); |
| 170 |
} |
| 171 |
SkUNREACHABLE; |
| 172 |
} |
| 173 |
|
| 174 |
} // namespace |
| 175 |
|
| 176 |
namespace skiagm { |
| 177 |
|
| 178 |
DEF_SIMPLE_GPU_GM_CAN_FAIL(fp_sample_chaining, rContext, canvas, errorMsg, 232, 306) { |
| 179 |
auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); |
| 180 |
if (!sdc) { |
| 181 |
*errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly; |
| 182 |
return DrawResult::kSkip; |
| 183 |
} |
| 184 |
|
| 185 |
SkBitmap bmp = make_test_bitmap(); |
| 186 |
|
| 187 |
int x = 10, y = 10; |
| 188 |
|
| 189 |
auto nextCol = [&] { x += (64 + 10); }; |
| 190 |
auto nextRow = [&] { x = 10; y += (64 + 10); }; |
| 191 |
|
| 192 |
auto draw = [&](std::initializer_list<EffectType> effects) { |
| 193 |
// Enable TestPatternEffect to get a fully procedural inner effect. It's not quite as nice |
| 194 |
// visually (no text labels in each box), but it avoids the extra GrMatrixEffect. |
| 195 |
// Switching it on actually triggers *more* shader compilation failures. |
| 196 |
#if 0 |
| 197 |
auto fp = std::unique_ptr<GrFragmentProcessor>(new TestPatternEffect()); |
| 198 |
#else |
| 199 |
auto view = std::get<0>(GrMakeCachedBitmapProxyView(rContext, |
| 200 |
GrMippedBitmap(bmp), |
| 201 |
/*label=*/"FpSampleChaining", |
| 202 |
skgpu::Mipmapped::kNo)); |
| 203 |
auto fp = GrTextureEffect::Make(std::move(view), bmp.alphaType()); |
| 204 |
#endif |
| 205 |
for (EffectType effectType : effects) { |
| 206 |
fp = wrap(std::move(fp), effectType, x, y); |
| 207 |
} |
| 208 |
GrPaint paint; |
| 209 |
paint.setColorFragmentProcessor(std::move(fp)); |
| 210 |
sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(x, y), |
| 211 |
SkRect::MakeIWH(64, 64)); |
| 212 |
nextCol(); |
| 213 |
}; |
| 214 |
|
| 215 |
// Reminder, in every case, the chain is more complicated than it seems, because the |
| 216 |
// GrTextureEffect is wrapped in a GrMatrixEffect, which is subject to the same bugs that |
| 217 |
// we're testing (particularly the bug about owner/base in UniformMatrixEffect). |
| 218 |
|
| 219 |
// First row: no transform, then each one independently applied |
| 220 |
draw({}); // Identity (4 rows and columns) |
| 221 |
draw({ kUniform }); // Scale Y axis by 2x (2 visible rows) |
| 222 |
draw({ kExplicit }); // Translate up by 8px |
| 223 |
nextRow(); |
| 224 |
|
| 225 |
// Second row: transform duplicated |
| 226 |
draw({ kUniform, kUniform }); // Scale Y axis by 4x (1 visible row) |
| 227 |
draw({ kExplicit, kExplicit }); // Translate up by 16px |
| 228 |
nextRow(); |
| 229 |
|
| 230 |
// Third row: Remember, these are applied inside out: |
| 231 |
draw({ kUniform, kExplicit }); // Scale Y by 2x and translate up by 8px |
| 232 |
draw({ kExplicit, kUniform }); // Scale Y by 2x and translate up by 16px |
| 233 |
nextRow(); |
| 234 |
|
| 235 |
// Fourth row: device space. |
| 236 |
draw({ kDevice, kUniform }); // Same as identity (uniform applied *before* |
| 237 |
// device so ignored). |
| 238 |
draw({ kExplicit, kUniform, kDevice }); // Scale Y by 2x and translate up by 16px |
| 239 |
draw({ kDevice, kExplicit, kUniform, kDevice }); // Identity, again. |
| 240 |
|
| 241 |
return DrawResult::kOk; |
| 242 |
} |
| 243 |
|
| 244 |
} // namespace skiagm |