Split mesh artifacts into material and texture artifacts
This commit is contained in:
@@ -1,18 +1,46 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Core/Asset/ArtifactFormats.h>
|
||||
#include <XCEngine/Core/Asset/AssetDatabase.h>
|
||||
#include <XCEngine/Resources/Material/MaterialLoader.h>
|
||||
#include <XCEngine/Core/Asset/ResourceManager.h>
|
||||
#include <XCEngine/Core/Asset/ResourceTypes.h>
|
||||
#include <XCEngine/Core/Containers/Array.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
|
||||
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<XCEngine::Core::uint32>(value.Length());
|
||||
output.write(reinterpret_cast<const char*>(&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();
|
||||
}
|
||||
|
||||
TEST(MaterialLoader, GetResourceType) {
|
||||
MaterialLoader loader;
|
||||
EXPECT_EQ(loader.GetResourceType(), ResourceType::Material);
|
||||
@@ -28,6 +56,7 @@ 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"));
|
||||
}
|
||||
@@ -184,4 +213,107 @@ TEST(MaterialLoader, ResourceManagerLoadsRelativeMaterialFromResourceRoot) {
|
||||
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, 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<const char*>(&fileHeader), sizeof(fileHeader));
|
||||
WriteArtifactString(output, "LazyMaterial");
|
||||
WriteArtifactString(output, "Assets/lazy.material");
|
||||
WriteArtifactString(output, "");
|
||||
WriteArtifactString(output, "");
|
||||
|
||||
MaterialArtifactHeader header;
|
||||
header.renderQueue = static_cast<XCEngine::Core::int32>(MaterialRenderQueue::Geometry);
|
||||
header.textureBindingCount = 1;
|
||||
output.write(reinterpret_cast<const char*>(&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<Material*>(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<Texture> initialTexture = material->GetTexture("baseColorTexture");
|
||||
EXPECT_FALSE(initialTexture.IsValid());
|
||||
EXPECT_GT(manager.GetAsyncPendingCount(), 0u);
|
||||
|
||||
ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager));
|
||||
const ResourceHandle<Texture> 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
|
||||
|
||||
Reference in New Issue
Block a user