Blender V5.3
shader_tool.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <filesystem>
11#include <fstream>
12#include <iostream>
13#include <string>
14
15#include "processor.hh"
16
17using namespace blender::gpu::shader;
18
19static std::vector<std::string> list_files(const std::string &dir)
20{
21 std::vector<std::string> files;
22 for (const auto &entry : std::filesystem::directory_iterator(std::filesystem::path(dir))) {
23 if (entry.is_regular_file()) {
24 std::string filename(entry.path().string());
25 /* We only allow including header files or shader files. */
26 if (filename.find(".hh") != std::string::npos ||
27 filename.find(".msl") != std::string::npos ||
28 filename.find(".glsl") != std::string::npos)
29 {
30 files.push_back(filename);
31 }
32 }
33 }
34 return files;
35}
36
37static metadata::Source scan_external_symbols(const std::vector<std::string> &file_list,
38 std::vector<std::string> &visited_files,
39 const std::string &file_buffer,
40 const std::string &file_name)
41{
42 Language language = language_from_filename(file_name);
43 SourceProcessor processor(file_buffer, file_name, language, file_list);
44
45 metadata::Source include_data = processor.parse_include_and_symbols();
46
47 bool errors = false;
48
49 for (const auto &dep : include_data.dependencies) {
50 std::string file;
51 for (const auto &filename : file_list) {
52 if (filename.find(dep) != std::string::npos) {
53 file = filename;
54 }
55 }
56
57 if (file.empty()) {
58 std::cout << "Error: Included file not found " << dep << std::endl;
59 errors = true;
60 }
61 else if (std::find(visited_files.begin(), visited_files.end(), file) == visited_files.end()) {
62 visited_files.emplace_back(file);
63
64 std::ifstream input_file(file);
65 if (!input_file) {
66 std::cerr << "Error: Could not open file " << file << std::endl;
67 errors = true;
68 }
69 else {
70 std::stringstream buffer;
71 buffer << input_file.rdbuf();
72
74 file_list, visited_files, buffer.str(), file);
75
76 /* Extend list. */
77 include_data.symbol_table.insert(include_data.symbol_table.end(),
78 source.symbol_table.begin(),
79 source.symbol_table.end());
80 include_data.template_definitions.insert(include_data.template_definitions.end(),
81 source.template_definitions.begin(),
82 source.template_definitions.end());
83 }
84 }
85 }
86
87 if (errors) {
88 exit(1);
89 }
90 return include_data;
91}
92
93int main(int argc, char **argv)
94{
95 using namespace blender;
96 if (argc < 6) {
97 std::cerr << "Usage: shader_tool <data_file_from> <data_file_to> <metadata_file_to> "
98 "<infos_file_to> <dep_file_to> <include_dir1> <include_dir2> ..."
99 << std::endl;
100 exit(1);
101 }
102
103 const char *input_file_name = argv[1];
104 const char *output_file_name = argv[2];
105 const char *metadata_file_name = argv[3];
106 const char *infos_file_name = argv[4];
107 const char *dep_file_name = argv[5];
108
109 /* Open the input file for reading */
110 std::ifstream input_file(input_file_name);
111 if (!input_file) {
112 std::cerr << "Error: Could not open input file " << input_file_name << std::endl;
113 exit(1);
114 }
115
116 /* We make the required directories here rather than having the build system
117 * do the work for us, as having cmake do it leads to several thousand cmake
118 * instances being launched, leading to significant overhead, see pr #141404
119 * for details. */
120 std::filesystem::path parent_dir = std::filesystem::path(output_file_name).parent_path();
121 std::error_code ec;
122 if (!std::filesystem::create_directories(parent_dir, ec)) {
123 if (ec) {
124 std::cerr << "Unable to create " << parent_dir << " : " << ec.message() << std::endl;
125 exit(1);
126 }
127 }
128
129 /* Open the output file for writing */
130 std::ofstream output_file(output_file_name, std::ofstream::out | std::ofstream::binary);
131 if (!output_file) {
132 std::cerr << "Error: Could not open output file " << output_file_name << std::endl;
133 input_file.close();
134 exit(1);
135 }
136
137 /* Open the output file for writing */
138 std::ofstream metadata_file(metadata_file_name, std::ofstream::out | std::ofstream::binary);
139 if (!metadata_file) {
140 std::cerr << "Error: Could not open output file " << metadata_file_name << std::endl;
141 input_file.close();
142 exit(1);
143 }
144
145 /* Open the output file for writing */
146 std::ofstream infos_file(infos_file_name, std::ofstream::out | std::ofstream::binary);
147 if (!infos_file) {
148 std::cerr << "Error: Could not open output file " << infos_file_name << std::endl;
149 input_file.close();
150 exit(1);
151 }
152
153 /* Open the output file for writing */
154 std::ofstream dep_file(dep_file_name, std::ofstream::out | std::ofstream::binary);
155 if (!dep_file) {
156 std::cerr << "Error: Could not open output file " << dep_file_name << std::endl;
157 input_file.close();
158 exit(1);
159 }
160
161 /* List of files available for include. */
162 std::vector<std::string> file_list;
163 for (int i = 6; i < argc; i++) {
164 auto list = list_files(std::string(argv[i]));
165 /* Extend list. */
166 file_list.insert(file_list.end(), list.begin(), list.end());
167 }
168
169 std::stringstream buffer;
170 buffer << input_file.rdbuf();
171
172 std::string filename(input_file_name);
173 const bool is_info = filename.ends_with("infos.hh") || filename.ends_with(".bsl.hh");
174
175 using namespace gpu::shader;
176
177 Language language = language_from_filename(filename);
178
179 if (language == Language::GLSL) {
180 /* All build-time GLSL files should be considered blender-GLSL. */
181 language = Language::BLENDER_GLSL;
182 }
183
184 if (language == Language::BSL) {
185 /* Until the new BSL compiler is fully working, use the legacy one. */
186 if (/* Add non-ported files here. */
187 filename.ends_with("gpu_shader_material_radial_tiling.bsl.hh") ||
188 filename.ends_with("gpu_shader_material_tex_voronoi.bsl.hh") ||
189 /* Need new compiler. */
190 (filename.find("gpu/shaders/") == std::string::npos &&
191 filename.find("draw/intern/shaders/") == std::string::npos &&
192 filename.find("workbench/shaders/") == std::string::npos &&
193 filename.find("overlay/shaders/") == std::string::npos &&
194 filename.find("select/shaders/") == std::string::npos &&
195 filename.find("eevee/shaders/") == std::string::npos &&
196 filename.find("gpencil/shaders/") == std::string::npos))
197 {
198 language = Language::BLENDER_GLSL;
199 }
200 }
201
202 metadata::Source external_symbols;
203 std::vector<std::string> visited_files{input_file_name};
204 if (language == Language::BLENDER_GLSL) {
205 external_symbols = scan_external_symbols(file_list, visited_files, buffer.str(), filename);
206 }
207
208 SourceProcessor processor(buffer.str(), input_file_name, language, file_list);
209
210 auto [result, metadata, error] = processor.convert(external_symbols);
211
212 output_file << result;
213
214 size_t last_slash = filename.find_last_of('/');
215 std::string name = (last_slash == std::string::npos) ? filename :
216 filename.substr(last_slash + 1);
217 std::string metadata_function_name = "metadata_" + name + "_tmp";
218 std::ranges::replace(metadata_function_name, '.', '_');
219
220 metadata_file << metadata.serialize(metadata_function_name);
221 if (is_info) {
222 infos_file << metadata.serialize_infos();
223 }
224 if (!metadata.visited_files.empty()) {
225 visited_files = metadata.visited_files;
226 }
227
228 /* Escape path according to the depfile syntax. */
229 auto escape_path = [](std::string filepath) {
230 size_t pos = 0;
231 while ((pos = filepath.find(' ', pos)) != std::string::npos) {
232 filepath.replace(pos, 1, "\\ ");
233 pos += 2;
234 }
235 return filepath;
236 };
237
238 dep_file << output_file_name << " : ";
239 for (const auto &file : visited_files) {
240 dep_file << escape_path(file) << " ";
241 }
242 dep_file << "\n";
243
244 input_file.close();
245 output_file.close();
246 metadata_file.close();
247 infos_file.close();
248 dep_file.close();
249
250 if (error) {
251 std::cerr << error.value().full_report << std::endl;
252 }
253
254 return error.has_value() ? 1 : 0;
255}
metadata::Source parse_include_and_symbols()
Definition processor.cc:416
Result convert(metadata::Source external_sources_symbols={})
Definition processor.cc:387
#define main()
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
static metadata::Source scan_external_symbols(const std::vector< std::string > &file_list, std::vector< std::string > &visited_files, const std::string &file_buffer, const std::string &file_name)
static std::vector< std::string > list_files(const std::string &dir)
std::vector< TemplateDefinition > template_definitions
Definition metadata.hh:288
std::vector< Symbol > symbol_table
Definition metadata.hh:287
std::vector< std::string > dependencies
Definition metadata.hh:274