X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fbuffer.cpp;h=811b3e41d7cbfb9ed16e613b29a7c28674d07659;hb=160e9eea29bd10034733d59507fa1bcca36be401;hp=78e85af0456b721a27316d9622849f00a2b37c3e;hpb=7b569bbfcfb65d8d88b47ac42ee1df6a7d27e784;p=libs%2Fgl.git diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index 78e85af0..811b3e41 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -1,40 +1,16 @@ #include -#include -#include -#include -#include #include #include "buffer.h" -#include "deviceinfo.h" #include "error.h" -#include "misc.h" -#include "vertexsetup.h" using namespace std; namespace Msp { namespace GL { -Buffer *Buffer::scratch_binding = 0; - Buffer::Buffer(): - size(0), - allocated(false) -{ - 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); -} + size(0) +{ } void Buffer::storage(unsigned sz) { @@ -48,63 +24,13 @@ void Buffer::storage(unsigned sz) 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; - if(ARB_direct_state_access) - glNamedBufferStorage(id, size, 0, flags); - else - { - bind_scratch(); - glBufferStorage(GL_ARRAY_BUFFER, size, 0, flags); - } - - allocated = true; - } - else - data(0); -} -void Buffer::set_usage(BufferUsage) -{ + allocate(); } 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, size, d, STATIC_DRAW); - else - { - bind_scratch(); - glBufferData(GL_ARRAY_BUFFER, size, d, STATIC_DRAW); - } - - allocated = true; -} - -void Buffer::data(unsigned sz, const void *d) -{ - if(size==0) - storage(sz); - else if(sz!=size) - throw incompatible_data("Buffer::data"); - - data(d); + return sub_data(0, size, d); } void Buffer::sub_data(unsigned off, unsigned sz, const void *d) @@ -112,15 +38,7 @@ 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 - { - bind_scratch(); - glBufferSubData(GL_ARRAY_BUFFER, off, sz, d); - } + BufferBackend::sub_data(off, sz, d); } void Buffer::require_size(unsigned req_sz) const @@ -129,63 +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); - - allocate(); - 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