]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/buffer.cpp
Move all OpenGL-specific code to a separate directory
[libs/gl.git] / source / core / buffer.cpp
index dcb83a7a5960f1cb48436cb9cff5063327c4d5f2..811b3e41d7cbfb9ed16e613b29a7c28674d07659 100644 (file)
@@ -1,10 +1,4 @@
 #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/gl/extensions/arb_vertex_buffer_object.h>
-#include <msp/gl/extensions/khr_debug.h>
-#include <msp/gl/extensions/oes_mapbuffer.h>
 #include <msp/strings/format.h>
 #include "buffer.h"
 #include "error.h"
@@ -14,25 +8,9 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Buffer *Buffer::scratch_binding = 0;
-
 Buffer::Buffer():
        size(0)
-{
-       static Require _req(ARB_vertex_buffer_object);
-
-       if(ARB_direct_state_access)
-               glCreateBuffers(1, &id);
-       else
-               glGenBuffers(1, &id);
-}
-
-Buffer::~Buffer()
-{
-       if(this==scratch_binding)
-               unbind_scratch();
-       glDeleteBuffers(1, &id);
-}
+{ }
 
 void Buffer::storage(unsigned sz)
 {
@@ -47,24 +25,7 @@ void Buffer::storage(unsigned sz)
 
        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
-               {
-                       bind_scratch();
-                       glBufferStorage(GL_ARRAY_BUFFER, size, 0, flags);
-               }
-       }
-       else if(ARB_direct_state_access)
-               glNamedBufferData(id, size, 0, GL_STATIC_DRAW);
-       else
-       {
-               bind_scratch();
-               glBufferData(GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
-       }
+       allocate();
 }
 
 void Buffer::data(const void *d)
@@ -77,13 +38,7 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
        if(size==0)
                throw invalid_operation("Buffer::sub_data");
 
-       if(ARB_direct_state_access)
-               glNamedBufferSubData(id, off, sz, d);
-       else
-       {
-               bind_scratch();
-               glBufferSubData(GL_ARRAY_BUFFER, off, sz, d);
-       }
+       BufferBackend::sub_data(off, sz, d);
 }
 
 void Buffer::require_size(unsigned req_sz) const
@@ -92,62 +47,5 @@ void Buffer::require_size(unsigned req_sz) const
                throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
 }
 
-void *Buffer::map()
-{
-       static Require _req(ARB_map_buffer_range);
-
-       if(ARB_direct_state_access)
-               return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
-       else
-       {
-               bind_scratch();
-               void *result = glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
-               return result;
-       }
-}
-
-bool Buffer::unmap()
-{
-       // TODO check if it's mapped
-       if(ARB_direct_state_access)
-               return glUnmapNamedBuffer(id);
-       else if(OES_mapbuffer)
-       {
-               bind_scratch();
-               bool result = glUnmapBuffer(GL_ARRAY_BUFFER);
-               return result;
-       }
-       else
-               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
-}
-
-void Buffer::bind_scratch()
-{
-       if(scratch_binding!=this)
-       {
-               glBindBuffer(GL_ARRAY_BUFFER, id);
-               scratch_binding = this;
-       }
-}
-
-void Buffer::unbind_scratch()
-{
-       if(scratch_binding)
-       {
-               glBindBuffer(GL_ARRAY_BUFFER, 0);
-               scratch_binding = 0;
-       }
-}
-
 } // namespace GL
 } // namespace Msp