Remove dependency on STKTexture (for HQ mipmap generator later)

This commit is contained in:
Benau 2017-03-11 15:33:36 +08:00
parent ead543fae4
commit 40a25e1d9e
7 changed files with 46 additions and 27 deletions

View File

@ -198,6 +198,14 @@ public:
virtual u64 getHandle() = 0;
virtual void unloadHandle() {}
virtual u32 getTextureSize() const { return 0; }
virtual void threadedReload(void* ptr, void* param) const {}
virtual void threadedSubImage(void* ptr) const {}
virtual void cleanThreadedLoader() {}
protected:
//! Helper function, helps to get the desired texture creation format from the flags.

View File

@ -425,7 +425,7 @@ void STKTexManager::checkThreadedLoadTextures(bool util_queue_empty)
// ----------------------------------------------------------------------------
bool STKTexManager::SmallestTexture::operator()
(const STKTexture *a, const STKTexture *b) const
(const video::ITexture* a, const video::ITexture* b) const
{
return a->getTextureSize() > b->getTextureSize();
} // SmallestTexture::operator

View File

@ -55,10 +55,12 @@ private:
class SmallestTexture
{
public:
bool operator() (const STKTexture *a, const STKTexture *b) const;
bool operator()(const irr::video::ITexture* a,
const irr::video::ITexture* b) const;
};
std::priority_queue<STKTexture*, std::vector<STKTexture*>,
SmallestTexture> m_threaded_load_textures;
std::priority_queue<irr::video::ITexture*,
std::vector<irr::video::ITexture*>, SmallestTexture>
m_threaded_load_textures;
pthread_mutex_t m_threaded_load_textures_mutex;
@ -151,10 +153,10 @@ public:
// ------------------------------------------------------------------------
void checkThreadedLoadTextures(bool util_queue_empty);
// ------------------------------------------------------------------------
STKTexture* getThreadedLoadTexture()
irr::video::ITexture* getThreadedLoadTexture()
{ return m_threaded_load_textures.top(); }
// ------------------------------------------------------------------------
void addThreadedLoadTexture(STKTexture* t)
void addThreadedLoadTexture(irr::video::ITexture* t)
{
pthread_mutex_lock(&m_threaded_load_textures_mutex);
m_threaded_load_textures.push(t);

View File

@ -575,7 +575,7 @@ bool STKTexture::useThreadedLoading() const
} // useThreadedLoading
//-----------------------------------------------------------------------------
void STKTexture::threadedReload(void* ptr) const
void STKTexture::threadedReload(void* ptr, void* param) const
{
video::IImage* orig_img =
m_img_loader->loadImage(m_file, true/*skip_checking*/);
@ -600,6 +600,16 @@ void STKTexture::threadedReload(void* ptr) const
delete[] data;
} // threadedReload
//-----------------------------------------------------------------------------
void STKTexture::threadedSubImage(void* ptr) const
{
glBindTexture(GL_TEXTURE_2D, m_texture_name);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.Width, m_size.Height,
m_single_channel ? GL_RED : GL_BGRA, GL_UNSIGNED_BYTE, ptr);
if (hasMipMaps())
glGenerateMipmap(GL_TEXTURE_2D);
} // threadedSubImage
//-----------------------------------------------------------------------------
void STKTexture::cleanThreadedLoader()
{

View File

@ -135,11 +135,9 @@ public:
// ------------------------------------------------------------------------
bool isMeshTexture() const { return m_mesh_texture; }
// ------------------------------------------------------------------------
bool isSingleChannel() const { return m_single_channel; }
// ------------------------------------------------------------------------
void setMeshTexture(bool val) { m_mesh_texture = val; }
// ------------------------------------------------------------------------
unsigned int getTextureSize() const { return m_texture_size; }
virtual unsigned int getTextureSize() const { return m_texture_size; }
// ------------------------------------------------------------------------
void reload(bool no_upload = false, uint8_t* preload_data = NULL,
video::IImage* preload_img = NULL);
@ -148,9 +146,11 @@ public:
// ------------------------------------------------------------------------
bool useThreadedLoading() const;
// ------------------------------------------------------------------------
void threadedReload(void* ptr) const;
virtual void threadedReload(void* ptr, void* param) const;
// ------------------------------------------------------------------------
void cleanThreadedLoader();
virtual void threadedSubImage(void* ptr) const;
// ------------------------------------------------------------------------
virtual void cleanThreadedLoader();
}; // STKTexture

View File

@ -16,12 +16,12 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "graphics/threaded_tex_loader.hpp"
#include "graphics/stk_texture.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "utils/string_utils.hpp"
#include "utils/vs.hpp"
#include <cassert>
#include <ITexture.h>
// ----------------------------------------------------------------------------
void* ThreadedTexLoader::startRoutine(void *obj)
@ -45,7 +45,8 @@ void* ThreadedTexLoader::startRoutine(void *obj)
pthread_cond_wait(ttl->m_cond_request, ttl->m_texture_queue_mutex);
waiting = ttl->m_stktm->isThreadedLoadTexturesEmpty();
}
STKTexture* target_tex = ttl->m_stktm->getThreadedLoadTexture();
irr::video::ITexture* target_tex =
ttl->m_stktm->getThreadedLoadTexture();
if (strcmp(target_tex->getName().getPtr(), "delete_ttl") == 0)
{
ttl->m_stktm->removeThreadedLoadTexture();
@ -64,7 +65,8 @@ void* ThreadedTexLoader::startRoutine(void *obj)
}
ttl->m_stktm->removeThreadedLoadTexture();
pthread_mutex_unlock(ttl->m_texture_queue_mutex);
target_tex->threadedReload(ttl->m_pbo_ptr + ttl->m_tex_size_loaded);
target_tex->threadedReload(ttl->m_pbo_ptr + ttl->m_tex_size_loaded,
ttl->m_stktm);
target_tex->cleanThreadedLoader();
ttl->m_tex_size_loaded += target_tex->getTextureSize();
ttl->m_completed_textures.push_back(target_tex);
@ -77,17 +79,11 @@ void ThreadedTexLoader::handleCompletedTextures()
{
#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
assert(m_locked);
size_t offset = 0;
for (STKTexture* stkt : m_completed_textures)
size_t offset = m_pbo_offset;
for (irr::video::ITexture* tex : m_completed_textures)
{
assert(!stkt->useThreadedLoading());
glBindTexture(GL_TEXTURE_2D, stkt->getOpenGLTextureName());
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, stkt->getSize().Width,
stkt->getSize().Height, stkt->isSingleChannel() ? GL_RED : GL_BGRA,
GL_UNSIGNED_BYTE, (const void*)(m_pbo_offset + offset));
if (stkt->hasMipMaps())
glGenerateMipmap(GL_TEXTURE_2D);
offset += stkt->getTextureSize();
tex->threadedSubImage((void*)offset);
offset += tex->getTextureSize();
}
m_completed_textures.clear();
#endif

View File

@ -25,7 +25,10 @@
#include <pthread.h>
#include <vector>
class STKTexture;
namespace irr
{
namespace video { class ITexture; }
}
class STKTexManager;
class ThreadedTexLoader : public NoCopy, public CanBeDeleted
@ -51,7 +54,7 @@ private:
bool m_finished_loading, m_locked;
std::vector<STKTexture*> m_completed_textures;
std::vector<irr::video::ITexture*> m_completed_textures;
public:
// ------------------------------------------------------------------------