Skip to content

Commit 332497f

Browse files
committed
Add option to skip glTF validation
1 parent e930476 commit 332497f

7 files changed

Lines changed: 56 additions & 22 deletions

File tree

src/guc/main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ static struct cag_option cmd_options[] = {
4747
.value_name = "<index>",
4848
.description = "Index of the material variant that is selected by default"
4949
},
50+
{
51+
.identifier = 's',
52+
.access_letters = "s",
53+
.access_name = "skip-validation",
54+
.value_name = NULL,
55+
.description = "Skip glTF validation for reduced processing time"
56+
},
5057
{
5158
.identifier = 'l',
5259
.access_letters = "l",
@@ -60,15 +67,16 @@ static struct cag_option cmd_options[] = {
6067
.access_name = "help",
6168
.value_name = NULL,
6269
.description = "Show the command help"
63-
}
70+
},
6471
};
6572

6673
int main(int argc, char* argv[])
6774
{
6875
struct guc_options options = {
6976
.emit_mtlx = false,
7077
.mtlx_as_usdshade = false,
71-
.default_material_variant = 0
78+
.default_material_variant = 0,
79+
.skip_validation = false
7280
};
7381

7482
cag_option_context context;
@@ -88,6 +96,9 @@ int main(int argc, char* argv[])
8896
options.default_material_variant = atoi(value); // fall back to 0 on error
8997
break;
9098
}
99+
case 's':
100+
options.skip_validation = true;
101+
break;
91102
case 'l': {
92103
printf("%s\n", license_text);
93104
return EXIT_SUCCESS;

src/libguc/include/guc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ struct guc_options
4040
// If the asset supports the KHR_materials_variants extension, select the material
4141
// variant at the given index by default.
4242
int default_material_variant;
43+
44+
// If set to true, don't validate the glTF data model on load. This reduces the
45+
// processing time at the cost of stability and security.
46+
bool skip_validation;
4347
};
4448

4549
bool guc_convert(const char* gltf_path,

src/libguc/src/cgltf_util.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ namespace detail
418418

419419
namespace guc
420420
{
421-
bool load_gltf(const char* gltfPath, cgltf_data** data)
421+
bool load_gltf(const char* gltfPath, cgltf_data** data, bool validate)
422422
{
423423
detail::BufferHolder* bufferHolder = new detail::BufferHolder;
424424

@@ -448,13 +448,16 @@ namespace guc
448448
return false;
449449
}
450450

451-
result = cgltf_validate(*data);
452-
453-
if (result != cgltf_result_success)
451+
if (validate)
454452
{
455-
TF_RUNTIME_ERROR("unable to validate glTF: %s", cgltf_error_string(result));
456-
free_gltf(*data);
457-
return false;
453+
result = cgltf_validate(*data);
454+
455+
if (result != cgltf_result_success)
456+
{
457+
TF_RUNTIME_ERROR("unable to validate glTF: %s", cgltf_error_string(result));
458+
free_gltf(*data);
459+
return false;
460+
}
458461
}
459462

460463
bool meshoptCompressionRequired = false;

src/libguc/src/cgltf_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace guc
2222
{
23-
bool load_gltf(const char* gltfPath, cgltf_data** data);
23+
bool load_gltf(const char* gltfPath, cgltf_data** data, bool validate);
2424

2525
void free_gltf(cgltf_data* data);
2626

src/libguc/src/fileFormat.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ TF_DEFINE_PRIVATE_TOKENS(
5151
(gltf)
5252
(glb)
5353
(emitMtlx)
54+
(skipValidation)
5455
);
5556

5657
TF_REGISTRY_FUNCTION(TfType)
@@ -110,6 +111,11 @@ SdfAbstractDataRefPtr UsdGlTFFileFormat::InitData(const FileFormatArguments& arg
110111
{
111112
data->emitMtlx = TfUnstringify<bool>(emitMtlxIt->second);
112113
}
114+
auto skipValidationIt = args.find(_tokens->skipValidation.GetText());
115+
if (skipValidationIt != args.end())
116+
{
117+
data->skipValidation = TfUnstringify<bool>(skipValidationIt->second);
118+
}
113119

114120
return data;
115121
}
@@ -131,16 +137,18 @@ bool UsdGlTFFileFormat::Read(SdfLayer* layer,
131137
ArDefaultResolverContext ctx({srcDir.string()});
132138
ArResolverContextBinder binder(ctx);
133139

140+
SdfAbstractDataRefPtr layerData = InitData(layer->GetFileFormatArguments());
141+
UsdGlTFDataConstPtr data = TfDynamic_cast<const UsdGlTFDataConstPtr>(layerData);
142+
143+
bool validateGltf = !data->skipValidation;
144+
134145
cgltf_data* gltf_data = nullptr;
135-
if (!load_gltf(resolvedPath.c_str(), &gltf_data))
146+
if (!load_gltf(resolvedPath.c_str(), &gltf_data, validateGltf))
136147
{
137148
TF_RUNTIME_ERROR("unable to load glTF file %s", resolvedPath.c_str());
138149
return false;
139150
}
140151

141-
SdfAbstractDataRefPtr layerData = InitData(layer->GetFileFormatArguments());
142-
UsdGlTFDataConstPtr data = TfDynamic_cast<const UsdGlTFDataConstPtr>(layerData);
143-
144152
Converter::Params params = {};
145153
params.srcDir = srcDir;
146154
params.dstDir = s_tmpDirHolder.makeDir();
@@ -208,10 +216,15 @@ void UsdGlTFFileFormat::ComposeFieldsForFileFormatArguments(const std::string& a
208216
FileFormatArguments* args,
209217
VtValue *dependencyContextData) const
210218
{
211-
VtValue emitMtlxValue;
212-
if (context.ComposeValue(_tokens->emitMtlx, &emitMtlxValue))
219+
VtValue emitMtlx;
220+
if (context.ComposeValue(_tokens->emitMtlx, &emitMtlx))
221+
{
222+
(*args)[_tokens->emitMtlx] = TfStringify(emitMtlx);
223+
}
224+
VtValue skipValidation;
225+
if (context.ComposeValue(_tokens->skipValidation, &skipValidation))
213226
{
214-
(*args)[_tokens->emitMtlx] = TfStringify(emitMtlxValue);
227+
(*args)[_tokens->skipValidation] = TfStringify(skipValidation);
215228
}
216229
}
217230

src/libguc/src/fileFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class UsdGlTFData : public SdfData
7777
{
7878
public:
7979
bool emitMtlx = false;
80+
bool skipValidation = false;
8081
};
8182

8283
PXR_NAMESPACE_CLOSE_SCOPE

src/libguc/src/guc.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ bool guc_convert(const char* gltf_path,
8989
fs::path base_usd_path = usd_path;
9090
fs::path dst_dir = base_usd_path.parent_path();
9191

92-
bool export_usdz = base_usd_path.extension() == ".usdz";
92+
bool exportUsdz = base_usd_path.extension() == ".usdz";
9393

94-
if (export_usdz)
94+
if (exportUsdz)
9595
{
9696
dst_dir = ArchMakeTmpSubdir(ArchGetTmpDir(), "guc");
9797
TF_DEBUG(GUC).Msg("using temp dir %s\n", dst_dir.string().c_str());
@@ -107,14 +107,16 @@ bool guc_convert(const char* gltf_path,
107107
TF_DEBUG(GUC).Msg("temporary USD path: %s\n", base_usd_path.string().c_str());
108108
}
109109

110+
bool validateGltf = !options->skip_validation;
111+
110112
cgltf_data* gltf_data = nullptr;
111-
if (!load_gltf(gltf_path, &gltf_data))
113+
if (!load_gltf(gltf_path, &gltf_data, validateGltf))
112114
{
113115
TF_RUNTIME_ERROR("unable to load glTF file %s", gltf_path);
114116
return false;
115117
}
116118

117-
bool copyExistingFiles = !export_usdz; // Add source files directly to archive in case of USDZ
119+
bool copyExistingFiles = !exportUsdz; // Add source files directly to archive in case of USDZ
118120

119121
Converter::FileExports fileExports;
120122
bool result = convertToUsd(src_dir, gltf_data, base_usd_path, copyExistingFiles, options, fileExports);
@@ -128,7 +130,7 @@ bool guc_convert(const char* gltf_path,
128130

129131
// In case of USDZ, we have now written the USDC file and all image files to a
130132
// temporary directory. Next, we invoke Pixar's USDZ API in order to zip them.
131-
if (export_usdz)
133+
if (exportUsdz)
132134
{
133135
auto usdz_dst_dir = fs::absolute(final_usd_path).parent_path();
134136
if (!fs::exists(usdz_dst_dir) && !fs::create_directories(usdz_dst_dir))

0 commit comments

Comments
 (0)