Skip to content

Commit

Permalink
Extract the JSON writing into a writeJson function
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Jan 3, 2024
1 parent 0245efc commit 2147a75
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
11 changes: 10 additions & 1 deletion include/fastgltf/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ namespace fastgltf {
std::filesystem::path getBufferFilePath(const Asset& asset, std::size_t index);
std::filesystem::path getImageFilePath(const Asset& asset, std::size_t index, MimeType mimeType);

std::string writeJson(const Asset& asset);

public:
/**
* Sets the base folder where images URIs should be based of from,
Expand All @@ -858,9 +860,13 @@ namespace fastgltf {
void setImagePath(std::filesystem::path folder);

/**
* Converts the Asset into a glTF JSON string.
* Generates a glTF JSON string from the given asset.
*/
Expected<ExportResult<std::string>> writeGltfJson(const Asset& asset, ExportOptions options = ExportOptions::None);

/**
* Generates a glTF binary (GLB) blob from the given asset.
*/
Expected<ExportResult<std::vector<std::byte>>> writeGltfBinary(const Asset& asset, ExportOptions options = ExportOptions::None);
};

Expand All @@ -874,6 +880,9 @@ namespace fastgltf {
using Exporter::writeGltfBinary;

public:
/**
* Writes a glTF JSON string generated from the given asset to the file pointed to by target.
*/
Error writeGltfJson(const Asset& asset, std::filesystem::path target, ExportOptions options = ExportOptions::None);
Error writeGltfBinary(const Asset& asset, std::filesystem::path target, ExportOptions options = ExportOptions::None);
};
Expand Down
50 changes: 27 additions & 23 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4342,18 +4342,7 @@ fs::path fg::Exporter::getImageFilePath(const Asset& asset, std::size_t index, M
return imageFolder / (name + std::to_string(index) + std::string(extension));
}

fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const Asset& asset, ExportOptions nOptions) {
bufferPaths.clear();
imagePaths.clear();
options = nOptions;

if (hasBit(options, ExportOptions::ValidateAsset)) {
auto validation = validate(asset);
if (validation != Error::None) {
return Expected<ExportResult<std::string>>{errorCode};
}
}

std::string fg::Exporter::writeJson(const fastgltf::Asset &asset) {
// Fairly rudimentary approach of just composing the JSON string using a std::string.
std::string outputString;

Expand Down Expand Up @@ -4393,6 +4382,23 @@ fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const As
prettyPrintJson(outputString);
}

return outputString;
}

fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const Asset& asset, ExportOptions nOptions) {
bufferPaths.clear();
imagePaths.clear();
options = nOptions;

if (hasBit(options, ExportOptions::ValidateAsset)) {
auto validation = validate(asset);
if (validation != Error::None) {
return Expected<ExportResult<std::string>>{errorCode};
}
}

// Fairly rudimentary approach of just composing the JSON string using a std::string.
std::string outputString = writeJson(asset);
if (errorCode != Error::None) {
return Expected<ExportResult<std::string>> { errorCode };
}
Expand All @@ -4413,21 +4419,19 @@ fg::Expected<fg::ExportResult<std::vector<std::byte>>> fg::Exporter::writeGltfBi
options &= (~ExportOptions::PrettyPrintJson);

ExportResult<std::vector<std::byte>> result;

// Generate the JSON.
auto expected = writeGltfJson(asset, options);
if (expected.error() != Error::None) {
return Expected<ExportResult<std::vector<std::byte>>> { expected.error() };
auto json = writeJson(asset);
if (errorCode != Error::None) {
return Expected<ExportResult<std::vector<std::byte>>> { errorCode };
}
auto& json = expected.get();
result.bufferPaths = std::move(json.bufferPaths);
result.imagePaths = std::move(json.imagePaths);

result.bufferPaths = std::move(bufferPaths);
result.imagePaths = std::move(imagePaths);

const bool withEmbeddedBuffer = !asset.buffers.empty()
&& std::get_if<sources::Vector>(&asset.buffers.front().data) != nullptr // We only support writing Vectors as embedded buffers
&& asset.buffers.front().byteLength < std::numeric_limits<decltype(BinaryGltfChunk::chunkLength)>::max();

std::size_t binarySize = sizeof(BinaryGltfHeader) + sizeof(BinaryGltfChunk) + json.output.size();
std::size_t binarySize = sizeof(BinaryGltfHeader) + sizeof(BinaryGltfChunk) + json.size();
if (withEmbeddedBuffer) {
binarySize += sizeof(BinaryGltfChunk) + asset.buffers.front().byteLength;
}
Expand All @@ -4446,10 +4450,10 @@ fg::Expected<fg::ExportResult<std::vector<std::byte>>> fg::Exporter::writeGltfBi

BinaryGltfChunk jsonChunk;
jsonChunk.chunkType = binaryGltfJsonChunkMagic;
jsonChunk.chunkLength = json.output.size();
jsonChunk.chunkLength = json.size();
write(&jsonChunk, sizeof jsonChunk);

write(json.output.data(), json.output.size() * sizeof(decltype(json.output)::value_type));
write(json.data(), json.size() * sizeof(decltype(json)::value_type));

if (withEmbeddedBuffer) {
const auto& buffer = asset.buffers.front();
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ TEST_CASE("Compare base64 decoding performance", "[gltf-benchmark]") {
}
#elif defined(FASTGLTF_IS_A64)
const auto& impls = simdjson::get_available_implementations();
if (const auto* neon = impls["arm64"]; avx2 != nullptr && neon->supported_by_runtime_system()) {
if (const auto* neon = impls["arm64"]; neon != nullptr && neon->supported_by_runtime_system()) {
BENCHMARK("Run fastgltf's Neon base64 decoder") {
return fastgltf::base64::neon_decode(generatedData);
};
Expand Down

0 comments on commit 2147a75

Please sign in to comment.