1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstring>
18 #include <fstream>
19 #include <iomanip>
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 
25 namespace {
26 
StrSplit(const std::string & s,const char delimiter)27 std::vector<std::string> StrSplit(const std::string& s, const char delimiter) {
28   std::vector<std::string> result;
29   std::istringstream stream(s);
30   std::string item;
31   while (std::getline(stream, item, delimiter)) {
32     result.push_back(item);
33   }
34   return result;
35 }
36 
37 }  // namespace
38 
main(int argc,char ** argv)39 int main(int argc, char** argv) {
40   if (argc != 5) {
41     std::cout << "Expected 5 arguments.";
42     std::exit(1);
43   }
44 
45   const std::string input_glsl_filename = argv[1];
46   const std::string input_spirv_filename = argv[2];
47   const std::string input_spirv_varname = argv[3];
48   const std::string output_embed_filename = argv[4];
49 
50   std::ifstream input_glsl_file(input_glsl_filename);
51   if (!input_glsl_file.is_open()) {
52     std::cout << "Failed to open input glsl file " << input_spirv_filename;
53     std::exit(1);
54   }
55 
56   const std::string input_glsl(
57       (std::istreambuf_iterator<char>(input_glsl_file)),
58       std::istreambuf_iterator<char>());
59   const std::vector<std::string> input_glsl_lines = StrSplit(input_glsl, '\n');
60 
61   std::ifstream input_spirv_file(input_spirv_filename,
62                                  std::ios::ate | std::ios::binary);
63   if (!input_spirv_file.is_open()) {
64     std::cout << "Failed to open input spirv file " << input_spirv_filename;
65     std::exit(1);
66   }
67 
68   const std::size_t input_spirv_bytes_size =
69       static_cast<std::size_t>(input_spirv_file.tellg());
70   std::vector<unsigned char> input_spirv_bytes(input_spirv_bytes_size);
71   input_spirv_file.seekg(0);
72   input_spirv_file.read(reinterpret_cast<char*>(input_spirv_bytes.data()),
73                         input_spirv_bytes_size);
74   input_spirv_file.close();
75 
76   std::ofstream output_embed_file(output_embed_filename);
77   if (!output_embed_file.is_open()) {
78     std::cout << "Failed to open output file " << output_embed_filename;
79     std::exit(1);
80   }
81 
82   output_embed_file << "// Generated from GLSL:\n//\n";
83   for (const std::string& input_glsl_line : input_glsl_lines) {
84     output_embed_file << "// " << input_glsl_line << "\n";
85   }
86 
87   output_embed_file << "const std::vector<uint8_t> " << input_spirv_varname
88                     << " = {";
89 
90   const unsigned char* spirv_data = input_spirv_bytes.data();
91   for (std::size_t i = 0; i < input_spirv_bytes_size; i++) {
92     constexpr const std::size_t kNumBytesPerLine = 16;
93 
94     if (i % kNumBytesPerLine == 0) {
95       output_embed_file << "\n\t";
96     }
97 
98     output_embed_file << "0x" << std::hex << std::setfill('0') << std::setw(2)
99                       << static_cast<int>(*spirv_data) << ", ";
100     ++spirv_data;
101   }
102   output_embed_file << "\n};\n\n";
103 
104   output_embed_file.close();
105   return 0;
106 }