Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Adapt GLModel::Geometry index format in dependence of data size, where possible

(cherry picked from commit prusa3d/PrusaSlicer@4d2d77e99c)
This commit is contained in:
enricoturri1966 2023-10-22 21:54:42 +08:00 committed by Noisyfox
parent 6d4e0840bc
commit 874f39aac1
4 changed files with 21 additions and 10 deletions

View file

@ -44,10 +44,11 @@ bool init_model_from_poly(GLModel &model, const ExPolygon &poly, float z)
if (triangles.empty() || triangles.size() % 3 != 0) if (triangles.empty() || triangles.size() % 3 != 0)
return false; return false;
const GLModel::Geometry::EIndexType index_type = (triangles.size() < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT;
GLModel::Geometry init_data; 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.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 min = triangles.front();
Vec2f max = min; Vec2f max = min;
@ -63,6 +64,7 @@ bool init_model_from_poly(GLModel &model, const ExPolygon &poly, float z)
Vec2f inv_size = size.cwiseInverse(); Vec2f inv_size = size.cwiseInverse();
inv_size.y() *= -1.0f; inv_size.y() *= -1.0f;
// vertices + indices
unsigned int vertices_counter = 0; unsigned int vertices_counter = 0;
for (const Vec2f &v : triangles) { for (const Vec2f &v : triangles) {
const Vec3f p = {v.x(), v.y(), z}; const Vec3f p = {v.x(), v.y(), z};

View file

@ -497,7 +497,8 @@ void GLCanvas3D::LayersEditing::render_profile(const Rect& bar_rect)
m_profile.profile.reset(); m_profile.profile.reset();
GLModel::Geometry init_data; 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.color = ColorRGBA::BLUE();
init_data.reserve_vertices(m_layer_height_profile.size() / 2); init_data.reserve_vertices(m_layer_height_profile.size() / 2);
init_data.reserve_indices(m_layer_height_profile.size() / 2); init_data.reserve_indices(m_layer_height_profile.size() / 2);
@ -506,6 +507,9 @@ void GLCanvas3D::LayersEditing::render_profile(const Rect& bar_rect)
for (unsigned int i = 0; i < (unsigned int)m_layer_height_profile.size(); i += 2) { 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, 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)); bar_rect.get_bottom() + float(m_layer_height_profile[i]) * scale_y));
if (index_type == GLModel::Geometry::EIndexType::USHORT)
init_data.add_ushort_index((unsigned short)i / 2);
else
init_data.add_uint_index(i / 2); init_data.add_uint_index(i / 2);
} }
@ -892,6 +896,8 @@ void GLCanvas3D::SequentialPrintClearance::set_polygons(const Polygons& polygons
unsigned int vertices_counter = 0; unsigned int vertices_counter = 0;
for (const ExPolygon& poly : polygons_union) { for (const ExPolygon& poly : polygons_union) {
const std::vector<Vec3d> triangulation = triangulate_expolygon_3d(poly); const std::vector<Vec3d> 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) { for (const Vec3d& v : triangulation) {
fill_data.add_vertex((Vec3f)(v.cast<float>() + 0.0125f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting fill_data.add_vertex((Vec3f)(v.cast<float>() + 0.0125f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting
++vertices_counter; ++vertices_counter;

View file

@ -345,9 +345,9 @@ void GLGizmoFlatten::update_planes()
for (size_t i = 0; i < plane.vertices.size(); ++i) { for (size_t i = 0; i < plane.vertices.size(); ++i) {
init_data.add_vertex((Vec3f)plane.vertices[i].cast<float>(), (Vec3f)plane.normal.cast<float>()); init_data.add_vertex((Vec3f)plane.vertices[i].cast<float>(), (Vec3f)plane.normal.cast<float>());
if (index_type == GLModel::Geometry::EIndexType::USHORT) if (index_type == GLModel::Geometry::EIndexType::USHORT)
init_data.add_ushort_index(i); init_data.add_ushort_index((unsigned short)i);
else else
init_data.add_uint_index(i); init_data.add_uint_index((unsigned int)i);
} }
plane.vbo.init_from(std::move(init_data)); plane.vbo.init_from(std::move(init_data));
// FIXME: vertices should really be local, they need not // FIXME: vertices should really be local, they need not

View file

@ -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) 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; 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.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) { for (const auto &l : lines) {
init_data.add_vertex(Vec3f(unscale<float>(l.a.x()), unscale<float>(l.a.y()), z)); init_data.add_vertex(Vec3f(unscale<float>(l.a.x()), unscale<float>(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) 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; 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.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) { for (const auto &l : lines) {
init_data.add_vertex(Vec3f(unscale<float>(l.a.x()), unscale<float>(l.a.y()), unscale<float>(l.a.z()))); init_data.add_vertex(Vec3f(unscale<float>(l.a.x()), unscale<float>(l.a.y()), unscale<float>(l.a.z())));