#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace XCEngine::Resources; using namespace XCEngine::Containers; namespace { std::string GetMeshFixturePath(const char* fileName) { return (std::filesystem::path(XCENGINE_TEST_FIXTURES_DIR) / "Resources" / "Mesh" / fileName).string(); } XCEngine::Core::uint32 ReadMeshIndex(const Mesh& mesh, XCEngine::Core::uint32 index) { if (mesh.IsUse32BitIndex()) { const auto* indices = static_cast(mesh.GetIndexData()); return indices[index]; } const auto* indices = static_cast(mesh.GetIndexData()); return static_cast(indices[index]); } XCEngine::Core::uint32 GetFirstSectionMaterialIndex(const Mesh& mesh) { if (mesh.GetSections().Empty()) { return 0; } return mesh.GetSections()[0].materialID; } Material* GetFirstSectionMaterial(Mesh& mesh) { const XCEngine::Core::uint32 materialIndex = GetFirstSectionMaterialIndex(mesh); if (materialIndex >= mesh.GetMaterials().Size()) { return nullptr; } return mesh.GetMaterial(materialIndex); } bool PumpAsyncLoadsUntilIdle(ResourceManager& manager, std::chrono::milliseconds timeout = std::chrono::milliseconds(4000)) { const auto deadline = std::chrono::steady_clock::now() + timeout; while (manager.IsAsyncLoading() && std::chrono::steady_clock::now() < deadline) { manager.UpdateAsyncLoads(); std::this_thread::sleep_for(std::chrono::milliseconds(5)); } manager.UpdateAsyncLoads(); return !manager.IsAsyncLoading(); } void FlipLastByte(const std::filesystem::path& path) { std::ifstream input(path, std::ios::binary); ASSERT_TRUE(input.is_open()); std::vector bytes( (std::istreambuf_iterator(input)), std::istreambuf_iterator()); ASSERT_FALSE(bytes.empty()); bytes.back() ^= 0x01; std::ofstream output(path, std::ios::binary | std::ios::trunc); ASSERT_TRUE(output.is_open()); output.write(bytes.data(), static_cast(bytes.size())); ASSERT_TRUE(static_cast(output)); } TEST(MeshLoader, GetResourceType) { MeshLoader loader; EXPECT_EQ(loader.GetResourceType(), ResourceType::Mesh); } TEST(MeshLoader, GetSupportedExtensions) { MeshLoader loader; auto extensions = loader.GetSupportedExtensions(); EXPECT_GE(extensions.Size(), 1u); } TEST(MeshLoader, CanLoad) { MeshLoader loader; EXPECT_TRUE(loader.CanLoad("test.obj")); EXPECT_TRUE(loader.CanLoad("test.fbx")); EXPECT_TRUE(loader.CanLoad("test.gltf")); EXPECT_TRUE(loader.CanLoad("test.OBJ")); EXPECT_FALSE(loader.CanLoad("test.txt")); EXPECT_FALSE(loader.CanLoad("test.png")); } TEST(MeshLoader, LoadInvalidPath) { MeshLoader loader; LoadResult result = loader.Load("invalid/path/mesh.obj"); EXPECT_FALSE(result); } TEST(MeshLoader, ResourceManagerRegistersMeshAndTextureLoaders) { ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); EXPECT_NE(manager.GetLoader(ResourceType::Mesh), nullptr); EXPECT_NE(manager.GetLoader(ResourceType::Texture), nullptr); manager.Shutdown(); } TEST(MeshLoader, LoadValidObjMesh) { MeshLoader loader; const std::string path = GetMeshFixturePath("triangle.obj"); LoadResult result = loader.Load(path.c_str()); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); auto* mesh = static_cast(result.resource); EXPECT_EQ(mesh->GetVertexCount(), 3u); EXPECT_EQ(mesh->GetIndexCount(), 3u); EXPECT_EQ(mesh->GetVertexStride(), sizeof(StaticMeshVertex)); EXPECT_FALSE(mesh->IsUse32BitIndex()); ASSERT_EQ(mesh->GetSections().Size(), 1u); EXPECT_EQ(mesh->GetSections()[0].vertexCount, 3u); EXPECT_EQ(mesh->GetSections()[0].indexCount, 3u); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Position)); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Normal)); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::UV0)); EXPECT_EQ(mesh->GetBounds().GetMin(), XCEngine::Math::Vector3(0.0f, 0.0f, -1.0f)); EXPECT_EQ(mesh->GetBounds().GetMax(), XCEngine::Math::Vector3(1.0f, 1.0f, -1.0f)); EXPECT_EQ(mesh->GetSections()[0].bounds.GetMin(), XCEngine::Math::Vector3(0.0f, 0.0f, -1.0f)); EXPECT_EQ(mesh->GetSections()[0].bounds.GetMax(), XCEngine::Math::Vector3(1.0f, 1.0f, -1.0f)); const auto* vertices = static_cast(mesh->GetVertexData()); ASSERT_NE(vertices, nullptr); EXPECT_FLOAT_EQ(vertices[0].position.x, 0.0f); EXPECT_FLOAT_EQ(vertices[0].position.y, 0.0f); EXPECT_FLOAT_EQ(vertices[0].position.z, -1.0f); EXPECT_FLOAT_EQ(vertices[0].normal.z, -1.0f); EXPECT_FLOAT_EQ(vertices[1].uv0.x, 1.0f); EXPECT_FLOAT_EQ(vertices[2].uv0.y, 1.0f); delete mesh; } TEST(MeshLoader, BuiltinSphereUsesFrontFacingWindingForOutwardNormals) { LoadResult result = CreateBuiltinMeshResource(GetBuiltinPrimitiveMeshPath(BuiltinPrimitiveType::Sphere)); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); auto* mesh = static_cast(result.resource); const auto* vertices = static_cast(mesh->GetVertexData()); ASSERT_NE(vertices, nullptr); ASSERT_GE(mesh->GetIndexCount(), 3u); bool foundNonDegenerateTriangle = false; for (XCEngine::Core::uint32 index = 0; index + 2 < mesh->GetIndexCount(); index += 3) { const XCEngine::Core::uint32 i0 = ReadMeshIndex(*mesh, index + 0); const XCEngine::Core::uint32 i1 = ReadMeshIndex(*mesh, index + 1); const XCEngine::Core::uint32 i2 = ReadMeshIndex(*mesh, index + 2); const XCEngine::Math::Vector3 edge01 = vertices[i1].position - vertices[i0].position; const XCEngine::Math::Vector3 edge02 = vertices[i2].position - vertices[i0].position; const XCEngine::Math::Vector3 geometricNormal = XCEngine::Math::Vector3::Cross(edge01, edge02); if (geometricNormal.SqrMagnitude() <= XCEngine::Math::EPSILON) { continue; } const XCEngine::Math::Vector3 averagedVertexNormal = (vertices[i0].normal + vertices[i1].normal + vertices[i2].normal).Normalized(); EXPECT_LT(XCEngine::Math::Vector3::Dot(geometricNormal, averagedVertexNormal), 0.0f); foundNonDegenerateTriangle = true; break; } EXPECT_TRUE(foundNonDegenerateTriangle); delete mesh; } TEST(MeshLoader, GeneratesNormalsAndTangentsWhenRequested) { MeshLoader loader; MeshImportSettings settings; settings.AddImportFlag(MeshImportFlags::GenerateNormals); settings.AddImportFlag(MeshImportFlags::GenerateTangents); const std::string path = GetMeshFixturePath("triangle_no_normals.obj"); LoadResult result = loader.Load(path.c_str(), &settings); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); auto* mesh = static_cast(result.resource); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Normal)); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Tangent)); EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Bitangent)); const auto* vertices = static_cast(mesh->GetVertexData()); ASSERT_NE(vertices, nullptr); EXPECT_GT(vertices[0].normal.Magnitude(), 0.0f); EXPECT_GT(vertices[0].tangent.Magnitude(), 0.0f); EXPECT_GT(vertices[0].bitangent.Magnitude(), 0.0f); delete mesh; } TEST(MeshLoader, ImportsMaterialTexturesFromObj) { MeshLoader loader; const std::string path = GetMeshFixturePath("textured_triangle.obj"); LoadResult result = loader.Load(path.c_str()); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); auto* mesh = static_cast(result.resource); ASSERT_EQ(mesh->GetSections().Size(), 1u); ASSERT_GE(mesh->GetMaterials().Size(), 1u); EXPECT_LT(mesh->GetSections()[0].materialID, mesh->GetMaterials().Size()); Material* material = mesh->GetMaterial(mesh->GetSections()[0].materialID); ASSERT_NE(material, nullptr); EXPECT_TRUE(material->HasProperty("baseColorTexture")); EXPECT_EQ(material->GetTextureBindingCount(), 1u); ResourceHandle diffuseTexture = material->GetTexture("baseColorTexture"); ASSERT_TRUE(diffuseTexture.IsValid()); EXPECT_EQ(diffuseTexture->GetWidth(), 2u); EXPECT_EQ(diffuseTexture->GetHeight(), 2u); EXPECT_EQ(diffuseTexture->GetPixelDataSize(), 16u); EXPECT_EQ(diffuseTexture->GetFormat(), TextureFormat::RGBA8_UNORM); EXPECT_EQ(mesh->GetTextures().Size(), 1u); delete mesh; } TEST(MeshLoader, AssetDatabaseCreatesModelArtifactAndReusesItWithoutReimport) { namespace fs = std::filesystem; using namespace std::chrono_literals; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_library_cache_test"; const fs::path assetsDir = projectRoot / "Assets"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::copy_file(GetMeshFixturePath("textured_triangle.obj"), assetsDir / "textured_triangle.obj", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"), assetsDir / "textured_triangle.mtl", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); MeshLoader sourceMeshLoader; LoadResult sourceMeshResult = sourceMeshLoader.Load((assetsDir / "textured_triangle.obj").string().c_str()); ASSERT_TRUE(sourceMeshResult); ASSERT_NE(sourceMeshResult.resource, nullptr); auto* sourceMesh = static_cast(sourceMeshResult.resource); ASSERT_NE(sourceMesh, nullptr); ASSERT_GE(sourceMesh->GetMaterials().Size(), 1u); Material* sourceSectionMaterial = GetFirstSectionMaterial(*sourceMesh); ASSERT_NE(sourceSectionMaterial, nullptr); const XCEngine::Core::uint32 sourceMaterialIndex = GetFirstSectionMaterialIndex(*sourceMesh); EXPECT_EQ(sourceSectionMaterial->GetTextureBindingCount(), 1u); EXPECT_EQ(sourceSectionMaterial->GetTextureBindingName(0), "baseColorTexture"); EXPECT_FALSE(sourceSectionMaterial->GetTextureBindingPath(0).Empty()); delete sourceMesh; AssetDatabase database; database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset firstResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, firstResolve)); ASSERT_TRUE(firstResolve.exists); ASSERT_TRUE(firstResolve.artifactReady); EXPECT_TRUE(fs::exists(projectRoot / "Assets" / "textured_triangle.obj.meta")); EXPECT_TRUE(fs::exists(projectRoot / "Library" / "SourceAssetDB" / "assets.db")); EXPECT_TRUE(fs::exists(projectRoot / "Library" / "ArtifactDB" / "artifacts.db")); EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr())); EXPECT_TRUE(fs::exists((fs::path(firstResolve.artifactDirectory.CStr()) / ("material_" + std::to_string(sourceMaterialIndex) + ".xcmat")))); EXPECT_TRUE(fs::exists((fs::path(firstResolve.artifactDirectory.CStr()) / "texture_0.xctex"))); MaterialLoader materialLoader; LoadResult materialArtifactResult = materialLoader.Load((fs::path(firstResolve.artifactDirectory.CStr()) / ("material_" + std::to_string(sourceMaterialIndex) + ".xcmat")).string().c_str()); ASSERT_TRUE(materialArtifactResult); ASSERT_NE(materialArtifactResult.resource, nullptr); auto* artifactMaterial = static_cast(materialArtifactResult.resource); ASSERT_NE(artifactMaterial, nullptr); EXPECT_EQ(artifactMaterial->GetTextureBindingCount(), 1u); EXPECT_EQ(artifactMaterial->GetTextureBindingName(0), "baseColorTexture"); EXPECT_FALSE(artifactMaterial->GetTextureBindingPath(0).Empty()); const ResourceHandle artifactLazyTexture = artifactMaterial->GetTexture("baseColorTexture"); EXPECT_FALSE(artifactLazyTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle artifactResolvedTexture = artifactMaterial->GetTexture("baseColorTexture"); ASSERT_TRUE(artifactResolvedTexture.IsValid()); EXPECT_EQ(artifactResolvedTexture->GetWidth(), 2u); EXPECT_EQ(artifactResolvedTexture->GetHeight(), 2u); delete artifactMaterial; MeshLoader meshLoader; LoadResult meshArtifactResult = meshLoader.Load(firstResolve.artifactMainPath.CStr()); ASSERT_TRUE(meshArtifactResult); ASSERT_NE(meshArtifactResult.resource, nullptr); auto* artifactMesh = static_cast(meshArtifactResult.resource); ASSERT_NE(artifactMesh, nullptr); ASSERT_GE(artifactMesh->GetMaterials().Size(), 1u); Material* artifactSectionMaterial = GetFirstSectionMaterial(*artifactMesh); ASSERT_NE(artifactSectionMaterial, nullptr); EXPECT_EQ(artifactSectionMaterial->GetTextureBindingCount(), 1u); EXPECT_EQ(artifactSectionMaterial->GetTextureBindingName(0), "baseColorTexture"); EXPECT_FALSE(artifactSectionMaterial->GetTextureBindingPath(0).Empty()); const ResourceHandle artifactMeshLazyTexture = artifactSectionMaterial->GetTexture("baseColorTexture"); EXPECT_FALSE(artifactMeshLazyTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle artifactMeshResolvedTexture = artifactSectionMaterial->GetTexture("baseColorTexture"); ASSERT_TRUE(artifactMeshResolvedTexture.IsValid()); EXPECT_EQ(artifactMeshResolvedTexture->GetWidth(), 2u); EXPECT_EQ(artifactMeshResolvedTexture->GetHeight(), 2u); delete artifactMesh; AssetRef assetRef; ASSERT_TRUE(database.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef)); EXPECT_TRUE(assetRef.IsValid()); const auto originalArtifactWriteTime = fs::last_write_time(firstResolve.artifactMainPath.CStr()); std::this_thread::sleep_for(50ms); AssetDatabase::ResolvedAsset secondResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, secondResolve)); EXPECT_EQ(firstResolve.artifactMainPath, secondResolve.artifactMainPath); EXPECT_EQ(originalArtifactWriteTime, fs::last_write_time(secondResolve.artifactMainPath.CStr())); database.Shutdown(); manager.Shutdown(); fs::remove_all(projectRoot); } TEST(MeshLoader, AssetDatabaseReimportsModelWhenDependencyChanges) { namespace fs = std::filesystem; using namespace std::chrono_literals; const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_dependency_reimport_test"; const fs::path assetsDir = projectRoot / "Assets"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::copy_file(GetMeshFixturePath("textured_triangle.obj"), assetsDir / "textured_triangle.obj", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"), assetsDir / "textured_triangle.mtl", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); AssetDatabase database; database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset firstResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, firstResolve)); ASSERT_TRUE(firstResolve.artifactReady); const String firstArtifactPath = firstResolve.artifactMainPath; database.Shutdown(); std::this_thread::sleep_for(50ms); { std::ofstream mtlOutput(assetsDir / "textured_triangle.mtl", std::ios::app); ASSERT_TRUE(mtlOutput.is_open()); mtlOutput << "\n# force dependency reimport\n"; ASSERT_TRUE(static_cast(mtlOutput)); } database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset secondResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, secondResolve)); ASSERT_TRUE(secondResolve.artifactReady); EXPECT_NE(firstArtifactPath, secondResolve.artifactMainPath); const String secondArtifactPath = secondResolve.artifactMainPath; database.Shutdown(); std::this_thread::sleep_for(50ms); FlipLastByte(assetsDir / "checker.bmp"); database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset thirdResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, thirdResolve)); ASSERT_TRUE(thirdResolve.artifactReady); EXPECT_NE(secondArtifactPath, thirdResolve.artifactMainPath); EXPECT_TRUE(fs::exists(thirdResolve.artifactMainPath.CStr())); database.Shutdown(); fs::remove_all(projectRoot); } TEST(MeshLoader, ResourceManagerLoadsModelByAssetRefFromProjectAssets) { namespace fs = std::filesystem; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_asset_ref_test"; const fs::path assetsDir = projectRoot / "Assets"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::copy_file(GetMeshFixturePath("textured_triangle.obj"), assetsDir / "textured_triangle.obj", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"), assetsDir / "textured_triangle.mtl", fs::copy_options::overwrite_existing); fs::copy_file(GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); manager.SetResourceRoot(projectRoot.string().c_str()); const auto firstHandle = manager.Load("Assets/textured_triangle.obj"); ASSERT_TRUE(firstHandle.IsValid()); EXPECT_EQ(firstHandle->GetVertexCount(), 3u); EXPECT_EQ(firstHandle->GetIndexCount(), 3u); EXPECT_GE(firstHandle->GetMaterials().Size(), 1u); EXPECT_EQ(firstHandle->GetTextures().Size(), 0u); const auto initialMaterialCount = firstHandle->GetMaterials().Size(); const XCEngine::Core::uint32 firstSectionMaterialIndex = GetFirstSectionMaterialIndex(*firstHandle.Get()); EXPECT_LT(firstSectionMaterialIndex, initialMaterialCount); Material* firstMaterial = GetFirstSectionMaterial(*firstHandle.Get()); ASSERT_NE(firstMaterial, nullptr); EXPECT_EQ(firstMaterial->GetTextureBindingCount(), 1u); EXPECT_EQ(firstMaterial->GetTextureBindingName(0), "baseColorTexture"); EXPECT_FALSE(firstMaterial->GetTextureBindingPath(0).Empty()); const ResourceHandle firstLazyTexture = firstMaterial->GetTexture("baseColorTexture"); EXPECT_FALSE(firstLazyTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle firstResolvedTexture = firstMaterial->GetTexture("baseColorTexture"); ASSERT_TRUE(firstResolvedTexture.IsValid()); EXPECT_EQ(firstResolvedTexture->GetWidth(), 2u); EXPECT_EQ(firstResolvedTexture->GetHeight(), 2u); AssetRef assetRef; ASSERT_TRUE(manager.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef)); EXPECT_TRUE(assetRef.IsValid()); manager.UnloadAll(); const auto secondHandle = manager.Load(assetRef); ASSERT_TRUE(secondHandle.IsValid()); EXPECT_EQ(secondHandle->GetPath(), "Assets/textured_triangle.obj"); EXPECT_EQ(secondHandle->GetVertexCount(), 3u); EXPECT_EQ(secondHandle->GetIndexCount(), 3u); EXPECT_EQ(secondHandle->GetMaterials().Size(), initialMaterialCount); EXPECT_EQ(secondHandle->GetTextures().Size(), 0u); EXPECT_EQ(GetFirstSectionMaterialIndex(*secondHandle.Get()), firstSectionMaterialIndex); Material* secondMaterial = GetFirstSectionMaterial(*secondHandle.Get()); ASSERT_NE(secondMaterial, nullptr); EXPECT_EQ(secondMaterial->GetTextureBindingCount(), 1u); EXPECT_EQ(secondMaterial->GetTextureBindingName(0), "baseColorTexture"); EXPECT_FALSE(secondMaterial->GetTextureBindingPath(0).Empty()); const ResourceHandle secondLazyTexture = secondMaterial->GetTexture("baseColorTexture"); EXPECT_FALSE(secondLazyTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle secondResolvedTexture = secondMaterial->GetTexture("baseColorTexture"); ASSERT_TRUE(secondResolvedTexture.IsValid()); EXPECT_EQ(secondResolvedTexture->GetWidth(), 2u); EXPECT_EQ(secondResolvedTexture->GetHeight(), 2u); manager.SetResourceRoot(""); manager.Shutdown(); fs::remove_all(projectRoot); } } // namespace