summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Vulkan/StateTracker.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Core/VideoBackends/Vulkan/StateTracker.cpp')
-rw-r--r--Source/Core/VideoBackends/Vulkan/StateTracker.cpp105
1 files changed, 43 insertions, 62 deletions
diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp
index c7c4c77c54..700f1d1057 100644
--- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp
+++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp
@@ -357,20 +357,9 @@ bool StateTracker::CheckForShaderChanges(u32 gx_primitive_type, DSTALPHA_MODE ds
void StateTracker::UpdateVertexShaderConstants()
{
- if (!VertexShaderManager::dirty)
+ if (!VertexShaderManager::dirty || !ReserveConstantStorage())
return;
- // Since the other stages uniform buffers' may be still be using the earlier data,
- // we can't reuse the earlier part of the buffer without re-uploading everything.
- if (!m_uniform_stream_buffer->ReserveMemory(m_uniform_buffer_reserve_size,
- g_vulkan_context->GetUniformBufferAlignment(), false,
- false, false))
- {
- // Re-upload all constants to a new portion of the buffer.
- UploadAllConstants();
- return;
- }
-
// Buffer allocation changed?
if (m_uniform_stream_buffer->GetBuffer() !=
m_bindings.uniform_buffer_bindings[UBO_DESCRIPTOR_SET_BINDING_VS].buffer)
@@ -394,17 +383,9 @@ void StateTracker::UpdateVertexShaderConstants()
void StateTracker::UpdateGeometryShaderConstants()
{
// Skip updating geometry shader constants if it's not in use.
- if (m_pipeline_state.gs == VK_NULL_HANDLE || !GeometryShaderManager::dirty)
- return;
-
- // Since the other stages uniform buffers' may be still be using the earlier data,
- // we can't reuse the earlier part of the buffer without re-uploading everything.
- if (!m_uniform_stream_buffer->ReserveMemory(m_uniform_buffer_reserve_size,
- g_vulkan_context->GetUniformBufferAlignment(), false,
- false, false))
+ if (m_pipeline_state.gs == VK_NULL_HANDLE || !GeometryShaderManager::dirty ||
+ !ReserveConstantStorage())
{
- // Re-upload all constants to a new portion of the buffer.
- UploadAllConstants();
return;
}
@@ -430,20 +411,9 @@ void StateTracker::UpdateGeometryShaderConstants()
void StateTracker::UpdatePixelShaderConstants()
{
- if (!PixelShaderManager::dirty)
+ if (!PixelShaderManager::dirty || !ReserveConstantStorage())
return;
- // Since the other stages uniform buffers' may be still be using the earlier data,
- // we can't reuse the earlier part of the buffer without re-uploading everything.
- if (!m_uniform_stream_buffer->ReserveMemory(m_uniform_buffer_reserve_size,
- g_vulkan_context->GetUniformBufferAlignment(), false,
- false, false))
- {
- // Re-upload all constants to a new portion of the buffer.
- UploadAllConstants();
- return;
- }
-
// Buffer allocation changed?
if (m_uniform_stream_buffer->GetBuffer() !=
m_bindings.uniform_buffer_bindings[UBO_DESCRIPTOR_SET_BINDING_PS].buffer)
@@ -464,33 +434,44 @@ void StateTracker::UpdatePixelShaderConstants()
PixelShaderManager::dirty = false;
}
+bool StateTracker::ReserveConstantStorage()
+{
+ // Since we invalidate all constants on command buffer execution, it doesn't matter if this
+ // causes the stream buffer to be resized.
+ if (m_uniform_stream_buffer->ReserveMemory(m_uniform_buffer_reserve_size,
+ g_vulkan_context->GetUniformBufferAlignment(), true,
+ true, false))
+ {
+ return true;
+ }
+
+ // The only places that call constant updates are safe to have state restored.
+ WARN_LOG(VIDEO, "Executing command buffer while waiting for space in uniform buffer");
+ Util::ExecuteCurrentCommandsAndRestoreState(false);
+
+ // Since we are on a new command buffer, all constants have been invalidated, and we need
+ // to reupload them. We may as well do this now, since we're issuing a draw anyway.
+ UploadAllConstants();
+ return false;
+}
+
void StateTracker::UploadAllConstants()
{
// We are free to re-use parts of the buffer now since we're uploading all constants.
+ size_t ub_alignment = g_vulkan_context->GetUniformBufferAlignment();
size_t pixel_constants_offset = 0;
size_t vertex_constants_offset =
- Util::AlignValue(pixel_constants_offset + sizeof(PixelShaderConstants),
- g_vulkan_context->GetUniformBufferAlignment());
+ Util::AlignValue(pixel_constants_offset + sizeof(PixelShaderConstants), ub_alignment);
size_t geometry_constants_offset =
- Util::AlignValue(vertex_constants_offset + sizeof(VertexShaderConstants),
- g_vulkan_context->GetUniformBufferAlignment());
- size_t total_allocation_size = geometry_constants_offset + sizeof(GeometryShaderConstants);
+ Util::AlignValue(vertex_constants_offset + sizeof(VertexShaderConstants), ub_alignment);
+ size_t allocation_size = geometry_constants_offset + sizeof(GeometryShaderConstants);
// Allocate everything at once.
- if (!m_uniform_stream_buffer->ReserveMemory(
- total_allocation_size, g_vulkan_context->GetUniformBufferAlignment(), true, true, false))
+ // We should only be here if the buffer was full and a command buffer was submitted anyway.
+ if (!m_uniform_stream_buffer->ReserveMemory(allocation_size, ub_alignment, true, true, false))
{
- // If this fails, wait until the GPU has caught up.
- // The only places that call constant updates are safe to have state restored.
- WARN_LOG(VIDEO, "Executing command list while waiting for space in uniform buffer");
- Util::ExecuteCurrentCommandsAndRestoreState(false);
- if (!m_uniform_stream_buffer->ReserveMemory(total_allocation_size,
- g_vulkan_context->GetUniformBufferAlignment(), true,
- true, false))
- {
- PanicAlert("Failed to allocate space for constants in streaming buffer");
- return;
- }
+ PanicAlert("Failed to allocate space for constants in streaming buffer");
+ return;
}
// Update bindings
@@ -528,7 +509,7 @@ void StateTracker::UploadAllConstants()
&GeometryShaderManager::constants, sizeof(GeometryShaderConstants));
// Finally, flush buffer memory after copying
- m_uniform_stream_buffer->CommitMemory(total_allocation_size);
+ m_uniform_stream_buffer->CommitMemory(allocation_size);
// Clear dirty flags
VertexShaderManager::dirty = false;
@@ -616,6 +597,13 @@ void StateTracker::InvalidateDescriptorSets()
m_dirty_flags &= ~DIRTY_FLAG_PS_SSBO;
}
+void StateTracker::InvalidateConstants()
+{
+ VertexShaderManager::dirty = true;
+ GeometryShaderManager::dirty = true;
+ PixelShaderManager::dirty = true;
+}
+
void StateTracker::SetPendingRebind()
{
m_dirty_flags |= DIRTY_FLAG_DYNAMIC_OFFSETS | DIRTY_FLAG_DESCRIPTOR_SET_BINDING |
@@ -708,11 +696,7 @@ bool StateTracker::Bind(bool rebind_all /*= false*/)
{
// We can fail to allocate descriptors if we exhaust the pool for this command buffer.
WARN_LOG(VIDEO, "Failed to get a descriptor set, executing buffer");
-
- // Try again after executing the current buffer.
- g_command_buffer_mgr->ExecuteCommandBuffer(false, false);
- InvalidateDescriptorSets();
- SetPendingRebind();
+ Util::ExecuteCurrentCommandsAndRestoreState(false, false);
if (!UpdateDescriptorSet())
{
// Something strange going on.
@@ -775,10 +759,7 @@ void StateTracker::OnDraw()
m_scheduled_command_buffer_kicks.end(), m_draw_counter))
{
// Kick a command buffer on the background thread.
- EndRenderPass();
- g_command_buffer_mgr->ExecuteCommandBuffer(true, false);
- InvalidateDescriptorSets();
- SetPendingRebind();
+ Util::ExecuteCurrentCommandsAndRestoreState(true);
}
}