--- /dev/null
+extension ARB_buffer_storage
#include <stdexcept>
+#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/strings/format.h>
static Require _req_ubo(ARB_uniform_buffer_object);
}
+void Buffer::storage(unsigned sz)
+{
+ if(size>0)
+ throw invalid_operation("Buffer::storage");
+ if(sz==0)
+ throw invalid_argument("Buffer::storage");
+
+ size = sz;
+ if(ARB_buffer_storage)
+ {
+ static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
+ if(ARB_direct_state_access)
+ glNamedBufferStorage(id, size, 0, flags);
+ else
+ {
+ BindRestore _bind(this, type);
+ glBufferStorage(type, size, 0, flags);
+ }
+ }
+}
+
void Buffer::set_usage(BufferUsage u)
{
// TODO OpenGL ES 2.0 doesn't support read or copy usages
usage = u;
}
-void Buffer::data(unsigned sz, const void *d)
+void Buffer::data(const void *d)
{
+ if(size==0)
+ throw invalid_operation("Buffer::data");
+
+ if(ARB_buffer_storage)
+ return sub_data(0, size, d);
+
if(ARB_direct_state_access)
- glNamedBufferData(id, sz, d, usage);
+ glNamedBufferData(id, size, d, usage);
else
{
BindRestore _bind(this, type);
- glBufferData(type, sz, d, usage);
+ glBufferData(type, size, d, usage);
}
- size = sz;
+}
+
+void Buffer::data(unsigned sz, const void *d)
+{
+ if(size==0)
+ storage(sz);
+ else if(sz!=size)
+ throw incompatible_data("Buffer::data");
+
+ data(d);
}
void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
/** Returns the default binding type for the buffer. */
BufferType get_type() const { return type; }
+ /** Defines the storage size of the buffer. Must be called before data can
+ be uploaded. Storage cannot be changed once set. */
+ void storage(unsigned);
+
/** Sets the usage hint of the buffer. It will take effect the next time
the buffer's contents are defined. */
DEPRECATED void set_usage(BufferUsage);
/** Uploads data into the buffer, completely replacing any previous
- contents. */
- void data(unsigned, const void *);
+ contents. Storage must be defined beforehand. The data must have size
+ matching the defined storage. */
+ void data(const void *);
+
+ DEPRECATED void data(unsigned, const void *);
- /** Overwrites part of the buffer data with new data. The buffer size can
- not be changed with this call. */
+ /** Overwrites part of the buffer data with new data. Storage must be
+ defined beforehand. */
void sub_data(unsigned, unsigned, const void *);
unsigned get_size() const { return size; }
update_offset();
}
-void Bufferable::buffer_resized()
+void Bufferable::change_buffer(Buffer *buf)
{
for(Bufferable *b=this; b; b=b->next_in_buffer)
{
+ b->buffer = buf;
b->location_dirty = true;
b->dirty = true;
}
for(Bufferable *b=prev_in_buffer; b; b=b->prev_in_buffer)
{
+ b->buffer = buf;
b->location_dirty = true;
b->dirty = true;
}
buffer, and this object is inserted after it. */
void use_buffer(Buffer *buf, Bufferable *prev = 0);
- /** Informs the objects in this chain that the buffer has been resized and
- data should be reuploaded. */
- void buffer_resized();
+ /** Sets the buffer for the entire chain of objects. */
+ void change_buffer(Buffer *);
/** Returns the total amount of storage required by this object and others
in the same chain, including any alignment between objects. */
{
instance_data->append();
unsigned req_size = instance_data->get_required_buffer_size();
- // XXX Inefficient, but will be rewritten imminently
- if(instance_buffer->get_size()<req_size)
- instance_buffer->data(req_size, 0);
+ if(instance_buffer->get_size()>0 && instance_buffer->get_size()<req_size)
+ {
+ delete instance_buffer;
+ instance_buffer = new Buffer(ARRAY_BUFFER);
+ instance_data->use_buffer(instance_buffer);
+ }
}
update_instance_matrix(instances.size()-1);
}
const Mesh *mesh = object.get_mesh();
mesh->get_vertices().refresh();
+ if(instance_buffer->get_size()==0)
+ instance_buffer->storage(instance_data->get_required_buffer_size());
instance_data->refresh();
Renderer::Push push(renderer);
{
if(mask&VERTEX_BUFFER)
{
- if(!vbuf)
+ unsigned req_size = vertices.get_required_buffer_size();
+ if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
{
+ delete vbuf;
vbuf = new Buffer(ARRAY_BUFFER);
vertices.use_buffer(vbuf);
vtx_setup.set_vertex_array(vertices);
- }
- unsigned req_size = vertices.get_required_buffer_size();
- if(vbuf->get_size()<req_size)
- {
dirty |= VERTEX_BUFFER;
- vertices.buffer_resized();
}
}
if(mask&INDEX_BUFFER)
{
- if(!ibuf)
+ unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
+ if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
{
+ delete ibuf;
ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
if(!batches.empty())
- batches.front().use_buffer(ibuf);
+ batches.front().change_buffer(ibuf);
vtx_setup.set_index_buffer(*ibuf);
- }
- unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
- if(ibuf->get_size()<req_size)
- {
dirty |= INDEX_BUFFER;
- if(!batches.empty())
- batches.front().buffer_resized();
}
}
}
void Mesh::resize_buffers() const
{
if(dirty&VERTEX_BUFFER)
- vbuf->data(vertices.get_required_buffer_size(), 0);
+ vbuf->storage(vertices.get_required_buffer_size());
if(dirty&INDEX_BUFFER)
- ibuf->data(batches.front().get_required_buffer_size(), 0);
+ ibuf->storage(batches.front().get_required_buffer_size());
dirty = 0;
}
{
unsigned required_size = last_block->get_required_buffer_size();
if(last_block->get_required_buffer_size()>buffer->get_size())
- buffer->data(required_size, 0);
+ {
+ if(buffer->get_size()>0)
+ {
+ delete buffer;
+ buffer = new Buffer(UNIFORM_BUFFER);
+ last_block->change_buffer(buffer);
+ }
+
+ buffer->storage(required_size);
+ }
}
}