Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meshoptimizer: Update to v0.20 (with a reduced patch) #84384

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ License: Apache-2.0

Files: ./thirdparty/meshoptimizer/
Comment: meshoptimizer
Copyright: 2016-2022, Arseny Kapoulkine
Copyright: 2016-2023, Arseny Kapoulkine
License: Expat

Files: ./thirdparty/minimp3/
Expand Down
16 changes: 8 additions & 8 deletions scene/resources/importer_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,10 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
}
}

LocalVector<float> normal_weights;
normal_weights.resize(merged_vertex_count);
for (unsigned int j = 0; j < merged_vertex_count; j++) {
normal_weights[j] = 2.0; // Give some weight to normal preservation, may be worth exposing as an import setting
}
const float normal_weights[3] = {
// Give some weight to normal preservation, may be worth exposing as an import setting
2.0f, 2.0f, 2.0f
};

Vector<float> merged_vertices_f32 = vector3_to_float32_array(merged_vertices_ptr, merged_vertex_count);
float scale = SurfaceTool::simplify_scale_func(merged_vertices_f32.ptr(), merged_vertex_count, sizeof(float) * 3);
Expand Down Expand Up @@ -460,12 +459,13 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
(const uint32_t *)merged_indices_ptr, index_count,
merged_vertices_f32.ptr(), merged_vertex_count,
sizeof(float) * 3, // Vertex stride
merged_normals_f32.ptr(),
sizeof(float) * 3, // Attribute stride
normal_weights, 3,
index_target,
max_mesh_error,
simplify_options,
&mesh_error,
merged_normals_f32.ptr(),
normal_weights.ptr(), 3);
&mesh_error);

if (new_index_count < last_index_count * 1.5f) {
index_target = index_target * 1.5f;
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/surface_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SurfaceTool : public RefCounted {
static OptimizeVertexCacheFunc optimize_vertex_cache_func;
typedef size_t (*SimplifyFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, unsigned int options, float *r_error);
static SimplifyFunc simplify_func;
typedef size_t (*SimplifyWithAttribFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_data, size_t vertex_count, size_t vertex_stride, size_t target_index_count, float target_error, unsigned int options, float *result_error, const float *attributes, const float *attribute_weights, size_t attribute_count);
typedef size_t (*SimplifyWithAttribFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_data, size_t vertex_count, size_t vertex_stride, const float *attributes, size_t attribute_stride, const float *attribute_weights, size_t attribute_count, size_t target_index_count, float target_error, unsigned int options, float *result_error);
static SimplifyWithAttribFunc simplify_with_attrib_func;
typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
static SimplifyScaleFunc simplify_scale_func;
Expand Down
8 changes: 3 additions & 5 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,16 @@ File extracted from upstream release tarball:
## meshoptimizer

- Upstream: https://github.com/zeux/meshoptimizer
- Version: git (4a287848fd664ae1c3fc8e5e008560534ceeb526, 2022)
- Version: git (c21d3be6ddf627f8ca852ba4b6db9903b0557858, 2023)
- License: MIT

Files extracted from upstream repository:

- All files in `src/`
- `LICENSE.md`

An [experimental upstream feature](https://github.com/zeux/meshoptimizer/tree/simplify-attr),
has been backported. On top of that, it was modified to report only distance
error metrics instead of a combination of distance and attribute errors. Patches
for both changes can be found in the `patches` directory.
A patch is included to modify the simplifier to report only distance error
metrics instead of a combination of distance and attribute errors.


## minimp3
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/meshoptimizer/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016-2022 Arseny Kapoulkine
Copyright (c) 2016-2023 Arseny Kapoulkine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/meshoptimizer/indexcodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace meshopt
const unsigned char kIndexHeader = 0xe0;
const unsigned char kSequenceHeader = 0xd0;

static int gEncodeIndexVersion = 0;
static int gEncodeIndexVersion = 1;

typedef unsigned int VertexFifo[16];
typedef unsigned int EdgeFifo[16][2];
Expand Down
41 changes: 34 additions & 7 deletions thirdparty/meshoptimizer/indexgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static T* hashLookup(T* table, size_t buckets, const Hash& hash, const T& key, c
}

assert(false && "Hash table is full"); // unreachable
return 0;
return NULL;
}

static void buildPositionRemap(unsigned int* remap, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, meshopt_Allocator& allocator)
Expand All @@ -178,6 +178,22 @@ static void buildPositionRemap(unsigned int* remap, const float* vertex_position

remap[index] = *entry;
}

allocator.deallocate(vertex_table);
}

template <size_t BlockSize>
static void remapVertices(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap)
{
size_t block_size = BlockSize == 0 ? vertex_size : BlockSize;
assert(block_size == vertex_size);

for (size_t i = 0; i < vertex_count; ++i)
if (remap[i] != ~0u)
{
assert(remap[i] < vertex_count);
memcpy(static_cast<unsigned char*>(destination) + remap[i] * block_size, static_cast<const unsigned char*>(vertices) + i * block_size, block_size);
}
}

} // namespace meshopt
Expand Down Expand Up @@ -288,6 +304,8 @@ size_t meshopt_generateVertexRemapMulti(unsigned int* destination, const unsigne

void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap)
{
using namespace meshopt;

assert(vertex_size > 0 && vertex_size <= 256);

meshopt_Allocator allocator;
Expand All @@ -300,14 +318,23 @@ void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t v
vertices = vertices_copy;
}

for (size_t i = 0; i < vertex_count; ++i)
// specialize the loop for common vertex sizes to ensure memcpy is compiled as an inlined intrinsic
switch (vertex_size)
{
if (remap[i] != ~0u)
{
assert(remap[i] < vertex_count);
case 4:
return remapVertices<4>(destination, vertices, vertex_count, vertex_size, remap);

memcpy(static_cast<unsigned char*>(destination) + remap[i] * vertex_size, static_cast<const unsigned char*>(vertices) + i * vertex_size, vertex_size);
}
case 8:
return remapVertices<8>(destination, vertices, vertex_count, vertex_size, remap);

case 12:
return remapVertices<12>(destination, vertices, vertex_count, vertex_size, remap);

case 16:
return remapVertices<16>(destination, vertices, vertex_count, vertex_size, remap);

default:
return remapVertices<0>(destination, vertices, vertex_count, vertex_size, remap);
}
}

Expand Down
Loading
Loading