]> git.tdb.fi Git - libs/gl.git/blobdiff - source/buffer.cpp
Use vector when there's no reason to use some other container
[libs/gl.git] / source / buffer.cpp
index 395057cd90ff4bf61e95a7be7c833fefaa20515f..2b18d7d7d7d3bf0a47611207850aec5aabc4cc9d 100644 (file)
-#include "arb_vertex_buffer_object.h"
-#include "extension.h"
+#include <stdexcept>
+#include <msp/gl/extensions/arb_direct_state_access.h>
+#include <msp/gl/extensions/arb_map_buffer_range.h>
 #include "buffer.h"
+#include "error.h"
+#include "mesh.h"
+#include "misc.h"
+
+using namespace std;
 
 namespace Msp {
 namespace GL {
 
+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),
-       usage(STATIC_DRAW)
+       usage(STATIC_DRAW),
+       size(0)
 {
-       static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
-       if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
-               static RequireExtension _req_pbo("GL_ARB_pixel_buffer_object");
+       require_buffer_type(type);
 
-       glGenBuffersARB(1, &id);
+       if(ARB_direct_state_access)
+               glCreateBuffers(1, &id);
+       else
+               glGenBuffers(1, &id);
 }
 
 Buffer::~Buffer()
 {
-       glDeleteBuffersARB(1, &id);
+       for(unsigned i=0; i<5; ++i)
+               if(bound[i]==this)
+                       unbind_from(buffer_types[i]);
+       glDeleteBuffers(1, &id);
+}
+
+void Buffer::require_buffer_type(BufferType type)
+{
+       static Require _req_vbo(ARB_vertex_buffer_object);
+       if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
+               static Require _req_pbo(ARB_pixel_buffer_object);
+       else if(type==UNIFORM_BUFFER)
+               static Require _req_ubo(ARB_uniform_buffer_object);
 }
 
 void Buffer::set_usage(BufferUsage u)
 {
+       // TODO OpenGL ES 2.0 doesn't support read or copy usages
        usage = u;
 }
 
-void Buffer::data(unsigned size, const void *d)
+void Buffer::data(unsigned sz, const void *d)
 {
-       const Buffer *old = current(type);
-       bind();
-       glBufferDataARB(type, size, d, usage);
-       restore(old, type);
+       if(ARB_direct_state_access)
+               glNamedBufferData(id, sz, d, usage);
+       else
+       {
+               BindRestore _bind(this, type);
+               glBufferData(type, sz, d, usage);
+       }
+       size = sz;
 }
 
-void Buffer::sub_data(unsigned offset, unsigned size, const void *d)
+void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
 {
-       const Buffer *old = current(type);
-       bind();
-       glBufferSubDataARB(type, offset, size, d);
-       restore(old, type);
+       if(ARB_direct_state_access)
+               glNamedBufferSubData(id, off, sz, d);
+       else
+       {
+               BindRestore _bind(this, type);
+               glBufferSubData(type, off, sz, d);
+       }
 }
 
-void Buffer::bind_to(BufferType t) const
+BufferRange *Buffer::create_range(unsigned s, unsigned o)
+{
+       return new BufferRange(*this, s, o);
+}
+
+void *Buffer::map(BufferAccess access)
 {
-       const Buffer *&ptr = binding(t);
-       if(ptr!=this)
+       if(ARB_map_buffer_range)
+       {
+               GLenum access_bits = 0;
+               if(access==READ_ONLY)
+                       access_bits = GL_MAP_READ_BIT;
+               else if(access==WRITE_ONLY)
+                       access_bits = GL_MAP_WRITE_BIT;
+               else if(access==READ_WRITE)
+                       access_bits = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT;
+               if(ARB_direct_state_access)
+                       return glMapNamedBufferRange(id, 0, size, access_bits);
+               else
+               {
+                       BindRestore _bind(this, type);
+                       return glMapBufferRange(type, 0, size, access_bits);
+               }
+       }
+       else if(ARB_direct_state_access)
+               return glMapNamedBuffer(id, access);
+       else if(OES_mapbuffer)
        {
-               glBindBufferARB(t, id);
-               ptr = this;
+               BindRestore _bind(this, type);
+               return glMapBuffer(type, access);
        }
+       else
+               throw invalid_operation("Buffer::map");
 }
 
-void Buffer::unbind_from(BufferType type)
+bool Buffer::unmap()
 {
-       const Buffer *&ptr = binding(type);
-       if(ptr)
+       // TODO check if it's mapped
+       if(ARB_direct_state_access)
+               return glUnmapNamedBuffer(id);
+       else if(OES_mapbuffer)
        {
-               glBindBufferARB(type, 0);
-               ptr = 0;
+               BindRestore _bind(this, type);
+               return glUnmapBuffer(type);
        }
+       else
+               return true;
+}
+
+void Buffer::bind_to(BufferType t) const
+{
+       if(t!=type)
+               require_buffer_type(t);
+       if(t==ELEMENT_ARRAY_BUFFER)
+               if(const Mesh *m = Mesh::current())
+               {
+                       // Don't change the binding in a mesh's vertex array object
+                       if(this==m->get_index_buffer())
+                               return;
+                       throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
+               }
+       if(set_current(t, this))
+               glBindBuffer(t, id);
+}
+
+const Buffer *Buffer::current(BufferType t)
+{
+       if(t==ELEMENT_ARRAY_BUFFER)
+               if(const Mesh *m = Mesh::current())
+                       return m->get_index_buffer();
+       return binding(t);
+}
+
+void Buffer::unbind_from(BufferType type)
+{
+       if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
+               throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
+       if(set_current(type, 0))
+               glBindBuffer(type, 0);
 }
 
 const Buffer *&Buffer::binding(BufferType type)
@@ -70,22 +162,101 @@ const Buffer *&Buffer::binding(BufferType type)
        case ELEMENT_ARRAY_BUFFER: return bound[1];
        case PIXEL_PACK_BUFFER:    return bound[2];
        case PIXEL_UNPACK_BUFFER:  return bound[3];
-       default: throw InvalidParameterValue("Invalid buffer type");
+       case UNIFORM_BUFFER:       return bound[4];
+       default: throw invalid_argument("Buffer::binding");
        }
 }
 
-void Buffer::restore(const Buffer *buf, BufferType type)
+bool Buffer::set_current(BufferType type, const Buffer *buf)
 {
-       if(buf!=current(type))
+       const Buffer *&ptr = binding(type);
+       if(ptr==buf)
+               return false;
+
+       ptr = buf;
+       return true;
+}
+
+
+vector<const BufferRange *> BufferRange::bound_uniform;
+
+BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
+       buffer(b),
+       offset(o),
+       size(s)
+{
+       if(o>buffer.get_size() || o+s>buffer.get_size())
+               throw out_of_range("BufferRange::BufferRange");
+}
+
+BufferRange::~BufferRange()
+{
+       for(unsigned i=0; i<bound_uniform.size(); ++i)
+               if(bound_uniform[i]==this)
+                       unbind_from(UNIFORM_BUFFER, i);
+}
+
+void BufferRange::data(const void *d)
+{
+       buffer.sub_data(offset, size, d);
+}
+
+void BufferRange::bind_to(BufferType t, unsigned i)
+{
+       if(t!=buffer.type)
+               Buffer::require_buffer_type(t);
+       if(set_current(t, i, this))
        {
-               if(buf)
-                       buf->bind_to(type);
-               else
-                       unbind_from(type);
+               // The buffer gets bound as a side effect
+               Buffer::set_current(t, &buffer);
+               glBindBufferRange(t, i, buffer.id, offset, size);
        }
 }
 
-const Buffer *Buffer::bound[4] = { 0, 0, 0, 0 };
+void BufferRange::unbind_from(BufferType t, unsigned i)
+{
+       if(set_current(t, i, 0))
+       {
+               Buffer::set_current(t, 0);
+               glBindBufferBase(t, i, 0);
+       }
+}
+
+const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
+{
+       if(type==UNIFORM_BUFFER)
+       {
+               if(index>=get_n_uniform_buffer_bindings())
+                       throw out_of_range("BufferRange::binding");
+               if(bound_uniform.size()<=index)
+                       bound_uniform.resize(index+1);
+               return bound_uniform[index];
+       }
+       else
+               throw invalid_argument("BufferRange::binding");
+}
+
+bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
+{
+       const BufferRange *&ptr = binding(type, index);
+       if(ptr==buf)
+               return false;
+
+       ptr = buf;
+       return true;
+}
+
+unsigned BufferRange::get_n_uniform_buffer_bindings()
+{
+       static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
+       return count;
+}
+
+unsigned BufferRange::get_uniform_buffer_alignment()
+{
+       static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
+       return align;
+}
 
 } // namespace GL
 } // namespace Msp