115 lines
2.0 KiB
C++
115 lines
2.0 KiB
C++
#ifndef __OPENGLPLAYGROUND_VBOMANAGER_HH__
|
|
#define __OPENGLPLAYGROUND_VBOMANAGER_HH__
|
|
|
|
#include <map>
|
|
#include <list>
|
|
#include <memory>
|
|
#include <cassert>
|
|
|
|
#include <glbinding/gl/types.h>
|
|
|
|
#include "Singleton.hh"
|
|
|
|
class VBOManager : public Singleton<VBOManager> {
|
|
public:
|
|
~VBOManager();
|
|
|
|
VBOManager(VBOManager const& copy) = delete;
|
|
VBOManager& operator=(VBOManager const& copy) = delete;
|
|
|
|
private:
|
|
VBOManager();
|
|
friend class Singleton<VBOManager>;
|
|
|
|
class VBO;
|
|
|
|
static const size_t default_size = 1048576;
|
|
static const size_t alignment = 8;
|
|
static const gl::GLenum default_type = gl::GL_STATIC_DRAW;
|
|
|
|
class AllocFailed {};
|
|
|
|
public:
|
|
class VBOAlloc {
|
|
public:
|
|
~VBOAlloc();
|
|
|
|
VBOAlloc(VBOAlloc&& move);
|
|
|
|
VBOAlloc(VBOAlloc const& copy) = delete;
|
|
VBOAlloc& operator=(VBOAlloc const& copy) = delete;
|
|
|
|
VBOAlloc& operator=(VBOAlloc&& move);
|
|
|
|
VBOAlloc();
|
|
|
|
size_t getOfs() const {
|
|
assert(_vbo);
|
|
return _ofs;
|
|
}
|
|
|
|
size_t getSize() const {
|
|
assert(_vbo);
|
|
return _size;
|
|
}
|
|
|
|
gl::GLuint getVBOId() const {
|
|
assert(_vbo);
|
|
return _vbo->getID();
|
|
}
|
|
|
|
explicit operator bool() const noexcept {
|
|
return (_vbo != nullptr);
|
|
}
|
|
|
|
private:
|
|
VBOAlloc(VBO &vbo, size_t ofs, size_t size);
|
|
|
|
friend class VBOManager;
|
|
friend class VBO;
|
|
|
|
VBO *_vbo;
|
|
size_t _ofs, _size;
|
|
};
|
|
|
|
VBOAlloc alloc(size_t size, gl::GLenum type = default_type);
|
|
|
|
private:
|
|
|
|
class VBO {
|
|
public:
|
|
VBO(size_t size = default_size, gl::GLenum type = default_type);
|
|
|
|
VBO(VBO const& copy) = delete;
|
|
VBO& operator=(VBO const& copy) = delete;
|
|
|
|
VBO(VBO&& move);
|
|
|
|
VBO& operator=(VBO&& move);
|
|
|
|
~VBO();
|
|
|
|
VBOAlloc alloc(size_t size);
|
|
|
|
void free(VBOAlloc& alloc);
|
|
|
|
gl::GLuint getID() const {
|
|
return _bufID;
|
|
}
|
|
|
|
private:
|
|
gl::GLuint _bufID;
|
|
|
|
struct _Entry {
|
|
size_t size;
|
|
bool used;
|
|
};
|
|
std::list<_Entry> _allocs;
|
|
|
|
};
|
|
|
|
std::map<gl::GLenum, std::vector<VBO> > _vbos;
|
|
};
|
|
|
|
#endif
|