Skip to content

Commit

Permalink
Fix: Keep copied JSON data alive for GLBs
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Dec 24, 2022
1 parent c3809b9 commit 2791095
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ namespace fastgltf {
constexpr auto hashTextures = crc32("textures");

struct ParserData {
// Can simdjson not store this data itself?
std::vector<uint8_t> bytes;
simdjson::padded_string json; // Only used to keep the copied json data with GLBs alive.
simdjson::ondemand::document doc;
simdjson::ondemand::object root;

Expand All @@ -64,8 +63,9 @@ namespace fastgltf {
void* userPointer = nullptr;
};

// ASCII for "glTF".
constexpr uint32_t binaryGltfHeaderMagic = 0x46546C67;
constexpr uint32_t binaryGltfHeaderMagic = 0x46546C67; // ASCII for "glTF".
constexpr uint32_t binaryGltfJsonChunkMagic = 0x4E4F534A;
constexpr uint32_t binaryGltfDataChunkMagic = 0x004E4942;

struct BinaryGltfHeader {
uint32_t magic;
Expand Down Expand Up @@ -1957,22 +1957,24 @@ std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs:
// 2. BIN chunk (optional)
BinaryGltfChunk jsonChunk = {};
read(&jsonChunk, sizeof jsonChunk);
if (jsonChunk.chunkType != 0x4E4F534A) {
if (jsonChunk.chunkType != binaryGltfJsonChunkMagic) {
errorCode = Error::InvalidGLB;
return nullptr;
}

// TODO: Keep this alive somehow
simdjson::padded_string jsonString(jsonChunk.chunkLength); // This adds the padding itself.
read(jsonString.data(), jsonChunk.chunkLength);

if (hasBit(options, Options::DontUseSIMD)) {
simdjson::get_active_implementation() = simdjson::get_available_implementations()["fallback"];
}

// The 'false' indicates that simdjson doesn't have to copy the data internally.
auto data = std::make_unique<ParserData>();
if (jsonParser->iterate(jsonString).get(data->doc) != SUCCESS) {

{
simdjson::padded_string glbJson(jsonChunk.chunkLength); // This adds the padding itself.
read(glbJson.data(), jsonChunk.chunkLength);
data->json = std::move(glbJson);
}

if (jsonParser->iterate(simdjson::padded_string_view(data->json)).get(data->doc) != SUCCESS) {
errorCode = Error::InvalidJson;
return nullptr;
}
Expand All @@ -1991,7 +1993,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs:
BinaryGltfChunk binaryChunk = {};
read(&binaryChunk, sizeof binaryChunk);

if (binaryChunk.chunkType != 0x004E4942) {
if (binaryChunk.chunkType != binaryGltfDataChunkMagic) {
errorCode = Error::InvalidGLB;
return nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions src/fastgltf_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ namespace fastgltf {
void parseScenes(simdjson::fallback::ondemand::array array);
void parseSkins(simdjson::fallback::ondemand::array array);
void parseTextures(simdjson::fallback::ondemand::array array);

public:
explicit glTF(const glTF& scene) = delete;
glTF& operator=(const glTF& scene) = delete;
Expand Down

0 comments on commit 2791095

Please sign in to comment.