From 9ec69b5925ea4230077e7f77795b7a0f7a05e963 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sat, 1 Mar 2025 22:16:09 -0600 Subject: VideoCommon: add a handle to custom asset, this is an id that is only relevant for a particular game session but is slightly faster as a numeric value for lookups than the traditional asset id --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index 1591f93f91..c06b184b7f 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -6,8 +6,8 @@ namespace VideoCommon { CustomAsset::CustomAsset(std::shared_ptr library, - const CustomAssetLibrary::AssetID& asset_id) - : m_owning_library(std::move(library)), m_asset_id(asset_id) + const CustomAssetLibrary::AssetID& asset_id, u64 asset_handle) + : m_owning_library(std::move(library)), m_asset_id(asset_id), m_handle(asset_handle) { } @@ -34,6 +34,11 @@ const CustomAssetLibrary::TimeType& CustomAsset::GetLastLoadedTime() const return m_last_loaded_time; } +std::size_t CustomAsset::GetHandle() const +{ + return m_handle; +} + const CustomAssetLibrary::AssetID& CustomAsset::GetAssetId() const { return m_asset_id; -- cgit v1.2.3 From 316740daede362a144b1a3756cf747e988ead0ab Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 4 May 2025 12:14:18 -0500 Subject: VideoCommon: add 'Unload' functionality to CustomAsset --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index c06b184b7f..5a9155d829 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -23,6 +23,18 @@ bool CustomAsset::Load() return load_information.m_bytes_loaded != 0; } +std::size_t CustomAsset::Unload() +{ + UnloadImpl(); + std::size_t bytes_loaded = 0; + { + std::lock_guard lk(m_info_lock); + bytes_loaded = m_bytes_loaded; + m_bytes_loaded = 0; + } + return bytes_loaded; +} + CustomAssetLibrary::TimeType CustomAsset::GetLastWriteTime() const { return m_owning_library->GetLastAssetWriteTime(m_asset_id); -- cgit v1.2.3 From 15f125ebeee950924c1317abbfdbc600d480ae78 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 6 Jun 2025 20:34:44 -0500 Subject: VideoCommon: change asset loading to return the number of bytes loaded instead of a pass/fail --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index 5a9155d829..2b41d60a21 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -11,7 +11,7 @@ CustomAsset::CustomAsset(std::shared_ptr library, { } -bool CustomAsset::Load() +std::size_t CustomAsset::Load() { const auto load_information = LoadImpl(m_asset_id); if (load_information.m_bytes_loaded > 0) @@ -19,8 +19,9 @@ bool CustomAsset::Load() std::lock_guard lk(m_info_lock); m_bytes_loaded = load_information.m_bytes_loaded; m_last_loaded_time = load_information.m_load_time; + return m_bytes_loaded; } - return load_information.m_bytes_loaded != 0; + return 0; } std::size_t CustomAsset::Unload() -- cgit v1.2.3 From bafe78203d4eb508c8b9e5d55923cde8d60b72ce Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 4 May 2025 17:50:14 -0500 Subject: VideoCommon: remove 'GetLastAssetWriteTime' and switch to a steady_clock for asset times --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index 2b41d60a21..0fc495fe44 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -18,7 +18,7 @@ std::size_t CustomAsset::Load() { std::lock_guard lk(m_info_lock); m_bytes_loaded = load_information.m_bytes_loaded; - m_last_loaded_time = load_information.m_load_time; + m_last_loaded_time = ClockType::now(); return m_bytes_loaded; } return 0; @@ -36,12 +36,7 @@ std::size_t CustomAsset::Unload() return bytes_loaded; } -CustomAssetLibrary::TimeType CustomAsset::GetLastWriteTime() const -{ - return m_owning_library->GetLastAssetWriteTime(m_asset_id); -} - -const CustomAssetLibrary::TimeType& CustomAsset::GetLastLoadedTime() const +const CustomAsset::TimeType& CustomAsset::GetLastLoadedTime() const { std::lock_guard lk(m_info_lock); return m_last_loaded_time; -- cgit v1.2.3 From 3b83907b88756f9d37ed39fa614efbcdc177e803 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 23 May 2025 23:27:11 -0500 Subject: VideoCommon: update CustomAsset's load time to be before the load occurs (this prevents issues where the load time might be incorrectly inflated by long load operations) Co-authored-by: Jordan Woyak --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index 0fc495fe44..60c7f1ee12 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -13,12 +13,17 @@ CustomAsset::CustomAsset(std::shared_ptr library, std::size_t CustomAsset::Load() { + // The load time needs to come from before the data is actually read. + // Using a time point from after the read marks the asset as more up-to-date than it actually is, + // and has potential to race (and not be updated) if a change happens immediately after load. + const auto load_time = ClockType::now(); + const auto load_information = LoadImpl(m_asset_id); if (load_information.m_bytes_loaded > 0) { std::lock_guard lk(m_info_lock); m_bytes_loaded = load_information.m_bytes_loaded; - m_last_loaded_time = ClockType::now(); + m_last_loaded_time = load_time; return m_bytes_loaded; } return 0; -- cgit v1.2.3 From b3f50c969eef54c07b28860be6c8e172e03daaed Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 6 Jun 2025 19:24:12 -0500 Subject: VideoCommon: rename m_bytes_loaded in asset library to bytes_loaded --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index 60c7f1ee12..ab82f03a95 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -19,10 +19,10 @@ std::size_t CustomAsset::Load() const auto load_time = ClockType::now(); const auto load_information = LoadImpl(m_asset_id); - if (load_information.m_bytes_loaded > 0) + if (load_information.bytes_loaded > 0) { std::lock_guard lk(m_info_lock); - m_bytes_loaded = load_information.m_bytes_loaded; + m_bytes_loaded = load_information.bytes_loaded; m_last_loaded_time = load_time; return m_bytes_loaded; } -- cgit v1.2.3 From 774a84a95371b73c1577414c58420d9afa6eadff Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 6 Jun 2025 19:55:03 -0500 Subject: VideoCommon: avoid race conditions with asset load/unload by moving the lock to the entire function, favor atomics for the memory/time getters --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index ab82f03a95..bc11fd81d8 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -13,6 +13,7 @@ CustomAsset::CustomAsset(std::shared_ptr library, std::size_t CustomAsset::Load() { + std::lock_guard lk(m_info_lock); // The load time needs to come from before the data is actually read. // Using a time point from after the read marks the asset as more up-to-date than it actually is, // and has potential to race (and not be updated) if a change happens immediately after load. @@ -21,7 +22,6 @@ std::size_t CustomAsset::Load() const auto load_information = LoadImpl(m_asset_id); if (load_information.bytes_loaded > 0) { - std::lock_guard lk(m_info_lock); m_bytes_loaded = load_information.bytes_loaded; m_last_loaded_time = load_time; return m_bytes_loaded; @@ -31,19 +31,13 @@ std::size_t CustomAsset::Load() std::size_t CustomAsset::Unload() { + std::lock_guard lk(m_info_lock); UnloadImpl(); - std::size_t bytes_loaded = 0; - { - std::lock_guard lk(m_info_lock); - bytes_loaded = m_bytes_loaded; - m_bytes_loaded = 0; - } - return bytes_loaded; + return m_bytes_loaded.exchange(0); } -const CustomAsset::TimeType& CustomAsset::GetLastLoadedTime() const +CustomAsset::TimeType CustomAsset::GetLastLoadedTime() const { - std::lock_guard lk(m_info_lock); return m_last_loaded_time; } @@ -59,7 +53,6 @@ const CustomAssetLibrary::AssetID& CustomAsset::GetAssetId() const std::size_t CustomAsset::GetByteSizeInMemory() const { - std::lock_guard lk(m_info_lock); return m_bytes_loaded; } -- cgit v1.2.3 From c3d3b8153388866117334cdc719889d1f676a16e Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 6 Jun 2025 20:43:31 -0500 Subject: VideoCommon: remove 'GetByteSizeInMemory()' from custom asset, it is not needed anymore --- Source/Core/VideoCommon/Assets/CustomAsset.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'Source/Core/VideoCommon/Assets/CustomAsset.cpp') diff --git a/Source/Core/VideoCommon/Assets/CustomAsset.cpp b/Source/Core/VideoCommon/Assets/CustomAsset.cpp index bc11fd81d8..7e2e817d15 100644 --- a/Source/Core/VideoCommon/Assets/CustomAsset.cpp +++ b/Source/Core/VideoCommon/Assets/CustomAsset.cpp @@ -3,6 +3,8 @@ #include "VideoCommon/Assets/CustomAsset.h" +#include + namespace VideoCommon { CustomAsset::CustomAsset(std::shared_ptr library, @@ -33,7 +35,7 @@ std::size_t CustomAsset::Unload() { std::lock_guard lk(m_info_lock); UnloadImpl(); - return m_bytes_loaded.exchange(0); + return std::exchange(m_bytes_loaded, 0); } CustomAsset::TimeType CustomAsset::GetLastLoadedTime() const @@ -51,9 +53,4 @@ const CustomAssetLibrary::AssetID& CustomAsset::GetAssetId() const return m_asset_id; } -std::size_t CustomAsset::GetByteSizeInMemory() const -{ - return m_bytes_loaded; -} - } // namespace VideoCommon -- cgit v1.2.3