From 874f39aac1e0249230361e5ac2dd23b5a4a105d2 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Sun, 22 Oct 2023 21:54:42 +0800 Subject: [PATCH] Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Adapt GLModel::Geometry index format in dependence of data size, where possible (cherry picked from commit prusa3d/PrusaSlicer@4d2d77e99c244c9da18983f6d624d277a9db4fec) --- src/slic3r/GUI/3DBed.cpp | 6 ++++-- src/slic3r/GUI/GLCanvas3D.cpp | 12 +++++++++--- src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp | 4 ++-- src/slic3r/GUI/PartPlate.cpp | 9 ++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index 68d2d89f85..3f29fd65b9 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -44,10 +44,11 @@ bool init_model_from_poly(GLModel &model, const ExPolygon &poly, float z) if (triangles.empty() || triangles.size() % 3 != 0) return false; - const GLModel::Geometry::EIndexType index_type = (triangles.size() < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; - GLModel::Geometry init_data; + const GLModel::Geometry::EIndexType index_type = (triangles.size() < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3T2, index_type }; + init_data.reserve_vertices(triangles.size()); + init_data.reserve_indices(triangles.size() / 3); Vec2f min = triangles.front(); Vec2f max = min; @@ -63,6 +64,7 @@ bool init_model_from_poly(GLModel &model, const ExPolygon &poly, float z) Vec2f inv_size = size.cwiseInverse(); inv_size.y() *= -1.0f; + // vertices + indices unsigned int vertices_counter = 0; for (const Vec2f &v : triangles) { const Vec3f p = {v.x(), v.y(), z}; diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 14d0254a8e..6c53d00b97 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -497,7 +497,8 @@ void GLCanvas3D::LayersEditing::render_profile(const Rect& bar_rect) m_profile.profile.reset(); GLModel::Geometry init_data; - init_data.format = { GLModel::Geometry::EPrimitiveType::LineStrip, GLModel::Geometry::EVertexLayout::P2, GLModel::Geometry::EIndexType::UINT }; + const GLModel::Geometry::EIndexType index_type = (m_layer_height_profile.size() / 2 < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; + init_data.format = { GLModel::Geometry::EPrimitiveType::LineStrip, GLModel::Geometry::EVertexLayout::P2, index_type }; init_data.color = ColorRGBA::BLUE(); init_data.reserve_vertices(m_layer_height_profile.size() / 2); init_data.reserve_indices(m_layer_height_profile.size() / 2); @@ -506,7 +507,10 @@ void GLCanvas3D::LayersEditing::render_profile(const Rect& bar_rect) for (unsigned int i = 0; i < (unsigned int)m_layer_height_profile.size(); i += 2) { init_data.add_vertex(Vec2f(bar_rect.get_left() + float(m_layer_height_profile[i + 1]) * scale_x, bar_rect.get_bottom() + float(m_layer_height_profile[i]) * scale_y)); - init_data.add_uint_index(i / 2); + if (index_type == GLModel::Geometry::EIndexType::USHORT) + init_data.add_ushort_index((unsigned short)i / 2); + else + init_data.add_uint_index(i / 2); } m_profile.profile.init_from(std::move(init_data)); @@ -891,7 +895,9 @@ void GLCanvas3D::SequentialPrintClearance::set_polygons(const Polygons& polygons const ExPolygons polygons_union = union_ex(polygons); unsigned int vertices_counter = 0; for (const ExPolygon& poly : polygons_union) { - const std::vector triangulation = triangulate_expolygon_3d(poly); + const std::vector triangulation = triangulate_expolygon_3d(poly); + fill_data.reserve_vertices(fill_data.vertices_count() + triangulation.size()); + fill_data.reserve_indices(fill_data.indices_count() + triangulation.size()); for (const Vec3d& v : triangulation) { fill_data.add_vertex((Vec3f)(v.cast() + 0.0125f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting ++vertices_counter; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp index b782ffbd6a..b67b18dfc3 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp @@ -345,9 +345,9 @@ void GLGizmoFlatten::update_planes() for (size_t i = 0; i < plane.vertices.size(); ++i) { init_data.add_vertex((Vec3f)plane.vertices[i].cast(), (Vec3f)plane.normal.cast()); if (index_type == GLModel::Geometry::EIndexType::USHORT) - init_data.add_ushort_index(i); + init_data.add_ushort_index((unsigned short)i); else - init_data.add_uint_index(i); + init_data.add_uint_index((unsigned int)i); } plane.vbo.init_from(std::move(init_data)); // FIXME: vertices should really be local, they need not diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index 67137e1621..27e819b45a 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -349,10 +349,12 @@ void PartPlate::calc_exclude_triangles(const ExPolygon &poly) static bool init_model_from_lines(GLModel &model, const Lines &lines, float z) { - const GLModel::Geometry::EIndexType index_type = (lines.size() < 65536 / 2) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; GLModel::Geometry init_data; + const GLModel::Geometry::EIndexType index_type = (lines.size() < 65536 / 2) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P3, index_type }; + init_data.reserve_vertices(2 * lines.size()); + init_data.reserve_indices(2 * lines.size()); for (const auto &l : lines) { init_data.add_vertex(Vec3f(unscale(l.a.x()), unscale(l.a.y()), z)); @@ -371,11 +373,12 @@ static bool init_model_from_lines(GLModel &model, const Lines &lines, float z) static bool init_model_from_lines(GLModel &model, const Lines3 &lines) { - const GLModel::Geometry::EIndexType index_type = (lines.size() < 65536 / 2) ? GLModel::Geometry::EIndexType::USHORT : - GLModel::Geometry::EIndexType::UINT; GLModel::Geometry init_data; + const GLModel::Geometry::EIndexType index_type = (lines.size() < 65536 / 2) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT; init_data.format = {GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P3, index_type}; + init_data.reserve_vertices(2 * lines.size()); + init_data.reserve_indices(2 * lines.size()); for (const auto &l : lines) { init_data.add_vertex(Vec3f(unscale(l.a.x()), unscale(l.a.y()), unscale(l.a.z())));