From 66cc857c1fe2f3e3341afde23e25ad3719397d8e Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Sat, 13 Jan 2024 00:27:41 +0100
Subject: [PATCH] Fix shady compiler output file

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 modules/shader_compiler/lib/shady             |  2 +-
 .../src/vkcv/shader/ShadyCompiler.cpp         | 44 +++++++++++++------
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/modules/shader_compiler/lib/shady b/modules/shader_compiler/lib/shady
index b270da94..2e72e345 160000
--- a/modules/shader_compiler/lib/shady
+++ b/modules/shader_compiler/lib/shady
@@ -1 +1 @@
-Subproject commit b270da9462d9b819fdf6b851c599a353839593bf
+Subproject commit 2e72e345035166d7fd2a0b94610cfc8fd603155b
diff --git a/modules/shader_compiler/src/vkcv/shader/ShadyCompiler.cpp b/modules/shader_compiler/src/vkcv/shader/ShadyCompiler.cpp
index da6680e5..54a10bf4 100644
--- a/modules/shader_compiler/src/vkcv/shader/ShadyCompiler.cpp
+++ b/modules/shader_compiler/src/vkcv/shader/ShadyCompiler.cpp
@@ -25,12 +25,11 @@ namespace vkcv::shader {
 		return *this;
 	}
 
-	static void shadyCompileModule(Module* module,
+	static bool shadyCompileModule(Module* module,
 								   ShaderStage shaderStage,
 								   const std::filesystem::path &shaderPath,
 								   const ShaderCompiledFunction &compiled,
-								   const std::filesystem::path &includePath,
-								   bool update) {
+								   const std::filesystem::path &includePath) {
 		ShadyErrorCodes codes = driver_load_source_file_from_filename(
 			shaderPath.c_str(), module
 		);
@@ -49,14 +48,19 @@ namespace vkcv::shader {
 			case ClangInvocationFailed:
 			default:
 				vkcv_log(LogLevel::ERROR, "Unknown error while loading shader");
-				return;
+				return false;
 		}
 
+		const std::filesystem::path tmp_path = generateTemporaryFilePath();
+
 		DriverConfig config = default_driver_config();
 
+		config.output_filename = tmp_path.c_str();
+
 		// TODO
 
 		codes = driver_compile(&config, module);
+		destroy_driver_config(&config);
 
 		switch (codes) {
 			case NoError:
@@ -72,28 +76,33 @@ namespace vkcv::shader {
 			case ClangInvocationFailed:
 			default:
 				vkcv_log(LogLevel::ERROR, "Unknown error while compiling shader");
-				return;
+				return false;
 		}
 
 		if (compiled) {
-			compiled(shaderStage, shaderPath);
+			compiled(shaderStage, tmp_path);
 		}
+
+		std::filesystem::remove(tmp_path);
+		return true;
 	}
 
-	static void shadyCompileArena(IrArena* arena,
+	static bool shadyCompileArena(IrArena* arena,
 								  ShaderStage shaderStage,
 								  const std::filesystem::path &shaderPath,
 								  const ShaderCompiledFunction &compiled,
-								  const std::filesystem::path &includePath,
-								  bool update) {
-		Module* module = new_module(arena, "NAME");
+								  const std::filesystem::path &includePath) {
+		Module* module = new_module(arena, "main");
 
 		if (nullptr == module) {
 			vkcv_log(LogLevel::ERROR, "Module could not be created");
-			return;
+			return false;
 		}
 
-		shadyCompileModule(module, shaderStage, shaderPath, compiled, includePath, update);
+		bool result = shadyCompileModule(module, shaderStage, shaderPath, compiled, includePath);
+
+		destroy_module(module);
+		return result;
 	}
 	
 	void ShadyCompiler::compile(ShaderStage shaderStage,
@@ -112,8 +121,17 @@ namespace vkcv::shader {
 			return;
 		}
 
-		shadyCompileArena(arena, shaderStage, shaderPath, compiled, includePath, update);
+		bool result = shadyCompileArena(arena, shaderStage, shaderPath, compiled, includePath);
+
 		destroy_ir_arena(arena);
+
+		if (!result) {
+			vkcv_log(LogLevel::ERROR, "Shader compilation failed: (%s)", shaderPath.string().c_str());
+		}
+
+		if (update) {
+			// TODO: Shader hot compilation during runtime
+		}
 	}
 	
 }
-- 
GitLab