diff --git a/lib/irrlicht/include/ITexture.h b/lib/irrlicht/include/ITexture.h index b07eb3cc8..0586ec673 100644 --- a/lib/irrlicht/include/ITexture.h +++ b/lib/irrlicht/include/ITexture.h @@ -195,6 +195,9 @@ public: //! return open gl texture name virtual u32 getOpenGLTextureName() const = 0; + virtual u64 getHandle() = 0; + + virtual void unloadHandle() {} protected: //! Helper function, helps to get the desired texture creation format from the flags. diff --git a/lib/irrlicht/source/Irrlicht/CNullDriver.h b/lib/irrlicht/source/Irrlicht/CNullDriver.h index cd3b18049..7a10cccad 100644 --- a/lib/irrlicht/source/Irrlicht/CNullDriver.h +++ b/lib/irrlicht/source/Irrlicht/CNullDriver.h @@ -745,6 +745,7 @@ namespace video virtual ECOLOR_FORMAT getColorFormat() const { return video::ECF_A1R5G5B5; }; virtual u32 getPitch() const { return 0; } virtual u32 getOpenGLTextureName() const { return 0; } + virtual u64 getHandle() { return 0; } virtual void regenerateMipMapLevels(void* mipmapData=0) {}; core::dimension2d size; }; diff --git a/lib/irrlicht/source/Irrlicht/COGLES2Texture.h b/lib/irrlicht/source/Irrlicht/COGLES2Texture.h index 3f6bb74e4..c010a4c4e 100644 --- a/lib/irrlicht/source/Irrlicht/COGLES2Texture.h +++ b/lib/irrlicht/source/Irrlicht/COGLES2Texture.h @@ -83,6 +83,8 @@ public: //! return open gl texture name virtual u32 getOpenGLTextureName() const; + virtual u64 getHandle() { return 0; } + //! return whether this texture has mipmaps virtual bool hasMipMaps() const; diff --git a/lib/irrlicht/source/Irrlicht/COpenGLTexture.h b/lib/irrlicht/source/Irrlicht/COpenGLTexture.h index 823dc2c4b..64f6464da 100644 --- a/lib/irrlicht/source/Irrlicht/COpenGLTexture.h +++ b/lib/irrlicht/source/Irrlicht/COpenGLTexture.h @@ -80,6 +80,8 @@ public: //! return open gl texture name virtual u32 getOpenGLTextureName() const; + virtual u64 getHandle() { return 0; } + //! return whether this texture has mipmaps virtual bool hasMipMaps() const; diff --git a/src/graphics/stk_mesh.cpp b/src/graphics/stk_mesh.cpp index af4a9aff3..397dd8b77 100644 --- a/src/graphics/stk_mesh.cpp +++ b/src/graphics/stk_mesh.cpp @@ -23,10 +23,8 @@ #include "graphics/central_settings.hpp" #include "graphics/irr_driver.hpp" #include "graphics/material_manager.hpp" -#include "graphics/materials.hpp" #include "graphics/shaders.hpp" #include "graphics/stk_tex_manager.hpp" -#include "graphics/texture_manager.hpp" #include #include @@ -397,14 +395,7 @@ static void setTexture(GLMesh &mesh, unsigned i, bool is_srgb, { if (!mesh.TextureHandles[i]) { - mesh.TextureHandles[i] = glGetTextureSamplerHandleARB( - mesh.textures[i]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - } - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[i])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[i]); - insertTextureHandle(mesh.TextureHandles[i]); + mesh.TextureHandles[i] = mesh.textures[i]->getHandle(); } } #endif @@ -486,14 +477,7 @@ void initTexturesTransparent(GLMesh &mesh) { if (!mesh.TextureHandles[0]) { - mesh.TextureHandles[0] = glGetTextureSamplerHandleARB( - mesh.textures[0]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - } - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[0]); - insertTextureHandle(mesh.TextureHandles[0]); + mesh.TextureHandles[0] = mesh.textures[0]->getHandle(); } } #endif diff --git a/src/graphics/stk_mesh_scene_node.cpp b/src/graphics/stk_mesh_scene_node.cpp index b01579a79..f06fa3d00 100644 --- a/src/graphics/stk_mesh_scene_node.cpp +++ b/src/graphics/stk_mesh_scene_node.cpp @@ -26,7 +26,6 @@ #include "graphics/render_info.hpp" #include "graphics/rtts.hpp" #include "graphics/shaders.hpp" -#include "graphics/texture_manager.hpp" #include "graphics/vao_manager.hpp" #include "tracks/track.hpp" #include "modes/world.hpp" @@ -360,15 +359,7 @@ void STKMeshSceneNode::render() { if (!mesh.TextureHandles[0]) { - mesh.TextureHandles[0] = - glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(), - ObjectPass1Shader - ::getInstance()->m_sampler_ids[0]); - } - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[0]); - insertTextureHandle(mesh.TextureHandles[0]); + mesh.TextureHandles[0] = mesh.textures[0]->getHandle(); } ObjectPass1Shader::getInstance()->setTextureHandles(mesh.TextureHandles[0]); } @@ -420,34 +411,13 @@ void STKMeshSceneNode::render() ObjectPass2Shader::getInstance()->m_sampler_ids[2]); if (!mesh.TextureHandles[0]) - mesh.TextureHandles[0] = - glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[0]); - insertTextureHandle(mesh.TextureHandles[0]); - } + mesh.TextureHandles[0] = mesh.textures[0]->getHandle(); if (!mesh.TextureHandles[1]) - mesh.TextureHandles[1] = - glGetTextureSamplerHandleARB(mesh.textures[1]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[1])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[1]); - insertTextureHandle(mesh.TextureHandles[1]); - } + mesh.TextureHandles[1] = mesh.textures[1]->getHandle(); if (!mesh.TextureHandles[2]) - mesh.TextureHandles[2] = - glGetTextureSamplerHandleARB(mesh.textures[2]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[2])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[2]); - insertTextureHandle(mesh.TextureHandles[2]); - } + mesh.TextureHandles[2] = mesh.textures[2]->getHandle(); ObjectPass2Shader::getInstance() ->setTextureHandles(DiffuseHandle, SpecularHandle, SSAOHandle, @@ -532,14 +502,7 @@ void STKMeshSceneNode::render() if (CVS->isAZDOEnabled()) { if (!mesh.TextureHandles[0]) - mesh.TextureHandles[0] = - glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[0]); - insertTextureHandle(mesh.TextureHandles[0]); - } + mesh.TextureHandles[0] = mesh.textures[0]->getHandle(); Shaders::TransparentFogShader::getInstance() ->setTextureHandles(mesh.TextureHandles[0]); } @@ -574,14 +537,7 @@ void STKMeshSceneNode::render() if (CVS->isAZDOEnabled()) { if (!mesh.TextureHandles[0]) - mesh.TextureHandles[0] = - glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(), - ObjectPass1Shader::getInstance()->m_sampler_ids[0]); - if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0])) - { - glMakeTextureHandleResidentARB(mesh.TextureHandles[0]); - insertTextureHandle(mesh.TextureHandles[0]); - } + mesh.TextureHandles[0] = mesh.textures[0]->getHandle(); Shaders::TransparentShader::getInstance()->setTextureHandles(mesh.TextureHandles[0]); } else diff --git a/src/graphics/stk_tex_manager.cpp b/src/graphics/stk_tex_manager.cpp index d7cfaa899..14b5cee1c 100644 --- a/src/graphics/stk_tex_manager.cpp +++ b/src/graphics/stk_tex_manager.cpp @@ -17,7 +17,7 @@ #include "graphics/stk_tex_manager.hpp" #include "graphics/central_settings.hpp" -#include "graphics/irr_driver.hpp" +#include "graphics/materials.hpp" #include "graphics/stk_texture.hpp" #include "io/file_manager.hpp" #include "utils/string_utils.hpp" @@ -222,3 +222,19 @@ core::stringw STKTexManager::reloadTexture(const irr::core::stringw& name) #endif // !SERVER_ONLY return result + "reloaded."; } // reloadTexture + +// ---------------------------------------------------------------------------- +void STKTexManager::reset() +{ +#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) + if (!CVS->isAZDOEnabled()) return; + for (auto p : m_all_textures) + { + if (p.second == NULL) + continue; + p.second->unloadHandle(); + } + // Driver seems to crash if texture handles are not cleared... + ObjectPass1Shader::getInstance()->recreateTrilinearSampler(0); +#endif +} // reset diff --git a/src/graphics/stk_tex_manager.hpp b/src/graphics/stk_tex_manager.hpp index 6d1c01f25..97708ad37 100644 --- a/src/graphics/stk_tex_manager.hpp +++ b/src/graphics/stk_tex_manager.hpp @@ -67,6 +67,8 @@ public: int dumpTextureUsage(); // ------------------------------------------------------------------------ irr::core::stringw reloadTexture(const irr::core::stringw& name); + // ------------------------------------------------------------------------ + void reset(); }; // STKTexManager diff --git a/src/graphics/stk_texture.cpp b/src/graphics/stk_texture.cpp index be955633b..2b00388c6 100644 --- a/src/graphics/stk_texture.cpp +++ b/src/graphics/stk_texture.cpp @@ -15,12 +15,13 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include "config/user_config.hpp" #include "graphics/stk_texture.hpp" +#include "config/user_config.hpp" #include "graphics/central_settings.hpp" #include "graphics/irr_driver.hpp" #include "graphics/material.hpp" #include "graphics/material_manager.hpp" +#include "graphics/materials.hpp" #include "modes/profile_world.hpp" #include "utils/log.hpp" #include "utils/string_utils.hpp" @@ -73,6 +74,7 @@ STKTexture::STKTexture(uint8_t* data, const std::string& name, size_t size, STKTexture::~STKTexture() { #ifndef SERVER_ONLY + unloadHandle(); if (m_texture_name != 0) { glDeleteTextures(1, &m_texture_name); @@ -433,9 +435,9 @@ bool STKTexture::loadCompressedTexture(const std::string& file_name) * file name. This function should only be used for textures sent to * glTexImage2D with a compressed internal format as argument.
* \note The following format is used to save the compressed texture:
- *
- * The first six elements are integers and the last one is stored - * on \c size bytes. + *
+ * The first element is the version of cache, next six elements are + * integers and the last one is stored on \c size bytes. * \see loadCompressedTexture */ void STKTexture::saveCompressedTexture(const std::string& compressed_tex) @@ -502,3 +504,34 @@ void* STKTexture::lock(video::E_TEXTURE_LOCK_MODE mode, u32 mipmap_level) #endif // !SERVER_ONLY return NULL; } // lock + +//----------------------------------------------------------------------------- +u64 STKTexture::getHandle() +{ +#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) + assert(CVS->isAZDOEnabled()); + if (m_texture_handle != 0) return m_texture_handle; + + m_texture_handle = + glGetTextureSamplerHandleARB( m_texture_name, + ObjectPass1Shader::getInstance()->m_sampler_ids[0]); + + if (!glIsTextureHandleResidentARB(m_texture_handle)) + glMakeTextureHandleResidentARB(m_texture_handle); + + return m_texture_handle; +#endif +} // getHandle + +//----------------------------------------------------------------------------- +void STKTexture::unloadHandle() +{ +#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) + if (CVS->isAZDOEnabled()) + { + if (m_texture_handle == 0) return; + glMakeTextureHandleNonResidentARB(m_texture_handle); + m_texture_handle = 0; + } +#endif +} // unloadHandle diff --git a/src/graphics/stk_texture.hpp b/src/graphics/stk_texture.hpp index 90a9b1d68..9c78eb11a 100644 --- a/src/graphics/stk_texture.hpp +++ b/src/graphics/stk_texture.hpp @@ -102,7 +102,9 @@ public: // ------------------------------------------------------------------------ virtual u32 getOpenGLTextureName() const { return m_texture_name; } // ------------------------------------------------------------------------ - uint64_t getTextureHandle() const { return m_texture_handle; } + virtual u64 getHandle(); + // ------------------------------------------------------------------------ + virtual void unloadHandle(); // ------------------------------------------------------------------------ bool isSrgb() const { return m_srgb; } // ------------------------------------------------------------------------ diff --git a/src/graphics/texture_manager.hpp b/src/graphics/texture_manager.hpp deleted file mode 100644 index 8b7ad8128..000000000 --- a/src/graphics/texture_manager.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2014-2015 SuperTuxKart-Team -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 3 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - - -#ifndef HEADER_TEXTURE_MANAGER_HPP -#define HEADER_TEXTURE_MANAGER_HPP - -static void insertTextureHandle(uint64_t handle){} - -#endif diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 74788546b..6fa2f9632 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -406,6 +406,7 @@ void Track::cleanup() irr_driver->clearLights(); irr_driver->clearForcedBloom(); irr_driver->clearBackgroundNodes(); + STKTexManager::getInstance()->reset(); if(UserConfigParams::logMemory()) {