Fixed generation of texture mipmap using glGenerateMipMap (added also for png)

This commit is contained in:
enricoturri1966 2023-01-13 08:12:20 +01:00
parent c329e1084a
commit 971f2a08e2
3 changed files with 32 additions and 18 deletions

View File

@ -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 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. // Load a PNG with an alpha channel.
wxImage image; wxImage image;
@ -447,7 +448,7 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo
} }
if (compression_enabled) { 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())); 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 { else {
// initializes the texture on GPU // initializes the texture on GPU
@ -459,7 +460,7 @@ bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps, ECo
else 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())); 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 // we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards
int lod_w = m_width; int lod_w = m_width;
int lod_h = m_height; 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)); 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 { else {
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); 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; m_source = filename;
if (compression_enabled && compression_type == MultiThreaded) if (use_compressor && compression_type == MultiThreaded)
// start asynchronous compression // start asynchronous compression
m_compressor.start_compressing(); 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 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, {}); NSVGimage* image = BitmapCache::nsvgParseFromFileWithReplace(filename.c_str(), "px", 96.0f, {});
if (image == nullptr) { 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); m_height += (4 - height_rem);
} }
int n_pixels = m_width * m_height; const int n_pixels = m_width * m_height;
if (n_pixels <= 0) { if (n_pixels <= 0) {
reset(); reset();
@ -582,10 +588,14 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, boo
} }
if (compression_enabled) { if (compression_enabled) {
// initializes the texture on GPU if (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, 0)); // initializes the texture on GPU
// and send the uncompressed data to the 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, 0));
m_compressor.add_level((unsigned int)m_width, (unsigned int)m_height, data); // 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 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())); 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_MAX_LEVEL, level));
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); 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 if (use_mipmaps && !OpenGLManager::use_manually_generated_mipmaps()) {
} else { 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_MIN_FILTER, GL_LINEAR));
glsafe(::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); 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; m_source = filename;
if (compression_enabled) if (use_compressor)
// start asynchronous compression // start asynchronous compression
m_compressor.start_compressing(); m_compressor.start_compressing();

View File

@ -257,7 +257,7 @@ std::vector<std::string> OpenGLManager::GLInfo::get_extensions_list() const
OpenGLManager::GLInfo OpenGLManager::s_gl_info; OpenGLManager::GLInfo OpenGLManager::s_gl_info;
bool OpenGLManager::s_compressed_textures_supported = false; 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::EMultisampleState OpenGLManager::s_multisample = OpenGLManager::EMultisampleState::Unknown;
OpenGLManager::EFramebufferType OpenGLManager::s_framebuffers_type = OpenGLManager::EFramebufferType::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) // containing the string 'Radeon' in the string returned by glGetString(GL_RENDERER)
const auto& gl_info = OpenGLManager::get_gl_info(); 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")) { 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."; BOOST_LOG_TRIVIAL(debug) << "Mipmapping through OpenGL was enabled.";
} }
#endif #endif

View File

@ -110,10 +110,11 @@ private:
static OSInfo s_os_info; static OSInfo s_os_info;
#endif //__APPLE__ #endif //__APPLE__
static bool s_compressed_textures_supported; static bool s_compressed_textures_supported;
static bool s_use_manually_generated_mipmaps;
static EMultisampleState s_multisample; static EMultisampleState s_multisample;
static EFramebufferType s_framebuffers_type; static EFramebufferType s_framebuffers_type;
static bool m_use_manually_generated_mipmaps;
public: public:
OpenGLManager() = default; OpenGLManager() = default;
~OpenGLManager(); ~OpenGLManager();
@ -138,7 +139,7 @@ public:
static EFramebufferType get_framebuffers_type() { return s_framebuffers_type; } static EFramebufferType get_framebuffers_type() { return s_framebuffers_type; }
static wxGLCanvas* create_wxglcanvas(wxWindow& parent); static wxGLCanvas* create_wxglcanvas(wxWindow& parent);
static const GLInfo& get_gl_info() { return s_gl_info; } 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: private:
#if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES #if ENABLE_GL_CORE_PROFILE || ENABLE_OPENGL_ES