diff options
| author | degasus <wickmarkus@web.de> | 2013-02-01 16:43:08 +0100 |
|---|---|---|
| committer | degasus <wickmarkus@web.de> | 2013-02-01 16:43:08 +0100 |
| commit | 3bf4ffe7fafe8380776be36bac4e5ee7b9749fdd (patch) | |
| tree | 3a5a9700f2199b99d8bc79c6c79a2a4553766f24 /Source | |
| parent | 3af9840a4c6cf87db3191e5e56bd9bcb9138aadd (diff) | |
usable sync of ringbuffer
Diffstat (limited to 'Source')
| -rw-r--r-- | Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.cpp | 52 | ||||
| -rw-r--r-- | Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.h | 3 |
2 files changed, 39 insertions, 16 deletions
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.cpp b/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.cpp index 22fb044718..21c6cf75e6 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.cpp @@ -26,12 +26,12 @@ namespace OGL static const u32 SYNC_POINTS = 16; StreamBuffer::StreamBuffer(u32 type, size_t size, StreamType uploadType) -: m_uploadtype(uploadType), m_buffertype(type), m_size(size), m_iterator(0), m_last_iterator(0) +: m_uploadtype(uploadType), m_buffertype(type), m_size(size), m_iterator(0), m_used_iterator(0), m_free_iterator(0) { glGenBuffers(1, &m_buffer); if(m_uploadtype == STREAM_DETECT) - m_uploadtype = MAP_AND_ORPHAN; + m_uploadtype = MAP_AND_SYNC; Init(); } @@ -42,6 +42,8 @@ StreamBuffer::~StreamBuffer() glDeleteBuffers(1, &m_buffer); } +#define SLOT(x) (x)*SYNC_POINTS/m_size + void StreamBuffer::Alloc ( size_t size, u32 stride ) { size_t m_iterator_aligned = m_iterator; @@ -49,35 +51,57 @@ void StreamBuffer::Alloc ( size_t size, u32 stride ) m_iterator_aligned--; m_iterator_aligned = m_iterator_aligned - (m_iterator_aligned % stride) + stride; } + size_t iter_end = m_iterator_aligned + size; switch(m_uploadtype) { case MAP_AND_ORPHAN: - if(m_iterator_aligned+size >= m_size) { + if(iter_end >= m_size) { glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW); m_iterator_aligned = 0; } break; case MAP_AND_SYNC: - for(u32 i=m_iterator*SYNC_POINTS/m_size+1; i<(m_iterator_aligned+size)*SYNC_POINTS/m_size+1 && i < SYNC_POINTS; i++) + + // insert waiting slots for used memory + for(u32 i=SLOT(m_used_iterator); i<SLOT(m_iterator); i++) + { + fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); + } + m_used_iterator = m_iterator; + + // wait for new slots to end of buffer + for(u32 i=SLOT(m_free_iterator)+1; i<=SLOT(iter_end) && i < SYNC_POINTS; i++) { glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); glDeleteSync(fences[i]); } + m_free_iterator = iter_end; - if(m_iterator_aligned+size >= m_size) { - for(u32 i=m_last_iterator*SYNC_POINTS/m_size; i < SYNC_POINTS; i++) + // if buffer is full + if(iter_end >= m_size) { + + // insert waiting slots in unused space at the end of the buffer + for(u32 i=SLOT(m_used_iterator); i < SYNC_POINTS; i++) fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); - m_iterator_aligned = 0; - m_last_iterator = 0; - glClientWaitSync(fences[0], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); - glDeleteSync(fences[0]); + + // move to the start + m_used_iterator = m_iterator_aligned = m_iterator = 0; // offset 0 is always aligned + iter_end = size; + + // wait for space at the start + for(u32 i=0; i<=SLOT(iter_end); i++) + { + glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); + glDeleteSync(fences[i]); + } + m_free_iterator = iter_end; } + break; case BUFFERSUBDATA: m_iterator_aligned = 0; break; } - m_iterator = m_iterator_aligned; } @@ -85,8 +109,6 @@ size_t StreamBuffer::Upload ( u8* data, size_t size ) { switch(m_uploadtype) { case MAP_AND_SYNC: - for(u32 i=m_last_iterator*SYNC_POINTS/m_size; i<m_iterator*SYNC_POINTS/m_size; i++) - fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); case MAP_AND_ORPHAN: pointer = (u8*)glMapBufferRange(m_buffertype, m_iterator, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); if(pointer) { @@ -100,9 +122,9 @@ size_t StreamBuffer::Upload ( u8* data, size_t size ) glBufferSubData(m_buffertype, m_iterator, size, data); break; } - m_last_iterator = m_iterator; + size_t ret = m_iterator; m_iterator += size; - return m_last_iterator; + return ret; } void StreamBuffer::Init() diff --git a/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.h b/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.h index 022f18f785..514d4569c9 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/StreamBuffer.h @@ -53,7 +53,8 @@ private: size_t m_size; u8 *pointer; size_t m_iterator; - size_t m_last_iterator; + size_t m_used_iterator; + size_t m_free_iterator; GLsync *fences; }; |
