From 66d4a913bd31101f09927044cfc63fd755dedfc0 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 28 Apr 2019 21:02:42 +0100 Subject: [PATCH] Use std::vector for speed --- meshers/blocky/voxel_mesher_blocky.cpp | 10 +++++----- meshers/blocky/voxel_mesher_blocky.h | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/meshers/blocky/voxel_mesher_blocky.cpp b/meshers/blocky/voxel_mesher_blocky.cpp index 964f69f..5cf98bc 100644 --- a/meshers/blocky/voxel_mesher_blocky.cpp +++ b/meshers/blocky/voxel_mesher_blocky.cpp @@ -231,7 +231,7 @@ void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelBuffer &bu { int append_index = arrays.positions.size(); arrays.positions.resize(arrays.positions.size() + vertex_count); - Vector3 *w = arrays.positions.ptrw() + append_index; + Vector3 *w = arrays.positions.data() + append_index; for (unsigned int i = 0; i < vertex_count; ++i) { w[i] = rv[i] + pos; } @@ -240,13 +240,13 @@ void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelBuffer &bu { int append_index = arrays.uvs.size(); arrays.uvs.resize(arrays.uvs.size() + vertex_count); - memcpy(arrays.uvs.ptrw() + append_index, rt.ptr(), vertex_count * sizeof(Vector2)); + memcpy(arrays.uvs.data() + append_index, rt.ptr(), vertex_count * sizeof(Vector2)); } { int append_index = arrays.normals.size(); arrays.normals.resize(arrays.normals.size() + vertex_count); - Vector3 *w = arrays.normals.ptrw() + append_index; + Vector3 *w = arrays.normals.data() + append_index; for (unsigned int i = 0; i < vertex_count; ++i) { w[i] = Cube::g_side_normals[side].to_vec3(); } @@ -257,7 +257,7 @@ void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelBuffer &bu int append_index = arrays.colors.size(); arrays.colors.resize(arrays.colors.size() + vertex_count); - Color *w = arrays.colors.ptrw() + append_index; + Color *w = arrays.colors.data() + append_index; for (unsigned int i = 0; i < vertex_count; ++i) { Vector3 v = rv[i]; @@ -290,7 +290,7 @@ void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelBuffer &bu { int i = arrays.indices.size(); arrays.indices.resize(arrays.indices.size() + index_count); - int *w = arrays.indices.ptrw(); + int *w = arrays.indices.data(); for (unsigned int j = 0; j < index_count; ++j) { w[i++] = index_offset + ri[j]; } diff --git a/meshers/blocky/voxel_mesher_blocky.h b/meshers/blocky/voxel_mesher_blocky.h index 3076581..e502828 100644 --- a/meshers/blocky/voxel_mesher_blocky.h +++ b/meshers/blocky/voxel_mesher_blocky.h @@ -7,6 +7,7 @@ #include "../voxel_mesher.h" #include #include +#include class VoxelMesherBlocky : public VoxelMesher { GDCLASS(VoxelMesherBlocky, VoxelMesher) @@ -33,13 +34,14 @@ protected: static void _bind_methods(); private: - // TODO Replace those with std::vector, it's faster + // Using std::vector because they make this mesher twice as fast than Godot Vectors. + // See why: https://github.com/godotengine/godot/issues/24731 struct Arrays { - Vector positions; - Vector normals; - Vector uvs; - Vector colors; - Vector indices; + std::vector positions; + std::vector normals; + std::vector uvs; + std::vector colors; + std::vector indices; }; Ref _library;