From 3552887886ccb57b754035e6767d7764c9d5d9b8 Mon Sep 17 00:00:00 2001 From: butters dousha <1216874964@qq.com> Date: Thu, 12 Jan 2023 22:38:27 +0800 Subject: [PATCH] cmd line added --- src/VK/offscreen/Vulkan_FSR2.hpp | 74 +++++++++----- src/VK/offscreen/helper.hpp | 33 ++++++- src/VK/offscreen/include/FindFiles.h | 143 +++++++++++++++++++++++++++ src/VK/offscreen/main.cpp | 101 +++++++++++++++++-- 4 files changed, 314 insertions(+), 37 deletions(-) create mode 100644 src/VK/offscreen/include/FindFiles.h diff --git a/src/VK/offscreen/Vulkan_FSR2.hpp b/src/VK/offscreen/Vulkan_FSR2.hpp index 6dd3d0f..8fe4861 100644 --- a/src/VK/offscreen/Vulkan_FSR2.hpp +++ b/src/VK/offscreen/Vulkan_FSR2.hpp @@ -111,9 +111,9 @@ inline VkPipelineStageFlags pipelineStageForLayout(VkImageLayout layout) { class Vulkan_FSR2 { public: - void run(std::vector& file_list,std::string input_file_c, std::string input_file_md, std::string out_path) { + void run(config& config_) { initVulkan(); - copy_work(file_list,input_file_c, input_file_md, out_path); + FSR2(config_); cleanup(); } @@ -152,10 +152,12 @@ private: //fsr2 stuff FfxFsr2ContextDescription initializationParameters = {}; FfxFsr2Context context; - double m_deltaTime; + double deltaTime; float scale_motion_x = 1.0f; float scale_motion_y = 1.0f; float scale_depth = 1.0f; + bool reset = false; + bool enableJitter = false; //----------- @@ -923,7 +925,7 @@ private: Fsr2_depth_ImageView = createImageView(Fsr2_depth, VK_FORMAT_D32_SFLOAT, VK_IMAGE_ASPECT_DEPTH_BIT, mipLevels_fsr); } - void DownloadFSR2Image(int index, std::string output_path, int& texWidth, int& texHeight, int& texChannels, VkImage& Fsr2_input_Image, VkDeviceMemory& Fsr2_input_ImageMemory) + void DownloadFSR2Image(std::string &index, std::string &output_path, int& texWidth, int& texHeight, int& texChannels, VkImage& Fsr2_input_Image, VkDeviceMemory& Fsr2_input_ImageMemory) { VkDeviceSize imageSize = texWidth * texHeight * sizeof(float) * texChannels; //mipLevels = static_cast(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1; @@ -948,7 +950,7 @@ private: vkFreeMemory(device, stagingBufferMemory, nullptr); //work with the vector - std::string file_name = std::to_string(index) + ".exr"; + std::string file_name = "result_" + index + ".exr"; std::string output_exr = output_path + file_name; write_exr(output_exr, data_float, texWidth, texHeight, texChannels, false); @@ -982,9 +984,8 @@ private: ffxFsr2ContextCreate(&context, &initializationParameters); } - void Fsr2_work_OnRender(uint32_t m_index,uint32_t renderWidth, uint32_t renderHeight, uint32_t displayWidth, uint32_t displayHeight) + void Fsr2_work_OnRender(uint32_t index,uint32_t renderWidth, uint32_t renderHeight, uint32_t displayWidth, uint32_t displayHeight) { - m_deltaTime = 33.3f;//33.3f for 30fps,if running at 60fps, the value passed should be around 16.6f. VkCommandBuffer commandBuffer = beginSingleTimeCommands(); FfxFsr2DispatchDescription dispatchParameters = {}; @@ -999,21 +1000,30 @@ private: //no transparency and compositon dispatchParameters.transparencyAndComposition = ffxGetTextureResourceVK(&context, nullptr, nullptr, 1, 1, VK_FORMAT_UNDEFINED, L"FSR2_EmptyTransparencyAndCompositionMap"); + float JitterX = 0.0f; + float JitterY = 0.0f; + + if (enableJitter) + { + const int32_t jitterPhaseCount = ffxFsr2GetJitterPhaseCount(renderWidth, displayWidth); + ffxFsr2GetJitterOffset(&JitterX, &JitterY, index, jitterPhaseCount); + } + dispatchParameters.output = ffxGetTextureResourceVK(&context, Fsr2_out_Image, Fsr2_out_ImageView, displayWidth, displayHeight, VK_FORMAT_R32G32B32A32_SFLOAT, L"FSR2_OutputUpscaledColor", FFX_RESOURCE_STATE_UNORDERED_ACCESS); - dispatchParameters.jitterOffset.x = 0.0f; - dispatchParameters.jitterOffset.y = 0.0f; + dispatchParameters.jitterOffset.x = JitterX; + dispatchParameters.jitterOffset.y = JitterY; dispatchParameters.motionVectorScale.x = (float)renderWidth; dispatchParameters.motionVectorScale.y = (float)renderHeight; - dispatchParameters.reset = false; + dispatchParameters.reset = reset; dispatchParameters.enableSharpening = false; dispatchParameters.sharpness = 0.0f; - dispatchParameters.frameTimeDelta = (float)m_deltaTime; + dispatchParameters.frameTimeDelta = (float)deltaTime; dispatchParameters.preExposure = 1.0f; dispatchParameters.renderSize.width = renderWidth; dispatchParameters.renderSize.height = renderHeight; dispatchParameters.cameraFar = 0.01f; dispatchParameters.cameraNear = 1e8; - dispatchParameters.cameraFovAngleVertical = 1.56f; + dispatchParameters.cameraFovAngleVertical = 1.56f;//fov 90กใ FfxErrorCode errorCode = ffxFsr2ContextDispatch(&context, &dispatchParameters); @@ -1021,7 +1031,7 @@ private: endSingleTimeCommands(commandBuffer); } - void Fsr2_work(std::string output_path, int index, std::vector& color_list, std::vector& motionVector_list, + void Fsr2_work(std::string &output_path, std::vector &filenames,int index, std::vector& color_list, std::vector& motionVector_list, uint32_t renderWidth, uint32_t renderHeight, uint32_t displayWidth, @@ -1039,11 +1049,18 @@ private: int width = displayWidth; int height = displayHeight; - DownloadFSR2Image(i, output_path, width, height, outChannels, Fsr2_out_Image, Fsr2_out_ImageMemory); + std::string file_name = filenames[i - 1]; + int suffix_pos = file_name.find(".exr"); + if (suffix_pos != file_name.npos) + { + file_name = file_name.substr(0, suffix_pos); + } + + DownloadFSR2Image(file_name, output_path, width, height, outChannels, Fsr2_out_Image, Fsr2_out_ImageMemory); cost_time = clock() - (render_cost_time + cost_time); - - printf("(%d / %d) done. render cost %f s, download image cost %f s.\n", i, color_list.size(), (float)((float)render_cost_time / CLOCKS_PER_SEC), (float)((float)cost_time / CLOCKS_PER_SEC)); + printf("%s ",filenames[i - 1].c_str()); + printf("(%d / %d) done. render cost %f s, download image cost %f s.\n", i, (int)color_list.size(), (float)((float)render_cost_time / CLOCKS_PER_SEC), (float)((float)cost_time / CLOCKS_PER_SEC)); if (i == color_list.size()) break; @@ -1058,21 +1075,22 @@ private: } } - void copy_work(std::vector &file_list,std::string &input_path_color, std::string &input_path_motionDepth, std::string &output_path) + void FSR2(config& config_) { std::vector source_list_color; std::vector source_list_motionDepth; - for (int i = 0; i < file_list.size(); i++) + for (int i = 0; i < config_.file_list.size(); i++) { - std::string file_name = file_list[i]; - std::string color_path = input_path_color + file_name; - std::string MotionDepth_path = input_path_motionDepth + file_name; + std::string file_name = config_.file_list[i]; + std::string color_path = config_.input_path + file_name; + std::string MotionDepth_path = config_.motion_depth_path + file_name; source_list_color.push_back(color_path); source_list_motionDepth.push_back(MotionDepth_path); } - scale_motion_x = -0.5f; - scale_motion_y = 0.5f; - scale_depth = 1000.0f; + scale_motion_x = config_.scale_motion_x; + scale_motion_y = config_.scale_motion_y; + scale_depth = config_.scale_depth; + deltaTime = config_.deltaTime; printf("hello,I'm ready to go!\n"); int texWidth, texHeight; @@ -1085,15 +1103,17 @@ private: //Let's do it! int outWidth, outHeight, outChannels; - outWidth = 3840; - outHeight = 2160; + outWidth = config_.outWidth; + outHeight = config_.outHeight; outChannels = texChannels; + enableJitter = config_.enableJitter; + reset = config_.reset; //create output result createImage(outWidth, outHeight, 1, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R32G32B32A32_SFLOAT, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, Fsr2_out_Image, Fsr2_out_ImageMemory); Fsr2_out_ImageView = createImageView(Fsr2_out_Image, VK_FORMAT_R32G32B32A32_SFLOAT, VK_IMAGE_ASPECT_COLOR_BIT, 1); - Fsr2_work(output_path, 1, source_list_color, source_list_motionDepth, texWidth, texHeight, outWidth, outHeight, outChannels, true, false); + Fsr2_work(config_.out_path, config_.file_list, 1, source_list_color, source_list_motionDepth, texWidth, texHeight, outWidth, outHeight, outChannels, true, false); printf("I am done.\n"); } diff --git a/src/VK/offscreen/helper.hpp b/src/VK/offscreen/helper.hpp index a051449..3963265 100644 --- a/src/VK/offscreen/helper.hpp +++ b/src/VK/offscreen/helper.hpp @@ -1,10 +1,36 @@ #pragma once #define TINYEXR_IMPLEMENTATION #include - +#include #include -std::vector read_image_exr(const std::string& file_path, int& w, int& h,int channel) { +struct config +{ + std::vector file_list; + std::string input_path = ""; + std::string motion_depth_path = ""; + std::string out_path = ""; + float scale_motion_x = 1.0f; + float scale_motion_y = 1.0f; + float scale_depth = 1.0f; + double deltaTime = 33.3f; + int outWidth = 1; + int outHeight = 1; + bool reset = false; + bool enableJitter = false; +}; + +bool get_files(std::vector& fileNames, std::string& input_path) +{ + FindFiles finder; + fileNames = finder.findFiles(input_path.c_str()); + if (fileNames.empty()) return false; + std::sort(fileNames.begin(), fileNames.end()); + return true; +} + +std::vector read_image_exr(const std::string& file_path, int& w, int& h,int channel) +{ float* data = nullptr; const char* err = nullptr; int ret = LoadEXR(&data, &w, &h, file_path.c_str(), &err); @@ -19,7 +45,8 @@ std::vector read_image_exr(const std::string& file_path, int& w, int& h,i } void write_exr(const std::string& _file_path, std::vector &data, int w, int h, int _channels=4, - bool use_half = false) { + bool use_half = false) +{ if (data.empty()) return; std::string file_path = _file_path; diff --git a/src/VK/offscreen/include/FindFiles.h b/src/VK/offscreen/include/FindFiles.h new file mode 100644 index 0000000..53daae1 --- /dev/null +++ b/src/VK/offscreen/include/FindFiles.h @@ -0,0 +1,143 @@ +/* + * Find and generate a file list of the folder. +**/ + +#ifndef FIND_FILES_H +#define FIND_FILES_H + +#include +#include + +class FindFiles +{ +public: + FindFiles(){} + ~FindFiles(){} + + std::vector findFiles( const char *lpPath, const char *secName = ".*" ); + +private: + std::vector file_lists; +}; + +#include +#include +#include +#include + +#ifdef WIN32 + +#include +#include + +std::vector FindFiles::findFiles( const char *lpPath, const char *secName /*= ".*" */ ) +{ + char szFind[MAX_PATH]; + char szFile[MAX_PATH]; + + WIN32_FIND_DATA FindFileData; + + strcpy(szFind,lpPath); + strcat(szFind,"\\*"); + strcat(szFind,secName); + + HANDLE hFind=::FindFirstFile(szFind,&FindFileData); + + if(INVALID_HANDLE_VALUE == hFind) + { + std::cout << "Empty folder!" << std::endl; + return std::vector(); + } + + do + { + if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if(FindFileData.cFileName[0]!='.') + { + strcpy(szFile,lpPath); + strcat(szFile,"\\"); + strcat(szFile,FindFileData.cFileName); + findFiles(szFile); + } + } + else + { + if ( szFile[0] ) + { + std::string filePath = lpPath; + filePath += "\\"; + filePath = FindFileData.cFileName; + file_lists.push_back(filePath); + } + else + { + std::string filePath = szFile; + filePath = FindFileData.cFileName; + file_lists.push_back(filePath); + } + } + + }while(::FindNextFile(hFind,&FindFileData)); + + ::FindClose(hFind); + return file_lists; +} + +#else + +#include +#include +#include + +std::vector FindFiles::findFiles( const char *lpPath, const char *secName /*= ".*" */ ) +{ + (void)secName; + + std::vector result; + std::queue queue; + std::string dirname; + + DIR *dir; + if ( !(dir = opendir ( lpPath )) ) { + return result; + } + queue.push( lpPath ); + + struct dirent *ent; + while ( !queue.empty() ) + { + + dirname = queue.front(); + dir = opendir( dirname.c_str() ); + queue.pop(); + if ( !dir ) { continue; } + + while( ent = readdir( dir ) ) + { + + if ( strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0 ) + { + continue; + } + if ( ent->d_type == DT_DIR ) + { + queue.push( dirname+"/"+ent->d_name ); + } + else + { + result.push_back( /*dirname + "/" + */ent->d_name); + } + + } + + closedir( dir ); + + } + + return result; +} + +#endif + +#endif // FIND_FILES_H diff --git a/src/VK/offscreen/main.cpp b/src/VK/offscreen/main.cpp index 95a6d08..4c48465 100644 --- a/src/VK/offscreen/main.cpp +++ b/src/VK/offscreen/main.cpp @@ -1,14 +1,101 @@ #include "Vulkan_FSR2.hpp" -int main() { - - std::vector file_list = {"1045.exr","1046.exr","1047.exr","1048.exr","1049.exr"}; - std::string input_path = "D:/stuff/ue_stuffs/showdown_out/test2/Input/"; - std::string motion_depth_path = "D:/stuff/ue_stuffs/showdown_out/test2/RawMotionDepth/"; - std::string out_path = "D:/workwork/sssr/FSR2output/"; +int main(int argc, const char* argv[]) { + if (argc < 2) { + std::cout << "Usage: FSR2 OffScreen [OPTIONS]\n\n"; + std::cout << "OPTIONS:\n"; + std::cout << " --color_path path of input color images\n"; + std::cout << " --mv_d_path path of motion vector and depth of input images\n"; + std::cout << " --out_path the output path of the result images\n"; + std::cout << " --s_mv_x the scale of x of motion vector\n"; + std::cout << " --s_mv_y the scale of y of motion vector\n"; + std::cout << " --s_depth the scale of depth\n"; + std::cout << " --fps Frames Per Second\n"; + std::cout << " --w output Width\n"; + std::cout << " --h output Height\n"; + std::cout << " --r reset for every frame\n"; + std::cout << " --j enable Jitter\n"; + + return 0; + } + std::vector file_list; + std::string input_path = ""; + std::string motion_depth_path = ""; + std::string out_path = ""; + float scale_motion_x = -0.5f;//default value cause I am working with ue4. + float scale_motion_y = 0.5f; + float scale_depth = 1000.0f; + double deltaTime = 33.3f;//33.3f for 30fps, if running at 60fps, the value passed should be around 16.6f. + float outWidth = 3840; + float outHeight = 2160; + bool reset = false; + bool enableJitter = false; + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--color_path") == 0 && i + 1 < argc) + { + input_path = argv[i + 1]; + } + else if (strcmp(argv[i], "--mv_d_path") == 0 && i + 1 < argc) + { + motion_depth_path = argv[i + 1]; + } + else if (strcmp(argv[i], "--out_path") == 0 && i + 1 < argc) + { + out_path = argv[i + 1]; + } + else if (strcmp(argv[i], "--s_mv_x") == 0 && i + 1 < argc) + { + scale_motion_x = atof(argv[i + 1]); + } + else if (strcmp(argv[i], "--s_mv_y") == 0 && i + 1 < argc) + { + scale_motion_y = atof(argv[i + 1]); + } + else if (strcmp(argv[i], "--s_depth") == 0 && i + 1 < argc) + { + scale_depth = atof(argv[i + 1]); + } + else if (strcmp(argv[i], "--fps") == 0 && i + 1 < argc) + { + double fps = atof(argv[i + 1]); + deltaTime = 1.0f / fps; + } + else if (strcmp(argv[i], "--w") == 0 && i + 1 < argc) + { + outWidth = atoi(argv[i + 1]); + } + else if (strcmp(argv[i], "--h") == 0 && i + 1 < argc) + { + outHeight = atoi(argv[i + 1]); + } + else if (strcmp(argv[i], "--r") == 0) + { + reset = true; + } + else if (strcmp(argv[i], "--j") == 0) + { + enableJitter = true; + } + } + get_files(file_list, input_path); + //copy + config config_fsr; + config_fsr.file_list = file_list; + config_fsr.input_path = input_path; + config_fsr.motion_depth_path = motion_depth_path; + config_fsr.out_path = out_path; + config_fsr.scale_motion_x = scale_motion_x; + config_fsr.scale_motion_y = scale_motion_y; + config_fsr.scale_depth = scale_depth; + config_fsr.deltaTime = deltaTime; + config_fsr.outWidth = outWidth; + config_fsr.outHeight = outHeight; + config_fsr.reset = reset; + config_fsr.enableJitter = enableJitter; Vulkan_FSR2 FSR2; - FSR2.run(file_list, input_path, motion_depth_path, out_path); + FSR2.run(config_fsr); return EXIT_SUCCESS;