From 3adeacb78d6c313020ea6f2562f76856f9431ec1 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 30 Nov 2016 22:31:41 +1000 Subject: Vulkan: Fix case where uniforms could be overwritten If a draw caused a command buffer submission, the current uniform storage should not be used for the new command buffer. --- Source/Core/VideoBackends/Vulkan/StateTracker.cpp | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'Source/Core/VideoBackends/Vulkan/StateTracker.cpp') diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index c7c4c77c54..40a32b0087 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp @@ -467,26 +467,21 @@ void StateTracker::UpdatePixelShaderConstants() 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)) + 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"); + WARN_LOG(VIDEO, "Executing command buffer 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)) + if (!m_uniform_stream_buffer->ReserveMemory(allocation_size, ub_alignment, true, true, false)) { PanicAlert("Failed to allocate space for constants in streaming buffer"); return; @@ -528,7 +523,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 +611,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 | -- cgit v1.2.3 From 6a4eba1153b7fc858aca895f17b57dc03f089860 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 30 Nov 2016 22:34:36 +1000 Subject: Vulkan: Replace explicit command buffer submits with wrapper function Should we ever introduce anything else that has to be done when a command buffer is executed (e.g. invalidating constants from previous commit), we don't have to update all the callers. --- Source/Core/VideoBackends/Vulkan/StateTracker.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'Source/Core/VideoBackends/Vulkan/StateTracker.cpp') diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index 40a32b0087..67583782c9 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp @@ -710,11 +710,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. @@ -777,10 +773,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); } } -- cgit v1.2.3 From ca691a9d95c437334cfeb8321a544ed36d17849b Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 30 Nov 2016 23:32:23 +1000 Subject: Vulkan: Allow re-use of uniform buffers when doing per-stage uploads This is safe now because we invalidate the pointers after submitting a command buffer. --- Source/Core/VideoBackends/Vulkan/StateTracker.cpp | 70 +++++++++-------------- 1 file changed, 28 insertions(+), 42 deletions(-) (limited to 'Source/Core/VideoBackends/Vulkan/StateTracker.cpp') diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index 67583782c9..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,6 +434,27 @@ 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. @@ -476,16 +467,11 @@ void StateTracker::UploadAllConstants() size_t allocation_size = geometry_constants_offset + sizeof(GeometryShaderConstants); // Allocate everything at once. + // 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)) { - // 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); - if (!m_uniform_stream_buffer->ReserveMemory(allocation_size, ub_alignment, 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 -- cgit v1.2.3