How to handle meshlet.triangle_count % 3 != 0?
#990
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
This is incorrect: the
This should only happen if you pass zero indices as an input I believe. Can you show the code that calls |
Beta Was this translation helpful? Give feedback.
-
Doing this: for (uint32_t i = 0; i < meshlet.triangle_count; i++) {
uint32_t triangle_index = (meshlet.triangle_offset + i) * 3;
uint8_t v0 = meshlet_triangles[triangle_index + 0];
uint8_t v1 = meshlet_triangles[triangle_index + 1];
uint8_t v2 = meshlet_triangles[triangle_index + 2];
result.indices.push_back({v0, v1, v2});
}Resulted in many degenerate triangles and ultimately a SEGFAULT due to out of bounds index value. Doing the same thing but assuming
So I run the mesh through classic meshopt pipeline, then generate a couple of discrete LODs (everything works fine up until now) and then feed each [[nodiscard]] std::pair<MeshletArray, MeshletBoundsArray> generate_meshlets(
const PositionArray &positions, IndexSpan indices)
{
ZoneScoped;
ZoneValue(positions.size());
ZoneValue(indices.size());
constexpr float cone_weight = 0.25;
constexpr float split_factor = 2.0;
constexpr size_t max_vertices = 96;
constexpr size_t min_triangles = 4;
constexpr size_t max_triangles = 96; // up to 126, but divisible by 4
size_t max_meshlets =
meshopt_buildMeshletsBound(indices.size(), max_vertices, min_triangles);
MeshletArray meshlet_array;
meshlet_array.meshlets.resize(max_meshlets);
meshlet_array.meshlet_vertices.resize(indices.size());
meshlet_array.meshlet_triangles.resize(indices.size() + max_meshlets * 3);
size_t meshlet_count = meshopt_buildMeshletsFlex((meshopt_Meshlet *)meshlet_array.meshlets.data(),
meshlet_array.meshlet_vertices.data(), meshlet_array.meshlet_triangles.data(),
indices.data(), indices.size(),
(float *)positions.data(), positions.size(), sizeof(Position),
max_vertices, min_triangles, max_triangles, cone_weight, split_factor);
// At this point `meshlet_count` might be 0
}I looked into what's happening inside and it seems that this if statement at // add meshlet to the output; when the current meshlet is full we reset the accumulated bounds
if (appendMeshlet(meshlet, a, b, c, used, meshlets, meshlet_vertices, meshlet_triangles, meshlet_offset, max_vertices, max_triangles, split))
{
meshlet_offset++;
memset(&meshlet_cone_acc, 0, sizeof(meshlet_cone_acc));
} |
Beta Was this translation helpful? Give feedback.



Looking at your full source code, this line is also incorrect as it needs to multiply triangle_count (but not offset) by 3:
The rest seems fine upon initial inspection, except extractMeshlet which is not quite the code we've been discussing but presumably some later updates haven't been pushed.
Also just noting that the verification step should not be necessary: generated meshlets should never contain out of bounds indices.