Skip to content

Commit

Permalink
VideoCommon: show loaded stats for the asset loader if turned on in c…
Browse files Browse the repository at this point in the history
…onfiguration
  • Loading branch information
iwubcode committed Mar 29, 2023
1 parent fd1b789 commit e0ceb11
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Source/Core/Core/Config/GraphicsSettings.cpp
Expand Up @@ -41,6 +41,8 @@ const Info<bool> GFX_LOG_RENDER_TIME_TO_FILE{{System::GFX, "Settings", "LogRende
const Info<bool> GFX_OVERLAY_STATS{{System::GFX, "Settings", "OverlayStats"}, false};
const Info<bool> GFX_OVERLAY_PROJ_STATS{{System::GFX, "Settings", "OverlayProjStats"}, false};
const Info<bool> GFX_OVERLAY_SCISSOR_STATS{{System::GFX, "Settings", "OverlayScissorStats"}, false};
const Info<bool> GFX_OVERLAY_ASSET_LOADER_STATS{
{System::GFX, "Settings", "OverlayAssetLoaderStats"}, false};
const Info<bool> GFX_DUMP_TEXTURES{{System::GFX, "Settings", "DumpTextures"}, false};
const Info<bool> GFX_DUMP_MIP_TEXTURES{{System::GFX, "Settings", "DumpMipTextures"}, true};
const Info<bool> GFX_DUMP_BASE_TEXTURES{{System::GFX, "Settings", "DumpBaseTextures"}, true};
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/Config/GraphicsSettings.h
Expand Up @@ -43,6 +43,7 @@ extern const Info<bool> GFX_LOG_RENDER_TIME_TO_FILE;
extern const Info<bool> GFX_OVERLAY_STATS;
extern const Info<bool> GFX_OVERLAY_PROJ_STATS;
extern const Info<bool> GFX_OVERLAY_SCISSOR_STATS;
extern const Info<bool> GFX_OVERLAY_ASSET_LOADER_STATS;
extern const Info<bool> GFX_DUMP_TEXTURES;
extern const Info<bool> GFX_DUMP_MIP_TEXTURES;
extern const Info<bool> GFX_DUMP_BASE_TEXTURES;
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinLib.props
Expand Up @@ -663,6 +663,7 @@
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\CustomAsset.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLibrary.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLoader.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLoaderStats.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\CustomTextureData.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\FBInfo.h" />
<ClInclude Include="VideoCommon\GraphicsModSystem\Runtime\GraphicsModAction.h" />
Expand Down Expand Up @@ -1269,6 +1270,7 @@
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\CustomAsset.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLibrary.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLoader.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\CustomAssetLoaderStats.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\CustomTextureData.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\FBInfo.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\GraphicsModActionFactory.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/VideoCommon/CMakeLists.txt
Expand Up @@ -71,6 +71,8 @@ add_library(videocommon
GraphicsModSystem/Runtime/CustomAssetLibrary.h
GraphicsModSystem/Runtime/CustomAssetLoader.cpp
GraphicsModSystem/Runtime/CustomAssetLoader.h
GraphicsModSystem/Runtime/CustomAssetLoaderStats.cpp
GraphicsModSystem/Runtime/CustomAssetLoaderStats.h
GraphicsModSystem/Runtime/CustomTextureData.cpp
GraphicsModSystem/Runtime/CustomTextureData.h
GraphicsModSystem/Runtime/FBInfo.cpp
Expand Down
67 changes: 51 additions & 16 deletions Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomAsset.cpp
Expand Up @@ -29,9 +29,9 @@ CustomAsset::CustomAsset(std::shared_ptr<CustomAssetLibrary> library,
{
}

bool CustomAsset::Load()
bool CustomAsset::Load(CustomAssetLoaderStats* stats)
{
const auto bytes_loaded = LoadImpl(m_asset_id);
const auto bytes_loaded = LoadImpl(m_asset_id, stats);
if (bytes_loaded > 0)
{
m_bytes_loaded = bytes_loaded;
Expand All @@ -40,6 +40,11 @@ bool CustomAsset::Load()
return bytes_loaded != 0;
}

void CustomAsset::Unload(CustomAssetLoaderStats* stats)
{
UnloadImpl(stats);
}

const std::optional<std::filesystem::file_time_type>& CustomAsset::GetLastWriteTime() const
{
return m_last_write_time;
Expand All @@ -60,24 +65,54 @@ std::size_t CustomAsset::GetBytesLoaded() const
return m_bytes_loaded;
}

std::size_t CustomTextureAsset::LoadImpl(const std::filesystem::path& asset_id)
std::size_t CustomTextureAsset::LoadImpl(const std::filesystem::path& asset_id,
CustomAssetLoaderStats* stats)
{
std::lock_guard lk(m_lock);
const bool result = m_owning_library->LoadTexture(asset_id, &m_data);
if (!result)
return 0;
m_loaded = true;
return GetAssetSize(m_data);
std::size_t result = 0;
{
std::lock_guard lk(m_lock);
if (!m_owning_library->LoadTexture(asset_id, &m_data))
return 0;
m_loaded = true;
result = GetAssetSize(m_data);
}
if (stats) [[likely]]
{
stats->AddTexture();
}
return result;
}

std::size_t CustomGameTextureAsset::LoadImpl(const std::filesystem::path& asset_id)
void CustomTextureAsset::UnloadImpl(CustomAssetLoaderStats* stats)
{
std::lock_guard lk(m_lock);
const bool result = m_owning_library->LoadGameTexture(asset_id, &m_data);
if (!result)
return 0;
m_loaded = true;
return GetAssetSize(m_data);
if (!stats) [[unlikely]]
return;
stats->RemoveGameTexture();
}

std::size_t CustomGameTextureAsset::LoadImpl(const std::filesystem::path& asset_id,
CustomAssetLoaderStats* stats)
{
std::size_t result = 0;
{
std::lock_guard lk(m_lock);
if (!m_owning_library->LoadGameTexture(asset_id, &m_data))
return 0;
m_loaded = true;
result = GetAssetSize(m_data);
}
if (stats) [[likely]]
{
stats->AddGameTexture();
}
return result;
}

void CustomGameTextureAsset::UnloadImpl(CustomAssetLoaderStats* stats)
{
if (!stats) [[unlikely]]
return;
stats->RemoveGameTexture();
}

bool CustomGameTextureAsset::Validate(u32 width, u32 height) const
Expand Down
16 changes: 12 additions & 4 deletions Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomAsset.h
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "Common/CommonTypes.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomAssetLoaderStats.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomTextureData.h"

#include <filesystem>
Expand All @@ -24,7 +25,8 @@ class CustomAsset
CustomAsset& operator=(const CustomAsset&) = default;
CustomAsset& operator=(CustomAsset&&) = default;

bool Load();
bool Load(CustomAssetLoaderStats* stats);
void Unload(CustomAssetLoaderStats* stats);
const std::optional<std::filesystem::file_time_type>& GetLastWriteTime() const;
const std::filesystem::path& GetAssetId() const;
const std::shared_ptr<CustomAssetLibrary>& GetOwningLibrary() const;
Expand All @@ -34,7 +36,9 @@ class CustomAsset
std::shared_ptr<CustomAssetLibrary> m_owning_library;

private:
virtual std::size_t LoadImpl(const std::filesystem::path& asset_id) = 0;
virtual std::size_t LoadImpl(const std::filesystem::path& asset_id,
CustomAssetLoaderStats* stats) = 0;
virtual void UnloadImpl(CustomAssetLoaderStats* stats) = 0;
std::filesystem::path m_asset_id;
std::size_t m_bytes_loaded = 0;
std::optional<std::filesystem::file_time_type> m_last_write_time = std::nullopt;
Expand Down Expand Up @@ -64,14 +68,18 @@ class CustomTextureAsset final : public CustomLoadableAsset<CustomTextureData>
{
public:
using CustomLoadableAsset::CustomLoadableAsset;
std::size_t LoadImpl(const std::filesystem::path& asset_id) override;
std::size_t LoadImpl(const std::filesystem::path& asset_id,
CustomAssetLoaderStats* stats) override;
void UnloadImpl(CustomAssetLoaderStats* stats) override;
};

class CustomGameTextureAsset final : public CustomLoadableAsset<CustomTextureData>
{
public:
using CustomLoadableAsset::CustomLoadableAsset;
std::size_t LoadImpl(const std::filesystem::path& asset_id) override;
std::size_t LoadImpl(const std::filesystem::path& asset_id,
CustomAssetLoaderStats* stats) override;
void UnloadImpl(CustomAssetLoaderStats* stats) override;

bool Validate(u32 width, u32 height) const;
};
Expand Down
Expand Up @@ -40,7 +40,8 @@ void CustomAssetLoader::Init()
if (write_time > file_to_monitor.m_cached_write_time)
{
file_to_monitor.m_cached_write_time = write_time;
(void)ptr->Load();
ptr->Unload(&m_stats);
(void)ptr->Load(&m_stats);
}
}
}
Expand All @@ -50,7 +51,7 @@ void CustomAssetLoader::Init()
m_asset_load_thread.Reset("Custom Asset Loader", [this](std::weak_ptr<CustomAsset> asset) {
if (auto ptr = asset.lock())
{
if (ptr->Load())
if (ptr->Load(&m_stats))
{
if (m_max_memory_available >= m_total_bytes_loaded + ptr->GetBytesLoaded())
{
Expand All @@ -65,6 +66,7 @@ void CustomAssetLoader::Init()
{
ERROR_LOG_FMT(VIDEO, "Failed to load asset {} because there was not enough memory.",
PathToString(ptr->GetAssetId()));
ptr->Unload(&m_stats);
}
}
}
Expand All @@ -79,6 +81,7 @@ void CustomAssetLoader ::Shutdown()
m_file_monitor_thread.join();
m_files_to_monitor.clear();
m_total_bytes_loaded = 0;
m_stats.Reset();
}

std::shared_ptr<CustomTextureAsset>
Expand All @@ -94,4 +97,9 @@ CustomAssetLoader::LoadGameTexture(const std::filesystem::path& texture_path,
{
return Load<CustomGameTextureAsset>(texture_path, m_game_textures, std::move(library));
}

const CustomAssetLoaderStats* CustomAssetLoader::GetStats() const
{
return &m_stats;
}
} // namespace VideoCommon
Expand Up @@ -12,6 +12,7 @@
#include "Common/Flag.h"
#include "Common/WorkQueueThread.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomAsset.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomAssetLoaderStats.h"

namespace VideoCommon
{
Expand All @@ -35,6 +36,8 @@ class CustomAssetLoader
LoadGameTexture(const std::filesystem::path& texture_path,
std::shared_ptr<CustomAssetLibrary> library);

const CustomAssetLoaderStats* GetStats() const;

private:
// TODO C++20: use a 'derived_from' concept when compilers catch up
template <typename AssetType>
Expand All @@ -49,6 +52,7 @@ class CustomAssetLoader
std::shared_ptr<AssetType> ptr(new AssetType(std::move(library), asset_path),
[&](AssetType* a) {
asset_map.erase(a->GetAssetId());
a->Unload(&m_stats);
m_total_bytes_loaded -= a->GetBytesLoaded();
std::lock_guard lk(m_files_lock);
m_files_to_monitor.erase(a->GetAssetId());
Expand Down Expand Up @@ -76,5 +80,7 @@ class CustomAssetLoader
std::map<std::filesystem::path, FileMonitorInfo> m_files_to_monitor;
std::mutex m_files_lock;
Common::WorkQueueThread<std::weak_ptr<CustomAsset>> m_asset_load_thread;

CustomAssetLoaderStats m_stats;
};
} // namespace VideoCommon
@@ -0,0 +1,70 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "VideoCommon/GraphicsModSystem/Runtime/CustomAssetLoaderStats.h"

#include <imgui.h>

namespace VideoCommon
{
void CustomAssetLoaderStats::Display() const
{
const float scale = ImGui::GetIO().DisplayFramebufferScale.x;
// TODO: This is the same position as the regular statistics text
ImGui::SetNextWindowPos(ImVec2(10.0f * scale, 10.0f * scale), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSizeConstraints(ImVec2(200.0f * scale, 100.0f * scale),
ImGui::GetIO().DisplaySize);
if (!ImGui::Begin("Asset Loader", nullptr, ImGuiWindowFlags_NoNavInputs))
{
ImGui::End();
return;
}

ImGui::Columns(2, "Asset Loader", true);

const auto draw_statistic = [](const char* name, const char* format, auto&&... args) {
ImGui::TextUnformatted(name);
ImGui::NextColumn();
ImGui::Text(format, std::forward<decltype(args)>(args)...);
ImGui::NextColumn();
};

{
std::lock_guard lk(m_lock);
draw_statistic("Textures", "%d", m_textures_loaded);
draw_statistic("Game Textures", "%d", m_game_textures_loaded);
}

ImGui::End();
}

void CustomAssetLoaderStats::AddTexture()
{
std::lock_guard lk(m_lock);
m_textures_loaded++;
}

void CustomAssetLoaderStats::RemoveTexture()
{
std::lock_guard lk(m_lock);
m_textures_loaded--;
}

void CustomAssetLoaderStats::AddGameTexture()
{
std::lock_guard lk(m_lock);
m_game_textures_loaded++;
}
void CustomAssetLoaderStats::RemoveGameTexture()
{
std::lock_guard lk(m_lock);
m_game_textures_loaded--;
}

void CustomAssetLoaderStats::Reset()
{
std::lock_guard lk(m_lock);
m_textures_loaded = 0;
m_game_textures_loaded = 0;
}
} // namespace VideoCommon
@@ -0,0 +1,30 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <mutex>

#include "Common/CommonTypes.h"

namespace VideoCommon
{
class CustomAssetLoaderStats
{
public:
void Display() const;

void AddTexture();
void RemoveTexture();

void AddGameTexture();
void RemoveGameTexture();

void Reset();

private:
mutable std::mutex m_lock;
u64 m_textures_loaded = 0;
u64 m_game_textures_loaded = 0;
};
} // namespace VideoCommon
12 changes: 12 additions & 0 deletions Source/Core/VideoCommon/OnScreenUI.cpp
Expand Up @@ -10,11 +10,13 @@
#include "Core/Config/MainSettings.h"
#include "Core/Config/NetplaySettings.h"
#include "Core/Movie.h"
#include "Core/System.h"

#include "VideoCommon/AbstractGfx.h"
#include "VideoCommon/AbstractPipeline.h"
#include "VideoCommon/AbstractShader.h"
#include "VideoCommon/FramebufferShaderGen.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomAssetLoader.h"
#include "VideoCommon/NetPlayChatUI.h"
#include "VideoCommon/NetPlayGolfUI.h"
#include "VideoCommon/OnScreenDisplay.h"
Expand Down Expand Up @@ -318,6 +320,16 @@ void OnScreenUI::DrawDebugText()
if (g_ActiveConfig.bOverlayScissorStats)
g_stats.DisplayScissor();

if (g_ActiveConfig.bOverlayAssetLoaderStats)
{
auto& system = Core::System::GetInstance();
const VideoCommon::CustomAssetLoaderStats* stats = system.GetCustomAssetLoader().GetStats();
if (stats)
{
stats->Display();
}
}

const std::string profile_output = Common::Profiler::ToString();
if (!profile_output.empty())
ImGui::TextUnformatted(profile_output.c_str());
Expand Down

0 comments on commit e0ceb11

Please sign in to comment.