From cbc2a595eccb76ad7b2ffb762d916774db2fccb1 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 25 May 2024 14:49:38 -0700 Subject: [PATCH] extern: Improve EXT_mesh_gpu_instancing validation in cgltf With this change we validate that instanced meshes have a mesh, have attributes and that all attributes agree on the number of elements. This makes sure that using the instancing data is less error prone and is unlikely to lead to memory safety issues. The same change was submitted upstream: https://github.com/jkuhlmann/cgltf/pull/252 --- extern/cgltf.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/extern/cgltf.h b/extern/cgltf.h index 17dc0ca5..ac392aba 100644 --- a/extern/cgltf.h +++ b/extern/cgltf.h @@ -1697,7 +1697,20 @@ cgltf_result cgltf_validate(cgltf_data* data) { if (data->nodes[i].weights && data->nodes[i].mesh) { - 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); + 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); + } + + if (data->nodes[i].has_mesh_gpu_instancing) + { + CGLTF_ASSERT_IF(data->nodes[i].mesh == NULL, cgltf_result_invalid_gltf); + CGLTF_ASSERT_IF(data->nodes[i].mesh_gpu_instancing.attributes_count == 0, cgltf_result_invalid_gltf); + + cgltf_accessor* first = data->nodes[i].mesh_gpu_instancing.attributes[0].data; + + for (cgltf_size k = 0; k < data->nodes[i].mesh_gpu_instancing.attributes_count; ++k) + { + CGLTF_ASSERT_IF(data->nodes[i].mesh_gpu_instancing.attributes[k].data->count != first->count, cgltf_result_invalid_gltf); + } } }