]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/buffer.cpp
Remove the notion of default binding point from Buffer
[libs/gl.git] / source / core / buffer.cpp
index 98625b813bdfd9a626d8be87f52e6a2d7db228d6..4750846ab1dceb291a600043977f1c06f868f403 100644 (file)
@@ -2,8 +2,10 @@
 #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"
@@ -16,12 +18,11 @@ 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),
+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);
@@ -49,7 +50,11 @@ void Buffer::require_buffer_type(BufferType type)
 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");
 
@@ -70,8 +75,8 @@ void Buffer::allocate()
                        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;
@@ -96,8 +101,8 @@ void Buffer::data(const void *d)
                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;
@@ -124,8 +129,8 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
                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);
        }
 }
 
@@ -142,26 +147,16 @@ BufferRange *Buffer::create_range(unsigned s, unsigned o)
 
 void *Buffer::map()
 {
+       static Require _req(ARB_map_buffer_range);
+
        allocate();
-       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)
+       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()
@@ -171,8 +166,8 @@ 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;
@@ -180,8 +175,7 @@ bool Buffer::unmap()
 
 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())
                {
@@ -233,6 +227,16 @@ bool Buffer::set_current(BufferType type, const Buffer *buf)
        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;
 
@@ -259,8 +263,7 @@ void BufferRange::data(const void *d)
 
 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
@@ -282,7 +285,7 @@ const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
 {
        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);
@@ -304,14 +307,12 @@ bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange
 
 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