Skip to content

Commit

Permalink
[rendering] fixed an issue where lines wouldn't render correctly, imp…
Browse files Browse the repository at this point in the history
…roved performance and fixed a memory leak
  • Loading branch information
PanosK92 committed Jan 8, 2025
1 parent 53dc757 commit 2851349
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 150 deletions.
1 change: 0 additions & 1 deletion data/shaders/line.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct vertex
vertex main_vs(vertex input)
{
input.position.w = 1.0f;
input.position = mul(input.position, buffer_pass.transform);
input.position = mul(input.position, buffer_frame.view_projection_unjittered);

return input;
Expand Down
4 changes: 2 additions & 2 deletions runtime/Game/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ namespace Spartan

// draw fz force
Math::Vector3 fz_end = start + Math::Vector3(parameters.pacejka_fz[wheel_index] * wheel_forward_dir) * 0.2f;
Renderer::DrawDirectionalArrow(start, fz_end, arrow_size, Color(0.0f, 1.0f, 0.0f, 1.0f), false);
Renderer::DrawDirectionalArrow(start, fz_end, arrow_size, Color(0.0f, 1.0f, 0.0f, 1.0f));

// draw fx force
Math::Vector3 fx_end = start + Math::Vector3(parameters.pacejka_fx[wheel_index] * wheel_right_dir) * 0.2f;
Renderer::DrawDirectionalArrow(start, fx_end, arrow_size, Color(1.0f, 0.0f, 0.0f, 1.0f), false);
Renderer::DrawDirectionalArrow(start, fx_end, arrow_size, Color(1.0f, 0.0f, 0.0f, 1.0f));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions runtime/Physics/PhysicsDebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ namespace Spartan
Renderer::DrawLine(
reinterpret_cast<const Vector3&>(from),
reinterpret_cast<const Vector3&>(to),
reinterpret_cast<const Color&>(color_from),
reinterpret_cast<const Color&>(color_to),
true
Color(color_from.getX(), color_from.getY(), color_from.getZ(), 1.0f),
Color(color_to.getX(), color_to.getY(), color_to.getZ(), 1.0f)
);
}

Expand Down
8 changes: 3 additions & 5 deletions runtime/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ namespace Spartan
Pcb_Pass Renderer::m_pcb_pass_cpu;

// line rendering
shared_ptr<RHI_Buffer> Renderer::m_vertex_buffer_lines;
vector<RHI_Vertex_PosCol> Renderer::m_line_vertices;
uint32_t Renderer::m_lines_index_depth_off;
uint32_t Renderer::m_lines_index_depth_on;
shared_ptr<RHI_Buffer> Renderer::m_lines_vertex_buffer;
vector<RHI_Vertex_PosCol> Renderer::m_lines_vertices;

// misc
uint32_t Renderer::m_resource_index = 0;
Expand Down Expand Up @@ -275,7 +273,7 @@ namespace Spartan

m_renderables.clear();
swap_chain = nullptr;
m_vertex_buffer_lines = nullptr;
m_lines_vertex_buffer = nullptr;
}

RHI_OpenImageDenoise::Shutdown();
Expand Down
20 changes: 9 additions & 11 deletions runtime/Rendering/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ namespace Spartan
static void Tick();

// primitive rendering (useful for debugging)
static void DrawLine(const Math::Vector3& from, const Math::Vector3& to, const Color& color_from = Color::standard_renderer_lines, const Color& color_to = Color::standard_renderer_lines, const bool depth = true);
static void DrawTriangle(const Math::Vector3& v0, const Math::Vector3& v1, const Math::Vector3& v2, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawBox(const Math::BoundingBox& box, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawCircle(const Math::Vector3& center, const Math::Vector3& axis, const float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawSphere(const Math::Vector3& center, float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawDirectionalArrow(const Math::Vector3& start, const Math::Vector3& end, float arrow_size, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawPlane(const Math::Plane& plane, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawLine(const Math::Vector3& from, const Math::Vector3& to, const Color& color_from = Color::standard_renderer_lines, const Color& color_to = Color::standard_renderer_lines);
static void DrawTriangle(const Math::Vector3& v0, const Math::Vector3& v1, const Math::Vector3& v2, const Color& color = Color::standard_renderer_lines);
static void DrawBox(const Math::BoundingBox& box, const Color& color = Color::standard_renderer_lines);
static void DrawCircle(const Math::Vector3& center, const Math::Vector3& axis, const float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines);
static void DrawSphere(const Math::Vector3& center, float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines);
static void DrawDirectionalArrow(const Math::Vector3& start, const Math::Vector3& end, float arrow_size, const Color& color = Color::standard_renderer_lines);
static void DrawPlane(const Math::Plane& plane, const Color& color = Color::standard_renderer_lines);
static void DrawString(const std::string& text, const Math::Vector2& position_screen_percentage);

// options
Expand Down Expand Up @@ -200,10 +200,8 @@ namespace Spartan
static std::unordered_map<Renderer_Entity, std::vector<std::shared_ptr<Entity>>> m_renderables;
static Cb_Frame m_cb_frame_cpu;
static Pcb_Pass m_pcb_pass_cpu;
static std::shared_ptr<RHI_Buffer> m_vertex_buffer_lines;
static std::vector<RHI_Vertex_PosCol> m_line_vertices;
static uint32_t m_lines_index_depth_off;
static uint32_t m_lines_index_depth_on;
static std::shared_ptr<RHI_Buffer> m_lines_vertex_buffer;
static std::vector<RHI_Vertex_PosCol> m_lines_vertices;
static uint32_t m_resource_index;
static std::atomic<bool> m_initialized_resources;
static std::atomic<bool> m_initialized_third_party;
Expand Down
62 changes: 17 additions & 45 deletions runtime/Rendering/Renderer_Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2110,15 +2110,11 @@ namespace Spartan

void Renderer::Pass_Lines(RHI_CommandList* cmd_list, RHI_Texture* tex_out)
{
// acquire resources
RHI_Shader* shader_v = GetShader(Renderer_Shader::line_v);
RHI_Shader* shader_p = GetShader(Renderer_Shader::line_p);
if (!shader_v->IsCompiled() || !shader_p->IsCompiled())
return;
RHI_Shader* shader_v = GetShader(Renderer_Shader::line_v);
RHI_Shader* shader_p = GetShader(Renderer_Shader::line_p);
uint32_t vertex_count = static_cast<uint32_t>(m_lines_vertices.size());

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) && !m_line_vertices.empty())
if (shader_v->IsCompiled() && shader_p->IsCompiled() && vertex_count != 0)
{
cmd_list->BeginTimeblock("lines");

Expand All @@ -2127,58 +2123,34 @@ namespace Spartan
pso.shaders[RHI_Shader_Type::Vertex] = shader_v;
pso.shaders[RHI_Shader_Type::Pixel] = shader_p;
pso.rasterizer_state = GetRasterizerState(Renderer_RasterizerState::Solid);
pso.blend_state = GetBlendState(Renderer_BlendState::Alpha);
pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::ReadGreaterEqual);
pso.render_target_color_textures[0] = tex_out;
pso.clear_color[0] = rhi_color_load;
pso.render_target_depth_texture = GetRenderTarget(Renderer_RenderTarget::gbuffer_depth_output);
pso.primitive_toplogy = RHI_PrimitiveTopology::LineList;
cmd_list->SetPipelineState(pso);

// grow vertex buffer (if needed)
uint32_t vertex_count = static_cast<uint32_t>(m_line_vertices.size());
if (vertex_count > m_vertex_buffer_lines->GetElementCount())
// grow vertex buffer (if needed)
if (vertex_count > m_lines_vertex_buffer->GetElementCount())
{
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");
m_lines_vertex_buffer = make_shared<RHI_Buffer>(RHI_Buffer_Type::Vertex, sizeof(m_lines_vertices[0]), vertex_count, static_cast<void*>(&m_lines_vertices[0]), true, "lines");
}

// 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);
// update and set vertex buffer
RHI_Vertex_PosCol* buffer = static_cast<RHI_Vertex_PosCol*>(m_lines_vertex_buffer->GetMappedData());
memset(buffer, 0, m_lines_vertex_buffer->GetObjectSize());
copy(m_lines_vertices.begin(), m_lines_vertices.end(), buffer);
cmd_list->SetBufferVertex(m_lines_vertex_buffer.get());

m_pcb_pass_cpu.transform = Matrix::Identity;
cmd_list->PushConstants(m_pcb_pass_cpu);
cmd_list->SetCullMode(RHI_CullMode::None);

// 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);
}

// 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->SetBufferVertex(m_vertex_buffer_lines.get());
cmd_list->Draw((m_lines_index_depth_on - (vertex_count / 2)) + 1, vertex_count / 2);
}

cmd_list->Draw(static_cast<uint32_t>(m_lines_vertices.size()));
cmd_list->SetCullMode(RHI_CullMode::Back);

cmd_list->EndTimeblock();
}

m_lines_index_depth_off = numeric_limits<uint32_t>::max(); // max +1 will wrap it to 0
m_lines_index_depth_on = (static_cast<uint32_t>(m_line_vertices.size()) / 2) - 1; // -1 because +1 will make it go to size / 2
m_lines_vertices.clear();
}

void Renderer::Pass_Outline(RHI_CommandList* cmd_list, RHI_Texture* tex_out)
Expand Down
Loading

0 comments on commit 2851349

Please sign in to comment.