Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_shadermanager.hpp
Go to the documentation of this file.
1#pragma once
2#include "../osi/foray_env.hpp"
4#include <set>
5#include <unordered_map>
6#include <unordered_set>
7#include <vector>
8
9namespace foray::core {
10
13 {
15 std::vector<osi::Utf8Path> IncludeDirs = {};
17 std::vector<std::string> Definitions = {};
19 std::string EntryPoint = "";
21 std::vector<std::string> AdditionalOptions = {};
22 };
23
24
32 {
33 public:
35 inline explicit ShaderManager(core::Context* context = nullptr) : mContext(context) {}
36
43 uint64_t CompileShader(osi::Utf8Path sourceFilePath, ShaderModule& shaderModule, const ShaderCompilerConfig& config = {}, core::Context* context = nullptr);
50 uint64_t CompileShader(osi::Utf8Path sourceFilePath, ShaderModule* shaderModule, const ShaderCompilerConfig& config = {}, core::Context* context = nullptr);
51
56 virtual bool CheckAndUpdateShaders(std::unordered_set<uint64_t>& out_recompiled);
57
58 ShaderManager() = default;
59
64 virtual bool CallGlslCompiler(std::string_view args);
65
66 protected:
68 using WriteTimeLookup = std::unordered_map<osi::Utf8Path, std::filesystem::file_time_type>;
69
72
74 virtual uint64_t MakeHash(std::string_view absoluteUniqueSourceFilePath, const ShaderCompilerConfig& config);
75
80 static std::filesystem::file_time_type GetWriteTime(const osi::Utf8Path& path, WriteTimeLookup& writeTimeLookup);
81
82 struct IncludeFile;
83
85 {
89 };
90
93 {
101 std::filesystem::file_time_type LastCompile = std::filesystem::file_time_type::min();
103 std::filesystem::file_time_type LastFailedCompile = std::filesystem::file_time_type::min();
105 std::unordered_set<IncludeFile*> Includes = {};
109 uint64_t Hash = 0;
114 ShaderCompilation(ShaderManager* manager, const osi::Utf8Path& source, const ShaderCompilerConfig& config, uint64_t hash);
119 bool Compile();
123 };
124
127 {
131 std::unordered_set<ShaderCompilation*> Includers;
133 std::unordered_set<IncludeFile*> Includees;
134
135 inline IncludeFile(const osi::Utf8Path& path) : Path(path) {}
136
138 void GetIncludesRecursively(std::unordered_set<IncludeFile*>& out);
139 };
140
142 std::unordered_map<uint64_t, std::unique_ptr<ShaderCompilation>> mTrackedCompilations;
144 std::unordered_map<osi::Utf8Path, std::unique_ptr<IncludeFile>> mTrackedIncludeFiles;
145
152 IncludeFile* RecursivelyProcessInclude(const osi::Utf8Path& path, const std::vector<osi::Utf8Path>& extraIncludeDirs);
153 };
154
155} // namespace foray::core
Shader manager maintains a structure of shader compilations.
Definition foray_shadermanager.hpp:32
virtual uint64_t MakeHash(std::string_view absoluteUniqueSourceFilePath, const ShaderCompilerConfig &config)
Calculates a unique hash based on source file path and config.
ShaderManager(core::Context *context=nullptr)
Definition foray_shadermanager.hpp:35
std::unordered_map< uint64_t, std::unique_ptr< ShaderCompilation > > mTrackedCompilations
Map of shader compilation keys to tracked compilations.
Definition foray_shadermanager.hpp:142
void DiscoverIncludes(ShaderCompilation *compilation)
Discover and register all includes for a shader compilation.
std::unordered_map< osi::Utf8Path, std::filesystem::file_time_type > WriteTimeLookup
maps files to last write times
Definition foray_shadermanager.hpp:68
ECompileCheckResult
Definition foray_shadermanager.hpp:85
virtual bool CallGlslCompiler(std::string_view args)
Calls glslc in path on linux, glslc.exe (derived from VULKAN_SDK environment variable) on windows.
static std::filesystem::file_time_type GetWriteTime(const osi::Utf8Path &path, WriteTimeLookup &writeTimeLookup)
Get last write time.
virtual bool CheckAndUpdateShaders(std::unordered_set< uint64_t > &out_recompiled)
Checks and updates shader compilations for source code changes.
uint64_t CompileShader(osi::Utf8Path sourceFilePath, ShaderModule *shaderModule, const ShaderCompilerConfig &config={}, core::Context *context=nullptr)
Accesses sourceFilePath, scans for dependencies, compiles if necessary, loads into shaderModule.
IncludeFile * RecursivelyProcessInclude(const osi::Utf8Path &path, const std::vector< osi::Utf8Path > &extraIncludeDirs)
Create or return an include file. Recursively processes #include directives to resolve nested include...
core::Context * mContext
Context used for initialization of shader modules.
Definition foray_shadermanager.hpp:71
std::unordered_map< osi::Utf8Path, std::unique_ptr< IncludeFile > > mTrackedIncludeFiles
Map of include file paths to include file structs.
Definition foray_shadermanager.hpp:144
uint64_t CompileShader(osi::Utf8Path sourceFilePath, ShaderModule &shaderModule, const ShaderCompilerConfig &config={}, core::Context *context=nullptr)
Accesses sourceFilePath, scans for dependencies, compiles if necessary, loads into shaderModule.
Wraps shader code driver handle (VkShaderModule). See ShaderManager for compiling shaders dynamically...
Definition foray_shadermodule.hpp:11
Utf8 encoded path wrapper.
Definition foray_env.hpp:28
Definition foray_commandbuffer.hpp:6
Non owning context object.
Definition foray_context.hpp:16
Shader compile Options struct (moved out of class to fix clang & gcc compiler bug https://gcc....
Definition foray_shadermanager.hpp:13
std::vector< std::string > AdditionalOptions
Options in this vector are appended to the option string as-is.
Definition foray_shadermanager.hpp:21
std::string EntryPoint
Shader Stage entry point passed to gslc via "-fentry-point=NAME". Leave empty to let glslc use defaul...
Definition foray_shadermanager.hpp:19
std::vector< std::string > Definitions
Preprocessor definitions passed to glslc via "-DDEFINITION" option.
Definition foray_shadermanager.hpp:17
std::vector< osi::Utf8Path > IncludeDirs
Additional include dirs passed to glslc via "-I "INCLUDEDIR"" option.
Definition foray_shadermanager.hpp:15
Represents a file included (nested) in a shader compilation, but not compiled by itself.
Definition foray_shadermanager.hpp:127
IncludeFile(const osi::Utf8Path &path)
Definition foray_shadermanager.hpp:135
std::unordered_set< IncludeFile * > Includees
Files included by this include file.
Definition foray_shadermanager.hpp:133
void GetIncludesRecursively(std::unordered_set< IncludeFile * > &out)
Recursively writes Includees to out.
osi::Utf8Path Path
File path.
Definition foray_shadermanager.hpp:129
std::unordered_set< ShaderCompilation * > Includers
Compilations depending on this file.
Definition foray_shadermanager.hpp:131
Represents a unique shader compilation (input source path + config => Spirv file)
Definition foray_shadermanager.hpp:93
bool Compile()
Invokes the shader compiler.
std::filesystem::file_time_type LastCompile
Last successful compile time.
Definition foray_shadermanager.hpp:101
osi::Utf8Path SourcePath
Source path.
Definition foray_shadermanager.hpp:97
ShaderCompilerConfig Config
Configuration of the shader compiler.
Definition foray_shadermanager.hpp:107
std::filesystem::file_time_type LastFailedCompile
Time of last failed compile.
Definition foray_shadermanager.hpp:103
std::unordered_set< IncludeFile * > Includes
List of includes.
Definition foray_shadermanager.hpp:105
ShaderCompilation(ShaderManager *manager, const osi::Utf8Path &source, const ShaderCompilerConfig &config, uint64_t hash)
Sets SpvPath and updates LastCompile.
ECompileCheckResult NeedsCompile(WriteTimeLookup &compileTimeLookup)
Checks for need to recompile.
uint64_t Hash
Hash identifying the compilation.
Definition foray_shadermanager.hpp:109
osi::Utf8Path SpvPath
Output path.
Definition foray_shadermanager.hpp:99
ShaderManager * Manager
Owning manager object.
Definition foray_shadermanager.hpp:95
void FindIncludes()
Finds all includes and configures them in the watch of the shader manager.