diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index f8f9f80e2..85158ccb2 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -70,6 +70,8 @@ #define ENABLE_LEGACY_OPENGL_REMOVAL (1 && ENABLE_2_5_0_ALPHA1) // Enable using vertex attributes and matrices in shaders #define ENABLE_GL_SHADERS_ATTRIBUTES (1 && ENABLE_LEGACY_OPENGL_REMOVAL) +// Shows an imgui dialog with GLModel statistics data +#define ENABLE_GLMODEL_STATISTICS (0 && ENABLE_LEGACY_OPENGL_REMOVAL) // Enable show non-manifold edges #define ENABLE_SHOW_NON_MANIFOLD_EDGES (1 && ENABLE_2_5_0_ALPHA1) // Enable rework of Reload from disk command diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index d8d1556c8..46219c1d1 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1667,6 +1667,10 @@ void GLCanvas3D::render() wxGetApp().plater()->init_environment_texture(); #endif // ENABLE_ENVIRONMENT_MAP +#if ENABLE_GLMODEL_STATISTICS + GLModel::reset_statistics_counters(); +#endif // ENABLE_GLMODEL_STATISTICS + const Size& cnv_size = get_canvas_size(); // Probably due to different order of events on Linux/GTK2, when one switched from 3D scene // to preview, this was called before canvas had its final size. It reported zero width @@ -1776,6 +1780,9 @@ void GLCanvas3D::render() #if ENABLE_CAMERA_STATISTICS camera.debug_render(); #endif // ENABLE_CAMERA_STATISTICS +#if ENABLE_GLMODEL_STATISTICS + GLModel::render_statistics(); +#endif // ENABLE_GLMODEL_STATISTICS std::string tooltip; diff --git a/src/slic3r/GUI/GLModel.cpp b/src/slic3r/GUI/GLModel.cpp index a92153330..b81287ece 100644 --- a/src/slic3r/GUI/GLModel.cpp +++ b/src/slic3r/GUI/GLModel.cpp @@ -4,6 +4,10 @@ #include "3DScene.hpp" #include "GUI_App.hpp" #include "GLShader.hpp" +#if ENABLE_GLMODEL_STATISTICS +#include "Plater.hpp" +#include "GLCanvas3D.hpp" +#endif // ENABLE_GLMODEL_STATISTICS #include "libslic3r/TriangleMesh.hpp" #include "libslic3r/Model.hpp" @@ -13,6 +17,10 @@ #include "libslic3r/Geometry/ConvexHull.hpp" #endif // ENABLE_LEGACY_OPENGL_REMOVAL +#if ENABLE_GLMODEL_STATISTICS +#include +#endif // ENABLE_GLMODEL_STATISTICS + #include #include @@ -380,6 +388,10 @@ size_t GLModel::Geometry::indices_count() const } #endif // ENABLE_LEGACY_OPENGL_REMOVAL +#if ENABLE_GLMODEL_STATISTICS +GLModel::Statistics GLModel::s_statistics; +#endif // ENABLE_GLMODEL_STATISTICS + #if ENABLE_LEGACY_OPENGL_REMOVAL void GLModel::init_from(Geometry&& data) #else @@ -702,10 +714,16 @@ void GLModel::reset() if (m_render_data.ibo_id > 0) { glsafe(::glDeleteBuffers(1, &m_render_data.ibo_id)); m_render_data.ibo_id = 0; +#if ENABLE_GLMODEL_STATISTICS + s_statistics.gpu_memory.indices.current -= indices_size_bytes(); +#endif // ENABLE_GLMODEL_STATISTICS } if (m_render_data.vbo_id > 0) { glsafe(::glDeleteBuffers(1, &m_render_data.vbo_id)); m_render_data.vbo_id = 0; +#if ENABLE_GLMODEL_STATISTICS + s_statistics.gpu_memory.vertices.current -= vertices_size_bytes(); +#endif // ENABLE_GLMODEL_STATISTICS } m_render_data.vertices_count = 0; @@ -899,6 +917,10 @@ void GLModel::render(const std::pair& range) #endif // ENABLE_GL_SHADERS_ATTRIBUTES glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); + +#if ENABLE_GLMODEL_STATISTICS + ++s_statistics.render_calls; +#endif // ENABLE_GLMODEL_STATISTICS } #endif // ENABLE_LEGACY_OPENGL_REMOVAL @@ -1054,6 +1076,10 @@ void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instance #endif // ENABLE_LEGACY_OPENGL_REMOVAL glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); + +#if ENABLE_GLMODEL_STATISTICS + ++s_statistics.render_instanced_calls; +#endif // ENABLE_GLMODEL_STATISTICS } #if ENABLE_LEGACY_OPENGL_REMOVAL @@ -1076,6 +1102,10 @@ bool GLModel::send_to_gpu() glsafe(::glBufferData(GL_ARRAY_BUFFER, data.vertices_size_bytes(), data.vertices.data(), GL_STATIC_DRAW)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); m_render_data.vertices_count = vertices_count(); +#if ENABLE_GLMODEL_STATISTICS + s_statistics.gpu_memory.vertices.current += data.vertices_size_bytes(); + s_statistics.gpu_memory.vertices.max = std::max(s_statistics.gpu_memory.vertices.current, s_statistics.gpu_memory.vertices.max); +#endif // ENABLE_GLMODEL_STATISTICS data.vertices = std::vector(); // indices @@ -1107,10 +1137,75 @@ bool GLModel::send_to_gpu() glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); } m_render_data.indices_count = indices_count; +#if ENABLE_GLMODEL_STATISTICS + s_statistics.gpu_memory.indices.current += data.indices_size_bytes(); + s_statistics.gpu_memory.indices.max = std::max(s_statistics.gpu_memory.indices.current, s_statistics.gpu_memory.indices.max); +#endif // ENABLE_GLMODEL_STATISTICS data.indices = std::vector(); return true; } + +#if ENABLE_GLMODEL_STATISTICS +void GLModel::render_statistics() +{ + static const float offset = 175.0f; + ImGuiWrapper& imgui = *wxGetApp().imgui(); + + auto add_memory = [&imgui](const std::string& label, int64_t memory) { + auto format_string = [memory](const std::string& units, float value) { + return std::to_string(memory) + " bytes (" + + Slic3r::float_to_string_decimal_point(float(memory) * value, 3) + + " " + units + ")"; + }; + + static const float kb = 1024.0f; + static const float inv_kb = 1.0f / kb; + static const float mb = 1024.0f * kb; + static const float inv_mb = 1.0f / mb; + static const float gb = 1024.0f * mb; + static const float inv_gb = 1.0f / gb; + imgui.text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, label); + ImGui::SameLine(offset); + if (static_cast(memory) < mb) + imgui.text(format_string("KB", inv_kb)); + else if (static_cast(memory) < gb) + imgui.text(format_string("MB", inv_mb)); + else + imgui.text(format_string("GB", inv_gb)); + }; + + auto add_counter = [&imgui](const std::string& label, int64_t counter) { + imgui.text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, label); + ImGui::SameLine(offset); + imgui.text(std::to_string(counter)); + }; + + imgui.set_next_window_pos(0.5f * wxGetApp().plater()->get_current_canvas3D()->get_canvas_size().get_width(), 0.0f, ImGuiCond_Once, 0.5f, 0.0f); + ImGui::SetNextWindowSizeConstraints({ 300.0f, 100.0f }, { 600.0f, 900.0f }); + imgui.begin(std::string("GLModel Statistics"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize); + ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindow()); + + add_counter(std::string("Render calls:"), s_statistics.render_calls); + add_counter(std::string("Render instanced calls:"), s_statistics.render_instanced_calls); + + if (ImGui::CollapsingHeader("GPU memory")) { + ImGui::Indent(10.0f); + if (ImGui::CollapsingHeader("Vertices")) { + add_memory(std::string("Current:"), s_statistics.gpu_memory.vertices.current); + add_memory(std::string("Max:"), s_statistics.gpu_memory.vertices.max); + } + if (ImGui::CollapsingHeader("Indices")) { + add_memory(std::string("Current:"), s_statistics.gpu_memory.indices.current); + add_memory(std::string("Max:"), s_statistics.gpu_memory.indices.max); + } + ImGui::Unindent(10.0f); + } + + imgui.end(); +} +#endif // ENABLE_GLMODEL_STATISTICS + #else void GLModel::send_to_gpu(RenderData& data, const std::vector& vertices, const std::vector& indices) { diff --git a/src/slic3r/GUI/GLModel.hpp b/src/slic3r/GUI/GLModel.hpp index e18d04678..e2da09484 100644 --- a/src/slic3r/GUI/GLModel.hpp +++ b/src/slic3r/GUI/GLModel.hpp @@ -177,6 +177,29 @@ namespace GUI { private: #if ENABLE_LEGACY_OPENGL_REMOVAL +#if ENABLE_GLMODEL_STATISTICS + struct Statistics + { + struct Buffers + { + struct Data + { + size_t current{ 0 }; + size_t max{ 0 }; + }; + Data indices; + Data vertices; + }; + + Buffers gpu_memory; + + int64_t render_calls{ 0 }; + int64_t render_instanced_calls{ 0 }; + }; + + static Statistics s_statistics; +#endif // ENABLE_GLMODEL_STATISTICS + RenderData m_render_data; // By default the vertex and index buffers data are sent to gpu at the first call to render() method. @@ -272,6 +295,14 @@ namespace GUI { ret += indices_size_bytes(); return ret; } + +#if ENABLE_GLMODEL_STATISTICS + static void render_statistics(); + static void reset_statistics_counters() { + s_statistics.render_calls = 0; + s_statistics.render_instanced_calls = 0; + } +#endif // ENABLE_GLMODEL_STATISTICS #endif // ENABLE_LEGACY_OPENGL_REMOVAL private: diff --git a/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp b/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp index 266b6a3ca..1d817b58b 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp @@ -182,8 +182,7 @@ void GLGizmoRotate::on_render() #if ENABLE_GL_SHADERS_ATTRIBUTES const Camera& camera = wxGetApp().plater()->get_camera(); - Transform3d view_model_matrix = camera.get_view_matrix() * m_grabbers.front().matrix; - + const Transform3d view_model_matrix = camera.get_view_matrix() * m_grabbers.front().matrix; shader->set_uniform("view_model_matrix", view_model_matrix); shader->set_uniform("projection_matrix", camera.get_projection_matrix()); #endif // ENABLE_GL_SHADERS_ATTRIBUTES