From 852ea8325cebf84ff1fefc9da60af1739070acf3 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 3 Nov 2022 23:11:21 +0200 Subject: [PATCH 1/5] Added support for KHR_materials_emissive_strength according to spec https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_emissive_strength --- code/AssetLib/glTF2/glTF2Asset.h | 13 +++++++++++++ code/AssetLib/glTF2/glTF2Asset.inl | 16 ++++++++++++++++ code/AssetLib/glTF2/glTF2AssetWriter.h | 1 + code/AssetLib/glTF2/glTF2AssetWriter.inl | 18 ++++++++++++++++++ code/AssetLib/glTF2/glTF2Exporter.cpp | 10 ++++++++++ code/AssetLib/glTF2/glTF2Exporter.h | 2 ++ code/AssetLib/glTF2/glTF2Importer.cpp | 7 +++++++ 7 files changed, 67 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index 3becc4d9b..4349274d8 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -51,6 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_transmission full * KHR_materials_volume full * KHR_materials_ior full + * KHR_materials_emissive_strength full */ #ifndef GLTF2ASSET_H_INC #define GLTF2ASSET_H_INC @@ -801,6 +802,13 @@ struct MaterialIOR { void SetDefaults(); }; +struct MaterialEmissiveStrength { + float emissiveStrength = 0.f; + + MaterialEmissiveStrength() { SetDefaults(); } + void SetDefaults(); +}; + //! The material appearance of a primitive. struct Material : public Object { //PBR metallic roughness properties @@ -833,6 +841,9 @@ struct Material : public Object { //extension: KHR_materials_ior Nullable materialIOR; + //extension: KHR_materials_emissive_strength + Nullable materialEmissiveStrength; + //extension: KHR_materials_unlit bool unlit; @@ -1106,6 +1117,7 @@ public: bool KHR_materials_transmission; bool KHR_materials_volume; bool KHR_materials_ior; + bool KHR_materials_emissive_strength; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; @@ -1120,6 +1132,7 @@ public: KHR_materials_transmission(false), KHR_materials_volume(false), KHR_materials_ior(false), + KHR_materials_emissive_strength(false), KHR_draco_mesh_compression(false), FB_ngon_encoding(false), KHR_texture_basisu(false) { diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index db47915d6..e13dd226e 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1313,6 +1313,16 @@ inline void Material::Read(Value &material, Asset &r) { } } + if (r.extensionsUsed.KHR_materials_emissive_strength) { + if (Value *curMaterialEmissiveStrength = FindObject(*extensions, "KHR_materials_emissive_strength")) { + MaterialEmissiveStrength emissiveStrength; + + ReadMember(*curMaterialEmissiveStrength, "emissiveStrength", emissiveStrength.emissiveStrength); + + this->materialEmissiveStrength = Nullable(emissiveStrength); + } + } + unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit"); } } @@ -1355,6 +1365,11 @@ inline void MaterialIOR::SetDefaults() { ior = 1.5f; } +inline void MaterialEmissiveStrength::SetDefaults() { + //KHR_materials_emissive_strength properties + emissiveStrength = 0.f; +} + inline void Mesh::Read(Value &pJSON_Object, Asset &pAsset_Root) { Value *curName = FindMember(pJSON_Object, "name"); if (nullptr != curName && curName->IsString()) { @@ -2026,6 +2041,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_volume); CHECK_EXT(KHR_materials_ior); + CHECK_EXT(KHR_materials_emissive_strength); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.h b/code/AssetLib/glTF2/glTF2AssetWriter.h index 089a15844..ec216101c 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.h +++ b/code/AssetLib/glTF2/glTF2AssetWriter.h @@ -51,6 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * KHR_materials_transmission: full * KHR_materials_volume: full * KHR_materials_ior: full + * KHR_materials_emissive_strength: full */ #ifndef GLTF2ASSETWRITER_H_INC #define GLTF2ASSETWRITER_H_INC diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 0be139595..249c738cd 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -511,6 +511,20 @@ namespace glTF2 { } } + if (m.materialEmissiveStrength.isPresent) { + Value materialEmissiveStrength(rapidjson::Type::kObjectType); + + MaterialEmissiveStrength &emissiveStrength = m.materialEmissiveStrength.value; + + if (emissiveStrength.emissiveStrength != 0.f) { + WriteFloat(materialEmissiveStrength, emissiveStrength.emissiveStrength, "emissiveStrength", w.mAl); + } + + if (!emissiveStrength.ObjectEmpty()) { + exts.AddMember("KHR_materials_emissive_strength", emissiveStrength, w.mAl); + } + } + if (!exts.ObjectEmpty()) { obj.AddMember("extensions", exts, w.mAl); } @@ -935,6 +949,10 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_ior"), mAl); } + if (this->mAsset.extensionsUsed.KHR_materials_emissive_strength) { + exts.PushBack(StringRef("KHR_materials_emissive_strength"), mAl); + } + if (this->mAsset.extensionsUsed.FB_ngon_encoding) { exts.PushBack(StringRef("FB_ngon_encoding"), mAl); } diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 3bfe49fba..2954f9f53 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -733,6 +733,10 @@ bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) { return mat.Get(AI_MATKEY_REFRACTI, ior.ior) == aiReturn_SUCCESS; } +bool glTF2Exporter::GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength) { + return mat.Get(AI_MATKEY_EMISSIVE_INTENSITY, emissiveStrength.emissiveStrength) == aiReturn_SUCCESS; +} + void glTF2Exporter::ExportMaterials() { aiString aiName; for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) { @@ -862,6 +866,12 @@ void glTF2Exporter::ExportMaterials() { if (GetMatIOR(mat, ior)) { mAsset->extensionsUsed.KHR_materials_ior = true; m->materialIOR = Nullable(ior); + } + + MaterialEmissiveStrength emissiveStrength; + if (GetMatEmissiveStrength(mat, emissiveStrength)) { + mAsset->extensionsUsed.KHR_materials_emissive_strength = true; + m->materialEmissiveStrength = Nullable(emissiveStrength); } } } diff --git a/code/AssetLib/glTF2/glTF2Exporter.h b/code/AssetLib/glTF2/glTF2Exporter.h index 99425228e..505e331a2 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.h +++ b/code/AssetLib/glTF2/glTF2Exporter.h @@ -81,6 +81,7 @@ struct MaterialClearcoat; struct MaterialTransmission; struct MaterialVolume; struct MaterialIOR; +struct MaterialEmissiveStrength; // Vec/matrix types, as raw float arrays typedef float(vec2)[2]; @@ -121,6 +122,7 @@ protected: bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission); bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume); bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior); + bool GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength); void ExportMetadata(); void ExportMaterials(); void ExportMeshes(); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 947edc8d5..7b0bf4b0b 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -357,6 +357,13 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M aimat->AddProperty(&ior.ior, 1, AI_MATKEY_REFRACTI); } + // KHR_materials_emissive_strength + if (mat.materialEmissiveStrength.isPresent) { + MaterialEmissiveStrength &emissiveStrength = mat.materialEmissiveStrength.value; + + aimat->AddProperty(&emissiveStrength.emissiveStrength, 1, AI_MATKEY_EMISSIVE_INTENSITY); + } + return aimat; } catch (...) { delete aimat; From 945d93b46a138048eac880d0d3de9313dfbcc387 Mon Sep 17 00:00:00 2001 From: Adam Beili Date: Fri, 4 Nov 2022 09:37:28 -0700 Subject: [PATCH 2/5] fixed indentation --- code/AssetLib/glTF2/glTF2Asset.h | 8 ++++---- code/AssetLib/glTF2/glTF2Asset.inl | 4 ++-- code/AssetLib/glTF2/glTF2AssetWriter.inl | 6 +++--- code/AssetLib/glTF2/glTF2Exporter.cpp | 4 ++-- code/AssetLib/glTF2/glTF2Exporter.h | 2 +- code/AssetLib/glTF2/glTF2Importer.cpp | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index 4349274d8..b9e19205d 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -840,8 +840,8 @@ struct Material : public Object { //extension: KHR_materials_ior Nullable materialIOR; - - //extension: KHR_materials_emissive_strength + + //extension: KHR_materials_emissive_strength Nullable materialEmissiveStrength; //extension: KHR_materials_unlit @@ -1117,7 +1117,7 @@ public: bool KHR_materials_transmission; bool KHR_materials_volume; bool KHR_materials_ior; - bool KHR_materials_emissive_strength; + bool KHR_materials_emissive_strength; bool KHR_draco_mesh_compression; bool FB_ngon_encoding; bool KHR_texture_basisu; @@ -1132,7 +1132,7 @@ public: KHR_materials_transmission(false), KHR_materials_volume(false), KHR_materials_ior(false), - KHR_materials_emissive_strength(false), + KHR_materials_emissive_strength(false), KHR_draco_mesh_compression(false), FB_ngon_encoding(false), KHR_texture_basisu(false) { diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index e13dd226e..bb4be88da 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -1313,7 +1313,7 @@ inline void Material::Read(Value &material, Asset &r) { } } - if (r.extensionsUsed.KHR_materials_emissive_strength) { + if (r.extensionsUsed.KHR_materials_emissive_strength) { if (Value *curMaterialEmissiveStrength = FindObject(*extensions, "KHR_materials_emissive_strength")) { MaterialEmissiveStrength emissiveStrength; @@ -2041,7 +2041,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) { CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_volume); CHECK_EXT(KHR_materials_ior); - CHECK_EXT(KHR_materials_emissive_strength); + CHECK_EXT(KHR_materials_emissive_strength); CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_texture_basisu); diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 249c738cd..6b776dee9 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -511,7 +511,7 @@ namespace glTF2 { } } - if (m.materialEmissiveStrength.isPresent) { + if (m.materialEmissiveStrength.isPresent) { Value materialEmissiveStrength(rapidjson::Type::kObjectType); MaterialEmissiveStrength &emissiveStrength = m.materialEmissiveStrength.value; @@ -520,8 +520,8 @@ namespace glTF2 { WriteFloat(materialEmissiveStrength, emissiveStrength.emissiveStrength, "emissiveStrength", w.mAl); } - if (!emissiveStrength.ObjectEmpty()) { - exts.AddMember("KHR_materials_emissive_strength", emissiveStrength, w.mAl); + if (!materialEmissiveStrength.ObjectEmpty()) { + exts.AddMember("KHR_materials_emissive_strength", materialEmissiveStrength, w.mAl); } } diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 2954f9f53..728249676 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -867,8 +867,8 @@ void glTF2Exporter::ExportMaterials() { mAsset->extensionsUsed.KHR_materials_ior = true; m->materialIOR = Nullable(ior); } - - MaterialEmissiveStrength emissiveStrength; + + MaterialEmissiveStrength emissiveStrength; if (GetMatEmissiveStrength(mat, emissiveStrength)) { mAsset->extensionsUsed.KHR_materials_emissive_strength = true; m->materialEmissiveStrength = Nullable(emissiveStrength); diff --git a/code/AssetLib/glTF2/glTF2Exporter.h b/code/AssetLib/glTF2/glTF2Exporter.h index 505e331a2..425180b14 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.h +++ b/code/AssetLib/glTF2/glTF2Exporter.h @@ -122,7 +122,7 @@ protected: bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission); bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume); bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior); - bool GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength); + bool GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength); void ExportMetadata(); void ExportMaterials(); void ExportMeshes(); diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 7b0bf4b0b..7d3a4b9fe 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -357,7 +357,7 @@ static aiMaterial *ImportMaterial(std::vector &embeddedTexIdxs, Asset &r, M aimat->AddProperty(&ior.ior, 1, AI_MATKEY_REFRACTI); } - // KHR_materials_emissive_strength + // KHR_materials_emissive_strength if (mat.materialEmissiveStrength.isPresent) { MaterialEmissiveStrength &emissiveStrength = mat.materialEmissiveStrength.value; From 95d98ec98beb89b2a77390994f2de2b733c85cb5 Mon Sep 17 00:00:00 2001 From: Adam Beili <54665621+Beilinson@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:55:20 +0200 Subject: [PATCH 3/5] fixed indentation error --- code/AssetLib/glTF2/glTF2AssetWriter.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 6b776dee9..f09efc35d 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -949,7 +949,7 @@ namespace glTF2 { exts.PushBack(StringRef("KHR_materials_ior"), mAl); } - if (this->mAsset.extensionsUsed.KHR_materials_emissive_strength) { + if (this->mAsset.extensionsUsed.KHR_materials_emissive_strength) { exts.PushBack(StringRef("KHR_materials_emissive_strength"), mAl); } From 355ebbedf3f0b17ccfffac8e1cfa74137eb95c41 Mon Sep 17 00:00:00 2001 From: David Korczynski Date: Mon, 21 Nov 2022 16:45:38 -0800 Subject: [PATCH 4/5] Add CIFuzz GitHub action --- .github/workflows/cifuzz.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/cifuzz.yml diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml new file mode 100644 index 000000000..a84be8cbc --- /dev/null +++ b/.github/workflows/cifuzz.yml @@ -0,0 +1,26 @@ +name: CIFuzz +on: [pull_request] +jobs: + Fuzzing: + runs-on: ubuntu-latest + steps: + - name: Build Fuzzers + id: build + uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master + with: + oss-fuzz-project-name: 'assimp' + dry-run: false + language: c++ + - name: Run Fuzzers + uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master + with: + oss-fuzz-project-name: 'assimp' + fuzz-seconds: 300 + dry-run: false + language: c++ + - name: Upload Crash + uses: actions/upload-artifact@v3 + if: failure() && steps.build.outcome == 'success' + with: + name: artifacts + path: ./out/artifacts From 5689ac7869c146c32f10752d55ae268fbfba5437 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 22 Nov 2022 10:50:52 +0100 Subject: [PATCH 5/5] Add overfolow check for invalid data. - closes https://github.com/assimp/assimp/issues/3422 --- code/AssetLib/MDL/MDLLoader.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/MDL/MDLLoader.cpp b/code/AssetLib/MDL/MDLLoader.cpp index c0a63709b..0ae25580a 100644 --- a/code/AssetLib/MDL/MDLLoader.cpp +++ b/code/AssetLib/MDL/MDLLoader.cpp @@ -404,8 +404,13 @@ void MDLImporter::InternReadFile_Quake1() { this->CreateTextureARGB8_3DGS_MDL3(szCurrent + iNumImages * sizeof(float)); } // go to the end of the skin section / the beginning of the next skin - szCurrent += pcHeader->skinheight * pcHeader->skinwidth + - sizeof(float) * iNumImages; + bool overflow = false; + if ((pcHeader->skinheight > INT_MAX / pcHeader->skinwidth) || (pcHeader->skinwidth > INT_MAX / pcHeader->skinheight)){ + overflow = true; + } + if (!overflow) { + szCurrent += pcHeader->skinheight * pcHeader->skinwidth +sizeof(float) * iNumImages; + } } } else { szCurrent += sizeof(uint32_t);