[WIP] cgt::Texture tracking

parent 4ce9ab6d
......@@ -29,12 +29,17 @@
#include "cgt/texture.h"
#include "cgt/types.h"
#include "cgt/logmanager.h"
#include "cgt/gpucapabilities.h"
#include "cgt/filesystem.h"
namespace cgt {
Texture::Texture(GLenum type, const cgt::ivec3& dimensions, GLint internalFormat, Filter filter /*= LINEAR*/)
std::vector<Texture::TextureDescriptor> Texture::__texStorage(12, Texture::TextureDescriptor());
size_t Texture::__itTexStorage = 0;
tbb::spin_mutex Texture::__mtxTexStorage;
Texture::Texture(GLenum type, const cgt::ivec3& dimensions, GLint internalFormat, Filter filter /*= LINEAR*/)
: dimensions_(dimensions)
, internalformat_(internalFormat)
, filter_(filter)
......@@ -62,8 +67,20 @@ Texture::Texture(GLenum type, const cgt::ivec3& dimensions, GLint internalFormat
Texture::~Texture() {
if (id_)
glDeleteTextures(1, &id_);
if (id_) {
if (type_ == GL_TEXTURE_2D) {
tbb::spin_mutex::scoped_lock lock(__mtxTexStorage);
if (__texStorage[__itTexStorage].id != 0)
glDeleteTextures(1, &__texStorage[__itTexStorage].id);
__texStorage[__itTexStorage] = TextureDescriptor(id_, internalformat_, dimensions_);
__itTexStorage = (__itTexStorage + 1) % __texStorage.size();
}
else {
glDeleteTextures(1, &id_);
}
}
}
......@@ -320,6 +337,35 @@ int Texture::getSizeOnGPU() const {
return bpp * hmul(dimensions_);
}
GLuint Texture::generateId() {
id_ = 0;
tbb::spin_mutex::scoped_lock lock(__mtxTexStorage);
for (size_t i = 0; i < __texStorage.size(); ++i) {
size_t index = (__itTexStorage + i) % __texStorage.size();
auto& td = __texStorage[index];
if (td.id != 0 && td.internalFormat == internalformat_ && td.size == dimensions_) {
id_ = td.id;
if (index > __itTexStorage) {
memcpy(&__texStorage.front() + __itTexStorage + 1, &__texStorage.front() + __itTexStorage, (index - __itTexStorage) * sizeof(TextureDescriptor));
}
else if (index < __itTexStorage) {
memcpy(&__texStorage.front() + 1, &__texStorage.front(), index * sizeof(TextureDescriptor));
__texStorage.front() = __texStorage.back();
memcpy(&__texStorage.front() + __itTexStorage + 1, &__texStorage.front() + __itTexStorage, (__texStorage.size() - __itTexStorage - 1) * sizeof(TextureDescriptor));
}
__texStorage[__itTexStorage] = TextureDescriptor();
return id_;
}
}
glGenTextures(1, &id_);
return id_;
}
void Texture::setFilter(Filter filter) {
filter_ = filter;
applyFilter();
......
......@@ -29,7 +29,12 @@
#ifndef CGT_TEXTURE_H
#define CGT_TEXTURE_H
#include <tbb/spin_mutex.h>
#include <ctime>
#include <string>
#include <vector>
#include "cgt/cgt_gl.h"
#include "cgt/types.h"
#include "cgt/vector.h"
......@@ -138,11 +143,7 @@ public:
* Generate OpenGL texture ID
* @return The generated ID
*/
GLuint generateId() {
id_ = 0;
glGenTextures(1, &id_);
return id_;
}
GLuint generateId();
std::string getName() const { return name_; }
void setName(const std::string& name) { name_ = name; }
......@@ -217,7 +218,31 @@ public:
*/
bool isDepthTexture() const;
struct TextureDescriptor {
TextureDescriptor() { memset(this, 0, sizeof(TextureDescriptor)); }
TextureDescriptor(GLuint id, GLint internalFormat, const cgt::ivec3& s)
: timestamp(clock())
, id(id)
, internalFormat(internalFormat)
, size(s)
{}
bool operator() (const TextureDescriptor& lhs, const TextureDescriptor& rhs) const {
return lhs.timestamp < rhs.timestamp;
}
clock_t timestamp;
GLuint id;
GLint internalFormat;
cgt::ivec3 size;
};
static std::vector<TextureDescriptor> __texStorage;
static size_t __itTexStorage;
static tbb::spin_mutex __mtxTexStorage;
protected:
cgt::ivec3 dimensions_;
GLint internalformat_; ///< GL_RGB...
Filter filter_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment