| 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/SkCanvas.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/core/SkSurface.h" |
| 17 |
#include "include/effects/SkGradient.h" |
| 18 |
#include "include/effects/SkImageFilters.h" |
| 19 |
#include "include/effects/SkRuntimeEffect.h" |
| 20 |
#include "src/base/SkRandom.h" |
| 21 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 22 |
#include "tools/Resources.h" |
| 23 |
#include "tools/ToolUtils.h" |
| 24 |
#include "tools/fonts/FontToolUtils.h" |
| 25 |
|
| 26 |
#if defined(SK_GANESH) |
| 27 |
#include "src/gpu/ganesh/GrCaps.h" |
| 28 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
static constexpr int kBoxSize = 100; |
| 32 |
static constexpr int kPadding = 5; |
| 33 |
static constexpr int kLabelHeight = 15; |
| 34 |
|
| 35 |
static void next_column(SkCanvas* canvas) { |
| 36 |
canvas->translate(kBoxSize + kPadding, 0); |
| 37 |
} |
| 38 |
|
| 39 |
static void next_row(SkCanvas* canvas) { |
| 40 |
canvas->restore(); |
| 41 |
canvas->translate(0, kBoxSize + kPadding + kLabelHeight); |
| 42 |
canvas->save(); |
| 43 |
} |
| 44 |
|
| 45 |
static constexpr int columns_to_width(int columns) { |
| 46 |
return (kPadding + kBoxSize) * columns + kPadding; |
| 47 |
} |
| 48 |
|
| 49 |
static constexpr int rows_to_height(int rows) { |
| 50 |
return (kPadding + kLabelHeight + kBoxSize) * rows + kPadding; |
| 51 |
} |
| 52 |
|
| 53 |
static void draw_label(SkCanvas* canvas, const char* label) { |
| 54 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 55 |
SkPaint p(SkColors::kBlack); |
| 56 |
SkRect bounds; |
| 57 |
font.measureText(label, strlen(label), SkTextEncoding::kUTF8, &bounds); |
| 58 |
|
| 59 |
canvas->drawSimpleText(label, strlen(label), SkTextEncoding::kUTF8, |
| 60 |
(kBoxSize - bounds.width()) * 0.5f, |
| 61 |
(kLabelHeight + bounds.height()) * 0.5f, font, p); |
| 62 |
canvas->translate(0, kLabelHeight); |
| 63 |
} |
| 64 |
|
| 65 |
static SkBitmap draw_shader(SkCanvas* canvas, sk_sp<SkShader> shader, |
| 66 |
bool allowRasterFallback = true) { |
| 67 |
SkPaint paint; |
| 68 |
paint.setShader(std::move(shader)); |
| 69 |
|
| 70 |
SkBitmap bitmap; |
| 71 |
SkImageInfo info = SkImageInfo::MakeN32Premul({kBoxSize, kBoxSize}); |
| 72 |
auto surface = canvas->makeSurface(info); |
| 73 |
if (allowRasterFallback && !surface) { |
| 74 |
surface = SkSurfaces::Raster(info); |
| 75 |
} |
| 76 |
|
| 77 |
if (surface) { |
| 78 |
surface->getCanvas()->clear(SK_ColorWHITE); |
| 79 |
surface->getCanvas()->scale(kBoxSize, kBoxSize); |
| 80 |
surface->getCanvas()->drawRect({0, 0, 1, 1}, paint); |
| 81 |
|
| 82 |
bitmap.allocPixels(info); |
| 83 |
surface->readPixels(bitmap, 0, 0); |
| 84 |
|
| 85 |
canvas->drawImage(bitmap.asImage(), 0, 0); |
| 86 |
} |
| 87 |
return bitmap; |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
Test cases are inserted into the middle of this shader. The pasted expression is expected to |
| 92 |
produce a single float. It can reference: |
| 93 |
|
| 94 |
'x' : float in [xMin, xMax] |
| 95 |
'xi' : int in [xMin, xMax] |
| 96 |
'p' : float2 in [xMin, xMax] Lerps from (xMax, xMin) to (xMin, xMax) |
| 97 |
'pi' : int2 in [xMin, xMax] Lerps from (xMax, xMin) to (xMin, xMax) |
| 98 |
(helpful for intrinsics with a mix of scalar/vector params) |
| 99 |
'v1' : float2(1) |
| 100 |
'v2' : float2(2) |
| 101 |
*/ |
| 102 |
static SkString make_unary_sksl_1d(const char* fn, bool requireES3) { |
| 103 |
return SkStringPrintf( |
| 104 |
"#version %s\n" |
| 105 |
"uniform float xScale; uniform float xBias;" |
| 106 |
"uniform float yScale; uniform float yBias;" |
| 107 |
"half4 main(float2 p) {" |
| 108 |
" const float2 v1 = float2(1);" |
| 109 |
" const float2 v2 = float2(2);" |
| 110 |
" p = float2(p.x, 1 - p.x) * xScale + xBias;" |
| 111 |
" float x = p.x;" |
| 112 |
" int2 pi = int2(floor(p));" |
| 113 |
" int xi = pi.x;" |
| 114 |
" float y = float(%s) * yScale + yBias;" |
| 115 |
" return y.xxx1;" |
| 116 |
"}", |
| 117 |
requireES3 ? "300" : "100", fn); |
| 118 |
} |
| 119 |
|
| 120 |
// Draws one row of boxes, then advances the canvas translation vertically |
| 121 |
static void plot(SkCanvas* canvas, |
| 122 |
const char* fn, |
| 123 |
float xMin, |
| 124 |
float xMax, |
| 125 |
float yMin, |
| 126 |
float yMax, |
| 127 |
const char* label = nullptr, |
| 128 |
bool requireES3 = false) { |
| 129 |
canvas->save(); |
| 130 |
|
| 131 |
draw_label(canvas, label ? label : fn); |
| 132 |
|
| 133 |
auto [effect, error] = SkRuntimeEffect::MakeForShader(make_unary_sksl_1d(fn, requireES3)); |
| 134 |
if (!effect) { |
| 135 |
SkDebugf("Error: %s\n", error.c_str()); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
SkRuntimeShaderBuilder builder(effect); |
| 140 |
builder.uniform("xScale") = xMax - xMin; |
| 141 |
builder.uniform("xBias") = xMin; |
| 142 |
builder.uniform("yScale") = 1.0f / (yMax - yMin); |
| 143 |
builder.uniform("yBias") = -yMin / (yMax - yMin); |
| 144 |
|
| 145 |
SkBitmap bitmap = |
| 146 |
draw_shader(canvas, builder.makeShader(), /*allowRasterFallback=*/!requireES3); |
| 147 |
if (!bitmap.empty()) { |
| 148 |
// Plot. |
| 149 |
SkPaint plotPaint({ 0.0f, 0.5f, 0.0f, 1.0f }); |
| 150 |
SkPoint pts[kBoxSize]; |
| 151 |
for (int x = 0; x < kBoxSize; ++x) { |
| 152 |
SkColor c = bitmap.getColor(x, 0); |
| 153 |
SkScalar y = (1 - (SkColorGetR(c) / 255.0f)) * kBoxSize; |
| 154 |
pts[x].set(x + 0.5f, y); |
| 155 |
} |
| 156 |
plotPaint.setAntiAlias(true); |
| 157 |
canvas->drawPoints(SkCanvas::kPolygon_PointMode, pts, plotPaint); |
| 158 |
} |
| 159 |
|
| 160 |
canvas->restore(); |
| 161 |
next_column(canvas); |
| 162 |
} |
| 163 |
|
| 164 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.1 |
| 165 |
DEF_SIMPLE_GM(runtime_intrinsics_trig, |
| 166 |
canvas, |
| 167 |
columns_to_width(3), |
| 168 |
rows_to_height(5)) { |
| 169 |
const float kPI = SK_FloatPI, kTwoPI = 2 * SK_FloatPI, kPIOverTwo = SK_FloatPI / 2; |
| 170 |
|
| 171 |
canvas->translate(kPadding, kPadding); |
| 172 |
canvas->save(); |
| 173 |
|
| 174 |
plot(canvas, "radians(x)", 0.0f, 360.0f, 0.0f, kTwoPI); |
| 175 |
plot(canvas, "degrees(x)", 0.0f, kTwoPI, 0.0f, 360.0f); |
| 176 |
next_row(canvas); |
| 177 |
|
| 178 |
plot(canvas, "sin(x)", 0.0f, kTwoPI, -1.0f, 1.0f); |
| 179 |
plot(canvas, "cos(x)", 0.0f, kTwoPI, -1.0f, 1.0f); |
| 180 |
plot(canvas, "tan(x)", 0.0f, kPI, -10.0f, 10.0f); |
| 181 |
next_row(canvas); |
| 182 |
|
| 183 |
plot(canvas, "asin(x)", -1.0f, 1.0f, -kPIOverTwo, kPIOverTwo); |
| 184 |
plot(canvas, "acos(x)", -1.0f, 1.0f, 0.0f, kPI); |
| 185 |
plot(canvas, "atan(x)", -10.0f, 10.0f, -kPIOverTwo, kPIOverTwo); |
| 186 |
next_row(canvas); |
| 187 |
|
| 188 |
plot(canvas, "atan(0.1, x)", -1.0f, 1.0f, 0.0f, kPI); |
| 189 |
plot(canvas, "atan(-0.1, x)", -1.0f, 1.0f, -kPI, 0.0f); |
| 190 |
next_row(canvas); |
| 191 |
|
| 192 |
plot(canvas, "atan(x, 0.1)", -1.0f, 1.0f, -kPIOverTwo, kPIOverTwo); |
| 193 |
plot(canvas, "atan(x, -0.1)", -1.0f, 1.0f, -kPI, kPI); |
| 194 |
next_row(canvas); |
| 195 |
} |
| 196 |
|
| 197 |
#if defined(SK_GANESH) |
| 198 |
static void plot_es3(SkCanvas* canvas, |
| 199 |
const char* fn, |
| 200 |
float xMin, |
| 201 |
float xMax, |
| 202 |
float yMin, |
| 203 |
float yMax, |
| 204 |
const char* label = nullptr) { |
| 205 |
plot(canvas, fn, xMin, xMax, yMin, yMax, label, /*requireES3=*/true); |
| 206 |
} |
| 207 |
|
| 208 |
// The OpenGL ES Shading Language, Version 3.00, Section 8.1 |
| 209 |
DEF_SIMPLE_GPU_GM_CAN_FAIL(runtime_intrinsics_trig_es3, |
| 210 |
ctx, canvas, errorMsg, |
| 211 |
columns_to_width(3), |
| 212 |
rows_to_height(2)) { |
| 213 |
if (ctx->priv().caps()->shaderCaps()->supportedSkSLVerion() < SkSL::Version::k300) { |
| 214 |
*errorMsg = "SkSL 300 is not supported."; |
| 215 |
return skiagm::DrawResult::kSkip; |
| 216 |
} |
| 217 |
|
| 218 |
canvas->translate(kPadding, kPadding); |
| 219 |
canvas->save(); |
| 220 |
|
| 221 |
plot_es3(canvas, "sinh(x)", -2.0f, 2.0f, -4.0f, 4.0f); |
| 222 |
plot_es3(canvas, "cosh(x)", -2.0f, 2.0f, 0.0f, 4.0f); |
| 223 |
plot_es3(canvas, "tanh(x)", -2.0f, 2.0f, -1.0f, 1.0f); |
| 224 |
next_row(canvas); |
| 225 |
|
| 226 |
if (ctx->priv().caps()->shaderCaps()->fInverseHyperbolicSupport) { |
| 227 |
plot_es3(canvas, "asinh(x)", -2.0f, 2.0f, -2.0f, 2.0f); |
| 228 |
plot_es3(canvas, "acosh(x)", 0.0f, 5.0f, 0.0f, 3.0f); |
| 229 |
plot_es3(canvas, "atanh(x)", -1.0f, 1.0f, -4.0f, 4.0f); |
| 230 |
} |
| 231 |
next_row(canvas); |
| 232 |
|
| 233 |
return skiagm::DrawResult::kOk; |
| 234 |
} |
| 235 |
#endif // SK_GANESH |
| 236 |
|
| 237 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.2 |
| 238 |
DEF_SIMPLE_GM(runtime_intrinsics_exponential, |
| 239 |
canvas, |
| 240 |
columns_to_width(2), |
| 241 |
rows_to_height(5)) { |
| 242 |
canvas->translate(kPadding, kPadding); |
| 243 |
canvas->save(); |
| 244 |
|
| 245 |
plot(canvas, "pow(x, 3)", 0.0f, 8.0f, 0.0f, 500.0f); |
| 246 |
plot(canvas, "pow(x, -3)", 0.0f, 4.0f, 0.0f, 10.0f); |
| 247 |
next_row(canvas); |
| 248 |
|
| 249 |
plot(canvas, "pow(0.9, x)", -10.0f, 10.0f, 0.0f, 3.0f); |
| 250 |
plot(canvas, "pow(1.1, x)", -10.0f, 10.0f, 0.0f, 3.0f); |
| 251 |
next_row(canvas); |
| 252 |
|
| 253 |
plot(canvas, "exp(x)", -1.0f, 7.0f, 0.0f, 1000.0f); |
| 254 |
plot(canvas, "log(x)", 0.0f, 2.5f, -4.0f, 1.0f); |
| 255 |
next_row(canvas); |
| 256 |
|
| 257 |
plot(canvas, "exp2(x)", -1.0f, 7.0f, 0.0f, 130.0f); |
| 258 |
plot(canvas, "log2(x)", 0.0f, 4.0f, -4.0f, 2.0f); |
| 259 |
next_row(canvas); |
| 260 |
|
| 261 |
plot(canvas, "sqrt(x)", 0.0f, 25.0f, 0.0f, 5.0f); |
| 262 |
plot(canvas, "inversesqrt(x)", 0.0f, 25.0f, 0.2f, 4.0f); |
| 263 |
next_row(canvas); |
| 264 |
} |
| 265 |
|
| 266 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.3 |
| 267 |
DEF_SIMPLE_GM(runtime_intrinsics_common, |
| 268 |
canvas, |
| 269 |
columns_to_width(6), |
| 270 |
rows_to_height(7)) { |
| 271 |
canvas->translate(kPadding, kPadding); |
| 272 |
canvas->save(); |
| 273 |
|
| 274 |
plot(canvas, "abs(x)", -10.0f, 10.0f, 0.0f, 10.0f); |
| 275 |
plot(canvas, "sign(x)", -1.0f, 1.0f, -1.5f, 1.5f); |
| 276 |
next_row(canvas); |
| 277 |
|
| 278 |
plot(canvas, "floor(x)", -3.0f, 3.0f, -4.0f, 4.0f); |
| 279 |
plot(canvas, "ceil(x)", -3.0f, 3.0f, -4.0f, 4.0f); |
| 280 |
plot(canvas, "fract(x)", -3.0f, 3.0f, 0.0f, 1.0f); |
| 281 |
plot(canvas, "mod(x, 2)", -4.0f, 4.0f, -2.0f, 2.0f, "mod(scalar)"); |
| 282 |
plot(canvas, "mod(p, -2).x", -4.0f, 4.0f, -2.0f, 2.0f, "mod(mixed)" ); |
| 283 |
plot(canvas, "mod(p, v2).x", -4.0f, 4.0f, -2.0f, 2.0f, "mod(vector)"); |
| 284 |
next_row(canvas); |
| 285 |
|
| 286 |
plot(canvas, "min(x, 1)", 0.0f, 2.0f, 0.0f, 2.0f, "min(scalar)"); |
| 287 |
plot(canvas, "min(p, 1).x", 0.0f, 2.0f, 0.0f, 2.0f, "min(mixed)" ); |
| 288 |
plot(canvas, "min(p, v1).x", 0.0f, 2.0f, 0.0f, 2.0f, "min(vector)"); |
| 289 |
plot(canvas, "max(x, 1)", 0.0f, 2.0f, 0.0f, 2.0f, "max(scalar)"); |
| 290 |
plot(canvas, "max(p, 1).x", 0.0f, 2.0f, 0.0f, 2.0f, "max(mixed)" ); |
| 291 |
plot(canvas, "max(p, v1).x", 0.0f, 2.0f, 0.0f, 2.0f, "max(vector)"); |
| 292 |
next_row(canvas); |
| 293 |
|
| 294 |
plot(canvas, "clamp(x, 1, 2)", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(scalar)"); |
| 295 |
plot(canvas, "clamp(p, 1, 2).x", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(mixed)" ); |
| 296 |
plot(canvas, "clamp(p, v1, v2).x", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(vector)"); |
| 297 |
plot(canvas, "saturate(x)", -1.0f, 2.0f, -0.5f, 1.5f); |
| 298 |
next_row(canvas); |
| 299 |
|
| 300 |
plot(canvas, "mix(1, 2, x)", -1.0f, 2.0f, 0.0f, 3.0f, "mix(scalar)"); |
| 301 |
plot(canvas, "mix(v1, v2, x).x", -1.0f, 2.0f, 0.0f, 3.0f, "mix(mixed)" ); |
| 302 |
plot(canvas, "mix(v1, v2, p).x", -1.0f, 2.0f, 0.0f, 3.0f, "mix(vector)"); |
| 303 |
next_row(canvas); |
| 304 |
|
| 305 |
plot(canvas, "step(1, x)", 0.0f, 2.0f, -0.5f, 1.5f, "step(scalar)"); |
| 306 |
plot(canvas, "step(1, p).x", 0.0f, 2.0f, -0.5f, 1.5f, "step(mixed)" ); |
| 307 |
plot(canvas, "step(v1, p).x", 0.0f, 2.0f, -0.5f, 1.5f, "step(vector)"); |
| 308 |
plot(canvas, "smoothstep(1, 2, x)", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(scalar)"); |
| 309 |
plot(canvas, "smoothstep(1, 2, p).x", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(mixed)" ); |
| 310 |
plot(canvas, "smoothstep(v1, v2, p).x", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(vector)"); |
| 311 |
next_row(canvas); |
| 312 |
|
| 313 |
plot(canvas, "floor(p).x", -3.0f, 3.0f, -4.0f, 4.0f); |
| 314 |
plot(canvas, "ceil(p).x", -3.0f, 3.0f, -4.0f, 4.0f); |
| 315 |
plot(canvas, "floor(p).y", -3.0f, 3.0f, -4.0f, 4.0f); |
| 316 |
plot(canvas, "ceil(p).y", -3.0f, 3.0f, -4.0f, 4.0f); |
| 317 |
next_row(canvas); |
| 318 |
} |
| 319 |
|
| 320 |
#if defined(SK_GANESH) |
| 321 |
// The OpenGL ES Shading Language, Version 3.00, Section 8.1 |
| 322 |
DEF_SIMPLE_GPU_GM_CAN_FAIL(runtime_intrinsics_common_es3, |
| 323 |
ctx, canvas, errorMsg, |
| 324 |
columns_to_width(6), |
| 325 |
rows_to_height(5)) { |
| 326 |
if (ctx->priv().caps()->shaderCaps()->supportedSkSLVerion() < SkSL::Version::k300) { |
| 327 |
*errorMsg = "SkSL 300 is not supported."; |
| 328 |
return skiagm::DrawResult::kSkip; |
| 329 |
} |
| 330 |
|
| 331 |
canvas->translate(kPadding, kPadding); |
| 332 |
canvas->save(); |
| 333 |
|
| 334 |
plot_es3(canvas, "floatBitsToInt(x)", -2, 2, -2'000'000'000, 2'000'000'000, |
| 335 |
"floatBitsToInt(s)"); |
| 336 |
plot_es3(canvas, "floatBitsToInt(p).x", -2, 2, -2'000'000'000, 2'000'000'000, |
| 337 |
"floatBitsToInt(v)"); |
| 338 |
plot_es3(canvas, "floatBitsToUint(x)", -2, 2, 0, 4'000'000'000, |
| 339 |
"floatBitsToUint(s)"); |
| 340 |
plot_es3(canvas, "floatBitsToUint(p).x", -2, 2, 0, 4'000'000'000, |
| 341 |
"floatBitsToUint(v)"); |
| 342 |
next_row(canvas); |
| 343 |
|
| 344 |
plot_es3(canvas, "intBitsToFloat(xi)", -2'000'000'000, 2'000'000'000, -2, 2, |
| 345 |
"intBitsToFloat(s)"); |
| 346 |
plot_es3(canvas, "intBitsToFloat(pi).x", -2'000'000'000, 2'000'000'000, -2, 2, |
| 347 |
"intBitsToFloat(v)"); |
| 348 |
plot_es3(canvas, "uintBitsToFloat(uint(xi))", 0, 4'000'000'000, -2, 2, |
| 349 |
"uintBitsToFloat(s)"); |
| 350 |
plot_es3(canvas, "uintBitsToFloat(uint2(pi)).x", 0, 4'000'000'000, -2, 2, |
| 351 |
"uintBitsToFloat(v)"); |
| 352 |
next_row(canvas); |
| 353 |
|
| 354 |
plot_es3(canvas, "trunc(x)", -2, 2, -3, 3); |
| 355 |
plot_es3(canvas, "trunc(p).x", -2, 2, -3, 3); |
| 356 |
plot_es3(canvas, "round(x)", -2, 2, -3, 3); |
| 357 |
plot_es3(canvas, "round(p).x", -2, 2, -3, 3); |
| 358 |
plot_es3(canvas, "roundEven(x)", -2, 2, -3, 3); |
| 359 |
plot_es3(canvas, "roundEven(p).x", -2, 2, -3, 3); |
| 360 |
next_row(canvas); |
| 361 |
|
| 362 |
plot_es3(canvas, "min(xi, 1)", -2, 5, -3, 5, "min(int-scalar)"); |
| 363 |
plot_es3(canvas, "min(pi, 1).x", -2, 5, -3, 5, "min(int-mixed)" ); |
| 364 |
plot_es3(canvas, "min(pi, int2(1)).x", -2, 5, -3, 5, "min(int-vector)"); |
| 365 |
plot_es3(canvas, "max(xi, 1)", -2, 5, -3, 5, "max(int-scalar)"); |
| 366 |
plot_es3(canvas, "max(pi, 1).x", -2, 5, -3, 5, "max(int-mixed)" ); |
| 367 |
plot_es3(canvas, "max(pi, int2(1)).x", -2, 5, -3, 5, "max(int-vector)"); |
| 368 |
next_row(canvas); |
| 369 |
|
| 370 |
plot_es3(canvas, "clamp(xi, 1, 3)", -1, 5, -1, 5, "clamp(int-scalar)"); |
| 371 |
plot_es3(canvas, "clamp(pi, 1, 3).x", -1, 5, -1, 5, "clamp(int-mixed)" ); |
| 372 |
plot_es3(canvas, "clamp(pi, int2(1), int2(3)).x", -1, 5, -1, 5, "clamp(int-vector)"); |
| 373 |
plot_es3(canvas, "mix(p.x, p.y, (x>0) )", -1, 2, 0, 3, "mix(scalar, bool)"); |
| 374 |
plot_es3(canvas, "mix(p.yx, p, (x>0).xx).x", -1, 2, 0, 3, "mix(vector, bool)"); |
| 375 |
next_row(canvas); |
| 376 |
|
| 377 |
return skiagm::DrawResult::kOk; |
| 378 |
} |
| 379 |
#endif // SK_GANESH |
| 380 |
|
| 381 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.4 |
| 382 |
DEF_SIMPLE_GM(runtime_intrinsics_geometric, |
| 383 |
canvas, |
| 384 |
columns_to_width(4), |
| 385 |
rows_to_height(5)) { |
| 386 |
canvas->translate(kPadding, kPadding); |
| 387 |
canvas->save(); |
| 388 |
|
| 389 |
plot(canvas, "length(x)", -1.0f, 1.0f, -0.5f, 1.5f); |
| 390 |
plot(canvas, "length(p)", 0.0f, 1.0f, 0.5f, 1.5f); |
| 391 |
plot(canvas, "distance(x, 0)", -1.0f, 1.0f, -0.5f, 1.5f); |
| 392 |
plot(canvas, "distance(p, v1)", 0.0f, 1.0f, 0.5f, 1.5f); |
| 393 |
next_row(canvas); |
| 394 |
|
| 395 |
plot(canvas, "dot(x, 2)", -1.0f, 1.0f, -2.5f, 2.5f); |
| 396 |
plot(canvas, "dot(p, p.y1)", -1.0f, 1.0f, -2.5f, 0.5f); |
| 397 |
next_row(canvas); |
| 398 |
|
| 399 |
plot(canvas, "cross(p.xy1, p.y1x).x", 0.0f, 1.0f, -1.0f, 1.0f); |
| 400 |
plot(canvas, "cross(p.xy1, p.y1x).y", 0.0f, 1.0f, -1.0f, 1.0f); |
| 401 |
plot(canvas, "cross(p.xy1, p.y1x).z", 0.0f, 1.0f, -1.0f, 1.0f); |
| 402 |
next_row(canvas); |
| 403 |
|
| 404 |
plot(canvas, "normalize(x)", -2.0f, 2.0f, -1.5f, 1.5f); |
| 405 |
plot(canvas, "normalize(p).x", 0.0f, 2.0f, 0.0f, 1.0f); |
| 406 |
plot(canvas, "normalize(p).y", 0.0f, 2.0f, 0.0f, 1.0f); |
| 407 |
plot(canvas, "faceforward(v1, p.x0, v1.x0).x", -1.0f, 1.0f, -1.5f, 1.5f, "faceforward"); |
| 408 |
next_row(canvas); |
| 409 |
|
| 410 |
plot(canvas, "reflect(p.x1, v1.0x).x", -1.0f, 1.0f, -1.0f, 1.0f, "reflect(horiz)"); |
| 411 |
plot(canvas, "reflect(p.x1, normalize(v1)).y", -1.0f, 1.0f, -1.0f, 1.0f, "reflect(diag)" ); |
| 412 |
plot(canvas, "refract(v1.x0, v1.0x, x).x", 0.0f, 1.0f, -1.0f, 1.0f, "refract().x"); |
| 413 |
plot(canvas, "refract(v1.x0, v1.0x, x).y", 0.0f, 1.0f, -1.0f, 1.0f, "refract().y"); |
| 414 |
next_row(canvas); |
| 415 |
} |
| 416 |
|
| 417 |
#define SKSL_MATRIX_SELECTORS \ |
| 418 |
"inline float2 sel2(float x) {" \ |
| 419 |
" return float2(" \ |
| 420 |
" x < 0.5 ? 1 : 0," \ |
| 421 |
" x >= 0.5 ? 1 : 0);" \ |
| 422 |
"}" \ |
| 423 |
"inline float3 sel3(float x) {" \ |
| 424 |
" return float3(" \ |
| 425 |
" x < 0.33 ? 1 : 0," \ |
| 426 |
" x >= 0.33 && x < 0.66 ? 1 : 0," \ |
| 427 |
" x >= 0.66 ? 1 : 0);" \ |
| 428 |
"}" \ |
| 429 |
"inline float4 sel4(float x) {" \ |
| 430 |
" return float4(" \ |
| 431 |
" x < 0.25 ? 1 : 0," \ |
| 432 |
" x >= 0.25 && x < 0.5 ? 1 : 0," \ |
| 433 |
" x >= 0.5 && x < 0.75 ? 1 : 0," \ |
| 434 |
" x >= 0.75 ? 1 : 0);" \ |
| 435 |
"}" |
| 436 |
|
| 437 |
// Shader for testing matrixCompMult intrinsic |
| 438 |
static SkString make_matrix_comp_mult_sksl(int dim) { |
| 439 |
return SkStringPrintf( |
| 440 |
"uniform float%dx%d m1;" // dim, dim |
| 441 |
"uniform float%dx%d m2;" // dim, dim |
| 442 |
SKSL_MATRIX_SELECTORS |
| 443 |
"half4 main(float2 p) {" |
| 444 |
" float%d colSel = sel%d(p.x);" // dim, dim |
| 445 |
" float%d rowSel = sel%d(p.y);" // dim, dim |
| 446 |
" float%d col = matrixCompMult(m1, m2) * colSel;" // dim |
| 447 |
" float v = dot(col, rowSel);" |
| 448 |
" return v.xxx1;" |
| 449 |
"}", dim, dim, dim, dim, dim, dim, dim, dim, dim); |
| 450 |
} |
| 451 |
|
| 452 |
template <int N> |
| 453 |
static void plot_matrix_comp_mult(SkCanvas* canvas, |
| 454 |
std::array<float, N*N> mtx1, |
| 455 |
std::array<float, N*N> mtx2, |
| 456 |
const char* label) { |
| 457 |
canvas->save(); |
| 458 |
|
| 459 |
draw_label(canvas, label); |
| 460 |
|
| 461 |
auto [effect, error] = SkRuntimeEffect::MakeForShader(make_matrix_comp_mult_sksl(N)); |
| 462 |
if (!effect) { |
| 463 |
SkDebugf("Error: %s\n", error.c_str()); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
SkRuntimeShaderBuilder builder(effect); |
| 468 |
builder.uniform("m1") = mtx1; |
| 469 |
builder.uniform("m2") = mtx2; |
| 470 |
|
| 471 |
draw_shader(canvas, builder.makeShader()); |
| 472 |
|
| 473 |
canvas->restore(); |
| 474 |
next_column(canvas); |
| 475 |
} |
| 476 |
|
| 477 |
// Shader for testing inverse() intrinsic |
| 478 |
static SkString make_matrix_inverse_sksl(int dim) { |
| 479 |
return SkStringPrintf( |
| 480 |
"uniform float scale; uniform float bias;" |
| 481 |
"uniform float%dx%d m;" // dim, dim |
| 482 |
SKSL_MATRIX_SELECTORS |
| 483 |
"half4 main(float2 p) {" |
| 484 |
" float%d colSel = sel%d(p.x);" // dim, dim |
| 485 |
" float%d rowSel = sel%d(p.y);" // dim, dim |
| 486 |
" float%d col = inverse(m) * colSel;" // dim |
| 487 |
" float v = dot(col, rowSel) * scale + bias;" |
| 488 |
" return v.xxx1;" |
| 489 |
"}", dim, dim, dim, dim, dim, dim, dim); |
| 490 |
} |
| 491 |
|
| 492 |
template <int N> |
| 493 |
static void plot_matrix_inverse(SkCanvas* canvas, std::array<float, N*N> mtx, const char* label) { |
| 494 |
canvas->save(); |
| 495 |
|
| 496 |
draw_label(canvas, label); |
| 497 |
|
| 498 |
auto [effect, error] = SkRuntimeEffect::MakeForShader(make_matrix_inverse_sksl(N)); |
| 499 |
if (!effect) { |
| 500 |
SkDebugf("Error: %s\n", error.c_str()); |
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
SkRuntimeShaderBuilder builder(effect); |
| 505 |
builder.uniform("scale") = 0.5f; |
| 506 |
builder.uniform("bias") = 0.5f; |
| 507 |
builder.uniform("m") = mtx; |
| 508 |
|
| 509 |
draw_shader(canvas, builder.makeShader()); |
| 510 |
|
| 511 |
canvas->restore(); |
| 512 |
next_column(canvas); |
| 513 |
} |
| 514 |
|
| 515 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.5 |
| 516 |
DEF_SIMPLE_GM(runtime_intrinsics_matrix, |
| 517 |
canvas, |
| 518 |
columns_to_width(3), |
| 519 |
rows_to_height(2)) { |
| 520 |
canvas->translate(kPadding, kPadding); |
| 521 |
canvas->save(); |
| 522 |
|
| 523 |
// Random pairs of matrices where the elements of matrixCompMult(m1, m2) lie in [0, 1] |
| 524 |
plot_matrix_comp_mult<2>(canvas, |
| 525 |
{1.00f, 0.0f, 2.0f, 0.5f}, |
| 526 |
{0.75f, 2.0f, 0.2f, 1.2f}, |
| 527 |
"compMult(2x2)"); |
| 528 |
|
| 529 |
plot_matrix_comp_mult<3>(canvas, |
| 530 |
{1.00f, 0.0f, 2.0f, 0.5f, -1.0f, -2.0f, -0.5f, 4.00f, 0.25f}, |
| 531 |
{0.75f, 2.0f, 0.2f, 1.2f, -0.8f, -0.1f, -1.8f, 0.25f, 2.00f}, |
| 532 |
"compMult(3x3)"); |
| 533 |
|
| 534 |
plot_matrix_comp_mult<4>(canvas, |
| 535 |
{1.00f, 0.0f, 2.0f, 0.5f, -1.0f, -2.0f, -0.5f, 4.00f, 0.25f, 0.05f, |
| 536 |
10.00f, -0.66f, -1.0f, -0.5f, 0.5f, 0.66f}, |
| 537 |
{0.75f, 2.0f, 0.2f, 1.2f, -0.8f, -0.1f, -1.8f, 0.25f, 2.00f, 2.00f, |
| 538 |
0.03f, -1.00f, -1.0f, -0.5f, 1.7f, 0.66f}, |
| 539 |
"compMult(4x4)"); |
| 540 |
next_row(canvas); |
| 541 |
|
| 542 |
// Random, invertible matrices where the elements of inverse(m) lie in [-1, 1] |
| 543 |
plot_matrix_inverse<2>(canvas, |
| 544 |
{ 1.20f, 0.68f, |
| 545 |
-0.27f, -1.55f}, |
| 546 |
"inverse(2x2)"); |
| 547 |
|
| 548 |
plot_matrix_inverse<3>(canvas, |
| 549 |
{-1.13f, -2.96f, -0.14f, |
| 550 |
1.45f, -1.88f, -1.02f, |
| 551 |
-2.54f, -2.58f, -1.17f}, |
| 552 |
"inverse(3x3)"); |
| 553 |
|
| 554 |
plot_matrix_inverse<4>(canvas, |
| 555 |
{-1.51f, -3.95f, -0.19f, 1.93f, |
| 556 |
-2.51f, -1.35f, -3.39f, -3.45f, |
| 557 |
-1.56f, 1.61f, -0.22f, -1.08f, |
| 558 |
-2.81f, -2.14f, -0.09f, 3.00f}, |
| 559 |
"inverse(4x4)"); |
| 560 |
next_row(canvas); |
| 561 |
} |
| 562 |
|
| 563 |
/* |
| 564 |
Specialized shader for testing relational operators. |
| 565 |
*/ |
| 566 |
static SkString make_bvec_sksl(const char* type, const char* fn) { |
| 567 |
// We use negative floats, to ensure that the integer variants are working with the correct |
| 568 |
// interpretation of the data. |
| 569 |
return SkStringPrintf( |
| 570 |
"uniform %s2 v1;" |
| 571 |
"half4 main(float2 p) {" |
| 572 |
" p.x = p.x < 0.33 ? -3.0 : (p.x < 0.66 ? -2.0 : -1.0);" |
| 573 |
" p.y = p.y < 0.33 ? -3.0 : (p.y < 0.66 ? -2.0 : -1.0);" |
| 574 |
" bool2 cmp = %s;" |
| 575 |
" return half4(cmp.x ? 1.0 : 0.0, cmp.y ? 1.0 : 0.0, 0, 1);" |
| 576 |
"}", |
| 577 |
type, fn); |
| 578 |
} |
| 579 |
|
| 580 |
template <typename T = float> |
| 581 |
static void plot_bvec(SkCanvas* canvas, const char* fn, const char* label) { |
| 582 |
canvas->save(); |
| 583 |
|
| 584 |
draw_label(canvas, label); |
| 585 |
|
| 586 |
const char* type = std::is_integral<T>::value ? "int" : "float"; |
| 587 |
auto [effect, error] = SkRuntimeEffect::MakeForShader(make_bvec_sksl(type, fn)); |
| 588 |
if (!effect) { |
| 589 |
SkDebugf("Error: %s\n", error.c_str()); |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
T uniformData[2] = { -2, -2 }; |
| 594 |
sk_sp<SkData> uniforms = SkData::MakeWithCopy(uniformData, sizeof(uniformData)); |
| 595 |
|
| 596 |
draw_shader(canvas, effect->makeShader(uniforms, /*children=*/{})); |
| 597 |
|
| 598 |
canvas->restore(); |
| 599 |
next_column(canvas); |
| 600 |
} |
| 601 |
|
| 602 |
// The OpenGL ES Shading Language, Version 1.00, Section 8.6 |
| 603 |
DEF_SIMPLE_GM(runtime_intrinsics_relational, |
| 604 |
canvas, |
| 605 |
columns_to_width(4), |
| 606 |
rows_to_height(6)) { |
| 607 |
canvas->translate(kPadding, kPadding); |
| 608 |
canvas->save(); |
| 609 |
|
| 610 |
plot_bvec<float>(canvas, "lessThan(p, v1)", "lessThan"); |
| 611 |
plot_bvec<int> (canvas, "lessThan(int2(p), v1)", "lessThan(int)"); |
| 612 |
plot_bvec<float>(canvas, "lessThanEqual(p, v1)", "lessThanEqual"); |
| 613 |
plot_bvec<int> (canvas, "lessThanEqual(int2(p), v1)", "lessThanEqual(int)"); |
| 614 |
next_row(canvas); |
| 615 |
|
| 616 |
plot_bvec<float>(canvas, "greaterThan(p, v1)", "greaterThan"); |
| 617 |
plot_bvec<int> (canvas, "greaterThan(int2(p), v1)", "greaterThan(int)"); |
| 618 |
plot_bvec<float>(canvas, "greaterThanEqual(p, v1)", "greaterThanEqual"); |
| 619 |
plot_bvec<int> (canvas, "greaterThanEqual(int2(p), v1)", "greaterThanEqual(int)"); |
| 620 |
next_row(canvas); |
| 621 |
|
| 622 |
plot_bvec<float>(canvas, "equal(p, v1)", "equal"); |
| 623 |
plot_bvec<int> (canvas, "equal(int2(p), v1)", "equal(int)"); |
| 624 |
plot_bvec<float>(canvas, "notEqual(p, v1)", "notEqual"); |
| 625 |
plot_bvec<int> (canvas, "notEqual(int2(p), v1)", "notEqual(int)"); |
| 626 |
next_row(canvas); |
| 627 |
|
| 628 |
plot_bvec(canvas, "equal( lessThanEqual(p, v1), greaterThanEqual(p, v1))", "equal(bvec)"); |
| 629 |
plot_bvec(canvas, "notEqual(lessThanEqual(p, v1), greaterThanEqual(p, v1))", "notequal(bvec)"); |
| 630 |
next_row(canvas); |
| 631 |
|
| 632 |
plot_bvec(canvas, "not(notEqual(p, v1))", "not(notEqual)"); |
| 633 |
plot_bvec(canvas, "not(equal(p, v1))", "not(equal)"); |
| 634 |
next_row(canvas); |
| 635 |
|
| 636 |
plot_bvec(canvas, "bool2(any(equal(p, v1)))", "any(equal)"); |
| 637 |
plot_bvec(canvas, "bool2(all(equal(p, v1)))", "all(equal)"); |
| 638 |
next_row(canvas); |
| 639 |
} |