From 971f2a08e2758ea1d91e75af9b1443d14b41f895 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Fri, 13 Jan 2023 08:12:20 +0100 Subject: [PATCH] Fixed generation of texture mipmap using glGenerateMipMap (added also for png) --- src/slic3r/GUI/GLTexture.cpp | 41 +++++++++++++++++++++----------- src/slic3r/GUI/OpenGLManager.cpp | 4 ++-- src/slic3r/GUI/OpenGLManager.hpp | 5 ++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 07a2edbe2..ef9a8ef20 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -377,7 +377,8 @@ void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right, bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECompressionType compression_type, bool apply_anisotropy) { - bool compression_enabled = (compression_type != None) && OpenGLManager::are_compressed_textures_supported(); + const bool compression_enabled = (compression_type != None) && OpenGLManager::are_compressed_textures_supported(); + const bool use_compressor = compression_enabled && OpenGLManager::use_manually_generated_mipmaps(); // Load a PNG with an alpha channel. wxImage image; @@ -447,7 +448,7 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo } if (compression_enabled) { - if (compression_type == SingleThreaded) + if (compression_type == SingleThreaded || !use_compressor) glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); else { // initializes the texture on GPU @@ -459,7 +460,7 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo else glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); - if (use_mipmaps) { + if (use_mipmaps && OpenGLManager::use_manually_generated_mipmaps()) { // we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards int lod_w = m_width; int lod_h = m_height; @@ -506,6 +507,10 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); } } + else if (use_mipmaps && !OpenGLManager::use_manually_generated_mipmaps()) { + glsafe(::glGenerateMipmap(GL_TEXTURE_2D)); + glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); + } else { glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); @@ -517,7 +522,7 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo m_source = filename; - if (compression_enabled && compression_type == MultiThreaded) + if (use_compressor && compression_type == MultiThreaded) // start asynchronous compression m_compressor.start_compressing(); @@ -526,7 +531,8 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, bool compress, bool apply_anisotropy, unsigned int max_size_px) { - bool compression_enabled = compress && OpenGLManager::are_compressed_textures_supported(); + const bool compression_enabled = compress && OpenGLManager::are_compressed_textures_supported(); + const bool use_compressor = compression_enabled && OpenGLManager::use_manually_generated_mipmaps(); NSVGimage* image = BitmapCache::nsvgParseFromFileWithReplace(filename.c_str(), "px", 96.0f, {}); if (image == nullptr) { @@ -551,7 +557,7 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, boo m_height += (4 - height_rem); } - int n_pixels = m_width * m_height; + const int n_pixels = m_width * m_height; if (n_pixels <= 0) { reset(); @@ -582,10 +588,14 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, boo } if (compression_enabled) { - // initializes the texture on GPU - glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); - // and send the uncompressed data to the compressor - m_compressor.add_level((unsigned int)m_width, (unsigned int)m_height, data); + if (use_compressor) { + // initializes the texture on GPU + glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); + // and send the uncompressed data to the compressor + m_compressor.add_level((unsigned int)m_width, (unsigned int)m_height, data); + } + else + glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); } else glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); @@ -619,9 +629,12 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, boo glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level)); glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); } - } else if (use_mipmaps && !OpenGLManager::use_manually_generated_mipmaps()) { - glGenerateMipmap(GL_TEXTURE_2D); - } else { + } + else if (use_mipmaps && !OpenGLManager::use_manually_generated_mipmaps()) { + glsafe(::glGenerateMipmap(GL_TEXTURE_2D)); + glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); + } + else { glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); } @@ -632,7 +645,7 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, boo m_source = filename; - if (compression_enabled) + if (use_compressor) // start asynchronous compression m_compressor.start_compressing(); diff --git a/src/slic3r/GUI/OpenGLManager.cpp b/src/slic3r/GUI/OpenGLManager.cpp index 66d72e346..8959553e6 100644 --- a/src/slic3r/GUI/OpenGLManager.cpp +++ b/src/slic3r/GUI/OpenGLManager.cpp @@ -257,7 +257,7 @@ std::vector OpenGLManager::GLInfo::get_extensions_list() const OpenGLManager::GLInfo OpenGLManager::s_gl_info; bool OpenGLManager::s_compressed_textures_supported = false; -bool OpenGLManager::m_use_manually_generated_mipmaps = true; +bool OpenGLManager::s_use_manually_generated_mipmaps = true; OpenGLManager::EMultisampleState OpenGLManager::s_multisample = OpenGLManager::EMultisampleState::Unknown; OpenGLManager::EFramebufferType OpenGLManager::s_framebuffers_type = OpenGLManager::EFramebufferType::Unknown; @@ -422,7 +422,7 @@ bool OpenGLManager::init_gl() // containing the string 'Radeon' in the string returned by glGetString(GL_RENDERER) const auto& gl_info = OpenGLManager::get_gl_info(); if (boost::contains(gl_info.get_vendor(), "ATI Technologies Inc.") && boost::contains(gl_info.get_renderer(), "Radeon")) { - m_use_manually_generated_mipmaps = false; + s_use_manually_generated_mipmaps = false; BOOST_LOG_TRIVIAL(debug) << "Mipmapping through OpenGL was enabled."; } #endif diff --git a/src/slic3r/GUI/OpenGLManager.hpp b/src/slic3r/GUI/OpenGLManager.hpp index ece900962..6b947deb5 100644 --- a/src/slic3r/GUI/OpenGLManager.hpp +++ b/src/slic3r/GUI/OpenGLManager.hpp @@ -110,10 +110,11 @@ private: static OSInfo s_os_info; #endif //__APPLE__ static bool s_compressed_textures_supported; + static bool s_use_manually_generated_mipmaps; + static EMultisampleState s_multisample; static EFramebufferType s_framebuffers_type; - static bool m_use_manually_generated_mipmaps; public: OpenGLManager() = default; ~OpenGLManager(); @@ -138,7 +139,7 @@ public: static EFramebufferType get_framebuffers_type() { return s_framebuffers_type; } static wxGLCanvas* create_wxglcanvas(wxWindow& parent); static const GLInfo& get_gl_info() { return s_gl_info; } - static bool use_manually_generated_mipmaps() { return m_use_manually_generated_mipmaps; } + static bool use_manually_generated_mipmaps() { return s_use_manually_generated_mipmaps; } private: #if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES