gltfio: robustness improvements for Draco meshes.
These changes are motivated by #3815. See also: https://github.com/KhronosGroup/glTF-Blender-IO/issues/1326 https://github.com/KhronosGroup/glTF-Blender-IO/issues/1345
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <geometry/SurfaceOrientation.h>
|
||||
|
||||
#include <utils/Panic.h>
|
||||
#include <utils/debug.h>
|
||||
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
@@ -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];
|
||||
|
||||
@@ -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<uint8_t>(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;
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user