mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-08 11:13:50 +00:00
Compare commits
90 Commits
v2.8.23
...
copilot/su
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9a9b1175a | ||
|
|
c870bd5fd6 | ||
|
|
f7bd377a69 | ||
|
|
5d6984b9fd | ||
|
|
3331c6cee2 | ||
|
|
2c7bf2c932 | ||
|
|
2aeac50277 | ||
|
|
78f4a5cfe8 | ||
|
|
aa63297061 | ||
|
|
7163d5ab17 | ||
|
|
f9397d296d | ||
|
|
c4e4155bf7 | ||
|
|
5dfa17d14b | ||
|
|
5b87beb373 | ||
|
|
0ab7e74933 | ||
|
|
247cb388a0 | ||
|
|
eb087e80e7 | ||
|
|
690585fa73 | ||
|
|
73d309ebfa | ||
|
|
4d16d528a5 | ||
|
|
229f2b8c88 | ||
|
|
ad531900cb | ||
|
|
9da2046cba | ||
|
|
ed13b0422a | ||
|
|
1dfcb11442 | ||
|
|
a2b55f008e | ||
|
|
fdf528f9aa | ||
|
|
ebcd8cc4ee | ||
|
|
f6c71cf88b | ||
|
|
5aaa3e4daf | ||
|
|
1117aa7191 | ||
|
|
bdba4dfb4c | ||
|
|
e379d0d60c | ||
|
|
659de95977 | ||
|
|
b1a7736249 | ||
|
|
9ab0d0d5f7 | ||
|
|
fca5da1b37 | ||
|
|
bdc37385f1 | ||
|
|
797bf0e023 | ||
|
|
10ac914244 | ||
|
|
dc6dddac98 | ||
|
|
b548191e41 | ||
|
|
17287c7fcf | ||
|
|
6c948d5bc3 | ||
|
|
d4a4a1b27a | ||
|
|
3d5453ecd0 | ||
|
|
52a453120b | ||
|
|
fc6d78a1b6 | ||
|
|
ae0bac486c | ||
|
|
b19e665747 | ||
|
|
40f6c2b875 | ||
|
|
e8c70dff1d | ||
|
|
1dc37f76ea | ||
|
|
8da66b8ca1 | ||
|
|
81bd50c106 | ||
|
|
6d8bba0d8a | ||
|
|
2aa77e5d0a | ||
|
|
1fac6234d9 | ||
|
|
bcd666fbd4 | ||
|
|
37250b3470 | ||
|
|
7385235e29 | ||
|
|
3564b48760 | ||
|
|
2ad433b68f | ||
|
|
1b517f2b23 | ||
|
|
bd7255e095 | ||
|
|
a5e653e46c | ||
|
|
d530cd410b | ||
|
|
1831424c71 | ||
|
|
5e008af65d | ||
|
|
fbff1f45b5 | ||
|
|
d950e7cd9b | ||
|
|
116d0030f9 | ||
|
|
ff972dcf1b | ||
|
|
8bec431699 | ||
|
|
21485496b1 | ||
|
|
fda7422022 | ||
|
|
decfabd67e | ||
|
|
10b23b6af2 | ||
|
|
fe3cfbe996 | ||
|
|
3b73caa8e8 | ||
|
|
fea6786129 | ||
|
|
fb58f88a4e | ||
|
|
143ff45b61 | ||
|
|
cfbec35dc7 | ||
|
|
4ad8c82c9e | ||
|
|
2e7ba45a6c | ||
|
|
cf9767668a | ||
|
|
8a269aa5e9 | ||
|
|
38614763e9 | ||
|
|
3245906248 |
92
.github/instructions/copilot-instructions.md
vendored
Normal file
92
.github/instructions/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
# Copilot Review Instructions for TinyGLTF
|
||||
|
||||
This document provides guidelines for reviewing code changes in the TinyGLTF repository.
|
||||
|
||||
## Memory Safety
|
||||
|
||||
- **Buffer Overflows**: Check for proper bounds checking when accessing arrays, vectors, and buffers. Verify that buffer sizes are validated before read/write operations.
|
||||
- **Null Pointer Dereferences**: Ensure all pointers are checked for null before dereferencing, especially when handling optional glTF fields.
|
||||
- **Memory Leaks**: Verify proper resource management, including RAII patterns for file handles, image data, and dynamically allocated memory.
|
||||
- **Use-After-Free**: Check for proper lifetime management of objects, especially when dealing with callbacks and asynchronous operations.
|
||||
|
||||
## Error Handling
|
||||
|
||||
- **File I/O**: Verify that all file operations have proper error checking and meaningful error messages.
|
||||
- **JSON Parsing**: Ensure JSON parsing errors are caught and reported with helpful context about the location and nature of the error.
|
||||
- **Resource Loading**: Check that failures in loading images, buffers, and other resources are properly handled and don't cause crashes.
|
||||
- **Error Propagation**: Verify that errors are properly propagated through the call stack with appropriate error messages.
|
||||
|
||||
## glTF 2.0 Specification Compliance
|
||||
|
||||
- **Required Fields**: Ensure all required glTF fields are validated and present.
|
||||
- **Data Types**: Verify that data types match the glTF specification (e.g., component types, accessor types).
|
||||
- **Constraints**: Check that glTF constraints are enforced (e.g., valid ranges for enums, buffer stride requirements).
|
||||
- **Extensions**: Verify proper handling of glTF extensions and that unknown extensions are handled gracefully.
|
||||
- **Validation**: Ensure new features align with the glTF 2.0 specification from the Khronos Group.
|
||||
|
||||
## Cross-Platform Compatibility
|
||||
|
||||
- **Windows**: Check for proper handling of Windows-specific issues (path separators, line endings, file operations).
|
||||
- **Linux**: Verify compatibility with various Linux distributions and compilers (GCC, Clang).
|
||||
- **macOS**: Ensure macOS-specific considerations are addressed (case-sensitive filesystems, Clang compatibility).
|
||||
- **Mobile Platforms**: Consider Android and iOS compatibility where applicable.
|
||||
- **Endianness**: Verify proper handling of byte order when reading binary data.
|
||||
- **Compiler Compatibility**: Ensure code compiles with C++11 standard and supported compilers (MSVC, GCC, Clang).
|
||||
|
||||
## Edge Cases in glTF Parsing
|
||||
|
||||
- **Empty/Minimal Files**: Verify handling of minimal valid glTF files.
|
||||
- **Large Files**: Check for proper handling of large glTF files and buffers without memory exhaustion.
|
||||
- **Malformed Data**: Ensure graceful handling of malformed or invalid glTF data.
|
||||
- **Missing Optional Fields**: Verify correct behavior when optional glTF fields are absent.
|
||||
- **Edge Values**: Check handling of boundary values (e.g., maximum buffer sizes, extreme floating-point values).
|
||||
- **Base64 Encoding**: Verify proper handling of base64-encoded data URIs and invalid encodings.
|
||||
|
||||
## Backwards Compatibility
|
||||
|
||||
- **API Changes**: Ensure public API changes maintain backwards compatibility or are properly deprecated.
|
||||
- **Breaking Changes**: Flag any breaking changes for major version updates and document migration paths.
|
||||
- **Binary Compatibility**: Consider ABI stability for header-only library changes.
|
||||
- **Default Behavior**: Verify that default behavior of existing functionality remains unchanged.
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
- **Parsing Performance**: Check for unnecessary copies, redundant allocations, and inefficient algorithms in parsing logic.
|
||||
- **Memory Usage**: Verify efficient memory usage, especially when loading large glTF files.
|
||||
- **I/O Operations**: Ensure efficient file reading and minimize unnecessary disk access.
|
||||
- **String Operations**: Check for efficient string handling (use of string_view, move semantics).
|
||||
- **STL Usage**: Verify appropriate use of STL containers and algorithms.
|
||||
|
||||
## Documentation
|
||||
|
||||
- **Public API**: Ensure all public functions, classes, and methods have clear documentation comments.
|
||||
- **Parameters**: Verify that function parameters are documented, including expected ranges and constraints.
|
||||
- **Return Values**: Document return values and possible error conditions.
|
||||
- **Examples**: Check that complex features include usage examples.
|
||||
- **Changelog**: Verify that significant changes are documented in release notes or changelog.
|
||||
|
||||
## Testing
|
||||
|
||||
- **Test Coverage**: Ensure new features include appropriate unit tests or integration tests.
|
||||
- **Edge Cases**: Verify that tests cover edge cases and error conditions.
|
||||
- **Cross-Platform Tests**: Check that tests run on all supported platforms.
|
||||
- **Regression Tests**: Ensure bug fixes include regression tests to prevent recurrence.
|
||||
- **Sample Files**: Verify that changes are tested with various valid and invalid glTF sample files.
|
||||
|
||||
## Code Style Consistency
|
||||
|
||||
- **Header-Only Pattern**: Maintain the header-only library structure.
|
||||
- **Naming Conventions**: Follow existing naming conventions (CamelCase for types, snake_case for functions where applicable).
|
||||
- **Formatting**: Adhere to the existing code formatting style (check `.clang-format` if available).
|
||||
- **Include Guards**: Verify proper include guards and header organization.
|
||||
- **Namespace Usage**: Ensure proper use of the `tinygltf` namespace.
|
||||
- **Comments**: Maintain consistent comment style with existing code.
|
||||
- **C++11 Compliance**: Verify that code uses C++11 features appropriately and doesn't require newer standards unless specified.
|
||||
|
||||
## Additional Considerations
|
||||
|
||||
- **Third-Party Dependencies**: Minimize new dependencies; prefer existing dependencies (json.hpp, stb_image).
|
||||
- **Warnings**: Ensure code compiles without warnings on supported compilers.
|
||||
- **const Correctness**: Verify proper use of const for parameters and methods.
|
||||
- **RAII**: Prefer RAII patterns for resource management over manual cleanup.
|
||||
- **noexcept**: Use noexcept appropriately for move constructors and move assignment operators.
|
||||
330
.github/workflows/ci.yml
vendored
Normal file
330
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,330 @@
|
||||
name: Comprehensive CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- release
|
||||
- devel
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- release
|
||||
- devel
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
# Linux x64 - GCC
|
||||
linux-gcc-x64:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux x64 - Clang 21
|
||||
linux-clang-x64:
|
||||
runs-on: ubuntu-24.04
|
||||
name: Linux x64 (Clang 21)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Clang 21
|
||||
run: |
|
||||
wget https://apt.llvm.org/llvm.sh
|
||||
chmod +x llvm.sh
|
||||
sudo ./llvm.sh 21
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
cmake -B build -DCMAKE_C_COMPILER=clang-21 -DCMAKE_CXX_COMPILER=clang++-21 -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux ARM64 - GCC (native)
|
||||
linux-arm64:
|
||||
runs-on: ubuntu-24.04-arm
|
||||
name: Linux ARM64 (GCC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# macOS ARM64 Apple Silicon
|
||||
macos-arm64:
|
||||
runs-on: macos-14
|
||||
name: macOS ARM64 Apple Silicon (Clang)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Windows x64 - MSVC
|
||||
windows-msvc-x64:
|
||||
runs-on: windows-latest
|
||||
name: Windows x64 (MSVC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A x64 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
.\build\Release\loader_example.exe models\Cube\Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
|
||||
# Windows x86 - MSVC
|
||||
windows-msvc-x86:
|
||||
runs-on: windows-latest
|
||||
name: Windows x86 (MSVC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A Win32 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
|
||||
# Windows ARM64 - MSVC (cross-compile)
|
||||
windows-msvc-arm64:
|
||||
runs-on: windows-latest
|
||||
name: Windows ARM64 (MSVC) - Cross-compile
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A ARM64 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
# Windows MinGW - MSYS2
|
||||
windows-mingw-msys2:
|
||||
runs-on: windows-latest
|
||||
name: Windows x64 (MinGW MSYS2)
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup MSYS2
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: UCRT64
|
||||
install: base-devel
|
||||
pacboy: >-
|
||||
cc:p cmake:p ninja:p
|
||||
update: true
|
||||
release: false
|
||||
|
||||
- name: Build with CMake
|
||||
run: |
|
||||
cmake -G"Ninja" -S . -B build
|
||||
cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux -> Windows MinGW Cross-compile
|
||||
linux-mingw-cross:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux→Windows (MinGW Cross) - Build Only
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install MinGW
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential mingw-w64
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
x86_64-w64-mingw32-g++ -std=c++11 -o loader_example.exe loader_example.cc
|
||||
|
||||
# Special Configuration: No Exceptions
|
||||
linux-noexception:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - No Exceptions
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example
|
||||
run: |
|
||||
g++ -DTINYGLTF_NOEXCEPTION -std=c++11 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests
|
||||
run: |
|
||||
cd tests
|
||||
g++ -DTINYGLTF_NOEXCEPTION -I../ -std=c++11 -g -O0 -o tester_noexcept tester.cc
|
||||
./tester_noexcept
|
||||
|
||||
# Special Configuration: Header-Only Mode
|
||||
linux-header-only:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - Header-Only Mode
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build with CMake Header-Only
|
||||
run: |
|
||||
mkdir build
|
||||
cmake -B build -DTINYGLTF_HEADER_ONLY=ON -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Special Configuration: RapidJSON Backend
|
||||
linux-rapidjson:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - RapidJSON Backend
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Clone RapidJSON
|
||||
run: |
|
||||
git clone https://github.com/Tencent/rapidjson
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
cmake -B build -DTINYGLTF_USE_RAPIDJSON=ON -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON -DCMAKE_PREFIX_PATH=$PWD/rapidjson
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Special Configuration: AddressSanitizer
|
||||
linux-asan:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (Clang) - AddressSanitizer
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example with ASan
|
||||
run: |
|
||||
clang++ -fsanitize=address -std=c++11 -g -O1 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests with ASan
|
||||
run: |
|
||||
cd tests
|
||||
clang++ -fsanitize=address -I../ -std=c++11 -g -O1 -o tester tester.cc
|
||||
./tester
|
||||
|
||||
# Special Configuration: UndefinedBehaviorSanitizer
|
||||
linux-ubsan:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (Clang) - UndefinedBehaviorSanitizer
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example with UBSan
|
||||
run: |
|
||||
clang++ -fsanitize=undefined -std=c++11 -g -O1 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests with UBSan
|
||||
run: |
|
||||
cd tests
|
||||
clang++ -fsanitize=undefined -I../ -std=c++11 -g -O1 -o tester tester.cc
|
||||
./tester
|
||||
16
.gitignore
vendored
16
.gitignore
vendored
@@ -21,6 +21,9 @@ premake5.tar.gz
|
||||
*.vcxproj*
|
||||
.vs
|
||||
|
||||
# default cmake build dir
|
||||
build/
|
||||
|
||||
#binary directories
|
||||
bin/
|
||||
obj/
|
||||
@@ -70,6 +73,19 @@ tests/tester
|
||||
tests/tester_noexcept
|
||||
tests/issue-97.gltf
|
||||
tests/issue-261.gltf
|
||||
tests/issue-495-external.gltf
|
||||
# Test-generated output files (written by tester.cc during test run)
|
||||
tests/Cube.gltf
|
||||
tests/Cube.bin
|
||||
tests/Cube.glb
|
||||
tests/Cube_BaseColor.png
|
||||
tests/Cube_MetallicRoughness.png
|
||||
tests/Cube_with_embedded_images.gltf
|
||||
tests/Cube_with_image_files.gltf
|
||||
tests/tmp.glb
|
||||
tests/ issue-236.gltf
|
||||
tests/ issue-236.bin
|
||||
tests/ 2x2 image has multiple spaces.png
|
||||
|
||||
# unignore
|
||||
!Makefile
|
||||
|
||||
@@ -14,8 +14,11 @@ option(TINYGLTF_BUILD_LOADER_EXAMPLE "Build loader_example(load glTF and dump in
|
||||
option(TINYGLTF_BUILD_GL_EXAMPLES "Build GL exampels(requires glfw, OpenGL, etc)" OFF)
|
||||
option(TINYGLTF_BUILD_VALIDATOR_EXAMPLE "Build validator exampe" OFF)
|
||||
option(TINYGLTF_BUILD_BUILDER_EXAMPLE "Build glTF builder example" OFF)
|
||||
option(TINYGLTF_BUILD_TESTS "Build unit tests" OFF)
|
||||
option(TINYGLTF_HEADER_ONLY "On: header-only mode. Off: create tinygltf library(No TINYGLTF_IMPLEMENTATION required in your project)" OFF)
|
||||
option(TINYGLTF_INSTALL "Install tinygltf files during install step. Usually set to OFF if you include tinygltf through add_subdirectory()" ON)
|
||||
option(TINYGLTF_INSTALL_VENDOR "Install vendored nlohmann/json and nothings/stb headers" ON)
|
||||
option(TINYGLTF_USE_CUSTOM_JSON "Use the built-in fast JSON parser (tinygltf_json.h) instead of nlohmann/json" OFF)
|
||||
|
||||
if (TINYGLTF_BUILD_LOADER_EXAMPLE)
|
||||
add_executable(loader_example
|
||||
@@ -36,6 +39,25 @@ if (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
add_subdirectory ( examples/build-gltf )
|
||||
endif (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
|
||||
if (TINYGLTF_BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_executable(tester tests/tester.cc)
|
||||
target_include_directories(tester PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests
|
||||
)
|
||||
add_test(NAME tester COMMAND tester WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
|
||||
# Build and run tests with the custom JSON backend enabled to catch regressions
|
||||
add_executable(tester_customjson tests/tester.cc)
|
||||
target_include_directories(tester_customjson PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/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)
|
||||
endif (TINYGLTF_BUILD_TESTS)
|
||||
|
||||
#
|
||||
# for add_subdirectory and standalone build
|
||||
#
|
||||
@@ -59,21 +81,37 @@ else (TINYGLTF_HEADER_ONLY)
|
||||
)
|
||||
endif (TINYGLTF_HEADER_ONLY)
|
||||
|
||||
if (TINYGLTF_USE_CUSTOM_JSON)
|
||||
if (TINYGLTF_HEADER_ONLY)
|
||||
target_compile_definitions(tinygltf INTERFACE TINYGLTF_USE_CUSTOM_JSON)
|
||||
else ()
|
||||
target_compile_definitions(tinygltf PUBLIC TINYGLTF_USE_CUSTOM_JSON)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (TINYGLTF_INSTALL)
|
||||
install(TARGETS tinygltf EXPORT tinygltfTargets)
|
||||
install(EXPORT tinygltfTargets NAMESPACE tinygltf:: FILE TinyGLTFTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
|
||||
install(EXPORT tinygltfTargets NAMESPACE tinygltf:: FILE TinyGLTFTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinygltf)
|
||||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/TinyGLTFConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinygltf)
|
||||
# Do not install .lib even if !TINYGLTF_HEADER_ONLY
|
||||
|
||||
INSTALL ( FILES
|
||||
json.hpp
|
||||
stb_image.h
|
||||
stb_image_write.h
|
||||
tiny_gltf.h
|
||||
tinygltf_json.h
|
||||
${TINYGLTF_EXTRA_SOUECES}
|
||||
DESTINATION
|
||||
include
|
||||
)
|
||||
|
||||
if(TINYGLTF_INSTALL_VENDOR)
|
||||
INSTALL ( FILES
|
||||
json.hpp
|
||||
stb_image.h
|
||||
stb_image_write.h
|
||||
DESTINATION
|
||||
include
|
||||
)
|
||||
endif()
|
||||
|
||||
endif(TINYGLTF_INSTALL)
|
||||
|
||||
18
README.md
18
README.md
@@ -9,7 +9,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch (b
|
||||
## Status
|
||||
|
||||
Currently TinyGLTF is stable and 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
|
||||
- v2.6.0 Support serializing sparse accessor(Thanks to @fynv).
|
||||
@@ -26,8 +26,6 @@ Currently TinyGLTF is stable and maintenance mode. No drastic changes and featur
|
||||
|
||||
## Builds
|
||||
|
||||
[](https://ci.appveyor.com/project/syoyo/tinygltf)
|
||||
|
||||

|
||||
|
||||
## Features
|
||||
@@ -159,9 +157,10 @@ Model model;
|
||||
TinyGLTF loader;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
std::string filename = "input.gltf";
|
||||
|
||||
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, argv[1]);
|
||||
//bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, argv[1]); // for binary glTF(.glb)
|
||||
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, filename);
|
||||
//bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, filename); // for binary glTF(.glb)
|
||||
|
||||
if (!warn.empty()) {
|
||||
printf("Warn: %s\n", warn.c_str());
|
||||
@@ -172,8 +171,7 @@ if (!err.empty()) {
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
printf("Failed to parse glTF\n");
|
||||
return -1;
|
||||
printf("Failed to parse glTF: %s\n", filename.c_str());
|
||||
}
|
||||
```
|
||||
|
||||
@@ -194,7 +192,6 @@ if (!ret) {
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE `: Disable including `stb_image.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE `: Disable including `stb_image_write.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_USE_RAPIDJSON` : Use RapidJSON as a JSON parser/serializer. RapidJSON files are not included in TinyGLTF repo. Please set an include path to RapidJSON if you enable this feature.
|
||||
* `TINYGLTF_USE_CPP14` : Use C++14 feature(requires C++14 compiler). This may give better performance than C++11.
|
||||
|
||||
|
||||
## CMake options
|
||||
@@ -211,6 +208,11 @@ set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)
|
||||
add_subdirectory(/path/to/tinygltf)
|
||||
```
|
||||
|
||||
NOTE: Using tinygltf as a submodule doesn't automatically add the headers to your include path (as standard for many libraries). To get this functionality, add the following to the CMakeLists.txt file from above:
|
||||
|
||||
```
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE "/path/to/tinygltf")
|
||||
```
|
||||
|
||||
### Saving gltTF 2.0 model
|
||||
|
||||
|
||||
18
appveyor.yml
18
appveyor.yml
@@ -1,18 +0,0 @@
|
||||
version: 0.9.{build}
|
||||
|
||||
image:
|
||||
- Visual Studio 2015
|
||||
|
||||
# scripts that runs after repo cloning.
|
||||
install:
|
||||
- vcsetup.bat
|
||||
|
||||
platform: x64
|
||||
configuration: Release
|
||||
|
||||
build:
|
||||
parallel: true
|
||||
project: TinyGLTFSolution.sln
|
||||
|
||||
after_build:
|
||||
- examples.bat
|
||||
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;
|
||||
}
|
||||
BIN
tests/issue-492.glb
Normal file
BIN
tests/issue-492.glb
Normal file
Binary file not shown.
237
tests/tester.cc
237
tests/tester.cc
@@ -474,7 +474,7 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(!warn.empty()); // relative image path won't exist in tests/
|
||||
REQUIRE(warn.empty());
|
||||
REQUIRE(saved.images.size() == model.images.size());
|
||||
|
||||
// The image uri in CubeImageUriMultipleSpaces.gltf is not encoded and
|
||||
@@ -662,10 +662,11 @@ TEST_CASE("serialize-image-callback", "[issue-394]") {
|
||||
|
||||
auto writer = [](const std::string *basepath, const std::string *filename,
|
||||
const tinygltf::Image *image, bool embedImages,
|
||||
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
|
||||
void *user_pointer) -> bool {
|
||||
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
|
||||
std::string *out_uri, void *user_pointer) -> bool {
|
||||
(void)basepath;
|
||||
(void)image;
|
||||
(void)fs;
|
||||
(void)uri_cb;
|
||||
REQUIRE(*filename == "foo");
|
||||
REQUIRE(embedImages == true);
|
||||
@@ -699,12 +700,13 @@ TEST_CASE("serialize-image-failure", "[issue-394]") {
|
||||
|
||||
auto writer = [](const std::string *basepath, const std::string *filename,
|
||||
const tinygltf::Image *image, bool embedImages,
|
||||
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
|
||||
void *user_pointer) -> bool {
|
||||
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
|
||||
std::string *out_uri, void *user_pointer) -> bool {
|
||||
(void)basepath;
|
||||
(void)filename;
|
||||
(void)image;
|
||||
(void)embedImages;
|
||||
(void)fs;
|
||||
(void)uri_cb;
|
||||
(void)out_uri;
|
||||
(void)user_pointer;
|
||||
@@ -1056,3 +1058,228 @@ TEST_CASE("serialize-lods", "[lods]") {
|
||||
CHECK(nodeWithoutLods.extensions.count("MSFT_lod") == 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("write-image-issue", "[issue-473]") {
|
||||
std::string err;
|
||||
std::string warn;
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
|
||||
REQUIRE(ok);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(warn.empty());
|
||||
|
||||
REQUIRE(model.images.size() == 2);
|
||||
REQUIRE(model.images[0].uri == "Cube_BaseColor.png");
|
||||
REQUIRE(model.images[1].uri == "Cube_MetallicRoughness.png");
|
||||
|
||||
REQUIRE_FALSE(model.images[0].image.empty());
|
||||
REQUIRE_FALSE(model.images[1].image.empty());
|
||||
|
||||
ok = ctx.WriteGltfSceneToFile(&model, "Cube.gltf");
|
||||
REQUIRE(ok);
|
||||
|
||||
for (const auto& image : model.images) {
|
||||
std::fstream file(image.uri);
|
||||
CHECK(file.good());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("images-as-is", "[issue-487]") {
|
||||
std::string err;
|
||||
std::string warn;
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
ctx.SetImagesAsIs(true);
|
||||
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
|
||||
REQUIRE(ok);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(warn.empty());
|
||||
|
||||
for (const auto& image : model.images) {
|
||||
CHECK(image.as_is == true);
|
||||
CHECK_FALSE(image.uri.empty());
|
||||
CHECK_FALSE(image.image.empty());
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Make sure we can decode the images
|
||||
int w = -1, h = -1, component = -1;
|
||||
unsigned char *data = stbi_load_from_memory(image.image.data(), static_cast<int>(image.image.size()), &w, &h, &component, 0);
|
||||
CHECK(data != nullptr);
|
||||
CHECK(w == 512);
|
||||
CHECK(h == 512);
|
||||
CHECK(component >= 3);
|
||||
stbi_image_free(data);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Write glTF model to disk, and images as separate files
|
||||
{
|
||||
ok = ctx.WriteGltfSceneToFile(&model, "Cube_with_image_files.gltf");
|
||||
REQUIRE(ok);
|
||||
|
||||
// All the images should have been written to disk with their original data
|
||||
for (const auto& image : model.images) {
|
||||
// Make sure the image files exist
|
||||
{
|
||||
std::fstream file(image.uri);
|
||||
CHECK(file.good());
|
||||
} // Close file before stbi_load (Windows sharing violation fix)
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Make sure we can load the images
|
||||
int w = -1, h = -1, component = -1;
|
||||
unsigned char *data = stbi_load(image.uri.c_str(), &w, &h, &component, 0);
|
||||
CHECK(data != nullptr);
|
||||
CHECK(w == 512);
|
||||
CHECK(h == 512);
|
||||
CHECK(component >= 3);
|
||||
stbi_image_free(data);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Write glTF model to disk, and embed images as data URIs
|
||||
{
|
||||
ok = ctx.WriteGltfSceneToFile(&model, "Cube_with_embedded_images.gltf", true, false);
|
||||
REQUIRE(ok);
|
||||
|
||||
// Load above model again, and check if the images are loaded properly
|
||||
tinygltf::Model embeddedImages;
|
||||
ctx.SetImagesAsIs(false);
|
||||
bool ok = ctx.LoadASCIIFromFile(&embeddedImages, &err, &warn, "Cube_with_embedded_images.gltf");
|
||||
REQUIRE(ok);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(warn.empty());
|
||||
|
||||
for (const auto& image : embeddedImages.images) {
|
||||
CHECK(image.as_is == false);
|
||||
CHECK_FALSE(image.mimeType.empty());
|
||||
CHECK_FALSE(image.image.empty());
|
||||
CHECK(image.width == 512);
|
||||
CHECK(image.height == 512);
|
||||
CHECK(image.component >= 3);
|
||||
}
|
||||
}
|
||||
|
||||
// Write glTF model to disk, as GLB
|
||||
{
|
||||
ok = ctx.WriteGltfSceneToFile(&model, "Cube.glb", true, true, true, true);
|
||||
REQUIRE(ok);
|
||||
|
||||
// Load above model again, and check if the images are loaded properly
|
||||
tinygltf::Model glbModel;
|
||||
ctx.SetImagesAsIs(false);
|
||||
bool ok = ctx.LoadBinaryFromFile(&glbModel, &err, &warn, "Cube.glb");
|
||||
REQUIRE(ok);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(warn.empty());
|
||||
|
||||
for (const auto& image : glbModel.images) {
|
||||
CHECK(image.as_is == false);
|
||||
CHECK_FALSE(image.mimeType.empty());
|
||||
CHECK_FALSE(image.image.empty());
|
||||
CHECK(image.width == 512);
|
||||
CHECK(image.height == 512);
|
||||
CHECK(image.component >= 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("inverse-bind-matrices-optional", "[issue-492]") {
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
bool ret = ctx.LoadBinaryFromFile(&model, &err, &warn, "issue-492.glb");
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN:" << warn << std::endl;
|
||||
}
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR:" << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(err.empty());
|
||||
}
|
||||
|
||||
bool LoadImageData(tinygltf::Image * /* image */, const int /* image_idx */, std::string * /* err */,
|
||||
std::string * /* warn */, int /* req_width */, int /* req_height */,
|
||||
const unsigned char * /* bytes */, int /* size */, void * /*user_data */) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteImageData(const std::string * /* basepath */, const std::string * /* filename */,
|
||||
const tinygltf::Image *image, bool /* embedImages */,
|
||||
const tinygltf::FsCallbacks * /* fs_cb */, const tinygltf::URICallbacks * /* uri_cb */,
|
||||
std::string * /* out_uri */, void * user_pointer) {
|
||||
REQUIRE(user_pointer != nullptr);
|
||||
auto counter = static_cast<int*>(user_pointer);
|
||||
*counter = *counter + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_CASE("empty-images-not-written", "[issue-495]") {
|
||||
std::string err;
|
||||
std::string warn;
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
|
||||
ctx.SetImageLoader(LoadImageData, nullptr);
|
||||
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
|
||||
REQUIRE(ok);
|
||||
REQUIRE(err.empty());
|
||||
REQUIRE(warn.empty());
|
||||
|
||||
CHECK(model.images.size() == 2);
|
||||
for (const auto& image : model.images) {
|
||||
// No data loaded or decoded
|
||||
CHECK(image.image.empty());
|
||||
// The URI is kept
|
||||
CHECK_FALSE(image.uri.empty());
|
||||
// The URI should not be a data URI
|
||||
CHECK(image.uri.find("data:") != 0);
|
||||
}
|
||||
|
||||
// Now write the loaded model
|
||||
int counter = 0;
|
||||
ctx.SetImageWriter(WriteImageData, &counter);
|
||||
ok = ctx.WriteGltfSceneToFile(&model, "issue-495-external.gltf");
|
||||
CHECK(ok);
|
||||
// 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;
|
||||
}
|
||||
361
tiny_gltf.h
361
tiny_gltf.h
@@ -25,7 +25,7 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
// Version: - v2.8.10
|
||||
// Version: - v2.9.*
|
||||
// See https://github.com/syoyo/tinygltf/releases for release history.
|
||||
//
|
||||
// Tiny glTF loader is using following third party libraries:
|
||||
@@ -50,12 +50,6 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// Auto-detect C++14 standard version
|
||||
#if !defined(TINYGLTF_USE_CPP14) && defined(__cplusplus) && \
|
||||
(__cplusplus >= 201402L)
|
||||
#define TINYGLTF_USE_CPP14
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
|
||||
#include <android/asset_manager.h>
|
||||
@@ -649,9 +643,7 @@ struct Image {
|
||||
// When this flag is true, data is stored to `image` in as-is format(e.g. jpeg
|
||||
// compressed for "image/jpeg" mime) This feature is good if you use custom
|
||||
// image loader function. (e.g. delayed decoding of images for faster glTF
|
||||
// parsing) Default parser for Image does not provide as-is loading feature at
|
||||
// the moment. (You can manipulate this by providing your own LoadImageData
|
||||
// function)
|
||||
// parsing).
|
||||
bool as_is{false};
|
||||
|
||||
Image() = default;
|
||||
@@ -836,20 +828,20 @@ struct Accessor {
|
||||
maxValues; // optional. integer value is promoted to double
|
||||
|
||||
struct Sparse {
|
||||
int count;
|
||||
bool isSparse;
|
||||
int count{0};
|
||||
bool isSparse{false};
|
||||
struct {
|
||||
size_t byteOffset;
|
||||
int bufferView;
|
||||
int componentType; // a TINYGLTF_COMPONENT_TYPE_ value
|
||||
size_t byteOffset{0};
|
||||
int bufferView{-1};
|
||||
int componentType{-1}; // a TINYGLTF_COMPONENT_TYPE_ value
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
std::string extras_json_string;
|
||||
std::string extensions_json_string;
|
||||
} indices;
|
||||
struct {
|
||||
int bufferView;
|
||||
size_t byteOffset;
|
||||
int bufferView{-1};
|
||||
size_t byteOffset{0};
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
std::string extras_json_string;
|
||||
@@ -900,11 +892,7 @@ struct Accessor {
|
||||
// unreachable return 0;
|
||||
}
|
||||
|
||||
Accessor()
|
||||
|
||||
{
|
||||
sparse.isSparse = false;
|
||||
}
|
||||
Accessor() = default;
|
||||
DEFAULT_METHODS(Accessor)
|
||||
bool operator==(const tinygltf::Accessor &) const;
|
||||
};
|
||||
@@ -1292,39 +1280,6 @@ struct URICallbacks {
|
||||
void *user_data; // An argument that is passed to all uri callbacks
|
||||
};
|
||||
|
||||
///
|
||||
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
|
||||
///
|
||||
using LoadImageDataFunction = std::function<bool(
|
||||
Image * /* image */, const int /* image_idx */, std::string * /* err */,
|
||||
std::string * /* warn */, int /* req_width */, int /* req_height */,
|
||||
const unsigned char * /* bytes */, int /* size */, void * /*user_data */)>;
|
||||
|
||||
///
|
||||
/// WriteImageDataFunction type. Signature for custom image writing callbacks.
|
||||
/// The out_uri parameter becomes the URI written to the gltf and may reference
|
||||
/// a file or contain a data URI.
|
||||
///
|
||||
using WriteImageDataFunction = std::function<bool(
|
||||
const std::string * /* basepath */, const std::string * /* filename */,
|
||||
const Image *image, bool /* embedImages */,
|
||||
const URICallbacks * /* uri_cb */, std::string * /* out_uri */,
|
||||
void * /* user_pointer */)>;
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Declaration of default image loader callback
|
||||
bool LoadImageData(Image *image, const int image_idx, std::string *err,
|
||||
std::string *warn, int req_width, int req_height,
|
||||
const unsigned char *bytes, int size, void *);
|
||||
#endif
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE_WRITE
|
||||
// Declaration of default image writer callback
|
||||
bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
const Image *image, bool embedImages,
|
||||
const URICallbacks *uri_cb, std::string *out_uri, void *);
|
||||
#endif
|
||||
|
||||
///
|
||||
/// FileExistsFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
@@ -1396,6 +1351,40 @@ bool GetFileSizeInBytes(size_t *filesize_out, std::string *err,
|
||||
const std::string &filepath, void *);
|
||||
#endif
|
||||
|
||||
///
|
||||
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
|
||||
///
|
||||
using LoadImageDataFunction = std::function<bool(
|
||||
Image * /* image */, const int /* image_idx */, std::string * /* err */,
|
||||
std::string * /* warn */, int /* req_width */, int /* req_height */,
|
||||
const unsigned char * /* bytes */, int /* size */, void * /*user_data */)>;
|
||||
|
||||
///
|
||||
/// WriteImageDataFunction type. Signature for custom image writing callbacks.
|
||||
/// The out_uri parameter becomes the URI written to the gltf and may reference
|
||||
/// a file or contain a data URI.
|
||||
///
|
||||
using WriteImageDataFunction = std::function<bool(
|
||||
const std::string * /* basepath */, const std::string * /* filename */,
|
||||
const Image *image, bool /* embedImages */,
|
||||
const FsCallbacks * /* fs_cb */, const URICallbacks * /* uri_cb */,
|
||||
std::string * /* out_uri */, void * /* user_pointer */)>;
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Declaration of default image loader callback
|
||||
bool LoadImageData(Image *image, const int image_idx, std::string *err,
|
||||
std::string *warn, int req_width, int req_height,
|
||||
const unsigned char *bytes, int size, void *);
|
||||
#endif
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE_WRITE
|
||||
// Declaration of default image writer callback
|
||||
bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
const Image *image, bool embedImages,
|
||||
const FsCallbacks* fs_cb, const URICallbacks *uri_cb,
|
||||
std::string *out_uri, void *);
|
||||
#endif
|
||||
|
||||
///
|
||||
/// glTF Parser/Serializer context.
|
||||
///
|
||||
@@ -1543,6 +1532,17 @@ class TinyGLTF {
|
||||
preserve_image_channels_ = onoff;
|
||||
}
|
||||
|
||||
bool GetPreserveImageChannels() const { return preserve_image_channels_; }
|
||||
|
||||
///
|
||||
/// Specifiy whether image data is decoded/decompressed during load, or left as is
|
||||
///
|
||||
void SetImagesAsIs(bool onoff) {
|
||||
images_as_is_ = onoff;
|
||||
}
|
||||
|
||||
bool GetImagesAsIs() const { return images_as_is_; }
|
||||
|
||||
///
|
||||
/// Set maximum allowed external file size in bytes.
|
||||
/// Default: 2GB
|
||||
@@ -1554,8 +1554,6 @@ class TinyGLTF {
|
||||
|
||||
size_t GetMaxExternalFileSize() const { return max_external_file_size_; }
|
||||
|
||||
bool GetPreserveImageChannels() const { return preserve_image_channels_; }
|
||||
|
||||
private:
|
||||
///
|
||||
/// Loads glTF asset from string(memory).
|
||||
@@ -1580,6 +1578,8 @@ class TinyGLTF {
|
||||
bool preserve_image_channels_ = false; /// Default false(expand channels to
|
||||
/// RGBA) for backward compatibility.
|
||||
|
||||
bool images_as_is_ = false; /// Default false (decode/decompress images)
|
||||
|
||||
size_t max_external_file_size_{
|
||||
size_t((std::numeric_limits<int32_t>::max)())}; // Default 2GB
|
||||
|
||||
@@ -1711,7 +1711,11 @@ class TinyGLTF {
|
||||
#endif // __GNUC__
|
||||
|
||||
#ifndef TINYGLTF_NO_INCLUDE_JSON
|
||||
#ifndef TINYGLTF_USE_RAPIDJSON
|
||||
#ifdef TINYGLTF_USE_CUSTOM_JSON
|
||||
#ifndef TINYGLTF_NO_INCLUDE_CUSTOM_JSON
|
||||
#include "tinygltf_json.h"
|
||||
#endif
|
||||
#elif !defined(TINYGLTF_USE_RAPIDJSON)
|
||||
#include "json.hpp"
|
||||
#else
|
||||
#ifndef TINYGLTF_NO_INCLUDE_RAPIDJSON
|
||||
@@ -1799,7 +1803,10 @@ class TinyGLTF {
|
||||
|
||||
namespace tinygltf {
|
||||
namespace detail {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
#ifdef TINYGLTF_USE_CUSTOM_JSON
|
||||
// Types and JsonParse are provided by tinygltf_json.h (already included above)
|
||||
// via 'namespace tinygltf { namespace detail { ... } }' declarations.
|
||||
#elif defined(TINYGLTF_USE_RAPIDJSON)
|
||||
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
|
||||
// This uses the RapidJSON CRTAllocator. It is thread safe and multiple
|
||||
@@ -1871,6 +1878,7 @@ using json_const_array_iterator = json_const_iterator;
|
||||
using JsonDocument = json;
|
||||
#endif
|
||||
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
void JsonParse(JsonDocument &doc, const char *str, size_t length,
|
||||
bool throwExc = false) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -1880,6 +1888,7 @@ void JsonParse(JsonDocument &doc, const char *str, size_t length,
|
||||
doc = detail::json::parse(str, str + length, nullptr, throwExc);
|
||||
#endif
|
||||
}
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
} // namespace detail
|
||||
} // namespace tinygltf
|
||||
|
||||
@@ -1905,6 +1914,9 @@ struct LoadImageDataOption {
|
||||
// channels) default `false`(channels are expanded to RGBA for backward
|
||||
// compatibility).
|
||||
bool preserve_channels{false};
|
||||
// true: do not decode/decompress image data.
|
||||
// default `false`: decode/decompress image data.
|
||||
bool as_is{false};
|
||||
};
|
||||
|
||||
// Equals function for Value, for recursivity
|
||||
@@ -2448,9 +2460,8 @@ inline unsigned char from_hex(unsigned char ch) {
|
||||
}
|
||||
|
||||
static const std::string urldecode(const std::string &str) {
|
||||
using namespace std;
|
||||
string result;
|
||||
string::size_type i;
|
||||
std::string result;
|
||||
std::string::size_type i;
|
||||
for (i = 0; i < str.size(); ++i) {
|
||||
if (str[i] == '+') {
|
||||
result += ' ';
|
||||
@@ -2613,48 +2624,65 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err,
|
||||
|
||||
int w = 0, h = 0, comp = 0, req_comp = 0;
|
||||
|
||||
unsigned char *data = nullptr;
|
||||
// Try to decode image header
|
||||
if (!stbi_info_from_memory(bytes, size, &w, &h, &comp)) {
|
||||
// On failure, if we load images as is, we just warn.
|
||||
std::string* msgOut = option.as_is ? warn : err;
|
||||
if (msgOut) {
|
||||
(*msgOut) +=
|
||||
"Unknown image format. STB cannot decode image header for image[" +
|
||||
std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
|
||||
}
|
||||
if (!option.as_is) {
|
||||
// If we decode images, error out.
|
||||
return false;
|
||||
} else {
|
||||
// If we load images as is, we copy the image data,
|
||||
// set all image properties to invalid, and report success.
|
||||
image->width = image->height = image->component = -1;
|
||||
image->bits = image->pixel_type = -1;
|
||||
image->image.resize(static_cast<size_t>(size));
|
||||
std::copy(bytes, bytes + size, image->image.begin());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int bits = 8;
|
||||
int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
||||
|
||||
if (stbi_is_16_bit_from_memory(bytes, size)) {
|
||||
bits = 16;
|
||||
pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
|
||||
}
|
||||
|
||||
// preserve_channels true: Use channels stored in the image file.
|
||||
// false: force 32-bit textures for common Vulkan compatibility. It appears
|
||||
// that some GPU drivers do not support 24-bit images for Vulkan
|
||||
req_comp = option.preserve_channels ? 0 : 4;
|
||||
int bits = 8;
|
||||
int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
||||
req_comp = (option.preserve_channels || option.as_is) ? 0 : 4;
|
||||
|
||||
// It is possible that the image we want to load is a 16bit per channel image
|
||||
// We are going to attempt to load it as 16bit per channel, and if it worked,
|
||||
// set the image data accordingly. We are casting the returned pointer into
|
||||
// unsigned char, because we are representing "bytes". But we are updating
|
||||
// the Image metadata to signal that this image uses 2 bytes (16bits) per
|
||||
// channel:
|
||||
if (stbi_is_16_bit_from_memory(bytes, size)) {
|
||||
data = reinterpret_cast<unsigned char *>(
|
||||
stbi_load_16_from_memory(bytes, size, &w, &h, &comp, req_comp));
|
||||
if (data) {
|
||||
bits = 16;
|
||||
pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
|
||||
unsigned char* data = nullptr;
|
||||
// Perform image decoding if requested
|
||||
if (!option.as_is) {
|
||||
// If the image is marked as 16 bit per channel, attempt to decode it as such first.
|
||||
// If that fails, we are going to attempt to load it as 8 bit per channel image.
|
||||
if (bits == 16) {
|
||||
data = reinterpret_cast<unsigned char *>(stbi_load_16_from_memory(bytes, size, &w, &h, &comp, req_comp));
|
||||
}
|
||||
}
|
||||
|
||||
// at this point, if data is still NULL, it means that the image wasn't
|
||||
// 16bit per channel, we are going to load it as a normal 8bit per channel
|
||||
// image as we used to do:
|
||||
// if image cannot be decoded, ignore parsing and keep it by its path
|
||||
// don't break in this case
|
||||
// FIXME we should only enter this function if the image is embedded. If
|
||||
// image->uri references
|
||||
// an image file, it should be left as it is. Image loading should not be
|
||||
// mandatory (to support other formats)
|
||||
if (!data) data = stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp);
|
||||
if (!data) {
|
||||
// NOTE: you can use `warn` instead of `err`
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"Unknown image format. STB cannot decode image data for image[" +
|
||||
std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
|
||||
// Load as 8 bit per channel data
|
||||
if (!data) {
|
||||
data = stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp);
|
||||
if (!data) {
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"Unknown image format. STB cannot decode image data for image[" +
|
||||
std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// If we were succesful, mark as 8 bit
|
||||
bits = 8;
|
||||
pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((w < 1) || (h < 1)) {
|
||||
@@ -2700,10 +2728,20 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err,
|
||||
image->component = comp;
|
||||
image->bits = bits;
|
||||
image->pixel_type = pixel_type;
|
||||
image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8));
|
||||
std::copy(data, data + w * h * comp * (bits / 8), image->image.begin());
|
||||
stbi_image_free(data);
|
||||
image->as_is = option.as_is;
|
||||
|
||||
if (option.as_is) {
|
||||
// Store the original image data
|
||||
image->image.resize(static_cast<size_t>(size));
|
||||
std::copy(bytes, bytes + size, image->image.begin());
|
||||
}
|
||||
else {
|
||||
// Store the decoded image data
|
||||
image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8));
|
||||
std::copy(data, data + w * h * comp * (bits / 8), image->image.begin());
|
||||
}
|
||||
|
||||
stbi_image_free(data);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -2725,36 +2763,51 @@ static void WriteToMemory_stbi(void *context, void *data, int size) {
|
||||
|
||||
bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
const Image *image, bool embedImages,
|
||||
const URICallbacks *uri_cb, std::string *out_uri,
|
||||
void *fsPtr) {
|
||||
const FsCallbacks* fs_cb, const URICallbacks *uri_cb,
|
||||
std::string *out_uri, void *) {
|
||||
// Early out on empty images, report the original uri if the image was not written.
|
||||
if (image->image.empty()) {
|
||||
*out_uri = *filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::string ext = GetFilePathExtension(*filename);
|
||||
|
||||
// Write image to temporary buffer
|
||||
std::string header;
|
||||
std::vector<unsigned char> data;
|
||||
|
||||
if (ext == "png") {
|
||||
if ((image->bits != 8) ||
|
||||
(image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)) {
|
||||
// Unsupported pixel format
|
||||
return false;
|
||||
}
|
||||
// If the image data is already encoded, take it as is
|
||||
if (image->as_is) {
|
||||
data = image->image;
|
||||
}
|
||||
|
||||
if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
image->height, image->component,
|
||||
&image->image[0], 0)) {
|
||||
return false;
|
||||
if (ext == "png") {
|
||||
if (!image->as_is) {
|
||||
if ((image->bits != 8) ||
|
||||
(image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)) {
|
||||
// Unsupported pixel format
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
image->height, image->component,
|
||||
&image->image[0], 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
header = "data:image/png;base64,";
|
||||
} else if (ext == "jpg") {
|
||||
if (!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
if (!image->as_is &&
|
||||
!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
image->height, image->component,
|
||||
&image->image[0], 100)) {
|
||||
return false;
|
||||
}
|
||||
header = "data:image/jpeg;base64,";
|
||||
} else if (ext == "bmp") {
|
||||
if (!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
if (!image->as_is &&
|
||||
!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width,
|
||||
image->height, image->component,
|
||||
&image->image[0])) {
|
||||
return false;
|
||||
@@ -2775,12 +2828,11 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
}
|
||||
} else {
|
||||
// Write image to disc
|
||||
FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr);
|
||||
if ((fs != nullptr) && (fs->WriteWholeFile != nullptr)) {
|
||||
if ((fs_cb != nullptr) && (fs_cb->WriteWholeFile != nullptr)) {
|
||||
const std::string imagefilepath = JoinPath(*basepath, *filename);
|
||||
std::string writeError;
|
||||
if (!fs->WriteWholeFile(&writeError, imagefilepath, data,
|
||||
fs->user_data)) {
|
||||
if (!fs_cb->WriteWholeFile(&writeError, imagefilepath, data,
|
||||
fs_cb->user_data)) {
|
||||
// Could not write image file to disc; Throw error ?
|
||||
return false;
|
||||
}
|
||||
@@ -3233,6 +3285,7 @@ static std::string MimeToExt(const std::string &mimeType) {
|
||||
|
||||
static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||
int index, bool embedImages,
|
||||
const FsCallbacks *fs_cb,
|
||||
const URICallbacks *uri_cb,
|
||||
const WriteImageDataFunction& WriteImageData,
|
||||
void *user_data, std::string *out_uri) {
|
||||
@@ -3260,13 +3313,14 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||
filename = std::to_string(index) + "." + ext;
|
||||
}
|
||||
|
||||
// If callback is set and image data exists, modify image data object. If
|
||||
// image data does not exist, this is not considered a failure and the
|
||||
// original uri should be maintained.
|
||||
// If callback is set, modify image data object.
|
||||
// Note that the callback is also invoked for images without data.
|
||||
// The default callback implementation simply returns true for
|
||||
// empty images and sets the out URI to filename.
|
||||
bool imageWritten = false;
|
||||
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
|
||||
if (WriteImageData != nullptr && !filename.empty()) {
|
||||
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
|
||||
uri_cb, out_uri, user_data);
|
||||
fs_cb, uri_cb, out_uri, user_data);
|
||||
if (!imageWritten) {
|
||||
return false;
|
||||
}
|
||||
@@ -3391,6 +3445,7 @@ bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
namespace detail {
|
||||
bool GetInt(const detail::json &o, int &val) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -3614,6 +3669,7 @@ std::string JsonToString(const detail::json &o, int spacing = -1) {
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
|
||||
static bool ParseJsonAsValue(Value *ret, const detail::json &o) {
|
||||
Value val{};
|
||||
@@ -4306,7 +4362,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
// Just only save some information here. Loading actual image data from
|
||||
// bufferView is done after this `ParseImage` function.
|
||||
image->bufferView = bufferView;
|
||||
image->mimeType = mime_type;
|
||||
image->mimeType = std::move( mime_type );
|
||||
image->width = width;
|
||||
image->height = height;
|
||||
|
||||
@@ -4338,7 +4394,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
}
|
||||
} else {
|
||||
// Assume external file
|
||||
// Keep texture path (for textures that cannot be decoded)
|
||||
// Unconditionally keep the external URI of the image
|
||||
image->uri = uri;
|
||||
#ifdef TINYGLTF_NO_EXTERNAL_IMAGE
|
||||
return true;
|
||||
@@ -4385,6 +4441,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return LoadImageData(image, image_idx, err, warn, 0, 0, &img.at(0),
|
||||
static_cast<int>(img.size()), load_image_user_data);
|
||||
}
|
||||
@@ -5204,7 +5261,7 @@ static bool ParseNode(Node *node, std::string *err, const detail::json &o,
|
||||
if (node->extensions.count("MSFT_lod") != 0) {
|
||||
auto const &msft_lod_ext = node->extensions["MSFT_lod"];
|
||||
if (msft_lod_ext.Has("ids")) {
|
||||
auto idsArr = msft_lod_ext.Get("ids");
|
||||
const auto &idsArr = msft_lod_ext.Get("ids");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5233,7 +5290,7 @@ static bool ParseScene(Scene *scene, std::string *err, const detail::json &o,
|
||||
if (scene->extensions.count("KHR_audio") != 0) {
|
||||
auto const &audio_ext = scene->extensions["KHR_audio"];
|
||||
if (audio_ext.Has("emitters")) {
|
||||
auto emittersArr = audio_ext.Get("emitters");
|
||||
const auto &emittersArr = audio_ext.Get("emitters");
|
||||
for (size_t i = 0; i < emittersArr.ArrayLen(); ++i) {
|
||||
scene->audioEmitters.emplace_back(emittersArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5269,7 +5326,7 @@ static bool ParsePbrMetallicRoughness(
|
||||
}
|
||||
return false;
|
||||
}
|
||||
pbr->baseColorFactor = baseColorFactor;
|
||||
pbr->baseColorFactor = std::move( baseColorFactor );
|
||||
}
|
||||
|
||||
{
|
||||
@@ -5421,7 +5478,7 @@ static bool ParseMaterial(Material *material, std::string *err, std::string *war
|
||||
if (material->extensions.count("MSFT_lod") != 0) {
|
||||
auto const &msft_lod_ext = material->extensions["MSFT_lod"];
|
||||
if (msft_lod_ext.Has("ids")) {
|
||||
auto idsArr = msft_lod_ext.Get("ids");
|
||||
const auto &idsArr = msft_lod_ext.Get("ids");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5548,7 +5605,7 @@ static bool ParseAnimation(Animation *animation, std::string *err,
|
||||
}
|
||||
sampler.input = inputIndex;
|
||||
sampler.output = outputIndex;
|
||||
ParseExtrasAndExtensions(&sampler, err, o,
|
||||
ParseExtrasAndExtensions(&sampler, err, s,
|
||||
store_original_json_for_extras_and_extensions);
|
||||
|
||||
animation->samplers.emplace_back(std::move(sampler));
|
||||
@@ -5611,7 +5668,7 @@ static bool ParseSkin(Skin *skin, std::string *err, const detail::json &o,
|
||||
skin->skeleton = skeleton;
|
||||
|
||||
int invBind = -1;
|
||||
ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin");
|
||||
ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", false, "Skin");
|
||||
skin->inverseBindMatrices = invBind;
|
||||
|
||||
ParseExtrasAndExtensions(skin, err, o,
|
||||
@@ -6059,16 +6116,8 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
}
|
||||
}
|
||||
|
||||
model->buffers.clear();
|
||||
model->bufferViews.clear();
|
||||
model->accessors.clear();
|
||||
model->meshes.clear();
|
||||
model->cameras.clear();
|
||||
model->nodes.clear();
|
||||
model->extensionsUsed.clear();
|
||||
model->extensionsRequired.clear();
|
||||
model->extensions.clear();
|
||||
model->defaultScene = -1;
|
||||
// Reset the model
|
||||
(*model) = Model();
|
||||
|
||||
// 1. Parse Asset
|
||||
{
|
||||
@@ -6359,6 +6408,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
load_image_user_data = load_image_user_data_;
|
||||
} else {
|
||||
load_image_option.preserve_channels = preserve_image_channels_;
|
||||
load_image_option.as_is = images_as_is_;
|
||||
load_image_user_data = reinterpret_cast<void *>(&load_image_option);
|
||||
}
|
||||
|
||||
@@ -6403,6 +6453,15 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
return false;
|
||||
}
|
||||
const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
|
||||
if (bufferView.byteOffset >= buffer.data.size()) {
|
||||
if (err) {
|
||||
std::stringstream ss;
|
||||
ss << "image[" << idx << "] bufferView \"" << image.bufferView
|
||||
<< "\" indexed out of bounds of its buffer." << std::endl;
|
||||
(*err) += ss.str();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (LoadImageData == nullptr) {
|
||||
if (err) {
|
||||
@@ -6784,7 +6843,7 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
// 'SHOULD' in glTF spec means 'RECOMMENDED',
|
||||
// So there is a situation that Chunk1(BIN) is composed of zero-sized BIN data
|
||||
// (chunksize(0) + binformat(BIN) = 8bytes).
|
||||
//
|
||||
//
|
||||
if ((header_and_json_size + 8ull) > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) =
|
||||
@@ -6914,6 +6973,7 @@ bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err,
|
||||
///////////////////////
|
||||
// GLTF Serialization
|
||||
///////////////////////
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
namespace detail {
|
||||
detail::json JsonFromString(const char *s) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -6986,6 +7046,7 @@ void JsonReserveArray(detail::json &o, size_t s) {
|
||||
(void)(s);
|
||||
}
|
||||
} // namespace detail
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
|
||||
// typedef std::pair<std::string, detail::json> json_object_pair;
|
||||
|
||||
@@ -7157,6 +7218,7 @@ static void SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
|
||||
static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
const std::string &binFilename) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(__GLIBCXX__) // mingw
|
||||
int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(),
|
||||
@@ -7185,6 +7247,9 @@ static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
// write empty file.
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0 // FIXME(syoyo): not used. will be removed in the future release.
|
||||
@@ -8406,6 +8471,7 @@ static bool WriteGltfStream(std::ostream &stream, const std::string &content) {
|
||||
|
||||
static bool WriteGltfFile(const std::string &output,
|
||||
const std::string &content) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str());
|
||||
@@ -8425,6 +8491,9 @@ static bool WriteGltfFile(const std::string &output,
|
||||
if (!gltfFile.is_open()) return false;
|
||||
#endif
|
||||
return WriteGltfStream(gltfFile, content);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool WriteBinaryGltfStream(std::ostream &stream,
|
||||
@@ -8491,6 +8560,7 @@ static bool WriteBinaryGltfStream(std::ostream &stream,
|
||||
static bool WriteBinaryGltfFile(const std::string &output,
|
||||
const std::string &content,
|
||||
const std::vector<unsigned char> &binBuffer) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary);
|
||||
@@ -8507,6 +8577,9 @@ static bool WriteBinaryGltfFile(const std::string &output,
|
||||
std::ofstream gltfFile(output.c_str(), std::ios::binary);
|
||||
#endif
|
||||
return WriteBinaryGltfStream(gltfFile, content, binBuffer);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
|
||||
@@ -8547,7 +8620,7 @@ bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
|
||||
// we
|
||||
std::string uri;
|
||||
if (!UpdateImageObject(model->images[i], dummystring, int(i), true,
|
||||
&uri_cb, this->WriteImageData,
|
||||
&fs, &uri_cb, this->WriteImageData,
|
||||
this->write_image_user_data_, &uri)) {
|
||||
return false;
|
||||
}
|
||||
@@ -8655,7 +8728,7 @@ bool TinyGLTF::WriteGltfSceneToFile(const Model *model,
|
||||
|
||||
std::string uri;
|
||||
if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
|
||||
&uri_cb, this->WriteImageData,
|
||||
&fs, &uri_cb, this->WriteImageData,
|
||||
this->write_image_user_data_, &uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
4419
tiny_gltf_v3.h
Normal file
4419
tiny_gltf_v3.h
Normal file
File diff suppressed because it is too large
Load Diff
2111
tinygltf_json.h
Normal file
2111
tinygltf_json.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user