Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
Ben Clayton | 80d154d | 2023-08-11 18:46:29 +0000 | [diff] [blame] | 28 | #ifndef SRC_TINT_CMD_BENCH_BENCH_H_ |
| 29 | #define SRC_TINT_CMD_BENCH_BENCH_H_ |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | |
| 31 | #include <memory> |
| 32 | #include <string> |
dan sinclair | 0a4e2a1 | 2022-06-18 14:34:00 +0000 | [diff] [blame] | 33 | #include <variant> |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
| 35 | #include "benchmark/benchmark.h" |
Ben Clayton | 80d154d | 2023-08-11 18:46:29 +0000 | [diff] [blame] | 36 | #include "src/tint/lang/wgsl/program/program.h" |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 37 | #include "src/tint/utils/macros/compiler.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 38 | #include "src/tint/utils/macros/concat.h" |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 39 | #include "src/tint/utils/result/result.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | |
| 41 | namespace tint::bench { |
| 42 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | /// ProgramAndFile holds a Program and a Source::File. |
| 44 | struct ProgramAndFile { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | /// The tint program parsed from file. |
| 46 | Program program; |
| 47 | /// The source file |
Ben Clayton | ad22d56 | 2023-05-25 09:56:04 +0000 | [diff] [blame] | 48 | std::unique_ptr<Source::File> file; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 51 | /// 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 |
| 54 | bool Initialize(); |
| 55 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | /// LoadInputFile attempts to load a benchmark input file with the given file |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 57 | /// name. Accepts files with the .wgsl and .spv extension. |
| 58 | /// SPIR-V files are automatically converted to WGSL. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | /// @param name the file name |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 60 | /// @returns the loaded Source::File |
| 61 | Result<Source::File> LoadInputFile(std::string name); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | |
| 63 | /// LoadInputFile attempts to load a benchmark input program with the given file |
| 64 | /// name. |
| 65 | /// @param name the file name |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 66 | /// @returns the loaded Program |
| 67 | Result<ProgramAndFile> LoadProgram(std::string name); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 69 | // 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 Clayton | 8fc9b86 | 2023-04-19 15:03:19 +0000 | [diff] [blame] | 75 | #else |
| 76 | #define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(x) |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 77 | #define TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(x) |
Ben Clayton | 8fc9b86 | 2023-04-19 15:03:19 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 80 | /// Declares a benchmark with the given function and WGSL file name |
Ben Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 81 | #define TINT_BENCHMARK_WGSL_PROGRAM(FUNC, WGSL_NAME) BENCHMARK_CAPTURE(FUNC, WGSL_NAME, WGSL_NAME) |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 82 | |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 83 | /// Declares a set of benchmarks for the given function using a list of WGSL files. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | #define TINT_BENCHMARK_WGSL_PROGRAMS(FUNC) \ |
Ben Clayton | 056f97a | 2022-07-19 14:50:33 +0000 | [diff] [blame] | 85 | TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "atan2-const-eval.wgsl"); \ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "cluster-lights.wgsl"); \ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 90 | TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "skinned-shadowed-pbr-fragment.wgsl"); \ |
Ben Clayton | 8fc9b86 | 2023-04-19 15:03:19 +0000 | [diff] [blame] | 91 | TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "skinned-shadowed-pbr-vertex.wgsl"); \ |
| 92 | TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(FUNC) |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 94 | /// 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 Clayton | 9d1b610 | 2023-09-29 12:12:48 +0000 | [diff] [blame] | 100 | TINT_BENCHMARK_SPV_PROGRAMS(FUNC) \ |
| 101 | TINT_REQUIRE_SEMICOLON |
Ben Clayton | 1a8bc1d | 2023-05-14 01:36:30 +0000 | [diff] [blame] | 102 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 103 | } // namespace tint::bench |
| 104 | |
Ben Clayton | 80d154d | 2023-08-11 18:46:29 +0000 | [diff] [blame] | 105 | #endif // SRC_TINT_CMD_BENCH_BENCH_H_ |