Vulkan: Simplify command buffer fence tracking

This commit is contained in:
Stenzek
2019-03-17 15:59:22 +10:00
parent f3fadd7302
commit 427dd45151
11 changed files with 138 additions and 208 deletions

View File

@ -674,11 +674,7 @@ VKStagingTexture::VKStagingTexture(StagingTextureType type, const TextureConfig&
{
}
VKStagingTexture::~VKStagingTexture()
{
if (m_needs_flush)
VKStagingTexture::Flush();
}
VKStagingTexture::~VKStagingTexture() = default;
std::unique_ptr<VKStagingTexture> VKStagingTexture::Create(StagingTextureType type,
const TextureConfig& config)
@ -739,14 +735,6 @@ void VKStagingTexture::CopyFromTexture(const AbstractTexture* src,
ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= m_config.width &&
dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= m_config.height);
if (m_needs_flush)
{
// Drop copy before reusing it.
g_command_buffer_mgr->RemoveFenceSignaledCallback(this);
m_flush_fence = VK_NULL_HANDLE;
m_needs_flush = false;
}
StateTracker::GetInstance()->EndRenderPass();
VkImageLayout old_layout = src_tex->GetLayout();
@ -773,16 +761,7 @@ void VKStagingTexture::CopyFromTexture(const AbstractTexture* src,
src_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_layout);
m_needs_flush = true;
m_flush_fence = g_command_buffer_mgr->GetCurrentCommandBufferFence();
g_command_buffer_mgr->AddFenceSignaledCallback(this, [this](VkFence fence) {
if (m_flush_fence != fence)
return;
m_flush_fence = VK_NULL_HANDLE;
m_needs_flush = false;
g_command_buffer_mgr->RemoveFenceSignaledCallback(this);
m_staging_buffer->InvalidateCPUCache();
});
m_flush_fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter();
}
void VKStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
@ -798,14 +777,6 @@ void VKStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, A
ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= dst_tex->GetWidth() &&
dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= dst_tex->GetHeight());
if (m_needs_flush)
{
// Drop copy before reusing it.
g_command_buffer_mgr->RemoveFenceSignaledCallback(this);
m_flush_fence = VK_NULL_HANDLE;
m_needs_flush = false;
}
// Flush caches before copying.
m_staging_buffer->FlushCPUCache();
StateTracker::GetInstance()->EndRenderPass();
@ -833,15 +804,7 @@ void VKStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, A
dst_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_layout);
m_needs_flush = true;
m_flush_fence = g_command_buffer_mgr->GetCurrentCommandBufferFence();
g_command_buffer_mgr->AddFenceSignaledCallback(this, [this](VkFence fence) {
if (m_flush_fence != fence)
return;
m_flush_fence = VK_NULL_HANDLE;
m_needs_flush = false;
g_command_buffer_mgr->RemoveFenceSignaledCallback(this);
});
m_flush_fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter();
}
bool VKStagingTexture::Map()
@ -860,25 +823,23 @@ void VKStagingTexture::Flush()
if (!m_needs_flush)
return;
// Either of the below two calls will cause the callback to fire.
g_command_buffer_mgr->RemoveFenceSignaledCallback(this);
if (m_flush_fence == g_command_buffer_mgr->GetCurrentCommandBufferFence())
// Is this copy in the current command buffer?
if (g_command_buffer_mgr->GetCurrentFenceCounter() == m_flush_fence_counter)
{
// The readback is in the current command buffer, and we must execute it.
// Execute the command buffer and wait for it to finish.
Renderer::GetInstance()->ExecuteCommandBuffer(false, true);
}
else
{
// WaitForFence should fire the callback.
g_command_buffer_mgr->WaitForFence(m_flush_fence);
// Wait for the GPU to finish with it.
g_command_buffer_mgr->WaitForFenceCounter(m_flush_fence_counter);
}
DEBUG_ASSERT(m_flush_fence == VK_NULL_HANDLE);
m_needs_flush = false;
// For readback textures, invalidate the CPU cache as there is new data there.
if (m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable)
m_staging_buffer->InvalidateCPUCache();
m_needs_flush = false;
}
VKFramebuffer::VKFramebuffer(VKTexture* color_attachment, VKTexture* depth_attachment, u32 width,