Skip to content

Commit 61c7b7f

Browse files
authored
Merge pull request godotengine#67853 from Zylann/fix_lods_with_doubles
Fix usages of mesh simplification functions in float=64 builds
2 parents b29bb11 + 2aefdcc commit 61c7b7f

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

core/io/marshalls.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1813,3 +1813,24 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
18131813

18141814
return OK;
18151815
}
1816+
1817+
Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count) {
1818+
// We always allocate a new array, and we don't memcpy.
1819+
// We also don't consider returning a pointer to the passed vectors when sizeof(real_t) == 4.
1820+
// One reason is that we could decide to put a 4th component in Vector3 for SIMD/mobile performance,
1821+
// which would cause trouble with these optimizations.
1822+
Vector<float> floats;
1823+
if (count == 0) {
1824+
return floats;
1825+
}
1826+
floats.resize(count * 3);
1827+
float *floats_w = floats.ptrw();
1828+
for (size_t i = 0; i < count; ++i) {
1829+
const Vector3 v = vecs[i];
1830+
floats_w[0] = v.x;
1831+
floats_w[1] = v.y;
1832+
floats_w[2] = v.z;
1833+
floats_w += 3;
1834+
}
1835+
return floats;
1836+
}

core/io/marshalls.h

+2
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,6 @@ class EncodedObjectAsID : public RefCounted {
215215
Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0);
216216
Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
217217

218+
Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count);
219+
218220
#endif // MARSHALLS_H

scene/3d/occluder_instance_3d.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/config/project_settings.h"
3434
#include "core/core_string_names.h"
35+
#include "core/io/marshalls.h"
3536
#include "core/math/geometry_2d.h"
3637
#include "core/math/triangulate.h"
3738
#include "scene/3d/importer_mesh_instance_3d.h"
@@ -533,11 +534,20 @@ void OccluderInstance3D::_bake_surface(const Transform3D &p_transform, Array p_s
533534
}
534535

535536
if (!Math::is_zero_approx(p_simplification_dist) && SurfaceTool::simplify_func) {
536-
float error_scale = SurfaceTool::simplify_scale_func((float *)vertices.ptr(), vertices.size(), sizeof(Vector3));
537+
Vector<float> vertices_f32 = vector3_to_float32_array(vertices.ptr(), vertices.size());
538+
539+
float error_scale = SurfaceTool::simplify_scale_func(vertices_f32.ptr(), vertices.size(), sizeof(float) * 3);
537540
float target_error = p_simplification_dist / error_scale;
538541
float error = -1.0f;
539542
int target_index_count = MIN(indices.size(), 36);
540-
uint32_t index_count = SurfaceTool::simplify_func((unsigned int *)indices.ptrw(), (unsigned int *)indices.ptr(), indices.size(), (float *)vertices.ptr(), vertices.size(), sizeof(Vector3), target_index_count, target_error, &error);
543+
544+
uint32_t index_count = SurfaceTool::simplify_func(
545+
(unsigned int *)indices.ptrw(),
546+
(unsigned int *)indices.ptr(),
547+
indices.size(),
548+
vertices_f32.ptr(), vertices.size(), sizeof(float) * 3,
549+
target_index_count, target_error, &error);
550+
541551
indices.resize(index_count);
542552
}
543553

scene/resources/importer_mesh.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "importer_mesh.h"
3232

33+
#include "core/io/marshalls.h"
3334
#include "core/math/random_pcg.h"
3435
#include "core/math/static_raycaster.h"
3536
#include "scene/resources/surface_tool.h"
@@ -424,9 +425,8 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
424425
normal_weights[j] = 2.0; // Give some weight to normal preservation, may be worth exposing as an import setting
425426
}
426427

427-
const float max_mesh_error = FLT_MAX; // We don't want to limit by error, just by index target
428-
float scale = SurfaceTool::simplify_scale_func((const float *)merged_vertices_ptr, merged_vertex_count, sizeof(Vector3));
429-
float mesh_error = 0.0f;
428+
Vector<float> merged_vertices_f32 = vector3_to_float32_array(merged_vertices_ptr, merged_vertex_count);
429+
float scale = SurfaceTool::simplify_scale_func(merged_vertices_f32.ptr(), merged_vertex_count, sizeof(float) * 3);
430430

431431
unsigned int index_target = 12; // Start with the smallest target, 4 triangles
432432
unsigned int last_index_count = 0;
@@ -446,11 +446,25 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
446446
raycaster->commit();
447447
}
448448

449+
const float max_mesh_error = FLT_MAX; // We don't want to limit by error, just by index target
450+
float mesh_error = 0.0f;
451+
449452
while (index_target < index_count) {
450453
PackedInt32Array new_indices;
451454
new_indices.resize(index_count);
452455

453-
size_t new_index_count = SurfaceTool::simplify_with_attrib_func((unsigned int *)new_indices.ptrw(), (const uint32_t *)merged_indices_ptr, index_count, (const float *)merged_vertices_ptr, merged_vertex_count, sizeof(Vector3), index_target, max_mesh_error, &mesh_error, (float *)merged_normals.ptr(), normal_weights.ptr(), 3);
456+
Vector<float> merged_normals_f32 = vector3_to_float32_array(merged_normals.ptr(), merged_normals.size());
457+
458+
size_t new_index_count = SurfaceTool::simplify_with_attrib_func(
459+
(unsigned int *)new_indices.ptrw(),
460+
(const uint32_t *)merged_indices_ptr, index_count,
461+
merged_vertices_f32.ptr(), merged_vertex_count,
462+
sizeof(float) * 3, // Vertex stride
463+
index_target,
464+
max_mesh_error,
465+
&mesh_error,
466+
merged_normals_f32.ptr(),
467+
normal_weights.ptr(), 3);
454468

455469
if (new_index_count < last_index_count * 1.5f) {
456470
index_target = index_target * 1.5f;

scene/resources/surface_tool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void SurfaceTool::strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32A
4646

4747
Vector<uint32_t> remap;
4848
remap.resize(r_vertices.size());
49-
uint32_t new_vertex_count = generate_remap_func(remap.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), (float *)r_vertices.ptr(), r_vertices.size(), sizeof(Vector3));
49+
uint32_t new_vertex_count = generate_remap_func(remap.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), r_vertices.ptr(), r_vertices.size(), sizeof(Vector3));
5050
remap_vertex_func(r_vertices.ptrw(), r_vertices.ptr(), r_vertices.size(), sizeof(Vector3), remap.ptr());
5151
r_vertices.resize(new_vertex_count);
5252
remap_index_func((unsigned int *)r_indices.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), remap.ptr());

0 commit comments

Comments
 (0)