diff --git a/Source/Core/VideoBackends/D3D/PSTextureEncoder.cpp b/Source/Core/VideoBackends/D3D/PSTextureEncoder.cpp index c337927ac9..b3209bef48 100644 --- a/Source/Core/VideoBackends/D3D/PSTextureEncoder.cpp +++ b/Source/Core/VideoBackends/D3D/PSTextureEncoder.cpp @@ -87,7 +87,7 @@ void PSTextureEncoder::Shutdown() SAFE_RELEASE(m_out); } -void PSTextureEncoder::Encode(u8* dst, const TextureCache::TCacheEntryBase *texture_entry, +void PSTextureEncoder::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) { @@ -101,9 +101,9 @@ void PSTextureEncoder::Encode(u8* dst, const TextureCache::TCacheEntryBase *text // Set up all the state for EFB encoding { - const u32 words_per_row = texture_entry->BytesPerRow() / sizeof(u32); + const u32 words_per_row = bytes_per_row / sizeof(u32); - D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, FLOAT(words_per_row), FLOAT(texture_entry->NumBlocksY())); + D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, FLOAT(words_per_row), FLOAT(num_blocks_y)); D3D::context->RSSetViewports(1, &vp); EFBRectangle fullSrcRect; @@ -125,7 +125,7 @@ void PSTextureEncoder::Encode(u8* dst, const TextureCache::TCacheEntryBase *text EFBEncodeParams params; params.SrcLeft = srcRect.left; params.SrcTop = srcRect.top; - params.DestWidth = texture_entry->native_width; + params.DestWidth = native_width; params.ScaleFactor = scaleByHalf ? 2 : 1; D3D::context->UpdateSubresource(m_encodeParams, 0, nullptr, ¶ms, 0, 0); D3D::stateman->SetPixelConstants(m_encodeParams); @@ -140,12 +140,12 @@ void PSTextureEncoder::Encode(u8* dst, const TextureCache::TCacheEntryBase *text targetRect.AsRECT(), Renderer::GetTargetWidth(), Renderer::GetTargetHeight(), - SetStaticShader(texture_entry->format, srcFormat, isIntensity, scaleByHalf), + SetStaticShader(format, srcFormat, isIntensity, scaleByHalf), VertexShaderCache::GetSimpleVertexShader(), VertexShaderCache::GetSimpleInputLayout()); // Copy to staging buffer - D3D11_BOX srcBox = CD3D11_BOX(0, 0, 0, words_per_row, texture_entry->NumBlocksY(), 1); + D3D11_BOX srcBox = CD3D11_BOX(0, 0, 0, words_per_row, num_blocks_y, 1); D3D::context->CopySubresourceRegion(m_outStage, 0, 0, 0, 0, m_out, 0, &srcBox); // Transfer staging buffer to GameCube/Wii RAM @@ -154,11 +154,11 @@ void PSTextureEncoder::Encode(u8* dst, const TextureCache::TCacheEntryBase *text CHECK(SUCCEEDED(hr), "map staging buffer (0x%x)", hr); u8* src = (u8*)map.pData; - u32 readStride = std::min(texture_entry->BytesPerRow(), map.RowPitch); - for (unsigned int y = 0; y < texture_entry->NumBlocksY(); ++y) + u32 readStride = std::min(bytes_per_row, map.RowPitch); + for (unsigned int y = 0; y < num_blocks_y; ++y) { memcpy(dst, src, readStride); - dst += texture_entry->memory_stride; + dst += memory_stride; src += map.RowPitch; } diff --git a/Source/Core/VideoBackends/D3D/PSTextureEncoder.h b/Source/Core/VideoBackends/D3D/PSTextureEncoder.h index 40f49a0728..b12030ec66 100644 --- a/Source/Core/VideoBackends/D3D/PSTextureEncoder.h +++ b/Source/Core/VideoBackends/D3D/PSTextureEncoder.h @@ -31,7 +31,7 @@ public: void Init(); void Shutdown(); - void Encode(u8* dst, const TextureCacheBase::TCacheEntryBase* texture_entry, + 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); diff --git a/Source/Core/VideoBackends/D3D/TextureCache.cpp b/Source/Core/VideoBackends/D3D/TextureCache.cpp index 1738a1f4d5..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(); @@ -236,11 +234,13 @@ void TextureCache::TCacheEntry::FromRenderTarget(u8* dst, unsigned int dstFormat D3D::context->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV(), FramebufferManager::GetEFBDepthTexture()->GetDSV()); g_renderer->RestoreAPIState(); +} - if (g_ActiveConfig.bSkipEFBCopyToRam) - this->Zero(dst); - else - g_encoder->Encode(dst, this, srcFormat, srcRect, isIntensity, scaleByHalf); +void TextureCache::CopyEFB(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) +{ + g_encoder->Encode(dst, format, native_width, bytes_per_row, num_blocks_y, memory_stride, srcFormat, srcRect, isIntensity, scaleByHalf); } const char palette_shader[] = diff --git a/Source/Core/VideoBackends/D3D/TextureCache.h b/Source/Core/VideoBackends/D3D/TextureCache.h index fa6aeafc3b..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; @@ -49,6 +47,10 @@ private: void ConvertTexture(TCacheEntryBase* entry, TCacheEntryBase* unconverted, void* palette, TlutFormat format) override; + void CopyEFB(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) override; + void CompileShaders() override { } void DeleteShaders() override { } diff --git a/Source/Core/VideoBackends/D3D/TextureEncoder.h b/Source/Core/VideoBackends/D3D/TextureEncoder.h index 2e3eef99ad..52ac6ba25e 100644 --- a/Source/Core/VideoBackends/D3D/TextureEncoder.h +++ b/Source/Core/VideoBackends/D3D/TextureEncoder.h @@ -26,7 +26,7 @@ public: virtual void Init() = 0; virtual void Shutdown() = 0; // Returns size in bytes of encoded block of memory - virtual void Encode(u8* dst, const TextureCacheBase::TCacheEntryBase* texture_entry, + virtual 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) = 0; diff --git a/Source/Core/VideoBackends/OGL/TextureCache.cpp b/Source/Core/VideoBackends/OGL/TextureCache.cpp index 2cd1f3942a..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 @@ -264,27 +262,27 @@ void TextureCache::TCacheEntry::FromRenderTarget(u8* dstPointer, unsigned int ds glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - if (g_ActiveConfig.bSkipEFBCopyToRam) - { - this->Zero(dstPointer); - } - else - { - TextureConverter::EncodeToRamFromTexture( - dstPointer, - this, - read_texture, - srcFormat == PEControl::Z24, - isIntensity, - scaleByHalf, - srcRect); - } - FramebufferManager::SetFramebuffer(0); - g_renderer->RestoreAPIState(); } +void TextureCache::CopyEFB(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) +{ + TextureConverter::EncodeToRamFromTexture( + dst, + format, + native_width, + bytes_per_row, + num_blocks_y, + memory_stride, + srcFormat, + isIntensity, + scaleByHalf, + srcRect); +} + TextureCache::TextureCache() { CompileShaders(); diff --git a/Source/Core/VideoBackends/OGL/TextureCache.h b/Source/Core/VideoBackends/OGL/TextureCache.h index 10c4a57378..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; @@ -56,6 +54,10 @@ private: TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) override; void ConvertTexture(TCacheEntryBase* entry, TCacheEntryBase* unconverted, void* palette, TlutFormat format) override; + void CopyEFB(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) override; + void CompileShaders() override; void DeleteShaders() override; }; diff --git a/Source/Core/VideoBackends/OGL/TextureConverter.cpp b/Source/Core/VideoBackends/OGL/TextureConverter.cpp index b780594101..b56b08ab4f 100644 --- a/Source/Core/VideoBackends/OGL/TextureConverter.cpp +++ b/Source/Core/VideoBackends/OGL/TextureConverter.cpp @@ -269,18 +269,27 @@ static void EncodeToRamUsingShader(GLuint srcTexture, } } -void EncodeToRamFromTexture(u8 *dest_ptr, const TextureCache::TCacheEntryBase *texture_entry, - GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, int bScaleByHalf, const EFBRectangle& source) +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) { - SHADER& texconv_shader = GetOrCreateEncodingShader(texture_entry->format); + g_renderer->ResetAPIState(); + + SHADER& texconv_shader = GetOrCreateEncodingShader(format); texconv_shader.Bind(); - glUniform4i(s_encodingUniforms[texture_entry->format], - source.left, source.top, texture_entry->native_width, bScaleByHalf ? 2 : 1); + glUniform4i(s_encodingUniforms[format], + source.left, source.top, native_width, bScaleByHalf ? 2 : 1); - EncodeToRamUsingShader(source_texture, - dest_ptr, texture_entry->BytesPerRow(), texture_entry->NumBlocksY(), - texture_entry->memory_stride, bScaleByHalf > 0 && !bFromZBuffer); + const GLuint read_texture = (srcFormat == PEControl::Z24) ? + FramebufferManager::ResolveAndGetDepthTarget(source) : + FramebufferManager::ResolveAndGetRenderTarget(source); + + EncodeToRamUsingShader(read_texture, + dest_ptr, bytes_per_row, num_blocks_y, + memory_stride, bScaleByHalf > 0 && srcFormat != PEControl::Z24); + + FramebufferManager::SetFramebuffer(0); + g_renderer->RestoreAPIState(); } void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, u32 dstWidth, u32 dstStride, u32 dstHeight) diff --git a/Source/Core/VideoBackends/OGL/TextureConverter.h b/Source/Core/VideoBackends/OGL/TextureConverter.h index b13bbc57f2..5fdb46e92f 100644 --- a/Source/Core/VideoBackends/OGL/TextureConverter.h +++ b/Source/Core/VideoBackends/OGL/TextureConverter.h @@ -26,8 +26,8 @@ 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, const TextureCacheBase::TCacheEntryBase* texture_entry, - GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, int bScaleByHalf, const EFBRectangle& source); +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 8f1d700dfd..693b4b5ba1 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -1032,61 +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; - u64 hash = entry->CalculateHash(); - entry->SetHashes(hash, hash); + bool copy_to_ram = !g_ActiveConfig.bSkipEFBCopyToRam; + bool copy_to_vram = true; - // 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()) + if (copy_to_ram) { - TexCache::iterator iter = textures_by_address.begin(); - while (iter != textures_by_address.end()) - { - if (iter->second->OverlapsMemoryRange(dstAddr, entry->size_in_bytes)) - iter = FreeTexture(iter); - else - ++iter; - } + g_texture_cache->CopyEFB( + dst, + dstFormat, + tex_w, + bytes_per_row, + num_blocks_y, + dstStride, + srcFormat, + srcRect, + isIntensity, + scaleByHalf); } - - if (g_ActiveConfig.bDumpEFBTarget) + else { - static int count = 0; - entry->Save(StringFromFormat("%sefb_frame_%i.png", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(), - count++), 0); + // 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++) + { + memset(ptr, 0, bytes_per_row); + ptr += dstStride; + } } 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) @@ -1158,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 9e7b4a7c71..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; }; @@ -130,6 +126,10 @@ public: virtual TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) = 0; + virtual void CopyEFB(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) = 0; + virtual void CompileShaders() = 0; // currently only implemented by OGL virtual void DeleteShaders() = 0; // currently only implemented by OGL