blob: e6cf6e7d565a944730bdcdc788b921882d786633 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2022 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
Ben Clayton80d154d2023-08-11 18:46:29 +000028#ifndef SRC_TINT_CMD_BENCH_BENCH_H_
29#define SRC_TINT_CMD_BENCH_BENCH_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
31#include <memory>
32#include <string>
dan sinclair0a4e2a12022-06-18 14:34:00 +000033#include <variant>
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
35#include "benchmark/benchmark.h"
Ben Clayton80d154d2023-08-11 18:46:29 +000036#include "src/tint/lang/wgsl/program/program.h"
Ben Clayton9d1b6102023-09-29 12:12:48 +000037#include "src/tint/utils/macros/compiler.h"
dan sinclair22b4dd22023-07-21 00:40:07 +000038#include "src/tint/utils/macros/concat.h"
Ben Clayton9d1b6102023-09-29 12:12:48 +000039#include "src/tint/utils/result/result.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
41namespace tint::bench {
42
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043/// ProgramAndFile holds a Program and a Source::File.
44struct ProgramAndFile {
dan sinclair41e4d9a2022-05-01 14:40:55 +000045 /// The tint program parsed from file.
46 Program program;
47 /// The source file
Ben Claytonad22d562023-05-25 09:56:04 +000048 std::unique_ptr<Source::File> file;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049};
50
Ben Clayton9d1b6102023-09-29 12:12:48 +000051/// Initializes the internal state for benchmarking.
52/// Must be called once by the benchmark executable entry point.
53/// @returns true on success, false of failure
54bool Initialize();
55
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056/// LoadInputFile attempts to load a benchmark input file with the given file
Ben Clayton1a8bc1d2023-05-14 01:36:30 +000057/// name. Accepts files with the .wgsl and .spv extension.
58/// SPIR-V files are automatically converted to WGSL.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059/// @param name the file name
Ben Clayton9d1b6102023-09-29 12:12:48 +000060/// @returns the loaded Source::File
61Result<Source::File> LoadInputFile(std::string name);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
63/// LoadInputFile attempts to load a benchmark input program with the given file
64/// name.
65/// @param name the file name
Ben Clayton9d1b6102023-09-29 12:12:48 +000066/// @returns the loaded Program
67Result<ProgramAndFile> LoadProgram(std::string name);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000068
Ben Clayton1a8bc1d2023-05-14 01:36:30 +000069// If TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER is defined, include that to
70// declare the TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS() and TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS()
71// macros, which appends external programs to the TINT_BENCHMARK_WGSL_PROGRAMS() and
72// TINT_BENCHMARK_SPV_PROGRAMS() list.
73#ifdef TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER
74#include TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER
Ben Clayton8fc9b862023-04-19 15:03:19 +000075#else
76#define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(x)
Ben Clayton1a8bc1d2023-05-14 01:36:30 +000077#define TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(x)
Ben Clayton8fc9b862023-04-19 15:03:19 +000078#endif
79
Ryan Harrisondbc13af2022-02-21 15:19:07 +000080/// Declares a benchmark with the given function and WGSL file name
Ben Clayton9d1b6102023-09-29 12:12:48 +000081#define TINT_BENCHMARK_WGSL_PROGRAM(FUNC, WGSL_NAME) BENCHMARK_CAPTURE(FUNC, WGSL_NAME, WGSL_NAME)
Ryan Harrisondbc13af2022-02-21 15:19:07 +000082
Ben Clayton1a8bc1d2023-05-14 01:36:30 +000083/// Declares a set of benchmarks for the given function using a list of WGSL files.
dan sinclair41e4d9a2022-05-01 14:40:55 +000084#define TINT_BENCHMARK_WGSL_PROGRAMS(FUNC) \
Ben Clayton056f97a2022-07-19 14:50:33 +000085 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "atan2-const-eval.wgsl"); \
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "cluster-lights.wgsl"); \
dan sinclair41e4d9a2022-05-01 14:40:55 +000087 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "metaball-isosurface.wgsl"); \
88 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "particles.wgsl"); \
89 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "shadow-fragment.wgsl"); \
dan sinclair41e4d9a2022-05-01 14:40:55 +000090 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "skinned-shadowed-pbr-fragment.wgsl"); \
Ben Clayton8fc9b862023-04-19 15:03:19 +000091 TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "skinned-shadowed-pbr-vertex.wgsl"); \
92 TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(FUNC)
Ryan Harrisondbc13af2022-02-21 15:19:07 +000093
Ben Clayton1a8bc1d2023-05-14 01:36:30 +000094/// Declares a set of benchmarks for the given function using a list of SPIR-V files.
95#define TINT_BENCHMARK_SPV_PROGRAMS(FUNC) TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(FUNC)
96
97/// Declares a set of benchmarks for the given function using a list of WGSL and SPIR-V files.
98#define TINT_BENCHMARK_PROGRAMS(FUNC) \
99 TINT_BENCHMARK_WGSL_PROGRAMS(FUNC) \
Ben Clayton9d1b6102023-09-29 12:12:48 +0000100 TINT_BENCHMARK_SPV_PROGRAMS(FUNC) \
101 TINT_REQUIRE_SEMICOLON
Ben Clayton1a8bc1d2023-05-14 01:36:30 +0000102
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000103} // namespace tint::bench
104
Ben Clayton80d154d2023-08-11 18:46:29 +0000105#endif // SRC_TINT_CMD_BENCH_BENCH_H_