From 6a8c7a87059c279bf7aba0a5092b5de41ff9b815 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 3 Jun 2019 13:53:30 +0200 Subject: [PATCH] Fixed race condition while compressing texture data and sending them to the GPU --- src/slic3r/GUI/GLTexture.cpp | 8 ++++++-- src/slic3r/GUI/GLTexture.hpp | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 469e192b5..78de38d54 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -55,7 +55,7 @@ bool GLTexture::Compressor::unsent_compressed_data_available() const { for (const Level& level : m_levels) { - if (!level.sent_to_gpu && (level.compressed_data.size() > 0)) + if (!level.sent_to_gpu && level.compressed) return true; } @@ -64,12 +64,14 @@ bool GLTexture::Compressor::unsent_compressed_data_available() const void GLTexture::Compressor::send_compressed_data_to_gpu() { + // this method should be called inside the main thread of Slicer or a new OpenGL context (sharing resources) would be needed + glsafe(::glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); glsafe(::glBindTexture(GL_TEXTURE_2D, m_texture.m_id)); for (int i = 0; i < (int)m_levels.size(); ++i) { Level& level = m_levels[i]; - if (!level.sent_to_gpu && (level.compressed_data.size() > 0)) + if (!level.sent_to_gpu && level.compressed) { glsafe(::glCompressedTexSubImage2D(GL_TEXTURE_2D, (GLint)i, 0, 0, (GLsizei)level.w, (GLsizei)level.h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, (GLsizei)level.compressed_data.size(), (const GLvoid*)level.compressed_data.data())); glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i)); @@ -77,6 +79,7 @@ void GLTexture::Compressor::send_compressed_data_to_gpu() level.sent_to_gpu = true; // we are done with the compressed data, we can discard it level.compressed_data.clear(); + level.compressed = false; } } glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); @@ -100,6 +103,7 @@ void GLTexture::Compressor::compress() // we are done with the source data, we can discard it level.src_data.clear(); + level.compressed = true; } m_is_compressing = false; diff --git a/src/slic3r/GUI/GLTexture.hpp b/src/slic3r/GUI/GLTexture.hpp index ed2070a44..d1ff366c3 100644 --- a/src/slic3r/GUI/GLTexture.hpp +++ b/src/slic3r/GUI/GLTexture.hpp @@ -20,9 +20,10 @@ namespace GUI { unsigned int h; std::vector src_data; std::vector compressed_data; + bool compressed; bool sent_to_gpu; - Level(unsigned int w, unsigned int h, const std::vector& data) : w(w), h(h), src_data(data), sent_to_gpu(false) {} + Level(unsigned int w, unsigned int h, const std::vector& data) : w(w), h(h), src_data(data), compressed(false), sent_to_gpu(false) {} }; GLTexture& m_texture;