55 const fs::path json_path = directory /
"library.json";
56 const fs::path text_path = directory /
"index.txt";
57 if (fs::is_regular_file(json_path)) {
58 manifest.
path = json_path;
61 cv::FileStorage storage(json_path.string(), cv::FileStorage::READ | cv::FileStorage::FORMAT_JSON);
62 if (!storage.isOpened()) {
63 throw std::runtime_error(
"unable to open shader manifest: " + json_path.string());
65 const cv::FileNode shader_entries = storage[
"shaders"];
66 if (shader_entries.type() == cv::FileNode::NONE || !shader_entries.isSeq()) {
67 throw std::runtime_error(json_path.string() +
" must contain a 'shaders' array");
69 for (
const cv::FileNode &entry : shader_entries) {
71 throw std::runtime_error(json_path.string() +
" contains too many shader entries");
74 if (entry.isString()) {
76 }
else if (entry.isMap() && !entry[
"file"].empty()) {
77 entry[
"file"] >> filename;
79 throw std::runtime_error(json_path.string() +
" contains a shader entry without a file name");
81 filename =
trim(std::move(filename));
82 if (filename.empty()) {
83 throw std::runtime_error(json_path.string() +
" contains a shader entry without a file name");
86 manifest.
entries.push_back(std::move(filename));
89 const cv::FileNode custom_uniforms = storage[
"custom_uniforms"];
90 if (!custom_uniforms.empty()) {
91 if (!custom_uniforms.isMap()) {
92 throw std::runtime_error(json_path.string() +
" field 'custom_uniforms' must be an object");
94 bool has_explicit_slots =
false;
95 bool has_implicit_slots =
false;
96 std::unordered_set<std::size_t> occupied_slots;
97 for (
auto iterator = custom_uniforms.begin(); iterator != custom_uniforms.end(); ++iterator) {
98 if (manifest.
custom_uniforms.size() >= mxvk::VK_Sprite::MAX_CUSTOM_UNIFORMS) {
99 throw std::runtime_error(json_path.string() +
" contains more than " + std::to_string(mxvk::VK_Sprite::MAX_CUSTOM_UNIFORMS) +
" custom uniforms");
102 const cv::FileNode entry = *iterator;
104 uniform.
name = entry.name();
106 throw std::runtime_error(json_path.string() +
" contains an invalid custom uniform: " + uniform.
name);
109 if (!entry[
"slot"].empty()) {
111 entry[
"slot"] >> slot;
112 if (slot < 0 || slot >=
static_cast<int>(mxvk::VK_Sprite::MAX_CUSTOM_UNIFORMS)) {
113 throw std::runtime_error(json_path.string() +
" contains an invalid slot for custom uniform: " + uniform.
name);
115 uniform.
slot =
static_cast<std::size_t
>(slot);
116 if (!occupied_slots.insert(uniform.
slot).second) {
117 throw std::runtime_error(json_path.string() +
" assigns more than one custom uniform to slot " + std::to_string(slot));
119 has_explicit_slots =
true;
121 has_implicit_slots =
true;
123 if (!entry[
"minimum"].empty()) {
124 entry[
"minimum"] >> uniform.
minimum;
126 if (!entry[
"maximum"].empty()) {
127 entry[
"maximum"] >> uniform.
maximum;
129 if (!entry[
"step"].empty()) {
130 entry[
"step"] >> uniform.
step;
133 if (!entry[
"value"].empty()) {
134 entry[
"value"] >> uniform.
value;
136 if (!std::isfinite(uniform.
minimum) || !std::isfinite(uniform.
maximum) || !std::isfinite(uniform.
step) || !std::isfinite(uniform.
value) || uniform.
maximum <= uniform.
minimum || uniform.
step <= 0.0 || std::abs(uniform.
minimum) > std::numeric_limits<float>::max() || std::abs(uniform.
maximum) > std::numeric_limits<float>::max() || std::abs(uniform.
step) > std::numeric_limits<float>::max() || std::abs(uniform.
value) > std::numeric_limits<float>::max()) {
137 throw std::runtime_error(json_path.string() +
" contains an invalid range for custom uniform: " + uniform.
name);
142 if (has_explicit_slots && has_implicit_slots) {
143 throw std::runtime_error(json_path.string() +
" must specify a slot for every custom uniform or none");
145 if (has_explicit_slots) {
147 for (std::size_t slot = 0; slot < manifest.
custom_uniforms.size(); ++slot) {
149 throw std::runtime_error(json_path.string() +
" custom uniform slots must be contiguous from zero");
154 }
catch (
const cv::Exception &error) {
155 throw std::runtime_error(
"unable to parse shader manifest " + json_path.string() +
": " + error.what());
160 if (!fs::is_regular_file(text_path)) {
161 throw std::runtime_error(
"no library.json or index.txt found in shader library: " + directory.string());
163 manifest.
path = text_path;
165 std::ifstream manifest_input(text_path);
166 if (!manifest_input) {
167 throw std::runtime_error(
"unable to open shader manifest: " + text_path.string());
170 std::size_t line_number = 1;
172 line =
trim(std::move(line));
173 if (!line.empty() && line.front() !=
'#') {
175 throw std::runtime_error(text_path.string() +
" contains too many shader entries");
178 manifest.
entries.push_back(std::move(line));
214 std::replace(entry.begin(), entry.end(),
'\\',
'/');
215 const fs::path relative_path(entry);
216 if (relative_path.is_absolute()) {
220 const fs::path normalized = relative_path.lexically_normal();
221 const std::string normalized_text = normalized.generic_string();
222 const std::string extension = normalized.extension().string();
223 if (normalized_text.empty() || normalized_text ==
"." || normalized_text ==
".." || normalized_text.starts_with(
"../") || normalized_text.find(
"/../") != std::string::npos || (extension !=
".frag" && extension !=
".comp" && extension !=
".spv")) {
227 std::error_code error;
228 const fs::path root = fs::weakly_canonical(directory, error);
232 const fs::path source = fs::weakly_canonical(root / normalized, error);
233 if (error || !fs::is_regular_file(source)) {
236 const std::string resolved_relative = source.lexically_relative(root).generic_string();
237 if (resolved_relative.empty() || resolved_relative ==
".." || resolved_relative.starts_with(
"../")) {
375 void runGlslc(
const std::string &executable,
const fs::path &source_root,
const fs::path &source,
const fs::path &output) {
376#if defined(__linux__) || defined(__APPLE__)
377 std::vector<std::string> arguments{executable,
"-I", source_root.string(), source.string(),
"-o", output.string()};
378 std::vector<char *> argument_pointers;
379 argument_pointers.reserve(arguments.size() + 1U);
380 for (std::string &argument : arguments) {
381 argument_pointers.push_back(argument.data());
383 argument_pointers.push_back(
nullptr);
386 const int spawn_result = posix_spawnp(&process, executable.c_str(),
nullptr,
nullptr, argument_pointers.data(),
environ);
387 if (spawn_result != 0) {
388 throw std::runtime_error(
"unable to execute glslc '" + executable +
"': " + std::strerror(spawn_result));
392 while (::waitpid(process, &status, 0) < 0) {
393 if (errno != EINTR) {
394 throw std::runtime_error(
"unable to wait for glslc: " + std::string(std::strerror(errno)));
397 if (!WIFEXITED(status)) {
398 throw std::runtime_error(
"glslc terminated by a signal for " + source.string());
400 if (WEXITSTATUS(status) != 0) {
401 throw ShaderCompilationError(
"glslc failed for " + source.string() +
" (exit status " + std::to_string(WEXITSTATUS(status)) +
")");
404 const DWORD result = run_windows_process({utf8_to_wide(executable), L
"-I", source_root.wstring(), source.wstring(), L
"-o", output.wstring()});
406 throw ShaderCompilationError(
"glslc failed for " + source.string() +
" (exit status " + std::to_string(result) +
")");
409#error Unsupported platform
414 const fs::path requested_manifest = fs::absolute(options.
build_manifest).lexically_normal();
415 if (requested_manifest.filename() !=
"library.json") {
416 throw std::runtime_error(
"--build must name a file called library.json");
420 std::error_code error;
421 const fs::path source_root = fs::weakly_canonical(requested_manifest.parent_path(), error);
422 if (error || source_root.empty()) {
423 throw std::runtime_error(
"unable to resolve source shader library: " + requested_manifest.string());
427 throw std::runtime_error(
"unable to create shader build directory: " + error.message());
429 const fs::path output_root = fs::weakly_canonical(options.
build_directory, error);
430 if (error || output_root.empty()) {
431 throw std::runtime_error(
"unable to resolve shader build directory: " + options.
build_directory);
433 if (source_root == output_root) {
434 throw std::runtime_error(
"the shader output directory must differ from the source "
435 "library directory");
439 if (manifest.
entries.empty()) {
440 throw std::runtime_error(
"source library.json contains no shader entries");
443 struct PreparedEntry {
445 std::string output_entry;
449 std::vector<PreparedEntry> prepared_entries(manifest.
entries.size());
450 std::vector<std::string> output_entries_by_index(manifest.
entries.size());
451 std::unordered_set<std::string> unique_outputs;
452 std::atomic<std::size_t> compiled{0};
453 std::atomic<std::size_t> copied{0};
454 std::atomic<std::size_t> current{0};
455 std::atomic<std::size_t> failed{0};
456 std::atomic<std::size_t> pruned{0};
457 std::atomic<std::size_t> processed{0};
458 int next_progress = 5;
459 std::mutex progress_mutex;
460 std::mutex error_mutex;
461 std::mutex failure_mutex;
462 std::exception_ptr first_failure;
464 const auto report_progress = [&] {
465 const std::size_t completed = processed.fetch_add(1U) + 1U;
466 const int percentage =
static_cast<int>(completed * 100U / manifest.
entries.size());
467 const std::lock_guard lock(progress_mutex);
468 while (next_progress <= 100 && percentage >= next_progress) {
469 std::cout <<
"acmxvk: build progress: " << next_progress <<
"% (" << completed <<
'/' << manifest.
entries.size() <<
")\n" << std::flush;
474 const auto store_failure = [&](std::exception_ptr failure) {
475 const std::lock_guard lock(failure_mutex);
476 if (!first_failure) {
477 first_failure = std::move(failure);
481 for (std::size_t index = 0; index < manifest.
entries.size(); ++index) {
482 const std::string &entry = manifest.
entries[index];
484 std::string normalized_entry = entry;
485 std::replace(normalized_entry.begin(), normalized_entry.end(),
'\\',
'/');
486 fs::path relative(normalized_entry);
487 relative = relative.lexically_normal();
488 if (relative.extension() !=
".spv") {
491 const std::string output_entry = relative.generic_string();
492 std::string output_key = output_entry;
493 std::transform(output_key.begin(), output_key.end(), output_key.begin(), [](
unsigned char character) { return static_cast<char>(std::tolower(character)); });
494 if (!unique_outputs.insert(output_key).second) {
495 throw std::runtime_error(
"source library produces a duplicate output path: " + output_entry);
497 prepared_entries[index] = PreparedEntry{relative, output_entry,
true};
498 }
catch (
const std::exception &failure_value) {
503 std::cerr <<
"acmxvk: fix omitted '" << entry <<
"': " << failure_value.what() <<
'\n';
508 const auto process_entry = [&](std::size_t index) {
509 const PreparedEntry &prepared = prepared_entries[index];
510 if (!prepared.ready) {
513 const std::string &entry = manifest.
entries[index];
515 fs::path destination;
518 if (source.empty()) {
519 throw std::runtime_error(
"source library contains an unavailable or unsafe shader: " + entry);
522 destination = output_root / prepared.relative;
523 std::error_code entry_error;
524 fs::create_directories(destination.parent_path(), entry_error);
526 throw std::runtime_error(
"unable to create shader output directory: " + entry_error.message());
528 const fs::path destination_parent = fs::weakly_canonical(destination.parent_path(), entry_error);
529 const std::string parent_relative = entry_error ? std::string{} : destination_parent.lexically_relative(output_root).generic_string();
530 if (entry_error || parent_relative ==
".." || parent_relative.starts_with(
"../") || fs::is_symlink(destination)) {
531 throw std::runtime_error(
"shader output resolves outside the output directory: " + prepared.output_entry);
534 bool needs_build = !fs::is_regular_file(destination);
536 needs_build = fs::last_write_time(destination, entry_error) < fs::last_write_time(source);
545 }
catch (
const std::runtime_error &) {
552 const bool copy_source = source.extension() ==
".spv";
556 fs::copy_file(source, temporary, fs::copy_options::overwrite_existing);
564 fs::remove(temporary);
575 output_entries_by_index[index] = prepared.output_entry;
576 }
catch (
const std::exception &failure_value) {
578 store_failure(std::current_exception());
582 if (!destination.empty()) {
583 std::error_code remove_error;
584 fs::remove(destination, remove_error);
586 throw std::runtime_error(
"unable to remove failed shader output " + destination.string() +
": " + remove_error.message());
589 if (options.
build_prune && compilation_failed && !source.empty() && (source.extension() ==
".frag" || source.extension() ==
".comp")) {
590 std::error_code remove_error;
591 const bool removed = fs::remove(source, remove_error);
592 if (remove_error || !removed) {
593 throw std::runtime_error(
"unable to prune failed shader source " + source.string() + (remove_error ?
": " + remove_error.message() :
": file was not removed"));
596 const std::lock_guard lock(error_mutex);
597 std::cerr <<
"acmxvk: pruned failed source '" << source.string() <<
"'\n";
600 const std::lock_guard lock(error_mutex);
601 std::cerr <<
"acmxvk: fix omitted '" << entry <<
"': " << failure_value.what() <<
'\n';
603 store_failure(std::current_exception());
610 const std::size_t worker_count = std::min(
static_cast<std::size_t
>(options.
build_parallel), manifest.
entries.size());
611 if (worker_count > 1U) {
612 std::cout <<
"acmxvk: building shader library with " << worker_count <<
" parallel jobs\n";
614 std::atomic<std::size_t> next_entry{0};
615 const auto worker = [&] {
617 const std::size_t index = next_entry.fetch_add(1U);
618 if (index >= manifest.
entries.size()) {
621 process_entry(index);
624 std::vector<std::thread> workers;
625 workers.reserve(worker_count);
626 for (std::size_t index = 0; index < worker_count; ++index) {
627 workers.emplace_back(worker);
629 for (std::thread &thread : workers) {
633 std::rethrow_exception(first_failure);
636 std::vector<std::string> output_entries;
637 output_entries.reserve(manifest.
entries.size());
638 for (std::string &entry : output_entries_by_index) {
639 if (!entry.empty()) {
640 output_entries.push_back(std::move(entry));
644 const fs::path output_manifest = output_root /
"library.json";
645 if (fs::is_symlink(output_manifest)) {
646 throw std::runtime_error(
"refusing to replace a symbolic-link output library.json");
650 std::ofstream output(temporary_manifest, std::ios::out | std::ios::trunc);
652 throw std::runtime_error(
"unable to create output library.json");
654 output <<
"{\n \"version\": 1"
655 <<
",\n \"backend\": \"acmxvk\""
656 <<
",\n \"library_type\": \"runtime\"";
658 output <<
",\n \"custom_uniforms\": {\n";
659 for (std::size_t index = 0; index < manifest.
custom_uniforms.size(); ++index) {
661 output <<
" \"" <<
escapeJson(uniform.
name) <<
"\": {\n" << std::setprecision(15) <<
" \"slot\": " << uniform.
slot <<
",\n \"minimum\": " << uniform.
minimum <<
",\n \"maximum\": " << uniform.
maximum <<
",\n \"step\": " << uniform.
step <<
",\n \"value\": " << uniform.
value <<
"\n }";
662 output << (index + 1U < manifest.
custom_uniforms.size() ?
",\n" :
"\n");
666 output <<
",\n \"shaders\": [\n";
667 for (std::size_t index = 0; index < output_entries.size(); ++index) {
668 output <<
" \"" <<
escapeJson(output_entries[index]) <<
'"' << (index + 1U < output_entries.size() ?
",\n" :
"\n");
672 fs::remove(temporary_manifest);
673 throw std::runtime_error(
"unable to write output library.json");
680 fs::remove(temporary_manifest);
684 std::cout <<
"acmxvk: shader library built in " << output_root <<
'\n' <<
"acmxvk: " << compiled <<
" compiled, " << copied <<
" copied, " << current <<
" up to date, " << failed <<
" failed, " << pruned <<
" pruned, " << output_entries.size() <<
" included\n";