Skip to content

Commit

Permalink
shader_manager: Use multithreading to load the replacement shader fil…
Browse files Browse the repository at this point in the history
…es concurrently (#41)
  • Loading branch information
Ishan09811 authored Dec 29, 2024
1 parent 55efa7d commit 4fc489c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 18 additions & 11 deletions app/src/main/cpp/skyline/gpu/shader_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}})};

// 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()));
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)};

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
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/skyline/gpu/shader_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4fc489c

Please sign in to comment.