From 7f217a8bb24da323042081a02b65f84f9ce9761f Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 14 Nov 2017 00:21:48 +1000 Subject: [PATCH] Vulkan: Drop StagingTexture2D class as it is now duplicated functionality --- .../Core/VideoBackends/Vulkan/CMakeLists.txt | 1 - .../Vulkan/FramebufferManager.cpp | 64 +++---- .../VideoBackends/Vulkan/FramebufferManager.h | 7 +- .../VideoBackends/Vulkan/StagingTexture2D.cpp | 164 ------------------ .../VideoBackends/Vulkan/StagingTexture2D.h | 58 ------- .../VideoBackends/Vulkan/TextureConverter.cpp | 1 - .../Core/VideoBackends/Vulkan/VKTexture.cpp | 2 +- .../Core/VideoBackends/Vulkan/Vulkan.vcxproj | 2 - 8 files changed, 27 insertions(+), 272 deletions(-) delete mode 100644 Source/Core/VideoBackends/Vulkan/StagingTexture2D.cpp delete mode 100644 Source/Core/VideoBackends/Vulkan/StagingTexture2D.h diff --git a/Source/Core/VideoBackends/Vulkan/CMakeLists.txt b/Source/Core/VideoBackends/Vulkan/CMakeLists.txt index f3d602afb6..25c12fb12c 100644 --- a/Source/Core/VideoBackends/Vulkan/CMakeLists.txt +++ b/Source/Core/VideoBackends/Vulkan/CMakeLists.txt @@ -11,7 +11,6 @@ set(SRCS ShaderCompiler.cpp StateTracker.cpp StagingBuffer.cpp - StagingTexture2D.cpp StreamBuffer.cpp SwapChain.cpp Texture2D.cpp diff --git a/Source/Core/VideoBackends/Vulkan/FramebufferManager.cpp b/Source/Core/VideoBackends/Vulkan/FramebufferManager.cpp index 78689b9638..94f773fb94 100644 --- a/Source/Core/VideoBackends/Vulkan/FramebufferManager.cpp +++ b/Source/Core/VideoBackends/Vulkan/FramebufferManager.cpp @@ -16,7 +16,6 @@ #include "VideoBackends/Vulkan/CommandBufferManager.h" #include "VideoBackends/Vulkan/ObjectCache.h" -#include "VideoBackends/Vulkan/StagingTexture2D.h" #include "VideoBackends/Vulkan/StateTracker.h" #include "VideoBackends/Vulkan/StreamBuffer.h" #include "VideoBackends/Vulkan/Texture2D.h" @@ -698,7 +697,7 @@ u32 FramebufferManager::PeekEFBColor(u32 x, u32 y) return 0; u32 value; - m_color_readback_texture->ReadTexel(x, y, &value, sizeof(value)); + m_color_readback_texture->ReadTexel(x, y, &value); return value; } @@ -711,7 +710,6 @@ bool FramebufferManager::PopulateColorReadbackTexture() // Issue a copy from framebuffer -> copy texture if we have >1xIR or MSAA on. VkRect2D src_region = {{0, 0}, {GetEFBWidth(), GetEFBHeight()}}; Texture2D* src_texture = m_efb_color_texture.get(); - VkImageAspectFlags src_aspect = VK_IMAGE_ASPECT_COLOR_BIT; if (GetEFBSamples() > 1) src_texture = ResolveEFBColorTexture(src_region); @@ -750,9 +748,9 @@ bool FramebufferManager::PopulateColorReadbackTexture() // Copy from EFB or copy texture to staging texture. src_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - m_color_readback_texture->CopyFromImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), - src_texture->GetImage(), src_aspect, 0, 0, EFB_WIDTH, - EFB_HEIGHT, 0, 0); + static_cast(m_color_readback_texture.get()) + ->CopyFromTexture(src_texture, m_color_readback_texture->GetConfig().GetRect(), 0, 0, + m_color_readback_texture->GetConfig().GetRect()); // Restore original layout if we used the EFB as a source. if (src_texture == m_efb_color_texture.get()) @@ -762,12 +760,7 @@ bool FramebufferManager::PopulateColorReadbackTexture() } // Wait until the copy is complete. - Util::ExecuteCurrentCommandsAndRestoreState(false, true); - - // Map to host memory. - if (!m_color_readback_texture->IsMapped() && !m_color_readback_texture->Map()) - return false; - + m_color_readback_texture->Flush(); m_color_readback_texture_valid = true; return true; } @@ -778,7 +771,7 @@ float FramebufferManager::PeekEFBDepth(u32 x, u32 y) return 0.0f; float value; - m_depth_readback_texture->ReadTexel(x, y, &value, sizeof(value)); + m_depth_readback_texture->ReadTexel(x, y, &value); return value; } @@ -791,12 +784,10 @@ bool FramebufferManager::PopulateDepthReadbackTexture() // Issue a copy from framebuffer -> copy texture if we have >1xIR or MSAA on. VkRect2D src_region = {{0, 0}, {GetEFBWidth(), GetEFBHeight()}}; Texture2D* src_texture = m_efb_depth_texture.get(); - VkImageAspectFlags src_aspect = VK_IMAGE_ASPECT_DEPTH_BIT; if (GetEFBSamples() > 1) { // EFB depth resolves are written out as color textures src_texture = ResolveEFBDepthTexture(src_region); - src_aspect = VK_IMAGE_ASPECT_COLOR_BIT; } if (GetEFBWidth() != EFB_WIDTH || GetEFBHeight() != EFB_HEIGHT) { @@ -828,15 +819,14 @@ bool FramebufferManager::PopulateDepthReadbackTexture() // Use this as a source texture now. src_texture = m_depth_copy_texture.get(); - src_aspect = VK_IMAGE_ASPECT_COLOR_BIT; } // Copy from EFB or copy texture to staging texture. src_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - m_depth_readback_texture->CopyFromImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), - src_texture->GetImage(), src_aspect, 0, 0, EFB_WIDTH, - EFB_HEIGHT, 0, 0); + static_cast(m_depth_readback_texture.get()) + ->CopyFromTexture(src_texture, m_depth_readback_texture->GetConfig().GetRect(), 0, 0, + m_depth_readback_texture->GetConfig().GetRect()); // Restore original layout if we used the EFB as a source. if (src_texture == m_efb_depth_texture.get()) @@ -846,12 +836,7 @@ bool FramebufferManager::PopulateDepthReadbackTexture() } // Wait until the copy is complete. - Util::ExecuteCurrentCommandsAndRestoreState(false, true); - - // Map to host memory. - if (!m_depth_readback_texture->IsMapped() && !m_depth_readback_texture->Map()) - return false; - + m_depth_readback_texture->Flush(); m_depth_readback_texture_valid = true; return true; } @@ -1011,32 +996,27 @@ bool FramebufferManager::CreateReadbackTextures() VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); - m_color_readback_texture = StagingTexture2D::Create(STAGING_BUFFER_TYPE_READBACK, EFB_WIDTH, - EFB_HEIGHT, EFB_COLOR_TEXTURE_FORMAT); - if (!m_color_copy_texture || !m_color_readback_texture) - { - ERROR_LOG(VIDEO, "Failed to create EFB color readback texture"); - return false; - } - m_depth_copy_texture = Texture2D::Create(EFB_WIDTH, EFB_HEIGHT, 1, 1, EFB_DEPTH_AS_COLOR_TEXTURE_FORMAT, VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); - m_depth_readback_texture = StagingTexture2D::Create(STAGING_BUFFER_TYPE_READBACK, EFB_WIDTH, - EFB_HEIGHT, EFB_DEPTH_TEXTURE_FORMAT); - if (!m_depth_copy_texture || !m_depth_readback_texture) + if (!m_color_copy_texture || !m_depth_copy_texture) { - ERROR_LOG(VIDEO, "Failed to create EFB depth readback texture"); + ERROR_LOG(VIDEO, "Failed to create EFB copy textures"); return false; } - // With Vulkan, we can leave these textures mapped and use invalidate/flush calls instead. - if (!m_color_readback_texture->Map() || !m_depth_readback_texture->Map()) + TextureConfig readback_texture_config(EFB_WIDTH, EFB_HEIGHT, 1, 1, AbstractTextureFormat::RGBA8, + false); + m_color_readback_texture = + g_renderer->CreateStagingTexture(StagingTextureType::Mutable, readback_texture_config); + m_depth_readback_texture = + g_renderer->CreateStagingTexture(StagingTextureType::Mutable, readback_texture_config); + if (!m_color_readback_texture || !m_depth_readback_texture) { - ERROR_LOG(VIDEO, "Failed to map EFB readback textures"); + ERROR_LOG(VIDEO, "Failed to create EFB readback textures"); return false; } @@ -1113,7 +1093,7 @@ void FramebufferManager::PokeEFBColor(u32 x, u32 y, u32 color) // Update the peek cache if it's valid, since we know the color of the pixel now. if (m_color_readback_texture_valid) - m_color_readback_texture->WriteTexel(x, y, &color, sizeof(color)); + m_color_readback_texture->WriteTexel(x, y, &color); } void FramebufferManager::PokeEFBDepth(u32 x, u32 y, float depth) @@ -1126,7 +1106,7 @@ void FramebufferManager::PokeEFBDepth(u32 x, u32 y, float depth) // Update the peek cache if it's valid, since we know the color of the pixel now. if (m_depth_readback_texture_valid) - m_depth_readback_texture->WriteTexel(x, y, &depth, sizeof(depth)); + m_depth_readback_texture->WriteTexel(x, y, &depth); } void FramebufferManager::CreatePokeVertices(std::vector* destination_list, u32 x, diff --git a/Source/Core/VideoBackends/Vulkan/FramebufferManager.h b/Source/Core/VideoBackends/Vulkan/FramebufferManager.h index 0c1a160728..e765abc907 100644 --- a/Source/Core/VideoBackends/Vulkan/FramebufferManager.h +++ b/Source/Core/VideoBackends/Vulkan/FramebufferManager.h @@ -13,9 +13,10 @@ #include "VideoCommon/FramebufferManagerBase.h" #include "VideoCommon/RenderState.h" +class AbstractStagingTexture; + namespace Vulkan { -class StagingTexture2D; class StateTracker; class StreamBuffer; class Texture2D; @@ -138,8 +139,8 @@ private: VkFramebuffer m_depth_copy_framebuffer = VK_NULL_HANDLE; // CPU-side EFB readback texture - std::unique_ptr m_color_readback_texture; - std::unique_ptr m_depth_readback_texture; + std::unique_ptr m_color_readback_texture; + std::unique_ptr m_depth_readback_texture; bool m_color_readback_texture_valid = false; bool m_depth_readback_texture_valid = false; diff --git a/Source/Core/VideoBackends/Vulkan/StagingTexture2D.cpp b/Source/Core/VideoBackends/Vulkan/StagingTexture2D.cpp deleted file mode 100644 index ee726fc8f2..0000000000 --- a/Source/Core/VideoBackends/Vulkan/StagingTexture2D.cpp +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2016 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#include -#include - -#include "Common/Assert.h" - -#include "VideoBackends/Vulkan/CommandBufferManager.h" -#include "VideoBackends/Vulkan/StagingTexture2D.h" -#include "VideoBackends/Vulkan/Util.h" -#include "VideoBackends/Vulkan/VulkanContext.h" - -namespace Vulkan -{ -StagingTexture2D::StagingTexture2D(STAGING_BUFFER_TYPE type, VkBuffer buffer, VkDeviceMemory memory, - VkDeviceSize size, bool coherent, u32 width, u32 height, - VkFormat format, u32 stride) - : StagingBuffer(type, buffer, memory, size, coherent), m_width(width), m_height(height), - m_format(format), m_texel_size(Util::GetTexelSize(format)), m_row_stride(stride) -{ -} - -StagingTexture2D::~StagingTexture2D() -{ -} - -void StagingTexture2D::ReadTexel(u32 x, u32 y, void* data, size_t data_size) const -{ - _assert_(data_size >= m_texel_size); - - VkDeviceSize offset = y * m_row_stride + x * m_texel_size; - VkDeviceSize map_offset = offset - m_map_offset; - _assert_(offset >= m_map_offset && (map_offset + m_texel_size) <= (m_map_offset + m_map_size)); - - const char* ptr = m_map_pointer + map_offset; - memcpy(data, ptr, data_size); -} - -void StagingTexture2D::WriteTexel(u32 x, u32 y, const void* data, size_t data_size) -{ - _assert_(data_size >= m_texel_size); - - VkDeviceSize offset = y * m_row_stride + x * m_texel_size; - VkDeviceSize map_offset = offset - m_map_offset; - _assert_(offset >= m_map_offset && (map_offset + m_texel_size) <= (m_map_offset + m_map_size)); - - char* ptr = m_map_pointer + map_offset; - memcpy(ptr, data, data_size); -} - -void StagingTexture2D::ReadTexels(u32 x, u32 y, u32 width, u32 height, void* data, - u32 data_stride) const -{ - const char* src_ptr = GetRowPointer(y); - - // Optimal path: same dimensions, same stride. - _assert_((x + width) <= m_width && (y + height) <= m_height); - if (x == 0 && width == m_width && m_row_stride == data_stride) - { - memcpy(data, src_ptr, m_row_stride * height); - return; - } - - u32 copy_size = std::min(width * m_texel_size, data_stride); - char* dst_ptr = reinterpret_cast(data); - for (u32 row = 0; row < height; row++) - { - memcpy(dst_ptr, src_ptr + (x * m_texel_size), copy_size); - src_ptr += m_row_stride; - dst_ptr += data_stride; - } -} - -void StagingTexture2D::WriteTexels(u32 x, u32 y, u32 width, u32 height, const void* data, - u32 data_stride) -{ - char* dst_ptr = GetRowPointer(y); - - // Optimal path: same dimensions, same stride. - _assert_((x + width) <= m_width && (y + height) <= m_height); - if (x == 0 && width == m_width && m_row_stride == data_stride) - { - memcpy(dst_ptr, data, m_row_stride * height); - return; - } - - u32 copy_size = std::min(width * m_texel_size, data_stride); - const char* src_ptr = reinterpret_cast(data); - for (u32 row = 0; row < height; row++) - { - memcpy(dst_ptr + (x * m_texel_size), src_ptr, copy_size); - dst_ptr += m_row_stride; - src_ptr += data_stride; - } -} - -void StagingTexture2D::CopyFromImage(VkCommandBuffer command_buffer, VkImage image, - VkImageAspectFlags src_aspect, u32 x, u32 y, u32 width, - u32 height, u32 level, u32 layer) -{ - // Issue the image->buffer copy. - VkBufferImageCopy image_copy = { - y * m_row_stride + x * m_texel_size, // VkDeviceSize bufferOffset - m_width, // uint32_t bufferRowLength - 0, // uint32_t bufferImageHeight - {src_aspect, level, layer, 1}, // VkImageSubresourceLayers imageSubresource - {static_cast(x), static_cast(y), 0}, // VkOffset3D imageOffset - {width, height, 1} // VkExtent3D imageExtent - }; - vkCmdCopyImageToBuffer(command_buffer, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_buffer, 1, - &image_copy); - - // Flush CPU and GPU caches if not coherent mapping. - VkDeviceSize buffer_flush_offset = y * m_row_stride; - VkDeviceSize buffer_flush_size = height * m_row_stride; - FlushGPUCache(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, - buffer_flush_offset, buffer_flush_size); - InvalidateCPUCache(buffer_flush_offset, buffer_flush_size); -} - -void StagingTexture2D::CopyToImage(VkCommandBuffer command_buffer, VkImage image, - VkImageAspectFlags dst_aspect, u32 x, u32 y, u32 width, - u32 height, u32 level, u32 layer) -{ - // Flush CPU and GPU caches if not coherent mapping. - VkDeviceSize buffer_flush_offset = y * m_row_stride; - VkDeviceSize buffer_flush_size = height * m_row_stride; - FlushCPUCache(buffer_flush_offset, buffer_flush_size); - InvalidateGPUCache(command_buffer, VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, - buffer_flush_offset, buffer_flush_size); - - // Issue the buffer->image copy. - VkBufferImageCopy image_copy = { - y * m_row_stride + x * m_texel_size, // VkDeviceSize bufferOffset - m_width, // uint32_t bufferRowLength - 0, // uint32_t bufferImageHeight - {dst_aspect, level, layer, 1}, // VkImageSubresourceLayers imageSubresource - {static_cast(x), static_cast(y), 0}, // VkOffset3D imageOffset - {width, height, 1} // VkExtent3D imageExtent - }; - vkCmdCopyBufferToImage(command_buffer, m_buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, - &image_copy); -} - -std::unique_ptr StagingTexture2D::Create(STAGING_BUFFER_TYPE type, u32 width, - u32 height, VkFormat format) -{ - // Assume tight packing. - u32 stride = Util::GetTexelSize(format) * width; - u32 size = stride * height; - VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - VkBuffer buffer; - VkDeviceMemory memory; - bool coherent; - if (!AllocateBuffer(type, size, usage, &buffer, &memory, &coherent)) - return nullptr; - - return std::make_unique(type, buffer, memory, size, coherent, width, height, - format, stride); -} -} // namespace Vulkan diff --git a/Source/Core/VideoBackends/Vulkan/StagingTexture2D.h b/Source/Core/VideoBackends/Vulkan/StagingTexture2D.h deleted file mode 100644 index 74dbb8cf07..0000000000 --- a/Source/Core/VideoBackends/Vulkan/StagingTexture2D.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2016 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#pragma once - -#include -#include - -#include "Common/CommonTypes.h" -#include "VideoBackends/Vulkan/Constants.h" -#include "VideoBackends/Vulkan/StagingBuffer.h" - -namespace Vulkan -{ -class StagingTexture2D final : public StagingBuffer -{ -public: - StagingTexture2D(STAGING_BUFFER_TYPE type, VkBuffer buffer, VkDeviceMemory memory, - VkDeviceSize size, bool coherent, u32 width, u32 height, VkFormat format, - u32 stride); - ~StagingTexture2D(); - - u32 GetWidth() const { return m_width; } - u32 GetHeight() const { return m_height; } - VkFormat GetFormat() const { return m_format; } - u32 GetRowStride() const { return m_row_stride; } - u32 GetTexelSize() const { return m_texel_size; } - // Requires Map() to be called first. - const char* GetRowPointer(u32 row) const { return m_map_pointer + row * m_row_stride; } - char* GetRowPointer(u32 row) { return m_map_pointer + row * m_row_stride; } - void ReadTexel(u32 x, u32 y, void* data, size_t data_size) const; - void WriteTexel(u32 x, u32 y, const void* data, size_t data_size); - void ReadTexels(u32 x, u32 y, u32 width, u32 height, void* data, u32 data_stride) const; - void WriteTexels(u32 x, u32 y, u32 width, u32 height, const void* data, u32 data_stride); - - // Assumes that image is in TRANSFER_SRC layout. - // Results are not ready until command_buffer has been executed. - void CopyFromImage(VkCommandBuffer command_buffer, VkImage image, VkImageAspectFlags src_aspect, - u32 x, u32 y, u32 width, u32 height, u32 level, u32 layer); - - // Assumes that image is in TRANSFER_DST layout. - // Buffer is not safe for re-use until after command_buffer has been executed. - void CopyToImage(VkCommandBuffer command_buffer, VkImage image, VkImageAspectFlags dst_aspect, - u32 x, u32 y, u32 width, u32 height, u32 level, u32 layer); - - // Creates the optimal format of image copy. - static std::unique_ptr Create(STAGING_BUFFER_TYPE type, u32 width, u32 height, - VkFormat format); - -protected: - u32 m_width; - u32 m_height; - VkFormat m_format; - u32 m_texel_size; - u32 m_row_stride; -}; -} diff --git a/Source/Core/VideoBackends/Vulkan/TextureConverter.cpp b/Source/Core/VideoBackends/Vulkan/TextureConverter.cpp index e0dacded9e..72995c7a4b 100644 --- a/Source/Core/VideoBackends/Vulkan/TextureConverter.cpp +++ b/Source/Core/VideoBackends/Vulkan/TextureConverter.cpp @@ -19,7 +19,6 @@ #include "VideoBackends/Vulkan/CommandBufferManager.h" #include "VideoBackends/Vulkan/FramebufferManager.h" #include "VideoBackends/Vulkan/ObjectCache.h" -#include "VideoBackends/Vulkan/StagingTexture2D.h" #include "VideoBackends/Vulkan/StateTracker.h" #include "VideoBackends/Vulkan/StreamBuffer.h" #include "VideoBackends/Vulkan/Texture2D.h" diff --git a/Source/Core/VideoBackends/Vulkan/VKTexture.cpp b/Source/Core/VideoBackends/Vulkan/VKTexture.cpp index d3ab16e0d9..59be6a0eba 100644 --- a/Source/Core/VideoBackends/Vulkan/VKTexture.cpp +++ b/Source/Core/VideoBackends/Vulkan/VKTexture.cpp @@ -14,7 +14,7 @@ #include "VideoBackends/Vulkan/CommandBufferManager.h" #include "VideoBackends/Vulkan/FramebufferManager.h" -#include "VideoBackends/Vulkan/StagingTexture2D.h" +#include "VideoBackends/Vulkan/StagingBuffer.h" #include "VideoBackends/Vulkan/StateTracker.h" #include "VideoBackends/Vulkan/Texture2D.h" #include "VideoBackends/Vulkan/Util.h" diff --git a/Source/Core/VideoBackends/Vulkan/Vulkan.vcxproj b/Source/Core/VideoBackends/Vulkan/Vulkan.vcxproj index 3492f7b0f2..1a24ff053b 100644 --- a/Source/Core/VideoBackends/Vulkan/Vulkan.vcxproj +++ b/Source/Core/VideoBackends/Vulkan/Vulkan.vcxproj @@ -46,7 +46,6 @@ - @@ -72,7 +71,6 @@ -