#include <msp/gl/extensions/arb_buffer_storage.h>
#include <msp/gl/extensions/arb_direct_state_access.h>
#include <msp/gl/extensions/arb_map_buffer_range.h>
+#include <msp/gl/extensions/khr_debug.h>
#include <msp/strings/format.h>
#include "buffer.h"
+#include "deviceinfo.h"
#include "error.h"
#include "misc.h"
#include "vertexsetup.h"
const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
-Buffer::Buffer(BufferType t):
- type(t),
- size(0)
+Buffer::Buffer():
+ size(0),
+ allocated(false)
{
- require_buffer_type(type);
+ static Require _req(ARB_vertex_buffer_object);
if(ARB_direct_state_access)
glCreateBuffers(1, &id);
void Buffer::storage(unsigned sz)
{
if(size>0)
- throw invalid_operation("Buffer::storage");
+ {
+ if(sz!=size)
+ throw incompatible_data("Buffer::storage");
+ return;
+ }
if(sz==0)
throw invalid_argument("Buffer::storage");
size = sz;
+}
+
+void Buffer::allocate()
+{
+ if(size==0)
+ throw invalid_operation("Buffer::allocate");
+ if(allocated)
+ return;
+
if(ARB_buffer_storage)
{
static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
glNamedBufferStorage(id, size, 0, flags);
else
{
- BindRestore _bind(this, type);
- glBufferStorage(type, size, 0, flags);
+ BindRestore _bind(this, ARRAY_BUFFER);
+ glBufferStorage(ARRAY_BUFFER, size, 0, flags);
}
+
+ allocated = true;
}
+ else
+ data(0);
}
void Buffer::set_usage(BufferUsage)
glNamedBufferData(id, size, d, STATIC_DRAW);
else
{
- BindRestore _bind(this, type);
- glBufferData(type, size, d, STATIC_DRAW);
+ BindRestore _bind(this, ARRAY_BUFFER);
+ glBufferData(ARRAY_BUFFER, size, d, STATIC_DRAW);
}
+
+ allocated = true;
}
void Buffer::data(unsigned sz, const void *d)
void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
{
+ if(size==0)
+ throw invalid_operation("Buffer::sub_data");
+
+ allocate();
+
if(ARB_direct_state_access)
glNamedBufferSubData(id, off, sz, d);
else
{
- BindRestore _bind(this, type);
- glBufferSubData(type, off, sz, d);
+ BindRestore _bind(this, ARRAY_BUFFER);
+ glBufferSubData(ARRAY_BUFFER, off, sz, d);
}
}
void *Buffer::map()
{
- if(ARB_map_buffer_range)
- {
- if(ARB_direct_state_access)
- return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
- else
- {
- BindRestore _bind(this, type);
- return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
- }
- }
- else if(ARB_direct_state_access)
- return glMapNamedBuffer(id, GL_READ_WRITE);
- else if(OES_mapbuffer)
+ static Require _req(ARB_map_buffer_range);
+
+ allocate();
+ if(ARB_direct_state_access)
+ return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
+ else
{
- BindRestore _bind(this, type);
- return glMapBuffer(type, GL_READ_WRITE);
+ BindRestore _bind(this, ARRAY_BUFFER);
+ return glMapBufferRange(ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
}
- else
- throw invalid_operation("Buffer::map");
}
bool Buffer::unmap()
return glUnmapNamedBuffer(id);
else if(OES_mapbuffer)
{
- BindRestore _bind(this, type);
- return glUnmapBuffer(type);
+ BindRestore _bind(this, ARRAY_BUFFER);
+ return glUnmapBuffer(ARRAY_BUFFER);
}
else
return true;
void Buffer::bind_to(BufferType t) const
{
- if(t!=type)
- require_buffer_type(t);
+ require_buffer_type(t);
if(t==ELEMENT_ARRAY_BUFFER)
if(const VertexSetup *vs = VertexSetup::current())
{
return true;
}
+void Buffer::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+ if(KHR_debug)
+ glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
+#else
+ (void)name;
+#endif
+}
+
vector<const BufferRange *> BufferRange::bound_uniform;
void BufferRange::bind_to(BufferType t, unsigned i)
{
- if(t!=buffer.type)
- Buffer::require_buffer_type(t);
+ Buffer::require_buffer_type(t);
if(set_current(t, i, this))
{
// The buffer gets bound as a side effect
{
if(type==UNIFORM_BUFFER)
{
- if(index>=get_n_uniform_buffer_bindings())
+ if(index>=Limits::get_global().max_uniform_bindings)
throw out_of_range("BufferRange::binding");
if(bound_uniform.size()<=index)
bound_uniform.resize(index+1);
unsigned BufferRange::get_n_uniform_buffer_bindings()
{
- static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
- return count;
+ return Limits::get_global().max_uniform_bindings;
}
unsigned BufferRange::get_uniform_buffer_alignment()
{
- static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
- return align;
+ return Limits::get_global().uniform_buffer_alignment;
}
} // namespace GL