From 7a29e8a973577b471d215dfe9afe0a2fc73de035 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Tue, 7 Jan 2025 22:43:26 +0000 Subject: [PATCH] [engine] fixed a crash that could happen if you tried to close the engine while it was running other threads (it will wait for them now) --- runtime/Core/Engine.cpp | 5 +- runtime/RHI/Vulkan/Vulkan_CommandList.cpp | 4 +- runtime/RHI/Vulkan/Vulkan_Device.cpp | 4 +- runtime/Rendering/Renderer_Passes.cpp | 75 +++++++++-------------- 4 files changed, 37 insertions(+), 51 deletions(-) diff --git a/runtime/Core/Engine.cpp b/runtime/Core/Engine.cpp index 90e540714..38d4e08e7 100644 --- a/runtime/Core/Engine.cpp +++ b/runtime/Core/Engine.cpp @@ -95,11 +95,14 @@ namespace Spartan void Engine::Shutdown() { + // the thread pool can hold state from other systems + // so shut it down first (it waits) to avoid crashes due to race conditions + ThreadPool::Shutdown(); + ResourceCache::Shutdown(); World::Shutdown(); Renderer::Shutdown(); Physics::Shutdown(); - ThreadPool::Shutdown(); Event::Shutdown(); Audio::Shutdown(); Window::Shutdown(); diff --git a/runtime/RHI/Vulkan/Vulkan_CommandList.cpp b/runtime/RHI/Vulkan/Vulkan_CommandList.cpp index 923a6a494..77545a171 100644 --- a/runtime/RHI/Vulkan/Vulkan_CommandList.cpp +++ b/runtime/RHI/Vulkan/Vulkan_CommandList.cpp @@ -552,6 +552,8 @@ namespace Spartan m_pso = pso; RHI_Device::GetOrCreatePipeline(m_pso, m_pipeline, m_descriptor_layout_current); + RenderPassBegin(); + // set pipeline { // get vulkan pipeline object @@ -600,8 +602,6 @@ namespace Spartan Renderer::SetStandardResources(this); descriptor_sets::set_dynamic(m_pso, m_rhi_resource, m_pipeline->GetRhiResourceLayout(), m_descriptor_layout_current); } - - RenderPassBegin(); } void RHI_CommandList::RenderPassBegin() diff --git a/runtime/RHI/Vulkan/Vulkan_Device.cpp b/runtime/RHI/Vulkan/Vulkan_Device.cpp index bd1b3d191..461065433 100644 --- a/runtime/RHI/Vulkan/Vulkan_Device.cpp +++ b/runtime/RHI/Vulkan/Vulkan_Device.cpp @@ -445,8 +445,8 @@ namespace Spartan // Access info (usage: SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL, prior_usage: SYNC_IMAGE_LAYOUT_TRANSITION, // write_barriers: 0, queue: VkQueue 0x1d7ba2ef110[graphics], submit: 2988, batch: 0, batch_tag: 91515, command: // vkCmdPipelineBarrier2, command_buffer: VkCommandBuffer 0x1d7bfd21870[cmd_list_1], seq_no: 62, reset_no: 1419). - //if (p_callback_data->messageIdNumber == 0xe17ab4ae || p_callback_data->messageIdNumber == 0x42f2f4ed) - //return VK_FALSE; + if (p_callback_data->messageIdNumber == 0xe17ab4ae || p_callback_data->messageIdNumber == 0x42f2f4ed) + return VK_FALSE; } } diff --git a/runtime/Rendering/Renderer_Passes.cpp b/runtime/Rendering/Renderer_Passes.cpp index d22b20403..e3c553181 100644 --- a/runtime/Rendering/Renderer_Passes.cpp +++ b/runtime/Rendering/Renderer_Passes.cpp @@ -1998,8 +1998,6 @@ namespace Spartan pso.render_target_color_textures[0] = tex_out; cmd_list->SetPipelineState(pso); - cmd_list->SetCullMode(RHI_CullMode::Back); - auto draw_icon = [&cmd_list](Entity* entity, RHI_Texture* texture) { const Vector3 pos_world = entity->GetPosition(); @@ -2130,15 +2128,13 @@ namespace Spartan pso.render_target_depth_texture = GetRenderTarget(Renderer_RenderTarget::gbuffer_depth_output); pso.primitive_toplogy = RHI_PrimitiveTopology::LineList; - // world space rendering - m_pcb_pass_cpu.transform = Matrix::Identity; - cmd_list->PushConstants(m_pcb_pass_cpu); - // draw independent lines const bool draw_lines_depth_off = m_lines_index_depth_off != numeric_limits::max(); const bool draw_lines_depth_on = m_lines_index_depth_on > ((m_line_vertices.size() / 2) - 1); - if (draw_lines_depth_off || draw_lines_depth_on) + if ((draw_lines_depth_off || draw_lines_depth_on) && !m_line_vertices.empty()) { + m_pcb_pass_cpu.transform = Matrix::Identity; + cmd_list->PushConstants(m_pcb_pass_cpu); cmd_list->SetCullMode(RHI_CullMode::None); // grow vertex buffer (if needed) @@ -2148,49 +2144,36 @@ namespace Spartan m_vertex_buffer_lines = make_shared(RHI_Buffer_Type::Vertex, sizeof(m_line_vertices[0]), vertex_count, static_cast(&m_line_vertices[0]), true, "lines"); } - if (vertex_count != 0) - { - RHI_Vertex_PosCol* buffer = static_cast(m_vertex_buffer_lines->GetMappedData()); - - // update vertex buffer - { - // zero out the entire buffer first - this is because the buffer grows but doesn't shrink back (so it can hold previous data) - memset(buffer, 0, m_vertex_buffer_lines->GetObjectSize()); - copy(m_line_vertices.begin(), m_line_vertices.end(), buffer); - } - - // depth off - if (draw_lines_depth_off) - { - cmd_list->BeginMarker("depth_off"); + // update vertex buffer + RHI_Vertex_PosCol* buffer = static_cast(m_vertex_buffer_lines->GetMappedData()); + memset(buffer, 0, m_vertex_buffer_lines->GetObjectSize()); + copy(m_line_vertices.begin(), m_line_vertices.end(), buffer); - // set pipeline state - pso.blend_state = GetBlendState(Renderer_BlendState::Off); - pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::Off); - cmd_list->SetPipelineState(pso); - - cmd_list->SetBufferVertex(m_vertex_buffer_lines.get()); - cmd_list->Draw(m_lines_index_depth_off + 1); - - cmd_list->EndMarker(); - } - - // depth on - if (m_lines_index_depth_on > (vertex_count / 2) - 1) - { - cmd_list->BeginMarker("depth_on"); - - // set pipeline state - pso.blend_state = GetBlendState(Renderer_BlendState::Alpha); - pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::ReadGreaterEqual); - cmd_list->SetPipelineState(pso); + // depth off + if (draw_lines_depth_off) + { + // set pipeline state + pso.blend_state = GetBlendState(Renderer_BlendState::Off); + pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::Off); + cmd_list->SetPipelineState(pso); + + cmd_list->SetBufferVertex(m_vertex_buffer_lines.get()); + cmd_list->Draw(m_lines_index_depth_off + 1); + } - cmd_list->SetBufferVertex(m_vertex_buffer_lines.get()); - cmd_list->Draw((m_lines_index_depth_on - (vertex_count / 2)) + 1, vertex_count / 2); + // depth on + if (m_lines_index_depth_on > (vertex_count / 2) - 1) + { + // set pipeline state + pso.blend_state = GetBlendState(Renderer_BlendState::Alpha); + pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::ReadGreaterEqual); + cmd_list->SetPipelineState(pso); - cmd_list->EndMarker(); - } + cmd_list->SetBufferVertex(m_vertex_buffer_lines.get()); + cmd_list->Draw((m_lines_index_depth_on - (vertex_count / 2)) + 1, vertex_count / 2); } + + cmd_list->SetCullMode(RHI_CullMode::Back); } m_lines_index_depth_off = numeric_limits::max(); // max +1 will wrap it to 0