From cc851c41c11bdc1c192003021df6dc3c2d2b74fb Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 29 Apr 2017 00:53:32 +1000 Subject: [PATCH] TextureCache: Move host texture utility functions to VideoCommon The appropriate place for these would be AbstractTexture, once it is finished. --- .../Core/VideoBackends/D3D/TextureCache.cpp | 22 +++------------- .../Core/VideoBackends/D3D12/TextureCache.cpp | 22 +++------------- .../Core/VideoBackends/OGL/TextureCache.cpp | 9 ++----- .../VideoBackends/Vulkan/TextureCache.cpp | 2 +- Source/Core/VideoBackends/Vulkan/Util.cpp | 26 ------------------- Source/Core/VideoBackends/Vulkan/Util.h | 2 -- Source/Core/VideoCommon/TextureCacheBase.cpp | 23 ++++++++++++++++ Source/Core/VideoCommon/TextureCacheBase.h | 4 +++ 8 files changed, 36 insertions(+), 74 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/TextureCache.cpp b/Source/Core/VideoBackends/D3D/TextureCache.cpp index 92941477e8..cd737e7201 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D/TextureCache.cpp @@ -30,23 +30,6 @@ static std::unique_ptr g_encoder; const size_t MAX_COPY_BUFFERS = 32; ID3D11Buffer* efbcopycbuf[MAX_COPY_BUFFERS] = {0}; -static u32 GetLevelPitch(HostTextureFormat format, u32 row_length) -{ - switch (format) - { - case HostTextureFormat::DXT1: - return row_length / 4 * 8; - - case HostTextureFormat::DXT3: - case HostTextureFormat::DXT5: - return row_length / 4 * 16; - - case HostTextureFormat::RGBA8: - default: - return row_length * 4; - } -} - static DXGI_FORMAT GetDXGIFormatForHostFormat(HostTextureFormat format) { switch (format) @@ -174,8 +157,9 @@ void TextureCache::TCacheEntry::CopyRectangleFromTexture(const TCacheEntryBase* void TextureCache::TCacheEntry::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size) { - u32 src_pitch = GetLevelPitch(config.format, row_length); - D3D::context->UpdateSubresource(texture->GetTex(), level, nullptr, buffer, src_pitch, 0); + size_t src_pitch = CalculateHostTextureLevelPitch(config.format, row_length); + D3D::context->UpdateSubresource(texture->GetTex(), level, nullptr, buffer, + static_cast(src_pitch), 0); } TextureCacheBase::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config) diff --git a/Source/Core/VideoBackends/D3D12/TextureCache.cpp b/Source/Core/VideoBackends/D3D12/TextureCache.cpp index cf0f237575..4b0a47b158 100644 --- a/Source/Core/VideoBackends/D3D12/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D12/TextureCache.cpp @@ -33,23 +33,6 @@ static u32 s_efb_copy_last_cbuf_id = UINT_MAX; static ID3D12Resource* s_texture_cache_entry_readback_buffer = nullptr; static size_t s_texture_cache_entry_readback_buffer_size = 0; -static u32 GetLevelPitch(HostTextureFormat format, u32 row_length) -{ - switch (format) - { - case HostTextureFormat::DXT1: - return row_length / 4 * 8; - - case HostTextureFormat::DXT3: - case HostTextureFormat::DXT5: - return row_length / 4 * 16; - - case HostTextureFormat::RGBA8: - default: - return row_length * 4; - } -} - static DXGI_FORMAT GetDXGIFormatForHostFormat(HostTextureFormat format) { switch (format) @@ -219,8 +202,9 @@ void TextureCache::TCacheEntry::CopyRectangleFromTexture(const TCacheEntryBase* void TextureCache::TCacheEntry::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size) { - u32 src_pitch = GetLevelPitch(config.format, row_length); - D3D::ReplaceRGBATexture2D(m_texture->GetTex12(), buffer, width, height, src_pitch, level, + size_t src_pitch = CalculateHostTextureLevelPitch(config.format, row_length); + D3D::ReplaceRGBATexture2D(m_texture->GetTex12(), buffer, width, height, + static_cast(src_pitch), level, m_texture->GetResourceUsageState()); } diff --git a/Source/Core/VideoBackends/OGL/TextureCache.cpp b/Source/Core/VideoBackends/OGL/TextureCache.cpp index 05261f1d33..2031d7e220 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.cpp +++ b/Source/Core/VideoBackends/OGL/TextureCache.cpp @@ -88,11 +88,6 @@ bool SaveTexture(const std::string& filename, u32 textarget, u32 tex, int virtua return TextureToPng(data.data(), width * 4, filename, width, height, true); } -static bool IsCompressedTextureFormat(HostTextureFormat format) -{ - return format >= HostTextureFormat::DXT1 && format <= HostTextureFormat::DXT5; -} - static GLenum GetGLInternalFormatForTextureFormat(HostTextureFormat format, bool storage) { switch (format) @@ -204,7 +199,7 @@ TextureCache::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConf if (config.rendertarget) { // We can't render to compressed formats. - _assert_(!IsCompressedTextureFormat(config.format)); + _assert_(!IsCompressedHostTextureFormat(config.format)); if (!g_ogl_config.bSupportsTextureStorage) { for (u32 level = 0; level < config.levels; level++) @@ -276,7 +271,7 @@ void TextureCache::TCacheEntry::Load(u32 level, u32 width, u32 height, u32 row_l glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); GLenum gl_internal_format = GetGLInternalFormatForTextureFormat(config.format, false); - if (IsCompressedTextureFormat(config.format)) + if (IsCompressedHostTextureFormat(config.format)) { if (g_ogl_config.bSupportsTextureStorage) { diff --git a/Source/Core/VideoBackends/Vulkan/TextureCache.cpp b/Source/Core/VideoBackends/Vulkan/TextureCache.cpp index fad7e789cd..67faf980ea 100644 --- a/Source/Core/VideoBackends/Vulkan/TextureCache.cpp +++ b/Source/Core/VideoBackends/Vulkan/TextureCache.cpp @@ -373,7 +373,7 @@ void TextureCache::TCacheEntry::Load(u32 level, u32 width, u32 height, u32 row_l u32 upload_alignment = static_cast(g_vulkan_context->GetBufferImageGranularity()); u32 block_size = Util::GetBlockSize(m_texture->GetFormat()); u32 num_rows = Common::AlignUp(height, block_size) / block_size; - size_t source_pitch = Util::GetPitchForTexture(m_texture->GetFormat(), row_length); + size_t source_pitch = CalculateHostTextureLevelPitch(config.format, row_length); size_t upload_size = source_pitch * num_rows; std::unique_ptr temp_buffer; VkBuffer upload_buffer; diff --git a/Source/Core/VideoBackends/Vulkan/Util.cpp b/Source/Core/VideoBackends/Vulkan/Util.cpp index a060cf3485..7ce433a79e 100644 --- a/Source/Core/VideoBackends/Vulkan/Util.cpp +++ b/Source/Core/VideoBackends/Vulkan/Util.cpp @@ -151,32 +151,6 @@ u32 GetBlockSize(VkFormat format) } } - -size_t GetPitchForTexture(VkFormat format, u32 row_length) -{ - switch (format) - { - case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: - return static_cast(std::max(1u, row_length / 4)) * 8; - - case VK_FORMAT_BC2_UNORM_BLOCK: - return static_cast(std::max(1u, row_length / 4)) * 16; - - case VK_FORMAT_BC3_UNORM_BLOCK: - return static_cast(std::max(1u, row_length / 4)) * 16; - - case VK_FORMAT_R8G8B8A8_UNORM: - case VK_FORMAT_B8G8R8A8_UNORM: - case VK_FORMAT_R32_SFLOAT: - case VK_FORMAT_D32_SFLOAT: - return static_cast(row_length) * 4; - - default: - PanicAlert("Unhandled pixel format"); - return row_length; - } -} - VkRect2D ClampRect2D(const VkRect2D& rect, u32 width, u32 height) { VkRect2D out; diff --git a/Source/Core/VideoBackends/Vulkan/Util.h b/Source/Core/VideoBackends/Vulkan/Util.h index f4596b89f7..85d447cf75 100644 --- a/Source/Core/VideoBackends/Vulkan/Util.h +++ b/Source/Core/VideoBackends/Vulkan/Util.h @@ -31,8 +31,6 @@ VkFormat GetVkFormatForHostTextureFormat(HostTextureFormat format); u32 GetTexelSize(VkFormat format); u32 GetBlockSize(VkFormat format); -size_t GetPitchForTexture(VkFormat format, u32 row_length); - // Clamps a VkRect2D to the specified dimensions. VkRect2D ClampRect2D(const VkRect2D& rect, u32 width, u32 height); diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index e3a9daa69d..613f2dfed9 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -47,6 +47,29 @@ TextureCacheBase::TCacheEntryBase::~TCacheEntryBase() { } +bool TextureCacheBase::IsCompressedHostTextureFormat(HostTextureFormat format) +{ + // This will need to be changed if we add any other uncompressed formats. + return format != HostTextureFormat::RGBA8; +} + +size_t TextureCacheBase::CalculateHostTextureLevelPitch(HostTextureFormat format, u32 row_length) +{ + switch (format) + { + case HostTextureFormat::DXT1: + return static_cast(std::max(1u, row_length / 4)) * 8; + + case HostTextureFormat::DXT3: + case HostTextureFormat::DXT5: + return static_cast(std::max(1u, row_length / 4)) * 16; + + case HostTextureFormat::RGBA8: + default: + return static_cast(row_length) * 4; + } +} + void TextureCacheBase::CheckTempSize(size_t required_size) { if (required_size <= temp_size) diff --git a/Source/Core/VideoCommon/TextureCacheBase.h b/Source/Core/VideoCommon/TextureCacheBase.h index f621f7d92a..7f2491a5c7 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.h +++ b/Source/Core/VideoCommon/TextureCacheBase.h @@ -146,6 +146,10 @@ public: virtual ~TextureCacheBase(); // needs virtual for DX11 dtor + // TODO: Move these to AbstractTexture once it is finished. + static bool IsCompressedHostTextureFormat(HostTextureFormat format); + static size_t CalculateHostTextureLevelPitch(HostTextureFormat format, u32 row_length); + void OnConfigChanged(VideoConfig& config); // Removes textures which aren't used for more than TEXTURE_KILL_THRESHOLD frames,