0
0
mirror of https://github.com/Ishan09811/pine.git synced 2025-04-24 08:55:10 +00:00

shader_manager: Use multithreading to load the replacement shader files concurrently (#41)

This commit is contained in:
Ishan09811 2024-12-29 12:00:28 +05:30 committed by GitHub
parent 55efa7dc96
commit 4fc489c62a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 10 deletions

View File

@ -33,21 +33,28 @@ namespace skyline::gpu {
void ShaderManager::LoadShaderReplacements(std::string_view replacementDir) {
std::filesystem::path replacementDirPath{replacementDir};
if (std::filesystem::exists(replacementDirPath)) {
std::vector<std::future<void>> tasks;
for (const auto &entry : std::filesystem::directory_iterator{replacementDirPath}) {
if (entry.is_regular_file()) {
// Parse hash from filename
auto path{entry.path()};
auto &replacementMap{path.extension().string() == ".spv" ? hostShaderReplacements : guestShaderReplacements};
u64 hash{std::stoull(path.stem().string(), nullptr, 16)};
auto it{replacementMap.insert({hash, {}})};
tasks.emplace_back(std::async(std::launch::async, [this, path = entry.path()]() {
// Parse hash from filename
auto &replacementMap{path.extension().string() == ".spv" ? hostShaderReplacements : guestShaderReplacements};
u64 hash{std::stoull(path.stem().string(), nullptr, 16)};
// Read file into map entry
std::ifstream file{entry.path(), std::ios::binary | std::ios::ate};
it.first->second.resize(static_cast<size_t>(file.tellg()));
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char *>(it.first->second.data()), static_cast<std::streamsize>(it.first->second.size()));
std::ifstream file{path, std::ios::binary | std::ios::ate};
std::vector<u8> data(static_cast<size_t>(file.tellg()));
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char *>(data.data()), static_cast<std::streamsize>(data.size()));
std::scoped_lock lock{replacementMapMutex};
replacementMap[hash] = std::move(data);
}));
}
}
for (auto &task : tasks) {
task.get(); // Wait for all tasks to complete
}
}
}

View File

@ -34,6 +34,7 @@ namespace skyline::gpu {
std::mutex poolMutex;
std::filesystem::path dumpPath;
std::mutex dumpMutex;
std::mutex replacementMapMutex;
/**
* @brief Called at init time to populate the shader replacements map from the input directory