From 72f7194625eb0cc1008993ecdc88a93979263ea7 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Sat, 17 Jul 2021 23:27:05 +0200
Subject: [PATCH] [#100] Implemented FSR to voxelization demo... at best
 quality obviously

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 config/ext/IncludeShader.cmake                |  2 +-
 include/vkcv/Core.hpp                         |  8 ++++++--
 .../src/vkcv/shader/GLSLCompiler.cpp          |  6 +++---
 modules/upscaling/CMakeLists.txt              |  8 ++++----
 modules/upscaling/config/FidelityFX_FSR.cmake |  8 ++++++++
 .../src/vkcv/upscaling/FSRUpscaling.cpp       |  9 +++++----
 projects/voxelization/CMakeLists.txt          |  4 ++--
 projects/voxelization/src/main.cpp            | 19 +++++++++++++++----
 src/vkcv/Core.cpp                             | 12 ++++++++----
 9 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/config/ext/IncludeShader.cmake b/config/ext/IncludeShader.cmake
index 2b2e7f96..7eea0df4 100644
--- a/config/ext/IncludeShader.cmake
+++ b/config/ext/IncludeShader.cmake
@@ -33,7 +33,7 @@ function(include_shader shader include_dir source_dir)
 		endif()
 	endforeach()
 	
-	string(APPEND shader_source "}\n")
+	string(APPEND shader_source "}\;\n")
 	string(APPEND shader_source "unsigned int ${varname}_LEN = ${filesize}\;")
 	
 	file(WRITE ${include_dir}/${filename}.hxx ${shader_header})
diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp
index 21153c3f..8141bb4b 100644
--- a/include/vkcv/Core.hpp
+++ b/include/vkcv/Core.hpp
@@ -223,9 +223,13 @@ namespace vkcv
 			Multisampling   multisampling = Multisampling::None);
 
         [[nodiscard]]
-        uint32_t getImageWidth(ImageHandle imageHandle);
+        uint32_t getImageWidth(const ImageHandle& image);
+        
         [[nodiscard]]
-        uint32_t getImageHeight(ImageHandle imageHandle);
+        uint32_t getImageHeight(const ImageHandle& image);
+	
+		[[nodiscard]]
+		vk::Format getImageFormat(const ImageHandle& image);
 
         /** TODO:
          *   @param setDescriptions
diff --git a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp
index ed2bf740..b8cf7210 100644
--- a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp
+++ b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp
@@ -243,18 +243,18 @@ namespace vkcv::shader {
 			return false;
 		}
 		
-		size_t pos = preprocessedGLSL.find("#version");
+		size_t pos = preprocessedGLSL.find("#version") + 8;
 		if (pos >= preprocessedGLSL.length()) {
 			pos = 0;
 		}
 		
-		const size_t epos = preprocessedGLSL.find_last_of("#extension", pos);
+		const size_t epos = preprocessedGLSL.find_last_of("#extension", pos) + 10;
 		if (epos < preprocessedGLSL.length()) {
 			pos = epos;
 		}
 		
 		pos = preprocessedGLSL.find('\n', pos) + 1;
-		preprocessedGLSL = preprocessedGLSL.insert(pos, defines.str());
+		preprocessedGLSL.insert(pos, defines.str(), defines.width());
 		
 		const char* preprocessedCString = preprocessedGLSL.c_str();
 		shader.setStrings(&preprocessedCString, 1);
diff --git a/modules/upscaling/CMakeLists.txt b/modules/upscaling/CMakeLists.txt
index f4d34bad..1625c3cc 100644
--- a/modules/upscaling/CMakeLists.txt
+++ b/modules/upscaling/CMakeLists.txt
@@ -13,16 +13,16 @@ set(vkcv_upscaling_sources
 		${vkcv_upscaling_source}/vkcv/upscaling/FSRUpscaling.cpp
 )
 
-# adding source files to the project
-add_library(vkcv_upscaling STATIC ${vkcv_upscaling_sources})
-
 # Setup some path variables to load libraries
 set(vkcv_upscaling_lib lib)
 set(vkcv_upscaling_lib_path ${PROJECT_SOURCE_DIR}/${vkcv_upscaling_lib})
 
-# Check and load GLSLANG
+# Check and load FidelityFX_FSR
 include(config/FidelityFX_FSR.cmake)
 
+# adding source files to the project
+add_library(vkcv_upscaling STATIC ${vkcv_upscaling_sources})
+
 # link the required libraries to the module
 target_link_libraries(vkcv_upscaling ${vkcv_upscaling_libraries} vkcv vkcv_shader_compiler)
 
diff --git a/modules/upscaling/config/FidelityFX_FSR.cmake b/modules/upscaling/config/FidelityFX_FSR.cmake
index 861f7651..cc52b418 100644
--- a/modules/upscaling/config/FidelityFX_FSR.cmake
+++ b/modules/upscaling/config/FidelityFX_FSR.cmake
@@ -5,6 +5,14 @@ if (EXISTS "${vkcv_upscaling_lib_path}/FidelityFX-FSR")
 	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)
+	
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_source}/ffx_a.h.cxx)
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_source}/ffx_fsr1.h.cxx)
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_source}/FSR_Pass.glsl.cxx)
+	
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_include}/ffx_a.h.hxx)
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_include}/ffx_fsr1.h.hxx)
+	list(APPEND vkcv_upscaling_sources ${vkcv_upscaling_include}/FSR_Pass.glsl.hxx)
 else()
 	message(WARNING "FidelityFX-FSR is required..! Update the submodules!")
 endif ()
diff --git a/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp b/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
index 6eba6868..bf386538 100644
--- a/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
+++ b/modules/upscaling/src/vkcv/upscaling/FSRUpscaling.cpp
@@ -124,14 +124,17 @@ namespace vkcv::upscaling {
 		const uint32_t outputWidth = m_core.getImageWidth(output);
 		const uint32_t outputHeight = m_core.getImageWidth(output);
 		
-		if ((outputWidth != m_core.getImageWidth(m_intermediateImage)) ||
+		if ((!m_intermediateImage) ||
+			(outputWidth != m_core.getImageWidth(m_intermediateImage)) ||
 			(outputHeight != m_core.getImageHeight(m_intermediateImage))) {
 			m_intermediateImage = m_core.createImage(
-					vk::Format::eR8G8B8A8Srgb,
+					m_core.getImageFormat(output),
 					outputWidth, outputHeight,1,
 					false,
 					true
 			).getHandle();
+			
+			m_core.prepareImageForStorage(cmdStream, m_intermediateImage);
 		}
 		
 		{
@@ -220,8 +223,6 @@ namespace vkcv::upscaling {
 					PushConstants(0)
 			);
 		}
-		
-		m_core.recordImageMemoryBarrier(cmdStream, output);
 	}
 	
 	bool FSRUpscaling::isHdrEnabled() const {
diff --git a/projects/voxelization/CMakeLists.txt b/projects/voxelization/CMakeLists.txt
index c962409f..d2f533b0 100644
--- a/projects/voxelization/CMakeLists.txt
+++ b/projects/voxelization/CMakeLists.txt
@@ -30,7 +30,7 @@ if(MSVC)
 endif()
 
 # including headers of dependencies and the VkCV framework
-target_include_directories(voxelization SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_asset_loader_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include} ${vkcv_gui_include})
+target_include_directories(voxelization SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_asset_loader_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include} ${vkcv_gui_include} ${vkcv_upscaling_include})
 
 # linking with libraries from all dependencies and the VkCV framework
-target_link_libraries(voxelization vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_camera vkcv_shader_compiler vkcv_gui)
+target_link_libraries(voxelization vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_camera vkcv_shader_compiler vkcv_gui vkcv_upscaling)
diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp
index ca995149..2d629311 100644
--- a/projects/voxelization/src/main.cpp
+++ b/projects/voxelization/src/main.cpp
@@ -11,6 +11,7 @@
 #include "vkcv/gui/GUI.hpp"
 #include "ShadowMapping.hpp"
 #include "BloomAndFlares.hpp"
+#include <vkcv/upscaling/FSRUpscaling.hpp>
 
 int main(int argc, const char** argv) {
 	const char* applicationName = "Voxelization";
@@ -393,6 +394,8 @@ int main(int argc, const char** argv) {
 	else {
 		resolvedColorBuffer = colorBuffer;
 	}
+	
+	vkcv::ImageHandle swapBuffer = core.createImage(colorBufferFormat, windowWidth, windowHeight, 1, false, true).getHandle();
 
 	const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle();
 
@@ -523,6 +526,8 @@ int main(int argc, const char** argv) {
 		vkcv::SamplerDescriptorWrite(5, voxelSampler) };
 	core.writeDescriptorSet(forwardShadingDescriptorSet, forwardDescriptorWrites);
 
+	vkcv::upscaling::FSRUpscaling upscaling (core);
+	
 	vkcv::gui::GUI gui(core, window);
 
 	glm::vec2   lightAnglesDegree               = glm::vec2(90.f, 0.f);
@@ -561,6 +566,8 @@ int main(int argc, const char** argv) {
 			else {
 				resolvedColorBuffer = colorBuffer;
 			}
+			
+			swapBuffer = core.createImage(colorBufferFormat, swapchainWidth, swapchainHeight, 1, false, true).getHandle();
 
 			windowWidth = swapchainWidth;
 			windowHeight = swapchainHeight;
@@ -575,7 +582,7 @@ int main(int argc, const char** argv) {
 		vkcv::DescriptorWrites tonemappingDescriptorWrites;
 		tonemappingDescriptorWrites.sampledImageWrites  = { vkcv::SampledImageDescriptorWrite(0, resolvedColorBuffer) };
 		tonemappingDescriptorWrites.samplerWrites       = { vkcv::SamplerDescriptorWrite(1, colorSampler) };
-		tonemappingDescriptorWrites.storageImageWrites  = { vkcv::StorageImageDescriptorWrite(2, swapchainInput) };
+		tonemappingDescriptorWrites.storageImageWrites  = { vkcv::StorageImageDescriptorWrite(2, swapBuffer) };
 
 		core.writeDescriptorSet(tonemappingDescriptorSet, tonemappingDescriptorWrites);
 
@@ -709,11 +716,11 @@ int main(int argc, const char** argv) {
 		bloomFlares.execWholePipeline(cmdStream, resolvedColorBuffer, windowWidth, windowHeight, 
 			glm::normalize(cameraManager.getActiveCamera().getFront()));
 
-		core.prepareImageForStorage(cmdStream, swapchainInput);
+		core.prepareImageForStorage(cmdStream, swapBuffer);
 		core.prepareImageForSampling(cmdStream, resolvedColorBuffer);
 
 		auto timeSinceStart = std::chrono::duration_cast<std::chrono::microseconds>(end - appStartTime);
-		float timeF         = static_cast<float>(timeSinceStart.count()) * 0.01;
+		float timeF         = static_cast<float>(timeSinceStart.count()) * 0.01f;
 
 		vkcv::PushConstants timePushConstants (sizeof(timeF));
 		timePushConstants.appendDrawcall(timeF);
@@ -723,7 +730,11 @@ int main(int argc, const char** argv) {
 			tonemappingPipeline, 
 			fulsscreenDispatchCount,
 			{ vkcv::DescriptorSetUsage(0, core.getDescriptorSet(tonemappingDescriptorSet).vulkanHandle) },
-			timePushConstants);
+			timePushConstants
+		);
+		
+		core.prepareImageForSampling(cmdStream, swapBuffer);
+		upscaling.recordUpscaling(cmdStream, swapBuffer, swapchainInput);
 
 		// present and end
 		core.prepareSwapchainImageForPresent(cmdStream);
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index a66c1e62..556f6474 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -516,14 +516,18 @@ namespace vkcv
 			multisampling);
 	}
 
-	uint32_t Core::getImageWidth(ImageHandle imageHandle)
+	uint32_t Core::getImageWidth(const ImageHandle& image)
 	{
-		return m_ImageManager->getImageWidth(imageHandle);
+		return m_ImageManager->getImageWidth(image);
 	}
 
-	uint32_t Core::getImageHeight(ImageHandle imageHandle)
+	uint32_t Core::getImageHeight(const ImageHandle& image)
 	{
-		return m_ImageManager->getImageHeight(imageHandle);
+		return m_ImageManager->getImageHeight(image);
+	}
+	
+	vk::Format Core::getImageFormat(const ImageHandle& image) {
+		return m_ImageManager->getImageFormat(image);
 	}
 
     DescriptorSetHandle Core::createDescriptorSet(const std::vector<DescriptorBinding>& bindings)
-- 
GitLab