Skip to content

Commit

Permalink
VideoCommon: introduce linked assets in TCacheEntry, allowing for ass…
Browse files Browse the repository at this point in the history
…ets to be reloaded
  • Loading branch information
iwubcode committed Jun 5, 2023
1 parent f1f1ecc commit 4ed67c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
35 changes: 33 additions & 2 deletions Source/Core/VideoCommon/TextureCacheBase.cpp
Expand Up @@ -272,6 +272,15 @@ void TextureCacheBase::SetBackupConfig(const VideoConfig& config)
config.graphics_mod_config ? config.graphics_mod_config->GetChangeCount() : 0;
}

bool TextureCacheBase::DidLinkedAssetsChange(const TCacheEntry& entry)
{
if (!entry.linked_asset.m_asset)
return false;

const auto last_asset_write_time = entry.linked_asset.m_asset->GetLastLoadedTime();
return last_asset_write_time > entry.linked_asset.m_last_write_time;
}

RcTcacheEntry TextureCacheBase::ApplyPaletteToEntry(RcTcacheEntry& entry, const u8* palette,
TLUTFormat tlutfmt)
{
Expand Down Expand Up @@ -1271,9 +1280,26 @@ class ArbitraryMipmapDetector
};

TCacheEntry* TextureCacheBase::Load(const TextureInfo& texture_info)
{
if (auto entry = LoadImpl(texture_info, false))
{
if (!DidLinkedAssetsChange(*entry))
{
return entry;
}

InvalidateTexture(GetTexCacheIter(entry));
return LoadImpl(texture_info, true);
}

return nullptr;
}

TCacheEntry* TextureCacheBase::LoadImpl(const TextureInfo& texture_info, bool force_reload)
{
// if this stage was not invalidated by changes to texture registers, keep the current texture
if (TMEM::IsValid(texture_info.GetStage()) && bound_textures[texture_info.GetStage()])
if (TMEM::IsValid(texture_info.GetStage()) && bound_textures[texture_info.GetStage()] &&
!force_reload)
{
TCacheEntry* entry = bound_textures[texture_info.GetStage()].get();
// If the TMEM configuration is such that this texture is more or less guaranteed to still
Expand Down Expand Up @@ -1582,6 +1608,7 @@ RcTcacheEntry TextureCacheBase::GetTexture(const int textureCacheSafetyColorSamp
InvalidateTexture(oldest_entry);
}

CachedTextureAsset cached_texture_asset;
std::shared_ptr<VideoCommon::CustomTextureData> data = nullptr;
bool has_arbitrary_mipmaps = false;
std::shared_ptr<HiresTexture> hires_texture;
Expand All @@ -1591,6 +1618,8 @@ RcTcacheEntry TextureCacheBase::GetTexture(const int textureCacheSafetyColorSamp
if (hires_texture)
{
data = hires_texture->GetAsset()->GetData();
cached_texture_asset = {hires_texture->GetAsset(),
hires_texture->GetAsset()->GetLastLoadedTime()};
has_arbitrary_mipmaps = hires_texture->HasArbitraryMipmaps();
if (data)
{
Expand All @@ -1603,9 +1632,11 @@ RcTcacheEntry TextureCacheBase::GetTexture(const int textureCacheSafetyColorSamp
}
}

return CreateTextureEntry(
auto entry = CreateTextureEntry(
TextureCreationInfo{base_hash, full_hash, bytes_per_block, palette_size}, texture_info,
textureCacheSafetyColorSampleSize, data.get(), has_arbitrary_mipmaps);
entry->linked_asset = std::move(cached_texture_asset);
return entry;
}

RcTcacheEntry TextureCacheBase::CreateTextureEntry(
Expand Down
16 changes: 15 additions & 1 deletion Source/Core/VideoCommon/TextureCacheBase.h
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <array>
#include <filesystem>
#include <fmt/format.h>
#include <map>
#include <memory>
Expand Down Expand Up @@ -35,7 +36,8 @@ struct VideoConfig;
namespace VideoCommon
{
class CustomTextureData;
}
class GameTextureAsset;
} // namespace VideoCommon

constexpr std::string_view EFB_DUMP_PREFIX = "efb1";
constexpr std::string_view XFB_DUMP_PREFIX = "xfb1";
Expand Down Expand Up @@ -113,6 +115,12 @@ struct fmt::formatter<EFBCopyParams>
}
};

struct CachedTextureAsset
{
std::shared_ptr<VideoCommon::GameTextureAsset> m_asset;
std::optional<std::filesystem::file_time_type> m_last_write_time;
};

struct TCacheEntry
{
// common members
Expand Down Expand Up @@ -164,6 +172,8 @@ struct TCacheEntry

std::string texture_info_name = "";

CachedTextureAsset linked_asset;

explicit TCacheEntry(std::unique_ptr<AbstractTexture> tex,
std::unique_ptr<AbstractFramebuffer> fb);

Expand Down Expand Up @@ -337,6 +347,10 @@ class TextureCacheBase

using TexPool = std::unordered_multimap<TextureConfig, TexPoolEntry>;

static bool DidLinkedAssetsChange(const TCacheEntry& entry);

TCacheEntry* LoadImpl(const TextureInfo& texture_info, bool force_reload);

bool CreateUtilityTextures();

void SetBackupConfig(const VideoConfig& config);
Expand Down

0 comments on commit 4ed67c4

Please sign in to comment.