| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/ShaderCompileUtils.h" |
| 5 | |
| 6 | #include <ranges> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "Common/FileUtil.h" |
| 10 | #include "Common/StringUtil.h" |
| 11 | |
| 12 | namespace VideoCommon |
| 13 | { |
| 14 | ShaderIncluder::ShaderIncluder(std::string user_path, std::string system_path) |
| 15 | : m_root_user_path(std::move(user_path)), m_root_system_path(std::move(system_path)) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | std::vector<std::string> ShaderIncluder::GetIncludes() const |
| 20 | { |
| 21 | const auto keys = std::views::keys(m_include_results); |
| 22 | return {keys.begin(), keys.end()}; |
| 23 | } |
| 24 | |
| 25 | ShaderIncluder::IncludeResult* |
| 26 | ShaderIncluder::includeLocal(const char* header_name, const char* includer_name, std::size_t depth) |
| 27 | { |
| 28 | return ProcessInclude(m_root_user_path, header_name, includer_name, depth); |
| 29 | } |
| 30 | |
| 31 | ShaderIncluder::IncludeResult* |
| 32 | ShaderIncluder::includeSystem(const char* header_name, const char* includer_name, std::size_t depth) |
| 33 | { |
| 34 | return ProcessInclude(m_root_system_path, header_name, includer_name, depth); |
| 35 | } |
| 36 | |
| 37 | void ShaderIncluder::releaseInclude(IncludeResult* result) |
| 38 | { |
| 39 | m_include_results.erase(result->headerName); |
| 40 | } |
| 41 | |
| 42 | ShaderIncluder::IncludeResult* ShaderIncluder::ProcessInclude(const std::string& root, |
| 43 | const char* header_name, |
| 44 | const char* includer_name, |
| 45 | std::size_t depth) |
| 46 | { |
| 47 | m_dirs.resize(depth); |
| 48 | if (depth == 1) |
| 49 | { |
| 50 | const auto includer_dir = GetDirectory(includer_name); |
| 51 | if (includer_dir == ".") |
| 52 | { |
| 53 | m_dirs.push_back(root); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | m_dirs.push_back(std::string{includer_dir}); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Search through directories back to front |
| 62 | for (auto iter = m_dirs.rbegin(); iter != m_dirs.rend(); iter++) |
| 63 | { |
| 64 | const std::string full_path = WithUnifiedPathSeparators(*iter + '/' + header_name); |
| 65 | |
| 66 | std::string file_data; |
| 67 | if (File::ReadFileToString(full_path, file_data)) |
| 68 | { |
| 69 | m_dirs.push_back(std::string{GetDirectory(full_path)}); |
| 70 | |
| 71 | const char* file_bytes = file_data.data(); |
| 72 | const std::size_t file_size = file_data.size(); |
| 73 | IncludeResultData result_data{ |
| 74 | .result = std::make_unique<IncludeResult>(full_path, file_bytes, file_size, nullptr), |
| 75 | .file_data = std::move(file_data)}; |
| 76 | |
| 77 | auto ptr = result_data.result.get(); |
| 78 | m_include_results[full_path] = std::move(result_data); |
| 79 | return ptr; |
| 80 | } |
| 81 | } |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | std::string_view ShaderIncluder::GetDirectory(std::string_view path) const |
| 86 | { |
| 87 | const auto last_pos = path.find_last_of('/'); |
| 88 | if (last_pos == std::string_view::npos) |
| 89 | return "."; |
| 90 | |
| 91 | return path.substr(0, last_pos); |
| 92 | } |
| 93 | } // namespace VideoCommon |