Clean up
This commit is contained in:
parent
66f3a2e32b
commit
437f89eea7
@ -41,7 +41,6 @@
|
||||
#include "graphics/stk_tex_manager.hpp"
|
||||
#include "graphics/stk_texture.hpp"
|
||||
#include "graphics/sun.hpp"
|
||||
#include "graphics/texture_manager.hpp"
|
||||
#include "guiengine/engine.hpp"
|
||||
#include "guiengine/message_queue.hpp"
|
||||
#include "guiengine/modaldialog.hpp"
|
||||
@ -162,9 +161,7 @@ IrrDriver::IrrDriver()
|
||||
IrrDriver::~IrrDriver()
|
||||
{
|
||||
assert(m_device != NULL);
|
||||
#ifndef SERVER_ONLY
|
||||
cleanUnicolorTextures();
|
||||
#endif
|
||||
STKTexManager::getInstance()->kill();
|
||||
m_device->drop();
|
||||
m_device = NULL;
|
||||
m_modes.clear();
|
||||
@ -932,8 +929,7 @@ void IrrDriver::applyResolutionSettings()
|
||||
// That's just error prone
|
||||
// (we're sure to update main.cpp at some point and forget this one...)
|
||||
VAOManager::getInstance()->kill();
|
||||
resetTextureTable();
|
||||
cleanUnicolorTextures();
|
||||
STKTexManager::getInstance()->kill();
|
||||
// initDevice will drop the current device.
|
||||
if (CVS->isGLSL())
|
||||
{
|
||||
|
@ -392,14 +392,13 @@ static void setTexture(GLMesh &mesh, unsigned i, bool is_srgb,
|
||||
mesh.textures[i] =
|
||||
STKTexManager::getInstance()->getUnicolorTexture(video::SColor(255, 127, 127, 127));
|
||||
}
|
||||
compressTexture(mesh.textures[i], is_srgb);
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
if (!mesh.TextureHandles[i])
|
||||
{
|
||||
mesh.TextureHandles[i] = glGetTextureSamplerHandleARB(
|
||||
getTextureGLuint(mesh.textures[i]),
|
||||
mesh.textures[i]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
}
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[i]))
|
||||
@ -482,14 +481,13 @@ void initTexturesTransparent(GLMesh &mesh)
|
||||
{
|
||||
mesh.textures[0] = STKTexManager::getInstance()->getUnicolorTexture(video::SColor(255, 255, 255, 255));
|
||||
}
|
||||
compressTexture(mesh.textures[0], true);
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
if (!mesh.TextureHandles[0])
|
||||
{
|
||||
mesh.TextureHandles[0] = glGetTextureSamplerHandleARB(
|
||||
getTextureGLuint(mesh.textures[0]),
|
||||
mesh.textures[0]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
}
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0]))
|
||||
|
@ -355,14 +355,13 @@ void STKMeshSceneNode::render()
|
||||
GLenum itype = mesh.IndexType;
|
||||
size_t count = mesh.IndexCount;
|
||||
|
||||
compressTexture(mesh.textures[0], true);
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
if (!mesh.TextureHandles[0])
|
||||
{
|
||||
mesh.TextureHandles[0] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[0]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader
|
||||
::getInstance()->m_sampler_ids[0]);
|
||||
}
|
||||
@ -375,7 +374,7 @@ void STKMeshSceneNode::render()
|
||||
}
|
||||
else
|
||||
#endif
|
||||
ObjectPass1Shader::getInstance()->setTextureUnits(getTextureGLuint(mesh.textures[0]));
|
||||
ObjectPass1Shader::getInstance()->setTextureUnits(mesh.textures[0]->getOpenGLTextureName());
|
||||
ObjectPass1Shader::getInstance()->setUniforms(AbsoluteTransformation, invmodel);
|
||||
assert(mesh.vao);
|
||||
glBindVertexArray(mesh.vao);
|
||||
@ -422,7 +421,7 @@ void STKMeshSceneNode::render()
|
||||
|
||||
if (!mesh.TextureHandles[0])
|
||||
mesh.TextureHandles[0] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[0]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0]))
|
||||
{
|
||||
@ -432,7 +431,7 @@ void STKMeshSceneNode::render()
|
||||
|
||||
if (!mesh.TextureHandles[1])
|
||||
mesh.TextureHandles[1] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[1]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[1]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[1]))
|
||||
{
|
||||
@ -442,7 +441,7 @@ void STKMeshSceneNode::render()
|
||||
|
||||
if (!mesh.TextureHandles[2])
|
||||
mesh.TextureHandles[2] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[2]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[2]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[2]))
|
||||
{
|
||||
@ -461,9 +460,9 @@ void STKMeshSceneNode::render()
|
||||
irr_driver->getRenderTargetTexture(RTT_DIFFUSE),
|
||||
irr_driver->getRenderTargetTexture(RTT_SPECULAR),
|
||||
irr_driver->getRenderTargetTexture(RTT_HALF1_R),
|
||||
getTextureGLuint(mesh.textures[0]),
|
||||
getTextureGLuint(mesh.textures[1]),
|
||||
getTextureGLuint(mesh.textures[2]));
|
||||
mesh.textures[0]->getOpenGLTextureName(),
|
||||
mesh.textures[1]->getOpenGLTextureName(),
|
||||
mesh.textures[2]->getOpenGLTextureName());
|
||||
ObjectPass2Shader::getInstance()->setUniforms(AbsoluteTransformation,
|
||||
mesh.texture_trans,
|
||||
core::vector2df(0.0f, 0.0f));
|
||||
@ -529,13 +528,12 @@ void STKMeshSceneNode::render()
|
||||
tmpcol.getGreen() / 255.0f,
|
||||
tmpcol.getBlue() / 255.0f);
|
||||
|
||||
compressTexture(mesh.textures[0], true);
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
if (!mesh.TextureHandles[0])
|
||||
mesh.TextureHandles[0] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[0]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0]))
|
||||
{
|
||||
@ -549,7 +547,7 @@ void STKMeshSceneNode::render()
|
||||
#endif
|
||||
{
|
||||
Shaders::TransparentFogShader::getInstance()
|
||||
->setTextureUnits(getTextureGLuint(mesh.textures[0]));
|
||||
->setTextureUnits(mesh.textures[0]->getOpenGLTextureName());
|
||||
}
|
||||
Shaders::TransparentFogShader::getInstance()
|
||||
->setUniforms(AbsoluteTransformation, mesh.texture_trans,
|
||||
@ -572,13 +570,12 @@ void STKMeshSceneNode::render()
|
||||
GLenum itype = mesh.IndexType;
|
||||
size_t count = mesh.IndexCount;
|
||||
|
||||
compressTexture(mesh.textures[0], true);
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
if (!mesh.TextureHandles[0])
|
||||
mesh.TextureHandles[0] =
|
||||
glGetTextureSamplerHandleARB(getTextureGLuint(mesh.textures[0]),
|
||||
glGetTextureSamplerHandleARB(mesh.textures[0]->getOpenGLTextureName(),
|
||||
ObjectPass1Shader::getInstance()->m_sampler_ids[0]);
|
||||
if (!glIsTextureHandleResidentARB(mesh.TextureHandles[0]))
|
||||
{
|
||||
@ -589,7 +586,7 @@ void STKMeshSceneNode::render()
|
||||
}
|
||||
else
|
||||
#endif
|
||||
Shaders::TransparentShader::getInstance()->setTextureUnits(getTextureGLuint(mesh.textures[0]));
|
||||
Shaders::TransparentShader::getInstance()->setTextureUnits(mesh.textures[0]->getOpenGLTextureName());
|
||||
|
||||
Shaders::TransparentShader::getInstance()->setUniforms(AbsoluteTransformation, mesh.texture_trans, 1.0f);
|
||||
assert(mesh.vao);
|
||||
|
@ -65,8 +65,6 @@ public:
|
||||
// ------------------------------------------------------------------------
|
||||
int dumpTextureUsage();
|
||||
// ------------------------------------------------------------------------
|
||||
void clean();
|
||||
// ------------------------------------------------------------------------
|
||||
irr::core::stringw reloadTexture(const irr::core::stringw& name);
|
||||
|
||||
}; // STKTexManager
|
||||
|
@ -1,249 +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 SERVER_ONLY
|
||||
|
||||
#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 "graphics/texture_manager.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
|
||||
#if defined(USE_GLES2)
|
||||
#define _IRR_COMPILE_WITH_OGLES2_
|
||||
#include "../../lib/irrlicht/source/Irrlicht/COGLES2Texture.h"
|
||||
#else
|
||||
#include "../../lib/irrlicht/source/Irrlicht/COpenGLTexture.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
GLuint getTextureGLuint(irr::video::ITexture *tex)
|
||||
{
|
||||
if (tex == NULL)
|
||||
return 0;
|
||||
return tex->getOpenGLTextureName();
|
||||
}
|
||||
|
||||
|
||||
static std::set<irr::video::ITexture *> AlreadyTransformedTexture;
|
||||
static std::map<int, video::ITexture*> unicolor_cache;
|
||||
static std::vector<uint64_t> texture_handles;
|
||||
|
||||
void resetTextureTable()
|
||||
{
|
||||
#if !defined(USE_GLES2)
|
||||
if (CVS->isAZDOEnabled())
|
||||
{
|
||||
// Driver seems to crash if texture handles are not cleared...
|
||||
for (uint64_t& handle : texture_handles)
|
||||
{
|
||||
glMakeTextureHandleNonResidentARB(handle);
|
||||
}
|
||||
ObjectPass1Shader::getInstance()->recreateTrilinearSampler(0);
|
||||
texture_handles.clear();
|
||||
}
|
||||
#endif
|
||||
AlreadyTransformedTexture.clear();
|
||||
}
|
||||
|
||||
void insertTextureHandle(uint64_t handle)
|
||||
{
|
||||
texture_handles.push_back(handle);
|
||||
}
|
||||
|
||||
void cleanUnicolorTextures()
|
||||
{
|
||||
for (std::pair<const int, video::ITexture*>& uc : unicolor_cache)
|
||||
{
|
||||
uc.second->drop();
|
||||
}
|
||||
unicolor_cache.clear();
|
||||
}
|
||||
|
||||
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha)
|
||||
{
|
||||
STKTexture* stk_tex = dynamic_cast<STKTexture*>(tex);
|
||||
if (stk_tex) return;
|
||||
|
||||
if (AlreadyTransformedTexture.find(tex) != AlreadyTransformedTexture.end())
|
||||
return;
|
||||
AlreadyTransformedTexture.insert(tex);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, getTextureGLuint(tex));
|
||||
|
||||
std::string cached_file;
|
||||
if (CVS->isTextureCompressionEnabled())
|
||||
{
|
||||
// Try to retrieve the compressed texture in cache
|
||||
std::string tex_name = irr_driver->getTextureName(tex);
|
||||
if (!tex_name.empty()) {
|
||||
cached_file = file_manager->getTextureCacheLocation(tex_name) + ".gltz";
|
||||
if (!file_manager->fileIsNewer(tex_name, cached_file)) {
|
||||
if (loadCompressedTexture(cached_file))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t w = tex->getSize().Width, h = tex->getSize().Height;
|
||||
unsigned char *data = new unsigned char[w * h * 4];
|
||||
memcpy(data, tex->lock(), w * h * 4);
|
||||
tex->unlock();
|
||||
unsigned internalFormat, Format;
|
||||
Format = tex->hasAlpha() ? GL_BGRA : GL_BGR;
|
||||
#if defined(USE_GLES2)
|
||||
if (!CVS->isEXTTextureFormatBGRA8888Usable())
|
||||
{
|
||||
Format = tex->hasAlpha() ? GL_RGBA : GL_RGB;
|
||||
|
||||
for (unsigned int i = 0; i < w * h; i++)
|
||||
{
|
||||
char tmp_val = data[i*4];
|
||||
data[i*4] = data[i*4 + 2];
|
||||
data[i*4 + 2] = tmp_val;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (premul_alpha)
|
||||
{
|
||||
for (unsigned i = 0; i < w * h; i++)
|
||||
{
|
||||
float alpha = data[4 * i + 3];
|
||||
if (alpha > 0.)
|
||||
alpha = pow(alpha / 255.f, 1.f / 2.2f);
|
||||
data[4 * i] = (unsigned char)(data[4 * i] * alpha);
|
||||
data[4 * i + 1] = (unsigned char)(data[4 * i + 1] * alpha);
|
||||
data[4 * i + 2] = (unsigned char)(data[4 * i + 2] * alpha);
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(USE_GLES2)
|
||||
if (!CVS->isTextureCompressionEnabled())
|
||||
{
|
||||
if (srgb)
|
||||
internalFormat = (tex->hasAlpha()) ? GL_SRGB_ALPHA : GL_SRGB;
|
||||
else
|
||||
internalFormat = (tex->hasAlpha()) ? GL_RGBA : GL_RGB;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (srgb)
|
||||
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
|
||||
else
|
||||
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
}
|
||||
#else
|
||||
internalFormat = (tex->hasAlpha()) ? GL_RGBA : GL_RGB;
|
||||
#endif
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, Format, GL_UNSIGNED_BYTE, (GLvoid *)data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
delete[] data;
|
||||
|
||||
if (CVS->isTextureCompressionEnabled() && !cached_file.empty())
|
||||
{
|
||||
// Save the compressed texture in the cache for later use.
|
||||
saveCompressedTexture(cached_file);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Try to load a compressed texture from the given file name.
|
||||
* Data in the specified file need to have a specific format. See the
|
||||
* saveCompressedTexture() function for a description of the format.
|
||||
* \return true if the loading succeeded, false otherwise.
|
||||
* \see saveCompressedTexture
|
||||
*/
|
||||
bool loadCompressedTexture(const std::string& compressed_tex)
|
||||
{
|
||||
std::ifstream ifs(compressed_tex.c_str(), std::ios::in | std::ios::binary);
|
||||
if (!ifs.is_open())
|
||||
return false;
|
||||
|
||||
int internal_format;
|
||||
int w, h;
|
||||
int size = -1;
|
||||
ifs.read((char*)&internal_format, sizeof(int));
|
||||
ifs.read((char*)&w, sizeof(int));
|
||||
ifs.read((char*)&h, sizeof(int));
|
||||
ifs.read((char*)&size, sizeof(int));
|
||||
|
||||
if (ifs.fail() || size == -1)
|
||||
return false;
|
||||
|
||||
char *data = new char[size];
|
||||
ifs.read(data, size);
|
||||
if (!ifs.fail())
|
||||
{
|
||||
glCompressedTexImage2D(GL_TEXTURE_2D, 0, internal_format,
|
||||
w, h, 0, size, (GLvoid*)data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
delete[] data;
|
||||
ifs.close();
|
||||
return true;
|
||||
}
|
||||
delete[] data;
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Try to save the last texture sent to glTexImage2D in a file of the given
|
||||
* file name. This function should only be used for textures sent to
|
||||
* glTexImage2D with a compressed internal format as argument.<br>
|
||||
* \note The following format is used to save the compressed texture:<br>
|
||||
* <internal-format><width><height><size><data> <br>
|
||||
* The first four elements are integers and the last one is stored
|
||||
* on \c size bytes.
|
||||
* \see loadCompressedTexture
|
||||
*/
|
||||
void saveCompressedTexture(const std::string& compressed_tex)
|
||||
{
|
||||
#if !defined(USE_GLES2)
|
||||
int internal_format, width, height, size, compressionSuccessful;
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint *)&internal_format);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint *)&width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint *)&height);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, (GLint *)&compressionSuccessful);
|
||||
if (!compressionSuccessful)
|
||||
return;
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, (GLint *)&size);
|
||||
|
||||
char *data = new char[size];
|
||||
glGetCompressedTexImage(GL_TEXTURE_2D, 0, (GLvoid*)data);
|
||||
std::ofstream ofs(compressed_tex.c_str(), std::ios::out | std::ios::binary);
|
||||
if (ofs.is_open())
|
||||
{
|
||||
ofs.write((char*)&internal_format, sizeof(int));
|
||||
ofs.write((char*)&width, sizeof(int));
|
||||
ofs.write((char*)&height, sizeof(int));
|
||||
ofs.write((char*)&size, sizeof(int));
|
||||
ofs.write(data, size);
|
||||
ofs.close();
|
||||
}
|
||||
delete[] data;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // !SERVER_ONLY
|
||||
|
@ -19,18 +19,6 @@
|
||||
#ifndef HEADER_TEXTURE_MANAGER_HPP
|
||||
#define HEADER_TEXTURE_MANAGER_HPP
|
||||
|
||||
#include "graphics/gl_headers.hpp"
|
||||
|
||||
#include <ITexture.h>
|
||||
#include <SColor.h>
|
||||
#include <string>
|
||||
|
||||
GLuint getTextureGLuint(irr::video::ITexture *tex);
|
||||
void resetTextureTable();
|
||||
void cleanUnicolorTextures();
|
||||
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha = false);
|
||||
bool loadCompressedTexture(const std::string& compressed_tex);
|
||||
void saveCompressedTexture(const std::string& compressed_tex);
|
||||
void insertTextureHandle(uint64_t handle);
|
||||
static void insertTextureHandle(uint64_t handle){}
|
||||
|
||||
#endif
|
||||
|
@ -40,7 +40,6 @@
|
||||
#include "graphics/particle_kind_manager.hpp"
|
||||
#include "graphics/render_target.hpp"
|
||||
#include "graphics/stk_tex_manager.hpp"
|
||||
#include "graphics/texture_manager.hpp"
|
||||
#include "graphics/vao_manager.hpp"
|
||||
#include "io/file_manager.hpp"
|
||||
#include "io/xml_node.hpp"
|
||||
@ -295,8 +294,6 @@ void Track::cleanup()
|
||||
#ifndef SERVER_ONLY
|
||||
VAOManager::kill();
|
||||
ParticleKindManager::get()->cleanUpTrackSpecificGfx();
|
||||
// Clear reminder of transformed textures
|
||||
resetTextureTable();
|
||||
#endif
|
||||
|
||||
// Clear reminder of the link between textures and file names.
|
||||
|
Loading…
x
Reference in New Issue
Block a user