MCPcopy Create free account
hub / github.com/defold/defold / ValidateSparseAccessorsForUnpack

Function ValidateSparseAccessorsForUnpack

engine/modelc/src/modelimporter_gltf.cpp:390–454  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

388}
389
390static bool ValidateSparseAccessorsForUnpack(Scene* scene, cgltf_data* data)
391{
392 for (cgltf_size i = 0; i < data->accessors_count; ++i)
393 {
394 cgltf_accessor* accessor = &data->accessors[i];
395 if (!accessor->is_sparse)
396 continue;
397
398 cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
399 if (element_size == 0)
400 {
401 SetLoadError(scene, "Invalid sparse accessor component type.");
402 return false;
403 }
404
405 if (accessor->buffer_view && accessor->count > 0)
406 {
407 cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
408 if (accessor->buffer_view->size < req_size)
409 {
410 SetLoadError(scene, "Sparse accessor base buffer view is too short.");
411 return false;
412 }
413 }
414
415 cgltf_accessor_sparse* sparse = &accessor->sparse;
416 cgltf_size index_component_size = cgltf_component_size(sparse->indices_component_type);
417 if (index_component_size == 0 || !sparse->indices_buffer_view || !sparse->values_buffer_view)
418 {
419 SetLoadError(scene, "Invalid sparse accessor indices or values.");
420 return false;
421 }
422
423 cgltf_size indices_req_size = sparse->indices_byte_offset + index_component_size * sparse->count;
424 cgltf_size values_req_size = sparse->values_byte_offset + element_size * sparse->count;
425 if (sparse->indices_buffer_view->size < indices_req_size ||
426 sparse->values_buffer_view->size < values_req_size)
427 {
428 SetLoadError(scene, "Sparse accessor buffer view is too short.");
429 return false;
430 }
431
432 const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
433 const uint8_t* values_data = cgltf_buffer_view_data(sparse->values_buffer_view);
434 if (!index_data || !values_data)
435 {
436 SetLoadError(scene, "Sparse accessor buffer data is missing.");
437 return false;
438 }
439
440 index_data += sparse->indices_byte_offset;
441 for (cgltf_size j = 0; j < sparse->count; ++j, index_data += index_component_size)
442 {
443 cgltf_size sparse_index = 0;
444 if (!ReadSparseIndex(index_data, sparse->indices_component_type, &sparse_index) ||
445 sparse_index >= accessor->count)
446 {
447 SetLoadError(scene, "Sparse accessor index is out of range.");

Callers 1

LoadFinalizeGltfFunction · 0.85

Calls 5

cgltf_calc_sizeFunction · 0.85
SetLoadErrorFunction · 0.85
cgltf_component_sizeFunction · 0.85
cgltf_buffer_view_dataFunction · 0.85
ReadSparseIndexFunction · 0.85

Tested by

no test coverage detected