Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: C interface #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ glTF parsers, it does not automatically load textures and external buffers to al
optimise to their liking. It does, however, load embedded data and also decodes base64 encoded
buffers using high speed SIMD algorithms.

fastgltf also provides a C99 API header which includes all the features of the C++ API so that you
can use fastgltf with virtually any language you want.

By utilising simdjson, this library can take advantage of SSE4, AVX2, AVX512, and ARM Neon.

## Features
Expand Down Expand Up @@ -42,6 +45,8 @@ the CMake script. The library is tested on GCC 9, GCC 10, Clang 12, and MSVC 14
using CI. The project uses a simple CMake 3.11, and can be simply used by adding fastgltf as a
subdirectory. Also, fastgltf is available from [vcpkg](https://github.com/microsoft/vcpkg).

### C++ API

```cpp
#include <fastgltf_parser.hpp>
#include <fastgltf_types.hpp>
Expand Down Expand Up @@ -86,6 +91,41 @@ All the nodes, meshes, buffers, textures, ... can now be accessed through the `f
type. References in between objects are done with a single `size_t`, which is used to index into
the various vectors in the asset.

### C99 API

```c
#include <fastgltf_c.h>

void load(const char* path, const char* directory) {
// Creates a parser object
fastgltf_parser* parser = fastgltf_create_parser(0);

fastgltf_gltf_data_buffer* data = fastgltf_create_gltf_data_buffer_from_path(path);

// For GLB files, use fastgltf_load_binary_gltf
fastgltf_gltf* gltf = fastgltf_load_gltf(parser, data, directory, OptionsDontRequireValidAssetMember);
if (fastgltf_get_parser_error(parser) != ErrorNone) {
// error
}

// Tell fastgltf to parse the whole glTF structure. Optionally, you can parse individual
// aspects of glTF files by calling the various parse methods.
fastgltf_parse_all(gltf);

// You can now destroy the parser and JSON data. It is advised, however, to reuse parsers.
fastgltf_destroy_gltf_data_buffer(data);
fastgltf_destroy_parser(parser);

// You can now query the parsed asset and destroy the glTF object.
fastgltf_asset* asset = fastgltf_get_parsed_asset(gltf);
fastgltf_destroy_gltf(gltf);

// Use the parsed asset here.

fastgltf_destroy_asset(asset);
}
```

## Performance

[spreadsheet-link]: https://docs.google.com/spreadsheets/d/1ocdHGoty-rF0N46ZlAlswzcPHVRsqG_tncy8paD3iMY/edit?usp=sharing
Expand Down
8 changes: 7 additions & 1 deletion cmake/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ macro(compiler_flags)
# cpuid, meaning no architecture flags or other compile flags need to be passed.
# See https://github.com/simdjson/simdjson/blob/master/doc/implementation-selection.md.
if (MSVC)
target_compile_options(${PARAM_TARGET} PRIVATE /EHsc $<$<CONFIG:RELEASE>:/O2 /Ob3 /Ot>)
target_compile_options(${PARAM_TARGET} PRIVATE /EHsc $<$<CONFIG:RELEASE>:/O2>)
if (MSVC_VERSION GREATER_EQUAL 1920)
# With VS 16 (2019) /Ob3 was added to more aggressively inline functions.
target_compile_options(${PARAM_TARGET} PRIVATE $<$<CONFIG:RELEASE>:/Ob3>)
else()
target_compile_options(${PARAM_TARGET} PRIVATE $<$<CONFIG:RELEASE>:/Ob2>)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${PARAM_TARGET} PRIVATE $<$<CONFIG:RELEASE>:-O3>)
endif()
Expand Down
7 changes: 4 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
add_library(fastgltf
"fastgltf.cpp" "fastgltf_util.hpp" "fastgltf_types.hpp" "fastgltf_parser.hpp"
"base64_decode.cpp" "base64_decode.hpp")
"base64_decode.cpp" "base64_decode.hpp"
"fastgltf_c.cpp" "fastgltf_c.h")

add_library(fastgltf::fastgltf ALIAS fastgltf)
target_include_directories(fastgltf PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> $<INSTALL_INTERFACE:include>)
target_compile_features(fastgltf PUBLIC cxx_std_17)
target_compile_features(fastgltf PUBLIC cxx_std_17 c_std_99)
compiler_flags(TARGET fastgltf)
enable_debug_inlining(TARGET fastgltf_simdjson)

Expand All @@ -23,7 +24,7 @@ endif()
target_compile_definitions(fastgltf PRIVATE "FASTGLTF_USE_CUSTOM_SMALLVECTOR=$<BOOL:${FASTGLTF_USE_CUSTOM_SMALLVECTOR}>")

install(
FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp"
FILES "base64_decode.hpp" "fastgltf_parser.hpp" "fastgltf_types.hpp" "fastgltf_util.hpp" "fastgltf_c.h"
TYPE INCLUDE
)

Expand Down
37 changes: 18 additions & 19 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
#error "fastgltf requires C++17"
#endif

#include <array>
#include <cmath>
#include <fstream>
#include <functional>
#include <utility>

#ifdef _MSC_VER
#pragma warning(push)
Expand Down Expand Up @@ -283,20 +280,22 @@ std::pair<fg::Error, fg::DataSource> fg::glTF::decodeUri(std::string_view uri) c
}

fg::MimeType fg::glTF::getMimeTypeFromString(std::string_view mime) {
if (mime == mimeTypeJpeg) {
return MimeType::JPEG;
} else if (mime == mimeTypePng) {
return MimeType::PNG;
} else if (mime == mimeTypeKtx) {
return MimeType::KTX2;
} else if (mime == mimeTypeDds) {
return MimeType::DDS;
} else if (mime == mimeTypeGltfBuffer) {
return MimeType::GltfBuffer;
} else if (mime == mimeTypeOctetStream) {
return MimeType::OctetStream;
} else {
return MimeType::None;
auto hash = crc32(mime);
switch (hash) {
case force_consteval<crc32(mimeTypeJpeg)>:
return MimeType::JPEG;
case force_consteval<crc32(mimeTypePng)>:
return MimeType::PNG;
case force_consteval<crc32(mimeTypeKtx)>:
return MimeType::KTX2;
case force_consteval<crc32(mimeTypeDds)>:
return MimeType::DDS;
case force_consteval<crc32(mimeTypeGltfBuffer)>:
return MimeType::GltfBuffer;
case force_consteval<crc32(mimeTypeOctetStream)>:
return MimeType::OctetStream;
default:
return MimeType::None;
}
}

Expand Down Expand Up @@ -2147,7 +2146,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadGLTF(GltfDataBuffer* buffer, fs::path
data->decodeCallback = decodeCallback;
data->userPointer = userPointer;

return std::unique_ptr<glTF>(new glTF(std::move(data), std::move(directory), options, extensions));
return std::unique_ptr<glTF>(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions));
}

std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs::path directory, Options options) {
Expand Down Expand Up @@ -2203,7 +2202,7 @@ std::unique_ptr<fg::glTF> fg::Parser::loadBinaryGLTF(GltfDataBuffer* buffer, fs:
data->decodeCallback = decodeCallback;
data->userPointer = userPointer;

auto gltf = std::unique_ptr<glTF>(new glTF(std::move(data), std::move(directory), options, extensions));
auto gltf = std::unique_ptr<glTF>(new (std::nothrow) glTF(std::move(data), std::move(directory), options, extensions));

// Is there enough room for another chunk header?
if (header.length > (offset + sizeof(BinaryGltfChunk))) {
Expand Down
171 changes: 171 additions & 0 deletions src/fastgltf_c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include <cassert>

#include <fastgltf_c.h>

#include <fastgltf_parser.hpp>
#include <fastgltf_types.hpp>

// Some checks to ensure some conversions are correct.
static_assert(DataSourceCount == std::variant_size_v<fastgltf::DataSource>,
"fastgltf_data_source does not properly reflect the fastgltf::DataSource variant.");
static_assert(std::is_same_v<std::monostate, std::variant_alternative_t<DataSourceNone, fastgltf::DataSource>>);
static_assert(std::is_same_v<fastgltf::sources::BufferView, std::variant_alternative_t<DataSourceBufferView, fastgltf::DataSource>>);
static_assert(std::is_same_v<fastgltf::sources::FilePath, std::variant_alternative_t<DataSourceFilePath, fastgltf::DataSource>>);
static_assert(std::is_same_v<fastgltf::sources::Vector, std::variant_alternative_t<DataSourceVector, fastgltf::DataSource>>);
static_assert(std::is_same_v<fastgltf::sources::CustomBuffer, std::variant_alternative_t<DataSourceCustomBuffer, fastgltf::DataSource>>);

fastgltf_component_type fastgltf_get_component_type(unsigned int componentType) {
return static_cast<fastgltf_component_type>(fastgltf::getComponentType(
static_cast<std::underlying_type_t<fastgltf::ComponentType>>(componentType)));
}

fastgltf_accessor_type fastgltf_get_accessor_type(const char* string) {
return static_cast<fastgltf_accessor_type>(fastgltf::getAccessorType(std::string_view { string }));
}

fastgltf_parser* fastgltf_create_parser(fastgltf_extensions extensions) {
return reinterpret_cast<fastgltf_parser*>(
new (std::nothrow) fastgltf::Parser(static_cast<fastgltf::Extensions>(extensions)));
}

void fastgltf_destroy_parser(fastgltf_parser* parser) {
delete reinterpret_cast<fastgltf::Parser*>(parser);
}

fastgltf_gltf_data_buffer* fastgltf_create_gltf_data_buffer(unsigned char* bytes, size_t size) {
auto data = new (std::nothrow) fastgltf::GltfDataBuffer();
data->copyBytes(bytes, size);
return reinterpret_cast<fastgltf_gltf_data_buffer*>(data);
}

fastgltf_gltf_data_buffer* fastgltf_create_gltf_data_buffer_from_path(const char* file) {
auto data = new (std::nothrow) fastgltf::GltfDataBuffer();
data->loadFromFile(std::string_view { file });
return reinterpret_cast<fastgltf_gltf_data_buffer*>(data);
}

fastgltf_gltf_data_buffer* fastgltf_create_gltf_data_buffer_from_wpath(const wchar_t* file) {
auto data = new (std::nothrow) fastgltf::GltfDataBuffer();
data->loadFromFile(std::wstring_view { file });
return reinterpret_cast<fastgltf_gltf_data_buffer*>(data);
}

void fastgltf_destroy_gltf_data_buffer(fastgltf_gltf_data_buffer* data) {
delete reinterpret_cast<fastgltf::GltfDataBuffer*>(data);
}

fastgltf_error fastgltf_get_parser_error(fastgltf_parser* parser) {
return static_cast<fastgltf_error>(
reinterpret_cast<fastgltf::Parser*>(parser)->getError());
}

fastgltf_gltf* fastgltf_load_gltf(fastgltf_parser* parser, fastgltf_gltf_data_buffer* json, const char* directory, fastgltf_options options) {
auto gltf = reinterpret_cast<fastgltf::Parser*>(parser)->loadGLTF(
reinterpret_cast<fastgltf::GltfDataBuffer*>(json), std::string_view { directory }, static_cast<fastgltf::Options>(options));
return reinterpret_cast<fastgltf_gltf*>(gltf.release());
}

fastgltf_gltf* fastgltf_load_binary_gltf(fastgltf_parser* parser, fastgltf_gltf_data_buffer* data, const char* directory, fastgltf_options options) {
auto gltf = reinterpret_cast<fastgltf::Parser*>(parser)->loadBinaryGLTF(
reinterpret_cast<fastgltf::GltfDataBuffer*>(data), std::string_view { directory }, static_cast<fastgltf::Options>(options));
return reinterpret_cast<fastgltf_gltf*>(gltf.release());
}

fastgltf_gltf* fastgltf_load_gltf_w(fastgltf_parser* parser, fastgltf_gltf_data_buffer* json, const wchar_t* directory, fastgltf_options options) {
auto gltf = reinterpret_cast<fastgltf::Parser*>(parser)->loadGLTF(
reinterpret_cast<fastgltf::GltfDataBuffer*>(json), std::wstring_view { directory }, static_cast<fastgltf::Options>(options));
return reinterpret_cast<fastgltf_gltf*>(gltf.release());
}

fastgltf_gltf* fastgltf_load_binary_gltf_w(fastgltf_parser* parser, fastgltf_gltf_data_buffer* data, const wchar_t* directory, fastgltf_options options) {
auto gltf = reinterpret_cast<fastgltf::Parser*>(parser)->loadBinaryGLTF(
reinterpret_cast<fastgltf::GltfDataBuffer*>(data), std::wstring_view { directory }, static_cast<fastgltf::Options>(options));
return reinterpret_cast<fastgltf_gltf*>(gltf.release());
}

void fastgltf_destroy_gltf(fastgltf_gltf* gltf) {
delete reinterpret_cast<fastgltf::glTF*>(gltf);
}

fastgltf_error fastgltf_parse_all(fastgltf_gltf* gltf) {
return static_cast<fastgltf_error>(
reinterpret_cast<fastgltf::glTF*>(gltf)->parse());
}

fastgltf_error fastgltf_parse(fastgltf_gltf* gltf, fastgltf_category categories) {
return static_cast<fastgltf_error>(
reinterpret_cast<fastgltf::glTF*>(gltf)->parse(static_cast<fastgltf::Category>(categories)));
}

fastgltf_asset* fastgltf_get_parsed_asset(fastgltf_gltf* gltf) {
// Obtain the unique_ptr from the glTF and release it. Otherwise, it gets destroyed with the
// destructor of fastgltf::glTF.
auto asset = reinterpret_cast<fastgltf::glTF*>(gltf)->getParsedAsset();
return reinterpret_cast<fastgltf_asset*>(asset.release());
}

void fastgltf_destroy_asset(fastgltf_asset* asset) {
delete reinterpret_cast<fastgltf::Asset*>(asset);
}

size_t fastgltf_get_buffer_count(fastgltf_asset* casset) {
auto* asset = reinterpret_cast<fastgltf::Asset*>(casset);
return asset->buffers.size();
}

fastgltf_buffer* fastgltf_get_buffer(fastgltf_asset* casset, size_t index) {
auto* asset = reinterpret_cast<fastgltf::Asset*>(casset);
assert(index <= asset->buffers.size());
return reinterpret_cast<fastgltf_buffer*>(&asset->buffers.at(index));
}

size_t fastgltf_get_buffer_length(fastgltf_buffer* buffer) {
return reinterpret_cast<fastgltf::Buffer*>(buffer)->byteLength;
}

fastgltf_data_source fastgltf_get_buffer_data_source_type(fastgltf_buffer* buffer) {
return static_cast<fastgltf_data_source>(
reinterpret_cast<fastgltf::Buffer*>(buffer)->data.index());
}

MimeType fastgltf_get_buffer_data_mime(fastgltf_buffer* buffer) {
return std::visit([](auto& arg) {
using namespace fastgltf::sources;
using T = std::decay_t<decltype(arg)>;
if constexpr (fastgltf::is_any<T, BufferView, FilePath, Vector, CustomBuffer>()) {
return static_cast<::MimeType>(arg.mimeType);
} else {
return MimeTypeNone;
}
}, reinterpret_cast<fastgltf::Buffer*>(buffer)->data);
}

void fastgltf_buffer_data_get_buffer_view(fastgltf_buffer* buffer, size_t* bufferView) {
auto* v = std::get_if<fastgltf::sources::BufferView>(
&reinterpret_cast<fastgltf::Buffer*>(buffer)->data);
assert(v != nullptr);
*bufferView = v->bufferViewIndex;
}

void fastgltf_buffer_data_get_file_path(fastgltf_buffer* buffer, size_t* offset, const wchar_t** path) {
auto* v = std::get_if<fastgltf::sources::FilePath>(
&reinterpret_cast<fastgltf::Buffer*>(buffer)->data);
assert(v != nullptr);
*path = v->path.c_str();
*offset = v->fileByteOffset;
}

void fastgltf_buffer_data_get_vector(fastgltf_buffer* buffer, size_t* size, uint8_t** data) {
auto* v = std::get_if<fastgltf::sources::Vector>(
&reinterpret_cast<fastgltf::Buffer*>(buffer)->data);
assert(v != nullptr);
*data = v->bytes.data();
*size = v->bytes.size();
}

fastgltf_custom_buffer fastgltf_buffer_data_get_custom_buffer(fastgltf_buffer* buffer) {
auto* v = std::get_if<fastgltf::sources::CustomBuffer>(
&reinterpret_cast<fastgltf::Buffer*>(buffer)->data);
assert(v != nullptr);
return static_cast<fastgltf_custom_buffer>(v->id);
}
Loading