From 087ba5268a5512e95783ad69bc743a5aeb0bb0b9 Mon Sep 17 00:00:00 2001 From: degasus Date: Thu, 29 Oct 2015 18:08:04 +0100 Subject: [PATCH] TextureCache: Rewrite EFB Copy control flow --- .../Core/VideoBackends/D3D/PSTextureEncoder.h | 4 +- .../Core/VideoBackends/D3D/TextureCache.cpp | 6 +- Source/Core/VideoBackends/D3D/TextureCache.h | 6 +- .../Core/VideoBackends/OGL/TextureCache.cpp | 6 +- Source/Core/VideoBackends/OGL/TextureCache.h | 6 +- .../VideoBackends/OGL/TextureConverter.cpp | 2 +- .../Core/VideoBackends/OGL/TextureConverter.h | 2 +- Source/Core/VideoCommon/TextureCacheBase.cpp | 135 ++++++++++-------- Source/Core/VideoCommon/TextureCacheBase.h | 8 +- 9 files changed, 91 insertions(+), 84 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/PSTextureEncoder.h b/Source/Core/VideoBackends/D3D/PSTextureEncoder.h index e82db8779e..b12030ec66 100644 --- a/Source/Core/VideoBackends/D3D/PSTextureEncoder.h +++ b/Source/Core/VideoBackends/D3D/PSTextureEncoder.h @@ -32,8 +32,8 @@ public: void Init(); void Shutdown(); void Encode(u8* dst, u32 format, u32 native_width, u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf); + PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool isIntensity, bool scaleByHalf); private: bool m_ready; diff --git a/Source/Core/VideoBackends/D3D/TextureCache.cpp b/Source/Core/VideoBackends/D3D/TextureCache.cpp index f2f5ae96b4..1d5141ce0e 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D/TextureCache.cpp @@ -184,10 +184,8 @@ TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntry } } -void TextureCache::TCacheEntry::FromRenderTarget(u8* dst, unsigned int dstFormat, u32 dstStride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf, unsigned int cbufid, - const float *colmat) +void TextureCache::TCacheEntry::FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool scaleByHalf, unsigned int cbufid, const float *colmat) { g_renderer->ResetAPIState(); diff --git a/Source/Core/VideoBackends/D3D/TextureCache.h b/Source/Core/VideoBackends/D3D/TextureCache.h index c852d2469c..37f0dcc6c7 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.h +++ b/Source/Core/VideoBackends/D3D/TextureCache.h @@ -34,10 +34,8 @@ private: void Load(unsigned int width, unsigned int height, unsigned int expanded_width, unsigned int levels) override; - void FromRenderTarget(u8* dst, unsigned int dstFormat, u32 dstStride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf, unsigned int cbufid, - const float *colmat) override; + void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool scaleByHalf, unsigned int cbufid, const float *colmat) override; void Bind(unsigned int stage) override; bool Save(const std::string& filename, unsigned int level) override; diff --git a/Source/Core/VideoBackends/OGL/TextureCache.cpp b/Source/Core/VideoBackends/OGL/TextureCache.cpp index e733cd8d87..5ffd87699b 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.cpp +++ b/Source/Core/VideoBackends/OGL/TextureCache.cpp @@ -215,10 +215,8 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, TextureCache::SetStage(); } -void TextureCache::TCacheEntry::FromRenderTarget(u8* dstPointer, unsigned int dstFormat, u32 dstStride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf, unsigned int cbufid, - const float *colmat) +void TextureCache::TCacheEntry::FromRenderTarget(u8* dstPointer, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool scaleByHalf, unsigned int cbufid, const float *colmat) { g_renderer->ResetAPIState(); // reset any game specific settings diff --git a/Source/Core/VideoBackends/OGL/TextureCache.h b/Source/Core/VideoBackends/OGL/TextureCache.h index 3028a25d38..5460e1be09 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.h +++ b/Source/Core/VideoBackends/OGL/TextureCache.h @@ -42,10 +42,8 @@ private: void Load(unsigned int width, unsigned int height, unsigned int expanded_width, unsigned int level) override; - void FromRenderTarget(u8 *dst, unsigned int dstFormat, u32 dstStride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf, unsigned int cbufid, - const float *colmat) override; + void FromRenderTarget(u8 *dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool scaleByHalf, unsigned int cbufid, const float *colmat) override; void Bind(unsigned int stage) override; bool Save(const std::string& filename, unsigned int level) override; diff --git a/Source/Core/VideoBackends/OGL/TextureConverter.cpp b/Source/Core/VideoBackends/OGL/TextureConverter.cpp index bd51ca745f..b56b08ab4f 100644 --- a/Source/Core/VideoBackends/OGL/TextureConverter.cpp +++ b/Source/Core/VideoBackends/OGL/TextureConverter.cpp @@ -269,7 +269,7 @@ static void EncodeToRamUsingShader(GLuint srcTexture, } } -void EncodeToRamFromTexture(u8 *dest_ptr, u32 format, u32 native_width, u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride, +void EncodeToRamFromTexture(u8* dest_ptr, u32 format, u32 native_width, u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride, PEControl::PixelFormat srcFormat, bool bIsIntensityFmt, int bScaleByHalf, const EFBRectangle& source) { g_renderer->ResetAPIState(); diff --git a/Source/Core/VideoBackends/OGL/TextureConverter.h b/Source/Core/VideoBackends/OGL/TextureConverter.h index e417e431a4..5fdb46e92f 100644 --- a/Source/Core/VideoBackends/OGL/TextureConverter.h +++ b/Source/Core/VideoBackends/OGL/TextureConverter.h @@ -26,7 +26,7 @@ void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTexture); // returns size of the encoded data (in bytes) -void EncodeToRamFromTexture(u8 *dest_ptr, u32 format, u32 native_width, u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride, +void EncodeToRamFromTexture(u8* dest_ptr, u32 format, u32 native_width, u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride, PEControl::PixelFormat srcFormat, bool bIsIntensityFmt, int bScaleByHalf, const EFBRectangle& source); } diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 44fdf9fe48..693b4b5ba1 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -1032,80 +1032,109 @@ void TextureCacheBase::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFo } } - // create the texture - TCacheEntryConfig config; - config.rendertarget = true; - config.width = scaled_tex_w; - config.height = scaled_tex_h; - config.layers = FramebufferManagerBase::GetEFBLayers(); + u32 blockH = TexDecoder_GetBlockHeightInTexels(dstFormat); + const u32 blockW = TexDecoder_GetBlockWidthInTexels(dstFormat); - TCacheEntryBase* entry = AllocateTexture(config); + // Round up source height to multiple of block size + u32 actualHeight = ROUND_UP(tex_h, blockH); + const u32 actualWidth = ROUND_UP(tex_w, blockW); - entry->SetGeneralParameters(dstAddr, 0, dstFormat); - entry->SetDimensions(tex_w, tex_h, 1); + u32 num_blocks_y = actualHeight / blockH; + const u32 num_blocks_x = actualWidth / blockW; - entry->frameCount = FRAMECOUNT_INVALID; - entry->SetEfbCopy(dstStride); - entry->is_custom_tex = false; + // RGBA takes two cache lines per block; all others take one + const u32 bytes_per_block = dstFormat == GX_TF_RGBA8 ? 64 : 32; - entry->FromRenderTarget(dst, dstFormat, dstStride, srcFormat, srcRect, isIntensity, scaleByHalf, cbufid, colmat); + u32 bytes_per_row = num_blocks_x * bytes_per_block; - if (g_ActiveConfig.bSkipEFBCopyToRam) - { - entry->Zero(dst); - } - else + bool copy_to_ram = !g_ActiveConfig.bSkipEFBCopyToRam; + bool copy_to_vram = true; + + if (copy_to_ram) { g_texture_cache->CopyEFB( dst, - entry->format, - entry->native_width, - entry->BytesPerRow(), - entry->NumBlocksY(), - entry->memory_stride, + dstFormat, + tex_w, + bytes_per_row, + num_blocks_y, + dstStride, srcFormat, srcRect, isIntensity, scaleByHalf); } - - u64 hash = entry->CalculateHash(); - entry->SetHashes(hash, hash); - - // Invalidate all textures that overlap the range of our efb copy. - // Unless our efb copy has a weird stride, then we want avoid invalidating textures which - // we might be able to do a partial texture update on. - if (entry->memory_stride == entry->BytesPerRow()) + else { - TexCache::iterator iter = textures_by_address.begin(); - while (iter != textures_by_address.end()) + // Hack: Most games don't actually need the correct texture data in RAM + // and we can just keep a copy in VRAM. We zero the memory so we + // can check it hasn't changed before using our copy in VRAM. + u8* ptr = dst; + for (u32 i = 0; i < num_blocks_y; i++) { - if (iter->second->OverlapsMemoryRange(dstAddr, entry->size_in_bytes)) - iter = FreeTexture(iter); - else - ++iter; + memset(ptr, 0, bytes_per_row); + ptr += dstStride; } } - if (g_ActiveConfig.bDumpEFBTarget) - { - static int count = 0; - entry->Save(StringFromFormat("%sefb_frame_%i.png", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(), - count++), 0); - } - if (g_bRecordFifoData) { // Mark the memory behind this efb copy as dynamicly generated for the Fifo log u32 address = dstAddr; - for (u32 i = 0; i < entry->NumBlocksY(); i++) + for (u32 i = 0; i < num_blocks_y; i++) { - FifoRecorder::GetInstance().UseMemory(address, entry->BytesPerRow(), MemoryUpdate::TEXTURE_MAP, true); - address += entry->memory_stride; + FifoRecorder::GetInstance().UseMemory(address, bytes_per_row, MemoryUpdate::TEXTURE_MAP, true); + address += dstStride; } } - textures_by_address.emplace((u64)dstAddr, entry); + // Invalidate all textures that overlap the range of our efb copy. + // Unless our efb copy has a weird stride, then we want avoid invalidating textures which + // we might be able to do a partial texture update on. + if (dstStride == bytes_per_row || !copy_to_vram) + { + TexCache::iterator iter = textures_by_address.begin(); + while (iter != textures_by_address.end()) + { + if (iter->second->addr + iter->second->size_in_bytes <= dstAddr || iter->second->addr >= dstAddr + num_blocks_y * dstStride) + ++iter; + else + iter = FreeTexture(iter); + } + } + + if (copy_to_vram) + { + // create the texture + TCacheEntryConfig config; + config.rendertarget = true; + config.width = scaled_tex_w; + config.height = scaled_tex_h; + config.layers = FramebufferManagerBase::GetEFBLayers(); + + TCacheEntryBase* entry = AllocateTexture(config); + + entry->SetGeneralParameters(dstAddr, 0, dstFormat); + entry->SetDimensions(tex_w, tex_h, 1); + + entry->frameCount = FRAMECOUNT_INVALID; + entry->SetEfbCopy(dstStride); + entry->is_custom_tex = false; + + entry->FromRenderTarget(dst, srcFormat, srcRect, scaleByHalf, cbufid, colmat); + + u64 hash = entry->CalculateHash(); + entry->SetHashes(hash, hash); + + if (g_ActiveConfig.bDumpEFBTarget) + { + static int count = 0; + entry->Save(StringFromFormat("%sefb_frame_%i.png", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(), + count++), 0); + } + + textures_by_address.emplace((u64)dstAddr, entry); + } } TextureCacheBase::TCacheEntryBase* TextureCacheBase::AllocateTexture(const TCacheEntryConfig& config) @@ -1177,16 +1206,6 @@ void TextureCacheBase::TCacheEntryBase::SetEfbCopy(u32 stride) size_in_bytes = memory_stride * NumBlocksY(); } -// Fill gamecube memory backing this texture with zeros. -void TextureCacheBase::TCacheEntryBase::Zero(u8* ptr) -{ - for (u32 i = 0; i < NumBlocksY(); i++) - { - memset(ptr, 0, BytesPerRow()); - ptr += memory_stride; - } -} - u64 TextureCacheBase::TCacheEntryBase::CalculateHash() const { u8* ptr = Memory::GetPointer(addr); diff --git a/Source/Core/VideoCommon/TextureCacheBase.h b/Source/Core/VideoCommon/TextureCacheBase.h index 1dbddd253f..00db6e46a3 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.h +++ b/Source/Core/VideoCommon/TextureCacheBase.h @@ -101,10 +101,8 @@ public: virtual void Load(unsigned int width, unsigned int height, unsigned int expanded_width, unsigned int level) = 0; - virtual void FromRenderTarget(u8* dst, unsigned int dstFormat, u32 dstStride, - PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, - bool isIntensity, bool scaleByHalf, unsigned int cbufid, - const float *colmat) = 0; + virtual void FromRenderTarget(u8* dst, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, + bool scaleByHalf, unsigned int cbufid, const float *colmat) = 0; bool OverlapsMemoryRange(u32 range_address, u32 range_size) const; @@ -113,8 +111,6 @@ public: u32 NumBlocksY() const; u32 BytesPerRow() const; - void Zero(u8* ptr); - u64 CalculateHash() const; };