Skip to content

Instantly share code, notes, and snippets.

@mlfarrell
Last active September 3, 2020 18:52
Show Gist options
  • Save mlfarrell/efc6e90dd7247dc29684df68cc61615d to your computer and use it in GitHub Desktop.
Save mlfarrell/efc6e90dd7247dc29684df68cc61615d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <utility>
using namespace std;
int main(int argc, char **argv)
{
auto loadSPIRV = [](const string &spirvPath) -> pair<uint8_t *, size_t> {
ifstream file(spirvPath, ios::ate | ios::binary);
if(!file.is_open())
return { nullptr, 0 };
size_t fileSize = (size_t)file.tellg();
uint8_t *data = new uint8_t[fileSize];
file.seekg(0);
file.read((char *)data, fileSize);
file.close();
return { data, fileSize };
};
auto varName = [](string spirvPath) {
string ret;
for(auto c : spirvPath)
{
if(c == '.')
ret += '_';
else
ret += c;
}
return ret;
};
if(argc != 3)
return 1;
auto [data, sz] = loadSPIRV(argv[1]);
if(data)
{
ofstream out(argv[2]);
if(!out.is_open())
return 1;
out << "/*spv2h for " << argv[1] << "*/" << endl << endl;
out << "static const uint8_t " << varName(argv[1]) << "[] = {";
for(size_t i = 0; i < sz; i++)
{
if(i % 40 == 0)
out << endl;
out << "\t" << hex << "0x" << (int)data[i] << ",";
}
out << endl;
out << "};" << endl << endl;
delete []data;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment