@@ -883,6 +883,9 @@ static const uint32_t GlbMagicBinChunk = 0x004E4942;
883883#ifndef CGLTF_ATOF
884884#define CGLTF_ATOF (str ) atof(str)
885885#endif
886+ #ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
887+ #define CGLTF_VALIDATE_ENABLE_ASSERTS 0
888+ #endif
886889
887890static void * cgltf_default_alloc (void * user , cgltf_size size )
888891{
@@ -1380,6 +1383,12 @@ static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_s
13801383 return bound ;
13811384}
13821385
1386+ #if CGLTF_VALIDATE_ENABLE_ASSERTS
1387+ #define CGLTF_ASSERT_IF (cond , result ) assert(!(cond)); if (cond) return result;
1388+ #else
1389+ #define CGLTF_ASSERT_IF (cond , result ) if (cond) return result;
1390+ #endif
1391+
13831392cgltf_result cgltf_validate (cgltf_data * data )
13841393{
13851394 for (cgltf_size i = 0 ; i < data -> accessors_count ; ++ i )
@@ -1392,10 +1401,7 @@ cgltf_result cgltf_validate(cgltf_data* data)
13921401 {
13931402 cgltf_size req_size = accessor -> offset + accessor -> stride * (accessor -> count - 1 ) + element_size ;
13941403
1395- if (accessor -> buffer_view -> size < req_size )
1396- {
1397- return cgltf_result_data_too_short ;
1398- }
1404+ CGLTF_ASSERT_IF (accessor -> buffer_view -> size < req_size , cgltf_result_data_too_short );
13991405 }
14001406
14011407 if (accessor -> is_sparse )
@@ -1406,27 +1412,18 @@ cgltf_result cgltf_validate(cgltf_data* data)
14061412 cgltf_size indices_req_size = sparse -> indices_byte_offset + indices_component_size * sparse -> count ;
14071413 cgltf_size values_req_size = sparse -> values_byte_offset + element_size * sparse -> count ;
14081414
1409- if (sparse -> indices_buffer_view -> size < indices_req_size ||
1410- sparse -> values_buffer_view -> size < values_req_size )
1411- {
1412- return cgltf_result_data_too_short ;
1413- }
1415+ CGLTF_ASSERT_IF (sparse -> indices_buffer_view -> size < indices_req_size ||
1416+ sparse -> values_buffer_view -> size < values_req_size , cgltf_result_data_too_short );
14141417
1415- if (sparse -> indices_component_type != cgltf_component_type_r_8u &&
1416- sparse -> indices_component_type != cgltf_component_type_r_16u &&
1417- sparse -> indices_component_type != cgltf_component_type_r_32u )
1418- {
1419- return cgltf_result_invalid_gltf ;
1420- }
1418+ CGLTF_ASSERT_IF (sparse -> indices_component_type != cgltf_component_type_r_8u &&
1419+ sparse -> indices_component_type != cgltf_component_type_r_16u &&
1420+ sparse -> indices_component_type != cgltf_component_type_r_32u , cgltf_result_invalid_gltf );
14211421
14221422 if (sparse -> indices_buffer_view -> buffer -> data )
14231423 {
14241424 cgltf_size index_bound = cgltf_calc_index_bound (sparse -> indices_buffer_view , sparse -> indices_byte_offset , sparse -> indices_component_type , sparse -> count );
14251425
1426- if (index_bound >= accessor -> count )
1427- {
1428- return cgltf_result_data_too_short ;
1429- }
1426+ CGLTF_ASSERT_IF (index_bound >= accessor -> count , cgltf_result_data_too_short );
14301427 }
14311428 }
14321429 }
@@ -1435,141 +1432,84 @@ cgltf_result cgltf_validate(cgltf_data* data)
14351432 {
14361433 cgltf_size req_size = data -> buffer_views [i ].offset + data -> buffer_views [i ].size ;
14371434
1438- if (data -> buffer_views [i ].buffer && data -> buffer_views [i ].buffer -> size < req_size )
1439- {
1440- return cgltf_result_data_too_short ;
1441- }
1435+ CGLTF_ASSERT_IF (data -> buffer_views [i ].buffer && data -> buffer_views [i ].buffer -> size < req_size , cgltf_result_data_too_short );
14421436
14431437 if (data -> buffer_views [i ].has_meshopt_compression )
14441438 {
14451439 cgltf_meshopt_compression * mc = & data -> buffer_views [i ].meshopt_compression ;
14461440
1447- if (mc -> buffer == NULL || mc -> buffer -> size < mc -> offset + mc -> size )
1448- {
1449- return cgltf_result_data_too_short ;
1450- }
1441+ CGLTF_ASSERT_IF (mc -> buffer == NULL || mc -> buffer -> size < mc -> offset + mc -> size , cgltf_result_data_too_short );
14511442
1452- if (data -> buffer_views [i ].stride && mc -> stride != data -> buffer_views [i ].stride )
1453- {
1454- return cgltf_result_invalid_gltf ;
1455- }
1443+ CGLTF_ASSERT_IF (data -> buffer_views [i ].stride && mc -> stride != data -> buffer_views [i ].stride , cgltf_result_invalid_gltf );
14561444
1457- if (data -> buffer_views [i ].size != mc -> stride * mc -> count )
1458- {
1459- return cgltf_result_invalid_gltf ;
1460- }
1445+ CGLTF_ASSERT_IF (data -> buffer_views [i ].size != mc -> stride * mc -> count , cgltf_result_invalid_gltf );
14611446
1462- if (mc -> mode == cgltf_meshopt_compression_mode_invalid )
1463- {
1464- return cgltf_result_invalid_gltf ;
1465- }
1447+ CGLTF_ASSERT_IF (mc -> mode == cgltf_meshopt_compression_mode_invalid , cgltf_result_invalid_gltf );
14661448
1467- if (mc -> mode == cgltf_meshopt_compression_mode_attributes && !(mc -> stride % 4 == 0 && mc -> stride <= 256 ))
1468- {
1469- return cgltf_result_invalid_gltf ;
1470- }
1449+ CGLTF_ASSERT_IF (mc -> mode == cgltf_meshopt_compression_mode_attributes && !(mc -> stride % 4 == 0 && mc -> stride <= 256 ), cgltf_result_invalid_gltf );
14711450
1472- if (mc -> mode == cgltf_meshopt_compression_mode_triangles && mc -> count % 3 != 0 )
1473- {
1474- return cgltf_result_invalid_gltf ;
1475- }
1451+ CGLTF_ASSERT_IF (mc -> mode == cgltf_meshopt_compression_mode_triangles && mc -> count % 3 != 0 , cgltf_result_invalid_gltf );
14761452
1477- if ((mc -> mode == cgltf_meshopt_compression_mode_triangles || mc -> mode == cgltf_meshopt_compression_mode_indices ) && mc -> stride != 2 && mc -> stride != 4 )
1478- {
1479- return cgltf_result_invalid_gltf ;
1480- }
1453+ CGLTF_ASSERT_IF ((mc -> mode == cgltf_meshopt_compression_mode_triangles || mc -> mode == cgltf_meshopt_compression_mode_indices ) && mc -> stride != 2 && mc -> stride != 4 , cgltf_result_invalid_gltf );
14811454
1482- if ((mc -> mode == cgltf_meshopt_compression_mode_triangles || mc -> mode == cgltf_meshopt_compression_mode_indices ) && mc -> filter != cgltf_meshopt_compression_filter_none )
1483- {
1484- return cgltf_result_invalid_gltf ;
1485- }
1455+ CGLTF_ASSERT_IF ((mc -> mode == cgltf_meshopt_compression_mode_triangles || mc -> mode == cgltf_meshopt_compression_mode_indices ) && mc -> filter != cgltf_meshopt_compression_filter_none , cgltf_result_invalid_gltf );
14861456
1487- if (mc -> filter == cgltf_meshopt_compression_filter_octahedral && mc -> stride != 4 && mc -> stride != 8 )
1488- {
1489- return cgltf_result_invalid_gltf ;
1490- }
1457+ CGLTF_ASSERT_IF (mc -> filter == cgltf_meshopt_compression_filter_octahedral && mc -> stride != 4 && mc -> stride != 8 , cgltf_result_invalid_gltf );
14911458
1492- if (mc -> filter == cgltf_meshopt_compression_filter_quaternion && mc -> stride != 8 )
1493- {
1494- return cgltf_result_invalid_gltf ;
1495- }
1459+ CGLTF_ASSERT_IF (mc -> filter == cgltf_meshopt_compression_filter_quaternion && mc -> stride != 8 , cgltf_result_invalid_gltf );
14961460 }
14971461 }
14981462
14991463 for (cgltf_size i = 0 ; i < data -> meshes_count ; ++ i )
15001464 {
15011465 if (data -> meshes [i ].weights )
15021466 {
1503- if (data -> meshes [i ].primitives_count && data -> meshes [i ].primitives [0 ].targets_count != data -> meshes [i ].weights_count )
1504- {
1505- return cgltf_result_invalid_gltf ;
1506- }
1467+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives_count && data -> meshes [i ].primitives [0 ].targets_count != data -> meshes [i ].weights_count , cgltf_result_invalid_gltf );
15071468 }
15081469
15091470 if (data -> meshes [i ].target_names )
15101471 {
1511- if (data -> meshes [i ].primitives_count && data -> meshes [i ].primitives [0 ].targets_count != data -> meshes [i ].target_names_count )
1512- {
1513- return cgltf_result_invalid_gltf ;
1514- }
1472+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives_count && data -> meshes [i ].primitives [0 ].targets_count != data -> meshes [i ].target_names_count , cgltf_result_invalid_gltf );
15151473 }
15161474
15171475 for (cgltf_size j = 0 ; j < data -> meshes [i ].primitives_count ; ++ j )
15181476 {
1519- if (data -> meshes [i ].primitives [j ].targets_count != data -> meshes [i ].primitives [0 ].targets_count )
1520- {
1521- return cgltf_result_invalid_gltf ;
1522- }
1477+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives [j ].targets_count != data -> meshes [i ].primitives [0 ].targets_count , cgltf_result_invalid_gltf );
15231478
15241479 if (data -> meshes [i ].primitives [j ].attributes_count )
15251480 {
15261481 cgltf_accessor * first = data -> meshes [i ].primitives [j ].attributes [0 ].data ;
15271482
15281483 for (cgltf_size k = 0 ; k < data -> meshes [i ].primitives [j ].attributes_count ; ++ k )
15291484 {
1530- if (data -> meshes [i ].primitives [j ].attributes [k ].data -> count != first -> count )
1531- {
1532- return cgltf_result_invalid_gltf ;
1533- }
1485+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives [j ].attributes [k ].data -> count != first -> count , cgltf_result_invalid_gltf );
15341486 }
15351487
15361488 for (cgltf_size k = 0 ; k < data -> meshes [i ].primitives [j ].targets_count ; ++ k )
15371489 {
15381490 for (cgltf_size m = 0 ; m < data -> meshes [i ].primitives [j ].targets [k ].attributes_count ; ++ m )
15391491 {
1540- if (data -> meshes [i ].primitives [j ].targets [k ].attributes [m ].data -> count != first -> count )
1541- {
1542- return cgltf_result_invalid_gltf ;
1543- }
1492+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives [j ].targets [k ].attributes [m ].data -> count != first -> count , cgltf_result_invalid_gltf );
15441493 }
15451494 }
15461495
15471496 cgltf_accessor * indices = data -> meshes [i ].primitives [j ].indices ;
15481497
1549- if (indices &&
1498+ CGLTF_ASSERT_IF (indices &&
15501499 indices -> component_type != cgltf_component_type_r_8u &&
15511500 indices -> component_type != cgltf_component_type_r_16u &&
1552- indices -> component_type != cgltf_component_type_r_32u )
1553- {
1554- return cgltf_result_invalid_gltf ;
1555- }
1501+ indices -> component_type != cgltf_component_type_r_32u , cgltf_result_invalid_gltf );
15561502
15571503 if (indices && indices -> buffer_view && indices -> buffer_view -> buffer -> data )
15581504 {
15591505 cgltf_size index_bound = cgltf_calc_index_bound (indices -> buffer_view , indices -> offset , indices -> component_type , indices -> count );
15601506
1561- if (index_bound >= first -> count )
1562- {
1563- return cgltf_result_data_too_short ;
1564- }
1507+ CGLTF_ASSERT_IF (index_bound >= first -> count , cgltf_result_data_too_short );
15651508 }
15661509
15671510 for (cgltf_size k = 0 ; k < data -> meshes [i ].primitives [j ].mappings_count ; ++ k )
15681511 {
1569- if (data -> meshes [i ].primitives [j ].mappings [k ].variant >= data -> variants_count )
1570- {
1571- return cgltf_result_invalid_gltf ;
1572- }
1512+ CGLTF_ASSERT_IF (data -> meshes [i ].primitives [j ].mappings [k ].variant >= data -> variants_count , cgltf_result_invalid_gltf );
15731513 }
15741514 }
15751515 }
@@ -1579,10 +1519,7 @@ cgltf_result cgltf_validate(cgltf_data* data)
15791519 {
15801520 if (data -> nodes [i ].weights && data -> nodes [i ].mesh )
15811521 {
1582- if (data -> nodes [i ].mesh -> primitives_count && data -> nodes [i ].mesh -> primitives [0 ].targets_count != data -> nodes [i ].weights_count )
1583- {
1584- return cgltf_result_invalid_gltf ;
1585- }
1522+ CGLTF_ASSERT_IF (data -> nodes [i ].mesh -> primitives_count && data -> nodes [i ].mesh -> primitives [0 ].targets_count != data -> nodes [i ].weights_count , cgltf_result_invalid_gltf );
15861523 }
15871524 }
15881525
@@ -1593,10 +1530,7 @@ cgltf_result cgltf_validate(cgltf_data* data)
15931530
15941531 while (p1 && p2 )
15951532 {
1596- if (p1 == p2 )
1597- {
1598- return cgltf_result_invalid_gltf ;
1599- }
1533+ CGLTF_ASSERT_IF (p1 == p2 , cgltf_result_invalid_gltf );
16001534
16011535 p1 = p1 -> parent ;
16021536 p2 = p2 -> parent ? p2 -> parent -> parent : NULL ;
@@ -1607,10 +1541,7 @@ cgltf_result cgltf_validate(cgltf_data* data)
16071541 {
16081542 for (cgltf_size j = 0 ; j < data -> scenes [i ].nodes_count ; ++ j )
16091543 {
1610- if (data -> scenes [i ].nodes [j ]-> parent )
1611- {
1612- return cgltf_result_invalid_gltf ;
1613- }
1544+ CGLTF_ASSERT_IF (data -> scenes [i ].nodes [j ]-> parent , cgltf_result_invalid_gltf );
16141545 }
16151546 }
16161547
@@ -1629,20 +1560,14 @@ cgltf_result cgltf_validate(cgltf_data* data)
16291560
16301561 if (channel -> target_path == cgltf_animation_path_type_weights )
16311562 {
1632- if (!channel -> target_node -> mesh || !channel -> target_node -> mesh -> primitives_count )
1633- {
1634- return cgltf_result_invalid_gltf ;
1635- }
1563+ CGLTF_ASSERT_IF (!channel -> target_node -> mesh || !channel -> target_node -> mesh -> primitives_count , cgltf_result_invalid_gltf );
16361564
16371565 components = channel -> target_node -> mesh -> primitives [0 ].targets_count ;
16381566 }
16391567
16401568 cgltf_size values = channel -> sampler -> interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1 ;
16411569
1642- if (channel -> sampler -> input -> count * components * values != channel -> sampler -> output -> count )
1643- {
1644- return cgltf_result_data_too_short ;
1645- }
1570+ CGLTF_ASSERT_IF (channel -> sampler -> input -> count * components * values != channel -> sampler -> output -> count , cgltf_result_data_too_short );
16461571 }
16471572 }
16481573
0 commit comments