Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 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 | */ |
Nathaniel Nifong | 60c37e7 | 2019-10-16 11:21:47 -0400 | [diff] [blame] | 7 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/utils/SkRandom.h" |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 10 | #include "src/sksl/SkSLByteCode.h" |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 11 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLInterpreter.h" |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 13 | |
Nathaniel Nifong | 60c37e7 | 2019-10-16 11:21:47 -0400 | [diff] [blame] | 14 | // Without this build flag, this bench isn't runnable. |
| 15 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
| 16 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 17 | // Benchmarks the interpreter with a function that has a color-filter style signature |
| 18 | class SkSLInterpreterCFBench : public Benchmark { |
| 19 | public: |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 20 | SkSLInterpreterCFBench(SkSL::String name, int pixels, const char* src) |
| 21 | : fName(SkStringPrintf("sksl_interp_cf_%d_%s", pixels, name.c_str())) |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 22 | , fSrc(src) |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 23 | , fCount(pixels) {} |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 24 | |
| 25 | protected: |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 26 | static constexpr int VecWidth = 16; |
| 27 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 28 | const char* onGetName() override { |
| 29 | return fName.c_str(); |
| 30 | } |
| 31 | |
| 32 | bool isSuitableFor(Backend backend) override { |
| 33 | return backend == kNonRendering_Backend; |
| 34 | } |
| 35 | |
| 36 | void onDelayedSetup() override { |
| 37 | SkSL::Compiler compiler; |
| 38 | SkSL::Program::Settings settings; |
| 39 | auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, fSrc, settings); |
| 40 | SkASSERT(compiler.errorCount() == 0); |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 41 | std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program); |
| 42 | fMain = byteCode->getFunction("main"); |
| 43 | fInterpreter.reset(new SkSL::Interpreter<VecWidth>(std::move(byteCode))); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 44 | SkASSERT(compiler.errorCount() == 0); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 45 | |
| 46 | SkRandom rnd; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 47 | fPixels.resize(fCount * 4); |
| 48 | for (float& c : fPixels) { |
| 49 | c = rnd.nextF(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void onDraw(int loops, SkCanvas*) override { |
| 54 | for (int i = 0; i < loops; i++) { |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 55 | float* args[] = { |
| 56 | fPixels.data() + 0 * fCount, |
| 57 | fPixels.data() + 1 * fCount, |
| 58 | fPixels.data() + 2 * fCount, |
| 59 | fPixels.data() + 3 * fCount, |
| 60 | }; |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 61 | |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 62 | fInterpreter->runStriped(fMain, fCount, (float**) args); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | SkString fName; |
| 68 | SkSL::String fSrc; |
Ethan Nicholas | b962eff | 2020-01-23 16:49:41 -0500 | [diff] [blame] | 69 | std::unique_ptr<SkSL::Interpreter<VecWidth>> fInterpreter; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 70 | const SkSL::ByteCodeFunction* fMain; |
| 71 | |
| 72 | int fCount; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 73 | std::vector<float> fPixels; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 74 | |
| 75 | typedef Benchmark INHERITED; |
| 76 | }; |
| 77 | |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 80 | const char* kLumaToAlphaSrc = R"( |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 81 | void main(inout float4 color) { |
| 82 | color.a = color.r*0.3 + color.g*0.6 + color.b*0.1; |
| 83 | color.r = 0; |
| 84 | color.g = 0; |
| 85 | color.b = 0; |
| 86 | } |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 87 | )"; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 88 | |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 89 | const char* kHighContrastFilterSrc = R"( |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 90 | half ucontrast_Stage2; |
| 91 | half hue2rgb_Stage2(half p, half q, half t) { |
| 92 | if (t < 0) t += 1; |
| 93 | if (t > 1) t -= 1; |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 94 | return (t < 1 / 6.) ? p + (q - p) * 6 * t |
| 95 | : (t < 1 / 2.) ? q |
| 96 | : (t < 2 / 3.) ? p + (q - p) * (2 / 3. - t) * 6 |
| 97 | : p; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 98 | } |
| 99 | half max(half a, half b) { return a > b ? a : b; } |
| 100 | half min(half a, half b) { return a < b ? a : b; } |
| 101 | void main(inout half4 color) { |
| 102 | ucontrast_Stage2 = 0.2; |
| 103 | |
| 104 | // HighContrastFilter |
| 105 | half nonZeroAlpha = max(color.a, 0.0001); |
| 106 | color = half4(color.rgb / nonZeroAlpha, nonZeroAlpha); |
| 107 | color.rgb = color.rgb * color.rgb; |
| 108 | half fmax = max(color.r, max(color.g, color.b)); |
| 109 | half fmin = min(color.r, min(color.g, color.b)); |
| 110 | half l = (fmax + fmin) / 2; |
| 111 | half h; |
| 112 | half s; |
| 113 | if (fmax == fmin) { |
| 114 | h = 0; |
| 115 | s = 0; |
| 116 | } else { |
| 117 | half d = fmax - fmin; |
| 118 | s = l > 0.5 ? d / (2 - fmax - fmin) : d / (fmax + fmin); |
| 119 | if (color.r >= color.g && color.r >= color.b) { |
| 120 | h = (color.g - color.b) / d + (color.g < color.b ? 6 : 0); |
| 121 | } else if (color.g >= color.b) { |
| 122 | h = (color.b - color.r) / d + 2; |
| 123 | } else { |
| 124 | h = (color.r - color.g) / d + 4; |
| 125 | } |
| 126 | } |
| 127 | h /= 6; |
| 128 | l = 1.0 - l; |
| 129 | if (s == 0) { |
| 130 | color = half4(l, l, l, 0); |
| 131 | } else { |
| 132 | half q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
| 133 | half p = 2 * l - q; |
| 134 | color.r = hue2rgb_Stage2(p, q, h + 1 / 3.); |
| 135 | color.g = hue2rgb_Stage2(p, q, h); |
| 136 | color.b = hue2rgb_Stage2(p, q, h - 1 / 3.); |
| 137 | } |
| 138 | if (ucontrast_Stage2 != 0) { |
| 139 | half m = (1 + ucontrast_Stage2) / (1 - ucontrast_Stage2); |
| 140 | half off = (-0.5 * m + 0.5); |
| 141 | color = m * color + off; |
| 142 | } |
| 143 | // color = saturate(color); |
| 144 | color.rgb = sqrt(color.rgb); |
| 145 | color.rgb *= color.a; |
| 146 | } |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 147 | )"; |
| 148 | |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 149 | DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, kLumaToAlphaSrc)); |
| 150 | DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, kHighContrastFilterSrc)); |
Nathaniel Nifong | 60c37e7 | 2019-10-16 11:21:47 -0400 | [diff] [blame] | 151 | #endif // SK_ENABLE_SKSL_INTERPRETER |