diff --git a/src/MeshTools/Interleave.h b/src/MeshTools/Interleave.h new file mode 100644 index 000000000..81b50e790 --- /dev/null +++ b/src/MeshTools/Interleave.h @@ -0,0 +1,188 @@ +#ifndef Magnum_MeshTools_Interleave_h +#define Magnum_MeshTools_Interleave_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +/** @file + * @brief Class Magnum::MeshTools::Interleave + */ + +#include +#include +#include +#include + +#include "Mesh.h" +#include "Buffer.h" + +namespace Magnum { namespace MeshTools { + +/** +@brief Vertex attribute interleaver implementation + +See interleave() for full documentation. +*/ +class Interleave { + public: + /** + * @brief Interleaved attribute array + * + * Size of the data buffer can be computed as follows: + * @code + * Result result; + * size_t size = result.attributeCount*result.stride; + * @endcode + */ + struct Result { + size_t attributeCount; /**< @brief Attribute count */ + size_t stride; /**< @brief Distance between two attributes in resulting array */ + char* data; /**< @brief Data buffer */ + }; + + /** @brief Constructor */ + inline Interleave(): result{0, 0, 0} {} + + /** + * @brief Functor + * + * See interleave(const std::vector&...) for full documentation. + */ + template Result operator()(const std::vector&... attributes) { + /* Compute buffer size and stride */ + result.attributeCount = attributeCount(attributes...); + if(result.attributeCount) { + result.stride = stride(attributes...); + + /* Create output buffer */ + result.data = new char[result.attributeCount*result.stride]; + + /* Save the data */ + write(result.data, attributes...); + } + + return result; + } + + /** + * @brief Functor + * + * See interleave(Mesh*, Buffer*, Buffer::Usage, const std::vector&...) for full documentation. + */ + template void operator()(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector&... attributes) { + if(!mesh->isInterleaved(buffer)) { + Corrade::Utility::Error() << "MeshTools::Interleave: the buffer is not interleaved, nothing done"; + assert(0); + return; + } + + operator()(attributes...); + + mesh->setVertexCount(result.attributeCount); + buffer->setData(result.attributeCount*result.stride, result.data, usage); + + delete[] result.data; + } + + /** @brief Minimal count of passed attributes */ + template inline static size_t attributeCount(const std::vector& attribute, const std::vector&... attributes) { + size_t count = attributeCount(attributes...); + if(sizeof...(attributes) != 0 && count != attribute.size()) { + Corrade::Utility::Error() << "MeshTools::Interleave: attribute arrays don't have the same length, nothing done."; + assert(0); + return 0; + } + return attribute.size(); + } + + /** @brief Distance between two attributes in resulting array */ + template inline static size_t stride(const std::vector& attribute, const std::vector&... attributes) { + return sizeof(T) + stride(attributes...); + } + + private: + template void write(char* startingOffset, const std::vector& attribute, const std::vector&... attributes) const { + /* Copy the data to the buffer */ + for(size_t i = 0; i != result.attributeCount; ++i) + memcpy(startingOffset+i*result.stride, reinterpret_cast(&attribute[i]), sizeof(T)); + + write(startingOffset+sizeof(T), attributes...); + } + + /* Terminator functions for recursive calls */ + inline static size_t attributeCount() { return 0; } + inline static size_t stride() { return 0; } + inline void write(char*) const {} + + Result result; +}; + +/** +@brief %Interleave vertex attributes +@param attributes Attribute arrays +@return Interleaved attribute array. Deleting the buffer is user's + responsibility. + +This function takes two or more attribute arrays and interleaves them, so data +for each attribute are in continuous place in memory. + +@attention Each vector should have the same size, if not, resulting array has +zero length. + +This is convenience function supplementing direct usage of Interleave class, +instead of +@code +MeshTools::Interleave()(attributes...); +@endcode +you can just write +@code +MeshTools::interleave(attributes...); +@endcode +*/ +template inline Interleave::Result interleave(const std::vector&... attributes) { + return Interleave()(attributes...); +} + +/** +@brief %Interleave vertex attributes and write them to array buffer +@param attributes Attribute arrays +@param mesh Output mesh +@param buffer Output array buffer +@param usage Array buffer usage + +The same as interleave(const std::vector&...), but this function writes +the output to given array buffer and updates vertex count in the mesh +accordingly. + +@attention The buffer must be set as interleaved (see Mesh::addBuffer()), +otherwise this function does nothing. Binding the attributes to shader is +left to user. + +This is convenience function supplementing direct usage of Interleave class, +instead of +@code +MeshTools::Interleave()(mesh, buffer, usage, attributes...); +@endcode +you can just write +@code +MeshTools::interleave(mesh, buffer, usage, attributes...); +@endcode +*/ +template inline void interleave(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector&... attributes) { + return Interleave()(mesh, buffer, usage, attributes...); +} + +}} + +#endif diff --git a/src/MeshTools/Test/CMakeLists.txt b/src/MeshTools/Test/CMakeLists.txt index c321eb021..dc498d898 100644 --- a/src/MeshTools/Test/CMakeLists.txt +++ b/src/MeshTools/Test/CMakeLists.txt @@ -1,6 +1,8 @@ corrade_add_test(CleanTest CleanTest.h CleanTest.cpp) corrade_add_test(CompressIndicesTest CompressIndicesTest.h CompressIndicesTest.cpp) target_link_libraries(CompressIndicesTest ${CORRADE_UTILITY_LIBRARY} ${MAGNUM_LIBRARY}) +corrade_add_test(InterleaveTest InterleaveTest.h InterleaveTest.cpp) +target_link_libraries(InterleaveTest ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(SubdivideTest SubdivideTest.h SubdivideTest.cpp) corrade_add_test(SubdivideCleanBenchmark SubdivideCleanBenchmark.h SubdivideCleanBenchmark.cpp) target_link_libraries(SubdivideCleanBenchmark ${MAGNUM_PRIMITIVES_LIBRARY}) diff --git a/src/MeshTools/Test/InterleaveTest.cpp b/src/MeshTools/Test/InterleaveTest.cpp new file mode 100644 index 000000000..74533d93c --- /dev/null +++ b/src/MeshTools/Test/InterleaveTest.cpp @@ -0,0 +1,77 @@ +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "InterleaveTest.h" + +#define NDEBUG + +#include +#include + +#include "Utility/Endianness.h" +#include "Utility/Debug.h" +#include "MeshTools/Interleave.h" + +QTEST_APPLESS_MAIN(Magnum::MeshTools::Test::InterleaveTest) + +using namespace std; +using namespace Corrade::Utility; + +namespace Magnum { namespace MeshTools { namespace Test { + +void InterleaveTest::attributeCount() { + stringstream ss; + Error::setOutput(&ss); + QCOMPARE((Interleave::attributeCount(vector{0, 1, 2}, + vector{0, 1, 2, 3, 4, 5})), size_t(0)); + QVERIFY(ss.str() == "MeshTools::Interleave: attribute arrays don't have the same length, nothing done.\n"); + + QCOMPARE((Interleave::attributeCount(vector{0, 1, 2}, + vector{3, 4, 5})), size_t(3)); +} + +void InterleaveTest::stride() { + QCOMPARE(Interleave::stride(vector()), size_t(1)); + QCOMPARE(Interleave::stride(vector()), size_t(4)); + QCOMPARE((Interleave::stride(vector(), vector())), size_t(5)); +} + +void InterleaveTest::write() { + Interleave::Result data = MeshTools::interleave( + vector{0, 1, 2}, + vector{3, 4, 5}, + vector{6, 7, 8}); + + QCOMPARE(data.attributeCount, size_t(3)); + QCOMPARE(data.stride, size_t(7)); + size_t size = data.attributeCount*data.stride; + if(!Endianness::isBigEndian()) { + QVERIFY((vector(data.data, data.data+size) == vector{ + 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x01, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, + 0x02, 0x05, 0x00, 0x00, 0x00, 0x08, 0x00 + })); + } else { + QVERIFY((vector(data.data, data.data+size) == vector{ + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x06, + 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, + 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08 + })); + } + + delete[] data.data; +} + +}}} diff --git a/src/MeshTools/Test/InterleaveTest.h b/src/MeshTools/Test/InterleaveTest.h new file mode 100644 index 000000000..02939f37e --- /dev/null +++ b/src/MeshTools/Test/InterleaveTest.h @@ -0,0 +1,33 @@ +#ifndef Magnum_MeshTools_Test_InterleaveTest_h +#define Magnum_MeshTools_Test_InterleaveTest_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include + +namespace Magnum { namespace MeshTools { namespace Test { + +class InterleaveTest: public QObject { + Q_OBJECT + + private slots: + void attributeCount(); + void stride(); + void write(); +}; + +}}} + +#endif