Debug tech ENABLE_GLMODEL_STATISTICS - Shows an imgui dialog with GLModel statistics data

This commit is contained in:
enricoturri1966 2022-03-10 15:55:15 +01:00
parent 9d2314557d
commit 3dddfa919a
5 changed files with 136 additions and 2 deletions

View File

@ -70,6 +70,8 @@
#define ENABLE_LEGACY_OPENGL_REMOVAL (1 && ENABLE_2_5_0_ALPHA1) #define ENABLE_LEGACY_OPENGL_REMOVAL (1 && ENABLE_2_5_0_ALPHA1)
// Enable using vertex attributes and matrices in shaders // Enable using vertex attributes and matrices in shaders
#define ENABLE_GL_SHADERS_ATTRIBUTES (1 && ENABLE_LEGACY_OPENGL_REMOVAL) #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 // Enable show non-manifold edges
#define ENABLE_SHOW_NON_MANIFOLD_EDGES (1 && ENABLE_2_5_0_ALPHA1) #define ENABLE_SHOW_NON_MANIFOLD_EDGES (1 && ENABLE_2_5_0_ALPHA1)
// Enable rework of Reload from disk command // Enable rework of Reload from disk command

View File

@ -1667,6 +1667,10 @@ void GLCanvas3D::render()
wxGetApp().plater()->init_environment_texture(); wxGetApp().plater()->init_environment_texture();
#endif // ENABLE_ENVIRONMENT_MAP #endif // ENABLE_ENVIRONMENT_MAP
#if ENABLE_GLMODEL_STATISTICS
GLModel::reset_statistics_counters();
#endif // ENABLE_GLMODEL_STATISTICS
const Size& cnv_size = get_canvas_size(); const Size& cnv_size = get_canvas_size();
// Probably due to different order of events on Linux/GTK2, when one switched from 3D scene // 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 // 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 #if ENABLE_CAMERA_STATISTICS
camera.debug_render(); camera.debug_render();
#endif // ENABLE_CAMERA_STATISTICS #endif // ENABLE_CAMERA_STATISTICS
#if ENABLE_GLMODEL_STATISTICS
GLModel::render_statistics();
#endif // ENABLE_GLMODEL_STATISTICS
std::string tooltip; std::string tooltip;

View File

@ -4,6 +4,10 @@
#include "3DScene.hpp" #include "3DScene.hpp"
#include "GUI_App.hpp" #include "GUI_App.hpp"
#include "GLShader.hpp" #include "GLShader.hpp"
#if ENABLE_GLMODEL_STATISTICS
#include "Plater.hpp"
#include "GLCanvas3D.hpp"
#endif // ENABLE_GLMODEL_STATISTICS
#include "libslic3r/TriangleMesh.hpp" #include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/Model.hpp" #include "libslic3r/Model.hpp"
@ -13,6 +17,10 @@
#include "libslic3r/Geometry/ConvexHull.hpp" #include "libslic3r/Geometry/ConvexHull.hpp"
#endif // ENABLE_LEGACY_OPENGL_REMOVAL #endif // ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GLMODEL_STATISTICS
#include <imgui/imgui_internal.h>
#endif // ENABLE_GLMODEL_STATISTICS
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
@ -380,6 +388,10 @@ size_t GLModel::Geometry::indices_count() const
} }
#endif // ENABLE_LEGACY_OPENGL_REMOVAL #endif // ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GLMODEL_STATISTICS
GLModel::Statistics GLModel::s_statistics;
#endif // ENABLE_GLMODEL_STATISTICS
#if ENABLE_LEGACY_OPENGL_REMOVAL #if ENABLE_LEGACY_OPENGL_REMOVAL
void GLModel::init_from(Geometry&& data) void GLModel::init_from(Geometry&& data)
#else #else
@ -702,10 +714,16 @@ void GLModel::reset()
if (m_render_data.ibo_id > 0) { if (m_render_data.ibo_id > 0) {
glsafe(::glDeleteBuffers(1, &m_render_data.ibo_id)); glsafe(::glDeleteBuffers(1, &m_render_data.ibo_id));
m_render_data.ibo_id = 0; 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) { if (m_render_data.vbo_id > 0) {
glsafe(::glDeleteBuffers(1, &m_render_data.vbo_id)); glsafe(::glDeleteBuffers(1, &m_render_data.vbo_id));
m_render_data.vbo_id = 0; 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; m_render_data.vertices_count = 0;
@ -899,6 +917,10 @@ void GLModel::render(const std::pair<size_t, size_t>& range)
#endif // ENABLE_GL_SHADERS_ATTRIBUTES #endif // ENABLE_GL_SHADERS_ATTRIBUTES
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
#if ENABLE_GLMODEL_STATISTICS
++s_statistics.render_calls;
#endif // ENABLE_GLMODEL_STATISTICS
} }
#endif // ENABLE_LEGACY_OPENGL_REMOVAL #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 #endif // ENABLE_LEGACY_OPENGL_REMOVAL
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
#if ENABLE_GLMODEL_STATISTICS
++s_statistics.render_instanced_calls;
#endif // ENABLE_GLMODEL_STATISTICS
} }
#if ENABLE_LEGACY_OPENGL_REMOVAL #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(::glBufferData(GL_ARRAY_BUFFER, data.vertices_size_bytes(), data.vertices.data(), GL_STATIC_DRAW));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
m_render_data.vertices_count = vertices_count(); 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<float>(); data.vertices = std::vector<float>();
// indices // indices
@ -1107,10 +1137,75 @@ bool GLModel::send_to_gpu()
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
} }
m_render_data.indices_count = indices_count; 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<unsigned int>(); data.indices = std::vector<unsigned int>();
return true; 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<float>(memory) < mb)
imgui.text(format_string("KB", inv_kb));
else if (static_cast<float>(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 #else
void GLModel::send_to_gpu(RenderData& data, const std::vector<float>& vertices, const std::vector<unsigned int>& indices) void GLModel::send_to_gpu(RenderData& data, const std::vector<float>& vertices, const std::vector<unsigned int>& indices)
{ {

View File

@ -177,6 +177,29 @@ namespace GUI {
private: private:
#if ENABLE_LEGACY_OPENGL_REMOVAL #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; RenderData m_render_data;
// By default the vertex and index buffers data are sent to gpu at the first call to render() method. // 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(); ret += indices_size_bytes();
return ret; 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 #endif // ENABLE_LEGACY_OPENGL_REMOVAL
private: private:

View File

@ -182,8 +182,7 @@ void GLGizmoRotate::on_render()
#if ENABLE_GL_SHADERS_ATTRIBUTES #if ENABLE_GL_SHADERS_ATTRIBUTES
const Camera& camera = wxGetApp().plater()->get_camera(); 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("view_model_matrix", view_model_matrix);
shader->set_uniform("projection_matrix", camera.get_projection_matrix()); shader->set_uniform("projection_matrix", camera.get_projection_matrix());
#endif // ENABLE_GL_SHADERS_ATTRIBUTES #endif // ENABLE_GL_SHADERS_ATTRIBUTES