Skip to content

Commit

Permalink
Change: Replace attribute_type with dedicated type
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Jun 21, 2024
1 parent 00ca410 commit 36f612d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/gl_viewer/gl_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ bool loadMesh(Viewer* viewer, fastgltf::Mesh& mesh) {

{
// Position
auto& positionAccessor = asset.accessors[positionIt->second];
auto& positionAccessor = asset.accessors[positionIt->accessorIndex];
if (!positionAccessor.bufferViewIndex.has_value())
continue;

Expand All @@ -442,7 +442,7 @@ bool loadMesh(Viewer* viewer, fastgltf::Mesh& mesh) {
auto texcoordAttribute = std::string("TEXCOORD_") + std::to_string(baseColorTexcoordIndex);
if (const auto* texcoord = it->findAttribute(texcoordAttribute); texcoord != it->attributes.end()) {
// Tex coord
auto& texCoordAccessor = asset.accessors[texcoord->second];
auto& texCoordAccessor = asset.accessors[texcoord->accessorIndex];
if (!texCoordAccessor.bufferViewIndex.has_value())
continue;

Expand Down
31 changes: 17 additions & 14 deletions include/fastgltf/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,10 @@ namespace fastgltf {
math::fvec3 scale = math::fvec3(1.f);
};

FASTGLTF_EXPORT using attribute_type = std::pair<FASTGLTF_STD_PMR_NS::string, std::size_t>;
FASTGLTF_EXPORT struct Attribute {
FASTGLTF_STD_PMR_NS::string name;
std::size_t accessorIndex;
};

FASTGLTF_EXPORT struct Node {
Optional<std::size_t> meshIndex;
Expand All @@ -1657,21 +1660,21 @@ namespace fastgltf {
/**
* Only ever non-empty when EXT_mesh_gpu_instancing is enabled and used by the asset.
*/
FASTGLTF_STD_PMR_NS::vector<attribute_type> instancingAttributes;
FASTGLTF_STD_PMR_NS::vector<Attribute> instancingAttributes;

FASTGLTF_STD_PMR_NS::string name;

[[nodiscard]] auto findInstancingAttribute(std::string_view attributeName) noexcept {
for (auto it = instancingAttributes.begin(); it != instancingAttributes.end(); ++it) {
if (it->first == attributeName)
if (it->name == attributeName)
return it;
}
return instancingAttributes.end();
}

[[nodiscard]] auto findInstancingAttribute(std::string_view attributeName) const noexcept {
for (auto it = instancingAttributes.cbegin(); it != instancingAttributes.cend(); ++it) {
if (it->first == attributeName)
if (it->name == attributeName)
return it;
}
return instancingAttributes.cend();
Expand All @@ -1681,10 +1684,10 @@ namespace fastgltf {
FASTGLTF_EXPORT struct Primitive {
// Instead of a map, we have a list of attributes here. Each pair contains
// the name of the attribute and the corresponding accessor index.
FASTGLTF_FG_PMR_NS::SmallVector<attribute_type, 4> attributes;
FASTGLTF_FG_PMR_NS::SmallVector<Attribute, 4> attributes;
PrimitiveType type = PrimitiveType::Triangles;

FASTGLTF_STD_PMR_NS::vector<FASTGLTF_FG_PMR_NS::SmallVector<attribute_type, 4>> targets;
FASTGLTF_STD_PMR_NS::vector<FASTGLTF_FG_PMR_NS::SmallVector<Attribute, 4>> targets;

Optional<std::size_t> indicesAccessor;
Optional<std::size_t> materialIndex;
Expand All @@ -1697,34 +1700,34 @@ namespace fastgltf {
std::vector<Optional<std::size_t>> mappings;

[[nodiscard]] auto findAttribute(std::string_view name) noexcept {
for (auto it = attributes.begin(); it != attributes.end(); ++it) {
if (it->first == name)
for (auto* it = attributes.begin(); it != attributes.end(); ++it) {
if (it->name == name)
return it;
}
return attributes.end();
}

[[nodiscard]] auto findAttribute(std::string_view name) const noexcept {
for (auto it = attributes.cbegin(); it != attributes.cend(); ++it) {
if (it->first == name)
for (const auto* it = attributes.cbegin(); it != attributes.cend(); ++it) {
if (it->name == name)
return it;
}
return attributes.cend();
}

[[nodiscard]] auto findTargetAttribute(std::size_t targetIndex, std::string_view name) noexcept {
auto& targetAttributes = targets[targetIndex];
for (auto it = targetAttributes.begin(); it != targetAttributes.end(); ++it) {
if (it->first == name)
for (auto* it = targetAttributes.begin(); it != targetAttributes.end(); ++it) {
if (it->name == name)
return it;
}
return targetAttributes.end();
}

[[nodiscard]] auto findTargetAttribute(std::size_t targetIndex, std::string_view name) const noexcept {
const auto& targetAttributes = targets[targetIndex];
for (auto it = targetAttributes.cbegin(); it != targetAttributes.cend(); ++it) {
if (it->first == name)
for (const auto* it = targetAttributes.cbegin(); it != targetAttributes.cend(); ++it) {
if (it->name == name)
return it;
}
return targetAttributes.cend();
Expand Down
18 changes: 10 additions & 8 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,20 +807,22 @@ template <typename T> fg::Error fg::Parser::parseAttributes(simdjson::dom::objec
for (const auto& field : object) {
const auto key = field.key;

std::uint64_t attributeIndex;
if (field.value.get_uint64().get(attributeIndex) != SUCCESS) FASTGLTF_UNLIKELY {
std::uint64_t accessorIndex;
if (field.value.get_uint64().get(accessorIndex) != SUCCESS) FASTGLTF_UNLIKELY {
return Error::InvalidGltf;
}
attributes.emplace_back(
std::make_pair(FASTGLTF_CONSTRUCT_PMR_RESOURCE(FASTGLTF_STD_PMR_NS::string, resourceAllocator.get(), key), static_cast<std::size_t>(attributeIndex)));
attributes.emplace_back(Attribute {
.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(FASTGLTF_STD_PMR_NS::string, resourceAllocator.get(), key),
.accessorIndex = static_cast<std::size_t>(accessorIndex),
});
}
return Error::None;
}

// TODO: Is there some nicer way of declaring a templated version parseAttributes?
// Currently, this exists because resourceAllocator is a optional field of Parser, which we can't unconditionally
// pass as a parameter to a function, so parseAttributes needs to be a member function of Parser.
template fg::Error fg::Parser::parseAttributes(simdjson::dom::object&, FASTGLTF_STD_PMR_NS::vector<attribute_type>&);
template fg::Error fg::Parser::parseAttributes(simdjson::dom::object&, FASTGLTF_STD_PMR_NS::vector<Attribute>&);
template fg::Error fg::Parser::parseAttributes(simdjson::dom::object&, decltype(fastgltf::Primitive::attributes)&);

namespace fastgltf {
Expand Down Expand Up @@ -886,7 +888,7 @@ fg::Error fg::Parser::generateMeshIndices(fastgltf::Asset& asset) const {
if (positionAttribute == primitive.attributes.end()) {
return Error::InvalidGltf;
}
auto positionCount = asset.accessors[positionAttribute->second].count;
auto positionCount = asset.accessors[positionAttribute->accessorIndex].count;

auto primitiveCount = [&]() -> std::size_t {
switch (primitive.type) {
Expand Down Expand Up @@ -4754,7 +4756,7 @@ void fg::Exporter::writeMeshes(const Asset& asset, std::string& json) {
{
json += R"("attributes":{)";
for (auto ita = itp->attributes.begin(); ita != itp->attributes.end(); ++ita) {
json += '"' + std::string(ita->first) + "\":" + std::to_string(ita->second);
json += '"' + std::string(ita->name) + "\":" + std::to_string(ita->accessorIndex);
if (uabs(std::distance(itp->attributes.begin(), ita)) + 1 <itp->attributes.size())
json += ',';
}
Expand Down Expand Up @@ -4929,7 +4931,7 @@ void fg::Exporter::writeNodes(const Asset& asset, std::string& json) {
if (!it->instancingAttributes.empty()) {
json += R"("EXT_mesh_gpu_instancing":{"attributes":{)";
for (auto ait = it->instancingAttributes.begin(); ait != it->instancingAttributes.end(); ++ait) {
json += '"' + std::string(ait->first) + "\":" + std::to_string(ait->second);
json += '"' + std::string(ait->name) + "\":" + std::to_string(ait->accessorIndex);
if (uabs(std::distance(it->instancingAttributes.begin(), ait)) + 1 <
it->instancingAttributes.size())
json += ',';
Expand Down

0 comments on commit 36f612d

Please sign in to comment.