diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index b62e470c0e..c6d9b72789 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -2208,6 +2208,10 @@ GLCanvas3D::~GLCanvas3D() glsafe(::glDeleteTextures(1, &m_fxaa_texture_id)); m_fxaa_texture_id = 0; } + if (m_fxaa_framebuffer_id != 0) { + glsafe(::glDeleteFramebuffers(1, &m_fxaa_framebuffer_id)); + m_fxaa_framebuffer_id = 0; + } if (m_ssao_color_texture_id != 0) { glsafe(::glDeleteTextures(1, &m_ssao_color_texture_id)); m_ssao_color_texture_id = 0; @@ -9422,7 +9426,7 @@ void GLCanvas3D::_render_fxaa_pass(unsigned int width, unsigned int height) if (shader == nullptr) return; - GLTexture::copy_from_framebuffer(m_fxaa_texture_id, m_fxaa_texture_size, width, height, GL_LINEAR); + GLTexture::copy_from_framebuffer(m_fxaa_texture_id, m_fxaa_framebuffer_id, m_fxaa_texture_size, width, height, GL_LINEAR); glsafe(::glDisable(GL_DEPTH_TEST)); glsafe(::glDisable(GL_BLEND)); diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index b178d3638f..cd6df36323 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -1022,6 +1022,7 @@ public: #endif // ENABLE_SHOW_CAMERA_TARGET GLModel m_background; unsigned int m_fxaa_texture_id{ 0 }; + unsigned int m_fxaa_framebuffer_id{ 0 }; std::array m_fxaa_texture_size{ 0, 0 }; unsigned int m_ssao_color_texture_id{ 0 }; unsigned int m_ssao_depth_texture_id{ 0 }; diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 90cae850c9..1be0c3b81b 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -753,7 +753,70 @@ void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right, glsafe(::glDisable(GL_BLEND)); } -void GLTexture::copy_from_framebuffer(unsigned int& tex_id, std::array& tex_size, unsigned int width, unsigned int height, int filter) +// How copy_from_framebuffer() gets the window's samples into the texture. Copying from the default framebuffer +// resolves them, but copying from a multisampled framebuffer object fails, and VirtualGL draws the window into one +// that it reports as framebuffer 0. +enum class ECopyMethod : unsigned char { Unknown, Copy, Blit }; + +// Resolves the bound read framebuffer into the texture, through framebuffer_id, created on first use. With check, +// tells whether the blit worked. +static bool blit_into_texture(unsigned int tex_id, unsigned int& framebuffer_id, unsigned int width, unsigned int height, bool check) +{ + GLint draw_framebuffer = 0; + glsafe(::glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer)); + // Unlike a copy, a blit is clipped by the scissor test. + const GLboolean scissor_test = ::glIsEnabled(GL_SCISSOR_TEST); + if (framebuffer_id == 0) + glsafe(::glGenFramebuffers(1, &framebuffer_id)); + glsafe(::glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_id)); + glsafe(::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_id, 0)); + if (scissor_test) + glsafe(::glDisable(GL_SCISSOR_TEST)); + ::glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST); + bool blitted = true; + if (check) // read here, as glsafe() would assert on the error in Debug builds + blitted = ::glGetError() == GL_NO_ERROR; + else + glcheck(); + if (scissor_test) + glsafe(::glEnable(GL_SCISSOR_TEST)); + glsafe(::glBindFramebuffer(GL_DRAW_FRAMEBUFFER, static_cast(draw_framebuffer))); + return blitted; +} + +// Copies the window into the bound texture, tex_id, and tells how to copy it from now on, Unknown to check again +// next time. +static ECopyMethod first_copy(unsigned int tex_id, unsigned int& framebuffer_id, unsigned int width, unsigned int height) +{ + GLint sample_buffers = 0; + glsafe(::glGetIntegerv(GL_SAMPLE_BUFFERS, &sample_buffers)); + if (sample_buffers == 0) { + glsafe(::glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height)); + return ECopyMethod::Copy; + } + while (::glGetError() != GL_NO_ERROR) {} // so the check below sees only this copy + ::glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height); + const GLenum error = ::glGetError(); + if (error == GL_NO_ERROR) { + BOOST_LOG_TRIVIAL(info) << "Copying from the multisampled window framebuffer resolves it"; + return ECopyMethod::Copy; + } + // Any other error says nothing about the samples. + if (error != GL_INVALID_OPERATION) + return ECopyMethod::Unknown; + // The blit uses the core framebuffer functions. + if (OpenGLManager::get_framebuffers_type() == OpenGLManager::EFramebufferType::Arb && + blit_into_texture(tex_id, framebuffer_id, width, height, true)) { + BOOST_LOG_TRIVIAL(info) << "Copying from the multisampled window framebuffer failed; resolving it with a blit instead"; + return ECopyMethod::Blit; + } + // Nothing resolves the samples: keep the copy. + BOOST_LOG_TRIVIAL(warning) << "Neither a copy nor a blit resolves the multisampled window framebuffer"; + return ECopyMethod::Copy; +} + +void GLTexture::copy_from_framebuffer(unsigned int& tex_id, unsigned int& framebuffer_id, std::array& tex_size, + unsigned int width, unsigned int height, int filter) { if (tex_id == 0) { glsafe(::glGenTextures(1, &tex_id)); @@ -767,12 +830,18 @@ void GLTexture::copy_from_framebuffer(unsigned int& tex_id, std::array& tex_size, unsigned int width, unsigned int height, int filter); + // Copies the window framebuffer, bound for reading and drawing, into an RGBA texture, creating it on first use and + // reallocating it when the size changes. Where a copy can't resolve the window's samples, a blit through + // framebuffer_id does, a framebuffer object created on first use; the caller deletes both. + static void copy_from_framebuffer(unsigned int& tex_id, unsigned int& framebuffer_id, std::array& tex_size, + unsigned int width, unsigned int height, int filter); private: bool load_from_png(const std::string& filename, bool use_mipmaps, ECompressionType compression_type, bool apply_anisotropy); diff --git a/src/slic3r/GUI/SceneCache.cpp b/src/slic3r/GUI/SceneCache.cpp index 5cca509a5e..4f040e33d2 100644 --- a/src/slic3r/GUI/SceneCache.cpp +++ b/src/slic3r/GUI/SceneCache.cpp @@ -21,7 +21,7 @@ void SceneCache::capture(Key key) if (wxGetApp().get_shader("flat_texture") == nullptr) return; - GLTexture::copy_from_framebuffer(m_texture_id, m_texture_size, key.size[0], key.size[1], GL_NEAREST); + GLTexture::copy_from_framebuffer(m_texture_id, m_framebuffer_id, m_texture_size, key.size[0], key.size[1], GL_NEAREST); m_key = std::move(key); m_valid = true; @@ -57,6 +57,10 @@ void SceneCache::reset() glsafe(::glDeleteTextures(1, &m_texture_id)); m_texture_id = 0; } + if (m_framebuffer_id != 0) { + glsafe(::glDeleteFramebuffers(1, &m_framebuffer_id)); + m_framebuffer_id = 0; + } m_texture_size = { { 0, 0 } }; } diff --git a/src/slic3r/GUI/SceneCache.hpp b/src/slic3r/GUI/SceneCache.hpp index d645d2d2a6..d15d2412f0 100644 --- a/src/slic3r/GUI/SceneCache.hpp +++ b/src/slic3r/GUI/SceneCache.hpp @@ -36,17 +36,18 @@ public: } }; - // Copies the bound read framebuffer, sized by key.size, and remembers key. + // Copies the window framebuffer, sized by key.size, and remembers key. void capture(Key key); bool matches(const Key& key) const { return m_valid && m_key == key; } // Draws the last capture over the whole viewport, on the given full screen quad. void render(GLModel& quad); void invalidate() { m_valid = false; } - // Frees the texture. + // Frees the texture and the framebuffer object. void reset(); private: unsigned int m_texture_id{ 0 }; + unsigned int m_framebuffer_id{ 0 }; std::array m_texture_size{ { 0, 0 } }; Key m_key; bool m_valid{ false };