Use an unique opengl context

This commit is contained in:
Enrico Turri 2018-10-04 10:41:11 +02:00
parent f31cf684cb
commit 96745e12d7
8 changed files with 91 additions and 7 deletions

View File

@ -10,6 +10,8 @@
#define ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM (1 && ENABLE_1_42_0) #define ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM (1 && ENABLE_1_42_0)
// Add double click on gizmo grabbers to reset transformation components to their default value // Add double click on gizmo grabbers to reset transformation components to their default value
#define ENABLE_GIZMOS_RESET (1 && ENABLE_1_42_0) #define ENABLE_GIZMOS_RESET (1 && ENABLE_1_42_0)
// Uses a unique opengl context
#define ENABLE_USE_UNIQUE_GLCONTEXT (1 && ENABLE_1_42_0)
#endif // _technologies_h_ #endif // _technologies_h_

View File

@ -1956,7 +1956,6 @@ wxDEFINE_EVENT(EVT_GIZMO_SCALE, Vec3dEvent);
wxDEFINE_EVENT(EVT_GIZMO_ROTATE, Vec3dEvent); wxDEFINE_EVENT(EVT_GIZMO_ROTATE, Vec3dEvent);
wxDEFINE_EVENT(EVT_GIZMO_FLATTEN, Vec3dEvent); wxDEFINE_EVENT(EVT_GIZMO_FLATTEN, Vec3dEvent);
GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas) GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
: m_canvas(canvas) : m_canvas(canvas)
, m_context(nullptr) , m_context(nullptr)
@ -1986,7 +1985,9 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
{ {
if (m_canvas != nullptr) if (m_canvas != nullptr)
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
m_context = new wxGLContext(m_canvas); m_context = new wxGLContext(m_canvas);
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_timer = new wxTimer(m_canvas); m_timer = new wxTimer(m_canvas);
} }
} }
@ -2001,11 +2002,13 @@ GLCanvas3D::~GLCanvas3D()
m_timer = nullptr; m_timer = nullptr;
} }
#if !ENABLE_USE_UNIQUE_GLCONTEXT
if (m_context != nullptr) if (m_context != nullptr)
{ {
delete m_context; delete m_context;
m_context = nullptr; m_context = nullptr;
} }
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
} }
void GLCanvas3D::post_event(wxEvent &&event) void GLCanvas3D::post_event(wxEvent &&event)
@ -2095,6 +2098,7 @@ bool GLCanvas3D::init(bool useVBOs, bool use_legacy_opengl)
return true; return true;
} }
#if !ENABLE_USE_UNIQUE_GLCONTEXT
bool GLCanvas3D::set_current() bool GLCanvas3D::set_current()
{ {
if ((m_canvas != nullptr) && (m_context != nullptr)) if ((m_canvas != nullptr) && (m_context != nullptr))
@ -2102,6 +2106,7 @@ bool GLCanvas3D::set_current()
return false; return false;
} }
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
void GLCanvas3D::set_as_dirty() void GLCanvas3D::set_as_dirty()
{ {
@ -2117,9 +2122,11 @@ void GLCanvas3D::reset_volumes()
{ {
if (!m_volumes.empty()) if (!m_volumes.empty())
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
// ensures this canvas is current // ensures this canvas is current
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_volumes.release_geometry(); m_volumes.release_geometry();
m_volumes.clear(); m_volumes.clear();
@ -2514,7 +2521,11 @@ void GLCanvas3D::render()
return; return;
// ensures this canvas is current and initialized // ensures this canvas is current and initialized
#if ENABLE_USE_UNIQUE_GLCONTEXT
if (!_set_current() || !_3DScene::init(m_canvas))
#else
if (!set_current() || !_3DScene::init(m_canvas)) if (!set_current() || !_3DScene::init(m_canvas))
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
return; return;
if (m_force_zoom_to_bed_enabled) if (m_force_zoom_to_bed_enabled)
@ -2619,9 +2630,11 @@ void GLCanvas3D::reload_scene(bool force)
reset_volumes(); reset_volumes();
#if !ENABLE_USE_UNIQUE_GLCONTEXT
// ensures this canvas is current // ensures this canvas is current
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
set_bed_shape(dynamic_cast<const ConfigOptionPoints*>(m_config->option("bed_shape"))->values); set_bed_shape(dynamic_cast<const ConfigOptionPoints*>(m_config->option("bed_shape"))->values);
@ -2709,9 +2722,11 @@ void GLCanvas3D::load_gcode_preview(const GCodePreviewData& preview_data, const
{ {
if ((m_canvas != nullptr) && (m_print != nullptr)) if ((m_canvas != nullptr) && (m_print != nullptr))
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
// ensures that this canvas is current // ensures that this canvas is current
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
if (m_volumes.empty()) if (m_volumes.empty())
{ {
@ -3503,8 +3518,10 @@ Point GLCanvas3D::get_local_mouse_position() const
void GLCanvas3D::reset_legend_texture() void GLCanvas3D::reset_legend_texture()
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_legend_texture.reset(); m_legend_texture.reset();
} }
@ -3650,13 +3667,27 @@ bool GLCanvas3D::_init_toolbar()
return true; return true;
} }
#if ENABLE_USE_UNIQUE_GLCONTEXT
bool GLCanvas3D::_set_current()
{
if ((m_canvas != nullptr) && (m_context != nullptr))
return m_canvas->SetCurrent(*m_context);
return false;
}
#endif ENABLE_USE_UNIQUE_GLCONTEXT
void GLCanvas3D::_resize(unsigned int w, unsigned int h) void GLCanvas3D::_resize(unsigned int w, unsigned int h)
{ {
if ((m_canvas == nullptr) && (m_context == nullptr)) if ((m_canvas == nullptr) && (m_context == nullptr))
return; return;
// ensures that this canvas is current // ensures that this canvas is current
#if ENABLE_USE_UNIQUE_GLCONTEXT
_set_current();
#else
set_current(); set_current();
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
::glViewport(0, 0, w, h); ::glViewport(0, 0, w, h);
::glMatrixMode(GL_PROJECTION); ::glMatrixMode(GL_PROJECTION);
@ -4315,9 +4346,11 @@ int GLCanvas3D::_get_first_selected_volume_id(int object_id) const
void GLCanvas3D::_load_print_toolpaths() void GLCanvas3D::_load_print_toolpaths()
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
// ensures this canvas is current // ensures this canvas is current
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
if (m_print == nullptr) if (m_print == nullptr)
return; return;
@ -5427,24 +5460,30 @@ std::vector<float> GLCanvas3D::_parse_colors(const std::vector<std::string>& col
void GLCanvas3D::_generate_legend_texture(const GCodePreviewData& preview_data, const std::vector<float>& tool_colors) void GLCanvas3D::_generate_legend_texture(const GCodePreviewData& preview_data, const std::vector<float>& tool_colors)
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_legend_texture.generate(preview_data, tool_colors); m_legend_texture.generate(preview_data, tool_colors);
} }
void GLCanvas3D::_generate_warning_texture(const std::string& msg) void GLCanvas3D::_generate_warning_texture(const std::string& msg)
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_warning_texture.generate(msg); m_warning_texture.generate(msg);
} }
void GLCanvas3D::_reset_warning_texture() void GLCanvas3D::_reset_warning_texture()
{ {
#if !ENABLE_USE_UNIQUE_GLCONTEXT
if (!set_current()) if (!set_current())
return; return;
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
m_warning_texture.reset(); m_warning_texture.reset();
} }

View File

@ -552,11 +552,17 @@ public:
GLCanvas3D(wxGLCanvas* canvas); GLCanvas3D(wxGLCanvas* canvas);
~GLCanvas3D(); ~GLCanvas3D();
wxGLCanvas* widget() { return m_canvas; } #if ENABLE_USE_UNIQUE_GLCONTEXT
void set_context(wxGLContext* context) { m_context = context; }
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
wxGLCanvas* get_wxglcanvas() { return m_canvas; }
bool init(bool useVBOs, bool use_legacy_opengl); bool init(bool useVBOs, bool use_legacy_opengl);
#if !ENABLE_USE_UNIQUE_GLCONTEXT
bool set_current(); bool set_current();
#endif // !ENABLE_USE_UNIQUE_GLCONTEXT
void set_as_dirty(); void set_as_dirty();
@ -668,6 +674,9 @@ private:
bool _init_toolbar(); bool _init_toolbar();
#if ENABLE_USE_UNIQUE_GLCONTEXT
bool _set_current();
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
void _resize(unsigned int w, unsigned int h); void _resize(unsigned int w, unsigned int h);
BoundingBoxf3 _max_bounding_box() const; BoundingBoxf3 _max_bounding_box() const;

View File

@ -113,13 +113,29 @@ std::string GLCanvas3DManager::GLInfo::to_string(bool format_as_html, bool exten
GLCanvas3DManager::EMultisampleState GLCanvas3DManager::s_multisample = GLCanvas3DManager::MS_Unknown; GLCanvas3DManager::EMultisampleState GLCanvas3DManager::s_multisample = GLCanvas3DManager::MS_Unknown;
GLCanvas3DManager::GLCanvas3DManager() GLCanvas3DManager::GLCanvas3DManager()
#if ENABLE_USE_UNIQUE_GLCONTEXT
: m_context(nullptr)
, m_current(nullptr)
#else
: m_current(nullptr) : m_current(nullptr)
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
, m_gl_initialized(false) , m_gl_initialized(false)
, m_use_legacy_opengl(false) , m_use_legacy_opengl(false)
, m_use_VBOs(false) , m_use_VBOs(false)
{ {
} }
#if ENABLE_USE_UNIQUE_GLCONTEXT
GLCanvas3DManager::~GLCanvas3DManager()
{
if (m_context != nullptr)
{
delete m_context;
m_context = nullptr;
}
}
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
bool GLCanvas3DManager::add(wxGLCanvas* canvas) bool GLCanvas3DManager::add(wxGLCanvas* canvas)
{ {
if (canvas == nullptr) if (canvas == nullptr)
@ -133,6 +149,18 @@ bool GLCanvas3DManager::add(wxGLCanvas* canvas)
return false; return false;
canvas3D->bind_event_handlers(); canvas3D->bind_event_handlers();
#if ENABLE_USE_UNIQUE_GLCONTEXT
if (m_context == nullptr)
{
m_context = new wxGLContext(canvas);
if (m_context == nullptr)
return false;
}
canvas3D->set_context(m_context);
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
m_canvases.insert(CanvasesMap::value_type(canvas, canvas3D)); m_canvases.insert(CanvasesMap::value_type(canvas, canvas3D));
return true; return true;

View File

@ -52,6 +52,9 @@ class GLCanvas3DManager
typedef std::map<wxGLCanvas*, GLCanvas3D*> CanvasesMap; typedef std::map<wxGLCanvas*, GLCanvas3D*> CanvasesMap;
CanvasesMap m_canvases; CanvasesMap m_canvases;
#if ENABLE_USE_UNIQUE_GLCONTEXT
wxGLContext* m_context;
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
wxGLCanvas* m_current; wxGLCanvas* m_current;
GLInfo m_gl_info; GLInfo m_gl_info;
bool m_gl_initialized; bool m_gl_initialized;
@ -61,6 +64,9 @@ class GLCanvas3DManager
public: public:
GLCanvas3DManager(); GLCanvas3DManager();
#if ENABLE_USE_UNIQUE_GLCONTEXT
~GLCanvas3DManager();
#endif // ENABLE_USE_UNIQUE_GLCONTEXT
bool add(wxGLCanvas* canvas); bool add(wxGLCanvas* canvas);
bool remove(wxGLCanvas* canvas); bool remove(wxGLCanvas* canvas);

View File

@ -339,13 +339,13 @@ void GLToolbar::do_action(unsigned int item_id)
item->set_state(GLToolbarItem::Hover); item->set_state(GLToolbarItem::Hover);
m_parent.render(); m_parent.render();
item->do_action(m_parent.widget()); item->do_action(m_parent.get_wxglcanvas());
} }
else else
{ {
item->set_state(GLToolbarItem::HoverPressed); item->set_state(GLToolbarItem::HoverPressed);
m_parent.render(); m_parent.render();
item->do_action(m_parent.widget()); item->do_action(m_parent.get_wxglcanvas());
if (item->get_state() != GLToolbarItem::Disabled) if (item->get_state() != GLToolbarItem::Disabled)
{ {
// the item may get disabled during the action, if not, set it back to hover state // the item may get disabled during the action, if not, set it back to hover state

View File

@ -50,7 +50,7 @@ public:
Preview(wxNotebook* notebook, DynamicPrintConfig* config, Print* print, GCodePreviewData* gcode_preview_data); Preview(wxNotebook* notebook, DynamicPrintConfig* config, Print* print, GCodePreviewData* gcode_preview_data);
virtual ~Preview(); virtual ~Preview();
wxGLCanvas* get_canvas() { return m_canvas; } wxGLCanvas* get_wxglcanvas() { return m_canvas; }
void set_number_extruders(unsigned int number_extruders); void set_number_extruders(unsigned int number_extruders);
void reset_gcode_preview_data(); void reset_gcode_preview_data();

View File

@ -520,7 +520,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) :
canvas3D->Bind(EVT_GLTOOLBAR_ADD, &priv::on_action_add, this); canvas3D->Bind(EVT_GLTOOLBAR_ADD, &priv::on_action_add, this);
canvas3D->Bind(EVT_GLCANVAS_VIEWPORT_CHANGED, &priv::on_viewport_changed, this); canvas3D->Bind(EVT_GLCANVAS_VIEWPORT_CHANGED, &priv::on_viewport_changed, this);
preview->get_canvas()->Bind(EVT_GLCANVAS_VIEWPORT_CHANGED, &priv::on_viewport_changed, this); preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_VIEWPORT_CHANGED, &priv::on_viewport_changed, this);
q->Bind(EVT_SLICING_COMPLETED, &priv::on_update_print_preview, this); q->Bind(EVT_SLICING_COMPLETED, &priv::on_update_print_preview, this);
q->Bind(EVT_PROCESS_COMPLETED, &priv::on_process_completed, this); q->Bind(EVT_PROCESS_COMPLETED, &priv::on_process_completed, this);
@ -893,7 +893,7 @@ void Plater::priv::on_action_add(SimpleEvent&)
void Plater::priv::on_viewport_changed(SimpleEvent& evt) void Plater::priv::on_viewport_changed(SimpleEvent& evt)
{ {
wxObject* o = evt.GetEventObject(); wxObject* o = evt.GetEventObject();
if (o == preview->get_canvas()) if (o == preview->get_wxglcanvas())
preview->set_viewport_into_scene(canvas3D); preview->set_viewport_into_scene(canvas3D);
else if (o == canvas3D) else if (o == canvas3D)
preview->set_viewport_from_scene(canvas3D); preview->set_viewport_from_scene(canvas3D);