fix: render-target aliases were synchronously read back and copied through host memory twice

This commit is contained in:
nmzik
2026-07-21 07:37:52 +02:00
parent 42f634a751
commit 728b5b7d01
6 changed files with 116 additions and 51 deletions
+9 -8
View File
@@ -49,14 +49,15 @@ struct GraphicContext: public VulkanInstance {
};
struct VulkanMemory {
vk::MemoryRequirements requirements = {};
vk::MemoryPropertyFlags property = {};
vk::DeviceMemory memory = nullptr;
VmaAllocation allocation = nullptr;
VmaAllocationInfo allocation_info = {};
vk::DeviceSize offset = 0;
uint32_t type = 0;
uint64_t unique_id = 0;
vk::MemoryRequirements requirements = {};
vk::MemoryPropertyFlags property = {};
vk::MemoryPropertyFlags preferred_property = {};
vk::DeviceMemory memory = nullptr;
VmaAllocation allocation = nullptr;
VmaAllocationInfo allocation_info = {};
vk::DeviceSize offset = 0;
uint32_t type = 0;
uint64_t unique_id = 0;
};
enum class VulkanImageType {
+27 -30
View File
@@ -3015,11 +3015,9 @@ void TextureCache::SynchronizeColorImageToBufferLocked(CachedImage& cached, uint
}
// This is the CPU Tiler backend for the image-to-buffer synchronization seam.
// The vectors and Vulkan staging allocation retain capacity; a future PS5 GPU tiler can replace
// this block without changing alias classification or ownership transitions.
// The guest vector and Vulkan staging allocation retain capacity; a future PS5 GPU tiler can
// replace this block without changing alias classification or ownership transitions.
Transfer::WaitForGraphicsIdle(cached.ctx);
m_buffer_transition_linear.resize(target.size);
std::fill(m_buffer_transition_linear.begin(), m_buffer_transition_linear.end(), 0);
std::vector<ImageBufferCopy> regions;
if (storage) {
auto layout = TextureCalcUploadLayout(
@@ -3040,24 +3038,23 @@ void TextureCache::SynchronizeColorImageToBufferLocked(CachedImage& cached, uint
regions = Transfer::MakeLayeredImageBufferCopies(target.layers, slice_size, target.pitch,
target.width, target.height);
}
Transfer::DownloadImage(cached.ctx, m_buffer_transition_linear.data(), target.size, regions,
cached.image, cached.image->layout);
if (storage) {
m_buffer_transition_guest.resize(target.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), m_buffer_transition_linear.data(),
cached.info);
Libs::LibKernel::Memory::WriteBacking(target.address, m_buffer_transition_guest.data(),
target.size);
} else if (tiled) {
m_buffer_transition_guest.resize(target.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), m_buffer_transition_linear.data(),
target);
Libs::LibKernel::Memory::WriteBacking(target.address, m_buffer_transition_guest.data(),
target.size);
} else {
Libs::LibKernel::Memory::WriteBacking(target.address, m_buffer_transition_linear.data(),
target.size);
}
Transfer::ProcessDownloadedImage(
cached.ctx, target.size, regions, cached.image, cached.image->layout,
[&](std::span<const uint8_t> linear) {
if (storage) {
m_buffer_transition_guest.resize(target.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), linear.data(), cached.info);
Libs::LibKernel::Memory::WriteBacking(
target.address, m_buffer_transition_guest.data(), target.size);
} else if (tiled) {
m_buffer_transition_guest.resize(target.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), linear.data(), target);
Libs::LibKernel::Memory::WriteBacking(
target.address, m_buffer_transition_guest.data(), target.size);
} else {
Libs::LibKernel::Memory::WriteBacking(target.address, linear.data(), target.size);
}
});
m_memory_tracker.ForEachDownloadRange<true>(target.address, target.size,
[](uint64_t, uint64_t) noexcept {});
// Only the impending buffer-write range needs publication into BufferCache ownership. The
@@ -3104,16 +3101,16 @@ void TextureCache::SynchronizeDepthImageToBufferLocked(CachedImage& cached, uint
has_stencil, has_htile, cached.gpu_modified, cached.buffer_modified);
}
Transfer::WaitForGraphicsIdle(cached.ctx);
m_buffer_transition_linear.resize(info.size);
std::fill(m_buffer_transition_linear.begin(), m_buffer_transition_linear.end(), 0);
const auto regions = Transfer::MakeLayeredImageBufferCopies(
1, info.size, info.pitch, info.width, info.height, vk::ImageAspectFlagBits::eDepth);
Transfer::DownloadImage(cached.ctx, m_buffer_transition_linear.data(), info.size, regions,
cached.image, cached.image->layout);
m_buffer_transition_guest.resize(info.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), m_buffer_transition_linear.data(), info);
Libs::LibKernel::Memory::WriteBacking(info.address, m_buffer_transition_guest.data(),
info.size);
Transfer::ProcessDownloadedImage(
cached.ctx, info.size, regions, cached.image, cached.image->layout,
[&](std::span<const uint8_t> linear) {
m_buffer_transition_guest.resize(info.size);
m_tiler.TileImage(m_buffer_transition_guest.data(), linear.data(), info);
Libs::LibKernel::Memory::WriteBacking(info.address, m_buffer_transition_guest.data(),
info.size);
});
m_memory_tracker.ForEachDownloadRange<true>(info.address, info.size,
[](uint64_t, uint64_t) noexcept {});
m_buffer_cache.PublishImageBacking(write_address, write_size);
@@ -176,7 +176,6 @@ private:
ImageOwnerIndex m_image_owner_index;
std::map<uint64_t, MetaDataInfo> m_surface_metas;
std::unique_ptr<ReadbackWorker> m_readback;
std::vector<uint8_t> m_buffer_transition_linear;
std::vector<uint8_t> m_buffer_transition_guest;
};
+66 -12
View File
@@ -351,12 +351,28 @@ public:
void DownloadFromImage(GraphicContext* ctx, void* dst_data, uint64_t size,
std::span<const ImageBufferCopy> regions, VulkanImage* src_image,
vk::ImageLayout src_layout) {
Common::LockGuard lock(m_mutex);
EnsureBuffer(ctx, size, vk::BufferUsageFlagBits::eTransferDst);
ExecuteImmediateCommands([&](CommandBuffer* command, vk::CommandBuffer) {
RecordImageToBuffer(*command, *src_image, m_buffer, regions, src_layout);
});
std::memcpy(dst_data, m_mapped_data, size);
WithDownloadedImage(ctx, size, regions, src_image, src_layout,
[&](std::span<const uint8_t> data) {
std::memcpy(dst_data, data.data(), data.size());
});
}
void ProcessDownloadedImage(GraphicContext* ctx, uint64_t size,
std::span<const ImageBufferCopy> regions,
VulkanImage* src_image, vk::ImageLayout src_layout,
const DownloadedImageConsumer& consumer) {
WithDownloadedImage(ctx, size, regions, src_image, src_layout,
[&](std::span<const uint8_t> data) {
if (m_host_cached) {
consumer(data);
return;
}
// Scattered CPU tiler reads can be much slower from uncached mapped
// memory. Preserve the original sequential-copy path as a fallback.
m_cached_readback.resize(data.size());
std::memcpy(m_cached_readback.data(), data.data(), data.size());
consumer(m_cached_readback);
});
}
void Release(GraphicContext* ctx) {
@@ -366,11 +382,26 @@ public:
VulkanUnmapMemory(ctx, &m_buffer.memory);
VulkanDeleteBuffer(ctx, &m_buffer);
}
m_capacity = 0;
m_mapped_data = nullptr;
m_capacity = 0;
m_mapped_data = nullptr;
m_host_cached = false;
}
private:
template <typename Consumer>
void WithDownloadedImage(GraphicContext* ctx, uint64_t size,
std::span<const ImageBufferCopy> regions,
VulkanImage* src_image, vk::ImageLayout src_layout,
const Consumer& consumer) {
Common::LockGuard lock(m_mutex);
EnsureBuffer(ctx, size, vk::BufferUsageFlagBits::eTransferDst);
ExecuteImmediateCommands([&](CommandBuffer* command, vk::CommandBuffer) {
RecordImageToBuffer(*command, *src_image, m_buffer, regions, src_layout);
});
consumer(std::span<const uint8_t>(static_cast<const uint8_t*>(m_mapped_data),
static_cast<size_t>(size)));
}
template <bool WaitIdle, typename Recorder>
void RecordUpload(GraphicContext* ctx, const void* src_data, uint64_t size,
const Recorder& recorder) {
@@ -400,17 +431,30 @@ private:
m_buffer.usage = usage;
m_buffer.memory.property = vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent;
// CPU readback benefits from cached host memory. Keep it optional so Vulkan devices
// without a coherent cached type can fall back to the original sequential-copy path.
m_buffer.memory.preferred_property =
usage & vk::BufferUsageFlagBits::eTransferDst
? vk::MemoryPropertyFlags(vk::MemoryPropertyFlagBits::eHostCached)
: vk::MemoryPropertyFlags {};
VulkanCreateBuffer(ctx, size, &m_buffer);
m_capacity = size;
const auto& properties = ctx->GetPhysicalDeviceMemoryProperties();
EXIT_IF(m_buffer.memory.type >= properties.memoryTypeCount);
m_host_cached =
static_cast<bool>(properties.memoryTypes[m_buffer.memory.type].propertyFlags &
vk::MemoryPropertyFlagBits::eHostCached);
VulkanMapMemory(ctx, &m_buffer.memory, &m_mapped_data);
}
}
Common::Mutex m_mutex;
VulkanBuffer m_buffer;
uint64_t m_capacity = 0;
void* m_mapped_data = nullptr;
Common::Mutex m_mutex;
VulkanBuffer m_buffer;
uint64_t m_capacity = 0;
void* m_mapped_data = nullptr;
bool m_host_cached = false;
std::vector<uint8_t> m_cached_readback;
};
// Do not replace with one
@@ -927,6 +971,16 @@ void DownloadImage(GraphicContext* ctx, void* dst_data, uint64_t size,
->DownloadFromImage(ctx, dst_data, size, regions, src_image, src_layout);
}
void ProcessDownloadedImage(GraphicContext* ctx, uint64_t size,
std::span<const ImageBufferCopy> regions, VulkanImage* src_image,
vk::ImageLayout src_layout,
const DownloadedImageConsumer& consumer) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(size == 0 || regions.empty() || !consumer);
GetStagingBuffer(StagingBufferType::ReadBack)
->ProcessDownloadedImage(ctx, size, regions, src_image, src_layout, consumer);
}
void UploadImage(GraphicContext* ctx, VulkanImage* image, const void* src_data, uint64_t size,
std::span<const BufferImageCopy> regions, vk::ImageLayout dst_layout) {
EXIT_IF(size == 0 || regions.empty());
+10
View File
@@ -5,6 +5,7 @@
#include "common/common.h"
#include "graphics/host_gpu/vulkanCommon.h"
#include <functional>
#include <span>
#include <utility>
#include <vector>
@@ -73,6 +74,8 @@ MakeLayeredImageBufferCopies(uint32_t layers, uint64_t slice_size, uint32_t pitc
enum class StagingBufferType { Texture, Vertex, ReadBack };
using DownloadedImageConsumer = std::function<void(std::span<const uint8_t>)>;
class ScratchBuffer {
public:
explicit ScratchBuffer(uint64_t size);
@@ -107,6 +110,13 @@ void DownloadImage(GraphicContext* ctx, void* dst_data, uint64_t size, uint32_t
void DownloadImage(GraphicContext* ctx, void* dst_data, uint64_t size,
std::span<const ImageBufferCopy> regions, VulkanImage* src_image,
vk::ImageLayout src_layout);
// Invokes consumer synchronously after the image copy fence while the readback staging buffer is
// reserved. The supplied span is valid only for the call; the consumer must not retain it or
// re-enter Transfer.
void ProcessDownloadedImage(GraphicContext* ctx, uint64_t size,
std::span<const ImageBufferCopy> regions, VulkanImage* src_image,
vk::ImageLayout src_layout,
const DownloadedImageConsumer& consumer);
void UploadBuffer(GraphicContext* ctx, StagingBufferType type, VulkanBuffer* dst_buffer,
uint64_t dst_offset, const void* src_data, uint64_t size);
void CopyBuffer(VulkanBuffer* src_buffer, VulkanBuffer* dst_buffer, uint64_t size);
+4
View File
@@ -127,6 +127,8 @@ void VulkanCreateBuffer(GraphicContext* ctx, uint64_t size, VulkanBuffer* buffer
VmaAllocationCreateInfo alloc_info {};
alloc_info.requiredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer->memory.property);
alloc_info.preferredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer->memory.preferred_property);
vk::Buffer::CType native_buffer = VK_NULL_HANDLE;
const auto result = static_cast<vk::Result>(vmaCreateBuffer(
@@ -170,6 +172,8 @@ bool VulkanCreateImage(GraphicContext* ctx, const vk::ImageCreateInfo& image_inf
auto& memory = image->memory;
VmaAllocationCreateInfo alloc_info {};
alloc_info.requiredFlags = static_cast<vk::MemoryPropertyFlags::MaskType>(memory.property);
alloc_info.preferredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(memory.preferred_property);
vk::Image::CType native_image = VK_NULL_HANDLE;
const auto result = static_cast<vk::Result>(vmaCreateImage(