26#ifdef SLEAK_HOT_RELOAD
29namespace fs = std::filesystem;
31constexpr float DEBOUNCE_SECONDS = 0.15f;
32constexpr float MS_PER_SECOND = 1000.0f;
33constexpr size_t PROCESS_LINE_SIZE = 512;
34constexpr const char* RUNTIME_SHADER_DIR =
"assets/shaders";
35constexpr const char* SPIRV_EXTENSION =
".spv";
36constexpr const char* TEMP_EXTENSION =
".tmp";
37constexpr std::string_view GL_SUFFIX =
"_gl";
38constexpr std::string_view DX12_SUFFIX =
"_dx12";
42 std::vector<fs::path> directories;
43 std::set<fs::path> pending;
54fs::path TempPathFor(
const fs::path& target) {
56 const int pid = _getpid();
58 const int pid =
static_cast<int>(getpid());
60 fs::path tmp = target;
61 tmp +=
"." + std::to_string(pid) + TEMP_EXTENSION;
66bool ReplaceWith(
const fs::path& tmp,
const fs::path& target) {
68 fs::rename(tmp, target, ec);
70 SLEAK_ERROR(
"Hot reload: can't replace {}: {}", target.filename().string(),
77bool IsShaderSource(
const fs::path& file) {
78 const fs::path ext = file.extension();
79 return ext ==
".vert" || ext ==
".frag" || ext ==
".hlsl";
83bool IsVulkanSource(
const fs::path& file) {
84 const fs::path ext = file.extension();
85 if (ext !=
".vert" && ext !=
".frag")
return false;
86 return !file.stem().string().ends_with(GL_SUFFIX);
90std::string StemOf(
const fs::path& file) {
91 std::string name = file.filename().string();
92 name = name.substr(0, name.find(
'.'));
93 for (std::string_view suffix : {GL_SUFFIX, DX12_SUFFIX}) {
94 if (name.ends_with(suffix)) {
95 name.resize(name.size() - suffix.size());
105 const std::string stem = file.stem().string();
106 if (file.extension() ==
".hlsl")
107 return stem.ends_with(DX12_SUFFIX) ? RendererType::DirectX12
108 : RendererType::DirectX11;
109 return stem.ends_with(GL_SUFFIX) ? RendererType::OpenGL
110 : RendererType::Vulkan;
114std::string VariantFor(
const std::string& stem,
118 case RendererType::Vulkan:
119 return stem +
".vert / " + stem +
".frag";
120 case RendererType::OpenGL:
121 return stem +
"_gl.vert / " + stem +
"_gl.frag";
122 case RendererType::DirectX11:
123 return stem +
".hlsl";
124 case RendererType::DirectX12:
125 return stem +
"_dx12.hlsl";
131bool IsOverridden(
const fs::path& file,
const State& state) {
132 const auto dir = std::find(state.directories.begin(),
133 state.directories.end(), file.parent_path());
134 if (dir == state.directories.end())
return false;
135 for (
auto later = dir + 1; later != state.directories.end(); ++later) {
137 if (fs::exists(*later / file.filename(), ec))
return true;
143bool CompileSpirv(
const fs::path& src,
const fs::path& spv) {
144#ifndef SLEAK_GLSLC_PATH
146 SLEAK_WARN(
"Hot reload: glslc was not found at configure time, {} skipped",
147 src.filename().string());
150 const fs::path tmp = TempPathFor(spv);
151 std::string cmd =
"\"" SLEAK_GLSLC_PATH
"\" \"" + src.string() +
152 "\" -o \"" + tmp.string() +
"\" 2>&1";
154 cmd =
"\"" + cmd +
"\"";
155 FILE* pipe = _popen(cmd.c_str(),
"r");
157 FILE* pipe = popen(cmd.c_str(),
"r");
161 src.filename().string());
165 char line[PROCESS_LINE_SIZE];
166 while (std::fgets(line,
sizeof(line), pipe)) output += line;
168 const int status = _pclose(pipe);
170 const int status = pclose(pipe);
176 "Hot reload: {} failed to compile, keeping the old "
178 src.filename().string(), output);
181 return ReplaceWith(tmp, spv);
186bool CopyToRuntime(
const fs::path& file) {
187 const fs::path target = fs::path(RUNTIME_SHADER_DIR) / file.filename();
188 const fs::path tmp = TempPathFor(target);
190 fs::copy_file(file, tmp, fs::copy_options::overwrite_existing, ec);
192 SLEAK_ERROR(
"Hot reload: can't copy {}: {}", file.filename().string(),
196 return ReplaceWith(tmp, target);
200std::string ProcessFile(
const fs::path& file,
const State& state) {
202 if (!fs::exists(file, ec))
return {};
203 if (IsOverridden(file, state)) {
205 "Hot reload: {} is overridden by a later shader folder, "
206 "edit that copy instead",
207 file.filename().string());
210 if (IsVulkanSource(file)) {
212 spv += SPIRV_EXTENSION;
213 if (!CompileSpirv(file, spv) || !CopyToRuntime(spv))
return {};
215 if (!CopyToRuntime(file))
return {};
223#ifdef SLEAK_HOT_RELOAD
225 const fs::path path = fs::canonical(dir, ec);
227 SLEAK_WARN(
"Hot reload: shader folder {} not found", dir);
230 State& state = GetState();
231 if (std::find(state.directories.begin(), state.directories.end(), path) !=
232 state.directories.end())
234 if (!state.watcher.AddDirectory(path.string()))
return;
235 state.directories.push_back(path);
236 SLEAK_INFO(
"Hot reload: watching {}", path.string());
243#ifdef SLEAK_HOT_RELOAD
244 State& state = GetState();
245 if (state.directories.empty())
return;
247 std::vector<std::string> changed;
248 state.watcher.Poll(changed);
249 for (
const std::string& path : changed) {
250 const fs::path file(path);
251 if (!IsShaderSource(file))
continue;
252 state.pending.insert(file);
253 state.quietTimer.Reset();
255 if (state.pending.empty() || state.quietTimer.Elapsed() < DEBOUNCE_SECONDS)
260 if (!renderer)
return;
264 std::vector<std::string> stems;
265 for (
const fs::path& file : state.pending) {
266 std::string stem = ProcessFile(file, state);
267 if (stem.empty())
continue;
268 if (BackendOf(file) != active) {
270 "Hot reload: {} is not used by the {} renderer, edit {} "
273 VariantFor(stem, active));
276 if (std::find(stems.begin(), stems.end(), stem) == stems.end())
277 stems.push_back(std::move(stem));
279 state.pending.clear();
280 if (stems.empty())
return;
284 SLEAK_WARN(
"Hot reload: the {} backend can't reload shaders yet",
290 for (
const std::string& stem : stems)
291 names += (names.empty() ?
"" :
", ") + stem;
292 SLEAK_INFO(
"Hot reload: reloaded {} in {:.0f} ms", names,
293 reloadTimer.
Elapsed() * MS_PER_SECOND);
const char * GetRendererTypeStr() const
Human-readable name of the active backend (e.g. "Vulkan").
void WaitGPUIdle()
Blocks until the GPU finishes all in-flight work. Call before tearing down scene resources.
static Application * GetInstance()
The one Application for this process, or null before construction.
RenderEngine::Renderer * GetRenderer()
Active renderer backend, or null before Run() initializes it.
static void AddSourceDirectory(const std::string &dir)
Watches a shader source folder. Later folders override earlier ones.
static void Poll()
Rebuilds changed shaders. Called once per frame by Application.
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
RendererType GetType() const
virtual bool ReloadShaders(const std::vector< std::string > &stems)
float Elapsed() const
Seconds since construction or the last Reset().
RendererType
Selects which graphics API a Renderer instance targets.
Root namespace for everything the engine exposes.