#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(); } void WriteArtifactString(std::ofstream& output, const XCEngine::Containers::String& value) { const XCEngine::Core::uint32 length = static_cast(value.Length()); output.write(reinterpret_cast(&length), sizeof(length)); if (length > 0) { output.write(value.CStr(), length); } } 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(MaterialLoader, GetResourceType) { MaterialLoader loader; EXPECT_EQ(loader.GetResourceType(), ResourceType::Material); } TEST(MaterialLoader, GetSupportedExtensions) { MaterialLoader loader; auto extensions = loader.GetSupportedExtensions(); EXPECT_GE(extensions.Size(), 1u); } TEST(MaterialLoader, CanLoad) { MaterialLoader loader; EXPECT_TRUE(loader.CanLoad("test.mat")); EXPECT_TRUE(loader.CanLoad("test.json")); EXPECT_TRUE(loader.CanLoad("test.xcmat")); EXPECT_FALSE(loader.CanLoad("test.txt")); EXPECT_FALSE(loader.CanLoad("test.png")); } TEST(MaterialLoader, LoadInvalidPath) { MaterialLoader loader; LoadResult result = loader.Load("invalid/path/material.mat"); EXPECT_FALSE(result); } TEST(MaterialLoader, ResourceManagerRegistersMaterialAndShaderLoaders) { ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); EXPECT_NE(manager.GetLoader(ResourceType::Material), nullptr); EXPECT_NE(manager.GetLoader(ResourceType::Shader), nullptr); manager.Shutdown(); } TEST(MaterialLoader, LoadValidMaterialParsesRenderMetadata) { const std::filesystem::path shaderPath = std::filesystem::current_path() / "material_loader_valid_shader.hlsl"; const std::filesystem::path materialPath = std::filesystem::current_path() / "material_loader_valid.material"; { std::ofstream shaderFile(shaderPath); ASSERT_TRUE(shaderFile.is_open()); shaderFile << "float4 MainPS() : SV_TARGET { return float4(1, 1, 1, 1); }"; } { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"shader\": \"" << shaderPath.generic_string() << "\",\n"; materialFile << " \"renderQueue\": \"Transparent\",\n"; materialFile << " \"shaderPass\": \"ForwardLit\",\n"; materialFile << " \"tags\": {\n"; materialFile << " \"LightMode\": \"ForwardBase\",\n"; materialFile << " \"RenderType\": \"Transparent\"\n"; materialFile << " },\n"; materialFile << " \"renderState\": {\n"; materialFile << " \"cull\": \"Back\",\n"; materialFile << " \"blend\": true,\n"; materialFile << " \"srcBlend\": \"SrcAlpha\",\n"; materialFile << " \"dstBlend\": \"InvSrcAlpha\",\n"; materialFile << " \"depthWrite\": false,\n"; materialFile << " \"depthFunc\": \"LessEqual\"\n"; materialFile << " }\n"; materialFile << "}"; } MaterialLoader loader; LoadResult result = loader.Load(materialPath.string().c_str()); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); Material* material = static_cast(result.resource); ASSERT_NE(material, nullptr); EXPECT_TRUE(material->IsValid()); EXPECT_NE(material->GetShader(), nullptr); EXPECT_EQ(material->GetRenderQueue(), static_cast(MaterialRenderQueue::Transparent)); EXPECT_EQ(material->GetShaderPass(), "ForwardLit"); EXPECT_EQ(material->GetTag("LightMode"), "ForwardBase"); EXPECT_EQ(material->GetTag("RenderType"), "Transparent"); EXPECT_EQ(material->GetRenderState().cullMode, MaterialCullMode::Back); EXPECT_TRUE(material->GetRenderState().blendEnable); EXPECT_EQ(material->GetRenderState().srcBlend, MaterialBlendFactor::SrcAlpha); EXPECT_EQ(material->GetRenderState().dstBlend, MaterialBlendFactor::InvSrcAlpha); EXPECT_FALSE(material->GetRenderState().depthWriteEnable); EXPECT_EQ(material->GetRenderState().depthFunc, MaterialComparisonFunc::LessEqual); delete material; std::remove(materialPath.string().c_str()); std::remove(shaderPath.string().c_str()); } TEST(MaterialLoader, LoadMaterialWithShaderManifestResolvesShaderPass) { namespace fs = std::filesystem; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path shaderRoot = fs::temp_directory_path() / "xc_material_shader_manifest_test"; const fs::path shaderDir = shaderRoot / "Shaders"; const fs::path manifestPath = shaderDir / "lit.shader"; const fs::path materialPath = shaderRoot / "manifest.material"; fs::remove_all(shaderRoot); fs::create_directories(shaderDir); { std::ofstream vertexFile(shaderDir / "lit.vert.glsl"); ASSERT_TRUE(vertexFile.is_open()); vertexFile << "#version 430\n// MATERIAL_MANIFEST_GL_VS\nvoid main() {}\n"; } { std::ofstream fragmentFile(shaderDir / "lit.frag.glsl"); ASSERT_TRUE(fragmentFile.is_open()); fragmentFile << "#version 430\n// MATERIAL_MANIFEST_GL_PS\nvoid main() {}\n"; } { std::ofstream manifestFile(manifestPath); ASSERT_TRUE(manifestFile.is_open()); manifestFile << "{\n"; manifestFile << " \"name\": \"ManifestLit\",\n"; manifestFile << " \"passes\": [\n"; manifestFile << " {\n"; manifestFile << " \"name\": \"ForwardLit\",\n"; manifestFile << " \"tags\": { \"LightMode\": \"ForwardBase\" },\n"; manifestFile << " \"variants\": [\n"; manifestFile << " { \"stage\": \"Vertex\", \"backend\": \"OpenGL\", \"language\": \"GLSL\", \"source\": \"lit.vert.glsl\" },\n"; manifestFile << " { \"stage\": \"Fragment\", \"backend\": \"OpenGL\", \"language\": \"GLSL\", \"source\": \"lit.frag.glsl\" }\n"; manifestFile << " ]\n"; manifestFile << " }\n"; manifestFile << " ]\n"; manifestFile << "}\n"; } { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"shader\": \"" << manifestPath.generic_string() << "\",\n"; materialFile << " \"shaderPass\": \"ForwardLit\",\n"; materialFile << " \"renderQueue\": \"Geometry\"\n"; materialFile << "}\n"; } MaterialLoader loader; LoadResult result = loader.Load(materialPath.string().c_str()); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); Material* material = static_cast(result.resource); ASSERT_NE(material, nullptr); ASSERT_NE(material->GetShader(), nullptr); EXPECT_EQ(material->GetShaderPass(), "ForwardLit"); ASSERT_NE(material->GetShader()->FindPass("ForwardLit"), nullptr); const ShaderStageVariant* vertexVariant = material->GetShader()->FindVariant("ForwardLit", ShaderType::Vertex, ShaderBackend::OpenGL); ASSERT_NE(vertexVariant, nullptr); EXPECT_NE(std::string(vertexVariant->sourceCode.CStr()).find("MATERIAL_MANIFEST_GL_VS"), std::string::npos); delete material; manager.Shutdown(); fs::remove_all(shaderRoot); } TEST(MaterialLoader, RejectsUnknownRenderQueueName) { const std::filesystem::path materialPath = std::filesystem::current_path() / "material_loader_invalid_queue.material"; { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{ \"renderQueue\": \"NotAQueue\" }"; } MaterialLoader loader; LoadResult result = loader.Load(materialPath.string().c_str()); EXPECT_FALSE(result); std::remove(materialPath.string().c_str()); } TEST(MaterialLoader, RejectsUnknownRenderStateEnum) { const std::filesystem::path materialPath = std::filesystem::current_path() / "material_loader_invalid_render_state.material"; { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{ \"renderState\": { \"cull\": \"Sideways\" } }"; } MaterialLoader loader; LoadResult result = loader.Load(materialPath.string().c_str()); EXPECT_FALSE(result); std::remove(materialPath.string().c_str()); } TEST(MaterialLoader, ResourceManagerLoadsRelativeMaterialFromResourceRoot) { namespace fs = std::filesystem; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path previousPath = fs::current_path(); const fs::path projectRoot = fs::temp_directory_path() / "xc_material_loader_resource_root"; const fs::path assetsDir = projectRoot / "Assets"; const fs::path materialPath = assetsDir / "relative.material"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"renderQueue\": \"geometry\",\n"; materialFile << " \"renderState\": {\n"; materialFile << " \"cull\": \"back\",\n"; materialFile << " \"colorWriteMask\": 15\n"; materialFile << " }\n"; materialFile << "}"; } manager.SetResourceRoot(projectRoot.string().c_str()); fs::current_path(projectRoot.parent_path()); { const auto materialHandle = manager.Load("Assets/relative.material"); ASSERT_TRUE(materialHandle.IsValid()); EXPECT_EQ(materialHandle->GetRenderQueue(), static_cast(MaterialRenderQueue::Geometry)); EXPECT_EQ(materialHandle->GetRenderState().cullMode, MaterialCullMode::Back); EXPECT_EQ(materialHandle->GetRenderState().colorWriteMask, 15); } fs::current_path(previousPath); fs::remove_all(projectRoot); manager.SetResourceRoot(""); manager.Shutdown(); } TEST(MaterialLoader, AssetDatabaseCreatesMaterialArtifact) { namespace fs = std::filesystem; const fs::path projectRoot = fs::temp_directory_path() / "xc_material_artifact_test"; const fs::path assetsDir = projectRoot / "Assets"; const fs::path materialPath = assetsDir / "relative.material"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"renderQueue\": \"geometry\",\n"; materialFile << " \"renderState\": {\n"; materialFile << " \"cull\": \"back\"\n"; materialFile << " }\n"; materialFile << "}"; } AssetDatabase database; database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset resolvedAsset; ASSERT_TRUE(database.EnsureArtifact("Assets/relative.material", ResourceType::Material, resolvedAsset)); EXPECT_TRUE(resolvedAsset.artifactReady); EXPECT_EQ(fs::path(resolvedAsset.artifactMainPath.CStr()).extension().string(), ".xcmat"); EXPECT_TRUE(fs::exists(resolvedAsset.artifactMainPath.CStr())); database.Shutdown(); fs::remove_all(projectRoot); } TEST(MaterialLoader, ResourceManagerLoadsProjectMaterialTextureAsLazyDependency) { namespace fs = std::filesystem; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path projectRoot = fs::temp_directory_path() / "xc_material_asset_texture_test"; const fs::path assetsDir = projectRoot / "Assets"; const fs::path materialPath = assetsDir / "textured.material"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::copy_file( GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"renderQueue\": \"geometry\",\n"; materialFile << " \"textures\": {\n"; materialFile << " \"baseColorTexture\": \"checker.bmp\"\n"; materialFile << " }\n"; materialFile << "}"; } manager.SetResourceRoot(projectRoot.string().c_str()); const auto materialHandle = manager.Load("Assets/textured.material"); ASSERT_TRUE(materialHandle.IsValid()); ASSERT_EQ(materialHandle->GetTextureBindingCount(), 1u); EXPECT_EQ(materialHandle->GetTextureBindingName(0), "baseColorTexture"); EXPECT_EQ( fs::path(materialHandle->GetTextureBindingPath(0).CStr()).lexically_normal().generic_string(), (projectRoot / "Assets" / "checker.bmp").lexically_normal().generic_string()); const ResourceHandle initialTexture = materialHandle->GetTexture("baseColorTexture"); EXPECT_FALSE(initialTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle loadedTexture = materialHandle->GetTexture("baseColorTexture"); ASSERT_TRUE(loadedTexture.IsValid()); EXPECT_EQ(loadedTexture->GetWidth(), 2u); EXPECT_EQ(loadedTexture->GetHeight(), 2u); manager.SetResourceRoot(""); manager.Shutdown(); fs::remove_all(projectRoot); } TEST(MaterialLoader, AssetDatabaseReimportsMaterialWhenTextureDependencyChanges) { namespace fs = std::filesystem; using namespace std::chrono_literals; const fs::path projectRoot = fs::temp_directory_path() / "xc_material_dependency_reimport_test"; const fs::path assetsDir = projectRoot / "Assets"; const fs::path materialPath = assetsDir / "textured.material"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::copy_file( GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); { std::ofstream materialFile(materialPath); ASSERT_TRUE(materialFile.is_open()); materialFile << "{\n"; materialFile << " \"baseColorTexture\": \"checker.bmp\"\n"; materialFile << "}"; } AssetDatabase database; database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset firstResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured.material", ResourceType::Material, firstResolve)); ASSERT_TRUE(firstResolve.artifactReady); const String firstArtifactPath = firstResolve.artifactMainPath; database.Shutdown(); std::this_thread::sleep_for(50ms); FlipLastByte(assetsDir / "checker.bmp"); database.Initialize(projectRoot.string().c_str()); AssetDatabase::ResolvedAsset secondResolve; ASSERT_TRUE(database.EnsureArtifact("Assets/textured.material", ResourceType::Material, secondResolve)); ASSERT_TRUE(secondResolve.artifactReady); EXPECT_NE(firstArtifactPath, secondResolve.artifactMainPath); EXPECT_TRUE(fs::exists(secondResolve.artifactMainPath.CStr())); database.Shutdown(); fs::remove_all(projectRoot); } TEST(MaterialLoader, LoadMaterialArtifactDefersTexturePayloadUntilRequested) { namespace fs = std::filesystem; ResourceManager& manager = ResourceManager::Get(); manager.Initialize(); const fs::path projectRoot = fs::temp_directory_path() / "xc_material_xcmat_lazy_texture_test"; const fs::path assetsDir = projectRoot / "Assets"; const fs::path libraryDir = projectRoot / "Library" / "Manual"; const fs::path materialArtifactPath = libraryDir / "test.xcmat"; fs::remove_all(projectRoot); fs::create_directories(assetsDir); fs::create_directories(libraryDir); fs::copy_file( GetMeshFixturePath("checker.bmp"), assetsDir / "checker.bmp", fs::copy_options::overwrite_existing); { std::ofstream output(materialArtifactPath, std::ios::binary | std::ios::trunc); ASSERT_TRUE(output.is_open()); MaterialArtifactFileHeader fileHeader; output.write(reinterpret_cast(&fileHeader), sizeof(fileHeader)); WriteArtifactString(output, "LazyMaterial"); WriteArtifactString(output, "Assets/lazy.material"); WriteArtifactString(output, ""); WriteArtifactString(output, ""); MaterialArtifactHeader header; header.renderQueue = static_cast(MaterialRenderQueue::Geometry); header.textureBindingCount = 1; output.write(reinterpret_cast(&header), sizeof(header)); WriteArtifactString(output, "baseColorTexture"); WriteArtifactString(output, "Assets/checker.bmp"); } manager.SetResourceRoot(projectRoot.string().c_str()); MaterialLoader loader; LoadResult result = loader.Load("Library/Manual/test.xcmat"); ASSERT_TRUE(result); ASSERT_NE(result.resource, nullptr); auto* material = static_cast(result.resource); ASSERT_NE(material, nullptr); EXPECT_EQ(material->GetTextureBindingCount(), 1u); EXPECT_EQ( fs::path(material->GetTextureBindingPath(0).CStr()).lexically_normal().generic_string(), (projectRoot / "Assets" / "checker.bmp").lexically_normal().generic_string()); const ResourceHandle initialTexture = material->GetTexture("baseColorTexture"); EXPECT_FALSE(initialTexture.IsValid()); EXPECT_GT(manager.GetAsyncPendingCount(), 0u); ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager)); const ResourceHandle loadedTexture = material->GetTexture("baseColorTexture"); ASSERT_TRUE(loadedTexture.IsValid()); EXPECT_EQ(loadedTexture->GetWidth(), 2u); EXPECT_EQ(loadedTexture->GetHeight(), 2u); delete material; manager.SetResourceRoot(""); manager.Shutdown(); fs::remove_all(projectRoot); } } // namespace