blob: 568495578d24a1c9114c1622ab3a2ff1ade804a5 [file] [log] [blame]
Brian Osman569f12f2019-06-13 11:23:57 -04001/*
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 Nifong60c37e72019-10-16 11:21:47 -04007
Brian Osman569f12f2019-06-13 11:23:57 -04008#include "bench/Benchmark.h"
9#include "include/utils/SkRandom.h"
Brian Osman08a84962019-06-14 10:17:16 -040010#include "src/sksl/SkSLByteCode.h"
Brian Osman569f12f2019-06-13 11:23:57 -040011#include "src/sksl/SkSLCompiler.h"
Ethan Nicholasb962eff2020-01-23 16:49:41 -050012#include "src/sksl/SkSLInterpreter.h"
Brian Osman569f12f2019-06-13 11:23:57 -040013
Nathaniel Nifong60c37e72019-10-16 11:21:47 -040014// Without this build flag, this bench isn't runnable.
15#if defined(SK_ENABLE_SKSL_INTERPRETER)
16
Brian Osman569f12f2019-06-13 11:23:57 -040017// Benchmarks the interpreter with a function that has a color-filter style signature
18class SkSLInterpreterCFBench : public Benchmark {
19public:
Brian Osmanb23d66e2019-09-27 10:25:57 -040020 SkSLInterpreterCFBench(SkSL::String name, int pixels, const char* src)
21 : fName(SkStringPrintf("sksl_interp_cf_%d_%s", pixels, name.c_str()))
Brian Osman569f12f2019-06-13 11:23:57 -040022 , fSrc(src)
Brian Osmanb23d66e2019-09-27 10:25:57 -040023 , fCount(pixels) {}
Brian Osman569f12f2019-06-13 11:23:57 -040024
25protected:
Ethan Nicholasb962eff2020-01-23 16:49:41 -050026 static constexpr int VecWidth = 16;
27
Brian Osman569f12f2019-06-13 11:23:57 -040028 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 Nicholasb962eff2020-01-23 16:49:41 -050041 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 Osman569f12f2019-06-13 11:23:57 -040044 SkASSERT(compiler.errorCount() == 0);
Brian Osman569f12f2019-06-13 11:23:57 -040045
46 SkRandom rnd;
Brian Osman08a84962019-06-14 10:17:16 -040047 fPixels.resize(fCount * 4);
48 for (float& c : fPixels) {
49 c = rnd.nextF();
Brian Osman569f12f2019-06-13 11:23:57 -040050 }
51 }
52
53 void onDraw(int loops, SkCanvas*) override {
54 for (int i = 0; i < loops; i++) {
Brian Osmanb23d66e2019-09-27 10:25:57 -040055 float* args[] = {
56 fPixels.data() + 0 * fCount,
57 fPixels.data() + 1 * fCount,
58 fPixels.data() + 2 * fCount,
59 fPixels.data() + 3 * fCount,
60 };
Brian Osman2b1a5442019-06-19 11:40:33 -040061
Ethan Nicholasb962eff2020-01-23 16:49:41 -050062 fInterpreter->runStriped(fMain, fCount, (float**) args);
Brian Osman569f12f2019-06-13 11:23:57 -040063 }
64 }
65
66private:
67 SkString fName;
68 SkSL::String fSrc;
Ethan Nicholasb962eff2020-01-23 16:49:41 -050069 std::unique_ptr<SkSL::Interpreter<VecWidth>> fInterpreter;
Brian Osman569f12f2019-06-13 11:23:57 -040070 const SkSL::ByteCodeFunction* fMain;
71
72 int fCount;
Brian Osman08a84962019-06-14 10:17:16 -040073 std::vector<float> fPixels;
Brian Osman569f12f2019-06-13 11:23:57 -040074
75 typedef Benchmark INHERITED;
76};
77
78///////////////////////////////////////////////////////////////////////////////
79
Brian Osman2b1a5442019-06-19 11:40:33 -040080const char* kLumaToAlphaSrc = R"(
Brian Osman569f12f2019-06-13 11:23:57 -040081 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 Osman2b1a5442019-06-19 11:40:33 -040087)";
Brian Osman569f12f2019-06-13 11:23:57 -040088
Brian Osman2b1a5442019-06-19 11:40:33 -040089const char* kHighContrastFilterSrc = R"(
Brian Osman569f12f2019-06-13 11:23:57 -040090 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 Osman4a47da72019-07-12 11:30:32 -040094 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 Osman569f12f2019-06-13 11:23:57 -040098 }
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 Osman2b1a5442019-06-19 11:40:33 -0400147)";
148
Brian Osmanb23d66e2019-09-27 10:25:57 -0400149DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, kLumaToAlphaSrc));
150DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, kHighContrastFilterSrc));
Nathaniel Nifong60c37e72019-10-16 11:21:47 -0400151#endif // SK_ENABLE_SKSL_INTERPRETER