Skip to content

Commit

Permalink
gltfio: refactor for clarity (#7652)
Browse files Browse the repository at this point in the history
- Pull certain utility functions in a separate header and cpp
- Refactor ResourceLoader::loadeResources into smaller methods
  • Loading branch information
poweifeng authored Mar 12, 2024
1 parent 9aad4df commit fa6b4eb
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 397 deletions.
2 changes: 2 additions & 0 deletions android/gltfio-android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ set(GLTFIO_SRCS
${GLTFIO_DIR}/src/TangentsJob.cpp
${GLTFIO_DIR}/src/TangentsJob.h
${GLTFIO_DIR}/src/UbershaderProvider.cpp
${GLTFIO_DIR}/src/Utility.cpp
${GLTFIO_DIR}/src/Utility.h
${GLTFIO_DIR}/src/Wireframe.cpp
${GLTFIO_DIR}/src/Wireframe.h
${GLTFIO_DIR}/src/downcast.h
Expand Down
2 changes: 2 additions & 0 deletions libs/gltfio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ set(SRCS
src/TangentsJob.cpp
src/TangentsJob.h
src/UbershaderProvider.cpp
src/Utility.cpp
src/Utility.h
src/Wireframe.cpp
src/Wireframe.h
src/downcast.h
Expand Down
1 change: 0 additions & 1 deletion libs/gltfio/include/gltfio/ResourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class UTILS_PUBLIC ResourceLoader {

private:
bool loadResources(FFilamentAsset* asset, bool async);
void normalizeSkinningWeights(FFilamentAsset* asset) const;
struct Impl;
Impl* pImpl;
};
Expand Down
31 changes: 9 additions & 22 deletions libs/gltfio/src/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "FNodeManager.h"
#include "FTrsTransformManager.h"
#include "GltfEnums.h"
#include "Utility.h"

#include <filament/Box.h>
#include <filament/BufferObject.h>
Expand Down Expand Up @@ -52,7 +53,6 @@

#include <tsl/robin_map.h>

#define CGLTF_IMPLEMENTATION
#include <cgltf.h>

#include "downcast.h"
Expand Down Expand Up @@ -85,21 +85,6 @@ static constexpr cgltf_material kDefaultMat = {
},
};

// Sometimes a glTF bufferview includes unused data at the end (e.g. in skinning.gltf) so we need to
// compute the correct size of the vertex buffer. Filament automatically infers the size of
// driver-level vertex buffers from the attribute data (stride, count, offset) and clients are
// expected to avoid uploading data blobs that exceed this size. Since this information doesn't
// exist in the glTF we need to compute it manually. This is a bit of a cheat, cgltf_calc_size is
// private but its implementation file is available in this cpp file.
uint32_t computeBindingSize(const cgltf_accessor* accessor) {
cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
return uint32_t(accessor->stride * (accessor->count - 1) + element_size);
}

uint32_t computeBindingOffset(const cgltf_accessor* accessor) {
return uint32_t(accessor->offset + accessor->buffer_view->offset);
}

static const char* getNodeName(const cgltf_node* node, const char* defaultNodeName) {
if (node->name) return node->name;
if (node->mesh && node->mesh->name) return node->mesh->name;
Expand Down Expand Up @@ -617,15 +602,15 @@ void FAssetLoader::recurseEntities(const cgltf_node* node, SceneMask scenes, Ent

void FAssetLoader::createPrimitives(const cgltf_node* node, const char* name,
FFilamentAsset* fAsset) {
const cgltf_data* srcAsset = fAsset->mSourceAsset->hierarchy;
cgltf_data* gltf = fAsset->mSourceAsset->hierarchy;
const cgltf_mesh* mesh = node->mesh;
assert_invariant(srcAsset != nullptr);
assert_invariant(gltf != nullptr);
assert_invariant(mesh != nullptr);

// If the mesh is already loaded, obtain the list of Filament VertexBuffer / IndexBuffer objects
// that were already generated (one for each primitive), otherwise allocate a new list of
// pointers for the primitives.
FixedCapacityVector<Primitive>& prims = fAsset->mMeshCache[mesh - srcAsset->meshes];
FixedCapacityVector<Primitive>& prims = fAsset->mMeshCache[mesh - gltf->meshes];
if (prims.empty()) {
prims.reserve(mesh->primitives_count);
prims.resize(mesh->primitives_count);
Expand Down Expand Up @@ -791,6 +776,7 @@ void FAssetLoader::createMaterialVariants(const cgltf_mesh* mesh, Entity entity,

bool FAssetLoader::createPrimitive(const cgltf_primitive& inPrim, const char* name,
Primitive* outPrim, FFilamentAsset* fAsset) {

Material* material = getMaterial(fAsset->mSourceAsset->hierarchy,
inPrim.material, &outPrim->uvmap, primitiveHasVertexColor(inPrim));
AttributeBitset requiredAttributes = material->getRequiredAttributes();
Expand Down Expand Up @@ -849,7 +835,7 @@ bool FAssetLoader::createPrimitive(const cgltf_primitive& inPrim, const char* na
bool hasUv0 = false, hasUv1 = false, hasVertexColor = false, hasNormals = false;
uint32_t vertexCount = 0;

const size_t firstSlot = fAsset->mBufferSlots.size();
const size_t firstSlot = slots->size();
int slot = 0;

for (cgltf_size aindex = 0; aindex < inPrim.attributes_count; aindex++) {
Expand Down Expand Up @@ -891,6 +877,7 @@ bool FAssetLoader::createPrimitive(const cgltf_primitive& inPrim, const char* na
utils::slog.e << "Too many joints in " << name << utils::io::endl;
continue;
}

if (atype == cgltf_attribute_type_texcoord) {
if (index >= UvMapSize) {
utils::slog.e << "Too many texture coordinate sets in " << name << utils::io::endl;
Expand Down Expand Up @@ -1065,8 +1052,8 @@ bool FAssetLoader::createPrimitive(const cgltf_primitive& inPrim, const char* na
fAsset->mPrimitives.push_back({&inPrim, vertices});
fAsset->mVertexBuffers.push_back(vertices);

for (size_t i = firstSlot; i < fAsset->mBufferSlots.size(); ++i) {
fAsset->mBufferSlots[i].vertexBuffer = vertices;
for (size_t i = firstSlot; i < slots->size(); ++i) {
(*slots)[i].vertexBuffer = vertices;
}

if (targetsCount > 0) {
Expand Down
1 change: 1 addition & 0 deletions libs/gltfio/src/FFilamentAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "DependencyGraph.h"
#include "DracoCache.h"
#include "FFilamentInstance.h"
#include "Utility.h"

#include <tsl/htrie_map.h>

Expand Down
Loading

0 comments on commit fa6b4eb

Please sign in to comment.