From 5f0d9297e2616347a1f0c5211d45c6e694bb6bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 2 Jul 2013 17:29:18 +0200 Subject: [PATCH] Templated version of Buffer::{data,subData}(). More convenient for end user, assert that buffer size is divisible by T size to avoid some issues. --- src/Buffer.cpp | 13 ------------- src/Buffer.h | 27 +++++++++++++++++++++------ src/Test/BufferGLTest.cpp | 24 ++++++++++++------------ 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 1bdbaa335..69bddf75c 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -24,7 +24,6 @@ #include "Buffer.h" -#include #include #include "Context.h" @@ -128,18 +127,6 @@ Int Buffer::size() { return size; } -#ifndef MAGNUM_TARGET_GLES -Containers::Array Buffer::data() { - return subData(0, size()); -} - -Containers::Array Buffer::subData(const GLintptr offset, const GLsizeiptr size) { - Containers::Array data(size); - if(size) (this->*getSubDataImplementation)(offset, size, data); - return std::move(data); -} -#endif - #ifndef MAGNUM_TARGET_GLES2 void Buffer::copyImplementationDefault(Buffer* read, Buffer* write, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size) { glCopyBufferSubData(static_cast(read->bindInternal(Target::CopyRead)), static_cast(write->bindInternal(Target::CopyWrite)), readOffset, writeOffset, size); diff --git a/src/Buffer.h b/src/Buffer.h index 00001239f..5f16b13a1 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -31,8 +31,9 @@ #include #include #include -#include +#include #include +#include #include "Magnum.h" #include "OpenGL.h" @@ -546,12 +547,12 @@ class MAGNUM_EXPORT Buffer { * @requires_gl %Buffer data queries are not available in OpenGL ES. * Use @ref Magnum::Buffer::map() "map()" instead. */ - Containers::Array data(); + template Containers::Array data(); /** * @brief %Buffer subdata - * @param offset Offset in the buffer - * @param size Data size + * @param offset Byte offset in the buffer + * @param size Data size (count of @p T values) * * Returns data of given buffer portion. If @extension{EXT,direct_state_access} * is not available and the buffer is not already bound somewhere, it @@ -561,7 +562,7 @@ class MAGNUM_EXPORT Buffer { * @requires_gl %Buffer data queries are not available in OpenGL ES. * Use @ref Magnum::Buffer::map() "map()" instead. */ - Containers::Array subData(GLintptr offset, GLsizeiptr size); + template Containers::Array subData(GLintptr offset, GLsizeiptr size); #endif /** @@ -814,7 +815,7 @@ class MAGNUM_EXPORT Buffer { typedef void(Buffer::*GetSubDataImplementation)(GLintptr, GLsizeiptr, GLvoid*); void MAGNUM_LOCAL getSubDataImplementationDefault(GLintptr offset, GLsizeiptr size, GLvoid* data); void MAGNUM_LOCAL getSubDataImplementationDSA(GLintptr offset, GLsizeiptr size, GLvoid* data); - static MAGNUM_LOCAL GetSubDataImplementation getSubDataImplementation; + static GetSubDataImplementation getSubDataImplementation; #endif typedef void(Buffer::*DataImplementation)(GLsizeiptr, const GLvoid*, Usage); @@ -884,6 +885,20 @@ CORRADE_ENUMSET_OPERATORS(Buffer::MapFlags) /** @debugoperator{Magnum::Buffer} */ Debug MAGNUM_EXPORT operator<<(Debug debug, Buffer::Target value); +#ifndef MAGNUM_TARGET_GLES +template Containers::Array inline Buffer::data() { + const Int bufferSize = size(); + CORRADE_ASSERT(bufferSize%sizeof(T) == 0, "Buffer::data(): the buffer size is" << bufferSize << "bytes, which can't be expressed as array of types with size" << sizeof(T), {}); + return subData(0, bufferSize/sizeof(T)); +} + +template Containers::Array inline Buffer::subData(const GLintptr offset, const GLsizeiptr size) { + Containers::Array data(size); + if(size) (this->*getSubDataImplementation)(offset, size*sizeof(T), data); + return std::move(data); +} +#endif + } #endif diff --git a/src/Test/BufferGLTest.cpp b/src/Test/BufferGLTest.cpp index 8e40094d1..a87a06da7 100644 --- a/src/Test/BufferGLTest.cpp +++ b/src/Test/BufferGLTest.cpp @@ -76,15 +76,15 @@ void BufferGLTest::construct() { void BufferGLTest::data() { Buffer buffer; - constexpr char data[] = {2, 7, 5, 13, 25}; - buffer.setData(5, data, Buffer::Usage::StaticDraw); + constexpr Int data[] = {2, 7, 5, 13, 25}; + buffer.setData(5*4, data, Buffer::Usage::StaticDraw); MAGNUM_VERIFY_NO_ERROR(); - CORRADE_COMPARE(buffer.size(), 5); + CORRADE_COMPARE(buffer.size(), 5*4); /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - const Containers::Array contents = buffer.data(); + const Containers::Array contents = buffer.data(); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(contents.size(), 5); CORRADE_COMPARE(contents[0], 2); @@ -94,15 +94,15 @@ void BufferGLTest::data() { CORRADE_COMPARE(contents[4], 25); #endif - constexpr char subData[] = {125, 3, 15}; - buffer.setSubData(1, 3, subData); + constexpr Int subData[] = {125, 3, 15}; + buffer.setSubData(4, 3*4, subData); MAGNUM_VERIFY_NO_ERROR(); - CORRADE_COMPARE(buffer.size(), 5); + CORRADE_COMPARE(buffer.size(), 5*4); /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - const Containers::Array subContents = buffer.subData(1, 3); + const Containers::Array subContents = buffer.subData(4, 3); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(subContents.size(), 3); CORRADE_COMPARE(subContents[0], 125); @@ -140,7 +140,7 @@ void BufferGLTest::map() { /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - Containers::Array changedContents = buffer.data(); + Containers::Array changedContents = buffer.data(); CORRADE_COMPARE(changedContents.size(), 5); CORRADE_COMPARE(changedContents[3], 107); #endif @@ -172,7 +172,7 @@ void BufferGLTest::mapRange() { /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - Containers::Array changedContents = buffer.data(); + Containers::Array changedContents = buffer.data(); CORRADE_COMPARE(changedContents.size(), 5); CORRADE_COMPARE(changedContents[4], 107); #endif @@ -212,7 +212,7 @@ void BufferGLTest::mapRangeExplicitFlush() { /* Flushed range should be changed */ /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - Containers::Array changedContents = buffer.data(); + Containers::Array changedContents = buffer.data(); CORRADE_COMPARE(changedContents.size(), 5); CORRADE_COMPARE(changedContents[4], 107); #endif @@ -232,7 +232,7 @@ void BufferGLTest::copy() { /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - const Containers::Array subContents = buffer2.subData(2, 3); + const Containers::Array subContents = buffer2.subData(2, 3); CORRADE_COMPARE(subContents.size(), 3); CORRADE_COMPARE(subContents[0], 7); CORRADE_COMPARE(subContents[1], 5);