Merge remote-tracking branch 'origin/et_splitted_vbuffer'

This commit is contained in:
enricoturri1966 2021-01-26 14:11:43 +01:00
commit 5700f4396f
4 changed files with 1643 additions and 222 deletions

View File

@ -107,4 +107,13 @@
#define ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING (1 && ENABLE_2_3_0_RC1)
//====================
// 2.3.1.alpha1 techs
//====================
#define ENABLE_2_3_1_ALPHA1 1
#define ENABLE_SPLITTED_VERTEX_BUFFER (1 && ENABLE_2_3_1_ALPHA1)
#endif // _prusaslicer_technologies_h_

File diff suppressed because it is too large Load Diff

View File

@ -19,8 +19,17 @@ namespace GUI {
class GCodeViewer
{
#if ENABLE_SPLITTED_VERTEX_BUFFER
using IBufferType = unsigned short;
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
using Color = std::array<float, 3>;
using VertexBuffer = std::vector<float>;
#if ENABLE_SPLITTED_VERTEX_BUFFER
using MultiVertexBuffer = std::vector<VertexBuffer>;
using IndexBuffer = std::vector<IBufferType>;
#else
using IndexBuffer = std::vector<unsigned int>;
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
using MultiIndexBuffer = std::vector<IndexBuffer>;
static const std::vector<Color> Extrusion_Role_Colors;
@ -39,7 +48,7 @@ class GCodeViewer
CustomGCodes
};
// vbo buffer containing vertices data used to rendder a specific toolpath type
// vbo buffer containing vertices data used to render a specific toolpath type
struct VBuffer
{
enum class EFormat : unsigned char
@ -53,20 +62,31 @@ class GCodeViewer
};
EFormat format{ EFormat::Position };
#if ENABLE_SPLITTED_VERTEX_BUFFER
// vbos id
std::vector<unsigned int> vbos;
// sizes of the buffers, in bytes, used in export to obj
std::vector<size_t> sizes;
#else
// vbo id
unsigned int id{ 0 };
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
// count of vertices, updated after data are sent to gpu
size_t count{ 0 };
size_t data_size_bytes() const { return count * vertex_size_bytes(); }
#if ENABLE_SPLITTED_VERTEX_BUFFER
// We set 65536 as max count of vertices inside a vertex buffer to allow
// to use unsigned short in place of unsigned int for indices in the index buffer, to save memory
size_t max_size_bytes() const { return 65536 * vertex_size_bytes(); }
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
size_t vertex_size_floats() const { return position_size_floats() + normal_size_floats(); }
size_t vertex_size_bytes() const { return vertex_size_floats() * sizeof(float); }
size_t position_offset_floats() const { return 0; }
size_t position_offset_size() const { return position_offset_floats() * sizeof(float); }
size_t position_size_floats() const
{
size_t position_size_floats() const {
switch (format)
{
case EFormat::Position:
@ -77,8 +97,7 @@ class GCodeViewer
}
size_t position_size_bytes() const { return position_size_floats() * sizeof(float); }
size_t normal_offset_floats() const
{
size_t normal_offset_floats() const {
switch (format)
{
case EFormat::Position:
@ -102,11 +121,18 @@ class GCodeViewer
void reset();
};
// ibo buffer containing indices data (lines/triangles) used to render a specific toolpath type
// ibo buffer containing indices data (for lines/triangles) used to render a specific toolpath type
struct IBuffer
{
#if ENABLE_SPLITTED_VERTEX_BUFFER
// id of the associated vertex buffer
unsigned int vbo{ 0 };
// ibo id
unsigned int ibo{ 0 };
#else
// ibo id
unsigned int id{ 0 };
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
// count of indices, updated after data are sent to gpu
size_t count{ 0 };
@ -118,19 +144,36 @@ class GCodeViewer
{
struct Endpoint
{
// index of the index buffer
// index of the buffer in the multibuffer vector
// the buffer type may change:
// it is the vertex buffer while extracting vertices data,
// the index buffer while extracting indices data
unsigned int b_id{ 0 };
// index into the index buffer
// index into the buffer
size_t i_id{ 0 };
// sequential id (index into the vertex buffer)
// move id
size_t s_id{ 0 };
Vec3f position{ Vec3f::Zero() };
};
EMoveType type{ EMoveType::Noop };
ExtrusionRole role{ erNone };
#if ENABLE_SPLITTED_VERTEX_BUFFER
struct Sub_Path
{
Endpoint first;
Endpoint last;
bool contains(size_t s_id) const {
return first.s_id <= s_id && s_id <= last.s_id;
}
};
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
EMoveType type{ EMoveType::Noop };
ExtrusionRole role{ erNone };
#if !ENABLE_SPLITTED_VERTEX_BUFFER
Endpoint first;
Endpoint last;
#endif // !ENABLE_SPLITTED_VERTEX_BUFFER
float delta_extruder{ 0.0f };
float height{ 0.0f };
float width{ 0.0f };
@ -139,17 +182,45 @@ class GCodeViewer
float volumetric_rate{ 0.0f };
unsigned char extruder_id{ 0 };
unsigned char cp_color_id{ 0 };
#if ENABLE_SPLITTED_VERTEX_BUFFER
std::vector<Sub_Path> sub_paths;
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
bool matches(const GCodeProcessor::MoveVertex& move) const;
#if ENABLE_SPLITTED_VERTEX_BUFFER
size_t vertices_count() const {
return sub_paths.empty() ? 0 : sub_paths.back().last.s_id - sub_paths.front().first.s_id + 1;
}
bool contains(size_t s_id) const {
return sub_paths.empty() ? false : sub_paths.front().first.s_id <= s_id && s_id <= sub_paths.back().last.s_id;
}
int get_id_of_sub_path_containing(size_t s_id) const {
if (sub_paths.empty())
return -1;
else {
for (int i = 0; i < static_cast<int>(sub_paths.size()); ++i) {
if (sub_paths[i].contains(s_id))
return i;
}
return -1;
}
}
void add_sub_path(const GCodeProcessor::MoveVertex& move, unsigned int b_id, size_t i_id, size_t s_id) {
Endpoint endpoint = { b_id, i_id, s_id, move.position };
sub_paths.push_back({ endpoint , endpoint });
}
#else
size_t vertices_count() const { return last.s_id - first.s_id + 1; }
bool contains(size_t id) const { return first.s_id <= id && id <= last.s_id; }
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
};
// Used to batch the indices needed to render paths
// Used to batch the indices needed to render the paths
struct RenderPath
{
// Render path property
Color color;
// Index of the buffer in TBuffer::indices
unsigned int index_buffer_id;
// Render path content
unsigned int path_id;
@ -195,16 +266,32 @@ class GCodeViewer
std::string shader;
std::vector<Path> paths;
// std::set seems to perform singificantly better, at least on Windows.
// std::set seems to perform significantly better, at least on Windows.
// std::unordered_set<RenderPath, RenderPathPropertyHash, RenderPathPropertyEqual> render_paths;
std::set<RenderPath, RenderPathPropertyLower> render_paths;
bool visible{ false };
void reset();
// b_id index of buffer contained in this->indices
// i_id index of first index contained in this->indices[b_id]
// s_id index of first vertex contained in this->vertices
void add_path(const GCodeProcessor::MoveVertex& move, unsigned int b_id, size_t i_id, size_t s_id);
#if ENABLE_SPLITTED_VERTEX_BUFFER
unsigned int max_vertices_per_segment() const {
switch (render_primitive_type)
{
case ERenderPrimitiveType::Point: { return 1; }
case ERenderPrimitiveType::Line: { return 2; }
case ERenderPrimitiveType::Triangle: { return 8; }
default: { return 0; }
}
}
size_t max_vertices_per_segment_size_floats() const { return vertices.vertex_size_floats() * static_cast<size_t>(max_vertices_per_segment()); }
size_t max_vertices_per_segment_size_bytes() const { return max_vertices_per_segment_size_floats() * sizeof(float); }
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
unsigned int indices_per_segment() const {
switch (render_primitive_type)
{
@ -214,26 +301,27 @@ class GCodeViewer
default: { return 0; }
}
}
unsigned int start_segment_vertex_offset() const {
switch (render_primitive_type)
{
case ERenderPrimitiveType::Point:
case ERenderPrimitiveType::Line:
case ERenderPrimitiveType::Triangle:
default: { return 0; }
}
}
#if ENABLE_SPLITTED_VERTEX_BUFFER
size_t indices_per_segment_size_bytes() const { return static_cast<size_t>(indices_per_segment() * sizeof(IBufferType)); }
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
unsigned int start_segment_vertex_offset() const { return 0; }
unsigned int end_segment_vertex_offset() const {
switch (render_primitive_type)
{
case ERenderPrimitiveType::Point: { return 0; }
case ERenderPrimitiveType::Line: { return 1; }
case ERenderPrimitiveType::Triangle: { return 36; } // 1 vertex of 13th triangle
case ERenderPrimitiveType::Triangle: { return 36; } // 1st vertex of 13th triangle
default: { return 0; }
}
}
#if ENABLE_SPLITTED_VERTEX_BUFFER
bool has_data() const {
return !vertices.vbos.empty() && vertices.vbos.front() != 0 && !indices.empty() && indices.front().ibo != 0;
}
#else
bool has_data() const { return vertices.id != 0 && !indices.empty() && indices.front().id != 0; }
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
};
// helper to render shells
@ -308,6 +396,12 @@ class GCodeViewer
{
size_t first{ 0 };
size_t last{ 0 };
#if ENABLE_SPLITTED_VERTEX_BUFFER
bool operator == (const Endpoints& other) const {
return first == other.first && last == other.last;
}
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
};
private:
@ -315,14 +409,12 @@ class GCodeViewer
std::vector<Endpoints> m_endpoints;
public:
void append(double z, Endpoints endpoints)
{
void append(double z, Endpoints endpoints) {
m_zs.emplace_back(z);
m_endpoints.emplace_back(endpoints);
}
void reset()
{
void reset() {
m_zs = std::vector<double>();
m_endpoints = std::vector<Endpoints>();
}
@ -334,6 +426,17 @@ class GCodeViewer
std::vector<Endpoints>& get_endpoints() { return m_endpoints; }
double get_z_at(unsigned int id) const { return (id < m_zs.size()) ? m_zs[id] : 0.0; }
Endpoints get_endpoints_at(unsigned int id) const { return (id < m_endpoints.size()) ? m_endpoints[id] : Endpoints(); }
#if ENABLE_SPLITTED_VERTEX_BUFFER
bool operator != (const Layers& other) const {
if (m_zs != other.m_zs)
return true;
if (!(m_endpoints == other.m_endpoints))
return true;
return false;
}
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
};
#if ENABLE_GCODE_VIEWER_STATISTICS
@ -362,8 +465,6 @@ class GCodeViewer
int64_t extrude_segments_count{ 0 };
int64_t vbuffers_count{ 0 };
int64_t ibuffers_count{ 0 };
int64_t max_vertices_in_vertex_buffer{ 0 };
int64_t max_indices_in_index_buffer{ 0 };
void reset_all() {
reset_times();
@ -401,8 +502,6 @@ class GCodeViewer
extrude_segments_count = 0;
vbuffers_count = 0;
ibuffers_count = 0;
max_vertices_in_vertex_buffer = 0;
max_indices_in_index_buffer = 0;
}
};
#endif // ENABLE_GCODE_VIEWER_STATISTICS
@ -461,7 +560,6 @@ public:
};
private:
bool m_initialized{ false };
mutable bool m_gl_data_initialized{ false };
unsigned int m_last_result_id{ 0 };
size_t m_moves_count{ 0 };
@ -490,7 +588,7 @@ private:
GCodeProcessor::Result::SettingsIds m_settings_ids;
public:
GCodeViewer() = default;
GCodeViewer();
~GCodeViewer() { reset(); }
// extract rendering data from the given parameters
@ -506,6 +604,9 @@ public:
void render() const;
bool has_data() const { return !m_roles.empty(); }
#if ENABLE_SPLITTED_VERTEX_BUFFER
bool can_export_toolpaths() const;
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
const BoundingBoxf3& get_paths_bounding_box() const { return m_paths_bounding_box; }
const BoundingBoxf3& get_max_bounding_box() const { return m_max_bounding_box; }
@ -536,7 +637,6 @@ public:
void export_toolpaths_to_obj(const char* filename) const;
private:
void init();
void load_toolpaths(const GCodeProcessor::Result& gcode_result);
void load_shells(const Print& print, bool initialized);
void refresh_render_paths(bool keep_sequential_current_first, bool keep_sequential_current_last) const;

View File

@ -3866,7 +3866,11 @@ void GLCanvas3D::update_tooltip_for_settings_item_in_main_toolbar()
bool GLCanvas3D::has_toolpaths_to_export() const
{
#if ENABLE_SPLITTED_VERTEX_BUFFER
return m_gcode_viewer.can_export_toolpaths();
#else
return m_gcode_viewer.has_data();
#endif // ENABLE_SPLITTED_VERTEX_BUFFER
}
void GLCanvas3D::export_toolpaths_to_obj(const char* filename) const