Browse Source

GL: allow creating a Buffer and directly filling it with data.

pull/420/head
Vladimír Vondruš 6 years ago
parent
commit
d955908a38
  1. 2
      doc/changelog.dox
  2. 8
      doc/snippets/MagnumGL.cpp
  3. 43
      src/Magnum/GL/Buffer.h
  4. 32
      src/Magnum/GL/Test/BufferGLTest.cpp

2
doc/changelog.dox

@ -69,6 +69,8 @@ See also:
fixes crashes on Apple macOS when attempting to modify a @ref GL::Buffer fixes crashes on Apple macOS when attempting to modify a @ref GL::Buffer
when a @ref GL::BufferTexture is bound. See @ref opengl-workarounds for when a @ref GL::BufferTexture is bound. See @ref opengl-workarounds for
more information. more information.
- New @ref GL::Buffer::Buffer(Containers::ArrayView<const void>, BufferUsage)
constructor for directly creating buffers filled with data.
@subsubsection changelog-latest-new-math Math library @subsubsection changelog-latest-new-math Math library

8
doc/snippets/MagnumGL.cpp

@ -425,7 +425,9 @@ GL::BufferImage2D image = framebuffer.read(framebuffer.viewport(),
GL::Buffer buffer; GL::Buffer buffer;
/* [Buffer-setdata] */ /* [Buffer-setdata] */
Containers::ArrayView<Vector3> data; Containers::ArrayView<Vector3> data;
buffer.setData(data, GL::BufferUsage::StaticDraw); buffer.setData(data);
GL::Buffer buffer2{data}; // or construct & fill in a single step
/* [Buffer-setdata] */ /* [Buffer-setdata] */
} }
@ -433,11 +435,11 @@ buffer.setData(data, GL::BufferUsage::StaticDraw);
GL::Buffer buffer; GL::Buffer buffer;
/* [Buffer-setdata-stl] */ /* [Buffer-setdata-stl] */
std::vector<Vector3> data; std::vector<Vector3> data;
buffer.setData(data, GL::BufferUsage::StaticDraw); buffer.setData(data);
/* [Buffer-setdata-stl] */ /* [Buffer-setdata-stl] */
/* [Buffer-setdata-allocate] */ /* [Buffer-setdata-allocate] */
buffer.setData({nullptr, 200*sizeof(Vector3)}, GL::BufferUsage::StaticDraw); buffer.setData({nullptr, 200*sizeof(Vector3)});
/* [Buffer-setdata-allocate] */ /* [Buffer-setdata-allocate] */
} }

43
src/Magnum/GL/Buffer.h

@ -145,9 +145,10 @@ data updates.
@section GL-Buffer-data-updating Data updating @section GL-Buffer-data-updating Data updating
Default way to set or update buffer data with @ref setData() or @ref setSubData() Default way to set or update buffer data with @ref setData(), @ref setSubData()
is to use @ref Corrade::Containers::ArrayView. See its documentation for or the shorthand @ref Buffer() constructor is to use
more information about automatic conversions etc. @ref Corrade::Containers::ArrayView. See its documentation for more information
about automatic conversions etc.
@snippet MagnumGL.cpp Buffer-setdata @snippet MagnumGL.cpp Buffer-setdata
@ -784,6 +785,42 @@ class MAGNUM_GL_EXPORT Buffer: public AbstractObject {
*/ */
explicit Buffer(NoCreateT) noexcept; explicit Buffer(NoCreateT) noexcept;
/**
* @brief Construct and directly fill with data
* @param targetHint Target hint, see @ref setTargetHint() for more
* information
* @param data Data
* @param usage Buffer usage
* @m_since_latest
*
* Equivalent to constructing via @ref Buffer(TargetHint) and then
* calling @ref setData().
*/
explicit Buffer(TargetHint targetHint, Containers::ArrayView<const void> data, BufferUsage usage = BufferUsage::StaticDraw): Buffer{targetHint} {
setData(data, usage);
}
/**
* @overload
* @m_since_latest
*/
template<class T> explicit Buffer(TargetHint targetHint, std::initializer_list<T> data, BufferUsage usage = BufferUsage::StaticDraw): Buffer{targetHint, Containers::arrayView(data), usage} {}
/**
* @overload
* @m_since_latest
*
* Equivalent to calling @ref Buffer(TargetHint, Containers::ArrayView<const void>, BufferUsage)
* with @ref TargetHint::Array. Unlike with
* @ref Buffer(TargetHint, std::initializer_list<T>, BufferUsage) an
* @ref std::initializer_list overload isn't provided in this case as
* it would cause a lot of undesired implicit conversions when using
* the `{}`-style construction. Use the above overload or the
* @ref Corrade::Containers::arrayView(std::initializer_list<T>) helper
* instead.
*/
explicit Buffer(Containers::ArrayView<const void> data, BufferUsage usage = BufferUsage::StaticDraw): Buffer{TargetHint::Array, data, usage} {}
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;

32
src/Magnum/GL/Test/BufferGLTest.cpp

@ -41,6 +41,7 @@ struct BufferGLTest: OpenGLTester {
explicit BufferGLTest(); explicit BufferGLTest();
void construct(); void construct();
void constructFromData();
void constructMove(); void constructMove();
void wrap(); void wrap();
@ -67,6 +68,7 @@ struct BufferGLTest: OpenGLTester {
BufferGLTest::BufferGLTest() { BufferGLTest::BufferGLTest() {
addTests({&BufferGLTest::construct, addTests({&BufferGLTest::construct,
&BufferGLTest::constructFromData,
&BufferGLTest::constructMove, &BufferGLTest::constructMove,
&BufferGLTest::wrap, &BufferGLTest::wrap,
@ -104,6 +106,36 @@ void BufferGLTest::construct() {
MAGNUM_VERIFY_NO_GL_ERROR(); MAGNUM_VERIFY_NO_GL_ERROR();
} }
void BufferGLTest::constructFromData() {
constexpr Int data[] = {2, 7, 5, 13, 25};
Buffer a{Buffer::TargetHint::ElementArray, data};
Buffer b{Buffer::TargetHint::ElementArray, {2, 7, 5, 13, 25}};
Buffer c{data};
Buffer d{{nullptr, 5*4}}; /* This should work too for reserving memory */
MAGNUM_VERIFY_NO_GL_ERROR();
CORRADE_COMPARE(a.size(), 5*4);
CORRADE_COMPARE(b.size(), 5*4);
CORRADE_COMPARE(c.size(), 5*4);
CORRADE_COMPARE(d.size(), 5*4);
/** @todo How to verify the contents in ES? */
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE_AS(Containers::arrayCast<Int>(a.data()),
Containers::arrayView(data),
TestSuite::Compare::Container);
CORRADE_COMPARE_AS(Containers::arrayCast<Int>(b.data()),
Containers::arrayView(data),
TestSuite::Compare::Container);
CORRADE_COMPARE_AS(Containers::arrayCast<Int>(c.data()),
Containers::arrayView(data),
TestSuite::Compare::Container);
/* d's data is undefined, not testing */
#endif
}
void BufferGLTest::constructMove() { void BufferGLTest::constructMove() {
Buffer a; Buffer a;
const Int id = a.id(); const Int id = a.id();

Loading…
Cancel
Save