Skip to content

Commit

Permalink
hot-fix: buffers resolve barriers fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
psucien committed Jan 6, 2025
1 parent 6f3c767 commit 5559f35
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
16 changes: 11 additions & 5 deletions src/video_core/buffer_cache/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,23 @@ class Buffer {
return buffer;
}

std::optional<vk::BufferMemoryBarrier2> GetBarrier(vk::AccessFlagBits2 dst_acess_mask,
vk::PipelineStageFlagBits2 dst_stage) {
std::optional<vk::BufferMemoryBarrier2> GetBarrier(
vk::Flags<vk::AccessFlagBits2> dst_acess_mask, vk::PipelineStageFlagBits2 dst_stage,
u32 offset = 0) {
if (dst_acess_mask == access_mask && stage == dst_stage) {
return {};
}

DEBUG_ASSERT(offset < size_bytes);

auto barrier = vk::BufferMemoryBarrier2{
.srcStageMask = stage,
.srcAccessMask = access_mask,
.dstStageMask = dst_stage,
.dstAccessMask = dst_acess_mask,
.buffer = buffer.buffer,
.size = size_bytes,
.offset = offset,
.size = size_bytes - offset,
};
access_mask = dst_acess_mask;
stage = dst_stage;
Expand All @@ -150,8 +154,10 @@ class Buffer {
Vulkan::Scheduler* scheduler;
MemoryUsage usage;
UniqueBuffer buffer;
vk::AccessFlagBits2 access_mask{vk::AccessFlagBits2::eNone};
vk::PipelineStageFlagBits2 stage{vk::PipelineStageFlagBits2::eNone};
vk::Flags<vk::AccessFlagBits2> access_mask{
vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite |
vk::AccessFlagBits2::eTransferRead | vk::AccessFlagBits2::eTransferWrite};
vk::PipelineStageFlagBits2 stage{vk::PipelineStageFlagBits2::eAllCommands};
};

class StreamBuffer : public Buffer {
Expand Down
60 changes: 27 additions & 33 deletions src/video_core/buffer_cache/buffer_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,43 +479,36 @@ void BufferCache::JoinOverlap(BufferId new_buffer_id, BufferId overlap_id,
};
scheduler.EndRendering();
const auto cmdbuf = scheduler.CommandBuffer();
const std::array pre_barriers = {
vk::BufferMemoryBarrier2{
.srcStageMask = vk::PipelineStageFlagBits2::eAllCommands,
.srcAccessMask = vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eTransfer,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.buffer = overlap.Handle(),
.offset = 0,
.size = overlap.SizeBytes(),
},
};
const std::array post_barriers = {
vk::BufferMemoryBarrier2{
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferRead,
.dstStageMask = vk::PipelineStageFlagBits2::eAllCommands,
.dstAccessMask = vk::AccessFlagBits2::eMemoryWrite,
.buffer = overlap.Handle(),
.offset = 0,
.size = overlap.SizeBytes(),
},
vk::BufferMemoryBarrier2{
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eAllCommands,
.dstAccessMask = vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite,
.buffer = new_buffer.Handle(),
.offset = dst_base_offset,
.size = overlap.SizeBytes(),
},
};

boost::container::static_vector<vk::BufferMemoryBarrier2, 2> pre_barriers{};
if (auto src_barrier = overlap.GetBarrier(vk::AccessFlagBits2::eTransferRead,
vk::PipelineStageFlagBits2::eTransfer)) {
pre_barriers.push_back(*src_barrier);
}
if (auto dst_barrier =
new_buffer.GetBarrier(vk::AccessFlagBits2::eTransferWrite,
vk::PipelineStageFlagBits2::eTransfer, dst_base_offset)) {
pre_barriers.push_back(*dst_barrier);
}
cmdbuf.pipelineBarrier2(vk::DependencyInfo{
.dependencyFlags = vk::DependencyFlagBits::eByRegion,
.bufferMemoryBarrierCount = 1,
.bufferMemoryBarrierCount = static_cast<u32>(pre_barriers.size()),
.pBufferMemoryBarriers = pre_barriers.data(),
});

cmdbuf.copyBuffer(overlap.Handle(), new_buffer.Handle(), copy);

boost::container::static_vector<vk::BufferMemoryBarrier2, 2> post_barriers{};
if (auto src_barrier =
overlap.GetBarrier(vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite,
vk::PipelineStageFlagBits2::eAllCommands)) {
post_barriers.push_back(*src_barrier);
}
if (auto dst_barrier = new_buffer.GetBarrier(
vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite,
vk::PipelineStageFlagBits2::eAllCommands, dst_base_offset)) {
post_barriers.push_back(*dst_barrier);
}
cmdbuf.pipelineBarrier2(vk::DependencyInfo{
.dependencyFlags = vk::DependencyFlagBits::eByRegion,
.bufferMemoryBarrierCount = static_cast<u32>(post_barriers.size()),
Expand Down Expand Up @@ -626,7 +619,8 @@ void BufferCache::SynchronizeBuffer(Buffer& buffer, VAddr device_addr, u32 size,
const auto cmdbuf = scheduler.CommandBuffer();
const vk::BufferMemoryBarrier2 pre_barrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eAllCommands,
.srcAccessMask = vk::AccessFlagBits2::eMemoryRead,
.srcAccessMask = vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite |
vk::AccessFlagBits2::eTransferRead | vk::AccessFlagBits2::eTransferWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eTransfer,
.dstAccessMask = vk::AccessFlagBits2::eTransferWrite,
.buffer = buffer.Handle(),
Expand Down

0 comments on commit 5559f35

Please sign in to comment.