Browse Source

Templated version of Buffer::{data,subData}().

More convenient for end user, assert that buffer size is divisible by T
size to avoid some issues.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
5f0d9297e2
  1. 13
      src/Buffer.cpp
  2. 27
      src/Buffer.h
  3. 24
      src/Test/BufferGLTest.cpp

13
src/Buffer.cpp

@ -24,7 +24,6 @@
#include "Buffer.h"
#include <Containers/Array.h>
#include <Utility/Debug.h>
#include "Context.h"
@ -128,18 +127,6 @@ Int Buffer::size() {
return size;
}
#ifndef MAGNUM_TARGET_GLES
Containers::Array<char> Buffer::data() {
return subData(0, size());
}
Containers::Array<char> Buffer::subData(const GLintptr offset, const GLsizeiptr size) {
Containers::Array<char> 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<GLenum>(read->bindInternal(Target::CopyRead)), static_cast<GLenum>(write->bindInternal(Target::CopyWrite)), readOffset, writeOffset, size);

27
src/Buffer.h

@ -31,8 +31,9 @@
#include <cstddef>
#include <array>
#include <vector>
#include <Containers/Containers.h>
#include <Containers/Array.h>
#include <Containers/EnumSet.h>
#include <Utility/Assert.h>
#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<char> data();
template<class T> Containers::Array<T> 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<char> subData(GLintptr offset, GLsizeiptr size);
template<class T> Containers::Array<T> 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<class T> Containers::Array<T> 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<T>(0, bufferSize/sizeof(T));
}
template<class T> Containers::Array<T> inline Buffer::subData(const GLintptr offset, const GLsizeiptr size) {
Containers::Array<T> data(size);
if(size) (this->*getSubDataImplementation)(offset, size*sizeof(T), data);
return std::move(data);
}
#endif
}
#endif

24
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<char> contents = buffer.data();
const Containers::Array<Int> contents = buffer.data<Int>();
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<char> subContents = buffer.subData(1, 3);
const Containers::Array<Int> subContents = buffer.subData<Int>(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<char> changedContents = buffer.data();
Containers::Array<char> changedContents = buffer.data<char>();
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<char> changedContents = buffer.data();
Containers::Array<char> changedContents = buffer.data<char>();
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<char> changedContents = buffer.data();
Containers::Array<char> changedContents = buffer.data<char>();
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<char> subContents = buffer2.subData(2, 3);
const Containers::Array<char> subContents = buffer2.subData<char>(2, 3);
CORRADE_COMPARE(subContents.size(), 3);
CORRADE_COMPARE(subContents[0], 7);
CORRADE_COMPARE(subContents[1], 5);

Loading…
Cancel
Save