Skip to content

Commit

Permalink
Don't refresh the 3D view unless really necessary
Browse files Browse the repository at this point in the history
If you spot a missed refresh, please tell me how to reproduce.
  • Loading branch information
supermerill committed Sep 6, 2021
1 parent 7be5f47 commit ca99544
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 deletions.
53 changes: 29 additions & 24 deletions src/slic3r/GUI/GLCanvas3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,9 +1339,10 @@ void GLCanvas3D::reset_volumes(bool is_destroying)

_set_current();

m_selection.clear(is_destroying);
m_volumes.clear();
m_dirty = true;
m_selection.clear(is_destroying);
m_volumes.release_geometry();
m_volumes.clear();
m_dirty = true;

if(!is_destroying)
_set_warning_texture(WarningTexture::ObjectOutside, false);
Expand Down Expand Up @@ -2326,21 +2327,21 @@ static void reserve_new_volume_finalize_old_volume(GLVolume& vol_new, GLVolume&
vol_old.finalize_geometry(gl_initialized);
}

void GLCanvas3D::load_gcode_preview(const GCodeProcessor::Result& gcode_result)
void GLCanvas3D::load_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors)
{
m_gcode_viewer.load(gcode_result, *this->fff_print(), m_initialized);
if (m_dirty_gcode) {
m_dirty_gcode = false;
m_gcode_viewer.load(gcode_result, *this->fff_print(), m_initialized);

if (wxGetApp().is_editor()) {
m_gcode_viewer.update_shells_color_by_extruder(m_config);
_show_warning_texture_if_needed(WarningTexture::ToolpathOutside);
}
}
if (wxGetApp().is_editor()) {
m_gcode_viewer.update_shells_color_by_extruder(m_config);
_show_warning_texture_if_needed(WarningTexture::ToolpathOutside);
}

void GLCanvas3D::refresh_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors)
{
m_gcode_viewer.refresh(gcode_result, str_tool_colors);
set_as_dirty();
request_extra_frame();
m_gcode_viewer.refresh(gcode_result, str_tool_colors);
set_as_dirty();
request_extra_frame();
}
}

#if ENABLE_RENDER_PATH_REFRESH_AFTER_OPTIONS_CHANGE
Expand All @@ -2367,23 +2368,27 @@ void GLCanvas3D::load_sla_preview()

void GLCanvas3D::load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<CustomGCode::Item>& color_print_values)
{
const Print *print = this->fff_print();
const Print* print = this->fff_print();
if (print == nullptr)
return;

_set_current();

// Release OpenGL data before generating new data.
this->reset_volumes();
if (m_dirty_preview || m_volumes.empty()) {
m_dirty_preview = false;
// Release OpenGL data before generating new data.
this->reset_volumes();
//note: this isn't releasing all the memory in all os, can make it crash on linux for exemple.

_load_print_toolpaths();
_load_wipe_tower_toolpaths(str_tool_colors);
for (const PrintObject* object : print->objects())
_load_print_toolpaths();
_load_wipe_tower_toolpaths(str_tool_colors);
for (const PrintObject* object : print->objects())
_load_print_object_toolpaths(*object, str_tool_colors, color_print_values);

_update_toolpath_volumes_outside_state();
_show_warning_texture_if_needed(WarningTexture::ToolpathOutside);
_update_toolpath_volumes_outside_state();
_show_warning_texture_if_needed(WarningTexture::ToolpathOutside);
}
}

void GLCanvas3D::bind_event_handlers()
{
Expand Down Expand Up @@ -4835,7 +4840,7 @@ bool GLCanvas3D::_init_collapse_toolbar()
bool GLCanvas3D::_set_current()
{
return m_context != nullptr && m_canvas->SetCurrent(*m_context);
}
}

void GLCanvas3D::_resize(unsigned int w, unsigned int h)
{
Expand Down
8 changes: 6 additions & 2 deletions src/slic3r/GUI/GLCanvas3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ class GLCanvas3D

// Screen is only refreshed from the OnIdle handler if it is dirty.
bool m_dirty;
bool m_dirty_preview = true;
bool m_dirty_gcode = true;
bool m_initialized;
bool m_apply_zoom_to_volumes_filter;
mutable std::vector<int> m_hover_volume_idxs;
Expand Down Expand Up @@ -557,6 +559,9 @@ class GLCanvas3D
void post_event(wxEvent &&event);

void set_as_dirty();
void set_preview_dirty() { m_dirty_preview = true; }
bool is_preview_dirty() { return m_dirty_preview; }
void set_gcode_viewer_dirty() { m_dirty_gcode = true; }
void set_items_show(bool show_objects, bool show_gcode);

unsigned int get_volumes_count() const;
Expand Down Expand Up @@ -666,8 +671,7 @@ class GLCanvas3D

void reload_scene(bool refresh_immediately, bool force_full_scene_refresh = false);

void load_gcode_preview(const GCodeProcessor::Result& gcode_result);
void refresh_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors);
void load_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors);
#if ENABLE_RENDER_PATH_REFRESH_AFTER_OPTIONS_CHANGE
void refresh_gcode_preview_render_paths();
#endif // ENABLE_RENDER_PATH_REFRESH_AFTER_OPTIONS_CHANGE
Expand Down
20 changes: 16 additions & 4 deletions src/slic3r/GUI/GUI_Preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ void Preview::reload_print(bool keep_volumes)
#ifdef __linux__
m_volumes_cleanup_required ||
#endif /* __linux__ */
!keep_volumes)
(!keep_volumes && m_canvas->is_preview_dirty()))
{
m_canvas->set_preview_dirty();
m_canvas->reset_volumes();
m_loaded = false;
#ifdef __linux__
Expand Down Expand Up @@ -1020,15 +1021,18 @@ void Preview::load_print_as_fff(bool keep_z_range)
m_canvas->set_selected_extruder(0);
if (current_force_state == ForceState::ForceGcode || (gcode_preview_data_valid && current_force_state != ForceState::ForceExtrusions)) {
// Load the real G-code preview.
m_canvas->load_gcode_preview(*m_gcode_result);
m_canvas->refresh_gcode_preview(*m_gcode_result, colors);
if (current_force_state == ForceState::NoForce)
m_canvas->set_items_show(false, true);
m_canvas->load_gcode_preview(*m_gcode_result, colors);
m_left_sizer->Show(m_bottom_toolbar_panel);
m_left_sizer->Layout();
Refresh();
zs = m_canvas->get_gcode_layers_zs();
m_loaded = true;
} else {
// Load the initial preview based on slices, not the final G-code.
if (current_force_state == ForceState::NoForce)
m_canvas->set_items_show(true, false);
m_canvas->load_preview(colors, color_print_values);
m_left_sizer->Hide(m_bottom_toolbar_panel);
m_left_sizer->Layout();
Expand All @@ -1039,8 +1043,9 @@ void Preview::load_print_as_fff(bool keep_z_range)
// all layers filtered out
hide_layers_slider();
m_canvas_widget->Refresh();
} else
} else {
update_layers_slider(zs, keep_z_range);
}
}

#if ENABLE_PREVIEW_TYPE_CHANGE
Expand Down Expand Up @@ -1071,6 +1076,13 @@ void Preview::load_print_as_fff(bool keep_z_range)
#endif // ENABLE_PREVIEW_TYPE_CHANGE
}

void Preview::reset_gcode_toolpaths()
{
if (current_force_state == ForceState::NoForce)
m_canvas->set_items_show(true, false);
m_canvas->reset_gcode_toolpaths();
}

void Preview::load_print_as_sla()
{
if (m_loaded || (m_process->current_printer_technology() != ptSLA))
Expand Down
1 change: 1 addition & 0 deletions src/slic3r/GUI/GUI_Preview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Preview(wxWindow* parent, Model* model, DynamicPrintConfig* config, BackgroundSl

bool can_display_gcode();
bool can_display_volume();
void reset_gcode_toolpaths();

private:
ForceState current_force_state = ForceState::NoForce;
Expand Down
15 changes: 12 additions & 3 deletions src/slic3r/GUI/Plater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3576,8 +3576,14 @@ void Plater::priv::set_current_panel(wxTitledPanel* panel)
// FIXME: it may be better to have a single function making this check and let it be called wherever needed
bool export_in_progress = this->background_process.is_export_scheduled();
bool model_fits = view3D->check_volumes_outside_state() != ModelInstancePVS_Partly_Outside;
if (!model.objects.empty() && !export_in_progress && model_fits)
this->q->reslice();

if (!model.objects.empty() && !export_in_progress && model_fits) {
//check if already slicing
bool already_running = this->background_process.state() == BackgroundSlicingProcess::State::STATE_RUNNING ||
this->background_process.state() == BackgroundSlicingProcess::State::STATE_STARTED;
if(!already_running)
this->q->reslice();
}
// keeps current gcode preview, if any
preview->reload_print(true);
}
Expand Down Expand Up @@ -3722,6 +3728,7 @@ void Plater::priv::on_slicing_update(SlicingStatusEvent &evt)

void Plater::priv::on_slicing_completed(wxCommandEvent & evt)
{
preview->get_canvas3d()->set_gcode_viewer_dirty();
// auto_switch_preview == 0 means "no force tab change"
// auto_switch_preview == 1 means "force tab change"
// auto_switch_preview == 2 means "force tab change only if already on a plater one"
Expand Down Expand Up @@ -3753,6 +3760,8 @@ void Plater::priv::on_slicing_began()
{
clear_warnings();
notification_manager->close_notification_of_type(NotificationType::SlicingComplete);
preview->get_canvas3d()->set_gcode_viewer_dirty();
preview->get_canvas3d()->set_preview_dirty();
}
void Plater::priv::add_warning(const Slic3r::PrintStateBase::Warning& warning, size_t oid)
{
Expand Down Expand Up @@ -4390,7 +4399,7 @@ void Plater::priv::enable_preview_moves_slider(bool enable)

void Plater::priv::reset_gcode_toolpaths()
{
preview->get_canvas3d()->reset_gcode_toolpaths();
preview->reset_gcode_toolpaths();
}

bool Plater::priv::can_set_instance_to_object() const
Expand Down

0 comments on commit ca99544

Please sign in to comment.