mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-11 20:53:50 +00:00
Compare commits
30 Commits
copilot/su
...
v3.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
135695e918 | ||
|
|
b163ff225a | ||
|
|
1a04c114c6 | ||
|
|
b5a962f1f4 | ||
|
|
f143766625 | ||
|
|
1215adc13a | ||
|
|
826b71cc24 | ||
|
|
dfd94f03fb | ||
|
|
131c4489fa | ||
|
|
9248070755 | ||
|
|
d8fb6cad78 | ||
|
|
e2e40f58ae | ||
|
|
306c72fce9 | ||
|
|
594c3a057b | ||
|
|
ad316367b9 | ||
|
|
1f15c2d140 | ||
|
|
1d5e721a24 | ||
|
|
5e0c5b9ada | ||
|
|
03b9db782e | ||
|
|
c99e713fab | ||
|
|
8c8cbfa0ba | ||
|
|
0949983acc | ||
|
|
b76cf7aa21 | ||
|
|
946c5a2d9b | ||
|
|
97316e140c | ||
|
|
0e370ef62f | ||
|
|
12affdcc64 | ||
|
|
2c1a8be82d | ||
|
|
df3efc6453 | ||
|
|
99720ea0cc |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
# CMake
|
||||
/build/
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
@@ -71,6 +72,7 @@ imgui.ini
|
||||
loader_example
|
||||
tests/tester
|
||||
tests/tester_noexcept
|
||||
tests/tester_intensive_customjson
|
||||
tests/issue-97.gltf
|
||||
tests/issue-261.gltf
|
||||
tests/issue-495-external.gltf
|
||||
|
||||
@@ -56,6 +56,15 @@ if (TINYGLTF_BUILD_TESTS)
|
||||
)
|
||||
target_compile_definitions(tester_customjson PRIVATE TINYGLTF_USE_CUSTOM_JSON)
|
||||
add_test(NAME tester_customjson COMMAND tester_customjson WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
|
||||
# Intensive parser tests for the custom JSON backend
|
||||
add_executable(tester_intensive_customjson tests/tester_intensive_customjson.cc)
|
||||
target_include_directories(tester_intensive_customjson PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests
|
||||
)
|
||||
target_compile_definitions(tester_intensive_customjson PRIVATE TINYGLTF_USE_CUSTOM_JSON)
|
||||
add_test(NAME tester_intensive_customjson COMMAND tester_intensive_customjson WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
endif (TINYGLTF_BUILD_TESTS)
|
||||
|
||||
#
|
||||
|
||||
53
README.md
53
README.md
@@ -1,14 +1,57 @@
|
||||
# Header only C++ tiny glTF library(loader/saver).
|
||||
|
||||
`TinyGLTF` is a header only C++11 glTF 2.0 https://github.com/KhronosGroup/glTF library.
|
||||
`TinyGLTF` is a header only C++ glTF 2.0 https://github.com/KhronosGroup/glTF library.
|
||||
|
||||
`TinyGLTF` uses Niels Lohmann's json library (https://github.com/nlohmann/json), so now it requires C++11 compiler.
|
||||
(Also, you can use RadpidJSON as an JSON backend)
|
||||
If you are looking for old, C++03 version, please use `devel-picojson` branch (but not maintained anymore).
|
||||
## TinyGLTF v3 (new major release)
|
||||
|
||||
**`tiny_gltf_v3.h`** is the new major version of TinyGLTF and the recommended API for new projects.
|
||||
|
||||
### What's new in v3
|
||||
|
||||
v3 is a ground-up rewrite with a C-centric, low-overhead design:
|
||||
|
||||
- **Pure C POD structs** — no STL containers in the public API; easy to bind to other languages.
|
||||
- **Arena-based memory management** — all parse-time allocations come from a single arena; a single `tg3_model_free()` frees everything.
|
||||
- **Structured error reporting** — `tg3_error_stack` provides machine-readable errors with severity levels and source locations.
|
||||
- **Custom JSON backend** — backed by `tinygltf_json.h`, a high-performance, locale-independent JSON parser with optional SIMD acceleration (SSE2 / AVX2 / NEON) and a float32 fast-path.
|
||||
- **Streaming callbacks** — opt-in streaming parse/write via user-supplied callbacks.
|
||||
- **No RTTI, no exceptions required** — suitable for embedded and game-engine use.
|
||||
- **Opt-in filesystem and image I/O** — `TINYGLTF3_ENABLE_FS` / `TINYGLTF3_ENABLE_STB_IMAGE` are off by default; you control when and how assets are loaded.
|
||||
- **C++20 coroutine facade** (optional, auto-detected). C17/C++17 default.
|
||||
|
||||
### Quick start (v3)
|
||||
|
||||
Copy `tiny_gltf_v3.h` and `tinygltf_json.h` to your project. In **one** `.cpp` file:
|
||||
|
||||
```cpp
|
||||
#define TINYGLTF3_IMPLEMENTATION
|
||||
#define TINYGLTF3_ENABLE_FS // enable file I/O
|
||||
#define TINYGLTF3_ENABLE_STB_IMAGE // enable image decoding
|
||||
#include "tiny_gltf_v3.h"
|
||||
```
|
||||
|
||||
Loading a glTF file:
|
||||
|
||||
```c
|
||||
tg3_load_options_t opts = tg3_load_options_default();
|
||||
tg3_error_stack_t errors = {0};
|
||||
tg3_model_t *model = tg3_load_from_file("scene.gltf", &opts, &errors);
|
||||
if (!model) {
|
||||
for (int i = 0; i < errors.count; i++)
|
||||
fprintf(stderr, "[%s] %s\n", tg3_severity_str(errors.items[i].severity),
|
||||
errors.items[i].message);
|
||||
}
|
||||
// ... use model ...
|
||||
tg3_model_free(model);
|
||||
```
|
||||
|
||||
## Status
|
||||
|
||||
Currently TinyGLTF is stable and maintenance mode. No drastic changes and feature additions planned.
|
||||
> ⚠️ **v2 deprecation notice:** `tiny_gltf.h` (v2) remains fully functional and is still supported,
|
||||
> but it is now in **maintenance mode only** — no new features will be added.
|
||||
> v2 will be **sunset after mid-2026**. New projects should use `tiny_gltf_v3.h`.
|
||||
|
||||
Currently TinyGLTF v2 is stable and in maintenance mode. No drastic changes and feature additions planned.
|
||||
- v2.9.0 Various fixes and improvements. Filesystem callback API change.
|
||||
- v2.8.0 Add URICallbacks for custom URI handling in Buffer and Image. PR#397
|
||||
- v2.7.0 Change WriteImageDataFunction user callback function signature. PR#393
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
all: ../tiny_gltf.h
|
||||
clang++ -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester tester.cc
|
||||
clang++ -DTINYGLTF_NOEXCEPTION -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester_noexcept tester.cc
|
||||
clang++ -DTINYGLTF_USE_CUSTOM_JSON -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester_intensive_customjson tester_intensive_customjson.cc
|
||||
|
||||
@@ -4,9 +4,14 @@ Do fuzzing test for TinyGLTF API.
|
||||
|
||||
## Supported API
|
||||
|
||||
* [x] LoadASCIIFromMemory
|
||||
* [x] LoadASCIIFromString
|
||||
* [ ] LoadBinaryFromMemory
|
||||
|
||||
### Custom JSON backend (`tinygltf_json.h`)
|
||||
|
||||
* [x] LoadASCIIFromString
|
||||
* [x] LoadBinaryFromMemory
|
||||
|
||||
## Requirements
|
||||
|
||||
* meson
|
||||
@@ -36,11 +41,17 @@ $ cd build
|
||||
$ ninja
|
||||
```
|
||||
|
||||
This builds two fuzzers:
|
||||
|
||||
* `fuzz_gltf` – default nlohmann/json backend
|
||||
* `fuzz_gltf_customjson` – custom `tinygltf_json.h` backend (tests both ASCII and binary parsing paths)
|
||||
|
||||
## How to run
|
||||
|
||||
Increase memory limit. e.g. `-rss_limit_mb=50000`
|
||||
|
||||
```
|
||||
$ ./fuzz_gltf -rss_limit_mb=20000 -jobs 4
|
||||
$ ./fuzz_gltf_customjson -rss_limit_mb=20000 -jobs 4
|
||||
```
|
||||
|
||||
|
||||
76
tests/fuzzer/fuzz_gltf_customjson.cc
Normal file
76
tests/fuzzer/fuzz_gltf_customjson.cc
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* LLVM libFuzzer harness for tinygltf with the custom JSON backend
|
||||
* (tinygltf_json.h).
|
||||
*
|
||||
* Exercises:
|
||||
* 1. LoadASCIIFromString – glTF JSON parsing
|
||||
* 2. LoadBinaryFromMemory – GLB binary parsing
|
||||
*
|
||||
* Build (clang with libFuzzer):
|
||||
* clang++ -std=c++11 -fsanitize=address,fuzzer \
|
||||
* -DTINYGLTF_USE_CUSTOM_JSON \
|
||||
* -I../../ fuzz_gltf_customjson.cc \
|
||||
* -o fuzz_gltf_customjson
|
||||
*
|
||||
* Run:
|
||||
* ./fuzz_gltf_customjson -rss_limit_mb=20000 -jobs 4
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#define TINYGLTF_IMPLEMENTATION
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
#define TINYGLTF_USE_CUSTOM_JSON
|
||||
#endif
|
||||
#include "tiny_gltf.h"
|
||||
|
||||
/* Fuzz the ASCII (JSON) parser path */
|
||||
static void fuzz_ascii(const uint8_t *data, size_t size) {
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
const char *str = reinterpret_cast<const char *>(data);
|
||||
|
||||
bool ret =
|
||||
ctx.LoadASCIIFromString(&model, &err, &warn, str,
|
||||
static_cast<unsigned int>(size), /* base_dir */ "");
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
/* Fuzz the binary (GLB) parser path */
|
||||
static void fuzz_binary(const uint8_t *data, size_t size) {
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
bool ret = ctx.LoadBinaryFromMemory(&model, &err, &warn, data,
|
||||
static_cast<unsigned int>(size),
|
||||
/* base_dir */ "");
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
if (size == 0) return 0;
|
||||
|
||||
/* Use the lowest bit of the first byte to select the parse path.
|
||||
* The remaining bits are left for the fuzzer engine to explore;
|
||||
* additional paths (e.g. LoadASCIIFromFile, check_sections flags)
|
||||
* can be added here in the future using more selector bits. */
|
||||
uint8_t selector = data[0];
|
||||
const uint8_t *payload = data + 1;
|
||||
size_t payload_size = size - 1;
|
||||
|
||||
if (selector & 1) {
|
||||
fuzz_binary(payload, payload_size);
|
||||
} else {
|
||||
fuzz_ascii(payload, payload_size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -7,3 +7,9 @@ executable('fuzz_gltf',
|
||||
cpp_args : '-fsanitize=address,fuzzer',
|
||||
link_args : '-fsanitize=address,fuzzer' )
|
||||
|
||||
executable('fuzz_gltf_customjson',
|
||||
'fuzz_gltf_customjson.cc',
|
||||
include_directories : incdirs,
|
||||
cpp_args : ['-fsanitize=address,fuzzer', '-DTINYGLTF_USE_CUSTOM_JSON'],
|
||||
link_args : '-fsanitize=address,fuzzer' )
|
||||
|
||||
|
||||
1089
tests/tester_intensive_customjson.cc
Normal file
1089
tests/tester_intensive_customjson.cc
Normal file
File diff suppressed because it is too large
Load Diff
@@ -47,7 +47,7 @@
|
||||
* Section 2: Configuration Macros
|
||||
* ====================================================================== */
|
||||
|
||||
/* Build mode: define in ONE .c/.cpp translation unit */
|
||||
/* Build mode: define in ONE C++ translation unit (.cpp) */
|
||||
/* #define TINYGLTF3_IMPLEMENTATION */
|
||||
|
||||
/* Opt-in features (OFF by default) */
|
||||
@@ -1221,17 +1221,19 @@ ParseGenerator tg3_parse_coro(
|
||||
|
||||
#ifdef TINYGLTF3_IMPLEMENTATION
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
#error "TINYGLTF3_IMPLEMENTATION requires a C++ translation unit (compile as .cpp)"
|
||||
#endif
|
||||
|
||||
/* Include JSON parser */
|
||||
#include "tinygltf_json.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
/* Implementation uses C++ features from tinygltf_json.h */
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#endif
|
||||
|
||||
/* Forward SIMD macros to tinygltf_json.h */
|
||||
#ifdef TINYGLTF3_JSON_SIMD_SSE2
|
||||
@@ -1358,10 +1360,13 @@ static void *tg3__arena_alloc(tg3_arena *arena, size_t size) {
|
||||
}
|
||||
|
||||
static char *tg3__arena_strdup(tg3_arena *arena, const char *s, size_t len) {
|
||||
if (!s || len == 0) return NULL;
|
||||
if (!s) return NULL;
|
||||
/* Allocate len+1 bytes; when len==0 this produces a 1-byte "\0" buffer so
|
||||
* that empty strings (data!=NULL, len==0) remain distinguishable from
|
||||
* absent strings (data==NULL, len==0). */
|
||||
char *dst = (char *)tg3__arena_alloc(arena, len + 1);
|
||||
if (!dst) return NULL;
|
||||
memcpy(dst, s, len);
|
||||
if (len > 0) memcpy(dst, s, len);
|
||||
dst[len] = '\0';
|
||||
return dst;
|
||||
}
|
||||
@@ -2931,6 +2936,22 @@ static int tg3__parse_audio_emitter(tg3__parse_ctx *ctx, const tg3__json &o,
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ======================================================================
|
||||
* Internal: Portable variadic-comma helper
|
||||
*
|
||||
* TG3__COMMA_VA_ARGS(__VA_ARGS__) expands to , __VA_ARGS__ when the
|
||||
* argument list is non-empty, and to nothing when it is empty.
|
||||
*
|
||||
* - C++20 and later: uses the standard __VA_OPT__(,) token.
|
||||
* - C++17 and earlier: falls back to the widely-supported GNU/MSVC
|
||||
* ##__VA_ARGS__ extension.
|
||||
* ====================================================================== */
|
||||
#if __cplusplus >= 202002L
|
||||
# define TG3__COMMA_VA_ARGS(...) __VA_OPT__(,) __VA_ARGS__
|
||||
#else
|
||||
# define TG3__COMMA_VA_ARGS(...) , ##__VA_ARGS__
|
||||
#endif
|
||||
|
||||
/* ======================================================================
|
||||
* Internal: Array Parse Macro
|
||||
* ====================================================================== */
|
||||
@@ -2946,7 +2967,7 @@ static int tg3__parse_audio_emitter(tg3__parse_ctx *ctx, const tg3__json &o,
|
||||
if (_items) { \
|
||||
uint32_t _i = 0; \
|
||||
for (auto _it = _arr_it->begin(); _it != _arr_it->end(); ++_it, ++_i) { \
|
||||
parse_fn((ctx), *_it, &_items[_i], ##__VA_ARGS__); \
|
||||
parse_fn((ctx), *_it, &_items[_i] TG3__COMMA_VA_ARGS(__VA_ARGS__)); \
|
||||
} \
|
||||
(model_field) = _items; \
|
||||
(count_field) = _count; \
|
||||
@@ -4117,7 +4138,7 @@ static tg3__json tg3__serialize_model(const tg3_model *model, int wd,
|
||||
if ((arr) && (cnt) > 0) { \
|
||||
tg3__json jarr; jarr.set_array(); \
|
||||
for (uint32_t _i = 0; _i < (cnt); ++_i) { \
|
||||
jarr.push_back(fn(&(arr)[_i], ##__VA_ARGS__)); \
|
||||
jarr.push_back(fn(&(arr)[_i] TG3__COMMA_VA_ARGS(__VA_ARGS__))); \
|
||||
} \
|
||||
root[key] = static_cast<tg3__json&&>(jarr); \
|
||||
}
|
||||
@@ -4332,7 +4353,7 @@ struct tg3_writer {
|
||||
TINYGLTF3_API tg3_writer *tg3_writer_create(
|
||||
tg3_write_chunk_fn chunk_fn, void *user_data,
|
||||
const tg3_write_options *options) {
|
||||
tg3_writer *w = (tg3_writer *)calloc(1, sizeof(tg3_writer));
|
||||
tg3_writer *w = new (std::nothrow) tg3_writer();
|
||||
if (!w) return NULL;
|
||||
w->chunk_fn = chunk_fn;
|
||||
w->user_data = user_data;
|
||||
@@ -4358,7 +4379,7 @@ TINYGLTF3_API tg3_error_code tg3_writer_begin(tg3_writer *w, const tg3_asset *as
|
||||
w->root[json_key] = static_cast<tg3__json&&>(arr); \
|
||||
} \
|
||||
w->root[json_key].push_back( \
|
||||
serialize_fn(item, w->options.serialize_defaults, ##__VA_ARGS__)); \
|
||||
serialize_fn(item, w->options.serialize_defaults TG3__COMMA_VA_ARGS(__VA_ARGS__))); \
|
||||
return TG3_OK; \
|
||||
}
|
||||
|
||||
@@ -4408,10 +4429,7 @@ TINYGLTF3_API tg3_error_code tg3_writer_end(tg3_writer *w) {
|
||||
}
|
||||
|
||||
TINYGLTF3_API void tg3_writer_destroy(tg3_writer *w) {
|
||||
if (w) {
|
||||
w->root.~tinygltf_json();
|
||||
free(w);
|
||||
}
|
||||
delete w;
|
||||
}
|
||||
|
||||
#endif /* TINYGLTF3_IMPLEMENTATION */
|
||||
|
||||
@@ -308,7 +308,8 @@ static const char *cj_scan_str(const char *p, const char *end) {
|
||||
* Breaks strict JSON/IEEE-754-double conformance.
|
||||
* ====================================================================== */
|
||||
|
||||
/* Safe double-to-int64 cast: clamp inf/NaN/out-of-range to 0. */
|
||||
/* Safe double-to-int64 cast: returns 0 for NaN; clamps +inf/out-of-range-high
|
||||
* to INT64_MAX and -inf/out-of-range-low to INT64_MIN. */
|
||||
static int64_t cj_dbl_to_i64(double d) {
|
||||
if (d != d) return 0; /* NaN */
|
||||
if (d >= (double)INT64_MAX) return INT64_MAX;
|
||||
|
||||
Reference in New Issue
Block a user