From b77651cf93e86768988e67f939f22d47726f7c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 21 Mar 2019 21:49:15 +0100 Subject: [PATCH] GL: remove Buffer::set[Sub]Data() taking std::array / std::vector. Instead make use of the ArrayView STL compatibility. To avoid breaking almost all existing code the Corrade/Containers/ArrayViewStl.h header is included implicitly when MAGNUM_BUILD_DEPRECATED is defined, but this will get removed in some future release to speed up the compilation. --- doc/changelog.dox | 9 +++++ doc/snippets/MagnumGL.cpp | 1 + doc/snippets/MagnumShaders.cpp | 1 + src/Magnum/GL/Buffer.h | 33 ++++++------------ src/Magnum/GL/Implementation/BufferState.h | 2 ++ src/Magnum/GL/Test/BufferGLTest.cpp | 40 +++++++++++----------- src/Magnum/GL/Test/MeshGLTest.cpp | 12 +++---- src/Magnum/Text/Renderer.cpp | 1 + 8 files changed, 51 insertions(+), 48 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index e77eafede..3a6c34df1 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -106,6 +106,9 @@ See also: - Detection of SwiftShader and ARM Mali drivers with @ref GL::Context::DetectedDriver::SwiftShader and @ref GL::Context::DetectedDriver::ArmMali +- It's now possible to pass @ref std::initializer_list to + @ref GL::Buffer::setData() / @ref GL::Buffer::setSubData() for a succint + upload of basic types @subsubsection changelog-latest-new-math Math library @@ -329,6 +332,12 @@ See also: deprecated in favor of @ref Text::AbstractFont::openData(Containers::ArrayView, Float) and @ref Text::AbstractFont::setFileCallback() +- @ref GL::Buffer::setData() and @ref GL::Buffer::setSubData() no longer has + overloads taking @ref std::vector / @ref std::array, but instead relies on + @ref Corrade/Containers/ArrayViewStl.h to provide conversion from these to + @ref Corrade::Containers::ArrayView. This header is included automatically + when @ref MAGNUM_BUILD_DEPRECATED is enabled, include it explicitly to stay + forward compatible. @subsection changelog-latest-compatibility Potential compatibility breakages, removed APIs diff --git a/doc/snippets/MagnumGL.cpp b/doc/snippets/MagnumGL.cpp index 2e874df00..618fe2f2e 100644 --- a/doc/snippets/MagnumGL.cpp +++ b/doc/snippets/MagnumGL.cpp @@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE. */ +#include #include #include "Magnum/Image.h" diff --git a/doc/snippets/MagnumShaders.cpp b/doc/snippets/MagnumShaders.cpp index e6f6a1986..4ebdcd8e1 100644 --- a/doc/snippets/MagnumShaders.cpp +++ b/doc/snippets/MagnumShaders.cpp @@ -25,6 +25,7 @@ #include #include +#include #include "Magnum/GL/Buffer.h" #include "Magnum/GL/DefaultFramebuffer.h" diff --git a/src/Magnum/GL/Buffer.h b/src/Magnum/GL/Buffer.h index 789b9c7ba..a543baaf8 100644 --- a/src/Magnum/GL/Buffer.h +++ b/src/Magnum/GL/Buffer.h @@ -30,8 +30,6 @@ */ #include -#include -#include #include #include #include @@ -41,6 +39,11 @@ #include "Magnum/GL/AbstractObject.h" #include "Magnum/GL/GL.h" +#ifdef MAGNUM_BUILD_DEPRECATED +/** @todo remove once people get used to including this where needed */ +#include +#endif + namespace Magnum { namespace GL { /** @@ -148,8 +151,8 @@ more information about automatic conversions etc. @snippet MagnumGL.cpp Buffer-setdata -There is also overload for array-like containers from STL, such as -@ref std::vector or @link std::array @endlink: +If you @cpp #include @ce @ref Corrade/Containers/ArrayViewStl.h, you can also +pass directly STL types such as @ref std::vector or @link std::array @endlink: @snippet MagnumGL.cpp Buffer-setdata-stl @@ -991,15 +994,8 @@ class MAGNUM_GL_EXPORT Buffer: public AbstractObject { Buffer& setData(Containers::ArrayView data, BufferUsage usage = BufferUsage::StaticDraw); /** @overload */ - template Buffer& setData(const std::vector& data, BufferUsage usage = BufferUsage::StaticDraw) { - setData({data.data(), data.size()}, usage); - return *this; - } - - /** @overload */ - template Buffer& setData(const std::array& data, BufferUsage usage = BufferUsage::StaticDraw) { - setData({data.data(), data.size()}, usage); - return *this; + template Buffer& setData(std::initializer_list data, BufferUsage usage = BufferUsage::StaticDraw) { + return setData({data.begin(), data.size()}, usage); } /** @@ -1017,15 +1013,8 @@ class MAGNUM_GL_EXPORT Buffer: public AbstractObject { Buffer& setSubData(GLintptr offset, Containers::ArrayView data); /** @overload */ - template Buffer& setSubData(GLintptr offset, const std::vector& data) { - setSubData(offset, {data.data(), data.size()}); - return *this; - } - - /** @overload */ - template Buffer& setSubData(GLintptr offset, const std::array& data) { - setSubData(offset, {data.data(), data.size()}); - return *this; + template Buffer& setSubData(GLintptr offset, std::initializer_list data) { + return setSubData(offset, {data.begin(), data.size()}); } /** diff --git a/src/Magnum/GL/Implementation/BufferState.h b/src/Magnum/GL/Implementation/BufferState.h index 81ebf01a4..700f47257 100644 --- a/src/Magnum/GL/Implementation/BufferState.h +++ b/src/Magnum/GL/Implementation/BufferState.h @@ -25,6 +25,8 @@ DEALINGS IN THE SOFTWARE. */ +#include + #include "Magnum/GL/Buffer.h" namespace Magnum { namespace GL { namespace Implementation { diff --git a/src/Magnum/GL/Test/BufferGLTest.cpp b/src/Magnum/GL/Test/BufferGLTest.cpp index fcee04b2b..1bf02aa0f 100644 --- a/src/Magnum/GL/Test/BufferGLTest.cpp +++ b/src/Magnum/GL/Test/BufferGLTest.cpp @@ -214,54 +214,54 @@ void BufferGLTest::data() { /* Plain array */ constexpr Int data[] = {2, 7, 5, 13, 25}; - buffer.setData({data, 5}, BufferUsage::StaticDraw); + buffer.setData(data, BufferUsage::StaticDraw); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(buffer.size(), 5*4); - /* STL vector */ - std::vector data2{2, 7, 5, 13, 25}; - buffer.setData(data2, BufferUsage::StaticDraw); + /** @todo How to verify the contents in ES? */ + #ifndef MAGNUM_TARGET_GLES MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE(buffer.size(), 5*4); + CORRADE_COMPARE_AS(Containers::arrayCast(buffer.data()), + Containers::arrayView(data), + TestSuite::Compare::Container); + #endif - /* STL array */ - std::array data3{{2, 7, 5, 13, 25}}; - buffer.setData(data3, BufferUsage::StaticDraw); + /* STL initializer list */ + buffer.setData({2, 7, 5, 13, 25}, BufferUsage::StaticDraw); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(buffer.size(), 5*4); /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - auto contents = buffer.data(); MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE_AS(Containers::arrayCast(contents), + CORRADE_COMPARE_AS(Containers::arrayCast(buffer.data()), Containers::arrayView(data), TestSuite::Compare::Container); #endif /* Plain array */ constexpr Int subData[] = {125, 3, 15}; - buffer.setSubData(4, {subData, 3}); + buffer.setSubData(4, subData); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(buffer.size(), 5*4); - /* STL vector */ - std::vector subData2{125, 3, 15}; - buffer.setSubData(4, subData2); + /** @todo How to verify the contents in ES? */ + #ifndef MAGNUM_TARGET_GLES MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE(buffer.size(), 5*4); + CORRADE_COMPARE_AS(Containers::arrayCast(buffer.subData(4, 3*4)), + Containers::arrayView(subData), + TestSuite::Compare::Container); + #endif - /* STL array */ - std::array subData3{{125, 3, 15}}; - buffer.setSubData(4, subData3); + /* STL initializer list */ + buffer.setSubData(4, {125, 3, 15}); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(buffer.size(), 5*4); /** @todo How to verify the contents in ES? */ #ifndef MAGNUM_TARGET_GLES - auto subContents = buffer.subData(4, 3*4); MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE_AS(Containers::arrayCast(subContents), + CORRADE_COMPARE_AS(Containers::arrayCast(buffer.subData(4, 3*4)), Containers::arrayView(subData), TestSuite::Compare::Container); #endif diff --git a/src/Magnum/GL/Test/MeshGLTest.cpp b/src/Magnum/GL/Test/MeshGLTest.cpp index de3c8f596..af5668e76 100644 --- a/src/Magnum/GL/Test/MeshGLTest.cpp +++ b/src/Magnum/GL/Test/MeshGLTest.cpp @@ -2236,7 +2236,7 @@ void MeshGLTest::unbindVAOWhenSettingIndexBufferData() { buffer.setData(data, BufferUsage::StaticDraw); Buffer indices{Buffer::TargetHint::ElementArray}; - indices.setData(std::vector{5, 0}, BufferUsage::StaticDraw); + indices.setData(std::initializer_list{5, 0}, BufferUsage::StaticDraw); Mesh mesh; mesh.addVertexBuffer(buffer, 4, Attribute{}) @@ -2244,7 +2244,7 @@ void MeshGLTest::unbindVAOWhenSettingIndexBufferData() { /* This buffer should have no effect on the mesh above */ Buffer otherIndices{Buffer::TargetHint::ElementArray}; - otherIndices.setData(std::vector{100, 1}, BufferUsage::StaticDraw); + otherIndices.setData(std::initializer_list{100, 1}, BufferUsage::StaticDraw); MAGNUM_VERIFY_NO_GL_ERROR(); @@ -2304,7 +2304,7 @@ void MeshGLTest::unbindIndexBufferWhenBindingVao() { nonindexed.addVertexBuffer(vertices, 0, Attribute{}); /* Fill index buffer for the indexed mesh */ - indices.setData(std::vector{5, 1}, BufferUsage::StaticDraw); + indices.setData(std::initializer_list{5, 1}, BufferUsage::StaticDraw); MAGNUM_VERIFY_NO_GL_ERROR(); @@ -2359,7 +2359,7 @@ void MeshGLTest::resetIndexBufferBindingWhenBindingVao() { /* Create an index buffer and fill it (the VAO is bound now, so it'll get unbound to avoid messing with its state). */ Buffer indices{Buffer::TargetHint::ElementArray}; - indices.setData(std::vector{5, 1}); + indices.setData(std::initializer_list{5, 1}); /* Add the index buffer. The VAO is unbound, so it gets bound. That resets the element array buffer binding and then the buffer gets bound to the @@ -2411,7 +2411,7 @@ void MeshGLTest::unbindVAOBeforeEnteringExternalSection() { buffer.setData(data, BufferUsage::StaticDraw); Buffer indices{Buffer::TargetHint::ElementArray}; - indices.setData(std::vector{5, 0}, BufferUsage::StaticDraw); + indices.setData(std::initializer_list{5, 0}, BufferUsage::StaticDraw); Mesh mesh; mesh.addVertexBuffer(buffer, 4, Attribute{}) @@ -2462,7 +2462,7 @@ void MeshGLTest::bindScratchVaoWhenEnteringExternalSection() { buffer.setData(data, BufferUsage::StaticDraw); Buffer indices{Buffer::TargetHint::ElementArray}; - indices.setData(std::vector{5, 0}, BufferUsage::StaticDraw); + indices.setData(std::initializer_list{5, 0}, BufferUsage::StaticDraw); Mesh mesh; mesh.addVertexBuffer(buffer, 4, Attribute{}) diff --git a/src/Magnum/Text/Renderer.cpp b/src/Magnum/Text/Renderer.cpp index fac3de231..8e1af7398 100644 --- a/src/Magnum/Text/Renderer.cpp +++ b/src/Magnum/Text/Renderer.cpp @@ -26,6 +26,7 @@ #include "Renderer.h" #include +#include #include "Magnum/Mesh.h" #include "Magnum/GL/Context.h"