Utilities.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // The MIT License(MIT)
  2. //
  3. // Copyright(c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files(the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #pragma once
  22. #include <chrono>
  23. #include <filesystem>
  24. #include <iomanip>
  25. #include <iostream>
  26. #include <map>
  27. #include <sstream>
  28. #include <unordered_map>
  29. #include <vector>
  30. inline std::vector<std::filesystem::path> getFiles(const std::string& path, const std::string& ext = "")
  31. {
  32. namespace fs = std::filesystem;
  33. std::vector<fs::path> ret;
  34. for (const auto& e : fs::directory_iterator(path)) {
  35. if (ext == "" || e.path().extension() == ext)
  36. ret.push_back(e.path());
  37. }
  38. return ret;
  39. }
  40. class FPS {
  41. public:
  42. FPS(double maxTime = 100000) : m_maxTime(maxTime), m_totalTime(0), m_averageTime(0), m_numFrames(0) {
  43. m_t0 = std::chrono::high_resolution_clock::now();
  44. }
  45. void update() {
  46. m_t1 = std::chrono::high_resolution_clock::now();
  47. m_totalTime += std::chrono::duration_cast<std::chrono::microseconds>(m_t1 - m_t0).count();
  48. m_t0 = m_t1;
  49. m_numFrames++;
  50. // update only when time is up
  51. if (m_totalTime > m_maxTime)
  52. {
  53. m_averageTime = m_totalTime / m_numFrames;
  54. m_totalTime = 0.0;
  55. m_numFrames = 0;
  56. }
  57. }
  58. void setMaxTime(double maxTime) {
  59. m_maxTime = maxTime;
  60. }
  61. double averageTime_us() {
  62. return m_averageTime;
  63. }
  64. double averageTime_ms() {
  65. return m_averageTime / 1E3;
  66. }
  67. double fps() {
  68. return 1 / m_averageTime * 1E6;
  69. }
  70. private:
  71. double m_maxTime;
  72. double m_totalTime;
  73. double m_averageTime;
  74. size_t m_numFrames;
  75. std::chrono::high_resolution_clock::time_point m_t0, m_t1;
  76. };
  77. class ElapsedTimer {
  78. public:
  79. ElapsedTimer(uint32_t maxIterations = 500) : m_maxIterations(maxIterations), m_totalTime(0), m_averageTime(0) {
  80. m_t0 = std::chrono::high_resolution_clock::now();
  81. }
  82. void start() {
  83. m_t0 = std::chrono::high_resolution_clock::now();
  84. }
  85. void end() {
  86. m_t1 = std::chrono::high_resolution_clock::now();
  87. m_totalTime += std::chrono::duration_cast<std::chrono::microseconds>(m_t1 - m_t0).count();
  88. m_numIterations++;
  89. if (m_numIterations > m_maxIterations)
  90. {
  91. m_averageTime = m_totalTime / m_maxIterations;
  92. m_totalTime = 0.0;
  93. m_numIterations = 0;
  94. }
  95. }
  96. void setMaxTime(uint32_t maxTime) {
  97. m_maxIterations = maxTime;
  98. }
  99. double averageTime_us() {
  100. return m_averageTime;
  101. }
  102. double averageTime_ms() {
  103. return m_averageTime / 1E3;
  104. }
  105. private:
  106. uint32_t m_maxIterations;
  107. double m_totalTime;
  108. double m_averageTime;
  109. size_t m_numIterations;
  110. std::chrono::high_resolution_clock::time_point m_t0, m_t1;
  111. };
  112. class ArgParser {
  113. public:
  114. ArgParser(int argc, char* argv[]) {
  115. programName = argv[0];
  116. addOption("-h", "Print this help");
  117. for (size_t i = 1; i < argc; ++i)
  118. arguments.push_back(argv[i]);
  119. }
  120. void addOption(const std::string& opt, const std::string& description) {
  121. options[opt] = description;
  122. }
  123. void printHelp() {
  124. std::cout << "Usage: " << programName << " " << std::endl;
  125. std::cout << "----------------------------------------" << std::endl;
  126. for (auto& e : options) {
  127. std::cout << e.first << " " << e.second << std::endl;;
  128. }
  129. }
  130. bool parse(bool requiredArgs = false) {
  131. if (arguments.size() > 0 && arguments[0] == "-h") {
  132. printHelp();
  133. return false;
  134. }
  135. if (arguments.size() < 1) {
  136. return !requiredArgs;
  137. }
  138. bool lastOpt = false;
  139. size_t i = 0;
  140. while (i < arguments.size()) {
  141. if (options.find(arguments[i]) != options.end() && i < arguments.size() - 1) {
  142. argMap[arguments[i]] = arguments[i + 1];
  143. i += 2;
  144. }
  145. else {
  146. std::cout << "Argument not found : " << arguments[i] << std::endl;
  147. printHelp();
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. template<typename T>
  154. T get(const std::string& opt, T defaultVal = T()) {
  155. T val;
  156. auto sval = argMap.find(opt);
  157. if (sval == argMap.end())
  158. return defaultVal;
  159. std::stringstream ss;
  160. ss << sval->second;
  161. ss >> val;
  162. return val;
  163. }
  164. private:
  165. std::map<std::string, std::string> options;
  166. std::vector<std::string> arguments;
  167. std::unordered_map<std::string, std::string> argMap;
  168. std::string programName;
  169. };
  170. #ifndef NIS_VK_SAMPLE
  171. inline std::wstring widen(const std::string& str)
  172. {
  173. int size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), int(str.size()) + 1, 0, 0);
  174. std::vector<wchar_t> temp(size);
  175. MultiByteToWideChar(CP_ACP, 0, str.c_str(), int(str.size()) + 1, &temp[0], int(temp.size()));
  176. return std::wstring(&temp[0]);
  177. }
  178. #endif
  179. template <typename T>
  180. inline std::string toStr(T value)
  181. {
  182. return std::to_string(value);
  183. }
  184. template <>
  185. inline std::string toStr<bool>(bool value)
  186. {
  187. return value ? "1" : "0";
  188. }
  189. template <>
  190. inline std::string toStr<std::string>(std::string value)
  191. {
  192. return value;
  193. }
  194. template <>
  195. inline std::string toStr<const char*>(const char* value)
  196. {
  197. return value;
  198. }
  199. inline uint32_t Align(uint32_t x, uint32_t alignment) {
  200. return (x + alignment - 1) / alignment * alignment;
  201. }