Vulkan: Drop StagingTexture2D class as it is now duplicated functionality

This commit is contained in:
Stenzek 2017-11-14 00:21:48 +10:00
parent db1d9de933
commit 7f217a8bb2
8 changed files with 27 additions and 272 deletions

View File

@ -11,7 +11,6 @@ set(SRCS
ShaderCompiler.cpp
StateTracker.cpp
StagingBuffer.cpp
StagingTexture2D.cpp
StreamBuffer.cpp
SwapChain.cpp
Texture2D.cpp

View File

@ -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<VKStagingTexture*>(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<VKStagingTexture*>(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<EFBPokeVertex>* destination_list, u32 x,

View File

@ -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<StagingTexture2D> m_color_readback_texture;
std::unique_ptr<StagingTexture2D> m_depth_readback_texture;
std::unique_ptr<AbstractStagingTexture> m_color_readback_texture;
std::unique_ptr<AbstractStagingTexture> m_depth_readback_texture;
bool m_color_readback_texture_valid = false;
bool m_depth_readback_texture_valid = false;

View File

@ -1,164 +0,0 @@
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <cstring>
#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<char*>(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<const char*>(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<s32>(x), static_cast<s32>(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<s32>(x), static_cast<s32>(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> 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<StagingTexture2D>(type, buffer, memory, size, coherent, width, height,
format, stride);
}
} // namespace Vulkan

View File

@ -1,58 +0,0 @@
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <cstddef>
#include <memory>
#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<StagingTexture2D> 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;
};
}

View File

@ -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"

View File

@ -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"

View File

@ -46,7 +46,6 @@
<ClCompile Include="PerfQuery.cpp" />
<ClCompile Include="RasterFont.cpp" />
<ClCompile Include="StagingBuffer.cpp" />
<ClCompile Include="StagingTexture2D.cpp" />
<ClCompile Include="Util.cpp" />
<ClCompile Include="VertexFormat.cpp" />
<ClCompile Include="ObjectCache.cpp" />
@ -72,7 +71,6 @@
<ClInclude Include="TextureConverter.h" />
<ClInclude Include="RasterFont.h" />
<ClInclude Include="StagingBuffer.h" />
<ClInclude Include="StagingTexture2D.h" />
<ClInclude Include="Util.h" />
<ClInclude Include="VertexFormat.h" />
<ClInclude Include="PerfQuery.h" />