Make sure all textures are uploaded without using timeout in TTL

This commit is contained in:
Benau
2017-03-11 13:35:33 +08:00
parent 6e80271ad9
commit b0adb92964
6 changed files with 56 additions and 39 deletions

View File

@@ -1837,6 +1837,9 @@ void IrrDriver::update(float dt)
PropertyAnimator::get()->update(dt);
STKTexManager::getInstance()
->checkThreadedLoadTextures(true/*util_queue_empty*/);
World *world = World::getWorld();
if (world)

View File

@@ -124,6 +124,7 @@ video::ITexture* STKTexManager::getTexture(const std::string& path, bool srgb,
bool no_upload, bool single_channel,
bool create_if_unfound)
{
checkThreadedLoadTextures(false/*util_queue_empty*/);
auto ret = m_all_textures.find(path);
if (!no_upload && ret != m_all_textures.end())
return ret->second;
@@ -163,7 +164,6 @@ video::ITexture* STKTexManager::getTexture(const std::string& path, bool srgb,
addThreadedLoadTexture(new_texture);
}
}
uploadBatch();
if (create_if_unfound && !no_upload)
addTexture(new_texture);
@@ -275,7 +275,6 @@ core::stringw STKTexManager::reloadTexture(const irr::core::stringw& name)
if (p.second->useThreadedLoading())
{
addThreadedLoadTexture(p.second);
uploadBatch();
}
Log::info("STKTexManager", "%s reloaded",
p.second->getName().getPtr());
@@ -302,7 +301,6 @@ core::stringw STKTexManager::reloadTexture(const irr::core::stringw& name)
if (p.second->useThreadedLoading())
{
addThreadedLoadTexture(p.second);
uploadBatch();
}
result += tex_name.c_str();
result += L" ";
@@ -351,11 +349,42 @@ void STKTexManager::setTextureErrorMessage(const std::string &error,
} // setTextureErrorMessage
// ----------------------------------------------------------------------------
void STKTexManager::uploadBatch()
void STKTexManager::checkThreadedLoadTextures(bool util_queue_empty)
{
#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
if (!CVS->supportsThreadedTextureLoading()) return;
bool uploaded = false;
bool empty_queue = false;
if (util_queue_empty)
{
while (true)
{
pthread_mutex_lock(&m_threaded_load_textures_mutex);
empty_queue = m_threaded_load_textures.empty();
pthread_mutex_unlock(&m_threaded_load_textures_mutex);
if (empty_queue)
{
for (ThreadedTexLoader* ttl : m_all_tex_loaders)
{
ttl->lock();
if (ttl->hasCompletedTextures())
{
ttl->setFinishLoading();
uploaded = true;
}
ttl->unlock(false/*finish_it*/);
}
break;
}
else
{
checkThreadedLoadTextures(false/*util_queue_empty*/);
}
}
}
if (empty_queue && !uploaded)
return;
uploaded = false;
for (ThreadedTexLoader* ttl : m_all_tex_loaders)
{
ttl->lock();
@@ -370,7 +399,7 @@ void STKTexManager::uploadBatch()
}
else
{
ttl->unlock();
ttl->unlock(false/*finish_it*/);
}
}
if (uploaded)
@@ -389,10 +418,10 @@ void STKTexManager::uploadBatch()
glDeleteSync(sync);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
for (ThreadedTexLoader* ttl : m_all_tex_loaders)
ttl->unlock();
ttl->unlock(true/*finish_it*/);
}
#endif
} // uploadBatch
} // checkThreadedLoadTextures
// ----------------------------------------------------------------------------
bool STKTexManager::SmallestTexture::operator()

View File

@@ -149,7 +149,7 @@ public:
std::string(detail));
} // getTexture
// ------------------------------------------------------------------------
void uploadBatch();
void checkThreadedLoadTextures(bool util_queue_empty);
// ------------------------------------------------------------------------
STKTexture* getThreadedLoadTexture()
{ return m_threaded_load_textures.top(); }

View File

@@ -38,32 +38,13 @@ void* ThreadedTexLoader::startRoutine(void *obj)
{
continue;
}
bool no_unloaded_tex = ttl->m_completed_textures.empty();
pthread_mutex_lock(ttl->m_texture_queue_mutex);
bool queue_empty = ttl->m_stktm->isThreadedLoadTexturesEmpty();
bool waiting = queue_empty && no_unloaded_tex;
bool waiting = ttl->m_stktm->isThreadedLoadTexturesEmpty();
while (waiting)
{
pthread_cond_wait(ttl->m_cond_request, ttl->m_texture_queue_mutex);
waiting = ttl->m_stktm->isThreadedLoadTexturesEmpty();
}
if (queue_empty)
{
if (ttl->m_waiting_timeout++ > 10)
{
pthread_mutex_lock(&ttl->m_mutex);
ttl->m_finished_loading = true;
ttl->m_tex_size_loaded = 0;
ttl->m_waiting_timeout = 0;
pthread_mutex_unlock(&ttl->m_mutex);
}
pthread_mutex_unlock(ttl->m_texture_queue_mutex);
continue;
}
else
{
ttl->m_waiting_timeout = 0;
}
STKTexture* target_tex = ttl->m_stktm->getThreadedLoadTexture();
if (strcmp(target_tex->getName().getPtr(), "delete_ttl") == 0)
{
@@ -76,9 +57,7 @@ void* ThreadedTexLoader::startRoutine(void *obj)
ttl->m_tex_capacity)
{
pthread_mutex_lock(&ttl->m_mutex);
ttl->m_finished_loading = true;
ttl->m_tex_size_loaded = 0;
ttl->m_waiting_timeout = 0;
ttl->setFinishLoading();
pthread_mutex_unlock(&ttl->m_mutex);
pthread_mutex_unlock(ttl->m_texture_queue_mutex);
continue;

View File

@@ -45,7 +45,7 @@ private:
STKTexManager* m_stktm;
unsigned m_tex_size_loaded, m_waiting_timeout;
unsigned m_tex_size_loaded;
pthread_t m_thread;
@@ -63,8 +63,8 @@ public:
: m_tex_capacity(capacity), m_pbo_offset(offset),
m_pbo_ptr(pbo_ptr), m_texture_queue_mutex(mutex),
m_cond_request(cond), m_stktm(stktm),
m_tex_size_loaded(0), m_waiting_timeout(0),
m_finished_loading(false), m_locked(false)
m_tex_size_loaded(0), m_finished_loading(false),
m_locked(false)
{
pthread_mutex_init(&m_mutex, NULL);
pthread_create(&m_thread, NULL, &startRoutine, this);
@@ -78,6 +78,14 @@ public:
// ------------------------------------------------------------------------
bool finishedLoading() const { return m_finished_loading; }
// ------------------------------------------------------------------------
void setFinishLoading()
{
m_finished_loading = true;
m_tex_size_loaded = 0;
}
// ------------------------------------------------------------------------
bool hasCompletedTextures() const { return !m_completed_textures.empty(); }
// ------------------------------------------------------------------------
void handleCompletedTextures();
// ------------------------------------------------------------------------
void lock()
@@ -86,11 +94,12 @@ public:
m_locked = true;
}
// ------------------------------------------------------------------------
void unlock()
void unlock(bool finish_it)
{
if (!m_locked) return;
m_locked = false;
m_finished_loading = false;
if (finish_it)
m_finished_loading = false;
pthread_mutex_unlock(&m_mutex);
}

View File

@@ -25,7 +25,6 @@
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "guiengine/engine.hpp"
#include "input/input_manager.hpp"
#include "input/wiimote_manager.hpp"
@@ -265,8 +264,6 @@ void MainLoop::run()
GUIEngine::update(dt);
PROFILER_POP_CPU_MARKER();
STKTexManager::getInstance()->uploadBatch();
// Update sfx and music after graphics, so that graphics code
// can use as many threads as possible without interfering
// with audio