13
13
namespace meshopt
14
14
{
15
15
16
- // This must be <= 255 since index 0xff is used internally to indice a vertex that doesn't belong to a meshlet
17
- const size_t kMeshletMaxVertices = 255 ;
16
+ // This must be <= 256 since meshlet indices are stored as bytes
17
+ const size_t kMeshletMaxVertices = 256 ;
18
18
19
19
// A reasonable limit is around 2*max_vertices or less
20
20
const size_t kMeshletMaxTriangles = 512 ;
@@ -328,22 +328,22 @@ static void finishMeshlet(meshopt_Meshlet& meshlet, unsigned char* meshlet_trian
328
328
meshlet_triangles[offset++] = 0 ;
329
329
}
330
330
331
- static bool appendMeshlet (meshopt_Meshlet& meshlet, unsigned int a, unsigned int b, unsigned int c, unsigned char * used, meshopt_Meshlet* meshlets, unsigned int * meshlet_vertices, unsigned char * meshlet_triangles, size_t meshlet_offset, size_t max_vertices, size_t max_triangles, bool split = false )
331
+ static bool appendMeshlet (meshopt_Meshlet& meshlet, unsigned int a, unsigned int b, unsigned int c, short * used, meshopt_Meshlet* meshlets, unsigned int * meshlet_vertices, unsigned char * meshlet_triangles, size_t meshlet_offset, size_t max_vertices, size_t max_triangles, bool split = false )
332
332
{
333
- unsigned char & av = used[a];
334
- unsigned char & bv = used[b];
335
- unsigned char & cv = used[c];
333
+ short & av = used[a];
334
+ short & bv = used[b];
335
+ short & cv = used[c];
336
336
337
337
bool result = false ;
338
338
339
- int used_extra = (av == 0xff ) + (bv == 0xff ) + (cv == 0xff );
339
+ int used_extra = (av < 0 ) + (bv < 0 ) + (cv < 0 );
340
340
341
341
if (meshlet.vertex_count + used_extra > max_vertices || meshlet.triangle_count >= max_triangles || split)
342
342
{
343
343
meshlets[meshlet_offset] = meshlet;
344
344
345
345
for (size_t j = 0 ; j < meshlet.vertex_count ; ++j)
346
- used[meshlet_vertices[meshlet.vertex_offset + j]] = 0xff ;
346
+ used[meshlet_vertices[meshlet.vertex_offset + j]] = - 1 ;
347
347
348
348
finishMeshlet (meshlet, meshlet_triangles);
349
349
@@ -355,33 +355,33 @@ static bool appendMeshlet(meshopt_Meshlet& meshlet, unsigned int a, unsigned int
355
355
result = true ;
356
356
}
357
357
358
- if (av == 0xff )
358
+ if (av < 0 )
359
359
{
360
- av = ( unsigned char ) meshlet.vertex_count ;
360
+ av = short ( meshlet.vertex_count ) ;
361
361
meshlet_vertices[meshlet.vertex_offset + meshlet.vertex_count ++] = a;
362
362
}
363
363
364
- if (bv == 0xff )
364
+ if (bv < 0 )
365
365
{
366
- bv = ( unsigned char ) meshlet.vertex_count ;
366
+ bv = short ( meshlet.vertex_count ) ;
367
367
meshlet_vertices[meshlet.vertex_offset + meshlet.vertex_count ++] = b;
368
368
}
369
369
370
- if (cv == 0xff )
370
+ if (cv < 0 )
371
371
{
372
- cv = ( unsigned char ) meshlet.vertex_count ;
372
+ cv = short ( meshlet.vertex_count ) ;
373
373
meshlet_vertices[meshlet.vertex_offset + meshlet.vertex_count ++] = c;
374
374
}
375
375
376
- meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 0 ] = av;
377
- meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 1 ] = bv;
378
- meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 2 ] = cv;
376
+ meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 0 ] = ( unsigned char ) av;
377
+ meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 1 ] = ( unsigned char ) bv;
378
+ meshlet_triangles[meshlet.triangle_offset + meshlet.triangle_count * 3 + 2 ] = ( unsigned char ) cv;
379
379
meshlet.triangle_count ++;
380
380
381
381
return result;
382
382
}
383
383
384
- static unsigned int getNeighborTriangle (const meshopt_Meshlet& meshlet, const Cone& meshlet_cone, const unsigned int * meshlet_vertices, const unsigned int * indices, const TriangleAdjacency2& adjacency, const Cone* triangles, const unsigned int * live_triangles, const unsigned char * used, float meshlet_expected_radius, float cone_weight)
384
+ static unsigned int getNeighborTriangle (const meshopt_Meshlet& meshlet, const Cone& meshlet_cone, const unsigned int * meshlet_vertices, const unsigned int * indices, const TriangleAdjacency2& adjacency, const Cone* triangles, const unsigned int * live_triangles, const short * used, float meshlet_expected_radius, float cone_weight)
385
385
{
386
386
unsigned int best_triangle = ~0u ;
387
387
int best_priority = 5 ;
@@ -399,7 +399,7 @@ static unsigned int getNeighborTriangle(const meshopt_Meshlet& meshlet, const Co
399
399
unsigned int triangle = neighbors[j];
400
400
unsigned int a = indices[triangle * 3 + 0 ], b = indices[triangle * 3 + 1 ], c = indices[triangle * 3 + 2 ];
401
401
402
- int extra = (used[a] == 0xff ) + (used[b] == 0xff ) + (used[c] == 0xff );
402
+ int extra = (used[a] < 0 ) + (used[b] < 0 ) + (used[c] < 0 );
403
403
assert (extra <= 2 );
404
404
405
405
int priority = -1 ;
@@ -785,9 +785,9 @@ size_t meshopt_buildMeshletsFlex(meshopt_Meshlet* meshlets, unsigned int* meshle
785
785
cornerz = cornerz > tri.pz ? tri.pz : cornerz;
786
786
}
787
787
788
- // index of the vertex in the meshlet, 0xff if the vertex isn't used
789
- unsigned char * used = allocator.allocate <unsigned char >(vertex_count);
790
- memset (used, -1 , vertex_count);
788
+ // index of the vertex in the meshlet, -1 if the vertex isn't used
789
+ short * used = allocator.allocate <short >(vertex_count);
790
+ memset (used, -1 , vertex_count * sizeof ( short ) );
791
791
792
792
// initial seed triangle is the one closest to the corner
793
793
unsigned int initial_seed = ~0u ;
@@ -846,7 +846,7 @@ size_t meshopt_buildMeshletsFlex(meshopt_Meshlet* meshlets, unsigned int* meshle
846
846
if (best_triangle == ~0u )
847
847
break ;
848
848
849
- int best_extra = (used[indices[best_triangle * 3 + 0 ]] == 0xff ) + (used[indices[best_triangle * 3 + 1 ]] == 0xff ) + (used[indices[best_triangle * 3 + 2 ]] == 0xff );
849
+ int best_extra = (used[indices[best_triangle * 3 + 0 ]] < 0 ) + (used[indices[best_triangle * 3 + 1 ]] < 0 ) + (used[indices[best_triangle * 3 + 2 ]] < 0 );
850
850
851
851
// if the best triangle doesn't fit into current meshlet, we re-select using seeds to maintain global flow
852
852
if (split || (meshlet.vertex_count + best_extra > max_vertices || meshlet.triangle_count >= max_triangles))
@@ -936,9 +936,9 @@ size_t meshopt_buildMeshletsScan(meshopt_Meshlet* meshlets, unsigned int* meshle
936
936
937
937
meshopt_Allocator allocator;
938
938
939
- // index of the vertex in the meshlet, 0xff if the vertex isn't used
940
- unsigned char * used = allocator.allocate <unsigned char >(vertex_count);
941
- memset (used, -1 , vertex_count);
939
+ // index of the vertex in the meshlet, -1 if the vertex isn't used
940
+ short * used = allocator.allocate <short >(vertex_count);
941
+ memset (used, -1 , vertex_count * sizeof ( short ) );
942
942
943
943
meshopt_Meshlet meshlet = {};
944
944
size_t meshlet_offset = 0 ;
@@ -1233,23 +1233,23 @@ void meshopt_optimizeMeshlet(unsigned int* meshlet_vertices, unsigned char* mesh
1233
1233
// reorder meshlet vertices for access locality assuming index buffer is scanned sequentially
1234
1234
unsigned int order[kMeshletMaxVertices ];
1235
1235
1236
- unsigned char remap[kMeshletMaxVertices ];
1237
- memset (remap, -1 , vertex_count);
1236
+ short remap[kMeshletMaxVertices ];
1237
+ memset (remap, -1 , vertex_count * sizeof ( short ) );
1238
1238
1239
1239
size_t vertex_offset = 0 ;
1240
1240
1241
1241
for (size_t i = 0 ; i < triangle_count * 3 ; ++i)
1242
1242
{
1243
- unsigned char & r = remap[indices[i]];
1243
+ short & r = remap[indices[i]];
1244
1244
1245
- if (r == 0xff )
1245
+ if (r < 0 )
1246
1246
{
1247
- r = ( unsigned char ) (vertex_offset);
1247
+ r = short (vertex_offset);
1248
1248
order[vertex_offset] = vertices[indices[i]];
1249
1249
vertex_offset++;
1250
1250
}
1251
1251
1252
- indices[i] = r;
1252
+ indices[i] = ( unsigned char ) r;
1253
1253
}
1254
1254
1255
1255
assert (vertex_offset <= vertex_count);
0 commit comments