TextureCacheBase: Eliminate static state

This commit is contained in:
Lioncash 2016-09-06 18:57:58 -04:00
parent 1fa61af413
commit 58a5395173
21 changed files with 141 additions and 163 deletions

View File

@ -304,7 +304,7 @@ void GFXDebuggerPanel::OnClearScreenButton(wxCommandEvent& event)
void GFXDebuggerPanel::OnClearTextureCacheButton(wxCommandEvent& event) void GFXDebuggerPanel::OnClearTextureCacheButton(wxCommandEvent& event)
{ {
TextureCacheBase::Invalidate(); g_texture_cache->Invalidate();
} }
void GFXDebuggerPanel::OnClearVertexShaderCacheButton(wxCommandEvent& event) void GFXDebuggerPanel::OnClearVertexShaderCacheButton(wxCommandEvent& event)

View File

@ -841,11 +841,11 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
OSD::DrawMessages(); OSD::DrawMessages();
D3D::EndFrame(); D3D::EndFrame();
TextureCacheBase::Cleanup(frameCount); g_texture_cache->Cleanup(frameCount);
// Enable configuration changes // Enable configuration changes
UpdateActiveConfig(); UpdateActiveConfig();
TextureCacheBase::OnConfigChanged(g_ActiveConfig); g_texture_cache->OnConfigChanged(g_ActiveConfig);
SetWindowSize(fbStride, fbHeight); SetWindowSize(fbStride, fbHeight);

View File

@ -127,12 +127,11 @@ void TextureCache::TCacheEntry::CopyRectangleFromTexture(const TCacheEntryBase*
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, void TextureCache::TCacheEntry::Load(const u8* buffer, u32 width, u32 height, u32 expanded_width,
unsigned int expanded_width, unsigned int level) u32 level)
{ {
unsigned int src_pitch = 4 * expanded_width; unsigned int src_pitch = 4 * expanded_width;
D3D::ReplaceRGBATexture2D(texture->GetTex(), TextureCache::temp, width, height, src_pitch, level, D3D::ReplaceRGBATexture2D(texture->GetTex(), buffer, width, height, src_pitch, level, usage);
usage);
} }
TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config) TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config)

View File

@ -32,8 +32,7 @@ private:
const MathUtil::Rectangle<int>& srcrect, const MathUtil::Rectangle<int>& srcrect,
const MathUtil::Rectangle<int>& dstrect) override; const MathUtil::Rectangle<int>& dstrect) override;
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 levels) override;
unsigned int levels) override;
void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
bool scaleByHalf, unsigned int cbufid, const float* colmat) override; bool scaleByHalf, unsigned int cbufid, const float* colmat) override;

View File

@ -794,11 +794,11 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height
OSD::DrawMessages(); OSD::DrawMessages();
D3D::EndFrame(); D3D::EndFrame();
TextureCacheBase::Cleanup(frameCount); g_texture_cache->Cleanup(frameCount);
// Enable configuration changes // Enable configuration changes
UpdateActiveConfig(); UpdateActiveConfig();
TextureCacheBase::OnConfigChanged(g_ActiveConfig); g_texture_cache->OnConfigChanged(g_ActiveConfig);
SetWindowSize(fb_stride, fb_height); SetWindowSize(fb_stride, fb_height);

View File

@ -172,12 +172,12 @@ void TextureCache::TCacheEntry::CopyRectangleFromTexture(const TCacheEntryBase*
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, void TextureCache::TCacheEntry::Load(const u8* buffer, u32 width, u32 height, u32 expanded_width,
unsigned int expanded_width, unsigned int level) u32 level)
{ {
unsigned int src_pitch = 4 * expanded_width; unsigned int src_pitch = 4 * expanded_width;
D3D::ReplaceRGBATexture2D(m_texture->GetTex12(), TextureCache::temp, width, height, src_pitch, D3D::ReplaceRGBATexture2D(m_texture->GetTex12(), buffer, width, height, src_pitch, level,
level, m_texture->GetResourceUsageState()); m_texture->GetResourceUsageState());
} }
TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config) TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config)

View File

@ -39,8 +39,7 @@ private:
const MathUtil::Rectangle<int>& src_rect, const MathUtil::Rectangle<int>& src_rect,
const MathUtil::Rectangle<int>& dst_rect) override; const MathUtil::Rectangle<int>& dst_rect) override;
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 levels) override;
unsigned int levels) override;
void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect, void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect,
bool scale_by_half, unsigned int cbuf_id, const float* colmat) override; bool scale_by_half, unsigned int cbuf_id, const float* colmat) override;

View File

@ -31,11 +31,7 @@ private:
{ {
TCacheEntry(const TCacheEntryConfig& _config) : TCacheEntryBase(_config) {} TCacheEntry(const TCacheEntryConfig& _config) : TCacheEntryBase(_config) {}
~TCacheEntry() {} ~TCacheEntry() {}
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 level) override {}
unsigned int level) override
{
}
void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect, void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect,
bool scale_by_half, unsigned int cbufid, const float* colmat) override bool scale_by_half, unsigned int cbufid, const float* colmat) override
{ {

View File

@ -1508,7 +1508,7 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
} }
// Clean out old stuff from caches. It's not worth it to clean out the shader caches. // Clean out old stuff from caches. It's not worth it to clean out the shader caches.
TextureCache::Cleanup(frameCount); g_texture_cache->Cleanup(frameCount);
// Render to the framebuffer. // Render to the framebuffer.
FramebufferManager::SetFramebuffer(0); FramebufferManager::SetFramebuffer(0);
@ -1518,7 +1518,7 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
g_Config.iSaveTargetId = 0; g_Config.iSaveTargetId = 0;
UpdateActiveConfig(); UpdateActiveConfig();
TextureCache::OnConfigChanged(g_ActiveConfig); g_texture_cache->OnConfigChanged(g_ActiveConfig);
// For testing zbuffer targets. // For testing zbuffer targets.
// Renderer::SetZBufferRender(); // Renderer::SetZBufferRender();

View File

@ -178,8 +178,8 @@ void TextureCache::TCacheEntry::CopyRectangleFromTexture(const TCacheEntryBase*
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, void TextureCache::TCacheEntry::Load(const u8* buffer, u32 width, u32 height, u32 expanded_width,
unsigned int expanded_width, unsigned int level) u32 level)
{ {
if (level >= config.levels) if (level >= config.levels)
PanicAlert("Texture only has %d levels, can't update level %d", config.levels, level); PanicAlert("Texture only has %d levels, can't update level %d", config.levels, level);
@ -196,7 +196,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width); glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width);
glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, width, height, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, width, height, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
temp); buffer);
if (expanded_width != width) if (expanded_width != width)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);

View File

@ -39,8 +39,7 @@ private:
const MathUtil::Rectangle<int>& srcrect, const MathUtil::Rectangle<int>& srcrect,
const MathUtil::Rectangle<int>& dstrect) override; const MathUtil::Rectangle<int>& dstrect) override;
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 level) override;
unsigned int level) override;
void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
bool scaleByHalf, unsigned int cbufid, const float* colmat) override; bool scaleByHalf, unsigned int cbufid, const float* colmat) override;

View File

@ -64,11 +64,7 @@ private:
{ {
TCacheEntry(const TCacheEntryConfig& _config) : TCacheEntryBase(_config) {} TCacheEntry(const TCacheEntryConfig& _config) : TCacheEntryBase(_config) {}
~TCacheEntry() {} ~TCacheEntry() {}
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 level) override {}
unsigned int level) override
{
}
void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
bool scaleByHalf, unsigned int cbufid, const float* colmat) override bool scaleByHalf, unsigned int cbufid, const float* colmat) override
{ {

View File

@ -559,7 +559,7 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height
CheckForTargetResize(fb_width, fb_stride, fb_height); CheckForTargetResize(fb_width, fb_stride, fb_height);
// Clean up stale textures. // Clean up stale textures.
TextureCacheBase::Cleanup(frameCount); TextureCache::GetInstance()->Cleanup(frameCount);
} }
void Renderer::DrawFrame(VkRenderPass render_pass, const TargetRectangle& target_rect, void Renderer::DrawFrame(VkRenderPass render_pass, const TargetRectangle& target_rect,
@ -1112,7 +1112,7 @@ void Renderer::CheckForConfigChanges()
bool aspect_changed = old_aspect_ratio != g_ActiveConfig.iAspectRatio; bool aspect_changed = old_aspect_ratio != g_ActiveConfig.iAspectRatio;
// Update texture cache settings with any changed options. // Update texture cache settings with any changed options.
TextureCache::OnConfigChanged(g_ActiveConfig); TextureCache::GetInstance()->OnConfigChanged(g_ActiveConfig);
// Handle internal resolution changes. // Handle internal resolution changes.
if (efb_scale_changed) if (efb_scale_changed)

View File

@ -328,7 +328,7 @@ TextureCache::TCacheEntry::~TCacheEntry()
g_command_buffer_mgr->DeferFramebufferDestruction(m_framebuffer); g_command_buffer_mgr->DeferFramebufferDestruction(m_framebuffer);
} }
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, void TextureCache::TCacheEntry::Load(const u8* buffer, unsigned int width, unsigned int height,
unsigned int expanded_width, unsigned int level) unsigned int expanded_width, unsigned int level)
{ {
// Can't copy data larger than the texture extents. // Can't copy data larger than the texture extents.
@ -383,7 +383,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
u8* image_upload_buffer_pointer = upload_buffer->GetCurrentHostPointer(); u8* image_upload_buffer_pointer = upload_buffer->GetCurrentHostPointer();
// Copy to the buffer using the stride from the subresource layout // Copy to the buffer using the stride from the subresource layout
const u8* source_ptr = TextureCache::temp; const u8* source_ptr = buffer;
if (upload_pitch != source_pitch) if (upload_pitch != source_pitch)
{ {
VkDeviceSize copy_pitch = std::min(source_pitch, upload_pitch); VkDeviceSize copy_pitch = std::min(source_pitch, upload_pitch);
@ -429,7 +429,7 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
} }
// Copy data to staging texture first, then to the "real" texture. // Copy data to staging texture first, then to the "real" texture.
staging_texture->WriteTexels(0, 0, width, height, TextureCache::temp, source_pitch); staging_texture->WriteTexels(0, 0, width, height, buffer, source_pitch);
staging_texture->CopyToImage(g_command_buffer_mgr->GetCurrentInitCommandBuffer(), staging_texture->CopyToImage(g_command_buffer_mgr->GetCurrentInitCommandBuffer(),
m_texture->GetImage(), VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, width, m_texture->GetImage(), VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, width,
height, level, 0); height, level, 0);

View File

@ -27,8 +27,8 @@ public:
Texture2D* GetTexture() const { return m_texture.get(); } Texture2D* GetTexture() const { return m_texture.get(); }
VkFramebuffer GetFramebuffer() const { return m_framebuffer; } VkFramebuffer GetFramebuffer() const { return m_framebuffer; }
void Load(unsigned int width, unsigned int height, unsigned int expanded_width, void Load(const u8* buffer, unsigned int width, unsigned int height,
unsigned int level) override; unsigned int expanded_width, unsigned int level) override;
void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect, void FromRenderTarget(u8* dst, PEControl::PixelFormat src_format, const EFBRectangle& src_rect,
bool scale_by_half, unsigned int cbufid, const float* colmat) override; bool scale_by_half, unsigned int cbufid, const float* colmat) override;
void CopyRectangleFromTexture(const TCacheEntryBase* source, void CopyRectangleFromTexture(const TCacheEntryBase* source,

View File

@ -227,7 +227,7 @@ static void BPWritten(const BPCmd& bp)
{ {
// bpmem.zcontrol.pixel_format to PEControl::Z24 is when the game wants to copy from ZBuffer // bpmem.zcontrol.pixel_format to PEControl::Z24 is when the game wants to copy from ZBuffer
// (Zbuffer uses 24-bit Format) // (Zbuffer uses 24-bit Format)
TextureCacheBase::CopyRenderTargetToTexture(destAddr, PE_copy.tp_realFormat(), destStride, g_texture_cache->CopyRenderTargetToTexture(destAddr, PE_copy.tp_realFormat(), destStride,
bpmem.zcontrol.pixel_format, srcRect, bpmem.zcontrol.pixel_format, srcRect,
!!PE_copy.intensity_fmt, !!PE_copy.half_scale); !!PE_copy.intensity_fmt, !!PE_copy.half_scale);
} }

View File

@ -243,6 +243,6 @@ void VideoBackendBase::CheckInvalidState()
m_invalid = false; m_invalid = false;
BPReload(); BPReload();
TextureCacheBase::Invalidate(); g_texture_cache->Invalidate();
} }
} }

View File

@ -108,7 +108,6 @@ static float AspectToWidescreen(float aspect)
Renderer::Renderer() Renderer::Renderer()
{ {
UpdateActiveConfig(); UpdateActiveConfig();
TextureCacheBase::OnConfigChanged(g_ActiveConfig);
OSDChoice = 0; OSDChoice = 0;
OSDTime = 0; OSDTime = 0;

View File

@ -45,16 +45,6 @@ static const u64 MAX_TEXTURE_BINARY_SIZE =
std::unique_ptr<TextureCacheBase> g_texture_cache; std::unique_ptr<TextureCacheBase> g_texture_cache;
alignas(16) u8* TextureCacheBase::temp = nullptr;
size_t TextureCacheBase::temp_size;
TextureCacheBase::TexCache TextureCacheBase::textures_by_address;
TextureCacheBase::TexCache TextureCacheBase::textures_by_hash;
TextureCacheBase::TexPool TextureCacheBase::texture_pool;
TextureCacheBase::TCacheEntryBase* TextureCacheBase::bound_textures[8];
TextureCacheBase::BackupConfig TextureCacheBase::backup_config;
TextureCacheBase::TCacheEntryBase::~TCacheEntryBase() TextureCacheBase::TCacheEntryBase::~TCacheEntryBase()
{ {
} }
@ -71,12 +61,13 @@ void TextureCacheBase::CheckTempSize(size_t required_size)
TextureCacheBase::TextureCacheBase() TextureCacheBase::TextureCacheBase()
{ {
SetBackupConfig(g_ActiveConfig);
temp_size = 2048 * 2048 * 4; temp_size = 2048 * 2048 * 4;
if (!temp)
temp = static_cast<u8*>(Common::AllocateAlignedMemory(temp_size, 16)); temp = static_cast<u8*>(Common::AllocateAlignedMemory(temp_size, 16));
TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, TexDecoder_SetTexFmtOverlayOptions(backup_config.texfmt_overlay,
g_ActiveConfig.bTexFmtOverlayCenter); backup_config.texfmt_overlay_center);
HiresTexture::Init(); HiresTexture::Init();
@ -111,42 +102,33 @@ TextureCacheBase::~TextureCacheBase()
void TextureCacheBase::OnConfigChanged(VideoConfig& config) void TextureCacheBase::OnConfigChanged(VideoConfig& config)
{ {
if (g_texture_cache) if (config.bHiresTextures != backup_config.hires_textures ||
{ config.bCacheHiresTextures != backup_config.cache_hires_textures)
if (config.bHiresTextures != backup_config.s_hires_textures ||
config.bCacheHiresTextures != backup_config.s_cache_hires_textures)
{ {
HiresTexture::Update(); HiresTexture::Update();
} }
// TODO: Invalidating texcache is really stupid in some of these cases // TODO: Invalidating texcache is really stupid in some of these cases
if (config.iSafeTextureCache_ColorSamples != backup_config.s_colorsamples || if (config.iSafeTextureCache_ColorSamples != backup_config.color_samples ||
config.bTexFmtOverlayEnable != backup_config.s_texfmt_overlay || config.bTexFmtOverlayEnable != backup_config.texfmt_overlay ||
config.bTexFmtOverlayCenter != backup_config.s_texfmt_overlay_center || config.bTexFmtOverlayCenter != backup_config.texfmt_overlay_center ||
config.bHiresTextures != backup_config.s_hires_textures) config.bHiresTextures != backup_config.hires_textures)
{ {
g_texture_cache->Invalidate(); Invalidate();
TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable,
g_ActiveConfig.bTexFmtOverlayCenter); g_ActiveConfig.bTexFmtOverlayCenter);
} }
if ((config.iStereoMode > 0) != backup_config.s_stereo_3d || if ((config.iStereoMode > 0) != backup_config.stereo_3d ||
config.bStereoEFBMonoDepth != backup_config.s_efb_mono_depth) config.bStereoEFBMonoDepth != backup_config.efb_mono_depth)
{ {
g_texture_cache->DeleteShaders(); g_texture_cache->DeleteShaders();
if (!g_texture_cache->CompileShaders()) if (!g_texture_cache->CompileShaders())
PanicAlert("Failed to recompile one or more texture conversion shaders."); PanicAlert("Failed to recompile one or more texture conversion shaders.");
} }
}
backup_config.s_colorsamples = config.iSafeTextureCache_ColorSamples; SetBackupConfig(config);
backup_config.s_texfmt_overlay = config.bTexFmtOverlayEnable;
backup_config.s_texfmt_overlay_center = config.bTexFmtOverlayCenter;
backup_config.s_hires_textures = config.bHiresTextures;
backup_config.s_cache_hires_textures = config.bCacheHiresTextures;
backup_config.s_stereo_3d = config.iStereoMode > 0;
backup_config.s_efb_mono_depth = config.bStereoEFBMonoDepth;
} }
void TextureCacheBase::Cleanup(int _frameCount) void TextureCacheBase::Cleanup(int _frameCount)
@ -220,33 +202,40 @@ bool TextureCacheBase::TCacheEntryBase::OverlapsMemoryRange(u32 range_address, u
return true; return true;
} }
TextureCacheBase::TCacheEntryBase* TextureCacheBase::TCacheEntryBase::ApplyPalette(u8* palette, void TextureCacheBase::SetBackupConfig(const VideoConfig& config)
u32 tlutfmt)
{ {
TCacheEntryConfig newconfig; backup_config.color_samples = config.iSafeTextureCache_ColorSamples;
newconfig.rendertarget = true; backup_config.texfmt_overlay = config.bTexFmtOverlayEnable;
newconfig.width = config.width; backup_config.texfmt_overlay_center = config.bTexFmtOverlayCenter;
newconfig.height = config.height; backup_config.hires_textures = config.bHiresTextures;
newconfig.layers = config.layers; backup_config.cache_hires_textures = config.bCacheHiresTextures;
TCacheEntryBase* decoded_entry = AllocateTexture(newconfig); backup_config.stereo_3d = config.iStereoMode > 0;
backup_config.efb_mono_depth = config.bStereoEFBMonoDepth;
}
if (decoded_entry) TextureCacheBase::TCacheEntryBase* TextureCacheBase::ApplyPaletteToEntry(TCacheEntryBase* entry,
u8* palette, u32 tlutfmt)
{ {
decoded_entry->SetGeneralParameters(addr, size_in_bytes, format); TCacheEntryConfig new_config = entry->config;
decoded_entry->SetDimensions(native_width, native_height, 1); new_config.levels = 1;
decoded_entry->SetHashes(base_hash, hash); new_config.rendertarget = true;
TCacheEntryBase* decoded_entry = AllocateTexture(new_config);
if (!decoded_entry)
return nullptr;
decoded_entry->SetGeneralParameters(entry->addr, entry->size_in_bytes, entry->format);
decoded_entry->SetDimensions(entry->native_width, entry->native_height, 1);
decoded_entry->SetHashes(entry->base_hash, entry->hash);
decoded_entry->frameCount = FRAMECOUNT_INVALID; decoded_entry->frameCount = FRAMECOUNT_INVALID;
decoded_entry->is_efb_copy = false; decoded_entry->is_efb_copy = false;
g_texture_cache->ConvertTexture(decoded_entry, this, palette, static_cast<TlutFormat>(tlutfmt)); ConvertTexture(decoded_entry, entry, palette, static_cast<TlutFormat>(tlutfmt));
textures_by_address.emplace(addr, decoded_entry); textures_by_address.emplace(entry->addr, decoded_entry);
return decoded_entry; return decoded_entry;
} }
return nullptr;
}
void TextureCacheBase::ScaleTextureCacheEntryTo(TextureCacheBase::TCacheEntryBase** entry, void TextureCacheBase::ScaleTextureCacheEntryTo(TextureCacheBase::TCacheEntryBase** entry,
u32 new_width, u32 new_height) u32 new_width, u32 new_height)
{ {
@ -342,7 +331,7 @@ TextureCacheBase::DoPartialTextureUpdates(TexCache::iterator iter_t, u8* palette
{ {
if (isPaletteTexture) if (isPaletteTexture)
{ {
TCacheEntryBase* decoded_entry = entry->ApplyPalette(palette, tlutfmt); TCacheEntryBase* decoded_entry = ApplyPaletteToEntry(entry, palette, tlutfmt);
if (decoded_entry) if (decoded_entry)
{ {
// Link the efb copy with the partially updated texture, so we won't apply this partial // Link the efb copy with the partially updated texture, so we won't apply this partial
@ -701,7 +690,7 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage)
if (unconverted_copy != textures_by_address.end()) if (unconverted_copy != textures_by_address.end())
{ {
TCacheEntryBase* decoded_entry = TCacheEntryBase* decoded_entry =
unconverted_copy->second->ApplyPalette(&texMem[tlutaddr], tlutfmt); ApplyPaletteToEntry(unconverted_copy->second, &texMem[tlutaddr], tlutfmt);
if (decoded_entry) if (decoded_entry)
{ {
@ -810,7 +799,7 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage)
entry->is_custom_tex = hires_tex != nullptr; entry->is_custom_tex = hires_tex != nullptr;
// load texture // load texture
entry->Load(width, height, expandedWidth, 0); entry->Load(temp, width, height, expandedWidth, 0);
std::string basename = ""; std::string basename = "";
if (g_ActiveConfig.bDumpTextures && !hires_tex) if (g_ActiveConfig.bDumpTextures && !hires_tex)
@ -827,7 +816,7 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage)
const auto& level = hires_tex->m_levels[level_index]; const auto& level = hires_tex->m_levels[level_index];
CheckTempSize(level.data_size); CheckTempSize(level.data_size);
memcpy(temp, level.data.get(), level.data_size); memcpy(temp, level.data.get(), level.data_size);
entry->Load(level.width, level.height, level.width, level_index); entry->Load(temp, level.width, level.height, level.width, level_index);
} }
} }
else else
@ -858,7 +847,7 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage)
mip_src_data += mip_src_data +=
TexDecoder_GetTextureSizeInBytes(expanded_mip_width, expanded_mip_height, texformat); TexDecoder_GetTextureSizeInBytes(expanded_mip_width, expanded_mip_height, texformat);
entry->Load(mip_width, mip_height, expanded_mip_width, level); entry->Load(temp, mip_width, mip_height, expanded_mip_width, level);
if (g_ActiveConfig.bDumpTextures) if (g_ActiveConfig.bDumpTextures)
DumpTexture(entry, basename, level); DumpTexture(entry, basename, level);
@ -1253,8 +1242,8 @@ void TextureCacheBase::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFo
if (copy_to_ram) if (copy_to_ram)
{ {
g_texture_cache->CopyEFB(dst, dstFormat, tex_w, bytes_per_row, num_blocks_y, dstStride, CopyEFB(dst, dstFormat, tex_w, bytes_per_row, num_blocks_y, dstStride, srcFormat, srcRect,
srcFormat, srcRect, isIntensity, scaleByHalf); isIntensity, scaleByHalf);
} }
else else
{ {
@ -1364,7 +1353,7 @@ TextureCacheBase::AllocateTexture(const TCacheEntryConfig& config)
} }
else else
{ {
entry = g_texture_cache->CreateTexture(config); entry = CreateTexture(config);
if (!entry) if (!entry)
return nullptr; return nullptr;

View File

@ -127,16 +127,13 @@ public:
const MathUtil::Rectangle<int>& srcrect, const MathUtil::Rectangle<int>& srcrect,
const MathUtil::Rectangle<int>& dstrect) = 0; const MathUtil::Rectangle<int>& dstrect) = 0;
virtual void Load(unsigned int width, unsigned int height, unsigned int expanded_width, virtual void Load(const u8* buffer, u32 width, u32 height, u32 expanded_width, u32 level) = 0;
unsigned int level) = 0;
virtual void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, virtual void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat,
const EFBRectangle& srcRect, bool scaleByHalf, const EFBRectangle& srcRect, bool scaleByHalf,
unsigned int cbufid, const float* colmat) = 0; unsigned int cbufid, const float* colmat) = 0;
bool OverlapsMemoryRange(u32 range_address, u32 range_size) const; bool OverlapsMemoryRange(u32 range_address, u32 range_size) const;
TextureCacheBase::TCacheEntryBase* ApplyPalette(u8* palette, u32 tlutfmt);
bool IsEfbCopy() const { return is_efb_copy; } bool IsEfbCopy() const { return is_efb_copy; }
u32 NumBlocksY() const; u32 NumBlocksY() const;
u32 BytesPerRow() const; u32 BytesPerRow() const;
@ -146,13 +143,13 @@ public:
virtual ~TextureCacheBase(); // needs virtual for DX11 dtor virtual ~TextureCacheBase(); // needs virtual for DX11 dtor
static void OnConfigChanged(VideoConfig& config); void OnConfigChanged(VideoConfig& config);
// Removes textures which aren't used for more than TEXTURE_KILL_THRESHOLD frames, // Removes textures which aren't used for more than TEXTURE_KILL_THRESHOLD frames,
// frameCount is the current frame number. // frameCount is the current frame number.
static void Cleanup(int _frameCount); void Cleanup(int _frameCount);
static void Invalidate(); void Invalidate();
virtual TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) = 0; virtual TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) = 0;
@ -163,13 +160,12 @@ public:
virtual bool CompileShaders() = 0; virtual bool CompileShaders() = 0;
virtual void DeleteShaders() = 0; virtual void DeleteShaders() = 0;
static TCacheEntryBase* Load(const u32 stage); TCacheEntryBase* Load(const u32 stage);
static void UnbindTextures(); void UnbindTextures();
virtual void BindTextures(); virtual void BindTextures();
static void CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat, u32 dstStride, void CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat, u32 dstStride,
PEControl::PixelFormat srcFormat, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
const EFBRectangle& srcRect, bool isIntensity, bool isIntensity, bool scaleByHalf);
bool scaleByHalf);
virtual void ConvertTexture(TCacheEntryBase* entry, TCacheEntryBase* unconverted, void* palette, virtual void ConvertTexture(TCacheEntryBase* entry, TCacheEntryBase* unconverted, void* palette,
TlutFormat format) = 0; TlutFormat format) = 0;
@ -177,46 +173,52 @@ public:
protected: protected:
TextureCacheBase(); TextureCacheBase();
alignas(16) static u8* temp; alignas(16) u8* temp = nullptr;
static size_t temp_size; size_t temp_size = 0;
static TCacheEntryBase* bound_textures[8]; TCacheEntryBase* bound_textures[8] = {};
private: private:
typedef std::multimap<u64, TCacheEntryBase*> TexCache; typedef std::multimap<u64, TCacheEntryBase*> TexCache;
typedef std::unordered_multimap<TCacheEntryConfig, TCacheEntryBase*, TCacheEntryConfig::Hasher> typedef std::unordered_multimap<TCacheEntryConfig, TCacheEntryBase*, TCacheEntryConfig::Hasher>
TexPool; TexPool;
static void ScaleTextureCacheEntryTo(TCacheEntryBase** entry, u32 new_width, u32 new_height);
static TCacheEntryBase* DoPartialTextureUpdates(TexCache::iterator iter, u8* palette,
u32 tlutfmt);
static void DumpTexture(TCacheEntryBase* entry, std::string basename, unsigned int level);
static void CheckTempSize(size_t required_size);
static TCacheEntryBase* AllocateTexture(const TCacheEntryConfig& config); void SetBackupConfig(const VideoConfig& config);
static TexPool::iterator FindMatchingTextureFromPool(const TCacheEntryConfig& config);
static TexCache::iterator GetTexCacheIter(TCacheEntryBase* entry); TCacheEntryBase* ApplyPaletteToEntry(TCacheEntryBase* entry, u8* palette, u32 tlutfmt);
void ScaleTextureCacheEntryTo(TCacheEntryBase** entry, u32 new_width, u32 new_height);
TCacheEntryBase* DoPartialTextureUpdates(TexCache::iterator iter, u8* palette, u32 tlutfmt);
void DumpTexture(TCacheEntryBase* entry, std::string basename, unsigned int level);
void CheckTempSize(size_t required_size);
TCacheEntryBase* AllocateTexture(const TCacheEntryConfig& config);
TexPool::iterator FindMatchingTextureFromPool(const TCacheEntryConfig& config);
TexCache::iterator GetTexCacheIter(TCacheEntryBase* entry);
// Removes and unlinks texture from texture cache and returns it to the pool // Removes and unlinks texture from texture cache and returns it to the pool
static TexCache::iterator InvalidateTexture(TexCache::iterator t_iter); TexCache::iterator InvalidateTexture(TexCache::iterator t_iter);
static TCacheEntryBase* ReturnEntry(unsigned int stage, TCacheEntryBase* entry); TCacheEntryBase* ReturnEntry(unsigned int stage, TCacheEntryBase* entry);
static TexCache textures_by_address; TexCache textures_by_address;
static TexCache textures_by_hash; TexCache textures_by_hash;
static TexPool texture_pool; TexPool texture_pool;
// Backup configuration values // Backup configuration values
static struct BackupConfig struct BackupConfig
{ {
int s_colorsamples; int color_samples;
bool s_texfmt_overlay; bool texfmt_overlay;
bool s_texfmt_overlay_center; bool texfmt_overlay_center;
bool s_hires_textures; bool hires_textures;
bool s_cache_hires_textures; bool cache_hires_textures;
bool s_copy_cache_enable; bool copy_cache_enable;
bool s_stereo_3d; bool stereo_3d;
bool s_efb_mono_depth; bool efb_mono_depth;
} backup_config; };
BackupConfig backup_config = {};
}; };
extern std::unique_ptr<TextureCacheBase> g_texture_cache; extern std::unique_ptr<TextureCacheBase> g_texture_cache;

View File

@ -214,10 +214,10 @@ void VertexManagerBase::Flush()
if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < bpmem.genMode.numindstages) if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < bpmem.genMode.numindstages)
usedtextures[bpmem.tevindref.getTexMap(bpmem.tevind[i].bt)] = true; usedtextures[bpmem.tevindref.getTexMap(bpmem.tevind[i].bt)] = true;
TextureCacheBase::UnbindTextures(); g_texture_cache->UnbindTextures();
for (unsigned int i : usedtextures) for (unsigned int i : usedtextures)
{ {
const TextureCacheBase::TCacheEntryBase* tentry = TextureCacheBase::Load(i); const auto* tentry = g_texture_cache->Load(i);
if (tentry) if (tentry)
{ {