mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-08 03:03:50 +00:00
3
.gitignore
vendored
3
.gitignore
vendored
@@ -22,6 +22,9 @@ premake5.tar.gz
|
||||
*.vcxproj*
|
||||
.vs
|
||||
|
||||
# default cmake build dir
|
||||
build/
|
||||
|
||||
#binary directories
|
||||
bin/
|
||||
obj/
|
||||
|
||||
49
README.md
49
README.md
@@ -6,9 +6,56 @@
|
||||
(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).
|
||||
|
||||
### 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
|
||||
|
||||
70
benchmark/Makefile
Normal file
70
benchmark/Makefile
Normal file
@@ -0,0 +1,70 @@
|
||||
# benchmark/Makefile — Build and run tinygltf v3 benchmarks
|
||||
#
|
||||
# Targets:
|
||||
# make — build gen_synthetic + bench_v3
|
||||
# make generate — generate synthetic test scenes
|
||||
# make run — run benchmarks on all generated scenes
|
||||
# make report — run benchmarks and produce CSV report
|
||||
# make clean — remove binaries and generated scenes
|
||||
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -O2 -std=c++17 -Wall -Wextra -Wno-unused-function
|
||||
CXXFLAGS += -fno-rtti -fno-exceptions
|
||||
INCLUDES = -I..
|
||||
|
||||
BINDIR = .
|
||||
GEN = $(BINDIR)/gen_synthetic
|
||||
BENCH_V3 = $(BINDIR)/bench_v3
|
||||
|
||||
# Iteration counts
|
||||
ITERATIONS ?= 10
|
||||
WARMUP ?= 2
|
||||
PREFIX ?= synthetic
|
||||
|
||||
.PHONY: all generate run report clean
|
||||
|
||||
all: $(GEN) $(BENCH_V3)
|
||||
|
||||
$(GEN): gen_synthetic.cpp
|
||||
$(CXX) $(CXXFLAGS) -o $@ $<
|
||||
|
||||
$(BENCH_V3): bench_v3.cpp ../tiny_gltf_v3.h ../tinygltf_json.h
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $<
|
||||
|
||||
# Generate synthetic scenes of varying sizes
|
||||
generate: $(GEN)
|
||||
@echo "=== Generating synthetic scenes ==="
|
||||
./$(GEN) --prefix $(PREFIX)
|
||||
@echo ""
|
||||
@echo "Generated files (binary + GLB):"
|
||||
@ls -lh $(PREFIX)_*.gltf $(PREFIX)_*.glb $(PREFIX)_*.bin 2>/dev/null || true
|
||||
|
||||
# Run benchmarks on all generated scenes
|
||||
run: $(BENCH_V3) generate
|
||||
@echo ""
|
||||
@echo "================================================================="
|
||||
@echo " tinygltf v3 Benchmark"
|
||||
@echo "================================================================="
|
||||
@echo ""
|
||||
@for f in $(PREFIX)_*.glb $(PREFIX)_*.gltf; do \
|
||||
if [ -f "$$f" ]; then \
|
||||
./$(BENCH_V3) "$$f" --iterations $(ITERATIONS) --warmup $(WARMUP); \
|
||||
echo ""; \
|
||||
fi; \
|
||||
done
|
||||
|
||||
# Run benchmarks and produce CSV report
|
||||
report: $(BENCH_V3) generate
|
||||
@echo "file,size_bytes,iterations,parse_min_ms,parse_max_ms,parse_avg_ms,parse_median_ms,throughput_mbs,arena_peak_bytes,meshes,nodes,accessors,materials,animations" > benchmark_report.csv
|
||||
@for f in $(PREFIX)_*.glb $(PREFIX)_*.gltf; do \
|
||||
if [ -f "$$f" ]; then \
|
||||
./$(BENCH_V3) "$$f" --iterations $(ITERATIONS) --warmup $(WARMUP) --csv | tail -1 >> benchmark_report.csv; \
|
||||
fi; \
|
||||
done
|
||||
@echo "=== Report written to benchmark_report.csv ==="
|
||||
@cat benchmark_report.csv | column -t -s,
|
||||
|
||||
clean:
|
||||
rm -f $(GEN) $(BENCH_V3)
|
||||
rm -f $(PREFIX)_*.gltf $(PREFIX)_*.glb $(PREFIX)_*.bin
|
||||
rm -f benchmark_report.csv
|
||||
396
benchmark/bench_v3.cpp
Normal file
396
benchmark/bench_v3.cpp
Normal file
@@ -0,0 +1,396 @@
|
||||
/*
|
||||
* bench_v3.cpp — Benchmark tinygltf v3 parser: parse speed & memory.
|
||||
*
|
||||
* Measures:
|
||||
* - File read time
|
||||
* - JSON parse + model build time
|
||||
* - Peak arena memory usage
|
||||
* - Throughput (MB/s)
|
||||
*
|
||||
* Usage:
|
||||
* bench_v3 <file.gltf|file.glb> [--iterations N] [--warmup N] [--quiet]
|
||||
* bench_v3 --batch <file1> <file2> ... [--iterations N]
|
||||
*/
|
||||
|
||||
#define TINYGLTF3_IMPLEMENTATION
|
||||
#define TINYGLTF3_ENABLE_FS
|
||||
#include "tiny_gltf_v3.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Timing helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
using TimePoint = Clock::time_point;
|
||||
|
||||
static double elapsed_ms(TimePoint start, TimePoint end) {
|
||||
return std::chrono::duration<double, std::milli>(end - start).count();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Memory tracking allocator */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct MemTracker {
|
||||
size_t current;
|
||||
size_t peak;
|
||||
size_t total_allocs;
|
||||
size_t total_frees;
|
||||
};
|
||||
|
||||
static void *tracked_alloc(size_t size, void *ud) {
|
||||
MemTracker *mt = (MemTracker *)ud;
|
||||
void *ptr = malloc(size);
|
||||
if (ptr) {
|
||||
mt->current += size;
|
||||
if (mt->current > mt->peak) mt->peak = mt->current;
|
||||
mt->total_allocs++;
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void *tracked_realloc(void *ptr, size_t old_size, size_t new_size, void *ud) {
|
||||
MemTracker *mt = (MemTracker *)ud;
|
||||
void *new_ptr = realloc(ptr, new_size);
|
||||
if (new_ptr) {
|
||||
mt->current -= old_size;
|
||||
mt->current += new_size;
|
||||
if (mt->current > mt->peak) mt->peak = mt->current;
|
||||
}
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
static void tracked_free(void *ptr, size_t size, void *ud) {
|
||||
MemTracker *mt = (MemTracker *)ud;
|
||||
if (ptr) {
|
||||
mt->current -= size;
|
||||
mt->total_frees++;
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* RSS measurement (Linux) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static size_t get_rss_bytes() {
|
||||
#if defined(__linux__)
|
||||
FILE *f = fopen("/proc/self/statm", "r");
|
||||
if (!f) return 0;
|
||||
long pages = 0;
|
||||
if (fscanf(f, "%*s %ld", &pages) != 1) pages = 0;
|
||||
fclose(f);
|
||||
return (size_t)pages * 4096;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Benchmark result */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct BenchResult {
|
||||
std::string filename;
|
||||
uint64_t file_size;
|
||||
int iterations;
|
||||
|
||||
/* Parse timing (ms) */
|
||||
double parse_min;
|
||||
double parse_max;
|
||||
double parse_avg;
|
||||
double parse_median;
|
||||
|
||||
/* Memory */
|
||||
size_t arena_peak; /* Peak arena allocation */
|
||||
size_t rss_before;
|
||||
size_t rss_after;
|
||||
|
||||
/* Model stats */
|
||||
uint32_t meshes;
|
||||
uint32_t nodes;
|
||||
uint32_t accessors;
|
||||
uint32_t materials;
|
||||
uint32_t animations;
|
||||
uint32_t buffers;
|
||||
uint32_t buffer_views;
|
||||
uint32_t images;
|
||||
uint32_t textures;
|
||||
|
||||
/* Derived */
|
||||
double throughput_mbs; /* MB/s based on median */
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Run benchmark for a single file */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static BenchResult bench_file(const char *filename, int iterations, int warmup,
|
||||
bool quiet, int float32_mode = 0) {
|
||||
BenchResult r = {};
|
||||
r.filename = filename;
|
||||
r.iterations = iterations;
|
||||
|
||||
/* Read file into memory */
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "ERROR: Cannot open '%s'\n", filename);
|
||||
return r;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (sz <= 0) { fclose(f); return r; }
|
||||
|
||||
std::vector<uint8_t> data((size_t)sz);
|
||||
size_t rd = fread(data.data(), 1, (size_t)sz, f);
|
||||
fclose(f);
|
||||
if ((long)rd != sz) { return r; }
|
||||
|
||||
r.file_size = (uint64_t)sz;
|
||||
|
||||
/* Extract base dir */
|
||||
std::string path(filename);
|
||||
std::string base_dir;
|
||||
size_t sep = path.find_last_of("/\\");
|
||||
if (sep != std::string::npos) base_dir = path.substr(0, sep);
|
||||
|
||||
/* Warmup iterations (not measured) */
|
||||
for (int i = 0; i < warmup; ++i) {
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
tg3_parse_auto(&model, &errors, data.data(), data.size(),
|
||||
base_dir.c_str(), (uint32_t)base_dir.size(), NULL);
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
}
|
||||
|
||||
/* Benchmark iterations */
|
||||
std::vector<double> times;
|
||||
times.reserve(iterations);
|
||||
|
||||
MemTracker tracker_best;
|
||||
memset(&tracker_best, 0, sizeof(tracker_best));
|
||||
|
||||
r.rss_before = get_rss_bytes();
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
MemTracker tracker;
|
||||
memset(&tracker, 0, sizeof(tracker));
|
||||
|
||||
tg3_parse_options opts;
|
||||
tg3_parse_options_init(&opts);
|
||||
opts.memory.allocator.alloc = tracked_alloc;
|
||||
opts.memory.allocator.realloc = tracked_realloc;
|
||||
opts.memory.allocator.free = tracked_free;
|
||||
opts.memory.allocator.user_data = &tracker;
|
||||
opts.parse_float32 = float32_mode;
|
||||
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
|
||||
TimePoint t0 = Clock::now();
|
||||
|
||||
tg3_error_code err = tg3_parse_auto(&model, &errors,
|
||||
data.data(), data.size(),
|
||||
base_dir.c_str(),
|
||||
(uint32_t)base_dir.size(),
|
||||
&opts);
|
||||
|
||||
TimePoint t1 = Clock::now();
|
||||
double ms = elapsed_ms(t0, t1);
|
||||
times.push_back(ms);
|
||||
|
||||
/* Capture model stats on first successful iteration */
|
||||
if (i == 0 && err == TG3_OK) {
|
||||
r.meshes = model.meshes_count;
|
||||
r.nodes = model.nodes_count;
|
||||
r.accessors = model.accessors_count;
|
||||
r.materials = model.materials_count;
|
||||
r.animations = model.animations_count;
|
||||
r.buffers = model.buffers_count;
|
||||
r.buffer_views = model.buffer_views_count;
|
||||
r.images = model.images_count;
|
||||
r.textures = model.textures_count;
|
||||
}
|
||||
|
||||
if (tracker.peak > tracker_best.peak) {
|
||||
tracker_best = tracker;
|
||||
}
|
||||
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
|
||||
if (err != TG3_OK && !quiet) {
|
||||
fprintf(stderr, " Parse error on iteration %d: code %d\n", i, (int)err);
|
||||
}
|
||||
}
|
||||
|
||||
r.rss_after = get_rss_bytes();
|
||||
r.arena_peak = tracker_best.peak;
|
||||
|
||||
/* Compute stats */
|
||||
std::sort(times.begin(), times.end());
|
||||
r.parse_min = times.front();
|
||||
r.parse_max = times.back();
|
||||
double sum = 0;
|
||||
for (double t : times) sum += t;
|
||||
r.parse_avg = sum / times.size();
|
||||
r.parse_median = times[times.size() / 2];
|
||||
|
||||
/* Throughput: file_size / median_time */
|
||||
if (r.parse_median > 0) {
|
||||
r.throughput_mbs = ((double)r.file_size / (1024.0 * 1024.0)) /
|
||||
(r.parse_median / 1000.0);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Print results */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static const char *human_bytes(size_t bytes, char *buf, size_t buf_sz) {
|
||||
if (bytes >= 1024ULL * 1024 * 1024)
|
||||
snprintf(buf, buf_sz, "%.2f GB", (double)bytes / (1024.0 * 1024 * 1024));
|
||||
else if (bytes >= 1024 * 1024)
|
||||
snprintf(buf, buf_sz, "%.2f MB", (double)bytes / (1024.0 * 1024));
|
||||
else if (bytes >= 1024)
|
||||
snprintf(buf, buf_sz, "%.2f KB", (double)bytes / 1024.0);
|
||||
else
|
||||
snprintf(buf, buf_sz, "%zu B", bytes);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void print_result(const BenchResult &r) {
|
||||
char buf1[64], buf2[64];
|
||||
|
||||
printf("┌─────────────────────────────────────────────────────────────────┐\n");
|
||||
printf("│ %-63s │\n", r.filename.c_str());
|
||||
printf("├─────────────────────────────────────────────────────────────────┤\n");
|
||||
printf("│ File size: %-47s │\n", human_bytes((size_t)r.file_size, buf1, sizeof(buf1)));
|
||||
printf("│ Iterations: %-47d │\n", r.iterations);
|
||||
printf("│ │\n");
|
||||
printf("│ Parse time (ms): │\n");
|
||||
printf("│ min: %10.3f │\n", r.parse_min);
|
||||
printf("│ max: %10.3f │\n", r.parse_max);
|
||||
printf("│ avg: %10.3f │\n", r.parse_avg);
|
||||
printf("│ median: %10.3f │\n", r.parse_median);
|
||||
printf("│ │\n");
|
||||
printf("│ Throughput: %-47s │\n",
|
||||
(snprintf(buf1, sizeof(buf1), "%.2f MB/s", r.throughput_mbs), buf1));
|
||||
printf("│ Arena peak: %-47s │\n", human_bytes(r.arena_peak, buf1, sizeof(buf1)));
|
||||
if (r.rss_before > 0) {
|
||||
printf("│ RSS before: %-47s │\n", human_bytes(r.rss_before, buf1, sizeof(buf1)));
|
||||
printf("│ RSS after: %-47s │\n", human_bytes(r.rss_after, buf2, sizeof(buf2)));
|
||||
}
|
||||
printf("│ │\n");
|
||||
printf("│ Model: %u meshes, %u nodes, %u accessors, %u materials",
|
||||
r.meshes, r.nodes, r.accessors, r.materials);
|
||||
printf(" │\n");
|
||||
printf("│ %u animations, %u buffers, %u images",
|
||||
r.animations, r.buffers, r.images);
|
||||
printf(" │\n");
|
||||
printf("└─────────────────────────────────────────────────────────────────┘\n");
|
||||
}
|
||||
|
||||
static void print_csv_header() {
|
||||
printf("file,size_bytes,iterations,parse_min_ms,parse_max_ms,parse_avg_ms,"
|
||||
"parse_median_ms,throughput_mbs,arena_peak_bytes,"
|
||||
"meshes,nodes,accessors,materials,animations\n");
|
||||
}
|
||||
|
||||
static void print_csv_row(const BenchResult &r) {
|
||||
printf("%s,%lu,%d,%.3f,%.3f,%.3f,%.3f,%.2f,%zu,%u,%u,%u,%u,%u\n",
|
||||
r.filename.c_str(), (unsigned long)r.file_size, r.iterations,
|
||||
r.parse_min, r.parse_max, r.parse_avg, r.parse_median,
|
||||
r.throughput_mbs, r.arena_peak,
|
||||
r.meshes, r.nodes, r.accessors, r.materials, r.animations);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Main */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void usage() {
|
||||
fprintf(stderr,
|
||||
"Usage:\n"
|
||||
" bench_v3 <file> [--iterations N] [--warmup N] [--csv] [--quiet]\n"
|
||||
" bench_v3 --batch <file1> [file2] ... [--iterations N] [--csv]\n"
|
||||
"\n"
|
||||
"Options:\n"
|
||||
" --iterations N Number of timed parse iterations (default: 10)\n"
|
||||
" --warmup N Number of warmup iterations (default: 2)\n"
|
||||
" --csv Output in CSV format\n"
|
||||
" --quiet Suppress per-iteration error messages\n"
|
||||
" --batch Benchmark multiple files\n"
|
||||
" --float32 Parse JSON floats as float32 (faster, less precise)\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) { usage(); return 1; }
|
||||
|
||||
int iterations = 10;
|
||||
int warmup = 2;
|
||||
bool csv = false;
|
||||
bool quiet = false;
|
||||
int float32_mode = 0;
|
||||
std::vector<std::string> files;
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "--iterations") == 0 && i + 1 < argc) {
|
||||
iterations = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--warmup") == 0 && i + 1 < argc) {
|
||||
warmup = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--csv") == 0) {
|
||||
csv = true;
|
||||
} else if (strcmp(argv[i], "--quiet") == 0) {
|
||||
quiet = true;
|
||||
} else if (strcmp(argv[i], "--float32") == 0) {
|
||||
float32_mode = 1;
|
||||
} else if (strcmp(argv[i], "--batch") == 0) {
|
||||
/* batch mode: just collect files */
|
||||
} else if (argv[i][0] != '-') {
|
||||
files.push_back(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (files.empty()) { usage(); return 1; }
|
||||
|
||||
if (csv) print_csv_header();
|
||||
|
||||
for (const auto &file : files) {
|
||||
if (!csv && !quiet) {
|
||||
printf("Benchmarking: %s (%d iterations, %d warmup%s)\n",
|
||||
file.c_str(), iterations, warmup,
|
||||
float32_mode ? ", float32" : "");
|
||||
}
|
||||
|
||||
BenchResult r = bench_file(file.c_str(), iterations, warmup, quiet, float32_mode);
|
||||
|
||||
if (csv) {
|
||||
print_csv_row(r);
|
||||
} else {
|
||||
print_result(r);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
740
benchmark/gen_synthetic.cpp
Normal file
740
benchmark/gen_synthetic.cpp
Normal file
@@ -0,0 +1,740 @@
|
||||
/*
|
||||
* gen_synthetic.cpp — Generate synthetic glTF 2.0 scenes for benchmarking.
|
||||
*
|
||||
* Produces .gltf (ASCII) and .glb (binary) files with configurable:
|
||||
* - Number of meshes, each with N vertices/triangles
|
||||
* - Number of nodes (flat hierarchy)
|
||||
* - Number of materials
|
||||
* - Number of animations with M keyframes
|
||||
*
|
||||
* Usage:
|
||||
* gen_synthetic [--meshes N] [--verts-per-mesh N] [--nodes N]
|
||||
* [--materials N] [--animations N] [--keyframes N]
|
||||
* [--prefix NAME]
|
||||
*
|
||||
* Outputs: <prefix>_<label>.gltf and <prefix>_<label>.glb
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Tiny JSON writer (no dependencies) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct JsonWriter {
|
||||
std::string buf;
|
||||
int indent;
|
||||
bool need_comma;
|
||||
std::vector<bool> stack; /* true = array context */
|
||||
|
||||
JsonWriter() : indent(0), need_comma(false) {}
|
||||
|
||||
void comma() {
|
||||
if (need_comma) buf += ",";
|
||||
buf += "\n";
|
||||
for (int i = 0; i < indent; ++i) buf += " ";
|
||||
}
|
||||
|
||||
void begin_obj() {
|
||||
if (!stack.empty()) comma();
|
||||
buf += "{";
|
||||
indent++;
|
||||
need_comma = false;
|
||||
stack.push_back(false);
|
||||
}
|
||||
void end_obj() {
|
||||
indent--;
|
||||
buf += "\n";
|
||||
for (int i = 0; i < indent; ++i) buf += " ";
|
||||
buf += "}";
|
||||
stack.pop_back();
|
||||
need_comma = true;
|
||||
}
|
||||
|
||||
void begin_arr() {
|
||||
if (!stack.empty() && !need_comma) { /* first elem */ }
|
||||
buf += "[";
|
||||
indent++;
|
||||
need_comma = false;
|
||||
stack.push_back(true);
|
||||
}
|
||||
void end_arr() {
|
||||
indent--;
|
||||
buf += "\n";
|
||||
for (int i = 0; i < indent; ++i) buf += " ";
|
||||
buf += "]";
|
||||
stack.pop_back();
|
||||
need_comma = true;
|
||||
}
|
||||
|
||||
void key(const char *k) {
|
||||
comma();
|
||||
buf += "\"";
|
||||
buf += k;
|
||||
buf += "\": ";
|
||||
need_comma = false;
|
||||
}
|
||||
|
||||
void val_str(const char *v) {
|
||||
if (stack.back()) comma();
|
||||
buf += "\"";
|
||||
buf += v;
|
||||
buf += "\"";
|
||||
need_comma = true;
|
||||
}
|
||||
void val_int(int64_t v) {
|
||||
if (stack.back()) comma();
|
||||
buf += std::to_string(v);
|
||||
need_comma = true;
|
||||
}
|
||||
void val_double(double v) {
|
||||
if (stack.back()) comma();
|
||||
char tmp[64];
|
||||
snprintf(tmp, sizeof(tmp), "%.6g", v);
|
||||
buf += tmp;
|
||||
need_comma = true;
|
||||
}
|
||||
void val_bool(bool v) {
|
||||
if (stack.back()) comma();
|
||||
buf += v ? "true" : "false";
|
||||
need_comma = true;
|
||||
}
|
||||
|
||||
void kv_str(const char *k, const char *v) { key(k); val_str(v); need_comma = true; }
|
||||
void kv_int(const char *k, int64_t v) { key(k); val_int(v); need_comma = true; }
|
||||
void kv_double(const char *k, double v) { key(k); val_double(v); need_comma = true; }
|
||||
void kv_bool(const char *k, bool v) { key(k); val_bool(v); need_comma = true; }
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Binary buffer builder */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct BinBuffer {
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
size_t offset() const { return data.size(); }
|
||||
|
||||
void push_float(float v) {
|
||||
const uint8_t *p = reinterpret_cast<const uint8_t*>(&v);
|
||||
data.insert(data.end(), p, p + 4);
|
||||
}
|
||||
void push_u16(uint16_t v) {
|
||||
const uint8_t *p = reinterpret_cast<const uint8_t*>(&v);
|
||||
data.insert(data.end(), p, p + 2);
|
||||
}
|
||||
void push_u32(uint32_t v) {
|
||||
const uint8_t *p = reinterpret_cast<const uint8_t*>(&v);
|
||||
data.insert(data.end(), p, p + 4);
|
||||
}
|
||||
void align4() {
|
||||
while (data.size() % 4 != 0) data.push_back(0);
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Scene config */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct SceneConfig {
|
||||
int num_meshes;
|
||||
int verts_per_mesh;
|
||||
int num_nodes;
|
||||
int num_materials;
|
||||
int num_animations;
|
||||
int keyframes;
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Generate the scene */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct AccessorInfo {
|
||||
int buffer_view;
|
||||
int component_type;
|
||||
int count;
|
||||
const char *type;
|
||||
float min_vals[3];
|
||||
float max_vals[3];
|
||||
int min_max_components; /* 0 = none, 1 = scalar, 3 = vec3 */
|
||||
};
|
||||
|
||||
static void generate_scene(const SceneConfig &cfg,
|
||||
std::string &out_json,
|
||||
std::vector<uint8_t> &out_bin) {
|
||||
BinBuffer bin;
|
||||
|
||||
/* Pre-compute sizes */
|
||||
int tris_per_mesh = cfg.verts_per_mesh / 3;
|
||||
if (tris_per_mesh < 1) tris_per_mesh = 1;
|
||||
int actual_verts = tris_per_mesh * 3;
|
||||
|
||||
/*
|
||||
* For each mesh:
|
||||
* - positions: actual_verts * 3 floats
|
||||
* - normals: actual_verts * 3 floats
|
||||
* - indices: tris_per_mesh * 3 uint16 (or uint32 if >65535)
|
||||
*
|
||||
* For each animation:
|
||||
* - time keys: keyframes floats
|
||||
* - translation values: keyframes * 3 floats
|
||||
*/
|
||||
|
||||
/* Track buffer views and accessors */
|
||||
std::vector<size_t> bv_offsets;
|
||||
std::vector<size_t> bv_lengths;
|
||||
std::vector<AccessorInfo> accessors;
|
||||
int bv_idx = 0;
|
||||
|
||||
bool use_u32_indices = (actual_verts > 65535);
|
||||
|
||||
/* === Mesh data === */
|
||||
for (int m = 0; m < cfg.num_meshes; ++m) {
|
||||
float mesh_offset_x = (float)m * 5.0f;
|
||||
|
||||
/* Positions */
|
||||
size_t pos_off = bin.offset();
|
||||
float pmin[3] = {1e30f, 1e30f, 1e30f};
|
||||
float pmax[3] = {-1e30f, -1e30f, -1e30f};
|
||||
for (int v = 0; v < actual_verts; ++v) {
|
||||
float angle = (float)v / (float)actual_verts * 6.2831853f;
|
||||
float r = 1.0f + 0.3f * sinf(angle * 5.0f);
|
||||
float x = mesh_offset_x + r * cosf(angle);
|
||||
float y = r * sinf(angle);
|
||||
float z = 0.5f * sinf(angle * 3.0f + (float)m);
|
||||
bin.push_float(x); bin.push_float(y); bin.push_float(z);
|
||||
if (x < pmin[0]) pmin[0] = x;
|
||||
if (x > pmax[0]) pmax[0] = x;
|
||||
if (y < pmin[1]) pmin[1] = y;
|
||||
if (y > pmax[1]) pmax[1] = y;
|
||||
if (z < pmin[2]) pmin[2] = z;
|
||||
if (z > pmax[2]) pmax[2] = z;
|
||||
}
|
||||
size_t pos_len = bin.offset() - pos_off;
|
||||
bin.align4();
|
||||
bv_offsets.push_back(pos_off); bv_lengths.push_back(pos_len);
|
||||
int pos_bv = bv_idx++;
|
||||
|
||||
AccessorInfo pos_acc;
|
||||
pos_acc.buffer_view = pos_bv;
|
||||
pos_acc.component_type = 5126; /* FLOAT */
|
||||
pos_acc.count = actual_verts;
|
||||
pos_acc.type = "VEC3";
|
||||
memcpy(pos_acc.min_vals, pmin, sizeof(pmin));
|
||||
memcpy(pos_acc.max_vals, pmax, sizeof(pmax));
|
||||
pos_acc.min_max_components = 3;
|
||||
accessors.push_back(pos_acc);
|
||||
|
||||
/* Normals */
|
||||
size_t norm_off = bin.offset();
|
||||
for (int v = 0; v < actual_verts; ++v) {
|
||||
float angle = (float)v / (float)actual_verts * 6.2831853f;
|
||||
float nx = cosf(angle), ny = sinf(angle), nz = 0.0f;
|
||||
float len = sqrtf(nx*nx + ny*ny + nz*nz);
|
||||
if (len > 0) { nx /= len; ny /= len; nz /= len; }
|
||||
bin.push_float(nx); bin.push_float(ny); bin.push_float(nz);
|
||||
}
|
||||
size_t norm_len = bin.offset() - norm_off;
|
||||
bin.align4();
|
||||
bv_offsets.push_back(norm_off); bv_lengths.push_back(norm_len);
|
||||
int norm_bv = bv_idx++;
|
||||
|
||||
AccessorInfo norm_acc;
|
||||
norm_acc.buffer_view = norm_bv;
|
||||
norm_acc.component_type = 5126;
|
||||
norm_acc.count = actual_verts;
|
||||
norm_acc.type = "VEC3";
|
||||
norm_acc.min_max_components = 0;
|
||||
accessors.push_back(norm_acc);
|
||||
|
||||
/* Indices */
|
||||
size_t idx_off = bin.offset();
|
||||
for (int t = 0; t < tris_per_mesh; ++t) {
|
||||
if (use_u32_indices) {
|
||||
bin.push_u32((uint32_t)(t * 3));
|
||||
bin.push_u32((uint32_t)(t * 3 + 1));
|
||||
bin.push_u32((uint32_t)(t * 3 + 2));
|
||||
} else {
|
||||
bin.push_u16((uint16_t)(t * 3));
|
||||
bin.push_u16((uint16_t)(t * 3 + 1));
|
||||
bin.push_u16((uint16_t)(t * 3 + 2));
|
||||
}
|
||||
}
|
||||
size_t idx_len = bin.offset() - idx_off;
|
||||
bin.align4();
|
||||
bv_offsets.push_back(idx_off); bv_lengths.push_back(idx_len);
|
||||
int idx_bv = bv_idx++;
|
||||
|
||||
AccessorInfo idx_acc;
|
||||
idx_acc.buffer_view = idx_bv;
|
||||
idx_acc.component_type = use_u32_indices ? 5125 : 5123; /* UINT or USHORT */
|
||||
idx_acc.count = tris_per_mesh * 3;
|
||||
idx_acc.type = "SCALAR";
|
||||
idx_acc.min_max_components = 0;
|
||||
accessors.push_back(idx_acc);
|
||||
}
|
||||
|
||||
/* === Animation data === */
|
||||
int anim_time_accessor_start = (int)accessors.size();
|
||||
for (int a = 0; a < cfg.num_animations; ++a) {
|
||||
/* Time keys */
|
||||
size_t time_off = bin.offset();
|
||||
float tmin = 0.0f, tmax = 0.0f;
|
||||
for (int k = 0; k < cfg.keyframes; ++k) {
|
||||
float t = (float)k / (float)(cfg.keyframes - 1) * 10.0f;
|
||||
bin.push_float(t);
|
||||
if (k == 0) tmin = t;
|
||||
tmax = t;
|
||||
}
|
||||
size_t time_len = bin.offset() - time_off;
|
||||
bin.align4();
|
||||
bv_offsets.push_back(time_off); bv_lengths.push_back(time_len);
|
||||
int time_bv = bv_idx++;
|
||||
|
||||
AccessorInfo time_acc;
|
||||
time_acc.buffer_view = time_bv;
|
||||
time_acc.component_type = 5126;
|
||||
time_acc.count = cfg.keyframes;
|
||||
time_acc.type = "SCALAR";
|
||||
time_acc.min_vals[0] = tmin;
|
||||
time_acc.max_vals[0] = tmax;
|
||||
time_acc.min_max_components = 1;
|
||||
accessors.push_back(time_acc);
|
||||
|
||||
/* Translation values */
|
||||
size_t val_off = bin.offset();
|
||||
for (int k = 0; k < cfg.keyframes; ++k) {
|
||||
float t = (float)k / (float)(cfg.keyframes - 1) * 10.0f;
|
||||
float x = sinf(t * 0.5f + (float)a) * 2.0f;
|
||||
float y = cosf(t * 0.3f) * 1.5f;
|
||||
float z = sinf(t * 0.7f + (float)a * 0.5f);
|
||||
bin.push_float(x); bin.push_float(y); bin.push_float(z);
|
||||
}
|
||||
size_t val_len = bin.offset() - val_off;
|
||||
bin.align4();
|
||||
bv_offsets.push_back(val_off); bv_lengths.push_back(val_len);
|
||||
int val_bv = bv_idx++;
|
||||
|
||||
AccessorInfo val_acc;
|
||||
val_acc.buffer_view = val_bv;
|
||||
val_acc.component_type = 5126;
|
||||
val_acc.count = cfg.keyframes;
|
||||
val_acc.type = "VEC3";
|
||||
val_acc.min_max_components = 0;
|
||||
accessors.push_back(val_acc);
|
||||
}
|
||||
|
||||
size_t total_bin = bin.data.size();
|
||||
|
||||
/* === Build JSON === */
|
||||
JsonWriter w;
|
||||
w.begin_obj();
|
||||
|
||||
/* asset */
|
||||
w.key("asset"); w.begin_obj();
|
||||
w.kv_str("version", "2.0");
|
||||
w.kv_str("generator", "tinygltf_benchmark_gen");
|
||||
w.end_obj();
|
||||
|
||||
/* scene */
|
||||
w.kv_int("scene", 0);
|
||||
|
||||
/* scenes */
|
||||
w.key("scenes"); w.begin_arr();
|
||||
w.begin_obj();
|
||||
w.kv_str("name", "BenchmarkScene");
|
||||
w.key("nodes"); w.begin_arr();
|
||||
for (int n = 0; n < cfg.num_nodes; ++n) w.val_int(n);
|
||||
w.end_arr();
|
||||
w.end_obj();
|
||||
w.end_arr();
|
||||
|
||||
/* nodes */
|
||||
w.key("nodes"); w.begin_arr();
|
||||
for (int n = 0; n < cfg.num_nodes; ++n) {
|
||||
w.begin_obj();
|
||||
w.kv_str("name", ("Node_" + std::to_string(n)).c_str());
|
||||
if (n < cfg.num_meshes) {
|
||||
w.kv_int("mesh", n);
|
||||
}
|
||||
w.key("translation"); w.begin_arr();
|
||||
w.val_double((double)n * 3.0);
|
||||
w.val_double(0.0);
|
||||
w.val_double(0.0);
|
||||
w.end_arr();
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
|
||||
/* meshes */
|
||||
w.key("meshes"); w.begin_arr();
|
||||
for (int m = 0; m < cfg.num_meshes; ++m) {
|
||||
int base_acc = m * 3; /* pos, norm, idx per mesh */
|
||||
w.begin_obj();
|
||||
w.kv_str("name", ("Mesh_" + std::to_string(m)).c_str());
|
||||
w.key("primitives"); w.begin_arr();
|
||||
w.begin_obj();
|
||||
w.key("attributes"); w.begin_obj();
|
||||
w.kv_int("POSITION", base_acc);
|
||||
w.kv_int("NORMAL", base_acc + 1);
|
||||
w.end_obj();
|
||||
w.kv_int("indices", base_acc + 2);
|
||||
w.kv_int("material", m % cfg.num_materials);
|
||||
w.kv_int("mode", 4);
|
||||
w.end_obj();
|
||||
w.end_arr();
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
|
||||
/* materials */
|
||||
w.key("materials"); w.begin_arr();
|
||||
for (int m = 0; m < cfg.num_materials; ++m) {
|
||||
w.begin_obj();
|
||||
w.kv_str("name", ("Material_" + std::to_string(m)).c_str());
|
||||
w.key("pbrMetallicRoughness"); w.begin_obj();
|
||||
w.key("baseColorFactor"); w.begin_arr();
|
||||
float hue = (float)m / (float)cfg.num_materials;
|
||||
w.val_double(0.5 + 0.5 * sin(hue * 6.28));
|
||||
w.val_double(0.5 + 0.5 * sin(hue * 6.28 + 2.09));
|
||||
w.val_double(0.5 + 0.5 * sin(hue * 6.28 + 4.19));
|
||||
w.val_double(1.0);
|
||||
w.end_arr();
|
||||
w.kv_double("metallicFactor", 0.2 + 0.6 * ((double)m / cfg.num_materials));
|
||||
w.kv_double("roughnessFactor", 0.3 + 0.5 * ((double)(cfg.num_materials - m) / cfg.num_materials));
|
||||
w.end_obj();
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
|
||||
/* accessors */
|
||||
w.key("accessors"); w.begin_arr();
|
||||
for (size_t i = 0; i < accessors.size(); ++i) {
|
||||
const AccessorInfo &a = accessors[i];
|
||||
w.begin_obj();
|
||||
w.kv_int("bufferView", a.buffer_view);
|
||||
w.kv_int("componentType", a.component_type);
|
||||
w.kv_int("count", a.count);
|
||||
w.kv_str("type", a.type);
|
||||
if (a.min_max_components == 1) {
|
||||
w.key("min"); w.begin_arr(); w.val_double(a.min_vals[0]); w.end_arr();
|
||||
w.key("max"); w.begin_arr(); w.val_double(a.max_vals[0]); w.end_arr();
|
||||
} else if (a.min_max_components == 3) {
|
||||
w.key("min"); w.begin_arr();
|
||||
w.val_double(a.min_vals[0]); w.val_double(a.min_vals[1]); w.val_double(a.min_vals[2]);
|
||||
w.end_arr();
|
||||
w.key("max"); w.begin_arr();
|
||||
w.val_double(a.max_vals[0]); w.val_double(a.max_vals[1]); w.val_double(a.max_vals[2]);
|
||||
w.end_arr();
|
||||
}
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
|
||||
/* bufferViews */
|
||||
w.key("bufferViews"); w.begin_arr();
|
||||
for (int i = 0; i < bv_idx; ++i) {
|
||||
w.begin_obj();
|
||||
w.kv_int("buffer", 0);
|
||||
w.kv_int("byteOffset", (int64_t)bv_offsets[i]);
|
||||
w.kv_int("byteLength", (int64_t)bv_lengths[i]);
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
|
||||
/* buffers */
|
||||
w.key("buffers"); w.begin_arr();
|
||||
w.begin_obj();
|
||||
w.kv_int("byteLength", (int64_t)total_bin);
|
||||
/* URI will be set by caller for .gltf, omitted for .glb */
|
||||
w.end_obj();
|
||||
w.end_arr();
|
||||
|
||||
/* animations */
|
||||
if (cfg.num_animations > 0) {
|
||||
w.key("animations"); w.begin_arr();
|
||||
for (int a = 0; a < cfg.num_animations; ++a) {
|
||||
int time_acc = anim_time_accessor_start + a * 2;
|
||||
int val_acc = time_acc + 1;
|
||||
/* Target node: cycle through available nodes */
|
||||
int target_node = a % cfg.num_nodes;
|
||||
|
||||
w.begin_obj();
|
||||
w.kv_str("name", ("Anim_" + std::to_string(a)).c_str());
|
||||
|
||||
w.key("channels"); w.begin_arr();
|
||||
w.begin_obj();
|
||||
w.kv_int("sampler", 0);
|
||||
w.key("target"); w.begin_obj();
|
||||
w.kv_int("node", target_node);
|
||||
w.kv_str("path", "translation");
|
||||
w.end_obj();
|
||||
w.end_obj();
|
||||
w.end_arr();
|
||||
|
||||
w.key("samplers"); w.begin_arr();
|
||||
w.begin_obj();
|
||||
w.kv_int("input", time_acc);
|
||||
w.kv_int("output", val_acc);
|
||||
w.kv_str("interpolation", "LINEAR");
|
||||
w.end_obj();
|
||||
w.end_arr();
|
||||
|
||||
w.end_obj();
|
||||
}
|
||||
w.end_arr();
|
||||
}
|
||||
|
||||
w.end_obj();
|
||||
|
||||
out_json = w.buf;
|
||||
out_bin = bin.data;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Write .gltf + .bin */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void write_gltf(const std::string &prefix, const std::string &label,
|
||||
const std::string &json_str,
|
||||
const std::vector<uint8_t> &bin_data) {
|
||||
std::string bin_name = prefix + "_" + label + ".bin";
|
||||
std::string gltf_name = prefix + "_" + label + ".gltf";
|
||||
|
||||
/* Inject "uri" into the buffer object in JSON */
|
||||
std::string json_patched = json_str;
|
||||
/* Find the buffers array and add uri before the closing } of the buffer */
|
||||
size_t pos = json_patched.find("\"byteLength\"");
|
||||
if (pos != std::string::npos) {
|
||||
/* Find the line end after byteLength value */
|
||||
size_t line_end = json_patched.find('\n', pos);
|
||||
if (line_end != std::string::npos) {
|
||||
/* Extract just the filename for uri */
|
||||
std::string bin_filename = prefix + "_" + label + ".bin";
|
||||
std::string uri_line = ",\n \"uri\": \"" + bin_filename + "\"";
|
||||
json_patched.insert(line_end, uri_line);
|
||||
}
|
||||
}
|
||||
|
||||
/* Write .bin */
|
||||
FILE *f = fopen(bin_name.c_str(), "wb");
|
||||
if (f) {
|
||||
fwrite(bin_data.data(), 1, bin_data.size(), f);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
/* Write .gltf */
|
||||
f = fopen(gltf_name.c_str(), "w");
|
||||
if (f) {
|
||||
fwrite(json_patched.c_str(), 1, json_patched.size(), f);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
printf(" Written: %s (%zu bytes JSON) + %s (%zu bytes binary)\n",
|
||||
gltf_name.c_str(), json_patched.size(),
|
||||
bin_name.c_str(), bin_data.size());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Write .glb */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void write_glb(const std::string &prefix, const std::string &label,
|
||||
const std::string &json_str,
|
||||
const std::vector<uint8_t> &bin_data) {
|
||||
std::string glb_name = prefix + "_" + label + ".glb";
|
||||
|
||||
uint32_t json_len = (uint32_t)json_str.size();
|
||||
uint32_t json_padded = (json_len + 3) & ~3u;
|
||||
uint32_t bin_len = (uint32_t)bin_data.size();
|
||||
uint32_t bin_padded = (bin_len + 3) & ~3u;
|
||||
|
||||
uint32_t total = 12 + 8 + json_padded + 8 + bin_padded;
|
||||
|
||||
FILE *f = fopen(glb_name.c_str(), "wb");
|
||||
if (!f) return;
|
||||
|
||||
/* Header */
|
||||
fwrite("glTF", 1, 4, f);
|
||||
uint32_t version = 2;
|
||||
fwrite(&version, 4, 1, f);
|
||||
fwrite(&total, 4, 1, f);
|
||||
|
||||
/* JSON chunk */
|
||||
uint32_t json_type = 0x4E4F534A;
|
||||
fwrite(&json_padded, 4, 1, f);
|
||||
fwrite(&json_type, 4, 1, f);
|
||||
fwrite(json_str.c_str(), 1, json_len, f);
|
||||
for (uint32_t i = json_len; i < json_padded; ++i) {
|
||||
char sp = ' ';
|
||||
fwrite(&sp, 1, 1, f);
|
||||
}
|
||||
|
||||
/* BIN chunk */
|
||||
uint32_t bin_type = 0x004E4942;
|
||||
fwrite(&bin_padded, 4, 1, f);
|
||||
fwrite(&bin_type, 4, 1, f);
|
||||
fwrite(bin_data.data(), 1, bin_len, f);
|
||||
for (uint32_t i = bin_len; i < bin_padded; ++i) {
|
||||
char z = 0;
|
||||
fwrite(&z, 1, 1, f);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
printf(" Written: %s (%u bytes)\n", glb_name.c_str(), total);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Preset configurations */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
struct Preset {
|
||||
const char *label;
|
||||
SceneConfig cfg;
|
||||
};
|
||||
|
||||
static Preset presets[] = {
|
||||
{"tiny", {1, 100, 2, 1, 0, 0}},
|
||||
{"small", {5, 1000, 10, 3, 2, 50}},
|
||||
{"medium", {20, 5000, 50, 10, 5, 200}},
|
||||
{"large", {100, 10000, 200, 20, 10, 500}},
|
||||
{"huge", {500, 50000, 1000, 50, 50, 1000}},
|
||||
};
|
||||
|
||||
static const int num_presets = (int)(sizeof(presets) / sizeof(presets[0]));
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Generate float-heavy scene (~500MB of ASCII float values in JSON) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void generate_float_heavy(const std::string &prefix, size_t target_mb) {
|
||||
std::string gltf_name = prefix + "_float_heavy.gltf";
|
||||
FILE *f = fopen(gltf_name.c_str(), "w");
|
||||
if (!f) {
|
||||
fprintf(stderr, "Cannot open %s\n", gltf_name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
/* Write minimal valid glTF with massive extras float array */
|
||||
fprintf(f, "{\n");
|
||||
fprintf(f, " \"asset\": {\"version\": \"2.0\", \"generator\": \"tinygltf_benchmark_gen\"},\n");
|
||||
fprintf(f, " \"scene\": 0,\n");
|
||||
fprintf(f, " \"scenes\": [{\"name\": \"FloatHeavy\", \"nodes\": [0]}],\n");
|
||||
fprintf(f, " \"nodes\": [{\"name\": \"Root\"}],\n");
|
||||
fprintf(f, " \"extras\": {\n");
|
||||
|
||||
size_t target_bytes = target_mb * 1024ULL * 1024ULL;
|
||||
size_t total_written = 0;
|
||||
int num_channels = 10;
|
||||
size_t per_channel = target_bytes / (size_t)num_channels;
|
||||
|
||||
for (int ch = 0; ch < num_channels; ++ch) {
|
||||
fprintf(f, " \"channel_%d\": [\n ", ch);
|
||||
size_t ch_written = 0;
|
||||
size_t count = 0;
|
||||
uint64_t seed = (uint64_t)ch * 7919ULL + 1;
|
||||
bool first = true;
|
||||
|
||||
while (ch_written < per_channel) {
|
||||
/* Comma before every value except the first */
|
||||
if (!first) {
|
||||
fwrite(",\n ", 1, 8, f);
|
||||
ch_written += 8;
|
||||
}
|
||||
first = false;
|
||||
|
||||
/* Generate varied float values: mix of magnitudes and precisions */
|
||||
seed = seed * 6364136223846793005ULL + 1442695040888963407ULL;
|
||||
double raw = (double)(int64_t)seed / (double)INT64_MAX;
|
||||
|
||||
double val;
|
||||
int kind = (int)(count % 5);
|
||||
switch (kind) {
|
||||
case 0: val = raw * 1000.0; break; /* large: -999.xxx */
|
||||
case 1: val = raw * 0.001; break; /* small: 0.000xxx */
|
||||
case 2: val = raw * 3.14159265358979; break; /* medium: -3.14..3.14 */
|
||||
case 3: val = raw * 1e6; break; /* very large */
|
||||
case 4: val = raw * 1e-6; break; /* very small */
|
||||
default: val = raw; break;
|
||||
}
|
||||
|
||||
char buf[64];
|
||||
int len = snprintf(buf, sizeof(buf), "%.8g", val);
|
||||
fwrite(buf, 1, (size_t)len, f);
|
||||
ch_written += (size_t)len;
|
||||
count++;
|
||||
}
|
||||
|
||||
total_written += ch_written;
|
||||
|
||||
if (ch < num_channels - 1) {
|
||||
fprintf(f, "\n ],\n");
|
||||
} else {
|
||||
fprintf(f, "\n ]\n");
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(f, " }\n");
|
||||
fprintf(f, "}\n");
|
||||
fclose(f);
|
||||
|
||||
/* Report actual file size */
|
||||
f = fopen(gltf_name.c_str(), "rb");
|
||||
if (f) {
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fclose(f);
|
||||
printf(" Written: %s (%.1f MB, ~%zu float values across %d channels)\n",
|
||||
gltf_name.c_str(), (double)sz / (1024.0 * 1024.0),
|
||||
total_written / 12, num_channels);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Main */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::string prefix = "synthetic";
|
||||
|
||||
/* Parse args */
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "--prefix") == 0 && i + 1 < argc) {
|
||||
prefix = argv[++i];
|
||||
}
|
||||
}
|
||||
|
||||
printf("Generating synthetic glTF benchmark scenes...\n\n");
|
||||
|
||||
for (int p = 0; p < num_presets; ++p) {
|
||||
const Preset &pr = presets[p];
|
||||
|
||||
printf("[%s] meshes=%d verts/mesh=%d nodes=%d materials=%d "
|
||||
"animations=%d keyframes=%d\n",
|
||||
pr.label, pr.cfg.num_meshes, pr.cfg.verts_per_mesh,
|
||||
pr.cfg.num_nodes, pr.cfg.num_materials,
|
||||
pr.cfg.num_animations, pr.cfg.keyframes);
|
||||
|
||||
std::string json;
|
||||
std::vector<uint8_t> bin;
|
||||
generate_scene(pr.cfg, json, bin);
|
||||
|
||||
write_gltf(prefix, pr.label, json, bin);
|
||||
write_glb(prefix, pr.label, json, bin);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* Float-heavy scene: ~500MB of ASCII floats in JSON */
|
||||
printf("[float_heavy] ~500MB of ASCII float values in JSON extras\n");
|
||||
generate_float_heavy(prefix, 500);
|
||||
printf("\n");
|
||||
|
||||
printf("Done.\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -1249,3 +1249,37 @@ TEST_CASE("empty-images-not-written", "[issue-495]") {
|
||||
// WriteImageData should be invoked for both images
|
||||
CHECK(counter == 2);
|
||||
}
|
||||
|
||||
#ifdef TINYGLTF_USE_CUSTOM_JSON
|
||||
/* Regression test: in float32_mode, integer-only tokens with more than 9
|
||||
* digits must still be parsed as integers (is_int == 1), not floats.
|
||||
* Previously, max_sig=9 was applied to the integer part too, causing excess
|
||||
* digits to bump exp10, which broke the exp10==0 guard in the integer
|
||||
* fast-path and mis-classified the value as a float. */
|
||||
TEST_CASE("cj-float32-long-integer", "[customjson]") {
|
||||
// Values chosen to cover exactly-at, just-over, and near int64 boundaries.
|
||||
struct {
|
||||
const char *text;
|
||||
int64_t expected;
|
||||
} cases[] = {
|
||||
{ "1234567890", 1234567890LL }, /* 10 digits */
|
||||
{ "12345678901", 12345678901LL }, /* 11 digits */
|
||||
{ "1000000000000", 1000000000000LL }, /* 13 digits */
|
||||
{ "9223372036854775807", INT64_MAX }, /* max int64 (19 digits) */
|
||||
{ "-1234567890", -1234567890LL }, /* negative 10 digits */
|
||||
{ "-9223372036854775808", INT64_MIN }, /* min int64 */
|
||||
};
|
||||
|
||||
for (auto &tc : cases) {
|
||||
int is_int = 0;
|
||||
int64_t ival = 0;
|
||||
double dval = 0.0;
|
||||
const char *end = tc.text + strlen(tc.text);
|
||||
const char *ret = cj_parse_number(tc.text, end, &is_int, &ival, &dval, /*float32_mode=*/1);
|
||||
CAPTURE(tc.text);
|
||||
REQUIRE(ret != nullptr);
|
||||
CHECK(is_int == 1);
|
||||
CHECK(ival == tc.expected);
|
||||
}
|
||||
}
|
||||
#endif /* TINYGLTF_USE_CUSTOM_JSON */
|
||||
|
||||
67
tests/v3/fuzzer/Makefile
Normal file
67
tests/v3/fuzzer/Makefile
Normal file
@@ -0,0 +1,67 @@
|
||||
# tests/v3/fuzzer/Makefile — Build libFuzzer harness for tinygltf v3
|
||||
#
|
||||
# Requires: clang++ with libFuzzer support
|
||||
#
|
||||
# Targets:
|
||||
# make — build fuzzer with ASan + UBSan
|
||||
# make run — run fuzzer with default settings
|
||||
# make seed — generate seed corpus from test models
|
||||
# make clean — remove binaries and corpus
|
||||
|
||||
CXX = clang++
|
||||
CXXFLAGS = -g -O1 -std=c++17 -fno-rtti -fno-exceptions
|
||||
SANITIZE = -fsanitize=fuzzer,address,undefined
|
||||
INCLUDES = -I../../..
|
||||
|
||||
FUZZER = fuzz_gltf_v3
|
||||
CORPUS = corpus
|
||||
ARTIFACTS = artifacts
|
||||
|
||||
# Fuzzer runtime options
|
||||
MAX_LEN ?= 65536
|
||||
JOBS ?= $(shell nproc 2>/dev/null || echo 4)
|
||||
MAX_TIME ?= 0
|
||||
|
||||
.PHONY: all run seed clean
|
||||
|
||||
all: $(FUZZER)
|
||||
|
||||
$(FUZZER): fuzz_gltf_v3.cc ../../../tiny_gltf_v3.h ../../../tinygltf_json.h
|
||||
$(CXX) $(CXXFLAGS) $(SANITIZE) $(INCLUDES) -o $@ $<
|
||||
|
||||
run: $(FUZZER) | $(CORPUS) $(ARTIFACTS)
|
||||
./$(FUZZER) $(CORPUS) \
|
||||
-artifact_prefix=$(ARTIFACTS)/ \
|
||||
-max_len=$(MAX_LEN) \
|
||||
-jobs=$(JOBS) \
|
||||
-workers=$(JOBS) \
|
||||
$(if $(filter-out 0,$(MAX_TIME)),-max_total_time=$(MAX_TIME))
|
||||
|
||||
# Generate seed corpus from existing test models
|
||||
seed: | $(CORPUS)
|
||||
@echo "Seeding corpus from test models..."
|
||||
@for f in ../../../models/Cube/Cube.gltf \
|
||||
../../../models/Cube/Cube.glb; do \
|
||||
if [ -f "$$f" ]; then \
|
||||
cp "$$f" $(CORPUS)/; \
|
||||
echo " Added: $$f"; \
|
||||
fi; \
|
||||
done
|
||||
@# Add a minimal valid glTF JSON
|
||||
@echo '{"asset":{"version":"2.0"},"scene":0,"scenes":[{"nodes":[0]}],"nodes":[{"name":"n"}]}' > $(CORPUS)/minimal.gltf
|
||||
@# Add a minimal valid GLB (header + empty JSON chunk)
|
||||
@printf 'glTF\x02\x00\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00JSON{} ' > $(CORPUS)/minimal.glb
|
||||
@# Add edge cases
|
||||
@echo '{}' > $(CORPUS)/empty_object.gltf
|
||||
@echo '{"asset":{"version":"2.0"}}' > $(CORPUS)/asset_only.gltf
|
||||
@echo "Corpus: $$(ls $(CORPUS) | wc -l) files"
|
||||
|
||||
$(CORPUS):
|
||||
mkdir -p $(CORPUS)
|
||||
|
||||
$(ARTIFACTS):
|
||||
mkdir -p $(ARTIFACTS)
|
||||
|
||||
clean:
|
||||
rm -f $(FUZZER)
|
||||
rm -rf $(CORPUS) $(ARTIFACTS)
|
||||
110
tests/v3/fuzzer/fuzz_gltf_v3.cc
Normal file
110
tests/v3/fuzzer/fuzz_gltf_v3.cc
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* fuzz_gltf_v3.cc — libFuzzer harness for tinygltf v3 parser.
|
||||
*
|
||||
* Fuzz targets:
|
||||
* - Auto-detect (GLB or JSON) parse from arbitrary bytes
|
||||
* - Exercises JSON parser, GLB header parsing, arena allocator,
|
||||
* error stack, and all glTF entity parsing paths.
|
||||
*
|
||||
* Build (clang with libFuzzer):
|
||||
* clang++ -g -O1 -fsanitize=fuzzer,address,undefined \
|
||||
* -std=c++17 -fno-rtti -fno-exceptions \
|
||||
* -I../../.. -o fuzz_gltf_v3 fuzz_gltf_v3.cc
|
||||
*
|
||||
* Run:
|
||||
* ./fuzz_gltf_v3 corpus/ -max_len=65536
|
||||
*
|
||||
* Seed corpus: place valid .gltf and .glb files in corpus/
|
||||
*/
|
||||
|
||||
#define TINYGLTF3_IMPLEMENTATION
|
||||
#include "tiny_gltf_v3.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
/* Memory budget to prevent OOM during fuzzing */
|
||||
static const uint64_t FUZZ_MEMORY_BUDGET = 64ULL * 1024 * 1024; /* 64 MB */
|
||||
|
||||
static void fuzz_parse_auto(const uint8_t *data, size_t size) {
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
|
||||
tg3_parse_options opts;
|
||||
tg3_parse_options_init(&opts);
|
||||
opts.memory.memory_budget = FUZZ_MEMORY_BUDGET;
|
||||
|
||||
tg3_parse_auto(&model, &errors, data, (uint64_t)size,
|
||||
"", 0, &opts);
|
||||
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
}
|
||||
|
||||
static void fuzz_parse_json(const uint8_t *data, size_t size) {
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
|
||||
tg3_parse_options opts;
|
||||
tg3_parse_options_init(&opts);
|
||||
opts.memory.memory_budget = FUZZ_MEMORY_BUDGET;
|
||||
|
||||
tg3_parse(&model, &errors, data, (uint64_t)size,
|
||||
"", 0, &opts);
|
||||
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
}
|
||||
|
||||
static void fuzz_parse_glb(const uint8_t *data, size_t size) {
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
|
||||
tg3_parse_options opts;
|
||||
tg3_parse_options_init(&opts);
|
||||
opts.memory.memory_budget = FUZZ_MEMORY_BUDGET;
|
||||
|
||||
tg3_parse_glb(&model, &errors, data, (uint64_t)size,
|
||||
"", 0, &opts);
|
||||
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
}
|
||||
|
||||
static void fuzz_parse_float32(const uint8_t *data, size_t size) {
|
||||
tg3_model model;
|
||||
tg3_error_stack errors;
|
||||
tg3_error_stack_init(&errors);
|
||||
|
||||
tg3_parse_options opts;
|
||||
tg3_parse_options_init(&opts);
|
||||
opts.memory.memory_budget = FUZZ_MEMORY_BUDGET;
|
||||
opts.parse_float32 = 1;
|
||||
|
||||
tg3_parse_auto(&model, &errors, data, (uint64_t)size,
|
||||
"", 0, &opts);
|
||||
|
||||
tg3_model_free(&model);
|
||||
tg3_error_stack_free(&errors);
|
||||
}
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
if (size == 0) return 0;
|
||||
|
||||
/* Use first byte to select parse path, rest is the payload */
|
||||
uint8_t selector = data[0] % 4;
|
||||
const uint8_t *payload = data + 1;
|
||||
size_t payload_size = size - 1;
|
||||
|
||||
switch (selector) {
|
||||
case 0: fuzz_parse_auto(payload, payload_size); break;
|
||||
case 1: fuzz_parse_json(payload, payload_size); break;
|
||||
case 2: fuzz_parse_glb(payload, payload_size); break;
|
||||
case 3: fuzz_parse_float32(payload, payload_size); break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
4437
tiny_gltf_v3.h
Normal file
4437
tiny_gltf_v3.h
Normal file
File diff suppressed because it is too large
Load Diff
313
tinygltf_json.h
313
tinygltf_json.h
@@ -295,93 +295,275 @@ static const char *cj_scan_str(const char *p, const char *end) {
|
||||
|
||||
/* ======================================================================
|
||||
* FAST NUMBER PARSING (C-style)
|
||||
*
|
||||
* Uses Clinger's fast path for float conversion, avoiding strtod() for the
|
||||
* vast majority of JSON numbers. The fast path itself is locale-independent
|
||||
* and typically 4-10x faster than strtod; however, rare fallback paths may
|
||||
* still invoke the C library's strtod(), which can be locale-dependent.
|
||||
*
|
||||
* Optional float32 mode (CJ_FLOAT32_MODE flag in cj_parse_number):
|
||||
* Parses floating-point values to float (single) precision and stores
|
||||
* the result as double. Faster because fewer significant digits are
|
||||
* needed and the fast path covers a wider exponent range.
|
||||
* Breaks strict JSON/IEEE-754-double conformance.
|
||||
* ====================================================================== */
|
||||
|
||||
static const char *cj_parse_uint64(const char *p, const char *end,
|
||||
uint64_t *result, int *overflow) {
|
||||
uint64_t v = 0;
|
||||
*overflow = 0;
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) {
|
||||
unsigned char d = (unsigned char)*p - '0';
|
||||
/* Detect multiplication/addition overflow */
|
||||
if (v > (UINT64_MAX - d) / 10u) {
|
||||
*overflow = 1;
|
||||
/* Consume remaining digits so caller sees the full token */
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) ++p;
|
||||
*result = UINT64_MAX;
|
||||
return p;
|
||||
}
|
||||
v = v * 10u + (uint64_t)d;
|
||||
++p;
|
||||
}
|
||||
*result = v;
|
||||
return p;
|
||||
/* 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;
|
||||
if (d <= (double)INT64_MIN) return INT64_MIN;
|
||||
return (int64_t)d;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse a JSON number starting at [p, end).
|
||||
/* Exact powers of 10 that are representable as IEEE 754 double.
|
||||
* 10^0 through 10^22 are all exactly representable. */
|
||||
static const double cj_exact_pow10[23] = {
|
||||
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
|
||||
1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
|
||||
1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
|
||||
};
|
||||
|
||||
/* Clinger's fast path: mantissa * 10^exp10 → double.
|
||||
* Requires mantissa <= 2^53 (exactly representable as double).
|
||||
* Returns 1 on success, 0 if fallback needed. */
|
||||
static int cj_fast_dbl_convert(uint64_t mantissa, int exp10, int neg, double *out) {
|
||||
if (mantissa == 0) {
|
||||
*out = neg ? -0.0 : 0.0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Primary: |exp10| <= 22, mantissa fits in double mantissa bits */
|
||||
if (mantissa <= (1ULL << 53)) {
|
||||
double d;
|
||||
if (exp10 >= 0 && exp10 <= 22) {
|
||||
d = (double)mantissa * cj_exact_pow10[exp10];
|
||||
*out = neg ? -d : d;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 < 0 && exp10 >= -22) {
|
||||
d = (double)mantissa / cj_exact_pow10[-exp10];
|
||||
*out = neg ? -d : d;
|
||||
return 1;
|
||||
}
|
||||
/* Extended: split exponent into two steps, each <= 22.
|
||||
* Positive: exp10 = 22 + remainder, both halves exact.
|
||||
* Negative: exp10 = -22 + remainder. */
|
||||
if (exp10 > 22 && exp10 <= 22 + 22) {
|
||||
d = (double)mantissa * cj_exact_pow10[exp10 - 22];
|
||||
d *= cj_exact_pow10[22];
|
||||
*out = neg ? -d : d;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 < -22 && exp10 >= -(22 + 22)) {
|
||||
d = (double)mantissa / cj_exact_pow10[-exp10 - 22];
|
||||
d /= cj_exact_pow10[22];
|
||||
*out = neg ? -d : d;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Fast path for float32: wider range because float mantissa is only 24 bits. */
|
||||
static int cj_fast_flt_convert(uint64_t mantissa, int exp10, int neg, float *out) {
|
||||
if (mantissa == 0) {
|
||||
*out = neg ? -0.0f : 0.0f;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Direct float path: mantissa fits in 24 bits, pow10 exact in float */
|
||||
if (mantissa <= (1ULL << 24)) {
|
||||
if (exp10 >= 0 && exp10 <= 10) {
|
||||
float f = (float)mantissa * (float)cj_exact_pow10[exp10];
|
||||
*out = neg ? -f : f;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 < 0 && exp10 >= -10) {
|
||||
float f = (float)mantissa / (float)cj_exact_pow10[-exp10];
|
||||
*out = neg ? -f : f;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wider path via double arithmetic (still float-precision result) */
|
||||
if (mantissa <= (1ULL << 53)) {
|
||||
double d;
|
||||
if (exp10 >= 0 && exp10 <= 22) {
|
||||
d = (double)mantissa * cj_exact_pow10[exp10];
|
||||
*out = neg ? -(float)d : (float)d;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 < 0 && exp10 >= -22) {
|
||||
d = (double)mantissa / cj_exact_pow10[-exp10];
|
||||
*out = neg ? -(float)d : (float)d;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 > 22 && exp10 <= 44) {
|
||||
d = (double)mantissa * cj_exact_pow10[exp10 - 22];
|
||||
d *= cj_exact_pow10[22];
|
||||
*out = neg ? -(float)d : (float)d;
|
||||
return 1;
|
||||
}
|
||||
if (exp10 < -22 && exp10 >= -44) {
|
||||
d = (double)mantissa / cj_exact_pow10[-exp10 - 22];
|
||||
d /= cj_exact_pow10[22];
|
||||
*out = neg ? -(float)d : (float)d;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parse a JSON number starting at [p, end).
|
||||
* Sets *is_int, *ival (integer result), *dval (floating-point result).
|
||||
* Returns pointer past the last character consumed, or NULL on error.
|
||||
*
|
||||
* NOTE: strtod is locale-dependent on some platforms (decimal separator).
|
||||
* JSON mandates '.' as decimal separator. Callers in environments where the
|
||||
* C locale may be overridden should ensure the locale is reset to "C" before
|
||||
* parsing floating-point JSON values.
|
||||
*/
|
||||
* float32_mode: when non-zero, floating-point values are parsed at float
|
||||
* (single) precision — only 9 significant digits are tracked for the
|
||||
* fraction part, and the result is stored as (double)(float)value. This
|
||||
* is faster but not JSON-conformant for high-precision doubles. Integer-
|
||||
* only tokens (no '.'/'e') are always parsed at full int64 precision
|
||||
* regardless of this flag.
|
||||
*
|
||||
* Uses Clinger's fast path (no strtod) for ~99% of JSON float values.
|
||||
* Falls back to strtod only for extreme exponents or >19 significant digits. */
|
||||
static const char *cj_parse_number(const char *p, const char *end,
|
||||
int *is_int, int64_t *ival, double *dval) {
|
||||
int *is_int, int64_t *ival, double *dval,
|
||||
int float32_mode) {
|
||||
const char *start = p;
|
||||
int neg = 0;
|
||||
if (p < end && *p == '-') { neg = 1; ++p; }
|
||||
if (p >= end) return NULL;
|
||||
|
||||
uint64_t int_part = 0;
|
||||
/* Accumulate ALL digits (integer + fraction) into a single mantissa.
|
||||
* Track the decimal exponent adjustment from the '.' position. */
|
||||
uint64_t mantissa = 0;
|
||||
int ndigits = 0; /* total significant digits consumed */
|
||||
int exp10 = 0; /* decimal exponent adjustment */
|
||||
int mantissa_overflow = 0; /* set if >19 significant digits */
|
||||
int has_frac = 0, has_exp = 0;
|
||||
int int_overflow = 0;
|
||||
|
||||
/* Max significant digits we track:
|
||||
* Integer part: always 19, so integer-only tokens (no '.'/'e') are always
|
||||
* accumulated fully and can be typed as int64 regardless of float32_mode.
|
||||
* Fraction part: 9 in float32_mode (single precision), 19 otherwise. */
|
||||
int max_sig_int = 19;
|
||||
int max_sig_frac = float32_mode ? 9 : 19;
|
||||
|
||||
/* Integer part */
|
||||
if (*p == '0') {
|
||||
++p;
|
||||
} else if ((unsigned)(*p - '1') <= 8u) {
|
||||
p = cj_parse_uint64(p, end, &int_part, &int_overflow);
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) {
|
||||
unsigned d = (unsigned)(*p - '0');
|
||||
if (ndigits < max_sig_int) {
|
||||
mantissa = mantissa * 10 + d;
|
||||
} else {
|
||||
exp10++; /* excess digit: bump exponent instead */
|
||||
if (ndigits >= 19) mantissa_overflow = 1;
|
||||
}
|
||||
ndigits++;
|
||||
++p;
|
||||
}
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (p < end && *p == '.') has_frac = 1;
|
||||
if (p < end && (*p == 'e' || *p == 'E')) has_exp = 1;
|
||||
|
||||
if (!has_frac && !has_exp && !int_overflow) {
|
||||
/* Guard signed overflow: -(int64_t)x is UB when x > INT64_MAX.
|
||||
* Positive: x must fit in [0, INT64_MAX].
|
||||
* Negative: magnitude must fit in [0, 2^63] i.e. <= INT64_MAX+1
|
||||
* (the upper bound covers INT64_MIN = -2^63). */
|
||||
int fits;
|
||||
if (!neg) {
|
||||
fits = (int_part <= (uint64_t)INT64_MAX);
|
||||
} else {
|
||||
fits = (int_part <= (uint64_t)INT64_MAX + 1u);
|
||||
/* Fraction part */
|
||||
if (p < end && *p == '.') {
|
||||
has_frac = 1;
|
||||
++p;
|
||||
/* JSON requires at least one digit after '.' */
|
||||
if (p >= end || (unsigned)(*p - '0') > 9u) return NULL;
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) {
|
||||
unsigned d = (unsigned)(*p - '0');
|
||||
if (ndigits < max_sig_frac) {
|
||||
mantissa = mantissa * 10 + d;
|
||||
exp10--;
|
||||
}
|
||||
/* else: ignore trailing fraction digits beyond precision */
|
||||
ndigits++;
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Exponent part */
|
||||
if (p < end && (*p == 'e' || *p == 'E')) {
|
||||
has_exp = 1;
|
||||
++p;
|
||||
int exp_neg = 0;
|
||||
if (p < end && *p == '+') ++p;
|
||||
else if (p < end && *p == '-') { exp_neg = 1; ++p; }
|
||||
/* JSON requires at least one digit in exponent */
|
||||
if (p >= end || (unsigned)(*p - '0') > 9u) return NULL;
|
||||
int exp_val = 0;
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) {
|
||||
exp_val = exp_val * 10 + (*p - '0');
|
||||
if (exp_val > 9999) {
|
||||
/* Prevent overflow; will fall through to strtod */
|
||||
while (p < end && (unsigned)(*p - '0') <= 9u) ++p;
|
||||
break;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
exp10 += exp_neg ? -exp_val : exp_val;
|
||||
}
|
||||
|
||||
/* ---- Integer fast path (no fraction, no exponent, fits int64) ---- */
|
||||
/* exp10 == 0 ensures all digits were accumulated (none truncated by max_sig) */
|
||||
if (!has_frac && !has_exp && !mantissa_overflow && exp10 == 0) {
|
||||
uint64_t mag = mantissa;
|
||||
int fits;
|
||||
if (!neg)
|
||||
fits = (mag <= (uint64_t)INT64_MAX);
|
||||
else
|
||||
fits = (mag <= (uint64_t)INT64_MAX + 1u);
|
||||
if (fits) {
|
||||
int64_t sv;
|
||||
if (neg && int_part == (uint64_t)INT64_MAX + 1u)
|
||||
sv = INT64_MIN; /* special case: magnitude 2^63 → INT64_MIN */
|
||||
if (neg && mag == (uint64_t)INT64_MAX + 1u)
|
||||
sv = INT64_MIN;
|
||||
else
|
||||
sv = neg ? -(int64_t)int_part : (int64_t)int_part;
|
||||
sv = neg ? -(int64_t)mag : (int64_t)mag;
|
||||
*is_int = 1;
|
||||
*ival = sv;
|
||||
*dval = (double)sv;
|
||||
return p;
|
||||
}
|
||||
/* Magnitude doesn't fit int64_t: fall through to strtod */
|
||||
}
|
||||
|
||||
/* Floating-point, integer overflow, or out-of-int64-range: use strtod */
|
||||
/* ---- Float fast path (Clinger's algorithm) ---- */
|
||||
if (!mantissa_overflow) {
|
||||
if (float32_mode) {
|
||||
float f;
|
||||
if (cj_fast_flt_convert(mantissa, exp10, neg, &f)) {
|
||||
*is_int = 0;
|
||||
*dval = (double)f;
|
||||
*ival = cj_dbl_to_i64((double)f);
|
||||
return p;
|
||||
}
|
||||
} else {
|
||||
double d;
|
||||
if (cj_fast_dbl_convert(mantissa, exp10, neg, &d)) {
|
||||
*is_int = 0;
|
||||
*dval = d;
|
||||
*ival = cj_dbl_to_i64(d);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- Fallback: strtod (handles extreme exponents, >19 digits) ---- */
|
||||
char *eptr = NULL;
|
||||
double d = strtod(start, &eptr);
|
||||
if (eptr == start) return NULL;
|
||||
if (float32_mode) d = (double)(float)d;
|
||||
*is_int = 0;
|
||||
*dval = d;
|
||||
*ival = (int64_t)d;
|
||||
*ival = cj_dbl_to_i64(d);
|
||||
return eptr;
|
||||
}
|
||||
|
||||
@@ -686,6 +868,12 @@ public:
|
||||
static tinygltf_json parse(const char *first, const char *last,
|
||||
std::nullptr_t = nullptr,
|
||||
bool allow_exceptions = false);
|
||||
|
||||
/* Parse with float32 mode: floating-point values are parsed at single
|
||||
* precision for speed. Breaks strict JSON double-precision conformance
|
||||
* but sufficient for glTF (which stores geometry/animation data as
|
||||
* single-precision floats in buffers anyway). */
|
||||
static tinygltf_json parse_float32(const char *first, const char *last);
|
||||
};
|
||||
|
||||
/* ======================================================================
|
||||
@@ -1245,6 +1433,7 @@ struct cj_parse_ctx {
|
||||
const char *cur;
|
||||
const char *end;
|
||||
int err;
|
||||
int float32_mode; /* 0 = double (default), 1 = float32 */
|
||||
char errmsg[256];
|
||||
};
|
||||
|
||||
@@ -1367,7 +1556,7 @@ static void cj_parse_scalar(cj_parse_ctx *ctx, tinygltf_json *slot) {
|
||||
} else { cj_ctx_error(ctx, "invalid literal 'null'"); }
|
||||
} else if (c == '-' || (c >= '0' && c <= '9')) {
|
||||
int is_int = 0; int64_t ival = 0; double dval = 0.0;
|
||||
const char *next = cj_parse_number(ctx->cur, ctx->end, &is_int, &ival, &dval);
|
||||
const char *next = cj_parse_number(ctx->cur, ctx->end, &is_int, &ival, &dval, ctx->float32_mode);
|
||||
if (!next) { cj_ctx_error(ctx, "invalid number"); return; }
|
||||
ctx->cur = next;
|
||||
slot->destroy_(); slot->init_null_();
|
||||
@@ -1757,10 +1946,11 @@ inline tinygltf_json tinygltf_json::parse(const char *first, const char *last,
|
||||
std::nullptr_t,
|
||||
bool allow_exceptions) {
|
||||
cj_parse_ctx ctx;
|
||||
ctx.cur = first;
|
||||
ctx.end = last;
|
||||
ctx.err = 0;
|
||||
ctx.errmsg[0] = '\0';
|
||||
ctx.cur = first;
|
||||
ctx.end = last;
|
||||
ctx.err = 0;
|
||||
ctx.float32_mode = 0;
|
||||
ctx.errmsg[0] = '\0';
|
||||
|
||||
tinygltf_json result;
|
||||
cj_parse_json(&ctx, &result);
|
||||
@@ -1779,6 +1969,21 @@ inline tinygltf_json tinygltf_json::parse(const char *first, const char *last,
|
||||
return result;
|
||||
}
|
||||
|
||||
inline tinygltf_json tinygltf_json::parse_float32(const char *first, const char *last) {
|
||||
cj_parse_ctx ctx;
|
||||
ctx.cur = first;
|
||||
ctx.end = last;
|
||||
ctx.err = 0;
|
||||
ctx.float32_mode = 1;
|
||||
ctx.errmsg[0] = '\0';
|
||||
|
||||
tinygltf_json result;
|
||||
cj_parse_json(&ctx, &result);
|
||||
|
||||
if (ctx.err) return tinygltf_json();
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ======================================================================
|
||||
* TINYGLTF DETAIL NAMESPACE COMPATIBILITY
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user