From d7de49ccf68aae6b3c12e35122435f30ea2f8533 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Thu, 1 May 2025 22:14:00 -0500 Subject: Core / VideoCommon: Remove original custom asset loader --- .../Core/VideoCommon/Assets/CustomAssetLoader.cpp | 108 --------------------- 1 file changed, 108 deletions(-) delete mode 100644 Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp (limited to 'Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp b/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp deleted file mode 100644 index e70faa8359..0000000000 --- a/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2023 Dolphin Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "VideoCommon/Assets/CustomAssetLoader.h" - -#include "Common/MemoryUtil.h" -#include "VideoCommon/Assets/CustomAssetLibrary.h" - -namespace VideoCommon -{ -void CustomAssetLoader::Init() -{ - m_asset_monitor_thread_shutdown.Clear(); - - const size_t sys_mem = Common::MemPhysical(); - const size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024); - // keep 2GB memory for system stability if system RAM is 4GB+ - use half of memory in other cases - m_max_memory_available = - (sys_mem / 2 < recommended_min_mem) ? (sys_mem / 2) : (sys_mem - recommended_min_mem); - - m_asset_monitor_thread = std::thread([this]() { - Common::SetCurrentThreadName("Asset monitor"); - while (true) - { - if (m_asset_monitor_thread_shutdown.IsSet()) - { - break; - } - - std::this_thread::sleep_for(TIME_BETWEEN_ASSET_MONITOR_CHECKS); - - std::lock_guard lk(m_asset_load_lock); - for (auto& [asset_id, asset_to_monitor] : m_assets_to_monitor) - { - if (auto ptr = asset_to_monitor.lock()) - { - const auto write_time = ptr->GetLastWriteTime(); - if (write_time > ptr->GetLastLoadedTime()) - { - (void)ptr->Load(); - } - } - } - } - }); - - m_asset_load_thread.Reset("Custom Asset Loader", [this](std::weak_ptr asset) { - if (auto ptr = asset.lock()) - { - if (m_memory_exceeded) - return; - - if (ptr->Load()) - { - std::lock_guard lk(m_asset_load_lock); - const std::size_t asset_memory_size = ptr->GetByteSizeInMemory(); - m_total_bytes_loaded += asset_memory_size; - m_assets_to_monitor.try_emplace(ptr->GetAssetId(), ptr); - if (m_total_bytes_loaded > m_max_memory_available) - { - ERROR_LOG_FMT(VIDEO, - "Asset memory exceeded with asset '{}', future assets won't load until " - "memory is available.", - ptr->GetAssetId()); - m_memory_exceeded = true; - } - } - } - }); -} - -void CustomAssetLoader::Shutdown() -{ - m_asset_load_thread.StopAndCancel(); - - m_asset_monitor_thread_shutdown.Set(); - m_asset_monitor_thread.join(); - m_assets_to_monitor.clear(); - m_total_bytes_loaded = 0; -} - -std::shared_ptr -CustomAssetLoader::LoadGameTexture(const CustomAssetLibrary::AssetID& asset_id, - std::shared_ptr library) -{ - return LoadOrCreateAsset(asset_id, m_game_textures, std::move(library)); -} - -std::shared_ptr -CustomAssetLoader::LoadPixelShader(const CustomAssetLibrary::AssetID& asset_id, - std::shared_ptr library) -{ - return LoadOrCreateAsset(asset_id, m_pixel_shaders, std::move(library)); -} - -std::shared_ptr -CustomAssetLoader::LoadMaterial(const CustomAssetLibrary::AssetID& asset_id, - std::shared_ptr library) -{ - return LoadOrCreateAsset(asset_id, m_materials, std::move(library)); -} - -std::shared_ptr CustomAssetLoader::LoadMesh(const CustomAssetLibrary::AssetID& asset_id, - std::shared_ptr library) -{ - return LoadOrCreateAsset(asset_id, m_meshes, std::move(library)); -} -} // namespace VideoCommon -- cgit v1.2.3 From 70abcb2030e0f45f46c056f6cb017ff4de14f5a2 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sat, 1 Mar 2025 21:51:21 -0600 Subject: VideoCommon: add resource manager and new asset loader; the resource manager uses a least recently used cache to determine which assets get priority for loading. Additionally, if the system is low on memory, assets will be purged with the less requested assets being the first to go. The loader is multithreaded now and loads assets as quickly as possible as long as memory is available Co-authored-by: Jordan Woyak --- .../Core/VideoCommon/Assets/CustomAssetLoader.cpp | 157 +++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp (limited to 'Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp b/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp new file mode 100644 index 0000000000..2ca227546c --- /dev/null +++ b/Source/Core/VideoCommon/Assets/CustomAssetLoader.cpp @@ -0,0 +1,157 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "VideoCommon/Assets/CustomAssetLoader.h" + +#include + +#include "Common/Logging/Log.h" +#include "Common/Thread.h" + +#include "UICommon/UICommon.h" + +namespace VideoCommon +{ +void CustomAssetLoader::Initialize() +{ + ResizeWorkerThreads(2); +} + +void CustomAssetLoader::Shutdown() +{ + Reset(false); +} + +bool CustomAssetLoader::StartWorkerThreads(u32 num_worker_threads) +{ + for (u32 i = 0; i < num_worker_threads; i++) + { + m_worker_threads.emplace_back(&CustomAssetLoader::WorkerThreadRun, this, i); + } + + return HasWorkerThreads(); +} + +bool CustomAssetLoader::ResizeWorkerThreads(u32 num_worker_threads) +{ + if (m_worker_threads.size() == num_worker_threads) + return true; + + StopWorkerThreads(); + return StartWorkerThreads(num_worker_threads); +} + +bool CustomAssetLoader::HasWorkerThreads() const +{ + return !m_worker_threads.empty(); +} + +void CustomAssetLoader::StopWorkerThreads() +{ + if (!HasWorkerThreads()) + return; + + // Signal worker threads to stop, and wake all of them. + { + std::lock_guard guard(m_assets_to_load_lock); + m_exit_flag.Set(); + m_worker_thread_wake.notify_all(); + } + + // Wait for worker threads to exit. + for (std::thread& thr : m_worker_threads) + thr.join(); + m_worker_threads.clear(); + m_exit_flag.Clear(); +} + +void CustomAssetLoader::WorkerThreadRun(u32 thread_index) +{ + Common::SetCurrentThreadName(fmt::format("Asset Loader {}", thread_index).c_str()); + + std::unique_lock load_lock(m_assets_to_load_lock); + while (true) + { + m_worker_thread_wake.wait(load_lock, + [&] { return !m_assets_to_load.empty() || m_exit_flag.IsSet(); }); + + if (m_exit_flag.IsSet()) + return; + + // If more memory than allowed has already been loaded, we will load nothing more + // until the next ScheduleAssetsToLoad from Manager. + if (m_change_in_memory > m_allowed_memory) + { + m_assets_to_load.clear(); + continue; + } + + auto* const item = m_assets_to_load.front(); + m_assets_to_load.pop_front(); + + // Make sure another thread isn't loading this handle. + if (!m_handles_in_progress.insert(item->GetHandle()).second) + continue; + + load_lock.unlock(); + + // Unload previously loaded asset. + m_change_in_memory -= item->Unload(); + + const std::size_t bytes_loaded = item->Load(); + m_change_in_memory += s64(bytes_loaded); + + load_lock.lock(); + + { + INFO_LOG_FMT(VIDEO, "CustomAssetLoader thread {} loaded: {} ({})", thread_index, + item->GetAssetId(), UICommon::FormatSize(bytes_loaded)); + + std::lock_guard lk{m_assets_loaded_lock}; + m_asset_handles_loaded.emplace_back(item->GetHandle(), bytes_loaded > 0); + + // Make sure no other threads try to re-process this item. + // Manager will take the handles and re-ScheduleAssetsToLoad based on timestamps if needed. + std::erase(m_assets_to_load, item); + } + + m_handles_in_progress.erase(item->GetHandle()); + } +} + +auto CustomAssetLoader::TakeLoadResults() -> LoadResults +{ + std::lock_guard guard(m_assets_loaded_lock); + return {std::move(m_asset_handles_loaded), m_change_in_memory.exchange(0)}; +} + +void CustomAssetLoader::ScheduleAssetsToLoad(std::list assets_to_load, + u64 allowed_memory) +{ + if (assets_to_load.empty()) [[unlikely]] + return; + + // There's new assets to process, notify worker threads + std::lock_guard guard(m_assets_to_load_lock); + m_allowed_memory = allowed_memory; + m_assets_to_load = std::move(assets_to_load); + m_worker_thread_wake.notify_all(); +} + +void CustomAssetLoader::Reset(bool restart_worker_threads) +{ + const std::size_t worker_thread_count = m_worker_threads.size(); + StopWorkerThreads(); + + m_assets_to_load.clear(); + m_asset_handles_loaded.clear(); + m_allowed_memory = 0; + m_change_in_memory = 0; + + if (restart_worker_threads) + { + StartWorkerThreads(static_cast(worker_thread_count)); + } +} + +} // namespace VideoCommon -- cgit v1.2.3