diff --git a/libs/geometry/src/SurfaceOrientation.cpp b/libs/geometry/src/SurfaceOrientation.cpp index 478bb08d1b..47fc2a7910 100644 --- a/libs/geometry/src/SurfaceOrientation.cpp +++ b/libs/geometry/src/SurfaceOrientation.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -226,6 +227,7 @@ SurfaceOrientation* OrientationBuilderImpl::buildWithUvs() { memset(tan2.data(), 0, sizeof(float3) * vertexCount); for (size_t a = 0; a < triangleCount; ++a) { uint3 tri = triangles16 ? uint3(triangles16[a]) : triangles32[a]; + assert_invariant(tri.x < vertexCount && tri.y < vertexCount && tri.z < vertexCount); const float3& v1 = positions[tri.x]; const float3& v2 = positions[tri.y]; const float3& v3 = positions[tri.z]; @@ -334,6 +336,7 @@ SurfaceOrientation* OrientationBuilderImpl::buildWithFlatNormals() { float3* normals = new float3[vertexCount]; for (size_t a = 0; a < triangleCount; ++a) { const uint3 tri = triangles16 ? uint3(triangles16[a]) : triangles32[a]; + assert_invariant(tri.x < vertexCount && tri.y < vertexCount && tri.z < vertexCount); const float3 v1 = positions[tri.x]; const float3 v2 = positions[tri.y]; const float3 v3 = positions[tri.z]; diff --git a/libs/gltfio/src/DracoCache.cpp b/libs/gltfio/src/DracoCache.cpp index 57eaf7ea8a..aadc32e84d 100644 --- a/libs/gltfio/src/DracoCache.cpp +++ b/libs/gltfio/src/DracoCache.cpp @@ -117,10 +117,10 @@ DracoMesh* DracoMesh::decode(const uint8_t* data, size_t dataSize) { return new DracoMesh(new DracoMeshDetails { std::move(meshStatus).value() }); } -void DracoMesh::getFaceIndices(cgltf_accessor* target) const { +bool DracoMesh::getFaceIndices(cgltf_accessor* target) const { // Return early if we've already decompressed this data. if (target->buffer_view) { - return; + return true; } draco::Mesh* mesh = mDetails->mesh.get(); @@ -131,7 +131,7 @@ void DracoMesh::getFaceIndices(cgltf_accessor* target) const { if (target->count != count) { slog.e << "The glTF accessor wants " << target->count << " indices, " << "but the decoded Draco mesh has " << count << " indices." << io::endl; - return; + return false; } cgltf_buffer_view* view = new cgltf_buffer_view; @@ -149,8 +149,9 @@ void DracoMesh::getFaceIndices(cgltf_accessor* target) const { case cgltf_component_type_r_8u: convertFaces(target, mesh); break; default: slog.e << "Unexpected component type for Draco indices." << io::endl; - break; + return false; } + return true; } bool DracoMesh::getVertexAttributes(uint32_t attributeId, cgltf_accessor* target) const { @@ -173,11 +174,12 @@ bool DracoMesh::getVertexAttributes(uint32_t attributeId, cgltf_accessor* target // DracoMesh. uint32_t count = mesh->num_points(); if (target->count != count) { - slog.w << "The glTF accessor wants " << target->count << " vertices, " + slog.e << "The glTF accessor wants " << target->count << " vertices, " << "but the decoded Draco mesh has " << count << " vertices." << io::endl; - if (target->count < count) { - count = target->count; - } + + // It is tempting to degrade gracefully by processing only the lesser of the two + // counts, but doing so would lead to invalid indices in the index buffer. + return false; } cgltf_buffer_view* view = new cgltf_buffer_view; @@ -209,7 +211,7 @@ bool DracoMesh::getVertexAttributes(uint32_t attributeId, cgltf_accessor* target DracoMesh::~DracoMesh() {} struct DracoMeshDetails {}; DracoMesh* DracoMesh::decode(const uint8_t* data, size_t dataSize) { return nullptr; } -void DracoMesh::getFaceIndices(cgltf_accessor* target) const {} +bool DracoMesh::getFaceIndices(cgltf_accessor* target) const {} bool DracoMesh::getVertexAttributes(uint32_t attributeId, cgltf_accessor* target) const { return false; diff --git a/libs/gltfio/src/DracoCache.h b/libs/gltfio/src/DracoCache.h index 2547ca3f73..88963da8d7 100644 --- a/libs/gltfio/src/DracoCache.h +++ b/libs/gltfio/src/DracoCache.h @@ -44,7 +44,11 @@ private: // Decodes a Draco mesh upon construction and retains the results. // -// For convenience, the decoded attributes and indices are written into glTF accessor structs. +// The DracoMesh API leverages cgltf accessor structs in a way that bears explanation. These are +// read / write parameters that tell the decoder where to write the decoded data, and what format +// is desired. The buffer_view in the accessor should be null unless decompressed data is already +// loaded. This tells the decoder that it should create a buffer_view and a buffer. The buffer +// view, the buffer, and the buffer's data are all automatically freed when DracoMesh is destroyed. // // Note that in the gltfio architecture, the AssetLoader has the job of constructing VertexBuffer // objects while the ResourceLoader has the job of populating them asychronously. This means that @@ -54,7 +58,7 @@ private: class DracoMesh { public: static DracoMesh* decode(const uint8_t* compressedData, size_t compressedSize); - void getFaceIndices(cgltf_accessor* destination) const; + bool getFaceIndices(cgltf_accessor* destination) const; bool getVertexAttributes(uint32_t attributeId, cgltf_accessor* destination) const; ~DracoMesh(); private: diff --git a/libs/gltfio/src/ResourceLoader.cpp b/libs/gltfio/src/ResourceLoader.cpp index 9427eecd01..de472d5e09 100644 --- a/libs/gltfio/src/ResourceLoader.cpp +++ b/libs/gltfio/src/ResourceLoader.cpp @@ -200,25 +200,30 @@ static void decodeDracoMeshes(FFilamentAsset* asset) { }; // Go through every primitive and check if it has a Draco mesh. - for (auto pair : asset->mPrimitives) { + for (auto& pair : asset->mPrimitives) { const cgltf_primitive* prim = pair.first; - VertexBuffer* vb = pair.second; if (!prim->has_draco_mesh_compression) { continue; } const cgltf_draco_mesh_compression& draco = prim->draco_mesh_compression; + // If an error occurs, we can simply set the primitive's associated VertexBuffer to null. + // This does not cause a leak because it is a weak reference. + auto& vertexBuffer = pair.second; + // Check if we have already decoded this mesh. DracoMesh* mesh = dracoCache->findOrCreateMesh(draco.buffer_view); if (!mesh) { - slog.w << "Cannot decompress mesh, Draco decoding error." << io::endl; + slog.e << "Cannot decompress mesh, Draco decoding error." << io::endl; + vertexBuffer = nullptr; continue; } // Copy over the decompressed data, converting the data type if necessary. - if (prim->indices) { - mesh->getFaceIndices(prim->indices); + if (prim->indices && !mesh->getFaceIndices(prim->indices)) { + vertexBuffer = nullptr; + continue; } // Go through each attribute in the decompressed mesh. @@ -237,7 +242,10 @@ static void decodeDracoMeshes(FFilamentAsset* asset) { } // Copy over the decompressed data, converting the data type if necessary. - mesh->getVertexAttributes(id, accessor); + if (!mesh->getVertexAttributes(id, accessor)) { + vertexBuffer = nullptr; + break; + } } } }