Skip to content
Snippets Groups Projects
Verified Commit 27780b2d authored by Tobias Frisch's avatar Tobias Frisch
Browse files

[#100] Added function to include shaders as const string, refactored temporary file usage

parent a7694ca0
No related branches found
No related tags found
1 merge request!82Resolve "Upscaling Module"
Pipeline #26412 failed
...@@ -18,3 +18,8 @@ cmake-build-release/ ...@@ -18,3 +18,8 @@ cmake-build-release/
# GUI configuration files # GUI configuration files
imgui.ini imgui.ini
# Generated source and header files for shaders
*.hxx
*.cxx
...@@ -34,6 +34,9 @@ endif () ...@@ -34,6 +34,9 @@ endif ()
# fix dependencies for different Linux distros (looking at you Ubuntu) # fix dependencies for different Linux distros (looking at you Ubuntu)
include(${vkcv_config_ext}/CheckLibraries.cmake) include(${vkcv_config_ext}/CheckLibraries.cmake)
# add custom function to include a file like a shader as string
include(${vkcv_config_ext}/IncludeShader.cmake)
# cleanup of compiler definitions aka preprocessor variables # cleanup of compiler definitions aka preprocessor variables
if (vkcv_definitions) if (vkcv_definitions)
list(REMOVE_DUPLICATES vkcv_definitions) list(REMOVE_DUPLICATES vkcv_definitions)
......
...@@ -6,6 +6,9 @@ set(vkcv_sources ...@@ -6,6 +6,9 @@ set(vkcv_sources
${vkcv_include}/vkcv/Core.hpp ${vkcv_include}/vkcv/Core.hpp
${vkcv_source}/vkcv/Core.cpp ${vkcv_source}/vkcv/Core.cpp
${vkcv_include}/vkcv/File.hpp
${vkcv_source}/vkcv/File.cpp
${vkcv_include}/vkcv/PassConfig.hpp ${vkcv_include}/vkcv/PassConfig.hpp
${vkcv_source}/vkcv/PassConfig.cpp ${vkcv_source}/vkcv/PassConfig.cpp
......
function(include_shader shader include_dir source_dir)
get_filename_component(filename ${shader} NAME)
file(SIZE ${shader} filesize)
string(TOUPPER ${filename} varname)
string(REPLACE "." "_" varname ${varname})
set(shader_header "")
string(APPEND shader_header "#pragma once\n")
string(APPEND shader_header "extern unsigned char ${varname} [${filesize}]\;\n")
string(APPEND shader_header "extern unsigned int ${varname}_LEN\;\n")
string(APPEND shader_header "const std::string ${varname}_SHADER (reinterpret_cast<const char*>(${varname}), ${varname}_LEN)\;")
set(shader_source "")
string(APPEND shader_source "unsigned char ${varname}[] = {")
math(EXPR max_fileoffset "${filesize} - 1" OUTPUT_FORMAT DECIMAL)
foreach(fileoffset RANGE ${max_fileoffset})
file(READ ${shader} shader_source_byte OFFSET ${fileoffset} LIMIT 1 HEX)
math(EXPR offset_modulo "${fileoffset} % 12" OUTPUT_FORMAT DECIMAL)
if (${offset_modulo} EQUAL 0)
string(APPEND shader_source "\n ")
endif()
if (${fileoffset} LESS ${max_fileoffset})
string(APPEND shader_source "0x${shader_source_byte}, ")
else()
string(APPEND shader_source "0x${shader_source_byte}\n")
endif()
endforeach()
string(APPEND shader_source "}\n")
string(APPEND shader_source "unsigned int ${varname}_LEN = ${filesize}\;")
file(WRITE ${include_dir}/${filename}.hxx ${shader_header})
file(WRITE ${source_dir}/${filename}.cxx ${shader_source})
endfunction()
#pragma once
#include <filesystem>
namespace vkcv {
std::filesystem::path generateTemporaryFilePath();
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <glslang/SPIRV/GlslangToSpv.h> #include <glslang/SPIRV/GlslangToSpv.h>
#include <glslang/StandAlone/DirStackFileIncluder.h> #include <glslang/StandAlone/DirStackFileIncluder.h>
#include <vkcv/File.hpp>
#include <vkcv/Logger.hpp> #include <vkcv/Logger.hpp>
namespace vkcv::shader { namespace vkcv::shader {
...@@ -263,7 +264,7 @@ namespace vkcv::shader { ...@@ -263,7 +264,7 @@ namespace vkcv::shader {
std::vector<uint32_t> spirv; std::vector<uint32_t> spirv;
glslang::GlslangToSpv(*intermediate, spirv); glslang::GlslangToSpv(*intermediate, spirv);
const std::filesystem::path tmp_path (std::tmpnam(nullptr)); const std::filesystem::path tmp_path = generateTemporaryFilePath();
if (!writeSpirvCode(tmp_path, spirv)) { if (!writeSpirvCode(tmp_path, spirv)) {
vkcv_log(LogLevel::ERROR, "Spir-V could not be written to disk"); vkcv_log(LogLevel::ERROR, "Spir-V could not be written to disk");
......
...@@ -23,6 +23,9 @@ set(vkcv_upscaling_lib_path ${PROJECT_SOURCE_DIR}/${vkcv_upscaling_lib}) ...@@ -23,6 +23,9 @@ set(vkcv_upscaling_lib_path ${PROJECT_SOURCE_DIR}/${vkcv_upscaling_lib})
# Check and load GLSLANG # Check and load GLSLANG
include(config/FidelityFX_FSR.cmake) include(config/FidelityFX_FSR.cmake)
# link the required libraries to the module
target_link_libraries(vkcv_upscaling ${vkcv_upscaling_libraries} vkcv vkcv_shader_compiler)
# including headers of dependencies and the VkCV framework # including headers of dependencies and the VkCV framework
target_include_directories(vkcv_upscaling SYSTEM BEFORE PRIVATE ${vkcv_upscaling_includes} ${vkcv_include} ${vkcv_shader_compiler_include}) target_include_directories(vkcv_upscaling SYSTEM BEFORE PRIVATE ${vkcv_upscaling_includes} ${vkcv_include} ${vkcv_shader_compiler_include})
......
if (EXISTS "${vkcv_upscaling_lib_path}/FidelityFX-FSR") 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})
list(APPEND vkcv_upscaling_includes ${vkcv_upscaling_lib}/FidelityFX-FSR/ffx-fsr) list(APPEND vkcv_upscaling_includes ${vkcv_upscaling_lib}/FidelityFX-FSR/ffx-fsr)
else() else()
message(WARNING "FidelityFX-FSR is required..! Update the submodules!") message(WARNING "FidelityFX-FSR is required..! Update the submodules!")
......
#pragma once #pragma once
namespace vkcv::upscaling { #include <vkcv/ShaderProgram.hpp>
namespace vkcv::upscaling {
class FSRUpscaling {
private:
ShaderProgram m_program;
public:
FSRUpscaling();
};
} }
...@@ -8,6 +8,23 @@ ...@@ -8,6 +8,23 @@
#include <ffx_a.h> #include <ffx_a.h>
#include <ffx_fsr1.h> #include <ffx_fsr1.h>
#include "ffx_a.h.hxx"
#include "ffx_fsr1.h.hxx"
#include <vkcv/shader/GLSLCompiler.hpp>
namespace vkcv::upscaling { namespace vkcv::upscaling {
static void setupTemporaryShaderDirectory() {
}
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) {
m_program.addShader(shaderStage, path);
});
}
} }
#include "vkcv/File.hpp"
#include <stdlib.h>
#include <unistd.h>
namespace vkcv {
std::filesystem::path generateTemporaryFilePath() {
std::error_code code;
auto tmp = std::filesystem::temp_directory_path(code);
if (tmp.empty()) {
tmp = std::filesystem::current_path();
}
char name [16] = "vkcv_tmp_XXXXXX";
int fd = mkstemp(name);
if (fd == -1) {
return "";
}
close(fd);
return tmp / name;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment