feat: add multi-pass shader manifest loading

This commit is contained in:
2026-04-02 22:54:25 +08:00
parent 19db2305d9
commit c9adc6ec5e
3 changed files with 1056 additions and 39 deletions

View File

@@ -11,6 +11,7 @@
#include <filesystem>
#include <fstream>
#include <thread>
#include <vector>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
@@ -41,6 +42,23 @@ bool PumpAsyncLoadsUntilIdle(ResourceManager& manager,
return !manager.IsAsyncLoading();
}
void FlipLastByte(const std::filesystem::path& path) {
std::ifstream input(path, std::ios::binary);
ASSERT_TRUE(input.is_open());
std::vector<char> bytes(
(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>());
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<std::streamsize>(bytes.size()));
ASSERT_TRUE(static_cast<bool>(output));
}
TEST(MaterialLoader, GetResourceType) {
MaterialLoader loader;
EXPECT_EQ(loader.GetResourceType(), ResourceType::Material);
@@ -136,6 +154,80 @@ TEST(MaterialLoader, LoadValidMaterialParsesRenderMetadata) {
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<Material*>(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";
@@ -247,6 +339,105 @@ TEST(MaterialLoader, AssetDatabaseCreatesMaterialArtifact) {
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<Material>("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<Texture> initialTexture = materialHandle->GetTexture("baseColorTexture");
EXPECT_FALSE(initialTexture.IsValid());
EXPECT_GT(manager.GetAsyncPendingCount(), 0u);
ASSERT_TRUE(PumpAsyncLoadsUntilIdle(manager));
const ResourceHandle<Texture> 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;