]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/buffer.cpp
Store implementation limits in a central struct
[libs/gl.git] / source / core / buffer.cpp
index 56705b8701b88a00b691bf25725d10042554679a..5b3339a1d66721c51d417494ab77f80c6a58d78c 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"
@@ -18,7 +20,8 @@ BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUF
 
 Buffer::Buffer(BufferType t):
        type(t),
-       size(0)
+       size(0),
+       allocated(false)
 {
        require_buffer_type(type);
 
@@ -48,11 +51,24 @@ 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");
 
        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;
@@ -63,7 +79,11 @@ void Buffer::storage(unsigned sz)
                        BindRestore _bind(this, type);
                        glBufferStorage(type, size, 0, flags);
                }
+
+               allocated = true;
        }
+       else
+               data(0);
 }
 
 void Buffer::set_usage(BufferUsage)
@@ -85,6 +105,8 @@ void Buffer::data(const void *d)
                BindRestore _bind(this, type);
                glBufferData(type, size, d, STATIC_DRAW);
        }
+
+       allocated = true;
 }
 
 void Buffer::data(unsigned sz, const void *d)
@@ -99,6 +121,11 @@ 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
@@ -121,6 +148,7 @@ BufferRange *Buffer::create_range(unsigned s, unsigned o)
 
 void *Buffer::map()
 {
+       allocate();
        if(ARB_map_buffer_range)
        {
                if(ARB_direct_state_access)
@@ -211,6 +239,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;
 
@@ -260,7 +298,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);
@@ -282,14 +320,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