From 9e6170e705fd243c60757633e39aae0767d33bc6 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sat, 29 Sep 2018 22:41:52 +0100 Subject: [PATCH] Use raw copy to speed up the mesher a little --- voxel_mesher.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/voxel_mesher.cpp b/voxel_mesher.cpp index c4505ee..6f89911 100644 --- a/voxel_mesher.cpp +++ b/voxel_mesher.cpp @@ -2,6 +2,15 @@ #include "voxel_library.h" #include "cube_tables.h" #include "utility.h" +#include + +template +void raw_copy_to(PoolVector &to, const Vector &from) { + to.resize(from.size()); + typename PoolVector::Write w = to.write(); + memcpy(w.ptr(), from.ptr(), from.size() * sizeof(T)); +} + VoxelMesher::VoxelMesher() : _baked_occlusion_darkness(0.75), @@ -68,6 +77,8 @@ Ref VoxelMesher::build_mesh(Ref buffer_ref, unsigned int } Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector3i min, Vector3i max) { + uint64_t time_before = OS::get_singleton()->get_ticks_usec(); + ERR_FAIL_COND_V(_library.is_null(), Array()); ERR_FAIL_COND_V(channel >= VoxelBuffer::MAX_CHANNELS, Array()); @@ -102,6 +113,9 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector int index_offset = 0; + uint64_t time_prep = OS::get_singleton()->get_ticks_usec() - time_before; + time_before = OS::get_singleton()->get_ticks_usec(); + // Iterate 3D padded data to extract voxel faces. // This is the most intensive job in this class, so all required data should be as fit as possible. for (unsigned int z = min.z; z < max.z; ++z) { @@ -260,6 +274,9 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector } } + uint64_t time_meshing = OS::get_singleton()->get_ticks_usec() - time_before; + time_before = OS::get_singleton()->get_ticks_usec(); + // Commit mesh // print_line(String("Made mesh v: ") + String::num(_arrays[0].positions.size()) @@ -282,11 +299,11 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector PoolVector colors; PoolVector indices; - copy_to(positions, arrays.positions); - copy_to(uvs, arrays.uvs); - copy_to(normals, arrays.normals); - copy_to(colors, arrays.colors); - copy_to(indices, arrays.indices); + raw_copy_to(positions, arrays.positions); + raw_copy_to(uvs, arrays.uvs); + raw_copy_to(normals, arrays.normals); + raw_copy_to(colors, arrays.colors); + raw_copy_to(indices, arrays.indices); mesh_arrays[Mesh::ARRAY_VERTEX] = positions; mesh_arrays[Mesh::ARRAY_TEX_UV] = uvs; @@ -299,6 +316,10 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector } } + uint64_t time_commit = OS::get_singleton()->get_ticks_usec() - time_before; + + //print_line(String("P: {0}, M: {1}, C: {2}").format(varray(time_prep, time_meshing, time_commit))); + return surfaces; }