From 886da08f89f8d9a4a2149c2784305b18a24ac5f3 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 28 May 2019 12:53:16 +0200 Subject: [PATCH] Texture compression set as an option --- src/slic3r/GUI/3DBed.cpp | 4 ++ src/slic3r/GUI/GLCanvas3D.cpp | 36 +++++++++++-- src/slic3r/GUI/GLCanvas3D.hpp | 8 +++ src/slic3r/GUI/GLTexture.cpp | 64 +++++++++++++++-------- src/slic3r/GUI/GLTexture.hpp | 19 +++++++ src/slic3r/GUI/GLToolbar.cpp | 8 +++ src/slic3r/GUI/Gizmos/GLGizmosManager.cpp | 8 +++ src/slic3r/GUI/ImGuiWrapper.cpp | 10 +++- src/slic3r/GUI/ImGuiWrapper.hpp | 4 ++ 9 files changed, 133 insertions(+), 28 deletions(-) diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index 2f20a65d9..4c4e600e8 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -517,7 +517,11 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const if ((m_texture.get_id() == 0) || (m_texture.get_source() != filename)) { +#if ENABLE_COMPRESSED_TEXTURES + if (!m_texture.load_from_svg_file(filename, true, true, max_tex_size)) +#else if (!m_texture.load_from_svg_file(filename, true, max_tex_size)) +#endif // ENABLE_COMPRESSED_TEXTURES { render_custom(); return; diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index f64ed41a4..f7d685502 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -405,7 +405,11 @@ void GLCanvas3D::LayersEditing::_render_tooltip_texture(const GLCanvas3D& canvas if (m_tooltip_texture.get_id() == 0) { std::string filename = resources_dir() + "/icons/variable_layer_height_tooltip.png"; +#if ENABLE_COMPRESSED_TEXTURES + if (!m_tooltip_texture.load_from_file(filename, false, true)) +#else if (!m_tooltip_texture.load_from_file(filename, false)) +#endif // ENABLE_COMPRESSED_TEXTURES return; } @@ -437,7 +441,11 @@ void GLCanvas3D::LayersEditing::_render_reset_texture(const Rect& reset_rect) co if (m_reset_texture.get_id() == 0) { std::string filename = resources_dir() + "/icons/variable_layer_height_reset.png"; +#if ENABLE_COMPRESSED_TEXTURES + if (!m_reset_texture.load_from_file(filename, false, true)) +#else if (!m_reset_texture.load_from_file(filename, false)) +#endif // ENABLE_COMPRESSED_TEXTURES return; } @@ -729,7 +737,11 @@ void GLCanvas3D::WarningTexture::activate(WarningTexture::Warning warning, bool } } +#if ENABLE_COMPRESSED_TEXTURES + generate(text, canvas, true, red_colored); // GUI::GLTexture::reset() is called at the beginning of generate(...) +#else _generate(text, canvas, red_colored); // GUI::GLTexture::reset() is called at the beginning of generate(...) +#endif // ENABLE_COMPRESSED_TEXTURES // save information for rescaling m_msg_text = text; @@ -790,7 +802,11 @@ static void msw_disable_cleartype(wxFont &font) } #endif /* __WXMSW__ */ +#if ENABLE_COMPRESSED_TEXTURES +bool GLCanvas3D::WarningTexture::generate(const std::string& msg_utf8, const GLCanvas3D& canvas, bool compress, bool red_colored/* = false*/) +#else bool GLCanvas3D::WarningTexture::_generate(const std::string& msg_utf8, const GLCanvas3D& canvas, const bool red_colored/* = false*/) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); @@ -865,7 +881,7 @@ bool GLCanvas3D::WarningTexture::_generate(const std::string& msg_utf8, const GL glsafe(::glGenTextures(1, &m_id)); glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)m_id)); #if ENABLE_COMPRESSED_TEXTURES - if (GLEW_EXT_texture_compression_s3tc) + if (compress && GLEW_EXT_texture_compression_s3tc) 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())); @@ -922,7 +938,11 @@ void GLCanvas3D::WarningTexture::msw_rescale(const GLCanvas3D& canvas) if (m_msg_text.empty()) return; +#if ENABLE_COMPRESSED_TEXTURES + generate(m_msg_text, canvas, true, m_is_colored_red); +#else _generate(m_msg_text, canvas, m_is_colored_red); +#endif // ENABLE_COMPRESSED_TEXTURES } const unsigned char GLCanvas3D::LegendTexture::Squares_Border_Color[3] = { 64, 64, 64 }; @@ -965,7 +985,11 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_values(const GCodePrevie } } +#if ENABLE_COMPRESSED_TEXTURES +bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas, bool compress) +#else bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); @@ -1155,7 +1179,7 @@ bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, c glsafe(::glGenTextures(1, &m_id)); glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)m_id)); #if ENABLE_COMPRESSED_TEXTURES - if (GLEW_EXT_texture_compression_s3tc) + if (compress && GLEW_EXT_texture_compression_s3tc) 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())); @@ -1714,9 +1738,9 @@ void GLCanvas3D::render() imgui.text(" ms"); #if ENABLE_COMPRESSED_TEXTURES ImGui::Separator(); - imgui.text("Textures: "); + imgui.text("Compressed textures: "); ImGui::SameLine(); - imgui.text(GLCanvas3DManager::are_compressed_textures_supported() ? "compressed" : "uncompressed"); + imgui.text(GLCanvas3DManager::are_compressed_textures_supported() ? "supported" : "not supported"); #endif // ENABLE_COMPRESSED_TEXTURES #if ENABLE_TEXTURES_MAXSIZE_DEPENDENT_ON_OPENGL_VERSION imgui.text("Max texture size: "); @@ -5698,7 +5722,11 @@ std::vector GLCanvas3D::_parse_colors(const std::vector& col void GLCanvas3D::_generate_legend_texture(const GCodePreviewData& preview_data, const std::vector& tool_colors) { +#if ENABLE_COMPRESSED_TEXTURES + m_legend_texture.generate(preview_data, tool_colors, *this, true); +#else m_legend_texture.generate(preview_data, tool_colors, *this); +#endif // ENABLE_COMPRESSED_TEXTURES } void GLCanvas3D::_set_warning_texture(WarningTexture::Warning warning, bool state) diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 51a36cf69..6d77a507f 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -374,7 +374,11 @@ class GLCanvas3D std::vector m_warnings; // Generates the texture with given text. +#if ENABLE_COMPRESSED_TEXTURES + bool generate(const std::string& msg, const GLCanvas3D& canvas, bool compress, bool red_colored = false); +#else bool _generate(const std::string& msg, const GLCanvas3D& canvas, const bool red_colored = false); +#endif // ENABLE_COMPRESSED_TEXTURES }; class LegendTexture : public GUI::GLTexture @@ -397,7 +401,11 @@ class GLCanvas3D void fill_color_print_legend_values(const GCodePreviewData& preview_data, const GLCanvas3D& canvas, std::vector>& cp_legend_values); +#if ENABLE_COMPRESSED_TEXTURES + bool generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas, bool compress); +#else bool generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas); +#endif // ENABLE_COMPRESSED_TEXTURES void render(const GLCanvas3D& canvas) const; }; diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 2216a14f2..bdd7ee624 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -18,10 +18,6 @@ #include "libslic3r/Utils.hpp" -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -#include -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - namespace Slic3r { namespace GUI { @@ -40,7 +36,11 @@ GLTexture::~GLTexture() reset(); } +#if ENABLE_COMPRESSED_TEXTURES +bool GLTexture::load_from_file(const std::string& filename, bool use_mipmaps, bool compress) +#else bool GLTexture::load_from_file(const std::string& filename, bool use_mipmaps) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); @@ -48,12 +48,20 @@ bool GLTexture::load_from_file(const std::string& filename, bool use_mipmaps) return false; if (boost::algorithm::iends_with(filename, ".png")) +#if ENABLE_COMPRESSED_TEXTURES + return load_from_png(filename, use_mipmaps, compress); +#else return load_from_png(filename, use_mipmaps); +#endif // ENABLE_COMPRESSED_TEXTURES else return false; } +#if ENABLE_COMPRESSED_TEXTURES +bool GLTexture::load_from_svg_file(const std::string& filename, bool use_mipmaps, bool compress, unsigned int max_size_px) +#else bool GLTexture::load_from_svg_file(const std::string& filename, bool use_mipmaps, unsigned int max_size_px) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); @@ -61,12 +69,20 @@ bool GLTexture::load_from_svg_file(const std::string& filename, bool use_mipmaps return false; if (boost::algorithm::iends_with(filename, ".svg")) +#if ENABLE_COMPRESSED_TEXTURES + return load_from_svg(filename, use_mipmaps, compress, max_size_px); +#else return load_from_svg(filename, use_mipmaps, max_size_px); +#endif // ENABLE_COMPRESSED_TEXTURES else return false; } +#if ENABLE_COMPRESSED_TEXTURES +bool GLTexture::load_from_svg_files_as_sprites_array(const std::vector& filenames, const std::vector>& states, unsigned int sprite_size_px, bool compress) +#else bool GLTexture::load_from_svg_files_as_sprites_array(const std::vector& filenames, const std::vector>& states, unsigned int sprite_size_px) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); @@ -183,7 +199,7 @@ bool GLTexture::load_from_svg_files_as_sprites_array(const std::vector(end_time - start_time).count() << std::endl; - start_time = end_time; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ if (use_mipmaps) { // we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards @@ -455,7 +478,7 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, uns nsvgRasterize(rast, image, 0, 0, scale, data.data(), lod_w, lod_h, lod_w * 4); #if ENABLE_COMPRESSED_TEXTURES - if (GLEW_EXT_texture_compression_s3tc) + if (compress && GLEW_EXT_texture_compression_s3tc) glsafe(::glTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)lod_w, (GLsizei)lod_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); else glsafe(::glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, (GLsizei)lod_w, (GLsizei)lod_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data())); @@ -481,11 +504,6 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, uns nsvgDeleteRasterizer(rast); nsvgDelete(image); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - end_time = std::chrono::high_resolution_clock::now(); - std::cout << "mipmaps to GPU in: " << std::chrono::duration_cast(end_time - start_time).count() << std::endl; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - return true; } diff --git a/src/slic3r/GUI/GLTexture.hpp b/src/slic3r/GUI/GLTexture.hpp index e00b3a3be..b474f7b05 100644 --- a/src/slic3r/GUI/GLTexture.hpp +++ b/src/slic3r/GUI/GLTexture.hpp @@ -38,8 +38,13 @@ namespace GUI { GLTexture(); virtual ~GLTexture(); +#if ENABLE_COMPRESSED_TEXTURES + bool load_from_file(const std::string& filename, bool use_mipmaps, bool compress); + bool load_from_svg_file(const std::string& filename, bool use_mipmaps, bool compress, unsigned int max_size_px); +#else bool load_from_file(const std::string& filename, bool use_mipmaps); bool load_from_svg_file(const std::string& filename, bool use_mipmaps, unsigned int max_size_px); +#endif // ENABLE_COMPRESSED_TEXTURES // meanings of states: (std::pair) // first field (int): // 0 -> no changes @@ -48,7 +53,11 @@ namespace GUI { // second field (bool): // false -> no changes // true -> add background color +#if ENABLE_COMPRESSED_TEXTURES + bool load_from_svg_files_as_sprites_array(const std::vector& filenames, const std::vector>& states, unsigned int sprite_size_px, bool compress); +#else bool load_from_svg_files_as_sprites_array(const std::vector& filenames, const std::vector>& states, unsigned int sprite_size_px); +#endif // ENABLE_COMPRESSED_TEXTURES void reset(); unsigned int get_id() const { return m_id; } @@ -61,10 +70,20 @@ namespace GUI { static void render_sub_texture(unsigned int tex_id, float left, float right, float bottom, float top, const Quad_UVs& uvs); protected: +#if ENABLE_COMPRESSED_TEXTURES + unsigned int generate_mipmaps(wxImage& image, bool compress); +#else unsigned int generate_mipmaps(wxImage& image); +#endif // ENABLE_COMPRESSED_TEXTURES + private: +#if ENABLE_COMPRESSED_TEXTURES + bool load_from_png(const std::string& filename, bool use_mipmaps, bool compress); + bool load_from_svg(const std::string& filename, bool use_mipmaps, bool compress, unsigned int max_size_px); +#else bool load_from_png(const std::string& filename, bool use_mipmaps); bool load_from_svg(const std::string& filename, bool use_mipmaps, unsigned int max_size_px); +#endif // ENABLE_COMPRESSED_TEXTURES }; } // namespace GUI diff --git a/src/slic3r/GUI/GLToolbar.cpp b/src/slic3r/GUI/GLToolbar.cpp index 00cbdfec7..813a319b8 100644 --- a/src/slic3r/GUI/GLToolbar.cpp +++ b/src/slic3r/GUI/GLToolbar.cpp @@ -194,7 +194,11 @@ bool GLToolbar::init(const ItemsIconsTexture::Metadata& icons_texture, const Bac #endif // ENABLE_SVG_ICONS if (!background_texture.filename.empty()) +#if ENABLE_COMPRESSED_TEXTURES + res = m_background_texture.texture.load_from_file(path + background_texture.filename, false, true); +#else res = m_background_texture.texture.load_from_file(path + background_texture.filename, false); +#endif // ENABLE_COMPRESSED_TEXTURES if (res) m_background_texture.metadata = background_texture; @@ -1338,7 +1342,11 @@ bool GLToolbar::generate_icons_texture() const states.push_back(std::make_pair(1, true)); } +#if ENABLE_COMPRESSED_TEXTURES + bool res = m_icons_texture.load_from_svg_files_as_sprites_array(filenames, states, (unsigned int)(m_layout.icons_size * m_layout.scale), true); +#else bool res = m_icons_texture.load_from_svg_files_as_sprites_array(filenames, states, (unsigned int)(m_layout.icons_size * m_layout.scale)); +#endif // ENABLE_COMPRESSED_TEXTURES if (res) m_icons_texture_dirty = false; diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp index 1006d2bd1..c254f5796 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp @@ -65,7 +65,11 @@ bool GLGizmosManager::init(GLCanvas3D& parent) if (!m_background_texture.metadata.filename.empty()) { +#if ENABLE_COMPRESSED_TEXTURES + if (!m_background_texture.texture.load_from_file(resources_dir() + "/icons/" + m_background_texture.metadata.filename, false, true)) +#else if (!m_background_texture.texture.load_from_file(resources_dir() + "/icons/" + m_background_texture.metadata.filename, false)) +#endif // ENABLE_COMPRESSED_TEXTURES { reset(); return false; @@ -1160,7 +1164,11 @@ bool GLGizmosManager::generate_icons_texture() const states.push_back(std::make_pair(0, false)); states.push_back(std::make_pair(0, true)); +#if ENABLE_COMPRESSED_TEXTURES + bool res = m_icons_texture.load_from_svg_files_as_sprites_array(filenames, states, (unsigned int)(m_overlay_icons_size * m_overlay_scale), true); +#else bool res = m_icons_texture.load_from_svg_files_as_sprites_array(filenames, states, (unsigned int)(m_overlay_icons_size * m_overlay_scale)); +#endif // ENABLE_COMPRESSED_TEXTURES if (res) m_icons_texture_dirty = false; diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 9202c0ac3..7267da295 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -206,7 +206,11 @@ void ImGuiWrapper::new_frame() } if (m_font_texture == 0) { +#if ENABLE_COMPRESSED_TEXTURES + init_font(true); +#else init_font(); +#endif // ENABLE_COMPRESSED_TEXTURES } ImGui::NewFrame(); @@ -383,7 +387,11 @@ bool ImGuiWrapper::want_any_input() const return io.WantCaptureMouse || io.WantCaptureKeyboard || io.WantTextInput; } +#if ENABLE_COMPRESSED_TEXTURES +void ImGuiWrapper::init_font(bool compress) +#else void ImGuiWrapper::init_font() +#endif // ENABLE_COMPRESSED_TEXTURES { destroy_font(); @@ -413,7 +421,7 @@ void ImGuiWrapper::init_font() glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); glsafe(::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); #if ENABLE_COMPRESSED_TEXTURES - if (GLEW_EXT_texture_compression_s3tc) + if (compress && GLEW_EXT_texture_compression_s3tc) glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); else glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index 37ef90ff3..a1e4048ac 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -77,7 +77,11 @@ public: bool want_any_input() const; private: +#if ENABLE_COMPRESSED_TEXTURES + void init_font(bool compress); +#else void init_font(); +#endif // ENABLE_COMPRESSED_TEXTURES void init_input(); void init_style(); void render_draw_data(ImDrawData *draw_data);