From 82001626ddd81a497f45797a65343c2914ba902a Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Tue, 30 Jan 2024 13:38:48 +0100 Subject: [PATCH] SPE-2124: Added command line option 'opengl-aa' to allow the user to turn on the automatic selection of max number of supported samples for OpenGL antialising --- src/PrusaSlicer.cpp | 21 +++++++++++++++++++-- src/libslic3r/PrintConfig.cpp | 8 ++++++++ src/libslic3r/Technologies.hpp | 3 +++ src/slic3r/GUI/GUI_App.cpp | 4 ---- src/slic3r/GUI/GUI_Init.hpp | 3 +++ src/slic3r/GUI/GUI_Preview.cpp | 13 +++++++++++++ src/slic3r/GUI/OpenGLManager.cpp | 29 ++++++++++++++++++++++++++--- src/slic3r/GUI/OpenGLManager.hpp | 16 ++++++++++++---- 8 files changed, 84 insertions(+), 13 deletions(-) diff --git a/src/PrusaSlicer.cpp b/src/PrusaSlicer.cpp index 0acdea245b..6eb7f6019e 100644 --- a/src/PrusaSlicer.cpp +++ b/src/PrusaSlicer.cpp @@ -181,13 +181,23 @@ int CLI::run(int argc, char **argv) return 1; #ifdef SLIC3R_GUI + std::vector::iterator it; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + bool opengl_aa = false; + it = std::find(m_actions.begin(), m_actions.end(), "opengl-aa"); + if (it != m_actions.end()) { + start_gui = true; + opengl_aa = true; + m_actions.erase(it); + } +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES #if ENABLE_GL_CORE_PROFILE std::pair opengl_version = { 0, 0 }; bool opengl_debug = false; bool opengl_compatibility_profile = false; // search for special keys into command line parameters - auto it = std::find(m_actions.begin(), m_actions.end(), "gcodeviewer"); + it = std::find(m_actions.begin(), m_actions.end(), "gcodeviewer"); if (it != m_actions.end()) { start_gui = true; start_as_gcodeviewer = true; @@ -242,14 +252,18 @@ int CLI::run(int argc, char **argv) #endif // ENABLE_GL_CORE_PROFILE #else // SLIC3R_GUI // If there is no GUI, we shall ignore the parameters. Remove them from the list. +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + for (const std::string& s : { "opengl-version", "opengl-compatibility", "opengl-debug", "opengl-aa", "gcodeviewer" }) { +#else for (const std::string& s : { "opengl-version", "opengl-compatibility", "opengl-debug", "gcodeviewer" }) { +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES auto it = std::find(m_actions.cbegin(), m_actions.cend(), s); if (it != m_actions.end()) { boost::nowide::cerr << "Parameter '" << s << "' is ignored, this PrusaSlicer build is CLI only." << std::endl; m_actions.erase(it); } } -#endif +#endif // SLIC3R_GUI // Read input file(s) if any. @@ -743,6 +757,9 @@ int CLI::run(int argc, char **argv) params.start_downloader = start_downloader; params.download_url = download_url; params.delete_after_load = delete_after_load; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + params.opengl_aa = opengl_aa; +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES #if ENABLE_GL_CORE_PROFILE params.opengl_version = opengl_version; params.opengl_debug = opengl_debug; diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 8f57c70f80..6d4cbcd9e9 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -4915,6 +4915,14 @@ CLIActionsConfigDef::CLIActionsConfigDef() def->cli = "gcodeviewer"; def->set_default_value(new ConfigOptionBool(false)); +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + def = this->add("opengl-aa", coBool); + def->label = L("Automatic OpenGL antialising samples number selection"); + def->tooltip = L("Automatically select the highest number of samples for OpenGL antialising."); + def->cli = "opengl-aa"; + def->set_default_value(new ConfigOptionBool(false)); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES + #if ENABLE_GL_CORE_PROFILE def = this->add("opengl-version", coString); def->label = L("OpenGL version"); diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index b96bf8926d..8ea8d649ac 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -54,6 +54,9 @@ // Enable OpenGL core profile context (tested against Mesa 20.1.8 on Windows) #define ENABLE_GL_CORE_PROFILE (1 && !ENABLE_OPENGL_ES) +// Enable automatic detection of highest number of samples supported for OpenGL antialising +#define ENABLE_OPENGL_AUTO_AA_SAMPLES 1 + // Enable imgui dialog which allows to set the parameters used to export binarized gcode #define ENABLE_BINARIZED_GCODE_DEBUG_WINDOW 0 diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 115eba8351..e69568614b 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -2652,11 +2652,7 @@ void GUI_App::open_preferences(const std::string& highlight_option /*= std::stri if (mainframe->preferences_dialog->recreate_GUI()) recreate_GUI(_L("Restart application") + dots); -#if ENABLE_GCODE_LINES_ID_IN_H_SLIDER - if (dlg.seq_top_layer_only_changed() || dlg.seq_seq_top_gcode_indices_changed()) -#else if (mainframe->preferences_dialog->seq_top_layer_only_changed()) -#endif // ENABLE_GCODE_LINES_ID_IN_H_SLIDER this->plater_->refresh_print(); #ifdef _WIN32 diff --git a/src/slic3r/GUI/GUI_Init.hpp b/src/slic3r/GUI/GUI_Init.hpp index b21da42a5e..2fb0429f05 100644 --- a/src/slic3r/GUI/GUI_Init.hpp +++ b/src/slic3r/GUI/GUI_Init.hpp @@ -38,6 +38,9 @@ struct GUI_InitParams bool opengl_debug; bool opengl_compatibiity_profile; #endif // ENABLE_GL_CORE_PROFILE +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + bool opengl_aa; +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES }; int GUI_Run(GUI_InitParams ¶ms); diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index c392942666..2650bfb0d4 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -10,6 +10,9 @@ #include "GUI_Preview.hpp" #include "GUI_App.hpp" #include "GUI.hpp" +#if ENABLE_OPENGL_AUTO_AA_SAMPLES +#include "GUI_Init.hpp" +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES #include "I18N.hpp" #include "3DScene.hpp" #include "BackgroundSlicingProcess.hpp" @@ -61,7 +64,12 @@ bool View3D::init(wxWindow* parent, Bed3D& bed, Model* model, DynamicPrintConfig if (!Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 /* disable wxTAB_TRAVERSAL */)) return false; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + const GUI_InitParams* const init_params = wxGetApp().init_params; + m_canvas_widget = OpenGLManager::create_wxglcanvas(*this, (init_params != nullptr) ? init_params->opengl_aa : false); +#else m_canvas_widget = OpenGLManager::create_wxglcanvas(*this); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES if (m_canvas_widget == nullptr) return false; @@ -205,7 +213,12 @@ bool Preview::init(wxWindow* parent, Bed3D& bed, Model* model) SetBackgroundColour(GetParent()->GetBackgroundColour()); #endif // _WIN32 +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + const GUI_InitParams* const init_params = wxGetApp().init_params; + m_canvas_widget = OpenGLManager::create_wxglcanvas(*this, (init_params != nullptr) ? init_params->opengl_aa : false); +#else m_canvas_widget = OpenGLManager::create_wxglcanvas(*this); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES if (m_canvas_widget == nullptr) return false; diff --git a/src/slic3r/GUI/OpenGLManager.cpp b/src/slic3r/GUI/OpenGLManager.cpp index 3ca88d96e0..4c5dc81782 100644 --- a/src/slic3r/GUI/OpenGLManager.cpp +++ b/src/slic3r/GUI/OpenGLManager.cpp @@ -131,8 +131,10 @@ void OpenGLManager::GLInfo::detect() const if (!GLEW_ARB_compatibility) *const_cast(&m_core_profile) = true; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES int* samples = const_cast(&m_samples); glsafe(::glGetIntegerv(GL_SAMPLES, samples)); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES *const_cast(&m_detected) = true; } @@ -212,7 +214,9 @@ std::string OpenGLManager::GLInfo::to_string(bool for_github) const out << b_start << "Renderer: " << b_end << m_renderer << line_end; out << b_start << "GLSL version: " << b_end << m_glsl_version << line_end; out << b_start << "Textures compression: " << b_end << (are_compressed_textures_supported() ? "Enabled" : "Disabled") << line_end; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES out << b_start << "Mutisampling: " << b_end << (can_multisample() ? "Enabled (" + std::to_string(m_samples) + " samples)" : "Disabled") << line_end; +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES { #if ENABLE_GL_CORE_PROFILE @@ -541,15 +545,20 @@ wxGLContext* OpenGLManager::init_glcontext(wxGLCanvas& canvas) return m_context; } +#if ENABLE_OPENGL_AUTO_AA_SAMPLES +wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent, bool enable_auto_aa_samples) +#else wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent) +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES { #if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES wxGLAttributes attribList; +#if ENABLE_OPENGL_AUTO_AA_SAMPLES s_multisample = EMultisampleState::Disabled; // Disable multi-sampling on ChromeOS, as the OpenGL virtualization swaps Red/Blue channels with multi-sampling enabled, // at least on some platforms. if (platform_flavor() != PlatformFlavor::LinuxOnChromium) { - for (int i = 16; i >= 4; i /= 2) { + for (int i = enable_auto_aa_samples ? 16 : 4; i >= 4; i /= 2) { attribList.Reset(); attribList.PlatformDefaults().RGBA().DoubleBuffer().MinRGBA(8, 8, 8, 8).Depth(24).SampleBuffers(1).Samplers(i).EndList(); if (wxGLCanvas::IsDisplaySupported(attribList)) { @@ -558,6 +567,9 @@ wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent) } } } +#else + attribList.PlatformDefaults().RGBA().DoubleBuffer().MinRGBA(8, 8, 8, 8).Depth(24).SampleBuffers(1).Samplers(4).EndList(); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES #ifdef __APPLE__ // on MAC the method RGBA() has no effect attribList.SetNeedsARB(true); @@ -580,7 +592,14 @@ wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent) }; #endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES +#if ENABLE_OPENGL_AUTO_AA_SAMPLES if (s_multisample != EMultisampleState::Enabled) +#else + if (s_multisample == EMultisampleState::Unknown) + detect_multisample(attribList); + + if (!can_multisample()) +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES #if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES { attribList.Reset(); @@ -599,8 +618,12 @@ wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent) #endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES } -#if !ENABLE_GL_CORE_PROFILE && !ENABLE_OPENGL_ES +#if !ENABLE_OPENGL_AUTO_AA_SAMPLES +#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES +void OpenGLManager::detect_multisample(const wxGLAttributes& attribList) +#else void OpenGLManager::detect_multisample(int* attribList) +#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES { int wxVersion = wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + wxRELEASE_NUMBER; bool enable_multisample = wxVersion >= 30003; @@ -614,7 +637,7 @@ void OpenGLManager::detect_multisample(int* attribList) // Alternative method: it was working on previous version of wxWidgets but not with the latest, at least on Windows // s_multisample = enable_multisample && wxGLCanvas::IsExtensionSupported("WGL_ARB_multisample"); } -#endif // !ENABLE_GL_CORE_PROFILE && !ENABLE_OPENGL_ES +#endif // !ENABLE_OPENGL_AUTO_AA_SAMPLES } // namespace GUI } // namespace Slic3r diff --git a/src/slic3r/GUI/OpenGLManager.hpp b/src/slic3r/GUI/OpenGLManager.hpp index 35169e5219..db59516212 100644 --- a/src/slic3r/GUI/OpenGLManager.hpp +++ b/src/slic3r/GUI/OpenGLManager.hpp @@ -34,9 +34,9 @@ public: bool m_core_profile{ false }; int m_max_tex_size{ 0 }; float m_max_anisotropy{ 0.0f }; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +#if ENABLE_OPENGL_AUTO_AA_SAMPLES int m_samples{ 0 }; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES std::string m_version_string; Semver m_version = Semver::invalid(); @@ -136,14 +136,22 @@ public: static bool can_multisample() { return s_multisample == EMultisampleState::Enabled; } static bool are_framebuffers_supported() { return (s_framebuffers_type != EFramebufferType::Unknown); } static EFramebufferType get_framebuffers_type() { return s_framebuffers_type; } +#if ENABLE_OPENGL_AUTO_AA_SAMPLES + static wxGLCanvas* create_wxglcanvas(wxWindow& parent, bool enable_auto_aa_samples); +#else static wxGLCanvas* create_wxglcanvas(wxWindow& parent); +#endif // ENABLE_OPENGL_AUTO_AA_SAMPLES static const GLInfo& get_gl_info() { return s_gl_info; } static bool force_power_of_two_textures() { return s_force_power_of_two_textures; } private: -#if !ENABLE_GL_CORE_PROFILE && !ENABLE_OPENGL_ES +#if !ENABLE_OPENGL_AUTO_AA_SAMPLES +#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES + static void detect_multisample(const wxGLAttributes& attribList); +#else static void detect_multisample(int* attribList); -#endif // !ENABLE_GL_CORE_PROFILE && !ENABLE_OPENGL_ES +#endif // ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES +#endif // !ENABLE_OPENGL_AUTO_AA_SAMPLES }; } // namespace GUI