2014-06-20 01:30:09 +00:00
|
|
|
#include "resources/TextureResource.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "platform.h"
|
2013-06-21 16:49:29 +00:00
|
|
|
#include GLHEADER
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "ImageIO.h"
|
|
|
|
#include "Renderer.h"
|
|
|
|
#include "Util.h"
|
2017-01-22 23:28:06 +00:00
|
|
|
#include "Settings.h"
|
2014-03-20 01:13:59 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
TextureDataManager TextureResource::sTextureDataManager;
|
2014-01-19 18:23:01 +00:00
|
|
|
std::map< TextureResource::TextureKeyType, std::weak_ptr<TextureResource> > TextureResource::sTextureMap;
|
2017-01-22 23:28:06 +00:00
|
|
|
std::set<TextureResource*> TextureResource::sAllTextures;
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
TextureResource::TextureResource(const std::string& path, bool tile, bool dynamic) : mTextureData(nullptr), mForceLoad(false)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
// Create a texture data object for this texture
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
// If there is a path then the 'dynamic' flag tells us whether to use the texture
|
|
|
|
// data manager to manage loading/unloading of this texture
|
|
|
|
std::shared_ptr<TextureData> data;
|
|
|
|
if (dynamic)
|
|
|
|
{
|
|
|
|
data = sTextureDataManager.add(this, tile);
|
|
|
|
data->initFromPath(path);
|
|
|
|
// Force the texture manager to load it using a blocking load
|
|
|
|
sTextureDataManager.load(data, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mTextureData = std::shared_ptr<TextureData>(new TextureData(tile));
|
|
|
|
data = mTextureData;
|
|
|
|
data->initFromPath(path);
|
|
|
|
// Load it so we can read the width/height
|
|
|
|
data->load();
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
mSize << data->width(), data->height();
|
|
|
|
mSourceSize << data->sourceWidth(), data->sourceHeight();
|
|
|
|
}
|
|
|
|
else
|
2014-03-20 01:13:59 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
// Create a texture managed by this class because it cannot be dynamically loaded and unloaded
|
|
|
|
mTextureData = std::shared_ptr<TextureData>(new TextureData(tile));
|
2014-03-20 01:13:59 +00:00
|
|
|
}
|
2017-01-22 23:28:06 +00:00
|
|
|
sAllTextures.insert(this);
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
TextureResource::~TextureResource()
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
if (mTextureData == nullptr)
|
|
|
|
sTextureDataManager.remove(this);
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
sAllTextures.erase(sAllTextures.find(this));
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
void TextureResource::initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height)
|
2013-09-20 23:55:05 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
// This is only valid if we have a local texture data object
|
|
|
|
assert(mTextureData != nullptr);
|
|
|
|
mTextureData->releaseVRAM();
|
|
|
|
mTextureData->releaseRAM();
|
|
|
|
mTextureData->initFromRGBA(dataRGBA, width, height);
|
|
|
|
// Cache the image dimensions
|
|
|
|
mSize << width, height;
|
|
|
|
mSourceSize << mTextureData->sourceWidth(), mTextureData->sourceHeight();
|
2013-09-20 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
void TextureResource::initFromMemory(const char* data, size_t length)
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
// This is only valid if we have a local texture data object
|
|
|
|
assert(mTextureData != nullptr);
|
|
|
|
mTextureData->releaseVRAM();
|
|
|
|
mTextureData->releaseRAM();
|
|
|
|
mTextureData->initImageFromMemory((const unsigned char*)data, length);
|
|
|
|
// Get the size from the texture data
|
|
|
|
mSize << mTextureData->width(), mTextureData->height();
|
|
|
|
mSourceSize << mTextureData->sourceWidth(), mTextureData->sourceHeight();
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
const Eigen::Vector2i TextureResource::getSize() const
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
return mSize;
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
bool TextureResource::isTiled() const
|
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
if (mTextureData != nullptr)
|
|
|
|
return mTextureData->tiled();
|
|
|
|
std::shared_ptr<TextureData> data = sTextureDataManager.get(this);
|
|
|
|
return data->tiled();
|
2014-01-19 18:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
bool TextureResource::bind()
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
if (mTextureData != nullptr)
|
|
|
|
{
|
|
|
|
mTextureData->uploadAndBind();
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
else
|
2017-01-22 23:28:06 +00:00
|
|
|
{
|
|
|
|
return sTextureDataManager.bind(this);
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, bool tile, bool forceLoad, bool dynamic)
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2013-10-04 23:09:54 +00:00
|
|
|
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
|
|
|
|
|
2014-05-23 22:21:01 +00:00
|
|
|
const std::string canonicalPath = getCanonicalPath(path);
|
|
|
|
if(canonicalPath.empty())
|
2013-08-07 04:35:06 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
std::shared_ptr<TextureResource> tex(new TextureResource("", tile, false));
|
2014-01-19 18:23:01 +00:00
|
|
|
rm->addReloadable(tex); //make sure we get properly deinitialized even though we do nothing on reinitialization
|
2013-08-07 04:35:06 +00:00
|
|
|
return tex;
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2014-05-23 22:21:01 +00:00
|
|
|
TextureKeyType key(canonicalPath, tile);
|
2014-01-19 18:23:01 +00:00
|
|
|
auto foundTexture = sTextureMap.find(key);
|
2013-07-09 05:44:24 +00:00
|
|
|
if(foundTexture != sTextureMap.end())
|
|
|
|
{
|
|
|
|
if(!foundTexture->second.expired())
|
|
|
|
return foundTexture->second.lock();
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
// need to create it
|
|
|
|
std::shared_ptr<TextureResource> tex;
|
2017-01-22 23:28:06 +00:00
|
|
|
tex = std::shared_ptr<TextureResource>(new TextureResource(key.first, tile, dynamic));
|
|
|
|
std::shared_ptr<TextureData> data = sTextureDataManager.get(tex.get());
|
2014-03-20 01:13:59 +00:00
|
|
|
|
|
|
|
// is it an SVG?
|
2017-01-22 23:28:06 +00:00
|
|
|
if(key.first.substr(key.first.size() - 4, std::string::npos) != ".svg")
|
2014-03-20 01:13:59 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
// Probably not. Add it to our map. We don't add SVGs because 2 svgs might be rasterized at different sizes
|
2014-03-27 21:47:25 +00:00
|
|
|
sTextureMap[key] = std::weak_ptr<TextureResource>(tex);
|
2014-03-20 01:13:59 +00:00
|
|
|
}
|
2017-01-22 23:28:06 +00:00
|
|
|
|
|
|
|
// Add it to the reloadable list
|
|
|
|
rm->addReloadable(tex);
|
|
|
|
|
|
|
|
// Force load it if necessary. Note that it may get dumped from VRAM if we run low
|
|
|
|
if (forceLoad)
|
|
|
|
{
|
|
|
|
tex->mForceLoad = forceLoad;
|
|
|
|
data->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tex;
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
2014-03-24 21:29:56 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
// For scalable source images in textures we want to set the resolution to rasterize at
|
|
|
|
void TextureResource::rasterizeAt(size_t width, size_t height)
|
2014-03-24 21:29:56 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
std::shared_ptr<TextureData> data;
|
|
|
|
if (mTextureData != nullptr)
|
|
|
|
data = mTextureData;
|
|
|
|
else
|
|
|
|
data = sTextureDataManager.get(this);
|
|
|
|
mSourceSize << (float)width, (float)height;
|
|
|
|
data->setSourceSize((float)width, (float)height);
|
|
|
|
if (mForceLoad || (mTextureData != nullptr))
|
|
|
|
data->load();
|
2014-03-24 21:29:56 +00:00
|
|
|
}
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
Eigen::Vector2f TextureResource::getSourceImageSize() const
|
2014-03-27 21:47:25 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
return mSourceSize;
|
|
|
|
}
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
bool TextureResource::isInitialized() const
|
|
|
|
{
|
|
|
|
return true;
|
2014-03-27 21:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t TextureResource::getTotalMemUsage()
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
2017-01-22 23:28:06 +00:00
|
|
|
// Count up all textures that manage their own texture data
|
|
|
|
for (auto tex : sAllTextures)
|
2014-03-27 21:47:25 +00:00
|
|
|
{
|
2017-01-22 23:28:06 +00:00
|
|
|
if (tex->mTextureData != nullptr)
|
|
|
|
total += tex->mTextureData->getVRAMUsage();
|
2014-03-27 21:47:25 +00:00
|
|
|
}
|
2017-01-22 23:28:06 +00:00
|
|
|
// Now get the committed memory from the manager
|
|
|
|
total += sTextureDataManager.getCommittedSize();
|
|
|
|
// And the size of the loading queue
|
|
|
|
total += sTextureDataManager.getQueueSize();
|
|
|
|
return total;
|
|
|
|
}
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2017-01-22 23:28:06 +00:00
|
|
|
size_t TextureResource::getTotalTextureSize()
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
// Count up all textures that manage their own texture data
|
|
|
|
for (auto tex : sAllTextures)
|
|
|
|
{
|
|
|
|
if (tex->mTextureData != nullptr)
|
|
|
|
total += tex->getSize().x() * tex->getSize().y() * 4;
|
|
|
|
}
|
|
|
|
// Now get the total memory from the manager
|
|
|
|
total += sTextureDataManager.getTotalSize();
|
2014-03-27 21:47:25 +00:00
|
|
|
return total;
|
|
|
|
}
|
2017-01-22 23:28:06 +00:00
|
|
|
|
|
|
|
void TextureResource::unload(std::shared_ptr<ResourceManager>& rm)
|
|
|
|
{
|
|
|
|
// Release the texture's resources
|
|
|
|
std::shared_ptr<TextureData> data;
|
|
|
|
if (mTextureData == nullptr)
|
|
|
|
data = sTextureDataManager.get(this);
|
|
|
|
else
|
|
|
|
data = mTextureData;
|
|
|
|
|
|
|
|
data->releaseVRAM();
|
|
|
|
data->releaseRAM();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureResource::reload(std::shared_ptr<ResourceManager>& rm)
|
|
|
|
{
|
|
|
|
// For dynamically loaded textures the texture manager will load them on demand.
|
|
|
|
// For manually loaded textures we have to reload them here
|
|
|
|
if (mTextureData)
|
|
|
|
mTextureData->load();
|
|
|
|
}
|