Commit b86a5178 authored by Romain Pacanowski's avatar Romain Pacanowski

Efficient memory managment when loading large mesh compared to available RAM

parent 7eed991a
......@@ -45,17 +45,33 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
ply_timer.start() ;
std::cout << " Loading Ply .... " << std::endl;
std::cout << " available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
mrf::io::loadPLY(full_path_to_mesh, _vertices, _normals, _uvs, _color_per_vertex, _index_faces);
ply_timer.stop();
// Forcing vectors to reduce their memory footprint
_vertices.shrink_to_fit(); _tangents.shrink_to_fit(); _normals.shrink_to_fit();
_uvs.shrink_to_fit(); _color_per_vertex.shrink_to_fit();
std::cout << " Ply loaded in ram preparing VBO " << ply_timer << " available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
unsigned int const size_from_vectors_read = ( ( (_vertices.size()+_normals.size()+_tangents.size()+_uvs.size()+_color_per_vertex.size())*sizeof(mrf::math::Vec3f) ) + _index_faces.size()*sizeof(unsigned int) ) / 1024.0 / 1024.0;
std::cout << " Size in MB of vectors retrieved from reading PLY file " << size_from_vectors_read << " MB " << std::endl;
if( _normals.size() != _vertices.size() )
{
mrf::geom::_Mesh<mrf::math::Vec3f>::computeNormals(_vertices, _index_faces, _normals );
std::cout << __FILE__ << " " << __LINE__ << " available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
}
mrf::geom::_Mesh<mrf::math::Vec3f>::computeTangents(_vertices, _normals, _tangents);
std::cout << __FILE__ << " " << __LINE__ << " available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
long unsigned int mesh_memory_size_in_Bytes = ( (_vertices.size()+_normals.size()+_tangents.size())*sizeof(mrf::math::Vec3f) ) ;
......@@ -76,13 +92,21 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
mesh_memory_size_in_Bytes += _vertices.size() * sizeof(mrf::math::Vec3f);
}
// Forcing vectors to reduce their memory footprint
_vertices.shrink_to_fit(); _tangents.shrink_to_fit(); _normals.shrink_to_fit();
_uvs.shrink_to_fit(); _color_per_vertex.shrink_to_fit();
long unsigned int const mesh_memory_size_in_MB = mesh_memory_size_in_Bytes / 1024.0 / 1024.0;
unsigned long long int const available_memory_in_bytes = mrf::util::CPUMemory::availableMemory() * 1024 * 1024;
long unsigned int const nb_vertex_before_erasing = (available_memory_in_bytes / _nb_bytes_between_two_vertex) / 2 ;
std::cout << " CPU Required Memory for Mesh on GPU is " << mesh_memory_size_in_MB << " MB " << std::endl;
std::cout << " Available Memory on the system " << available_memory_in_bytes / 1024.0 / 1024.0 << std::endl;
std::cout << " Available Memory on the system " << available_memory_in_bytes / 1024.0 / 1024.0 << " MB " << std::endl;
std::cout << " Number of bytes to store one Vertex is: " << _nb_bytes_between_two_vertex << std::endl;
std::cout << " Nb vertex before erasing " << available_memory_in_bytes / _nb_bytes_between_two_vertex << std::endl;
std::cout << " Nb vertex before erasing " << nb_vertex_before_erasing << std::endl;
bool const has_color_per_vertex = _color_per_vertex.size() == _vertices.size();
......@@ -93,11 +117,10 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
auto it_end = _vertices.end();
unsigned int cpt = 0;
for( auto it_vert= _vertices.begin(), it_norm=_normals.begin(), it_tang=_tangents.begin(); it_vert != _vertices.end() ; ++it_vert, ++it_norm, ++it_tang )
long unsigned int cpt = 0;
for( auto it_vert= _vertices.begin(), it_norm=_normals.begin(), it_tang=_tangents.begin(); it_vert != it_end ; ++it_vert, ++it_norm, ++it_tang )
{
//std::cout << " Vertex " << cpt << std::endl;
// _vboCPU.push_back( _vertices[i] );
// _vboCPU.push_back( _normals[i] );
// _vboCPU.push_back(_tangents[i]);
......@@ -106,13 +129,6 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
_vboCPU.push_back( *it_norm );
_vboCPU.push_back( *it_tang );
// it_vert = _vertices.erase(it_vert);
// it_end = _vertices.end();
// it_norm = _normals.erase(it_norm);
// it_tang = _tangents.erase(it_tang);
if (has_uv_per_vertex)
{
_vboCPU.push_back( *it_uvs);
......@@ -125,7 +141,53 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
++it_cpv;
}
cpt++;
if( cpt >= nb_vertex_before_erasing)
{
std::cout << " Vertex " << cpt << std::endl;
std::cout << " BEFORE available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
it_vert = _vertices.erase(it_vert-cpt, it_vert);
it_norm = _normals.erase(it_norm-cpt, it_norm );
it_tang = _tangents.erase(it_tang-cpt, it_tang );
_vertices.shrink_to_fit();
it_vert = _vertices.begin();
it_end = _vertices.end();
_normals.shrink_to_fit();
it_norm = _normals.begin();
_tangents.shrink_to_fit();
it_tang = _tangents.begin();
if (has_uv_per_vertex)
{
it_uvs = _uvs.erase( it_uvs-cpt, it_uvs );
_uvs.shrink_to_fit();
it_uvs = _uvs.begin();
}
if (has_color_per_vertex)
{
it_cpv = _color_per_vertex.erase( it_cpv-cpt, it_cpv);
_color_per_vertex.shrink_to_fit();
it_cpv = _color_per_vertex.begin();
}
std::cout << " after releasing memory available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
cpt = 0;
}
else
{
if( cpt == nb_vertex_before_erasing-1)
{
std::cout << " Currently available RAM Memory in MB is " << mrf::util::CPUMemory::availableMemory() << std::endl;
}
cpt++;
}
}// enf of for loop VBO construction
......@@ -145,6 +207,8 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
_uvs.clear();
_tangents.clear();
_color_per_vertex.clear();
//vector<T>().swap(x); // clear x reallocating
}
Viewer::~Viewer()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment