diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 68ad793545..f5b0796d73 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -230,6 +230,8 @@ void AppConfig::set_defaults() if (mode != "off" && mode != "solid" && mode != "shell") set("preview_reduced_detail_mode", "off"); } + if (get("show_preview_slow_render_hint").empty()) + set_bool("show_preview_slow_render_hint", true); // ORCA: darken the layers the preview layer slider is not scrubbed to if (get("preview_dim_previous_layers").empty()) diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 76e8a115e4..47801e03c6 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -570,6 +570,7 @@ set(SLIC3R_GUI_SOURCES GUI/PresetComboBoxes.hpp GUI/PresetHints.cpp GUI/PresetHints.hpp + GUI/PreviewFrameRateMonitor.hpp GUI/PrimitiveSizeDialog.cpp GUI/PrimitiveSizeDialog.hpp GUI/PrinterCloudAuthDialog.cpp diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 73546f9bcd..decb05ce91 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -1847,7 +1847,8 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const // the shell drag mode hides the infill only where the profile covers it: with no top or bottom // shell, or no walls, the infill is the surface. Per-object settings are not consulted. const PrintRegionConfig& region = print.default_region_config(); - m_viewer.set_reduced_detail_hide_infill(region.top_shell_layers.value > 0 && region.bottom_shell_layers.value > 0 && region.wall_loops.value > 0); + m_reduced_detail_hides_infill = region.top_shell_layers.value > 0 && region.bottom_shell_layers.value > 0 && region.wall_loops.value > 0; + m_viewer.set_reduced_detail_hide_infill(m_reduced_detail_hides_infill); // Orca hack: Hide filament group for non-bbl printers if (!print.is_BBL_printer()) m_nozzle_nums = 1; std::vector filament_maps = print.get_filament_maps(); @@ -3071,11 +3072,8 @@ void GCodeViewer::update_layers_slider_mode() void GCodeViewer::set_interacting(bool interacting) { - // With the mode off nothing changes while dragging. The solid model needs shells to stand in - // for the toolpaths and a single height to cut them at; a belt printer's layers are tilted - // planes, which no height cuts. - const bool usable = m_reduced_detail_mode != libvgcode::EReducedDetailMode::Off && - (!solid_model_enabled() || (!m_shells.volumes.empty() && m_layers_z_rising && !m_belt_view_enabled)); + // With the mode off nothing changes while dragging. + const bool usable = m_reduced_detail_mode != libvgcode::EReducedDetailMode::Off && (!solid_model_enabled() || solid_mode_usable()); // sampled afresh as each drag begins: the legend may have recoloured the toolpaths, and the // sliders may have moved the span, since the last one if (interacting && !m_viewer.is_reduced_detail()) diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index e129005878..d5e95f7634 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -336,6 +336,8 @@ private: bool m_legend_enabled{ true }; // what the preview draws while the user drags, as the preference has it libvgcode::EReducedDetailMode m_reduced_detail_mode{ libvgcode::EReducedDetailMode::Off }; + // whether the shell mode has infill to hide in the loaded print + bool m_reduced_detail_hides_infill{ false }; void read_reduced_detail_preferences(); static libvgcode::EReducedDetailMode reduced_detail_mode_from_string(const std::string& mode); // in the solid model mode, the sliced objects are drawn as solid shapes instead of toolpaths @@ -600,6 +602,16 @@ public: // while the user drags the camera or a slider, draw the reduced set, if the preference asks for one void set_interacting(bool interacting); bool is_reduced_detail() const { return m_viewer.is_reduced_detail(); } + // whether each drag mode would draw less than the full toolpaths; the OpenGL ES path has no reduced set +#if SLIC3R_OPENGL_ES + bool shell_mode_hides_infill() const { return false; } + bool solid_mode_usable() const { return false; } +#else + bool shell_mode_hides_infill() const { return m_reduced_detail_hides_infill; } + // whether the solid model can stand in for the toolpaths: it needs shells, and a single height to cut + // them at, which a belt printer's tilted layers do not have + bool solid_mode_usable() const { return !m_shells.volumes.empty() && m_layers_z_rising && !m_belt_view_enabled; } +#endif // the preference's string value: "off", "solid" or "shell" void set_reduced_detail_mode(const std::string& mode); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index f0f23a8524..0909c7bfbf 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -108,6 +108,7 @@ #include "I18N.hpp" #include "format.hpp" #include "NotificationManager.hpp" +#include "PreviewFrameRateMonitor.hpp" #include "DailyTips.hpp" #include "FilamentMapDialog.hpp" #include "Gizmos/GLGizmoUtils.hpp" @@ -3222,6 +3223,8 @@ void GLCanvas3D::_render_frame(bool scene_dirty, bool only_init) if (only_init) return; + const auto frame_start = std::chrono::steady_clock::now(); + #if ENABLE_ENVIRONMENT_MAP if (wxGetApp().is_editor()) wxGetApp().plater()->init_environment_texture(); @@ -3302,6 +3305,7 @@ void GLCanvas3D::_render_frame(bool scene_dirty, bool only_init) // Whether the preview draws its reduced set is decided before the cached scene is consulted, // since switching changes what the scene pass draws. + m_preview_interacting = false; if (m_canvas_type == ECanvasType::CanvasPreview && m_render_preview && m_gcode_viewer.has_data() && _update_preview_interaction()) scene_dirty = true; @@ -3488,6 +3492,9 @@ void GLCanvas3D::_render_frame(bool scene_dirty, bool only_init) if (!overlay_tick) m_render_stats.increment_fps_counter(); m_presented_signature = signature; + // A slider drag rebuilds the visible range on the CPU, which a cheaper drag mode does not speed up. + if (m_preview_interacting && !reuse_scene && !overlay_tick && !m_benchmarking && !m_gcode_viewer.is_slider_dragging()) + _track_preview_frame_time(std::chrono::duration(std::chrono::steady_clock::now() - frame_start).count()); } else { if (m_canvas->IsShownOnScreen()) { @@ -4591,6 +4598,9 @@ void GLCanvas3D::load_gcode_preview(const GCodeProcessorResult& gcode_result, co PartPlate* plate = partplate_list.get_curr_plate(); const std::vector& exclude_bounding_box = plate->get_exclude_areas(); + // The first frames of a new preview upload it; they say nothing about how fast it draws. + m_preview_frame_monitor.reset(); + _set_shown_canvas_current(); // The viewer uploads to the GPU from here on. Without a context the driver lets us make // current, the OpenGL functions were never loaded and there is nothing to draw it with. @@ -10861,6 +10871,38 @@ void GLCanvas3D::_render_wireframe_overlay() shader->stop_using(); } +void GLCanvas3D::_track_preview_frame_time(double frame_ms) +{ + const AppConfig* cfg = wxGetApp().app_config; + const std::optional suggested = suggested_preview_drag_mode(cfg->get("preview_reduced_detail_mode"), + m_gcode_viewer.shell_mode_hides_infill(), + m_gcode_viewer.solid_mode_usable()); + if (!suggested || s_preview_slow_hints_shown.count(*suggested) > 0 || !cfg->get_bool("show_preview_slow_render_hint")) { + m_preview_frame_monitor.reset(); + return; + } + if (const std::optional fps = m_preview_frame_monitor.add_frame(frame_ms)) { + s_preview_slow_hints_shown.insert(*suggested); + const int max_shown_fps = static_cast(std::ceil(PreviewFrameRateMonitor::min_fps)) - 1; + wxGetApp().plater()->get_notification_manager()->push_preview_slow_render_notification( + std::clamp(static_cast(std::lround(*fps)), 1, max_shown_fps), *suggested); + } +} + +void GLCanvas3D::apply_preview_reduced_detail_mode(const std::string& mode) +{ + AppConfig* cfg = wxGetApp().app_config; + cfg->set("preview_reduced_detail_mode", mode); + cfg->save(); + m_gcode_viewer.set_reduced_detail_mode(mode); + // slow seconds counted in the old mode say nothing about the new one, and a hint still on screen + // would offer to replace the mode just chosen + m_preview_frame_monitor.reset(); + wxGetApp().plater()->get_notification_manager()->close_notification_of_type(NotificationType::PreviewSlowRender); + set_as_dirty(); + request_extra_frame(); +} + // The reduced set is drawn while the camera, the navigator or either slider is dragged. A wheel // step has no duration, so it holds the reduced set for a settle time instead, and the frame that // restores the full toolpaths is scheduled for when that time runs out. Returns whether what the scene @@ -10870,6 +10912,7 @@ bool GLCanvas3D::_update_preview_interaction() const auto now = std::chrono::steady_clock::now(); const bool settling = now < m_preview_interaction_until; const bool dragging = is_user_interacting(); + m_preview_interacting = dragging || settling; const bool was_reduced = m_gcode_viewer.is_reduced_detail(); m_gcode_viewer.set_interacting(dragging || settling); if (settling && !dragging && m_gcode_viewer.is_reduced_detail()) { @@ -12123,22 +12166,27 @@ void GLCanvas3D::_render_canvas_toolbar() ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding , ImVec2(4.f, 10.f) * sc); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing , ImVec2(0.f, 8.f ) * sc); + // Drawn after the popup's style is popped, so the tooltip does not take the popup's colors. + std::string hovered_tip; if (ImGui::BeginPopup("CanvasToolbarMenu", ImGuiWindowFlags_NoMove)) { ImGui::PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true); Plater* p = wxGetApp().plater(); AppConfig* cfg = wxGetApp().app_config; - auto create_menu_item = [sc]( + auto create_menu_item = [sc, &hovered_tip]( const std::string& name, bool enable, bool condition, - const std::function& action + const std::function& action, + const std::string& tooltip = {} ) { ImGui::Dummy({2.f * sc,0}); ImGui::SameLine(); if (ImGui::BBLMenuItem((" " + _u8L(name)).c_str(), nullptr, false, enable, ImGui::CalcTextSize(_u8L(name).c_str()).y)) action(); + if (!tooltip.empty() && ImGui::IsItemHovered()) + hovered_tip = tooltip; ImGui::SameLine(12.f * sc); ImGui::TextColored(enable ? ImVec4(1,1,1,1) : ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled), "%s", into_u8(condition ? ImGui::VisibleIcon : ImGui::HiddenIcon).c_str()); }; @@ -12248,6 +12296,30 @@ void GLCanvas3D::_render_canvas_toolbar() [this]{wxGetApp().toggle_show_center_of_mass(); m_dirty = true;} ); + ImGui::Separator(); + + // Both eyes closed means the full toolpaths are drawn while dragging. + const std::string reduced_detail_mode = cfg->get("preview_reduced_detail_mode"); + auto create_drag_mode_item = [&](const std::string& name, const std::string& mode, const std::string& tooltip) { + const bool on = reduced_detail_mode == mode; + create_menu_item(name, + m_canvas_type == ECanvasType::CanvasPreview, // work only on preview + on, + [this, p, next = on ? std::string("off") : mode]{ + p->CallAfter([this, next]{ apply_preview_reduced_detail_mode(next); }); + }, + tooltip + ); + }; + // TRN View menu toggle: what the sliced preview draws while the camera or a slider is dragged. + create_drag_mode_item(_utf8(L("Solid model while dragging")), "solid", + _u8L("While you drag the camera or a preview slider, or zoom, the sliced objects are drawn as solid shapes. " + "The full toolpaths return when you let go. A print made object by object, or one on a belt printer, keeps its full toolpaths.")); + // TRN View menu toggle: what the sliced preview draws while the camera or a slider is dragged. + create_drag_mode_item(_utf8(L("Shell only while dragging")), "shell", + _u8L("While you drag the camera or a preview slider, or zoom, the toolpaths are drawn without the infill hidden under the walls. " + "The full toolpaths return when you let go.")); + // Belt printers, G-code preview only: show the raw machine-frame G-code instead of // the designed (upright) view. This menu is the only place the toggle lives (plus // its shortcut); the reload is deferred (CallAfter) so the preview is not rebuilt @@ -12271,6 +12343,9 @@ void GLCanvas3D::_render_canvas_toolbar() ImGui::PopStyleColor(6); ImGui::PopStyleVar(6); + if (!hovered_tip.empty()) + imgui.tooltip(hovered_tip, ImGui::GetFontSize() * 20.f); + imgui.end(); _render_section_view_panel(section_panel_pos); diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 1d936ef701..c7d205f3f1 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -42,6 +42,7 @@ #include "SceneCache.hpp" #include "SelectionOutline.hpp" #include "FrameProfiler.hpp" +#include "PreviewFrameRateMonitor.hpp" #include "IMToolbar.hpp" #include "slic3r/GUI/3DBed.hpp" #include "libslic3r/Slicing.hpp" @@ -52,6 +53,7 @@ #include #include #include +#include #include #include @@ -904,6 +906,13 @@ private: std::chrono::time_point m_preview_interaction_until{}; // whether the frame that restores the toolpaths once that time is up is still owed bool m_preview_settle_pending{ false }; + // whether this frame is drawn while the user moves the preview (a drag or a wheel step's settle time) + bool m_preview_interacting{ false }; + // watches those frames for a preview too slow to move in its current drag mode + PreviewFrameRateMonitor m_preview_frame_monitor; + // the drag modes the slow preview notification has offered, each once per run; static, so that + // rebuilding the GUI does not reset them + inline static std::set s_preview_slow_hints_shown; //BBS:add plate related logic mutable std::vector m_hover_volume_idxs; @@ -1542,6 +1551,9 @@ public: void msw_rescale() { m_gcode_viewer.invalidate_legend(); } void request_extra_frame() { m_extra_frame_requested = true; } + // Stores what the preview draws while dragging ("off", "solid" or "shell") and applies it to this canvas. + // Inside a frame, call it through CallAfter: switching can upload volumes. + void apply_preview_reduced_detail_mode(const std::string& mode); // whether the user is holding the camera, the navigator, a gizmo, the rectangle selection or a preview slider bool is_user_interacting() const; @@ -1737,6 +1749,8 @@ private: // decides whether the preview draws its reduced set this frame and returns whether what the scene // pass draws changed; runs before the cached scene is consulted bool _update_preview_interaction(); + // feeds the render time of a frame drawn while the user moves the preview to the slow preview monitor + void _track_preview_frame_time(double frame_ms); //BBS: render a plane for assemble void _render_plane() const; void _render_selection(); diff --git a/src/slic3r/GUI/NotificationManager.cpp b/src/slic3r/GUI/NotificationManager.cpp index f0aa8ac1d0..4dedef6101 100644 --- a/src/slic3r/GUI/NotificationManager.cpp +++ b/src/slic3r/GUI/NotificationManager.cpp @@ -835,6 +835,24 @@ void NotificationManager::PopNotification::render_hypertext(ImGuiWrapper& imgui, } +float NotificationManager::PopNotification::render_text_lines(ImGuiWrapper& imgui) +{ + const float starting_y = m_line_height / 2; + size_t last_end = 0; + for (size_t i = 0; i < m_endlines.size(); i++) { + if (m_text1.size() >= m_endlines[i]) { + const std::string line = m_text1.substr(last_end, m_endlines[i] - last_end); + last_end = m_endlines[i]; + if (m_text1.size() > m_endlines[i]) + last_end += (m_text1[m_endlines[i]] == '\n' || m_text1[m_endlines[i]] == ' ' ? 1 : 0); + ImGui::SetCursorPosX(m_left_indentation); + ImGui::SetCursorPosY(starting_y + i * m_line_height); + imgui.text(line.c_str()); + } + } + return starting_y + m_endlines.size() * m_line_height; +} + void NotificationManager::PopNotification::render_hyperlink_action(ImGuiWrapper& imgui, float text_x, float text_y, const std::string& text, const char* button_id, const std::function& on_click) { @@ -2458,6 +2476,36 @@ void NotificationManager::SharedProfilesNotification::render_hypertext(ImGuiWrap render_hyperlink_action(imgui, text_x, text_y, text, "##browse_btn", [this] { if (on_text_click()) close(); }); } +void NotificationManager::PreviewSlowRenderNotification::init() +{ + PopNotification::init(); + // A row for each link, so that a long translation of one does not push the other out. + m_lines_count = m_lines_count + 2; +} + +void NotificationManager::PreviewSlowRenderNotification::render_text(ImGuiWrapper& imgui, + const float win_size_x, const float win_size_y, + const float win_pos_x, const float win_pos_y) +{ + const float x_offset = m_left_indentation; + const float action_y = render_text_lines(imgui); + // TRN Links in the slow preview notification. "Shell only" and "Solid model" are options of "Simplify preview while dragging" in Preferences. + const std::string use_text = m_mode == "solid" ? _u8L("Use Solid model") : _u8L("Use Shell only"); + render_hyperlink_action(imgui, x_offset, action_y, use_text, "##preview_slow_use", [this] { + wxGetApp().CallAfter([mode = m_mode] { + if (Plater* plater = wxGetApp().plater()) + if (GLCanvas3D* canvas = plater->get_preview_canvas3D()) + canvas->apply_preview_reduced_detail_mode(mode); + }); + close(); + }); + render_hyperlink_action(imgui, x_offset, action_y + m_line_height, _u8L("Don't show again"), "##preview_slow_dont_show", [this] { + wxGetApp().app_config->set_bool("show_preview_slow_render_hint", false); + wxGetApp().app_config->save(); + close(); + }); +} + void NotificationManager::OrcaSyncConflictNotification::init() { PopNotification::init(); @@ -2469,25 +2517,8 @@ void NotificationManager::OrcaSyncConflictNotification::render_text(ImGuiWrapper const float win_size_x, const float win_size_y, const float win_pos_x, const float win_pos_y) { - float x_offset = m_left_indentation; - float shift_y = m_line_height; - float starting_y = m_line_height / 2; - - int last_end = 0; - std::string line; - for (size_t i = 0; i < m_endlines.size(); i++) { - if (m_text1.size() >= m_endlines[i]) { - line = m_text1.substr(last_end, m_endlines[i] - last_end); - last_end = m_endlines[i]; - if (m_text1.size() > m_endlines[i]) - last_end += (m_text1[m_endlines[i]] == '\n' || m_text1[m_endlines[i]] == ' ' ? 1 : 0); - ImGui::SetCursorPosX(x_offset); - ImGui::SetCursorPosY(starting_y + i * shift_y); - imgui.text(line.c_str()); - } - } - - const float action_y = starting_y + m_endlines.size() * shift_y; + const float x_offset = m_left_indentation; + const float action_y = render_text_lines(imgui); const std::string pull_text = conflict_code == -3 ? _u8L("Delete") : _u8L("Pull"); render_hyperlink_action(imgui, x_offset, action_y, pull_text, "##orca_sync_pull", [this] { if (m_pull_callback && m_pull_callback(m_evt_handler)) close(); }); @@ -2508,6 +2539,18 @@ void NotificationManager::push_shared_profiles_notification(const std::string& e push_notification_data(std::make_unique(data, m_id_provider, m_evt_handler, explore_url), 0); } +void NotificationManager::push_preview_slow_render_notification(int fps, const std::string& mode) +{ + close_notification_of_type(NotificationType::PreviewSlowRender); + const std::string text = mode == "solid" ? + // TRN %1% is the measured frame rate. "Solid model" is an option of "Simplify preview while dragging" in Preferences. + format(_u8L("The preview is rendering slowly (about %1% fps) while you move it. Solid model draws the objects as solid shapes while dragging so the view moves faster."), fps) : + // TRN %1% is the measured frame rate. "Shell only" is an option of "Simplify preview while dragging" in Preferences. + format(_u8L("The preview is rendering slowly (about %1% fps) while you move it. Shell only hides the infill while dragging so the view moves faster."), fps); + NotificationData data{ NotificationType::PreviewSlowRender, NotificationLevel::RegularNotificationLevel, 0, text }; + push_notification_data(std::make_unique(data, m_id_provider, m_evt_handler, mode), 0); +} + void NotificationManager::push_orca_sync_conflict_notification(const std::string& text, int conflict_code, std::function pull_callback, @@ -3353,6 +3396,8 @@ void NotificationManager::set_in_preview(bool preview) notification->hide(preview); if (notification->get_type() == NotificationType::SignDetected) notification->hide(!preview); + if (notification->get_type() == NotificationType::PreviewSlowRender) + notification->hide(!preview); if (notification->get_type() == NotificationType::BBLObjectInfo) notification->hide(preview); if (notification->get_type() == NotificationType::BBLSeqPrintInfo) diff --git a/src/slic3r/GUI/NotificationManager.hpp b/src/slic3r/GUI/NotificationManager.hpp index 809a03fe5f..84c6a22798 100644 --- a/src/slic3r/GUI/NotificationManager.hpp +++ b/src/slic3r/GUI/NotificationManager.hpp @@ -186,6 +186,8 @@ enum class NotificationType // A mixed-color filament is printed on a single-nozzle printer (frequent changes and purging). BBLSingleExtruderMixedFilamentRisk, OrcaSharedProfilesAvailable, + // The sliced preview keeps drawing slowly while dragged; suggests a cheaper drag mode. + PreviewSlowRender, OrcaCloudAPIError, OrcaSyncConflict, // Active preset requires plugin capabilities that are not installed/loadable. Persistent, @@ -323,6 +325,9 @@ public: // Shared profiles available for selected printer void push_shared_profiles_notification(const std::string& explore_url); + // fps is the measured frame rate, shown in the text. + // mode: the drag mode the notification offers, "shell" or "solid" + void push_preview_slow_render_notification(int fps, const std::string& mode); void push_orca_sync_conflict_notification(const std::string& text, int conflict_code, std::function pull_callback, @@ -549,6 +554,8 @@ private: virtual void render_text(ImGuiWrapper& imgui, const float win_size_x, const float win_size_y, const float win_pos_x , const float win_pos_y); + // Draws every wrapped line of the text from the top of the window; returns the y of the row below it. + float render_text_lines(ImGuiWrapper& imgui); virtual void render_close_button(ImGuiWrapper& imgui, const float win_size_x, const float win_size_y, const float win_pos_x , const float win_pos_y); @@ -959,6 +966,28 @@ private: bool m_dont_show_clicked{ false }; }; + class PreviewSlowRenderNotification : public PopNotification + { + public: + PreviewSlowRenderNotification(const NotificationData& n, NotificationIDProvider& id_provider, wxEvtHandler* evt_handler, + const std::string& mode) + : PopNotification(n, id_provider, evt_handler) + , m_mode(mode) + { + m_multiline = true; + } + protected: + void init() override; + void render_text(ImGuiWrapper& imgui, + const float win_size_x, const float win_size_y, + const float win_pos_x, const float win_pos_y) override; + // Collapsed, the window would hide the links with nothing to bring them back. + void render_minimize_button(ImGuiWrapper& /*imgui*/, + const float /*win_pos_x*/, const float /*win_pos_y*/) override { m_minimize_b_visible = false; } + + std::string m_mode; + }; + class OrcaSyncConflictNotification : public PopNotification { public: diff --git a/src/slic3r/GUI/Operations/PreferenceOps.cpp b/src/slic3r/GUI/Operations/PreferenceOps.cpp index ffc147cec4..d88e32b0c9 100644 --- a/src/slic3r/GUI/Operations/PreferenceOps.cpp +++ b/src/slic3r/GUI/Operations/PreferenceOps.cpp @@ -350,6 +350,10 @@ std::vector build_table() "outside.\n" "The bottom and top layers of the visible range are always drawn whole; a layer with no infill of its own, as with infill " "combination, takes the layers down to the nearest one with infill along.")}); + t.checkbox("show_preview_slow_render_hint", L("Warn when the preview renders slowly"), + // TRN "Shell only" and "Solid model" are values of "Simplify preview while dragging" above; keep them as translated there. + {L("Show a notification when the sliced preview draws slower than 15 frames per second for 5 seconds while you drag it. " + "It suggests Shell only, or Solid model when Shell only is already on or would not help.")}); t.checkbox("preview_dim_previous_layers", L("Dim lower layers"), {L("When scrubbing the layer slider in the sliced preview, render the layers below the current one darkened so that only " "the layer being viewed is shown at full brightness.")}); @@ -835,10 +839,8 @@ void apply_preference(const std::string& key) } } else if (key == "preview_reduced_detail_mode") { // apply the new mode immediately to the currently loaded preview - if (GLCanvas3D* canvas = preview_canvas()) { - canvas->get_gcode_viewer().set_reduced_detail_mode(config.get(key)); - redraw_preview(*canvas); - } + if (GLCanvas3D* canvas = preview_canvas()) + canvas->apply_preview_reduced_detail_mode(config.get(key)); #ifdef __WXMSW__ } else if (key == "associate_3mf" || key == "associate_drc" || key == "associate_stl" || key == "associate_step") { const std::wstring extension = wxString::FromUTF8(key.substr(std::string("associate_").size())).ToStdWstring(); diff --git a/src/slic3r/GUI/PreviewFrameRateMonitor.hpp b/src/slic3r/GUI/PreviewFrameRateMonitor.hpp new file mode 100644 index 0000000000..1c2dd52687 --- /dev/null +++ b/src/slic3r/GUI/PreviewFrameRateMonitor.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include +#include + +namespace Slic3r::GUI { + +// Tells whether the sliced preview keeps drawing slower than min_fps while the user moves it. +// It is fed the render time of each frame drawn during an interaction, not the time between frames: +// the preview draws only on demand, so the spacing of frames follows the mouse and the FPS cap rather +// than the machine. Render time is summed into one-second buckets, and slow_seconds_needed slow buckets +// in a row report a slow preview. +class PreviewFrameRateMonitor +{ +public: + static constexpr double min_fps = 15.; + static constexpr int slow_seconds_needed = 5; + + // Adds one frame's render time. Returns the average frame rate over the streak when the frame closes + // the last slow bucket of a streak, and starts over; otherwise std::nullopt. + std::optional add_frame(double frame_ms) + { + m_bucket_ms += frame_ms; + ++m_bucket_frames; + if (m_bucket_ms < bucket_ms) + return std::nullopt; + + // A closed bucket starts the next one from zero, so a long frame never spills into the next second. + if (m_bucket_frames * 1000. < min_fps * m_bucket_ms) { + ++m_slow_buckets; + m_streak_ms += m_bucket_ms; + m_streak_frames += m_bucket_frames; + } else { + m_slow_buckets = 0; + m_streak_ms = 0.; + m_streak_frames = 0; + } + m_bucket_ms = 0.; + m_bucket_frames = 0; + + if (m_slow_buckets < slow_seconds_needed) + return std::nullopt; + const double fps = m_streak_frames * 1000. / m_streak_ms; + reset(); + return fps; + } + + void reset() { *this = PreviewFrameRateMonitor(); } + +private: + static constexpr double bucket_ms = 1000.; + + double m_bucket_ms{0.}; + int m_bucket_frames{0}; + int m_slow_buckets{0}; + double m_streak_ms{0.}; + int m_streak_frames{0}; +}; + +// The drag mode to offer a preview that stays slow in `mode`: Shell only first, then Solid model, which +// draws the least. A mode that would draw the same toolpaths for this print is skipped. +inline std::optional suggested_preview_drag_mode(const std::string& mode, bool shell_hides_infill, bool solid_usable) +{ + if (mode == "off" && shell_hides_infill) + return std::string("shell"); + if ((mode == "off" || mode == "shell") && solid_usable) + return std::string("solid"); + return std::nullopt; +} + +} // namespace Slic3r::GUI diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 38aa5a729a..aa951e68a4 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(${_TEST_NAME}_tests test_part_plate.cpp test_scene_raycaster.cpp test_scene_benchmark.cpp + test_preview_frame_rate_monitor.cpp test_lazy.cpp test_mcp_activity_log.cpp test_mcp_config.cpp diff --git a/tests/slic3rutils/test_preview_frame_rate_monitor.cpp b/tests/slic3rutils/test_preview_frame_rate_monitor.cpp new file mode 100644 index 0000000000..2658909d52 --- /dev/null +++ b/tests/slic3rutils/test_preview_frame_rate_monitor.cpp @@ -0,0 +1,105 @@ +#include +#include +#include + +#include +#include + +#include "slic3r/GUI/PreviewFrameRateMonitor.hpp" + +using Slic3r::GUI::PreviewFrameRateMonitor; +using Catch::Matchers::WithinAbs; + +namespace { + +// Feeds `count` frames of `frame_ms` and returns how many of them reported a slow preview. +int count_reports(PreviewFrameRateMonitor& monitor, int count, double frame_ms) +{ + int reports = 0; + for (int i = 0; i < count; ++i) + if (monitor.add_frame(frame_ms)) + ++reports; + return reports; +} + +} // namespace + +TEST_CASE("Fast frames never report a slow preview", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + // 10 ms frames are 100 fps. + REQUIRE(count_reports(monitor, 1000, 10.) == 0); +} + +TEST_CASE("Five slow seconds in a row report the average frame rate", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + // 100 ms frames are 10 fps: a one-second bucket closes every 10 frames, so the fifth closes on frame 50. + REQUIRE(count_reports(monitor, 49, 100.) == 0); + const std::optional fps = monitor.add_frame(100.); + REQUIRE(fps.has_value()); + REQUIRE_THAT(*fps, WithinAbs(10., 0.01)); +} + +TEST_CASE("A fast second resets the slow streak", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + REQUIRE(count_reports(monitor, 40, 100.) == 0); // four slow seconds + REQUIRE(count_reports(monitor, 100, 10.) == 0); // one fast second + REQUIRE(count_reports(monitor, 40, 100.) == 0); // four slow seconds again +} + +TEST_CASE("Frames just faster than 15 fps never report a slow preview", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + // 66 ms is 15.15 fps. + REQUIRE(count_reports(monitor, 1000, 66.) == 0); +} + +TEST_CASE("Frames just slower than 15 fps report a slow preview", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + // 68 ms is 14.7 fps. + REQUIRE(count_reports(monitor, 1000, 68.) > 0); +} + +TEST_CASE("A single long frame counts as one slow second", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + // The first frame after a load can take seconds; on its own it must not report. + REQUIRE_FALSE(monitor.add_frame(3000.).has_value()); + REQUIRE(count_reports(monitor, 100, 10.) == 0); +} + +TEST_CASE("Reset drops a partial slow streak", "[PreviewFrameRateMonitor]") +{ + PreviewFrameRateMonitor monitor; + REQUIRE(count_reports(monitor, 40, 100.) == 0); + monitor.reset(); + REQUIRE(count_reports(monitor, 40, 100.) == 0); +} + +using Slic3r::GUI::suggested_preview_drag_mode; + +TEST_CASE("A slow preview with the mode off is offered Shell only", "[PreviewFrameRateMonitor]") +{ + REQUIRE(suggested_preview_drag_mode("off", true, true) == std::optional("shell")); + REQUIRE(suggested_preview_drag_mode("off", true, false) == std::optional("shell")); +} + +TEST_CASE("A slow preview where Shell only hides nothing is offered Solid model", "[PreviewFrameRateMonitor]") +{ + REQUIRE(suggested_preview_drag_mode("off", false, true) == std::optional("solid")); + REQUIRE_FALSE(suggested_preview_drag_mode("off", false, false).has_value()); +} + +TEST_CASE("A preview still slow in Shell only is offered Solid model", "[PreviewFrameRateMonitor]") +{ + REQUIRE(suggested_preview_drag_mode("shell", true, true) == std::optional("solid")); + REQUIRE_FALSE(suggested_preview_drag_mode("shell", true, false).has_value()); +} + +TEST_CASE("A preview already in Solid model is offered nothing", "[PreviewFrameRateMonitor]") +{ + REQUIRE_FALSE(suggested_preview_drag_mode("solid", true, true).has_value()); +}