Debian Codegraph @

bench/SkSLBench.cpp

raw
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 "bench/SkSLBench.h"
9
10 #include "bench/Benchmark.h"
11 #include "bench/ResultsWriter.h"
12 #include "include/core/SkCanvas.h"
13 #include "src/base/SkArenaAlloc.h"
14 #include "src/core/SkRasterPipeline.h"
15 #include "src/gpu/ganesh/GrCaps.h"
16 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
17 #include "src/gpu/ganesh/mock/GrMockCaps.h"
18 #include "src/sksl/SkSLCompiler.h"
19 #include "src/sksl/SkSLModuleLoader.h"
20 #include "src/sksl/SkSLParser.h"
21 #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h"
22 #include "src/sksl/codegen/SkSLMetalCodeGenerator.h"
23 #include "src/sksl/codegen/SkSLNativeShader.h"
24 #include "src/sksl/codegen/SkSLRasterPipelineBuilder.h"
25 #include "src/sksl/codegen/SkSLRasterPipelineCodeGenerator.h"
26 #include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h"
27 #include "src/sksl/codegen/SkSLWGSLCodeGenerator.h"
28 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
29 #include "src/sksl/ir/SkSLProgram.h"
30
31 #include <regex>
32
33 #include "src/sksl/generated/sksl_shared.minified.sksl"
34 #include "src/sksl/generated/sksl_compute.minified.sksl"
35 #include "src/sksl/generated/sksl_frag.minified.sksl"
36 #include "src/sksl/generated/sksl_gpu.minified.sksl"
37 #include "src/sksl/generated/sksl_public.minified.sksl"
38 #include "src/sksl/generated/sksl_rt_shader.minified.sksl"
39 #include "src/sksl/generated/sksl_vert.minified.sksl"
40 #include "src/sksl/generated/sksl_graphite_frag.minified.sksl"
41 #include "src/sksl/generated/sksl_graphite_vert.minified.sksl"
42
43 class SkSLCompilerStartupBench : public Benchmark {
44 protected:
45 const char* onGetName() override {
46 return "sksl_compiler_startup";
47 }
48
49 bool isSuitableFor(Backend backend) override {
50 return backend == Backend::kNonRendering;
51 }
52
53 void onDraw(int loops, SkCanvas*) override {
54 for (int i = 0; i < loops; i++) {
55 SkSL::Compiler compiler;
56 }
57 }
58 };
59
60 DEF_BENCH(return new SkSLCompilerStartupBench();)
61
62 enum class Output {
63 kNone,
64 kGLSL,
65 kMetal,
66 kSPIRV,
67 kSkRP,
68 kGrMtl,
69 kGrWGSL,
70 };
71
72 class SkSLCompileBench : public Benchmark {
73 public:
74 static const char* output_string(Output output) {
75 switch (output) {
76 case Output::kNone: return "";
77 case Output::kGLSL: return "glsl_";
78 case Output::kMetal: return "metal_";
79 case Output::kSPIRV: return "spirv_";
80 case Output::kGrMtl: return "grmtl_";
81 case Output::kGrWGSL: return "grwgsl_";
82 case Output::kSkRP: return "skrp_";
83 }
84 SkUNREACHABLE;
85 }
86
87 SkSLCompileBench(std::string name, const char* src, bool optimize, Output output)
88 : fName(std::string("sksl_") + (optimize ? "" : "unoptimized_") +
89 output_string(output) + name)
90 , fSrc(src)
91 , fCaps(GrContextOptions(), GrMockOptions())
92 , fOutput(output) {
93 fSettings.fOptimize = optimize;
94 // The test programs we compile don't follow Vulkan rules and thus produce invalid SPIR-V.
95 // This is harmless, so long as we don't try to validate them.
96 fSettings.fValidateSPIRV = false;
97
98 this->fixUpSource();
99 }
100
101 protected:
102 const char* onGetName() override {
103 return fName.c_str();
104 }
105
106 bool isSuitableFor(Backend backend) override {
107 #if !defined(SK_GRAPHITE)
108 if (this->usesGraphite()) {
109 return false;
110 }
111 #endif
112 return backend == Backend::kNonRendering;
113 }
114
115 bool usesRuntimeShader() const {
116 return fOutput == Output::kSkRP;
117 }
118
119 bool usesGraphite() const {
120 return fOutput == Output::kGrMtl || fOutput == Output::kGrWGSL;
121 }
122
123 void fixUpSource() {
124 auto fixup = [this](const char* input, const char* replacement) {
125 fSrc = std::regex_replace(fSrc, std::regex(input), replacement);
126 };
127
128 // Runtime shaders have slightly different conventions than fragment shaders.
129 // Perform a handful of fixups to compensate. These are hand-tuned for our current set of
130 // test shaders and will probably need to be updated if we add more.
131 if (this->usesRuntimeShader()) {
132 fixup(R"(void main\(\))", "half4 main(float2 xy)");
133 fixup(R"(sk_FragColor =)", "return");
134 fixup(R"(sk_FragCoord)", "_FragCoord");
135 fixup(R"(sampler2D )", "uniform shader ");
136 fixup(R"((flat |noperspective |)in )", "uniform ");
137 fixup(R"(sample\(([A-Za-z0-9_]+), ([A-Za-z0-9_]+)\))", "$01.eval($02)");
138 fSrc = "#version 300\nuniform float4 _FragCoord;\n" + fSrc;
139 }
140 }
141
142 void onDraw(int loops, SkCanvas* canvas) override {
143 SkSL::ProgramKind kind;
144 if (this->usesRuntimeShader()) {
145 kind = SkSL::ProgramKind::kRuntimeShader;
146 } else if (this->usesGraphite()) {
147 kind = SkSL::ProgramKind::kGraphiteFragment;
148 } else {
149 kind = SkSL::ProgramKind::kFragment;
150 }
151 for (int i = 0; i < loops; i++) {
152 std::unique_ptr<SkSL::Program> program = fCompiler.convertProgram(kind, fSrc,
153 fSettings);
154 if (fCompiler.errorCount()) {
155 SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str());
156 }
157 SkSL::NativeShader result;
158 switch (fOutput) {
159 case Output::kNone:
160 break;
161
162 case Output::kGLSL:
163 SkAssertResult(SkSL::ToGLSL(*program, fCaps.shaderCaps(), &result));
164 break;
165
166 case Output::kMetal:
167 case Output::kGrMtl:
168 SkAssertResult(SkSL::ToMetal(*program, fCaps.shaderCaps(), &result));
169 break;
170
171 case Output::kSPIRV:
172 SkAssertResult(SkSL::ToSPIRV(*program, fCaps.shaderCaps(), &result));
173 break;
174
175 case Output::kGrWGSL:
176 SkAssertResult(SkSL::ToWGSL(*program, fCaps.shaderCaps(), &result));
177 break;
178
179 case Output::kSkRP:
180 SkAssertResult(CompileToSkRP(*program));
181 break;
182 }
183 }
184 }
185
186 static bool CompileToSkRP(const SkSL::Program& program) {
187 const SkSL::FunctionDeclaration* main = program.getFunction("main");
188 if (!main) {
189 return false;
190 }
191
192 // Compile our program.
193 std::unique_ptr<SkSL::RP::Program> rasterProg = SkSL::MakeRasterPipelineProgram(
194 program, *main->definition(), /*debugTrace=*/nullptr, /*writeTraceOps=*/false);
195 if (!rasterProg) {
196 return false;
197 }
198
199 // We need to supply a valid uniform range, but the uniform values inside don't actually
200 // matter, since we aren't going to run the shader.
201 float uniformBuffer[1024];
202 if (rasterProg->numUniforms() > (int)std::size(uniformBuffer)) {
203 return false;
204 }
205
206 // Append the program to a raster pipeline.
207 SkSTArenaAlloc<2048> alloc;
208 SkRasterPipeline pipeline(&alloc);
209 rasterProg->appendStages(&pipeline,
210 &alloc,
211 /*callbacks=*/nullptr,
212 /*uniforms=*/{uniformBuffer, (size_t)rasterProg->numUniforms()});
213 return true;
214 }
215
216 private:
217 std::string fName;
218 std::string fSrc;
219 GrMockCaps fCaps;
220 SkSL::Compiler fCompiler;
221 SkSL::ProgramSettings fSettings;
222 Output fOutput;
223
224 using INHERITED = Benchmark;
225 };
226
227 ///////////////////////////////////////////////////////////////////////////////
228
229 #define COMPILER_BENCH(name, text) \
230 static constexpr char name ## _SRC[] = text; \
231 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/false, Output::kNone);) \
232 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kNone);) \
233 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kGLSL);) \
234 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kMetal);) \
235 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kSPIRV);) \
236 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kSkRP);)
237
238 // This fragment shader is from the third tile on the top row of GM_gradients_2pt_conical_outside.
239 // To get an ES2 compatible shader, nonconstantArrayIndexSupport in GrShaderCaps is forced off.
240 COMPILER_BENCH(large, R"(
241 uniform half4 uthresholds1_7_S1_c0_c0_c0;
242 uniform half4 uthresholds9_13_S1_c0_c0_c0;
243 uniform float4 uscale_S1_c0_c0_c0[4];
244 uniform float4 ubias_S1_c0_c0_c0[4];
245 uniform half uinvR1_S1_c0_c0_c1_c0;
246 uniform half ufx_S1_c0_c0_c1_c0;
247 uniform float3x3 umatrix_S1_c0_c0_c1;
248 uniform half4 uleftBorderColor_S1_c0_c0;
249 uniform half4 urightBorderColor_S1_c0_c0;
250 uniform float3x3 umatrix_S1_c1;
251 uniform half urange_S1;
252 sampler2D uTextureSampler_0_S1;
253 flat in half4 vcolor_S0;
254 noperspective in float2 vTransformedCoords_6_S0;
255 half4 UnrolledBinaryColorizer_S1_c0_c0_c0(half4 _input, float2 _coords)
256 {
257 half4 _tmp_0_inColor = _input;
258 float2 _tmp_1_coords = _coords;
259 half t = half(_tmp_1_coords.x);
260 float4 s;
261 float4 b;
262 {
263 if (t < uthresholds1_7_S1_c0_c0_c0.y)
264 {
265 if (t < uthresholds1_7_S1_c0_c0_c0.x)
266 {
267 s = uscale_S1_c0_c0_c0[0];
268 b = ubias_S1_c0_c0_c0[0];
269 }
270 else
271 {
272 s = uscale_S1_c0_c0_c0[1];
273 b = ubias_S1_c0_c0_c0[1];
274 }
275 }
276 else
277 {
278 if (t < uthresholds1_7_S1_c0_c0_c0.z)
279 {
280 s = uscale_S1_c0_c0_c0[2];
281 b = ubias_S1_c0_c0_c0[2];
282 }
283 else
284 {
285 s = uscale_S1_c0_c0_c0[3];
286 b = ubias_S1_c0_c0_c0[3];
287 }
288 }
289 }
290 return half4(half4(float(t) * s + b));
291 }
292 half4 TwoPointConicalFocalLayout_S1_c0_c0_c1_c0(half4 _input)
293 {
294 half4 _tmp_2_inColor = _input;
295 float2 _tmp_3_coords = vTransformedCoords_6_S0;
296 float t = -1.0;
297 half v = 1.0;
298 float x_t = -1.0;
299 if (bool(int(0)))
300 {
301 x_t = dot(_tmp_3_coords, _tmp_3_coords) / _tmp_3_coords.x;
302 }
303 else if (bool(int(0)))
304 {
305 x_t = length(_tmp_3_coords) - _tmp_3_coords.x * float(uinvR1_S1_c0_c0_c1_c0);
306 }
307 else
308 {
309 float temp = _tmp_3_coords.x * _tmp_3_coords.x - _tmp_3_coords.y * _tmp_3_coords.y;
310 if (temp >= 0.0)
311 {
312 if (bool(int(0)) || !bool(int(1)))
313 {
314 x_t = -sqrt(temp) - _tmp_3_coords.x * float(uinvR1_S1_c0_c0_c1_c0);
315 }
316 else
317 {
318 x_t = sqrt(temp) - _tmp_3_coords.x * float(uinvR1_S1_c0_c0_c1_c0);
319 }
320 }
321 }
322 if (!bool(int(0)))
323 {
324 if (x_t <= 0.0)
325 {
326 v = -1.0;
327 }
328 }
329 if (bool(int(1)))
330 {
331 if (bool(int(0)))
332 {
333 t = x_t;
334 }
335 else
336 {
337 t = x_t + float(ufx_S1_c0_c0_c1_c0);
338 }
339 }
340 else
341 {
342 if (bool(int(0)))
343 {
344 t = -x_t;
345 }
346 else
347 {
348 t = -x_t + float(ufx_S1_c0_c0_c1_c0);
349 }
350 }
351 if (bool(int(0)))
352 {
353 t = 1.0 - t;
354 }
355 return half4(half4(half(t), v, 0.0, 0.0));
356 }
357 half4 MatrixEffect_S1_c0_c0_c1(half4 _input)
358 {
359 return TwoPointConicalFocalLayout_S1_c0_c0_c1_c0(_input);
360 }
361 half4 ClampedGradient_S1_c0_c0(half4 _input)
362 {
363 half4 _tmp_4_inColor = _input;
364 half4 t = MatrixEffect_S1_c0_c0_c1(_tmp_4_inColor);
365 half4 outColor;
366 if (!bool(int(0)) && t.y < 0.0)
367 {
368 outColor = half4(0.0);
369 }
370 else if (t.x < 0.0)
371 {
372 outColor = uleftBorderColor_S1_c0_c0;
373 }
374 else if (t.x > 1.0)
375 {
376 outColor = urightBorderColor_S1_c0_c0;
377 }
378 else
379 {
380 outColor = UnrolledBinaryColorizer_S1_c0_c0_c0(_tmp_4_inColor, float2(half2(t.x, 0.0)));
381 }
382 return half4(outColor);
383 }
384 half4 DisableCoverageAsAlpha_S1_c0(half4 _input)
385 {
386 _input = ClampedGradient_S1_c0_c0(_input);
387 half4 _tmp_5_inColor = _input;
388 return half4(_input);
389 }
390 half4 TextureEffect_S1_c1_c0(half4 _input, float2 _coords)
391 {
392 return sample(uTextureSampler_0_S1, _coords).000r;
393 }
394 half4 MatrixEffect_S1_c1(half4 _input, float2 _coords)
395 {
396 return TextureEffect_S1_c1_c0(_input, float3x2(umatrix_S1_c1) * _coords.xy1);
397 }
398 half4 Dither_S1(half4 _input)
399 {
400 half4 _tmp_6_inColor = _input;
401 half4 color = DisableCoverageAsAlpha_S1_c0(_tmp_6_inColor);
402 half value = MatrixEffect_S1_c1(_tmp_6_inColor, sk_FragCoord.xy).w - 0.5;
403 return half4(half4(clamp(color.xyz + value * urange_S1, 0.0, color.w), color.w));
404 }
405 void main()
406 {
407 // Stage 0, QuadPerEdgeAAGeometryProcessor
408 half4 outputColor_S0;
409 outputColor_S0 = vcolor_S0;
410 const half4 outputCoverage_S0 = half4(1);
411 half4 output_S1;
412 output_S1 = Dither_S1(outputColor_S0);
413 {
414 // Xfer Processor: Porter Duff
415 sk_FragColor = output_S1 * outputCoverage_S0;
416 }
417 }
418 )")
419
420 // This fragment shader is taken from GM_BlurDrawImage.
421 COMPILER_BENCH(medium, R"(
422 uniform float3x3 umatrix_S1_c0;
423 uniform float3x3 umatrix_S2_c0_c0;
424 uniform float4 urect_S2_c0;
425 sampler2D uTextureSampler_0_S1;
426 sampler2D uTextureSampler_0_S2;
427 flat in half4 vcolor_S0;
428 noperspective in float2 vTransformedCoords_3_S0;
429 half4 TextureEffect_S1_c0_c0(half4 _input)
430 {
431 return sample(uTextureSampler_0_S1, vTransformedCoords_3_S0);
432 }
433 half4 MatrixEffect_S1_c0(half4 _input)
434 {
435 return TextureEffect_S1_c0_c0(_input);
436 }
437 half4 DisableCoverageAsAlpha_S1(half4 _input)
438 {
439 _input = MatrixEffect_S1_c0(_input);
440 half4 _tmp_0_inColor = _input;
441 return half4(_input);
442 }
443 half4 TextureEffect_S2_c0_c0_c0(half4 _input, float2 _coords)
444 {
445 return sample(uTextureSampler_0_S2, _coords).000r;
446 }
447 half4 MatrixEffect_S2_c0_c0(half4 _input, float2 _coords)
448 {
449 return TextureEffect_S2_c0_c0_c0(_input, float3x2(umatrix_S2_c0_c0) * _coords.xy1);
450 }
451 half4 RectBlur_S2_c0(half4 _input, float2 _coords)
452 {
453 half4 _tmp_1_inColor = _input;
454 float2 _tmp_2_coords = _coords;
455 half xCoverage;
456 half yCoverage;
457 if (bool(int(1)))
458 {
459 half2 xy = max(half2(urect_S2_c0.xy - _tmp_2_coords), half2(_tmp_2_coords - urect_S2_c0.zw));
460 xCoverage = MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(xy.x, 0.5))).w;
461 yCoverage = MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(xy.y, 0.5))).w;
462 }
463 else
464 {
465 half4 rect = half4(half2(urect_S2_c0.xy - _tmp_2_coords), half2(_tmp_2_coords - urect_S2_c0.zw));
466 xCoverage = (1.0 - MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(rect.x, 0.5))).w) - MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(rect.z, 0.5))).w;
467 yCoverage = (1.0 - MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(rect.y, 0.5))).w) - MatrixEffect_S2_c0_c0(_tmp_1_inColor, float2(half2(rect.w, 0.5))).w;
468 }
469 return half4((_input * xCoverage) * yCoverage);
470 }
471 half4 DeviceSpace_S2(half4 _input)
472 {
473 return RectBlur_S2_c0(_input, sk_FragCoord.xy);
474 }
475 void main()
476 {
477 // Stage 0, QuadPerEdgeAAGeometryProcessor
478 half4 outputColor_S0;
479 outputColor_S0 = vcolor_S0;
480 const half4 outputCoverage_S0 = half4(1);
481 half4 output_S1;
482 output_S1 = DisableCoverageAsAlpha_S1(outputColor_S0);
483 half4 output_S2;
484 output_S2 = DeviceSpace_S2(outputCoverage_S0);
485 {
486 // Xfer Processor: Porter Duff
487 sk_FragColor = output_S1 * output_S2;
488 }
489 }
490 )")
491
492 // This fragment shader is taken from GM_lcdtext.
493 COMPILER_BENCH(small, R"(
494 sampler2D uTextureSampler_0_S0;
495 noperspective in float2 vTextureCoords_S0;
496 flat in float vTexIndex_S0;
497 noperspective in half4 vinColor_S0;
498 void main()
499 {
500 // Stage 0, BitmapText
501 half4 outputColor_S0;
502 outputColor_S0 = vinColor_S0;
503 half4 texColor;
504 {
505 texColor = sample(uTextureSampler_0_S0, vTextureCoords_S0).rrrr;
506 }
507 half4 outputCoverage_S0 = texColor;
508 {
509 // Xfer Processor: Porter Duff
510 sk_FragColor = outputColor_S0 * outputCoverage_S0;
511 }
512 }
513 )")
514
515 COMPILER_BENCH(tiny, "void main() { sk_FragColor = half4(1); }")
516
517 #define GRAPHITE_BENCH(name, text) \
518 static constexpr char name##_SRC[] = text; \
519 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kGrMtl);) \
520 DEF_BENCH(return new SkSLCompileBench(#name, name##_SRC, /*optimize=*/true, Output::kGrWGSL);)
521
522 // This fragment shader is from the third tile on the top row of GM_gradients_2pt_conical_outside.
523 GRAPHITE_BENCH(graphite_large, R"(
524 layout(location=0) in flat int shadingSsboIndexVar;
525 layout(location=1) in float2 localCoordsVar;
526 layout(location=2) in float4 jacobian;
527 layout(location=3) in float4 edgeDistances;
528 layout(location=4) in float4 xRadii;
529 layout(location=5) in float4 yRadii;
530 layout(location=6) in float2 strokeParams;
531 layout(location=7) in float2 perPixelControl;
532 struct FSUniformData
533 {
534 // 0 - SolidColor uniforms
535 float4 color_0;
536 // 2 - ConicalGradient8 uniforms
537 float4 colors_2[8];
538 float4 offsets_2[2];
539 float2 point0_2;
540 float2 point1_2;
541 float radius0_2;
542 float radius1_2;
543 int tilemode_2;
544 int colorSpace_2;
545 int doUnPremul_2;
546 // 3 - ColorSpaceTransform uniforms
547 int flags_3;
548 int srcKind_3;
549 half3x3 gamutTransform_3;
550 int dstKind_3;
551 half4x4 csXformCoeffs_3;
552 // 4 - DitherShader uniforms
553 half range_4;
554 }
555 ;
556 layout (binding=2) buffer FSUniforms
557 {
558 FSUniformData fsUniformData[];
559 }
560 ;
561 // 4 - DitherShader samplers
562 layout(binding=0) sampler2D sampler_4;
563 // [1] 1: ColorFilterShader
564 half4 ColorFilterShader_1(half4 inColor, half4 destColor, float2 coords)
565 {
566 return sk_color_space_transform(sk_conical_grad_8_shader(coords, fsUniformData[shadingSsboIndexVar].colors_2, fsUniformData[shadingSsboIndexVar].offsets_2, fsUniformData[shadingSsboIndexVar].point0_2, fsUniformData[shadingSsboIndexVar].point1_2, fsUniformData[shadingSsboIndexVar].radius0_2, fsUniformData[shadingSsboIndexVar].radius1_2, fsUniformData[shadingSsboIndexVar].tilemode_2, fsUniformData[shadingSsboIndexVar].colorSpace_2, fsUniformData[shadingSsboIndexVar].doUnPremul_2), fsUniformData[shadingSsboIndexVar].flags_3, fsUniformData[shadingSsboIndexVar].srcKind_3, fsUniformData[shadingSsboIndexVar].gamutTransform_3, fsUniformData[shadingSsboIndexVar].dstKind_3, fsUniformData[shadingSsboIndexVar].csXformCoeffs_3, half4(0), half4(0));
567 }
568 void main()
569 {
570 half4 initialColor = half4(0);
571 // [0] SolidColor
572 half4 outColor_0 = sk_solid_shader(fsUniformData[shadingSsboIndexVar].color_0);
573 // [1] ColorFilterShader
574 half4 outColor_1 = ColorFilterShader_1(outColor_0, half4(1), localCoordsVar);
575 // [4] DitherShader
576 half4 outColor_4 = sk_dither_shader(outColor_1, localCoordsVar, fsUniformData[shadingSsboIndexVar].range_4, sampler_4);
577 // [5] SrcOver
578 half4 outColor_5 = outColor_4;
579 half4 outputCoverage;
580 outputCoverage = analytic_rrect_coverage_fn(sk_FragCoord, jacobian, edgeDistances, xRadii, yRadii, strokeParams, perPixelControl);
581 sk_FragColor = outColor_5 * outputCoverage;
582 }
583 )")
584
585 // This fragment shader is taken from GM_lcdtext.
586 GRAPHITE_BENCH(graphite_small, R"(
587 layout(location=0) in flat int shadingSsboIndexVar;
588 layout(location=1) in float2 textureCoords;
589 layout(location=2) in half texIndex;
590 layout(location=3) in half maskFormat;
591 layout (binding=1) uniform StepUniforms
592 {
593 layout(offset=0) float4x4 subRunDeviceMatrix;
594 layout(offset=64) float4x4 deviceToLocal;
595 layout(offset=128) float2 atlasSizeInv;
596 }
597 ;
598 struct FSUniformData
599 {
600 // 0 - SolidColor uniforms
601 float4 color_0;
602 }
603 ;
604 layout (binding=2) buffer FSUniforms
605 {
606 FSUniformData fsUniformData[];
607 }
608 ;
609 layout(binding=0) sampler2D text_atlas_0;
610 layout(binding=1) sampler2D text_atlas_1;
611 layout(binding=2) sampler2D text_atlas_2;
612 layout(binding=3) sampler2D text_atlas_3;
613 void main()
614 {
615 half4 initialColor = half4(0);
616 // [0] SolidColor
617 half4 outColor_0 = sk_solid_shader(fsUniformData[shadingSsboIndexVar].color_0);
618 // [1] SrcOver
619 half4 outColor_1 = outColor_0;
620 half4 outputCoverage;
621 outputCoverage = bitmap_text_coverage_fn(sample_indexed_atlas(textureCoords, int(texIndex), text_atlas_0, text_atlas_1, text_atlas_2, text_atlas_3), int(maskFormat));
622 sk_FragColor = outColor_1 * outputCoverage;
623 }
624 )")
625
626 #if defined(SK_BUILD_FOR_UNIX)
627
628 #include <malloc.h>
629 static int64_t heap_bytes_used() {
630 return (int64_t)mallinfo().uordblks;
631 }
632
633 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
634
635 #include <malloc/malloc.h>
636 static int64_t heap_bytes_used() {
637 malloc_statistics_t stats;
638 malloc_zone_pressure_relief(malloc_default_zone(), 0);
639 malloc_zone_statistics(malloc_default_zone(), &stats);
640 return (int64_t)stats.size_in_use;
641 }
642
643 #else
644
645 static int64_t heap_bytes_used() {
646 return -1;
647 }
648
649 #endif
650
651 static void bench(NanoJSONResultsWriter* log, const char* name, int bytes) {
652 SkDEBUGCODE(SkDebugf("%s: %d bytes\n", name, bytes);)
653 log->beginObject(name); // test
654 log->beginObject("meta"); // config
655 log->appendS32("bytes", bytes); // sub_result
656 log->endObject(); // config
657 log->endObject(); // test
658 }
659
660 // These benchmarks aren't timed, they produce memory usage statistics. They run standalone, and
661 // directly add their results to the nanobench log.
662 void RunSkSLModuleBenchmarks(NanoJSONResultsWriter* log) {
663 // Heap used by a default compiler (with no modules loaded)
664 int64_t before = heap_bytes_used();
665 SkSL::Compiler compiler;
666 int baselineBytes = heap_bytes_used();
667 if (baselineBytes >= 0) {
668 baselineBytes = (baselineBytes - before);
669 bench(log, "sksl_compiler_baseline", baselineBytes);
670 }
671
672 // Heap used by a compiler with the two main GPU modules (fragment + vertex) and runtime effects
673 // (shader + color filter + blender) loaded. Ganesh will load all of these in regular usage.
674 before = heap_bytes_used();
675 compiler.moduleForProgramKind(SkSL::ProgramKind::kVertex);
676 compiler.moduleForProgramKind(SkSL::ProgramKind::kFragment);
677 compiler.moduleForProgramKind(SkSL::ProgramKind::kRuntimeColorFilter);
678 compiler.moduleForProgramKind(SkSL::ProgramKind::kRuntimeShader);
679 compiler.moduleForProgramKind(SkSL::ProgramKind::kRuntimeBlender);
680 compiler.moduleForProgramKind(SkSL::ProgramKind::kPrivateRuntimeColorFilter);
681 compiler.moduleForProgramKind(SkSL::ProgramKind::kPrivateRuntimeShader);
682 compiler.moduleForProgramKind(SkSL::ProgramKind::kPrivateRuntimeBlender);
683 int64_t gpuBytes = heap_bytes_used();
684 if (gpuBytes >= 0) {
685 gpuBytes = (gpuBytes - before) + baselineBytes;
686 bench(log, "sksl_compiler_gpu", gpuBytes);
687 }
688
689 #if defined(SK_GRAPHITE)
690 // Heap used by a compiler with the Graphite modules loaded.
691 before = heap_bytes_used();
692 compiler.moduleForProgramKind(SkSL::ProgramKind::kGraphiteVertex);
693 compiler.moduleForProgramKind(SkSL::ProgramKind::kGraphiteFragment);
694 int64_t graphiteBytes = heap_bytes_used();
695 if (graphiteBytes >= 0) {
696 graphiteBytes = (graphiteBytes - before) + gpuBytes;
697 bench(log, "sksl_compiler_graphite", graphiteBytes);
698 }
699
700 // Heap used by a compiler with compute-shader support loaded.
701 before = heap_bytes_used();
702 compiler.moduleForProgramKind(SkSL::ProgramKind::kCompute);
703 int64_t computeBytes = heap_bytes_used();
704 if (computeBytes >= 0) {
705 computeBytes = (computeBytes - before) + baselineBytes;
706 bench(log, "sksl_compiler_compute", computeBytes);
707 }
708 #endif
709
710 // Report the minified module sizes.
711 int compilerGPUBinarySize = std::size(SKSL_MINIFIED_sksl_shared) +
712 std::size(SKSL_MINIFIED_sksl_gpu) +
713 std::size(SKSL_MINIFIED_sksl_vert) +
714 std::size(SKSL_MINIFIED_sksl_frag) +
715 std::size(SKSL_MINIFIED_sksl_public) +
716 std::size(SKSL_MINIFIED_sksl_rt_shader);
717 bench(log, "sksl_binary_size_gpu", compilerGPUBinarySize);
718
719 int compilerGraphiteBinarySize = std::size(SKSL_MINIFIED_sksl_graphite_frag) +
720 std::size(SKSL_MINIFIED_sksl_graphite_vert);
721 bench(log, "sksl_binary_size_graphite", compilerGraphiteBinarySize);
722
723 int compilerComputeBinarySize = std::size(SKSL_MINIFIED_sksl_compute);
724 bench(log, "sksl_binary_size_compute", compilerComputeBinarySize);
725 }
726
727 class SkSLModuleLoaderBench : public Benchmark {
728 public:
729 SkSLModuleLoaderBench(const char* name, std::vector<SkSL::ProgramKind> moduleList)
730 : fName(name), fModuleList(std::move(moduleList)) {}
731
732 const char* onGetName() override {
733 return fName;
734 }
735
736 bool isSuitableFor(Backend backend) override {
737 return backend == Backend::kNonRendering;
738 }
739
740 bool shouldLoop() const override {
741 return false;
742 }
743
744 void onPreDraw(SkCanvas*) override {
745 SkSL::ModuleLoader::Get().unloadModules();
746 }
747
748 void onDraw(int loops, SkCanvas*) override {
749 SkASSERT(loops == 1);
750 SkSL::Compiler compiler;
751 for (SkSL::ProgramKind kind : fModuleList) {
752 compiler.moduleForProgramKind(kind);
753 }
754 }
755
756 const char* fName;
757 std::vector<SkSL::ProgramKind> fModuleList;
758 };
759
760 DEF_BENCH(return new SkSLModuleLoaderBench("sksl_module_loader_ganesh",
761 {
762 SkSL::ProgramKind::kVertex,
763 SkSL::ProgramKind::kFragment,
764 SkSL::ProgramKind::kRuntimeColorFilter,
765 SkSL::ProgramKind::kRuntimeShader,
766 SkSL::ProgramKind::kRuntimeBlender,
767 SkSL::ProgramKind::kPrivateRuntimeColorFilter,
768 SkSL::ProgramKind::kPrivateRuntimeShader,
769 SkSL::ProgramKind::kPrivateRuntimeBlender,
770 SkSL::ProgramKind::kCompute,
771 });)
772
773 DEF_BENCH(return new SkSLModuleLoaderBench("sksl_module_loader_graphite",
774 {
775 SkSL::ProgramKind::kVertex,
776 SkSL::ProgramKind::kFragment,
777 SkSL::ProgramKind::kRuntimeColorFilter,
778 SkSL::ProgramKind::kRuntimeShader,
779 SkSL::ProgramKind::kRuntimeBlender,
780 SkSL::ProgramKind::kPrivateRuntimeColorFilter,
781 SkSL::ProgramKind::kPrivateRuntimeShader,
782 SkSL::ProgramKind::kPrivateRuntimeBlender,
783 SkSL::ProgramKind::kCompute,
784 SkSL::ProgramKind::kGraphiteVertex,
785 SkSL::ProgramKind::kGraphiteFragment,
786 });)