blob: 4d6fe9b9b95eb3e9a15a2b04eeb062fa971300e0 [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 */
7#include "bench/Benchmark.h"
8#include "include/utils/SkRandom.h"
Brian Osman08a84962019-06-14 10:17:16 -04009#include "src/sksl/SkSLByteCode.h"
Brian Osman569f12f2019-06-13 11:23:57 -040010#include "src/sksl/SkSLCompiler.h"
Brian Osman569f12f2019-06-13 11:23:57 -040011
12// Benchmarks the interpreter with a function that has a color-filter style signature
13class SkSLInterpreterCFBench : public Benchmark {
14public:
Brian Osman2b1a5442019-06-19 11:40:33 -040015 SkSLInterpreterCFBench(SkSL::String name, int pixels, bool striped, const char* src)
16 : fName(SkStringPrintf("sksl_interp_cf_%d_%d_%s", pixels, striped ? 1 : 0, name.c_str()))
Brian Osman569f12f2019-06-13 11:23:57 -040017 , fSrc(src)
Brian Osman2b1a5442019-06-19 11:40:33 -040018 , fCount(pixels)
19 , fStriped(striped) {}
Brian Osman569f12f2019-06-13 11:23:57 -040020
21protected:
22 const char* onGetName() override {
23 return fName.c_str();
24 }
25
26 bool isSuitableFor(Backend backend) override {
27 return backend == kNonRendering_Backend;
28 }
29
30 void onDelayedSetup() override {
31 SkSL::Compiler compiler;
32 SkSL::Program::Settings settings;
33 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, fSrc, settings);
34 SkASSERT(compiler.errorCount() == 0);
35 fByteCode = compiler.toByteCode(*program);
36 SkASSERT(compiler.errorCount() == 0);
37 fMain = fByteCode->getFunction("main");
38
39 SkRandom rnd;
Brian Osman08a84962019-06-14 10:17:16 -040040 fPixels.resize(fCount * 4);
41 for (float& c : fPixels) {
42 c = rnd.nextF();
Brian Osman569f12f2019-06-13 11:23:57 -040043 }
44 }
45
46 void onDraw(int loops, SkCanvas*) override {
47 for (int i = 0; i < loops; i++) {
Brian Osman2b1a5442019-06-19 11:40:33 -040048 if (fStriped) {
49 float* args[] = {
50 fPixels.data() + 0 * fCount,
51 fPixels.data() + 1 * fCount,
52 fPixels.data() + 2 * fCount,
53 fPixels.data() + 3 * fCount,
54 };
55
Mike Reed3fd3cc92019-06-20 12:40:30 -040056 fByteCode->runStriped(fMain, args, 4, fCount, nullptr, 0, nullptr, 0);
Brian Osman2b1a5442019-06-19 11:40:33 -040057 } else {
58 fByteCode->run(fMain, fPixels.data(), nullptr, fCount, nullptr, 0);
59 }
Brian Osman569f12f2019-06-13 11:23:57 -040060 }
61 }
62
63private:
64 SkString fName;
65 SkSL::String fSrc;
66 std::unique_ptr<SkSL::ByteCode> fByteCode;
67 const SkSL::ByteCodeFunction* fMain;
68
69 int fCount;
Brian Osman2b1a5442019-06-19 11:40:33 -040070 bool fStriped;
Brian Osman08a84962019-06-14 10:17:16 -040071 std::vector<float> fPixels;
Brian Osman569f12f2019-06-13 11:23:57 -040072
73 typedef Benchmark INHERITED;
74};
75
76///////////////////////////////////////////////////////////////////////////////
77
Brian Osman2b1a5442019-06-19 11:40:33 -040078const char* kLumaToAlphaSrc = R"(
Brian Osman569f12f2019-06-13 11:23:57 -040079 void main(inout float4 color) {
80 color.a = color.r*0.3 + color.g*0.6 + color.b*0.1;
81 color.r = 0;
82 color.g = 0;
83 color.b = 0;
84 }
Brian Osman2b1a5442019-06-19 11:40:33 -040085)";
Brian Osman569f12f2019-06-13 11:23:57 -040086
Brian Osman2b1a5442019-06-19 11:40:33 -040087const char* kHighContrastFilterSrc = R"(
Brian Osman569f12f2019-06-13 11:23:57 -040088 half ucontrast_Stage2;
89 half hue2rgb_Stage2(half p, half q, half t) {
90 if (t < 0) t += 1;
91 if (t > 1) t -= 1;
92 if (t < 1 / 6.) return p + (q - p) * 6 * t;
93 if (t < 1 / 2.) return q;
94 if (t < 2 / 3.) return p + (q - p) * (2 / 3. - t) * 6;
95 return p;
96 }
97 half max(half a, half b) { return a > b ? a : b; }
98 half min(half a, half b) { return a < b ? a : b; }
99 void main(inout half4 color) {
100 ucontrast_Stage2 = 0.2;
101
102 // HighContrastFilter
103 half nonZeroAlpha = max(color.a, 0.0001);
104 color = half4(color.rgb / nonZeroAlpha, nonZeroAlpha);
105 color.rgb = color.rgb * color.rgb;
106 half fmax = max(color.r, max(color.g, color.b));
107 half fmin = min(color.r, min(color.g, color.b));
108 half l = (fmax + fmin) / 2;
109 half h;
110 half s;
111 if (fmax == fmin) {
112 h = 0;
113 s = 0;
114 } else {
115 half d = fmax - fmin;
116 s = l > 0.5 ? d / (2 - fmax - fmin) : d / (fmax + fmin);
117 if (color.r >= color.g && color.r >= color.b) {
118 h = (color.g - color.b) / d + (color.g < color.b ? 6 : 0);
119 } else if (color.g >= color.b) {
120 h = (color.b - color.r) / d + 2;
121 } else {
122 h = (color.r - color.g) / d + 4;
123 }
124 }
125 h /= 6;
126 l = 1.0 - l;
127 if (s == 0) {
128 color = half4(l, l, l, 0);
129 } else {
130 half q = l < 0.5 ? l * (1 + s) : l + s - l * s;
131 half p = 2 * l - q;
132 color.r = hue2rgb_Stage2(p, q, h + 1 / 3.);
133 color.g = hue2rgb_Stage2(p, q, h);
134 color.b = hue2rgb_Stage2(p, q, h - 1 / 3.);
135 }
136 if (ucontrast_Stage2 != 0) {
137 half m = (1 + ucontrast_Stage2) / (1 - ucontrast_Stage2);
138 half off = (-0.5 * m + 0.5);
139 color = m * color + off;
140 }
141 // color = saturate(color);
142 color.rgb = sqrt(color.rgb);
143 color.rgb *= color.a;
144 }
Brian Osman2b1a5442019-06-19 11:40:33 -0400145)";
146
147DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, false, kLumaToAlphaSrc));
148DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, true, kLumaToAlphaSrc));
149
150DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, false, kHighContrastFilterSrc));
151DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, true, kHighContrastFilterSrc));
Brian Osman569f12f2019-06-13 11:23:57 -0400152
153class SkSLInterpreterSortBench : public Benchmark {
154public:
155 SkSLInterpreterSortBench(int groups, int values, const char* src)
156 : fName(SkStringPrintf("sksl_interp_sort_%dx%d", groups, values))
157 , fCode(src)
158 , fGroups(groups)
159 , fValues(values) {
160 }
161
162protected:
163 const char* onGetName() override {
164 return fName.c_str();
165 }
166
167 bool isSuitableFor(Backend backend) override {
168 return backend == kNonRendering_Backend;
169 }
170
171 void onDelayedSetup() override {
172 SkSL::Compiler compiler;
173 SkSL::Program::Settings settings;
174 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, fCode, settings);
175 SkASSERT(compiler.errorCount() == 0);
176 fByteCode = compiler.toByteCode(*program);
177 SkASSERT(compiler.errorCount() == 0);
178 fMain = fByteCode->getFunction("main");
179
180 fSrc.resize(fGroups * fValues);
181 fDst.resize(fGroups * fValues);
182
183 SkRandom rnd;
Brian Osman08a84962019-06-14 10:17:16 -0400184 for (float& x : fSrc) {
185 x = rnd.nextF();
Brian Osman569f12f2019-06-13 11:23:57 -0400186 }
187
188 // Trigger one run now to check correctness
Brian Osman08a84962019-06-14 10:17:16 -0400189 fByteCode->run(fMain, fSrc.data(), fDst.data(), fGroups, nullptr, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400190 for (int i = 0; i < fGroups; ++i) {
191 for (int j = 1; j < fValues; ++j) {
192 SkASSERT(fDst[i * fValues + j] >= fDst[i * fValues + j - 1]);
193 }
194 }
195 }
196
197 void onDraw(int loops, SkCanvas*) override {
198 for (int i = 0; i < loops; i++) {
Brian Osman08a84962019-06-14 10:17:16 -0400199 fByteCode->run(fMain, fSrc.data(), fDst.data(), fGroups, nullptr, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400200 }
201 }
202
203private:
204 SkString fName;
205 SkSL::String fCode;
206 std::unique_ptr<SkSL::ByteCode> fByteCode;
207 const SkSL::ByteCodeFunction* fMain;
208
209 int fGroups;
210 int fValues;
Brian Osman08a84962019-06-14 10:17:16 -0400211 std::vector<float> fSrc;
212 std::vector<float> fDst;
Brian Osman569f12f2019-06-13 11:23:57 -0400213
214 typedef Benchmark INHERITED;
215};
216
217// Currently, this exceeds the interpreter's stack. Consider it a test case for some eventual
218// bounds checking.
219#if 0
220DEF_BENCH(return new SkSLInterpreterSortBench(1024, 32, R"(
Brian Osman08a84962019-06-14 10:17:16 -0400221 float[32] main(float v[32]) {
Brian Osman569f12f2019-06-13 11:23:57 -0400222 for (int i = 1; i < 32; ++i) {
223 for (int j = i; j > 0 && v[j-1] > v[j]; --j) {
Brian Osman08a84962019-06-14 10:17:16 -0400224 float t = v[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400225 v[j] = v[j-1];
226 v[j-1] = t;
227 }
228 }
229 return v;
230 }
231)"));
232#endif