Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions source/MRMesh/MRBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#pragma once

#include "MRMeshFwd.h"
#include <cassert>
#include <memory>

namespace MR
{

/**
* \brief std::vector<T>-like container that is
* 1) resized without initialization of its elements,
* 2) much simplified: no push_back and many other methods
* \tparam T type of stored elements
* \ingroup BasicGroup
*/
template <typename T>
class Buffer
{
public:
using reference = T&;
using const_reference = const T&;
using iterator = T*;
using const_iterator = const T*;

Buffer() = default;
explicit Buffer( size_t size ) { resize( size ); }
Buffer( std::vector<T> && vec ) : data_( std::move( data_ ) ), size_( std::move( size_ ) ) { }

void clear() { data_.reset(); size_ = 0; }

[[nodiscard]] auto size() const { return size_; }

void resize( size_t newSize )
{
if ( newSize == 0 )
clear();
else if ( newSize != size_ )
{
#if __cpp_lib_smart_ptr_for_overwrite >= 202002L
data_ = std::make_unique_for_overwrite<T[]>( size_ = newSize );
#else
// The array elements are value-initialized, so it is much slower - for older compilers
#pragma message("No std::make_unique_for_overwrite is available")
data_ = std::make_unique<T[]>( size_ = newSize );
#endif
}
}

[[nodiscard]] const_reference operator[]( size_t i ) const
{
assert( i < size_ );
return data_[i];
}
[[nodiscard]] reference operator[]( size_t i )
{
assert( i < size_ );
return data_[i];
}

[[nodiscard]] auto data() { return data_.get(); }
[[nodiscard]] auto data() const { return data_.get(); }

void swap( Buffer & b ) { data_.swap( b.data_ ); std::swap( size_, b.size_ ); }

/// returns the amount of memory this object occupies on heap
[[nodiscard]] size_t heapBytes() const { return size() * sizeof(T); }

private:
std::unique_ptr<T[]> data_;
size_t size_ = 0;
};

template <typename T, typename I>
[[nodiscard]] inline auto begin( const Buffer<T> & a )
{ return a.data(); }

template <typename T, typename I>
[[nodiscard]] inline auto begin( Buffer<T> & a )
{ return a.data(); }

template <typename T, typename I>
[[nodiscard]] inline auto end( const Buffer<T> & a )
{ return a.data() + a.size(); }

template <typename T, typename I>
[[nodiscard]] inline auto end( Buffer<T> & a )
{ return a.data() + a.size(); }

} // namespace MR
3 changes: 2 additions & 1 deletion source/MRMesh/MRMesh.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="MRBooleanOperation.h" />
<ClInclude Include="MRBuffer.h" />
<ClInclude Include="MRChangeVoxelsAction.h" />
<ClInclude Include="MRChangeLabelAction.h" />
<ClInclude Include="MRChangePointCloudAction.h" />
Expand Down Expand Up @@ -576,4 +577,4 @@ copy $(ProjectDir)..\..\thirdparty\python\python310.zip $(TargetDir)python310.zi
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
5 changes: 4 additions & 1 deletion source/MRMesh/MRMesh.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,9 @@
<ClInclude Include="MRMatrix3Decompose.h">
<Filter>Source Files\Math</Filter>
</ClInclude>
<ClInclude Include="MRBuffer.h">
<Filter>Source Files\Basic</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MRId.cpp">
Expand Down Expand Up @@ -1455,4 +1458,4 @@
<ItemGroup>
<None Include="..\.editorconfig" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions source/MRMesh/MRMeshFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MRMESH_CLASS VoxelTag;

template <typename T> class Id;
template <typename T, typename I> class Vector;
template <typename T> class Buffer;

using EdgeId = Id<EdgeTag>;
using UndirectedEdgeId = Id<UndirectedEdgeTag>;
Expand Down
4 changes: 2 additions & 2 deletions source/MRViewer/MRRenderGLHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace MR
{
template<typename T>
template<typename T, template<typename, typename...> class C, typename... args>
GLint bindVertexAttribArray(
const GLuint program_shader,
const std::string& name,
GLuint bufferID,
const std::vector<T>& V,
const C<T, args...>& V,
int baseTypeElementsNumber,
bool refresh,
bool forceUse = false )
Expand Down
16 changes: 8 additions & 8 deletions source/MRViewer/MRRenderMeshObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ void RenderMeshObject::renderPicker( const BaseRenderParams& parameters, unsigne

size_t RenderMeshObject::heapBytes() const
{
return MR::heapBytes( vertPosBufferObj_ )
+ MR::heapBytes( vertNormalsBufferObj_ )
+ MR::heapBytes( vertColorsBufferObj_ )
+ MR::heapBytes( vertUVBufferObj_ )
+ MR::heapBytes( facesIndicesBufferObj_ )
+ MR::heapBytes( edgesIndicesBufferObj_ )
+ MR::heapBytes( faceSelectionTexture_ )
+ MR::heapBytes( faceNormalsTexture_ )
return vertPosBufferObj_.heapBytes()
+ vertNormalsBufferObj_.heapBytes()
+ vertColorsBufferObj_.heapBytes()
+ vertUVBufferObj_.heapBytes()
+ facesIndicesBufferObj_.heapBytes()
+ edgesIndicesBufferObj_.heapBytes()
+ faceSelectionTexture_.heapBytes()
+ faceNormalsTexture_.heapBytes()
+ MR::heapBytes( borderHighlightPoints_ )
+ MR::heapBytes( selectedEdgesPoints_ );
}
Expand Down
18 changes: 10 additions & 8 deletions source/MRViewer/MRRenderMeshObject.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "MRMesh/MRIRenderObject.h"
#include "MRMesh/MRMeshTexture.h"
#include "MRMesh/MRBuffer.h"

namespace MR
{
Expand All @@ -18,14 +20,14 @@ class RenderMeshObject : public IRenderObject
const ObjectMeshHolder* objMesh_;

// need this to use per corner rendering (this is not simple copy of mesh vertices etc.)
mutable std::vector<Vector3f> vertPosBufferObj_;
mutable std::vector<Vector3f> vertNormalsBufferObj_;
mutable std::vector<Color> vertColorsBufferObj_;
mutable std::vector<UVCoord> vertUVBufferObj_;
mutable std::vector<Vector3i> facesIndicesBufferObj_;
mutable std::vector<Vector2i> edgesIndicesBufferObj_;
mutable std::vector<unsigned> faceSelectionTexture_;
mutable std::vector<Vector4f> faceNormalsTexture_;
mutable Buffer<Vector3f> vertPosBufferObj_;
mutable Buffer<Vector3f> vertNormalsBufferObj_;
mutable Buffer<Color> vertColorsBufferObj_;
mutable Buffer<UVCoord> vertUVBufferObj_;
mutable Buffer<Vector3i> facesIndicesBufferObj_;
mutable Buffer<Vector2i> edgesIndicesBufferObj_;
mutable Buffer<unsigned> faceSelectionTexture_;
mutable Buffer<Vector4f> faceNormalsTexture_;
mutable std::vector<Vector3f> borderHighlightPoints_;
mutable std::vector<Vector3f> selectedEdgesPoints_;

Expand Down