1 #include "libslic3r/libslic3r.h"
2 #include "OpenGLManager.hpp"
3
4 #include "GUI.hpp"
5 #include "I18N.hpp"
6 #include "3DScene.hpp"
7
8 #include "libslic3r/Platform.hpp"
9
10 #include <GL/glew.h>
11
12 #include <boost/algorithm/string/split.hpp>
13 #include <boost/algorithm/string/classification.hpp>
14 #include <boost/log/trivial.hpp>
15
16 #include <wx/glcanvas.h>
17 #include <wx/msgdlg.h>
18
19 #if ENABLE_HACK_CLOSING_ON_OSX_10_9_5
20 #ifdef __APPLE__
21 // Part of hack to remove crash when closing the application on OSX 10.9.5 when building against newer wxWidgets
22 #include <wx/platinfo.h>
23 #endif // __APPLE__
24 #endif // ENABLE_HACK_CLOSING_ON_OSX_10_9_5
25
26 #ifdef __APPLE__
27 #include "../Utils/MacDarkMode.hpp"
28 #endif // __APPLE__
29
30 namespace Slic3r {
31 namespace GUI {
32
33 // A safe wrapper around glGetString to report a "N/A" string in case glGetString returns nullptr.
gl_get_string_safe(GLenum param,const std::string & default_value)34 inline std::string gl_get_string_safe(GLenum param, const std::string& default_value)
35 {
36 const char* value = (const char*)::glGetString(param);
37 return std::string((value != nullptr) ? value : default_value);
38 }
39
get_version() const40 const std::string& OpenGLManager::GLInfo::get_version() const
41 {
42 if (!m_detected)
43 detect();
44
45 return m_version;
46 }
47
get_glsl_version() const48 const std::string& OpenGLManager::GLInfo::get_glsl_version() const
49 {
50 if (!m_detected)
51 detect();
52
53 return m_glsl_version;
54 }
55
get_vendor() const56 const std::string& OpenGLManager::GLInfo::get_vendor() const
57 {
58 if (!m_detected)
59 detect();
60
61 return m_vendor;
62 }
63
get_renderer() const64 const std::string& OpenGLManager::GLInfo::get_renderer() const
65 {
66 if (!m_detected)
67 detect();
68
69 return m_renderer;
70 }
71
get_max_tex_size() const72 int OpenGLManager::GLInfo::get_max_tex_size() const
73 {
74 if (!m_detected)
75 detect();
76
77 // clamp to avoid the texture generation become too slow and use too much GPU memory
78 #ifdef __APPLE__
79 // and use smaller texture for non retina systems
80 return (Slic3r::GUI::mac_max_scaling_factor() > 1.0) ? std::min(m_max_tex_size, 8192) : std::min(m_max_tex_size / 2, 4096);
81 #else
82 // and use smaller texture for older OpenGL versions
83 return is_version_greater_or_equal_to(3, 0) ? std::min(m_max_tex_size, 8192) : std::min(m_max_tex_size / 2, 4096);
84 #endif // __APPLE__
85 }
86
get_max_anisotropy() const87 float OpenGLManager::GLInfo::get_max_anisotropy() const
88 {
89 if (!m_detected)
90 detect();
91
92 return m_max_anisotropy;
93 }
94
detect() const95 void OpenGLManager::GLInfo::detect() const
96 {
97 m_version = gl_get_string_safe(GL_VERSION, "N/A");
98 m_glsl_version = gl_get_string_safe(GL_SHADING_LANGUAGE_VERSION, "N/A");
99 m_vendor = gl_get_string_safe(GL_VENDOR, "N/A");
100 m_renderer = gl_get_string_safe(GL_RENDERER, "N/A");
101
102 glsafe(::glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_max_tex_size));
103
104 m_max_tex_size /= 2;
105
106 if (Slic3r::total_physical_memory() / (1024 * 1024 * 1024) < 6)
107 m_max_tex_size /= 2;
108
109 if (GLEW_EXT_texture_filter_anisotropic)
110 glsafe(::glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_max_anisotropy));
111
112 m_detected = true;
113 }
114
version_greater_or_equal_to(const std::string & version,unsigned int major,unsigned int minor)115 static bool version_greater_or_equal_to(const std::string& version, unsigned int major, unsigned int minor)
116 {
117 if (version == "N/A")
118 return false;
119
120 std::vector<std::string> tokens;
121 boost::split(tokens, version, boost::is_any_of(" "), boost::token_compress_on);
122
123 if (tokens.empty())
124 return false;
125
126 std::vector<std::string> numbers;
127 boost::split(numbers, tokens[0], boost::is_any_of("."), boost::token_compress_on);
128
129 unsigned int gl_major = 0;
130 unsigned int gl_minor = 0;
131
132 if (numbers.size() > 0)
133 gl_major = ::atoi(numbers[0].c_str());
134
135 if (numbers.size() > 1)
136 gl_minor = ::atoi(numbers[1].c_str());
137
138 if (gl_major < major)
139 return false;
140 else if (gl_major > major)
141 return true;
142 else
143 return gl_minor >= minor;
144 }
145
is_version_greater_or_equal_to(unsigned int major,unsigned int minor) const146 bool OpenGLManager::GLInfo::is_version_greater_or_equal_to(unsigned int major, unsigned int minor) const
147 {
148 if (!m_detected)
149 detect();
150
151 return version_greater_or_equal_to(m_version, major, minor);
152 }
153
is_glsl_version_greater_or_equal_to(unsigned int major,unsigned int minor) const154 bool OpenGLManager::GLInfo::is_glsl_version_greater_or_equal_to(unsigned int major, unsigned int minor) const
155 {
156 if (!m_detected)
157 detect();
158
159 return version_greater_or_equal_to(m_glsl_version, major, minor);
160 }
161
to_string(bool format_as_html,bool extensions) const162 std::string OpenGLManager::GLInfo::to_string(bool format_as_html, bool extensions) const
163 {
164 if (!m_detected)
165 detect();
166
167 std::stringstream out;
168
169 std::string h2_start = format_as_html ? "<b>" : "";
170 std::string h2_end = format_as_html ? "</b>" : "";
171 std::string b_start = format_as_html ? "<b>" : "";
172 std::string b_end = format_as_html ? "</b>" : "";
173 std::string line_end = format_as_html ? "<br>" : "\n";
174
175 out << h2_start << "OpenGL installation" << h2_end << line_end;
176 out << b_start << "GL version: " << b_end << m_version << line_end;
177 out << b_start << "Vendor: " << b_end << m_vendor << line_end;
178 out << b_start << "Renderer: " << b_end << m_renderer << line_end;
179 out << b_start << "GLSL version: " << b_end << m_glsl_version << line_end;
180
181 if (extensions)
182 {
183 std::vector<std::string> extensions_list;
184 std::string extensions_str = gl_get_string_safe(GL_EXTENSIONS, "");
185 boost::split(extensions_list, extensions_str, boost::is_any_of(" "), boost::token_compress_off);
186
187 if (!extensions_list.empty())
188 {
189 out << h2_start << "Installed extensions:" << h2_end << line_end;
190
191 std::sort(extensions_list.begin(), extensions_list.end());
192 for (const std::string& ext : extensions_list)
193 {
194 out << ext << line_end;
195 }
196 }
197 }
198
199 return out.str();
200 }
201
202 OpenGLManager::GLInfo OpenGLManager::s_gl_info;
203 bool OpenGLManager::s_compressed_textures_supported = false;
204 OpenGLManager::EMultisampleState OpenGLManager::s_multisample = OpenGLManager::EMultisampleState::Unknown;
205 OpenGLManager::EFramebufferType OpenGLManager::s_framebuffers_type = OpenGLManager::EFramebufferType::Unknown;
206
207 #if ENABLE_HACK_CLOSING_ON_OSX_10_9_5
208 #ifdef __APPLE__
209 // Part of hack to remove crash when closing the application on OSX 10.9.5 when building against newer wxWidgets
210 OpenGLManager::OSInfo OpenGLManager::s_os_info;
211 #endif // __APPLE__
212 #endif // ENABLE_HACK_CLOSING_ON_OSX_10_9_5
213
~OpenGLManager()214 OpenGLManager::~OpenGLManager()
215 {
216 m_shaders_manager.shutdown();
217
218 #if ENABLE_HACK_CLOSING_ON_OSX_10_9_5
219 #ifdef __APPLE__
220 // This is an ugly hack needed to solve the crash happening when closing the application on OSX 10.9.5 with newer wxWidgets
221 // The crash is triggered inside wxGLContext destructor
222 if (s_os_info.major != 10 || s_os_info.minor != 9 || s_os_info.micro != 5)
223 {
224 #endif //__APPLE__
225 #endif // ENABLE_HACK_CLOSING_ON_OSX_10_9_5
226
227 if (m_context != nullptr)
228 delete m_context;
229
230 #if ENABLE_HACK_CLOSING_ON_OSX_10_9_5
231 #ifdef __APPLE__
232 }
233 #endif //__APPLE__
234 #endif // ENABLE_HACK_CLOSING_ON_OSX_10_9_5
235 }
236
init_gl()237 bool OpenGLManager::init_gl()
238 {
239 if (!m_gl_initialized) {
240 if (glewInit() != GLEW_OK) {
241 BOOST_LOG_TRIVIAL(error) << "Unable to init glew library";
242 /* Ugly fix for https://github.com/prusa3d/PrusaSlicer/issues/6396 */
243 //return false;
244 }
245 m_gl_initialized = true;
246 if (GLEW_EXT_texture_compression_s3tc)
247 s_compressed_textures_supported = true;
248 else
249 s_compressed_textures_supported = false;
250
251 if (GLEW_ARB_framebuffer_object)
252 s_framebuffers_type = EFramebufferType::Arb;
253 else if (GLEW_EXT_framebuffer_object)
254 s_framebuffers_type = EFramebufferType::Ext;
255 else
256 s_framebuffers_type = EFramebufferType::Unknown;
257
258 bool valid_version = s_gl_info.is_version_greater_or_equal_to(2, 0);
259 if (!valid_version) {
260 // Complain about the OpenGL version.
261 wxString message = from_u8((boost::format(
262 _utf8(L("PrusaSlicer requires OpenGL 2.0 capable graphics driver to run correctly, \n"
263 "while OpenGL version %s, render %s, vendor %s was detected."))) % s_gl_info.get_version() % s_gl_info.get_renderer() % s_gl_info.get_vendor()).str());
264 message += "\n";
265 message += _L("You may need to update your graphics card driver.");
266 #ifdef _WIN32
267 message += "\n";
268 message += _L("As a workaround, you may run PrusaSlicer with a software rendered 3D graphics by running prusa-slicer.exe with the --sw_renderer parameter.");
269 #endif
270 wxMessageBox(message, wxString("PrusaSlicer - ") + _L("Unsupported OpenGL version"), wxOK | wxICON_ERROR);
271 }
272
273 if (valid_version) {
274 // load shaders
275 auto [result, error] = m_shaders_manager.init();
276 if (!result) {
277 wxString message = from_u8((boost::format(
278 _utf8(L("Unable to load the following shaders:\n%s"))) % error).str());
279 wxMessageBox(message, wxString("PrusaSlicer - ") + _L("Error loading shaders"), wxOK | wxICON_ERROR);
280 }
281 }
282 }
283
284 return true;
285 }
286
init_glcontext(wxGLCanvas & canvas)287 wxGLContext* OpenGLManager::init_glcontext(wxGLCanvas& canvas)
288 {
289 if (m_context == nullptr) {
290 m_context = new wxGLContext(&canvas);
291
292 #if ENABLE_HACK_CLOSING_ON_OSX_10_9_5
293 #ifdef __APPLE__
294 // Part of hack to remove crash when closing the application on OSX 10.9.5 when building against newer wxWidgets
295 s_os_info.major = wxPlatformInfo::Get().GetOSMajorVersion();
296 s_os_info.minor = wxPlatformInfo::Get().GetOSMinorVersion();
297 s_os_info.micro = wxPlatformInfo::Get().GetOSMicroVersion();
298 #endif //__APPLE__
299 #endif // ENABLE_HACK_CLOSING_ON_OSX_10_9_5
300 }
301 return m_context;
302 }
303
create_wxglcanvas(wxWindow & parent)304 wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent)
305 {
306 int attribList[] = {
307 WX_GL_RGBA,
308 WX_GL_DOUBLEBUFFER,
309 // RGB channels each should be allocated with 8 bit depth. One should almost certainly get these bit depths by default.
310 WX_GL_MIN_RED, 8,
311 WX_GL_MIN_GREEN, 8,
312 WX_GL_MIN_BLUE, 8,
313 // Requesting an 8 bit alpha channel. Interestingly, the NVIDIA drivers would most likely work with some alpha plane, but glReadPixels would not return
314 // the alpha channel on NVIDIA if not requested when the GL context is created.
315 WX_GL_MIN_ALPHA, 8,
316 WX_GL_DEPTH_SIZE, 24,
317 WX_GL_SAMPLE_BUFFERS, GL_TRUE,
318 WX_GL_SAMPLES, 4,
319 0
320 };
321
322 if (s_multisample == EMultisampleState::Unknown)
323 {
324 detect_multisample(attribList);
325 // // debug output
326 // std::cout << "Multisample " << (can_multisample() ? "enabled" : "disabled") << std::endl;
327 }
328
329 if (! can_multisample())
330 attribList[12] = 0;
331
332 return new wxGLCanvas(&parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS);
333 }
334
detect_multisample(int * attribList)335 void OpenGLManager::detect_multisample(int* attribList)
336 {
337 int wxVersion = wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + wxRELEASE_NUMBER;
338 bool enable_multisample = wxVersion >= 30003;
339 s_multisample =
340 enable_multisample &&
341 // Disable multi-sampling on ChromeOS, as the OpenGL virtualization swaps Red/Blue channels with multi-sampling enabled,
342 // at least on some platforms.
343 platform_flavor() != PlatformFlavor::LinuxOnChromium &&
344 wxGLCanvas::IsDisplaySupported(attribList)
345 ? EMultisampleState::Enabled : EMultisampleState::Disabled;
346 // Alternative method: it was working on previous version of wxWidgets but not with the latest, at least on Windows
347 // s_multisample = enable_multisample && wxGLCanvas::IsExtensionSupported("WGL_ARB_multisample");
348 }
349
350 } // namespace GUI
351 } // namespace Slic3r
352