diff --git a/data/shaders/line.hlsl b/data/shaders/line.hlsl index 51efee11f..13a728280 100644 --- a/data/shaders/line.hlsl +++ b/data/shaders/line.hlsl @@ -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; diff --git a/runtime/Game/Car.cpp b/runtime/Game/Car.cpp index 849fe1af8..f8373ada2 100644 --- a/runtime/Game/Car.cpp +++ b/runtime/Game/Car.cpp @@ -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)); } } } diff --git a/runtime/Physics/PhysicsDebugDraw.cpp b/runtime/Physics/PhysicsDebugDraw.cpp index 799c5279b..219bb8aac 100644 --- a/runtime/Physics/PhysicsDebugDraw.cpp +++ b/runtime/Physics/PhysicsDebugDraw.cpp @@ -54,9 +54,8 @@ namespace Spartan Renderer::DrawLine( reinterpret_cast(from), reinterpret_cast(to), - reinterpret_cast(color_from), - reinterpret_cast(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) ); } diff --git a/runtime/Rendering/Renderer.cpp b/runtime/Rendering/Renderer.cpp index f91176131..4d2ba677a 100644 --- a/runtime/Rendering/Renderer.cpp +++ b/runtime/Rendering/Renderer.cpp @@ -55,10 +55,8 @@ namespace Spartan Pcb_Pass Renderer::m_pcb_pass_cpu; // line rendering - shared_ptr Renderer::m_vertex_buffer_lines; - vector Renderer::m_line_vertices; - uint32_t Renderer::m_lines_index_depth_off; - uint32_t Renderer::m_lines_index_depth_on; + shared_ptr Renderer::m_lines_vertex_buffer; + vector Renderer::m_lines_vertices; // misc uint32_t Renderer::m_resource_index = 0; @@ -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(); diff --git a/runtime/Rendering/Renderer.h b/runtime/Rendering/Renderer.h index c4c40f41a..4be49a78b 100644 --- a/runtime/Rendering/Renderer.h +++ b/runtime/Rendering/Renderer.h @@ -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 @@ -200,10 +200,8 @@ namespace Spartan static std::unordered_map>> m_renderables; static Cb_Frame m_cb_frame_cpu; static Pcb_Pass m_pcb_pass_cpu; - static std::shared_ptr m_vertex_buffer_lines; - static std::vector m_line_vertices; - static uint32_t m_lines_index_depth_off; - static uint32_t m_lines_index_depth_on; + static std::shared_ptr m_lines_vertex_buffer; + static std::vector m_lines_vertices; static uint32_t m_resource_index; static std::atomic m_initialized_resources; static std::atomic m_initialized_third_party; diff --git a/runtime/Rendering/Renderer_Passes.cpp b/runtime/Rendering/Renderer_Passes.cpp index 4796a1978..a676fdc7b 100644 --- a/runtime/Rendering/Renderer_Passes.cpp +++ b/runtime/Rendering/Renderer_Passes.cpp @@ -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(m_lines_vertices.size()); - 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) && !m_line_vertices.empty()) + if (shader_v->IsCompiled() && shader_p->IsCompiled() && vertex_count != 0) { cmd_list->BeginTimeblock("lines"); @@ -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(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_Type::Vertex, sizeof(m_line_vertices[0]), vertex_count, static_cast(&m_line_vertices[0]), true, "lines"); + m_lines_vertex_buffer = make_shared(RHI_Buffer_Type::Vertex, sizeof(m_lines_vertices[0]), vertex_count, static_cast(&m_lines_vertices[0]), true, "lines"); } - // 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); + // update and set vertex buffer + RHI_Vertex_PosCol* buffer = static_cast(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(m_lines_vertices.size())); cmd_list->SetCullMode(RHI_CullMode::Back); cmd_list->EndTimeblock(); } - m_lines_index_depth_off = numeric_limits::max(); // max +1 will wrap it to 0 - m_lines_index_depth_on = (static_cast(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) diff --git a/runtime/Rendering/Renderer_Primitives.cpp b/runtime/Rendering/Renderer_Primitives.cpp index 3cca0ec18..43702e6de 100644 --- a/runtime/Rendering/Renderer_Primitives.cpp +++ b/runtime/Rendering/Renderer_Primitives.cpp @@ -34,88 +34,39 @@ using namespace Spartan::Math; namespace Spartan { - namespace + void Renderer::DrawLine(const Vector3& from, const Vector3& to, const Color& color_from, const Color& color_to) { - vector m_line_vertices; - uint32_t m_lines_index_depth_off = 0; - uint32_t m_lines_index_depth_on = 0; + m_lines_vertices.emplace_back(from, color_from); + m_lines_vertices.emplace_back(to, color_to); } - void Renderer::DrawLine(const Vector3& from, const Vector3& to, const Color& color_from, const Color& color_to, const bool depth /*= true*/) + void Renderer::DrawTriangle(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Color& color /*= DEBUG_COLOR*/) { - // get vertex index - uint32_t& index = depth ? m_lines_index_depth_on : m_lines_index_depth_off; - - // grow vertex vector (if needed) - uint32_t vertex_count = static_cast(m_line_vertices.size()); - if (index + 2 >= vertex_count) - { - uint32_t new_vertex_count = vertex_count == 0 ? 32768 : vertex_count * 2; - - // if this is not the first allocation, inform the user - if (vertex_count != 0) - { - SP_LOG_INFO("Line buffer can hold %d vertices but %d are needed, resizing the buffer to fit %d vertices.", vertex_count, index + 2, new_vertex_count); - } - - m_lines_index_depth_off = numeric_limits::max(); // max because it's incremented below - m_lines_index_depth_on = (new_vertex_count / 2) - 1; // -1 because it's incremented below - - m_line_vertices.reserve(new_vertex_count); - m_line_vertices.resize(new_vertex_count); - } - - // write lines - { - index++; - RHI_Vertex_PosCol& line_start = m_line_vertices[index]; - line_start.pos[0] = from.x; - line_start.pos[1] = from.y; - line_start.pos[2] = from.z; - line_start.col[0] = color_from.r; - line_start.col[1] = color_from.g; - line_start.col[2] = color_from.b; - line_start.col[3] = 1.0f; - - index++; - RHI_Vertex_PosCol& line_end = m_line_vertices[index]; - line_end.pos[0] = to.x; - line_end.pos[1] = to.y; - line_end.pos[2] = to.z; - line_end.col[0] = color_to.r; - line_end.col[1] = color_to.g; - line_end.col[2] = color_to.b; - line_end.col[3] = 1.0f; - } - } - - void Renderer::DrawTriangle(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Color& color /*= DEBUG_COLOR*/, bool depth /*= true*/) - { - DrawLine(v0, v1, color, color, depth); - DrawLine(v1, v2, color, color, depth); - DrawLine(v2, v0, color, color, depth); + DrawLine(v0, v1, color, color); + DrawLine(v1, v2, color, color); + DrawLine(v2, v0, color, color); } - void Renderer::DrawBox(const BoundingBox& box, const Color& color, const bool depth /*= true*/) + void Renderer::DrawBox(const BoundingBox& box, const Color& color) { const Vector3& min = box.GetMin(); const Vector3& max = box.GetMax(); - DrawLine(Vector3(min.x, min.y, min.z), Vector3(max.x, min.y, min.z), color, color, depth); - DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color, color, depth); - DrawLine(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color, color, depth); - DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, min.y, min.z), color, color, depth); - DrawLine(Vector3(min.x, min.y, min.z), Vector3(min.x, min.y, max.z), color, color, depth); - DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, min.y, max.z), color, color, depth); - DrawLine(Vector3(max.x, max.y, min.z), Vector3(max.x, max.y, max.z), color, color, depth); - DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color, color, depth); - DrawLine(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color, color, depth); - DrawLine(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color, color, depth); - DrawLine(Vector3(max.x, max.y, max.z), Vector3(min.x, max.y, max.z), color, color, depth); - DrawLine(Vector3(min.x, max.y, max.z), Vector3(min.x, min.y, max.z), color, color, depth); + DrawLine(Vector3(min.x, min.y, min.z), Vector3(max.x, min.y, min.z), color, color); + DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color, color); + DrawLine(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color, color); + DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, min.y, min.z), color, color); + DrawLine(Vector3(min.x, min.y, min.z), Vector3(min.x, min.y, max.z), color, color); + DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, min.y, max.z), color, color); + DrawLine(Vector3(max.x, max.y, min.z), Vector3(max.x, max.y, max.z), color, color); + DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color, color); + DrawLine(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color, color); + DrawLine(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color, color); + DrawLine(Vector3(max.x, max.y, max.z), Vector3(min.x, max.y, max.z), color, color); + DrawLine(Vector3(min.x, max.y, max.z), Vector3(min.x, min.y, max.z), color, color); } - void Renderer::DrawCircle(const Vector3& center, const Vector3& axis, const float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/) + void Renderer::DrawCircle(const Vector3& center, const Vector3& axis, const float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/) { if (radius <= 0.0f) return; @@ -149,11 +100,11 @@ namespace Spartan // Draw for (uint32_t i = 0; i <= segment_count - 1; i++) { - DrawLine(points[i], points[i + 1], color, color, depth); + DrawLine(points[i], points[i + 1], color, color); } } - void Renderer::DrawSphere(const Vector3& center, float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/) + void Renderer::DrawSphere(const Vector3& center, float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/) { // Need at least 4 segments segment_count = Helper::Max(segment_count, 4); @@ -185,8 +136,8 @@ namespace Spartan Vertex2 = Vector3((CosX * SinY1), (SinX * SinY1), CosY1) * radius + center; Vertex4 = Vector3((CosX * SinY2), (SinX * SinY2), CosY2) * radius + center; - DrawLine(Vertex1, Vertex2, color, color, depth); - DrawLine(Vertex1, Vertex3, color, color, depth); + DrawLine(Vertex1, Vertex2, color, color); + DrawLine(Vertex1, Vertex3, color, color); Vertex1 = Vertex2; Vertex3 = Vertex4; @@ -198,11 +149,11 @@ namespace Spartan } } - void Renderer::DrawDirectionalArrow(const Vector3& start, const Vector3& end, float arrow_size, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/) + void Renderer::DrawDirectionalArrow(const Vector3& start, const Vector3& end, float arrow_size, const Color& color /*= DEBUG_COLOR*/) { arrow_size = Helper::Max(0.1f, arrow_size); - DrawLine(start, end, color, color, depth); + DrawLine(start, end, color, color); Vector3 Dir = (end - start); Dir.Normalize(); @@ -221,21 +172,21 @@ namespace Spartan // since dir is x direction, my arrow will be pointing +y, -x and -y, -x float arrow_sqrt = Helper::Sqrt(arrow_size); Vector3 arrow_pos; - DrawLine(end, end + TM * Vector3(-arrow_sqrt, arrow_sqrt, 0), color, color, depth); - DrawLine(end, end + TM * Vector3(-arrow_sqrt, -arrow_sqrt, 0), color, color, depth); + DrawLine(end, end + TM * Vector3(-arrow_sqrt, arrow_sqrt, 0), color, color); + DrawLine(end, end + TM * Vector3(-arrow_sqrt, -arrow_sqrt, 0), color, color); } - void Renderer::DrawPlane(const Math::Plane& plane, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/) + void Renderer::DrawPlane(const Math::Plane& plane, const Color& color /*= DEBUG_COLOR*/) { // Arrow indicating normal Vector3 plane_origin = plane.normal * plane.d; - DrawDirectionalArrow(plane_origin, plane_origin + plane.normal * 2.0f, 0.2f, color, depth); + DrawDirectionalArrow(plane_origin, plane_origin + plane.normal * 2.0f, 0.2f, color); Vector3 U, V; plane.normal.FindBestAxisVectors(U, V); static const float scale = 10000.0f; - DrawLine(plane_origin - U * scale, plane_origin + U * scale, color, color, depth); - DrawLine(plane_origin - V * scale, plane_origin + V * scale, color, color, depth); + DrawLine(plane_origin - U * scale, plane_origin + U * scale, color, color); + DrawLine(plane_origin - V * scale, plane_origin + V * scale, color, color); } void Renderer::AddLinesToBeRendered() diff --git a/runtime/Rendering/Renderer_Resources.cpp b/runtime/Rendering/Renderer_Resources.cpp index 91ee04ce6..984bbe1d4 100644 --- a/runtime/Rendering/Renderer_Resources.cpp +++ b/runtime/Rendering/Renderer_Resources.cpp @@ -536,7 +536,7 @@ namespace Spartan create_mesh(MeshType::Grid); // this buffers holds all debug primitives that can be drawn - m_vertex_buffer_lines = make_shared(); + m_lines_vertex_buffer = make_shared(); } void Renderer::CreateStandardTextures()