vulkan-shaders-gen.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <stdexcept>
  6. #include <array>
  7. #include <vector>
  8. #include <map>
  9. #include <thread>
  10. #include <mutex>
  11. #include <future>
  12. #include <queue>
  13. #include <condition_variable>
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <cstdlib>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #include <direct.h> // For _mkdir on Windows
  22. #else
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #include <fcntl.h>
  26. #endif
  27. #define ASYNCIO_CONCURRENCY 64
  28. std::mutex lock;
  29. std::vector<std::pair<std::string, std::string>> shader_fnames;
  30. std::string GLSLC = "glslc";
  31. std::string input_dir = "vulkan-shaders";
  32. std::string output_dir = "/tmp";
  33. std::string target_hpp = "ggml-vulkan-shaders.hpp";
  34. std::string target_cpp = "ggml-vulkan-shaders.cpp";
  35. bool no_clean = false;
  36. const std::vector<std::string> type_names = {
  37. "f32",
  38. "f16",
  39. "q4_0",
  40. "q4_1",
  41. "q5_0",
  42. "q5_1",
  43. "q8_0",
  44. "q2_k",
  45. "q3_k",
  46. "q4_k",
  47. "q5_k",
  48. "q6_k"
  49. };
  50. void execute_command(const std::string& command, std::string& stdout_str, std::string& stderr_str) {
  51. #ifdef _WIN32
  52. HANDLE stdout_read, stdout_write;
  53. HANDLE stderr_read, stderr_write;
  54. SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  55. if (!CreatePipe(&stdout_read, &stdout_write, &sa, 0) ||
  56. !SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0)) {
  57. throw std::runtime_error("Failed to create stdout pipe");
  58. }
  59. if (!CreatePipe(&stderr_read, &stderr_write, &sa, 0) ||
  60. !SetHandleInformation(stderr_read, HANDLE_FLAG_INHERIT, 0)) {
  61. throw std::runtime_error("Failed to create stderr pipe");
  62. }
  63. PROCESS_INFORMATION pi;
  64. STARTUPINFOA si = { sizeof(STARTUPINFOA) };
  65. si.dwFlags = STARTF_USESTDHANDLES;
  66. si.hStdOutput = stdout_write;
  67. si.hStdError = stderr_write;
  68. std::vector<char> cmd(command.begin(), command.end());
  69. cmd.push_back('\0');
  70. if (!CreateProcessA(NULL, cmd.data(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
  71. throw std::runtime_error("Failed to create process");
  72. }
  73. CloseHandle(stdout_write);
  74. CloseHandle(stderr_write);
  75. std::array<char, 128> buffer;
  76. DWORD bytes_read;
  77. while (ReadFile(stdout_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
  78. stdout_str.append(buffer.data(), bytes_read);
  79. }
  80. while (ReadFile(stderr_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
  81. stderr_str.append(buffer.data(), bytes_read);
  82. }
  83. CloseHandle(stdout_read);
  84. CloseHandle(stderr_read);
  85. WaitForSingleObject(pi.hProcess, INFINITE);
  86. CloseHandle(pi.hProcess);
  87. CloseHandle(pi.hThread);
  88. #else
  89. int stdout_pipe[2];
  90. int stderr_pipe[2];
  91. if (pipe(stdout_pipe) != 0 || pipe(stderr_pipe) != 0) {
  92. throw std::runtime_error("Failed to create pipes");
  93. }
  94. pid_t pid = fork();
  95. if (pid < 0) {
  96. throw std::runtime_error("Failed to fork process");
  97. }
  98. if (pid == 0) {
  99. close(stdout_pipe[0]);
  100. close(stderr_pipe[0]);
  101. dup2(stdout_pipe[1], STDOUT_FILENO);
  102. dup2(stderr_pipe[1], STDERR_FILENO);
  103. close(stdout_pipe[1]);
  104. close(stderr_pipe[1]);
  105. execl("/bin/sh", "sh", "-c", command.c_str(), (char*) nullptr);
  106. _exit(EXIT_FAILURE);
  107. } else {
  108. close(stdout_pipe[1]);
  109. close(stderr_pipe[1]);
  110. std::array<char, 128> buffer;
  111. ssize_t bytes_read;
  112. while ((bytes_read = read(stdout_pipe[0], buffer.data(), buffer.size())) > 0) {
  113. stdout_str.append(buffer.data(), bytes_read);
  114. }
  115. while ((bytes_read = read(stderr_pipe[0], buffer.data(), buffer.size())) > 0) {
  116. stderr_str.append(buffer.data(), bytes_read);
  117. }
  118. close(stdout_pipe[0]);
  119. close(stderr_pipe[0]);
  120. waitpid(pid, nullptr, 0);
  121. }
  122. #endif
  123. }
  124. bool directory_exists(const std::string& path) {
  125. struct stat info;
  126. if (stat(path.c_str(), &info) != 0) {
  127. return false; // Path doesn't exist or can't be accessed
  128. }
  129. return (info.st_mode & S_IFDIR) != 0; // Check if it is a directory
  130. }
  131. bool create_directory(const std::string& path) {
  132. #ifdef _WIN32
  133. return _mkdir(path.c_str()) == 0 || errno == EEXIST; // EEXIST means the directory already exists
  134. #else
  135. return mkdir(path.c_str(), 0755) == 0 || errno == EEXIST; // 0755 is the directory permissions
  136. #endif
  137. }
  138. std::string to_uppercase(const std::string& input) {
  139. std::string result = input;
  140. for (char& c : result) {
  141. c = std::toupper(c);
  142. }
  143. return result;
  144. }
  145. bool string_ends_with(const std::string& str, const std::string& suffix) {
  146. if (suffix.size() > str.size()) {
  147. return false;
  148. }
  149. return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
  150. }
  151. #ifdef _WIN32
  152. static const char path_separator = '\\';
  153. #else
  154. static const char path_separator = '/';
  155. #endif
  156. std::string join_paths(const std::string& path1, const std::string& path2) {
  157. return path1 + path_separator + path2;
  158. }
  159. std::string basename(const std::string &path) {
  160. return path.substr(path.find_last_of("/\\") + 1);
  161. }
  162. void string_to_spv(const std::string& _name, const std::string& in_fname, const std::map<std::string, std::string>& defines, bool fp16 = true) {
  163. std::string name = _name + (fp16 ? "" : "_fp32");
  164. std::string out_fname = join_paths(output_dir, name + ".spv");
  165. std::string in_path = join_paths(input_dir, in_fname);
  166. std::vector<std::string> cmd = {GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", in_path, "-o", out_fname};
  167. for (const auto& define : defines) {
  168. cmd.push_back("-D" + define.first + "=" + define.second);
  169. }
  170. std::string command;
  171. for (const auto& part : cmd) {
  172. command += part + " ";
  173. }
  174. std::string stdout_str, stderr_str;
  175. try {
  176. // std::cout << "Executing command: ";
  177. // for (const auto& part : cmd) {
  178. // std::cout << part << " ";
  179. // }
  180. // std::cout << std::endl;
  181. execute_command(command, stdout_str, stderr_str);
  182. if (!stderr_str.empty()) {
  183. std::cerr << "cannot compile " << name << "\n\n" << command << "\n\n" << stderr_str << std::endl;
  184. return;
  185. }
  186. std::lock_guard<std::mutex> guard(lock);
  187. shader_fnames.push_back(std::make_pair(name, out_fname));
  188. } catch (const std::exception& e) {
  189. std::cerr << "Error executing command for " << name << ": " << e.what() << std::endl;
  190. }
  191. }
  192. std::map<std::string, std::string> merge_maps(const std::map<std::string, std::string>& a, const std::map<std::string, std::string>& b) {
  193. std::map<std::string, std::string> result = a;
  194. result.insert(b.begin(), b.end());
  195. return result;
  196. }
  197. void matmul_shaders(std::vector<std::future<void>>& tasks, bool fp16, bool matmul_id) {
  198. std::string load_vec = fp16 ? "8" : "4";
  199. std::string aligned_b_type_f32 = fp16 ? "mat2x4" : "vec4";
  200. std::string aligned_b_type_f16 = fp16 ? "f16mat2x4" : "f16vec4";
  201. std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", fp16 ? "float16_t" : "float"}};
  202. std::string shader_name = "matmul";
  203. if (matmul_id) {
  204. base_dict["MUL_MAT_ID"] = "1";
  205. shader_name = "matmul_id";
  206. }
  207. if (fp16) {
  208. base_dict["FLOAT16"] = "1";
  209. }
  210. // Shaders with f16 B_TYPE
  211. tasks.push_back(std::async(std::launch::async, [=] {
  212. string_to_spv(shader_name + "_f32_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
  213. }));
  214. tasks.push_back(std::async(std::launch::async, [=] {
  215. string_to_spv(shader_name + "_f32_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
  216. }));
  217. tasks.push_back(std::async(std::launch::async, [=] {
  218. string_to_spv(shader_name + "_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
  219. }));
  220. tasks.push_back(std::async(std::launch::async, [=] {
  221. string_to_spv(shader_name + "_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
  222. }));
  223. for (const auto& tname : type_names) {
  224. std::string data_a_key = "DATA_A_" + to_uppercase(tname);
  225. std::string load_vec_a = (tname == "f32" || tname == "f16") ? load_vec : "2";
  226. tasks.push_back(std::async(std::launch::async, [=] {
  227. string_to_spv(shader_name + "_" + tname + "_f32", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}), fp16);
  228. }));
  229. tasks.push_back(std::async(std::launch::async, [=] {
  230. string_to_spv(shader_name + "_" + tname + "_f32_aligned", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f32}, {"D_TYPE", "float"}}), fp16);
  231. }));
  232. }
  233. }
  234. void process_shaders(std::vector<std::future<void>>& tasks) {
  235. std::cout << "ggml_vulkan: Generating and compiling shaders to SPIR-V" << std::endl;
  236. std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", "float"}};
  237. for (const auto& fp16 : {false, true}) {
  238. matmul_shaders(tasks, fp16, false);
  239. matmul_shaders(tasks, fp16, true);
  240. }
  241. for (const auto& tname : type_names) {
  242. // mul mat vec
  243. std::string data_a_key = "DATA_A_" + to_uppercase(tname);
  244. std::string shader = (string_ends_with(tname, "_k")) ? "mul_mat_vec_" + tname + ".comp" : "mul_mat_vec.comp";
  245. tasks.push_back(std::async(std::launch::async, [=] {
  246. string_to_spv("mul_mat_vec_" + tname + "_f32_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  247. }));
  248. tasks.push_back(std::async(std::launch::async, [=] {
  249. string_to_spv("mul_mat_vec_" + tname + "_f16_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
  250. }));
  251. tasks.push_back(std::async(std::launch::async, [=] {
  252. string_to_spv("mul_mat_vec_id_" + tname + "_f32", shader, merge_maps(base_dict, {{"MUL_MAT_ID", "1"}, {data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  253. }));
  254. // Dequant shaders
  255. if (tname != "f16") {
  256. tasks.push_back(std::async(std::launch::async, [=] {
  257. string_to_spv("dequant_" + tname, "dequant_" + tname + ".comp", merge_maps(base_dict, {{data_a_key, "1"}, {"D_TYPE", "float16_t"}}));
  258. }));
  259. }
  260. if (!string_ends_with(tname, "_k")) {
  261. shader = (tname == "f32" || tname == "f16") ? "get_rows.comp" : "get_rows_quant.comp";
  262. if (tname == "f16") {
  263. tasks.push_back(std::async(std::launch::async, [=] {
  264. string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
  265. }));
  266. } else {
  267. tasks.push_back(std::async(std::launch::async, [=] {
  268. string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}});
  269. }));
  270. }
  271. tasks.push_back(std::async(std::launch::async, [=] {
  272. string_to_spv("get_rows_" + tname + "_f32", shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float"}});
  273. }));
  274. }
  275. }
  276. tasks.push_back(std::async(std::launch::async, [] {
  277. string_to_spv("mul_mat_vec_p021_f16_f32", "mul_mat_vec_p021.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  278. }));
  279. tasks.push_back(std::async(std::launch::async, [] {
  280. string_to_spv("mul_mat_vec_nc_f16_f32", "mul_mat_vec_nc.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  281. }));
  282. // Norms
  283. tasks.push_back(std::async(std::launch::async, [=] {
  284. string_to_spv("norm_f32", "norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  285. }));
  286. tasks.push_back(std::async(std::launch::async, [=] {
  287. string_to_spv("rms_norm_f32", "rms_norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  288. }));
  289. tasks.push_back(std::async(std::launch::async, [] {
  290. string_to_spv("cpy_f32_f32", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  291. }));
  292. tasks.push_back(std::async(std::launch::async, [] {
  293. string_to_spv("cpy_f32_f16", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float16_t"}});
  294. }));
  295. tasks.push_back(std::async(std::launch::async, [] {
  296. string_to_spv("cpy_f16_f16", "copy.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
  297. }));
  298. tasks.push_back(std::async(std::launch::async, [] {
  299. string_to_spv("add_f32", "add.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  300. }));
  301. tasks.push_back(std::async(std::launch::async, [] {
  302. string_to_spv("split_k_reduce", "mul_mat_split_k_reduce.comp", {});
  303. }));
  304. tasks.push_back(std::async(std::launch::async, [] {
  305. string_to_spv("mul_f32", "mul.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  306. }));
  307. tasks.push_back(std::async(std::launch::async, [] {
  308. string_to_spv("div_f32", "div.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  309. }));
  310. tasks.push_back(std::async(std::launch::async, [] {
  311. string_to_spv("scale_f32", "scale.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  312. }));
  313. tasks.push_back(std::async(std::launch::async, [] {
  314. string_to_spv("sqr_f32", "square.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  315. }));
  316. tasks.push_back(std::async(std::launch::async, [] {
  317. string_to_spv("clamp_f32", "clamp.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  318. }));
  319. tasks.push_back(std::async(std::launch::async, [] {
  320. string_to_spv("gelu_f32", "gelu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  321. }));
  322. tasks.push_back(std::async(std::launch::async, [] {
  323. string_to_spv("silu_f32", "silu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  324. }));
  325. tasks.push_back(std::async(std::launch::async, [] {
  326. string_to_spv("relu_f32", "relu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  327. }));
  328. tasks.push_back(std::async(std::launch::async, [] {
  329. string_to_spv("diag_mask_inf_f32", "diag_mask_inf.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  330. }));
  331. tasks.push_back(std::async(std::launch::async, [=] {
  332. string_to_spv("soft_max_f32", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  333. }));
  334. tasks.push_back(std::async(std::launch::async, [=] {
  335. string_to_spv("soft_max_f32_f16", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
  336. }));
  337. tasks.push_back(std::async(std::launch::async, [] {
  338. string_to_spv("rope_norm_f32", "rope_norm.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  339. }));
  340. tasks.push_back(std::async(std::launch::async, [] {
  341. string_to_spv("rope_norm_f16", "rope_norm.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
  342. }));
  343. tasks.push_back(std::async(std::launch::async, [] {
  344. string_to_spv("rope_neox_f32", "rope_neox.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  345. }));
  346. tasks.push_back(std::async(std::launch::async, [] {
  347. string_to_spv("rope_neox_f16", "rope_neox.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
  348. }));
  349. tasks.push_back(std::async(std::launch::async, [] {
  350. string_to_spv("argsort_f32", "argsort.comp", {{"A_TYPE", "float"}});
  351. }));
  352. tasks.push_back(std::async(std::launch::async, [=] {
  353. string_to_spv("sum_rows_f32", "sum_rows.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  354. }));
  355. }
  356. void write_output_files() {
  357. FILE* hdr = fopen(target_hpp.c_str(), "w");
  358. FILE* src = fopen(target_cpp.c_str(), "w");
  359. fprintf(hdr, "#include <cstdint>\n\n");
  360. fprintf(src, "#include \"%s\"\n\n", basename(target_hpp).c_str());
  361. for (const auto& pair : shader_fnames) {
  362. const std::string& name = pair.first;
  363. const std::string& path = pair.second;
  364. FILE* spv = fopen(path.c_str(), "rb");
  365. if (!spv) {
  366. std::cerr << "Error opening SPIR-V file: " << path << "\n";
  367. continue;
  368. }
  369. fseek(spv, 0, SEEK_END);
  370. size_t size = ftell(spv);
  371. fseek(spv, 0, SEEK_SET);
  372. std::vector<unsigned char> data(size);
  373. size_t read_size = fread(data.data(), 1, size, spv);
  374. fclose(spv);
  375. if (read_size != size) {
  376. std::cerr << "Error reading SPIR-V file: " << path << "\n";
  377. continue;
  378. }
  379. fprintf(hdr, "extern unsigned char %s_data[%zu];\n", name.c_str(), size);
  380. fprintf(hdr, "const uint64_t %s_len = %zu;\n\n", name.c_str(), size);
  381. fprintf(src, "unsigned char %s_data[%zu] = {\n", name.c_str(), size);
  382. for (size_t i = 0; i < size; ++i) {
  383. fprintf(src, "0x%02x,", data[i]);
  384. if ((i + 1) % 12 == 0) fprintf(src, "\n");
  385. }
  386. fprintf(src, "\n};\n\n");
  387. if (!no_clean) {
  388. std::remove(path.c_str());
  389. }
  390. }
  391. fclose(hdr);
  392. fclose(src);
  393. }
  394. int main(int argc, char** argv) {
  395. std::map<std::string, std::string> args;
  396. for (int i = 1; i < argc; i += 2) {
  397. if (i + 1 < argc) {
  398. args[argv[i]] = argv[i + 1];
  399. }
  400. }
  401. if (args.find("--glslc") != args.end()) {
  402. GLSLC = args["--glslc"]; // Path to glslc
  403. }
  404. if (args.find("--input-dir") != args.end()) {
  405. input_dir = args["--input-dir"]; // Directory containing shader sources
  406. }
  407. if (args.find("--output-dir") != args.end()) {
  408. output_dir = args["--output-dir"]; // Directory for containing SPIR-V output
  409. }
  410. if (args.find("--target-hpp") != args.end()) {
  411. target_hpp = args["--target-hpp"]; // Path to generated header file
  412. }
  413. if (args.find("--target-cpp") != args.end()) {
  414. target_cpp = args["--target-cpp"]; // Path to generated cpp file
  415. }
  416. if (args.find("--no-clean") != args.end()) {
  417. no_clean = true; // Keep temporary SPIR-V files in output-dir after build
  418. }
  419. if (!directory_exists(input_dir)) {
  420. std::cerr << "\"" << input_dir << "\" must be a valid directory containing shader sources" << std::endl;
  421. return EXIT_FAILURE;
  422. }
  423. if (!directory_exists(output_dir)) {
  424. if (!create_directory(output_dir)) {
  425. std::cerr << "Error creating output directory: " << output_dir << "\n";
  426. return EXIT_FAILURE;
  427. }
  428. }
  429. std::vector<std::future<void>> tasks;
  430. process_shaders(tasks);
  431. for (auto& task : tasks) {
  432. task.get();
  433. }
  434. write_output_files();
  435. return EXIT_SUCCESS;
  436. }