From afbe0d9e60bcb5bd7c4efa75585be417ee8ccb62 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Thu, 25 Apr 2019 18:48:31 +0200 Subject: [PATCH] Further optimizations for SLA memory usage. --- src/libslic3r/Rasterizer/Rasterizer.cpp | 29 ++++++++++++------------- src/libslic3r/Rasterizer/Rasterizer.hpp | 10 ++------- src/libslic3r/SLA/SLASupportTree.cpp | 11 ++++++++++ src/libslic3r/SLAPrint.cpp | 12 +++++++--- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/Rasterizer/Rasterizer.cpp b/src/libslic3r/Rasterizer/Rasterizer.cpp index 8eb721b43..6384a241f 100644 --- a/src/libslic3r/Rasterizer/Rasterizer.cpp +++ b/src/libslic3r/Rasterizer/Rasterizer.cpp @@ -248,20 +248,21 @@ RawBytes Raster::save(Raster::Compression comp) { assert(m_impl); - std::uint8_t *ptr = nullptr; size_t s = 0; + std::vector data; size_t s = 0; switch(comp) { case Compression::PNG: { - void *rawdata = tdefl_write_image_to_png_file_in_memory( m_impl->buffer().data(), int(resolution().width_px), int(resolution().height_px), 1, &s); if(rawdata == nullptr) break; - - ptr = static_cast(rawdata); - + auto ptr = static_cast(rawdata); + + data.reserve(s); std::copy(ptr, ptr + s, std::back_inserter(data)); + + MZ_FREE(rawdata); break; } case Compression::RAW: { @@ -270,21 +271,19 @@ RawBytes Raster::save(Raster::Compression comp) std::to_string(m_impl->resolution().height_px) + " " + "255 "; auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type); - s = sz + header.size(); - ptr = static_cast(MZ_MALLOC(s)); - + + data.reserve(s); + auto buff = reinterpret_cast(m_impl->buffer().data()); - std::copy(buff, buff+sz, ptr + header.size()); + std::copy(header.begin(), header.end(), std::back_inserter(data)); + std::copy(buff, buff+sz, std::back_inserter(data)); + + break; } } - return {ptr, s}; -} - -void RawBytes::MinzDeleter::operator()(uint8_t *rawptr) -{ - MZ_FREE(rawptr); + return {std::move(data)}; } } diff --git a/src/libslic3r/Rasterizer/Rasterizer.hpp b/src/libslic3r/Rasterizer/Rasterizer.hpp index 09b166d98..3fffe1a36 100644 --- a/src/libslic3r/Rasterizer/Rasterizer.hpp +++ b/src/libslic3r/Rasterizer/Rasterizer.hpp @@ -15,18 +15,12 @@ class ExPolygon; // Raw byte buffer paired with its size. Suitable for compressed PNG data. class RawBytes { - class MinzDeleter { - public: - void operator()(std::uint8_t *rawptr); - }; - std::vector m_buffer; - public: RawBytes() = default; - RawBytes(std::uint8_t *rawptr, size_t s): m_buffer(rawptr, rawptr + s) { MinzDeleter()(rawptr); } - + RawBytes(std::vector&& data): m_buffer(std::move(data)) {} + size_t size() const { return m_buffer.size(); } const uint8_t * data() { return m_buffer.data(); } diff --git a/src/libslic3r/SLA/SLASupportTree.cpp b/src/libslic3r/SLA/SLASupportTree.cpp index 75f9b0140..1e139ebfb 100644 --- a/src/libslic3r/SLA/SLASupportTree.cpp +++ b/src/libslic3r/SLA/SLASupportTree.cpp @@ -846,6 +846,16 @@ public: if(!meshcache_valid) merged_mesh(); return model_height; } + + // Intended to be called after the generation is fully complete + void clear_support_data() { + merged_mesh(); // in case the mesh is not generated, it should be... + m_heads.clear(); + m_pillars.clear(); + m_junctions.clear(); + m_bridges.clear(); + m_compact_bridges.clear(); + } }; @@ -2285,6 +2295,7 @@ SLASupportTree::SLASupportTree(const std::vector &points, { m_impl->ground_level = emesh.ground_level() - cfg.object_elevation_mm; generate(points, emesh, cfg, ctl); + m_impl->clear_support_data(); } SLASupportTree::SLASupportTree(const SLASupportTree &c): diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index 2bbcc700a..ecae36cc5 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -436,6 +436,12 @@ SLAPrint::ApplyStatus SLAPrint::apply(const Model &model, const DynamicPrintConf if (new_objects) update_apply_status(false); } + + if(m_objects.empty()) { + m_printer.release(); + m_printer_input.clear(); + m_print_statistics.clear(); + } #ifdef _DEBUG check_model_ids_equal(m_model, model); @@ -669,7 +675,7 @@ void SLAPrint::process() // Slicing the model object. This method is oversimplified and needs to // be compared with the fff slicing algorithm for verification auto slice_model = [this, ilhs, ilh](SLAPrintObject& po) { - TriangleMesh mesh = po.transformed_mesh(); + const TriangleMesh& mesh = po.transformed_mesh(); // We need to prepare the slice index... @@ -708,7 +714,7 @@ void SLAPrint::process() po.m_model_height_levels.emplace_back(it->slice_level()); } - mesh.require_shared_vertices(); // TriangleMeshSlicer needs this +// mesh.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer slicer(&mesh); po.m_model_slices.clear(); @@ -1534,7 +1540,7 @@ SLAPrintObject::SLAPrintObject(SLAPrint *print, ModelObject *model_object): Inherited(print, model_object), m_stepmask(slaposCount, true), m_transformed_rmesh( [this](TriangleMesh& obj){ - obj = m_model_object->raw_mesh(); obj.transform(m_trafo); + obj = m_model_object->raw_mesh(); obj.transform(m_trafo); obj.require_shared_vertices(); }) { }