Skip to content

Commit

Permalink
[engine] fixed a crash that could happen if you tried to close the en…
Browse files Browse the repository at this point in the history
…gine while it was running other threads (it will wait for them now)
  • Loading branch information
PanosK92 committed Jan 7, 2025
1 parent d3a6d5c commit 7a29e8a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 51 deletions.
5 changes: 4 additions & 1 deletion runtime/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions runtime/RHI/Vulkan/Vulkan_CommandList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions runtime/RHI/Vulkan/Vulkan_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
75 changes: 29 additions & 46 deletions runtime/Rendering/Renderer_Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<uint32_t>::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)
Expand All @@ -2148,49 +2144,36 @@ namespace Spartan
m_vertex_buffer_lines = make_shared<RHI_Buffer>(RHI_Buffer_Type::Vertex, sizeof(m_line_vertices[0]), vertex_count, static_cast<void*>(&m_line_vertices[0]), true, "lines");
}

if (vertex_count != 0)
{
RHI_Vertex_PosCol* buffer = static_cast<RHI_Vertex_PosCol*>(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<RHI_Vertex_PosCol*>(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<uint32_t>::max(); // max +1 will wrap it to 0
Expand Down

0 comments on commit 7a29e8a

Please sign in to comment.