device/shader_compiler.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file shader_compiler.h | ||
| 3 | /// @brief Slang modules compiled at run time: to SPIR-V for Vulkan, to WGSL for WebGPU. | ||
| 4 | /// | ||
| 5 | /// References: | ||
| 6 | /// - Y. He, K. Fatahalian and T. Foley, "Slang: language mechanisms for extensible real-time | ||
| 7 | /// shading systems", ACM Transactions on Graphics 37(4), 141, 2018. | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <cstdint> | ||
| 11 | #include <memory> | ||
| 12 | #include <span> | ||
| 13 | #include <string> | ||
| 14 | #include <string_view> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace ernie { | ||
| 18 | |||
| 19 | /// @brief A Slang module: its name, as `import`s name it, and its source. | ||
| 20 | 366 | struct ShaderModule { | |
| 21 | std::string name; | ||
| 22 | std::string source; | ||
| 23 | }; | ||
| 24 | |||
| 25 | /// @brief Compiles Slang modules, built-in kernels and users' models alike. Each compiler has | ||
| 26 | /// its own Slang session, started at its first compile; one thread compiles at a time. | ||
| 27 | class ShaderCompiler { | ||
| 28 | public: | ||
| 29 | ShaderCompiler(); | ||
| 30 | ~ShaderCompiler(); | ||
| 31 | ShaderCompiler(const ShaderCompiler&) = delete; | ||
| 32 | ShaderCompiler& operator=(const ShaderCompiler&) = delete; | ||
| 33 | |||
| 34 | /// @brief Compiles `entryPoint` of the last module to SPIR-V 1.6. | ||
| 35 | /// @param modules Imported modules before the modules importing them; the last holds the | ||
| 36 | /// entry point. | ||
| 37 | /// @throws std::invalid_argument with Slang's diagnostics if a module fails or has no such | ||
| 38 | /// entry point. | ||
| 39 | std::vector<std::uint32_t> Spirv(std::span<const ShaderModule> modules, | ||
| 40 | std::string_view entryPoint) const; | ||
| 41 | /// @brief Compiles `entryPoint` of the last module to WGSL, as `Spirv` does. | ||
| 42 | std::string Wgsl(std::span<const ShaderModule> modules, std::string_view entryPoint) const; | ||
| 43 | |||
| 44 | private: | ||
| 45 | struct Session; | ||
| 46 | std::unique_ptr<Session> session_; | ||
| 47 | }; | ||
| 48 | |||
| 49 | } // namespace ernie | ||
| 50 |