Support simultaneous gltf load/saves (TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR)

This allows multiple gltf's to be loaded/saved in parallel.  It removes the restriction of a single JsonDocument active at once which is default behavior.  Enable with TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
This commit is contained in:
jrkoonce 2019-09-04 12:01:39 -05:00
parent 6ff95392b0
commit ab63db6318
4 changed files with 6975 additions and 69 deletions

View File

@ -4,7 +4,7 @@
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
//#define TINYGLTF_USE_RAPIDJSON
#define TINYGLTF_USE_RAPIDJSON
#include "tiny_gltf.h"
#include <cstdio>

View File

@ -1,33 +1,35 @@
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define TINYGLTF_USE_RAPIDJSON
#define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
#include "tiny_gltf.h"
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
// this in one cpp file
#include "catch.hpp"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <fstream>
TEST_CASE("parse-error", "[parse]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, "bora", static_cast<int>(strlen("bora")), /* basedir*/ "");
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, "bora",
static_cast<int>(strlen("bora")),
/* basedir*/ "");
REQUIRE(false == ret);
}
TEST_CASE("datauri-in-glb", "[issue-79]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
@ -42,13 +44,13 @@ TEST_CASE("datauri-in-glb", "[issue-79]") {
}
TEST_CASE("extension-with-empty-object", "[issue-97]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
bool ret = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Extensions-issue97/test.gltf");
bool ret = ctx.LoadASCIIFromFile(&model, &err, &warn,
"../models/Extensions-issue97/test.gltf");
if (!err.empty()) {
std::cerr << err << std::endl;
}
@ -82,7 +84,6 @@ TEST_CASE("extension-with-empty-object", "[issue-97]") {
REQUIRE(m.materials[0].extensions.size() == 1);
REQUIRE(m.materials[0].extensions.count("VENDOR_material_some_ext") == 1);
}
}
TEST_CASE("invalid-primitive-indices", "[bounds-checking]") {
@ -137,10 +138,13 @@ TEST_CASE("glb-invalid-length", "[bounds-checking]") {
// This glb has a much longer length than the provided data and should fail
// initial range checks.
const unsigned char glb_invalid_length[] = "glTF"
"\x20\x00\x00\x00" "\x6c\x66\x00\x00" //
// | version | length |
"\x02\x00\x00\x00" "\x4a\x53\x4f\x4e{}"; //
const unsigned char glb_invalid_length[] =
"glTF"
"\x20\x00\x00\x00"
"\x6c\x66\x00\x00" //
// | version | length |
"\x02\x00\x00\x00"
"\x4a\x53\x4f\x4e{}"; //
// | model length | model format |
bool ret = ctx.LoadBinaryFromMemory(&model, &err, &warn, glb_invalid_length,
@ -167,13 +171,13 @@ TEST_CASE("parse-integer", "[bounds-checking]") {
SECTION("parses valid numbers") {
std::string err;
int result = 123;
CHECK(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct("{\"zero\" : 0}"), "zero",
true));
CHECK(tinygltf::ParseIntegerProperty(
&result, &err, JsonConstruct("{\"zero\" : 0}"), "zero", true));
REQUIRE(err == "");
REQUIRE(result == 0);
CHECK(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct("{\"int\": -1234}"), "int",
true));
CHECK(tinygltf::ParseIntegerProperty(
&result, &err, JsonConstruct("{\"int\": -1234}"), "int", true));
REQUIRE(err == "");
REQUIRE(result == -1234);
}
@ -181,7 +185,8 @@ TEST_CASE("parse-integer", "[bounds-checking]") {
SECTION("detects missing properties") {
std::string err;
int result = -1;
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct(""), "int", true));
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct(""),
"int", true));
REQUIRE_THAT(err, Catch::Contains("'int' property is missing"));
REQUIRE(result == -1);
}
@ -189,8 +194,8 @@ TEST_CASE("parse-integer", "[bounds-checking]") {
SECTION("handled missing but not required properties") {
std::string err;
int result = -1;
CHECK_FALSE(
tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct(""), "int", false));
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct(""),
"int", false));
REQUIRE(err == "");
REQUIRE(result == -1);
}
@ -199,14 +204,14 @@ TEST_CASE("parse-integer", "[bounds-checking]") {
std::string err;
int result = -1;
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct("{\"int\": 0.5}"),
"int", true));
CHECK_FALSE(tinygltf::ParseIntegerProperty(
&result, &err, JsonConstruct("{\"int\": 0.5}"), "int", true));
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
// Excessively large values and NaN aren't allowed either.
err.clear();
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, JsonConstruct("{\"int\": 1e300}"),
"int", true));
CHECK_FALSE(tinygltf::ParseIntegerProperty(
&result, &err, JsonConstruct("{\"int\": 1e300}"), "int", true));
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
err.clear();
@ -214,9 +219,8 @@ TEST_CASE("parse-integer", "[bounds-checking]") {
JsonDocument o;
double nan = std::numeric_limits<double>::quiet_NaN();
tinygltf::JsonAddMember(o, "int", json(nan));
CHECK_FALSE(tinygltf::ParseIntegerProperty(
&result, &err, o,
"int", true));
CHECK_FALSE(
tinygltf::ParseIntegerProperty(&result, &err, o, "int", true));
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
}
}
@ -240,19 +244,19 @@ TEST_CASE("parse-unsigned", "[bounds-checking]") {
std::string err;
size_t result = -1;
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, JsonConstruct("{\"int\": -1234}"),
"int", true));
CHECK_FALSE(tinygltf::ParseUnsignedProperty(
&result, &err, JsonConstruct("{\"int\": -1234}"), "int", true));
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
err.clear();
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, JsonConstruct("{\"int\": 0.5}"),
"int", true));
CHECK_FALSE(tinygltf::ParseUnsignedProperty(
&result, &err, JsonConstruct("{\"int\": 0.5}"), "int", true));
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
// Excessively large values and NaN aren't allowed either.
err.clear();
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, JsonConstruct("{\"int\": 1e300}"),
"int", true));
CHECK_FALSE(tinygltf::ParseUnsignedProperty(
&result, &err, JsonConstruct("{\"int\": 1e300}"), "int", true));
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
err.clear();
@ -260,9 +264,8 @@ TEST_CASE("parse-unsigned", "[bounds-checking]") {
JsonDocument o;
double nan = std::numeric_limits<double>::quiet_NaN();
tinygltf::JsonAddMember(o, "int", json(nan));
CHECK_FALSE(tinygltf::ParseUnsignedProperty(
&result, &err, o,
"int", true));
CHECK_FALSE(
tinygltf::ParseUnsignedProperty(&result, &err, o, "int", true));
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
}
}
@ -272,8 +275,8 @@ TEST_CASE("parse-integer-array", "[bounds-checking]") {
SECTION("parses valid integers") {
std::string err;
std::vector<int> result;
CHECK(tinygltf::ParseIntegerArrayProperty(&result, &err,
JsonConstruct("{\"x\": [-1, 2, 3]}"), "x", true));
CHECK(tinygltf::ParseIntegerArrayProperty(
&result, &err, JsonConstruct("{\"x\": [-1, 2, 3]}"), "x", true));
REQUIRE(err == "");
REQUIRE(result.size() == 3);
REQUIRE(result[0] == -1);
@ -298,22 +301,27 @@ TEST_CASE("pbr-khr-texture-transform", "[material]") {
// Loading is expected to fail, but not crash.
bool ret = ctx.LoadASCIIFromFile(
&model, &err, &warn,
"../models/Cube-texture-ext/Cube-textransform.gltf");
&model, &err, &warn, "../models/Cube-texture-ext/Cube-textransform.gltf");
REQUIRE(ret == true);
REQUIRE(model.materials.size() == 2);
REQUIRE(model.materials[0].emissiveTexture.extensions.count("KHR_texture_transform") == 1);
REQUIRE(model.materials[0].emissiveTexture.extensions["KHR_texture_transform"].IsObject());
REQUIRE(model.materials[0].emissiveTexture.extensions.count(
"KHR_texture_transform") == 1);
REQUIRE(model.materials[0]
.emissiveTexture.extensions["KHR_texture_transform"]
.IsObject());
tinygltf::Value::Object &texform = model.materials[0].emissiveTexture.extensions["KHR_texture_transform"].Get<tinygltf::Value::Object>();
tinygltf::Value::Object &texform =
model.materials[0]
.emissiveTexture.extensions["KHR_texture_transform"]
.Get<tinygltf::Value::Object>();
REQUIRE(texform.count("scale"));
REQUIRE(texform["scale"].IsArray());
// Note: It looks json.hpp parse integer JSON number as integer, not floating point.
// IsNumber return true either value is int or floating point.
// Note: It looks json.hpp parse integer JSON number as integer, not floating
// point. IsNumber return true either value is int or floating point.
REQUIRE(texform["scale"].Get(0).IsNumber());
REQUIRE(texform["scale"].Get(1).IsNumber());
@ -323,5 +331,4 @@ TEST_CASE("pbr-khr-texture-transform", "[material]") {
REQUIRE(scale[0] == Approx(1.0));
REQUIRE(scale[1] == Approx(-1.0));
}

6880
tiny_gltf - Copy.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1650,33 +1650,52 @@ class TinyGLTF {
namespace
{
#ifdef TINYGLTF_USE_RAPIDJSON
#ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
// This uses the RapidJSON CRTAllocator. It is thread safe and multiple
// documents may be active at once.
using json = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
using json_const_iterator = json::ConstMemberIterator;
using json_const_array_iterator = json const *;
using JsonDocument =
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
rapidjson::CrtAllocator s_CrtAllocator; //stateless and thread safe
rapidjson::CrtAllocator &GetAllocator() { return s_CrtAllocator; }
#else
// This uses the default RapidJSON MemoryPoolAllocator. It is very fast, but
// not thread safe. Only a single JsonDocument may be active at any one time,
// meaning only a single gltf load/save can be active any one time.
using json = rapidjson::Value;
using json_const_iterator = json::ConstMemberIterator;
using json_const_array_iterator = json const*;
rapidjson::Document* s_pActiveDocument = nullptr;
struct JsonDocument : public rapidjson::Document
{
using json_const_array_iterator = json const *;
rapidjson::Document *s_pActiveDocument = nullptr;
rapidjson::Document::AllocatorType &GetAllocator() {
assert(s_pActiveDocument); //Root json node must be JsonDocument type
return s_pActiveDocument->GetAllocator();
}
struct JsonDocument : public rapidjson::Document {
JsonDocument() {
assert(s_pActiveDocument == nullptr); //Code assumes only one document is active at a time
assert(s_pActiveDocument ==
nullptr); // When using default allocator, only one document can be active at a time, if you need
// multiple active at once, define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
s_pActiveDocument = this;
}
JsonDocument(const JsonDocument&) = delete;
JsonDocument(JsonDocument&& rhs) noexcept : rapidjson::Document(std::move(rhs))
{
JsonDocument(const JsonDocument &) = delete;
JsonDocument(JsonDocument &&rhs) noexcept
: rapidjson::Document(std::move(rhs)) {
s_pActiveDocument = this;
rhs.isNil = true;
}
~JsonDocument() {
if (!isNil)
{
if (!isNil) {
s_pActiveDocument = nullptr;
}
}
private:
bool isNil = false;
};
#endif // TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
#else
using nlohmann::json;
using json_const_iterator = json::const_iterator;
@ -5674,7 +5693,7 @@ namespace
json JsonFromString(const char* s)
{
#ifdef TINYGLTF_USE_RAPIDJSON
return json(s, s_pActiveDocument->GetAllocator());
return json(s, GetAllocator());
#else
return json(s);
#endif
@ -5705,7 +5724,7 @@ namespace
void JsonAssign(json& dest, const json& src)
{
#ifdef TINYGLTF_USE_RAPIDJSON
dest.CopyFrom(src, s_pActiveDocument->GetAllocator());
dest.CopyFrom(src, GetAllocator());
#else
dest = src;
#endif
@ -5718,7 +5737,7 @@ namespace
{
o.SetObject();
}
o.AddMember(json(key, s_pActiveDocument->GetAllocator()), std::move(value), s_pActiveDocument->GetAllocator());
o.AddMember(json(key, GetAllocator()), std::move(value), GetAllocator());
#else
o[key] = std::move(value);
#endif
@ -5727,7 +5746,7 @@ namespace
void JsonPushBack(json& o, json&& value)
{
#ifdef TINYGLTF_USE_RAPIDJSON
o.PushBack(std::move(value), s_pActiveDocument->GetAllocator());
o.PushBack(std::move(value), GetAllocator());
#else
o.push_back(std::move(value));
#endif
@ -5755,7 +5774,7 @@ namespace
{
#ifdef TINYGLTF_USE_RAPIDJSON
o.SetArray();
o.Reserve(static_cast<rapidjson::SizeType>(s), s_pActiveDocument->GetAllocator());
o.Reserve(static_cast<rapidjson::SizeType>(s), GetAllocator());
#endif
(void)(o);
(void)(s);
@ -5820,16 +5839,16 @@ static bool ValueToJson(const Value &value, json *ret) {
obj.SetBool(value.Get<bool>());
break;
case STRING_TYPE:
obj.SetString(value.Get<std::string>().c_str(), s_pActiveDocument->GetAllocator());
obj.SetString(value.Get<std::string>().c_str(), GetAllocator());
break;
case ARRAY_TYPE: {
obj.SetArray();
obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()), s_pActiveDocument->GetAllocator());
obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()), GetAllocator());
for (unsigned int i = 0; i < value.ArrayLen(); ++i) {
Value elementValue = value.Get(int(i));
json elementJson;
if (ValueToJson(value.Get(int(i)), &elementJson))
obj.PushBack(std::move(elementJson), s_pActiveDocument->GetAllocator());
obj.PushBack(std::move(elementJson), GetAllocator());
}
break;
}
@ -5844,7 +5863,7 @@ static bool ValueToJson(const Value &value, json *ret) {
for (auto &it : objMap) {
json elementJson;
if (ValueToJson(it.second, &elementJson)) {
obj.AddMember(json(it.first.c_str(), s_pActiveDocument->GetAllocator()), std::move(elementJson), s_pActiveDocument->GetAllocator());
obj.AddMember(json(it.first.c_str(), GetAllocator()), std::move(elementJson), GetAllocator());
}
}
break;