From e10fc80efcce8257270c1dd989feb55446e72048 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Sat, 17 Jul 2021 18:03:14 +0200
Subject: [PATCH] [#100] Added sample compute shader to use FSR as string

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 modules/upscaling/config/FidelityFX_FSR.cmake |  1 +
 .../src/vkcv/upscaling/FSRUpscaling.cpp       | 42 +++++++++++++++++--
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/modules/upscaling/config/FidelityFX_FSR.cmake b/modules/upscaling/config/FidelityFX_FSR.cmake
index 37a6321c..861f7651 100644
--- a/modules/upscaling/config/FidelityFX_FSR.cmake
+++ b/modules/upscaling/config/FidelityFX_FSR.cmake
@@ -2,6 +2,7 @@
 if (EXISTS "${vkcv_upscaling_lib_path}/FidelityFX-FSR")
 	include_shader(${vkcv_upscaling_lib_path}/FidelityFX-FSR/ffx-fsr/ffx_a.h ${vkcv_upscaling_include} ${vkcv_upscaling_source})
 	include_shader(${vkcv_upscaling_lib_path}/FidelityFX-FSR/ffx-fsr/ffx_fsr1.h ${vkcv_upscaling_include} ${vkcv_upscaling_source})
+	include_shader(${vkcv_upscaling_lib_path}/FidelityFX-FSR/sample/src/VK/FSR_Pass.glsl ${vkcv_upscaling_include} ${vkcv_upscaling_source})
 	
 	list(APPEND vkcv_upscaling_includes ${vkcv_upscaling_lib}/FidelityFX-FSR/ffx-fsr)
 else()
diff --git a/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp b/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
index 9434f017..934d550c 100644
--- a/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
+++ b/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
@@ -10,19 +10,53 @@
 
 #include "ffx_a.h.hxx"
 #include "ffx_fsr1.h.hxx"
+#include "FSR_Pass.glsl.hxx"
 
+#include <vkcv/File.hpp>
+#include <vkcv/Logger.hpp>
 #include <vkcv/shader/GLSLCompiler.hpp>
 
 namespace vkcv::upscaling {
 	
-	static void setupTemporaryShaderDirectory() {
+	static bool writeShaderCode(const std::filesystem::path &shaderPath, const std::string& code) {
+		std::ofstream file (shaderPath.string(), std::ios::out);
+		
+		if (!file.is_open()) {
+			vkcv_log(LogLevel::ERROR, "The file could not be opened (%s)", shaderPath.string().c_str());
+			return false;
+		}
+		
+		file.seekp(0);
+		file.write(code.c_str(), static_cast<std::streamsize>(code.length()));
+		file.close();
+		
+		return true;
+	}
 	
+	static bool compileFSRShaders(const shader::ShaderCompiledFunction& compiled) {
+		std::filesystem::path directory = generateTemporaryFilePath();
+		
+		if (!std::filesystem::create_directory(directory)) {
+			vkcv_log(LogLevel::ERROR, "The directory could not be created (%s)", directory.string().c_str());
+			return false;
+		}
+		
+		if (!writeShaderCode(directory / "ffx_a.h", FFX_A_H_SHADER)) {
+			return false;
+		}
+		
+		if (!writeShaderCode(directory / "ffx_fsr1.h", FFX_FSR1_H_SHADER)) {
+			return false;
+		}
+		
+		vkcv::shader::GLSLCompiler compiler;
+		return compiler.compileSource(vkcv::ShaderStage::COMPUTE,
+									  FSR_PASS_GLSL_SHADER.c_str(),
+									  compiled, directory);
 	}
 	
 	FSRUpscaling::FSRUpscaling() {
-		vkcv::shader::GLSLCompiler compiler;
-		compiler.compile(vkcv::ShaderStage::VERTEX, std::filesystem::path("resources/shaders/shader.vert"),
-						 [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) {
+		compileFSRShaders([&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) {
 			m_program.addShader(shaderStage, path);
 		});
 	}
-- 
GitLab