X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fbuffer.cpp;h=71ab6e2387a4d451e3a70c8d76a828cd6a598176;hb=6065f6622cc275dc0b20baaf7c267e71169d18f3;hp=ecf7b49a15e35d29493c83f1a6ea2f8013065483;hpb=9b3bce7ae76ff8c0c81315d2505ea96bf422a318;p=libs%2Fgl.git diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index ecf7b49a..71ab6e23 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -2,19 +2,20 @@ #include #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) @@ -29,6 +30,8 @@ Buffer::Buffer(): Buffer::~Buffer() { + if(this==scratch_binding) + unbind_scratch(); glDeleteBuffers(1, &id); } @@ -60,9 +63,8 @@ void Buffer::allocate() glNamedBufferStorage(id, size, 0, flags); else { - glBindBuffer(GL_ARRAY_BUFFER, id); + bind_scratch(); glBufferStorage(GL_ARRAY_BUFFER, size, 0, flags); - glBindBuffer(GL_ARRAY_BUFFER, 0); } allocated = true; @@ -71,10 +73,6 @@ void Buffer::allocate() data(0); } -void Buffer::set_usage(BufferUsage) -{ -} - void Buffer::data(const void *d) { if(size==0) @@ -84,27 +82,16 @@ void Buffer::data(const void *d) return sub_data(0, size, d); if(ARB_direct_state_access) - glNamedBufferData(id, size, d, STATIC_DRAW); + glNamedBufferData(id, size, d, GL_STATIC_DRAW); else { - glBindBuffer(GL_ARRAY_BUFFER, id); - glBufferData(GL_ARRAY_BUFFER, size, d, STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + bind_scratch(); + glBufferData(GL_ARRAY_BUFFER, size, d, GL_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); -} - void Buffer::sub_data(unsigned off, unsigned sz, const void *d) { if(size==0) @@ -116,9 +103,8 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d) glNamedBufferSubData(id, off, sz, d); else { - glBindBuffer(GL_ARRAY_BUFFER, id); + bind_scratch(); glBufferSubData(GL_ARRAY_BUFFER, off, sz, d); - glBindBuffer(GL_ARRAY_BUFFER, 0); } } @@ -137,9 +123,8 @@ void *Buffer::map() return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT); else { - glBindBuffer(GL_ARRAY_BUFFER, id); + bind_scratch(); void *result = glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT); - glBindBuffer(GL_ARRAY_BUFFER, 0); return result; } } @@ -151,9 +136,8 @@ bool Buffer::unmap() return glUnmapNamedBuffer(id); else if(OES_mapbuffer) { - glBindBuffer(GL_ARRAY_BUFFER, id); + bind_scratch(); bool result = glUnmapBuffer(GL_ARRAY_BUFFER); - glBindBuffer(GL_ARRAY_BUFFER, 0); return result; } else @@ -170,5 +154,23 @@ void Buffer::set_debug_name(const string &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