From b734119d05307cbd64789bbb8113dfe0797db7ab Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 21 May 2024 23:49:45 +0200 Subject: [PATCH] Move data directly rather than copying --- irr/include/ISkinnedMesh.h | 3 +++ irr/include/SSkinMeshBuffer.h | 32 +++++++++----------------------- irr/include/irrArray.h | 3 +++ irr/src/CGLTFMeshFileLoader.cpp | 7 +++---- irr/src/CSkinnedMesh.cpp | 6 ++++++ irr/src/CSkinnedMesh.h | 3 +++ 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/irr/include/ISkinnedMesh.h b/irr/include/ISkinnedMesh.h index 9cc7469cb..96f5f8815 100644 --- a/irr/include/ISkinnedMesh.h +++ b/irr/include/ISkinnedMesh.h @@ -199,6 +199,9 @@ class ISkinnedMesh : public IAnimatedMesh //! Adds a new meshbuffer to the mesh, access it as last one virtual SSkinMeshBuffer *addMeshBuffer() = 0; + //! Adds a new meshbuffer to the mesh, access it as last one + virtual void addMeshBuffer(SSkinMeshBuffer &&meshbuf) = 0; + //! Adds a new joint to the mesh, access it as last one virtual SJoint *addJoint(SJoint *parent = 0) = 0; diff --git a/irr/include/SSkinMeshBuffer.h b/irr/include/SSkinMeshBuffer.h index d122583e2..d91692eb3 100644 --- a/irr/include/SSkinMeshBuffer.h +++ b/irr/include/SSkinMeshBuffer.h @@ -6,6 +6,7 @@ #include "IMeshBuffer.h" #include "S3DVertex.h" +#include "irrArray.h" #include @@ -24,10 +25,14 @@ struct SSkinMeshBuffer : public IMeshBuffer MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), BoundingBoxNeedsRecalculated(true) + {} + + //! Constructor for standard vertices + SSkinMeshBuffer(core::array &&vertices, core::array &&indices) : + SSkinMeshBuffer() { -#ifdef _DEBUG - setDebugName("SSkinMeshBuffer"); -#endif + Vertices_Standard = vertices; + Indices = indices; } //! Get Material of this buffer. @@ -303,26 +308,7 @@ struct SSkinMeshBuffer : public IMeshBuffer } //! append the vertices and indices to the current buffer - void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) override { - if (vertices == getVertices()) - throw std::logic_error("can't append own vertices"); - - if (VertexType != video::EVT_STANDARD) - throw std::logic_error("invalid vertex type"); - - const u32 prevVertexCount = getVertexCount(); - - Vertices_Standard.reallocate(prevVertexCount + numVertices); - for (u32 i=0; i < numVertices; ++i) { - Vertices_Standard.push_back(static_cast(vertices)[i]); - BoundingBox.addInternalPoint(static_cast(vertices)[i].Pos); - } - - Indices.reallocate(getIndexCount() + numIndices); - for (u32 i=0; i < numIndices; ++i) { - Indices.push_back(indices[i] + prevVertexCount); - } - } + void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) override {} //! get the current hardware mapping hint for vertex buffers E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override diff --git a/irr/include/irrArray.h b/irr/include/irrArray.h index b6f573a79..8299b958b 100644 --- a/irr/include/irrArray.h +++ b/irr/include/irrArray.h @@ -45,6 +45,9 @@ class array { } + //! Move constructor + array(std::vector &&data) : m_data(data), is_sorted(false) {} + //! Reallocates the array, make it bigger or smaller. /** \param new_size New size of array. \param canShrink Specifies whether the array is reallocated even if diff --git a/irr/src/CGLTFMeshFileLoader.cpp b/irr/src/CGLTFMeshFileLoader.cpp index f96098c71..6fcf599ab 100644 --- a/irr/src/CGLTFMeshFileLoader.cpp +++ b/irr/src/CGLTFMeshFileLoader.cpp @@ -429,9 +429,8 @@ void CGLTFMeshFileLoader::MeshExtractor::loadMesh( indices = generateIndices(vertices->size()); } - auto *meshbuf = m_irr_model->addMeshBuffer(); - meshbuf->append(vertices->data(), vertices->size(), - indices.data(), indices.size()); + m_irr_model->addMeshBuffer( + SSkinMeshBuffer(std::move(*vertices), std::move(indices))); } } @@ -686,7 +685,7 @@ std::optional CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* } try { return tiniergltf::GlTF(json); - } catch (const std::runtime_error &e) { + } catch (const std::runtime_error &e) { return std::nullopt; } catch (const std::out_of_range &e) { return std::nullopt; diff --git a/irr/src/CSkinnedMesh.cpp b/irr/src/CSkinnedMesh.cpp index eb7317309..45f29c185 100644 --- a/irr/src/CSkinnedMesh.cpp +++ b/irr/src/CSkinnedMesh.cpp @@ -6,6 +6,7 @@ #include #include "CBoneSceneNode.h" #include "IAnimatedMeshSceneNode.h" +#include "SSkinMeshBuffer.h" #include "os.h" namespace @@ -1061,6 +1062,11 @@ scene::SSkinMeshBuffer *CSkinnedMesh::addMeshBuffer() return buffer; } +void CSkinnedMesh::addMeshBuffer(SSkinMeshBuffer &&meshbuf) +{ + LocalBuffers.push_back(new SSkinMeshBuffer(std::move(meshbuf))); +} + CSkinnedMesh::SJoint *CSkinnedMesh::addJoint(SJoint *parent) { SJoint *joint = new SJoint; diff --git a/irr/src/CSkinnedMesh.h b/irr/src/CSkinnedMesh.h index b0228c93b..9ce666c46 100644 --- a/irr/src/CSkinnedMesh.h +++ b/irr/src/CSkinnedMesh.h @@ -129,6 +129,9 @@ class CSkinnedMesh : public ISkinnedMesh //! Adds a new meshbuffer to the mesh, access it as last one SSkinMeshBuffer *addMeshBuffer() override; + //! Adds a new meshbuffer to the mesh, access it as last one + void addMeshBuffer(SSkinMeshBuffer &&meshbuf) override; + //! Adds a new joint to the mesh, access it as last one SJoint *addJoint(SJoint *parent = 0) override;