|
| 1 | +#include "air_compiler.h" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <spawn.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <sys/wait.h> |
| 7 | +#include <cstdio> |
| 8 | + |
| 9 | +std::vector<uint8_t> AirCompiler::compile(const std::string& shaderSource) { |
| 10 | + // First, generate AIR from shader source |
| 11 | + std::string inputFile = ".metal"; |
| 12 | + int tmpFD = makeTemporaryFile(inputFile); |
| 13 | + write(tmpFD, shaderSource.data(), shaderSource.size()); |
| 14 | + close(tmpFD); |
| 15 | + |
| 16 | + std::string irFile = ".ir"; |
| 17 | + tmpFD = makeTemporaryFile(irFile); |
| 18 | + close(tmpFD); |
| 19 | + |
| 20 | + pid_t pid; |
| 21 | + char* airArgv[] = { "xcrun", "-sdk", "macosx", "metal", "-o", irFile.data(), "-c", inputFile.data(), "-D__air__", "-DUNLEASHED_RECOMP", "-Wno-unused-variable", nullptr }; |
| 22 | + |
| 23 | + if (posix_spawn(&pid, "/usr/bin/xcrun", nullptr, nullptr, airArgv, nullptr) != 0) { |
| 24 | + unlink(inputFile.data()); |
| 25 | + unlink(irFile.data()); |
| 26 | + fmt::println("Failed to spawn AIR xcrun process"); |
| 27 | + exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + int status; |
| 31 | + |
| 32 | + if (waitpid(pid, &status, 0) == -1) { |
| 33 | + unlink(inputFile.data()); |
| 34 | + unlink(irFile.data()); |
| 35 | + fmt::println("Failed to wait AIR xcrun process"); |
| 36 | + exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 40 | + unlink(inputFile.data()); |
| 41 | + unlink(irFile.data()); |
| 42 | + fmt::println("AIR xcrun exited with code {}", WEXITSTATUS(status)); |
| 43 | + fmt::println("{}", shaderSource); |
| 44 | + exit(1); |
| 45 | + } |
| 46 | + |
| 47 | + unlink(inputFile.data()); |
| 48 | + |
| 49 | + // Now we need to turn the AIR into a .metallib |
| 50 | + std::string libFile = ".metallib"; |
| 51 | + tmpFD = makeTemporaryFile(libFile); |
| 52 | + close(tmpFD); |
| 53 | + |
| 54 | + char* libArgv[] = { "xcrun", "-sdk", "macosx", "metallib", "-o", libFile.data(), irFile.data(), nullptr }; |
| 55 | + |
| 56 | + if (posix_spawn(&pid, "/usr/bin/xcrun", nullptr, nullptr, libArgv, nullptr) != 0) { |
| 57 | + unlink(irFile.data()); |
| 58 | + unlink(libFile.data()); |
| 59 | + fmt::println("Failed to spawn .metallib xcrun process"); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + if (waitpid(pid, &status, 0) == -1) { |
| 64 | + unlink(irFile.data()); |
| 65 | + unlink(libFile.data()); |
| 66 | + fmt::println("Failed to wait .metallib xcrun process"); |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 71 | + unlink(irFile.data()); |
| 72 | + unlink(libFile.data()); |
| 73 | + fmt::println(".metallib exited with code {}", WEXITSTATUS(status)); |
| 74 | + exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + // Read from built .metallib |
| 78 | + FILE* file = fopen(libFile.data(), "rb"); |
| 79 | + fseek(file, 0, SEEK_END); |
| 80 | + size_t fileSize = ftell(file); |
| 81 | + fseek(file, 0, SEEK_SET); |
| 82 | + auto data = std::vector<uint8_t>(fileSize); |
| 83 | + fread(data.data(), 1, fileSize, file); |
| 84 | + fclose(file); |
| 85 | + |
| 86 | + // Cleanup temporary files |
| 87 | + unlink(irFile.data()); |
| 88 | + unlink(libFile.data()); |
| 89 | + |
| 90 | + return data; |
| 91 | +} |
| 92 | + |
| 93 | +int AirCompiler::makeTemporaryFile(std::string &extension) { |
| 94 | + const std::string path = "/tmp/xenos_metal_XXXXXX"; |
| 95 | + |
| 96 | + size_t size = path.size() + extension.size() + 1; |
| 97 | + char fullTemplate[size]; |
| 98 | + snprintf(fullTemplate, size, "%s%s", path.c_str(), extension.c_str()); |
| 99 | + |
| 100 | + int tmpFD = mkstemps(fullTemplate, extension.size()); |
| 101 | + if (tmpFD == -1) { |
| 102 | + fmt::println("Failed to open temporary file, \"{}\"!", std::string(fullTemplate)); |
| 103 | + unlink(fullTemplate); |
| 104 | + exit(1); |
| 105 | + } |
| 106 | + |
| 107 | + extension = fullTemplate; |
| 108 | + return tmpFD; |
| 109 | +} |
0 commit comments