1218 lines
43 KiB
C++
1218 lines
43 KiB
C++
#include <gtest/gtest.h>
|
|
#include <XCEngine/Core/Asset/ArtifactFormats.h>
|
|
#include <XCEngine/Core/Asset/AssetDatabase.h>
|
|
#include <XCEngine/Resources/BuiltinResources.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>
|
|
#include <vector>
|
|
|
|
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();
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
void WriteTextFile(const std::filesystem::path& path, const std::string& contents) {
|
|
std::ofstream output(path, std::ios::binary | std::ios::trunc);
|
|
ASSERT_TRUE(output.is_open());
|
|
output << contents;
|
|
ASSERT_TRUE(static_cast<bool>(output));
|
|
}
|
|
|
|
std::filesystem::path WriteSchemaMaterialShaderAuthoring(const std::filesystem::path& rootPath) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path shaderDir = rootPath / "Shaders";
|
|
fs::create_directories(shaderDir);
|
|
|
|
const fs::path shaderPath = shaderDir / "schema.shader";
|
|
std::ofstream shader(shaderPath, std::ios::binary | std::ios::trunc);
|
|
EXPECT_TRUE(shader.is_open());
|
|
if (!shader.is_open()) {
|
|
return {};
|
|
}
|
|
shader << R"(Shader "SchemaMaterialShader"
|
|
{
|
|
Properties
|
|
{
|
|
_BaseColor ("Base Color", Color) = (1,0.5,0.25,1) [Semantic(BaseColor)]
|
|
_Metallic ("Metallic", Float) = 0.7
|
|
_Mode ("Mode", Int) = 2
|
|
_MainTex ("Main Tex", 2D) = "white" [Semantic(BaseColorTexture)]
|
|
}
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
HLSLPROGRAM
|
|
#pragma vertex MainVS
|
|
#pragma fragment MainPS
|
|
float4 MainVS() : SV_POSITION { return 0; }
|
|
float4 MainPS() : SV_TARGET { return 1; }
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|
|
)";
|
|
EXPECT_TRUE(static_cast<bool>(shader));
|
|
|
|
return shaderPath;
|
|
}
|
|
|
|
std::filesystem::path WriteKeywordMaterialShaderAuthoring(const std::filesystem::path& rootPath) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path shaderDir = rootPath / "Assets" / "Shaders";
|
|
fs::create_directories(shaderDir);
|
|
|
|
const fs::path shaderPath = shaderDir / "keyword.shader";
|
|
WriteTextFile(
|
|
shaderPath,
|
|
R"(Shader "KeywordMaterialShader"
|
|
{
|
|
Properties
|
|
{
|
|
_BaseColor ("Base Color", Color) = (1,1,1,1) [Semantic(BaseColor)]
|
|
}
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
HLSLPROGRAM
|
|
#pragma vertex Vert
|
|
#pragma fragment Frag
|
|
#pragma multi_compile _ XC_MAIN_LIGHT_SHADOWS
|
|
#pragma shader_feature_local _ XC_ALPHA_TEST
|
|
float4 Vert() : SV_POSITION { return 0; }
|
|
float4 Frag() : SV_TARGET { return 1; }
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|
|
)");
|
|
|
|
return shaderPath;
|
|
}
|
|
|
|
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) {
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const std::filesystem::path shaderPath =
|
|
std::filesystem::current_path() / "material_loader_valid_shader.shader";
|
|
const std::filesystem::path materialPath =
|
|
std::filesystem::current_path() / "material_loader_valid.material";
|
|
|
|
{
|
|
std::ofstream shaderFile(shaderPath);
|
|
ASSERT_TRUE(shaderFile.is_open());
|
|
shaderFile << R"(Shader "Test/ValidMaterial"
|
|
{
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
HLSLPROGRAM
|
|
#pragma vertex MainVS
|
|
#pragma fragment MainPS
|
|
float4 MainVS() : SV_POSITION { return 0; }
|
|
float4 MainPS() : SV_TARGET { return float4(1, 1, 1, 1); }
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|
|
)";
|
|
}
|
|
|
|
{
|
|
std::ofstream materialFile(materialPath);
|
|
ASSERT_TRUE(materialFile.is_open());
|
|
materialFile << "{\n";
|
|
materialFile << " \"shader\": \"" << shaderPath.generic_string() << "\",\n";
|
|
materialFile << " \"renderQueue\": \"Transparent\",\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<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
EXPECT_TRUE(material->IsValid());
|
|
EXPECT_NE(material->GetShader(), nullptr);
|
|
EXPECT_EQ(material->GetRenderQueue(), static_cast<XCEngine::Core::int32>(MaterialRenderQueue::Transparent));
|
|
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);
|
|
EXPECT_TRUE(material->HasRenderStateOverride());
|
|
|
|
delete material;
|
|
manager.Shutdown();
|
|
std::remove(materialPath.string().c_str());
|
|
std::remove(shaderPath.string().c_str());
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialWithoutRenderStateLeavesOverrideDisabled) {
|
|
const std::filesystem::path materialPath =
|
|
std::filesystem::current_path() / "material_loader_no_render_state.material";
|
|
|
|
{
|
|
std::ofstream materialFile(materialPath);
|
|
ASSERT_TRUE(materialFile.is_open());
|
|
materialFile << "{ \"renderQueue\": \"Geometry\" }";
|
|
}
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
EXPECT_FALSE(material->HasRenderStateOverride());
|
|
|
|
delete material;
|
|
std::remove(materialPath.string().c_str());
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialParsesOffsetAndStencilRenderState) {
|
|
const std::filesystem::path materialPath =
|
|
std::filesystem::current_path() / "material_loader_offset_stencil.material";
|
|
|
|
{
|
|
std::ofstream materialFile(materialPath);
|
|
ASSERT_TRUE(materialFile.is_open());
|
|
materialFile << "{\n";
|
|
materialFile << " \"renderState\": {\n";
|
|
materialFile << " \"offset\": [1.5, 2],\n";
|
|
materialFile << " \"stencil\": {\n";
|
|
materialFile << " \"ref\": 7,\n";
|
|
materialFile << " \"readMask\": 63,\n";
|
|
materialFile << " \"writeMask\": 31,\n";
|
|
materialFile << " \"comp\": \"Equal\",\n";
|
|
materialFile << " \"pass\": \"Replace\",\n";
|
|
materialFile << " \"fail\": \"Keep\",\n";
|
|
materialFile << " \"zFail\": \"IncrSat\",\n";
|
|
materialFile << " \"compBack\": \"NotEqual\",\n";
|
|
materialFile << " \"passBack\": \"DecrWrap\",\n";
|
|
materialFile << " \"failBack\": \"Invert\",\n";
|
|
materialFile << " \"zFailBack\": \"Zero\"\n";
|
|
materialFile << " }\n";
|
|
materialFile << " }\n";
|
|
materialFile << "}\n";
|
|
}
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
EXPECT_FLOAT_EQ(material->GetRenderState().depthBiasFactor, 1.5f);
|
|
EXPECT_EQ(material->GetRenderState().depthBiasUnits, 2);
|
|
EXPECT_TRUE(material->GetRenderState().stencil.enabled);
|
|
EXPECT_EQ(material->GetRenderState().stencil.reference, 7u);
|
|
EXPECT_EQ(material->GetRenderState().stencil.readMask, 63u);
|
|
EXPECT_EQ(material->GetRenderState().stencil.writeMask, 31u);
|
|
EXPECT_EQ(material->GetRenderState().stencil.front.func, MaterialComparisonFunc::Equal);
|
|
EXPECT_EQ(material->GetRenderState().stencil.front.passOp, MaterialStencilOp::Replace);
|
|
EXPECT_EQ(material->GetRenderState().stencil.front.depthFailOp, MaterialStencilOp::IncrSat);
|
|
EXPECT_EQ(material->GetRenderState().stencil.back.func, MaterialComparisonFunc::NotEqual);
|
|
EXPECT_EQ(material->GetRenderState().stencil.back.passOp, MaterialStencilOp::DecrWrap);
|
|
EXPECT_EQ(material->GetRenderState().stencil.back.failOp, MaterialStencilOp::Invert);
|
|
EXPECT_EQ(material->GetRenderState().stencil.back.depthFailOp, MaterialStencilOp::Zero);
|
|
|
|
delete material;
|
|
std::remove(materialPath.string().c_str());
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialWithAuthoringShaderResolvesShaderPass) {
|
|
namespace fs = std::filesystem;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path shaderRoot = fs::temp_directory_path() / "xc_material_shader_authoring_test";
|
|
const fs::path shaderDir = shaderRoot / "Shaders";
|
|
const fs::path shaderPath = shaderDir / "lit.shader";
|
|
const fs::path materialPath = shaderRoot / "authoring.material";
|
|
|
|
fs::remove_all(shaderRoot);
|
|
fs::create_directories(shaderDir);
|
|
WriteTextFile(
|
|
shaderPath,
|
|
R"(Shader "AuthoringLit"
|
|
{
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
Tags { "LightMode" = "ForwardLit" }
|
|
HLSLPROGRAM
|
|
#pragma vertex MainVS
|
|
#pragma fragment MainPS
|
|
float4 MainVS() : SV_POSITION
|
|
{
|
|
return 0; // MATERIAL_AUTHORING_VS
|
|
}
|
|
float4 MainPS() : SV_TARGET
|
|
{
|
|
return 1; // MATERIAL_AUTHORING_PS
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|
|
)");
|
|
|
|
{
|
|
std::ofstream materialFile(materialPath);
|
|
ASSERT_TRUE(materialFile.is_open());
|
|
materialFile << "{\n";
|
|
materialFile << " \"shader\": \"" << shaderPath.generic_string() << "\",\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);
|
|
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_AUTHORING_VS"), std::string::npos);
|
|
|
|
delete material;
|
|
manager.Shutdown();
|
|
fs::remove_all(shaderRoot);
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialWithPropertiesObjectAppliesTypedOverrides) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_properties_override_test";
|
|
fs::remove_all(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "override.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"_BaseColor\": [0.2, 0.4, 0.6, 0.8],\n"
|
|
" \"_Metallic\": 0.15,\n"
|
|
" \"_Mode\": 5\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
ASSERT_NE(material->GetShader(), nullptr);
|
|
EXPECT_EQ(material->GetFloat4("_BaseColor"), XCEngine::Math::Vector4(0.2f, 0.4f, 0.6f, 0.8f));
|
|
EXPECT_FLOAT_EQ(material->GetFloat("_Metallic"), 0.15f);
|
|
EXPECT_EQ(material->GetInt("_Mode"), 5);
|
|
EXPECT_TRUE(material->HasProperty("_MainTex"));
|
|
EXPECT_EQ(material->GetTextureBindingCount(), 0u);
|
|
|
|
delete material;
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadBuiltinShaderMaterialUsesShaderMetadataWithoutMaterialPassHint) {
|
|
namespace fs = std::filesystem;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_builtin_hint_strip_test";
|
|
fs::remove_all(rootPath);
|
|
fs::create_directories(rootPath);
|
|
|
|
const fs::path materialPath = rootPath / "builtin_unlit.material";
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + std::string(GetBuiltinUnlitShaderPath().CStr()) + "\"\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
ASSERT_NE(material->GetShader(), nullptr);
|
|
EXPECT_NE(material->GetShader()->FindPass("Unlit"), nullptr);
|
|
|
|
delete material;
|
|
manager.Shutdown();
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, RejectsUnknownPropertyAgainstShaderSchema) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_properties_unknown_test";
|
|
fs::remove_all(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "unknown_property.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"_Unknown\": 1.0\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
EXPECT_FALSE(result);
|
|
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, RejectsTypeMismatchAgainstShaderSchema) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_properties_mismatch_test";
|
|
fs::remove_all(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "type_mismatch.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"_BaseColor\": 1.0\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
EXPECT_FALSE(result);
|
|
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialWithPropertiesObjectPreservesShaderDefaultsForOmittedValues) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_properties_defaults_test";
|
|
fs::remove_all(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "defaults.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"_Metallic\": 0.33\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
EXPECT_EQ(material->GetFloat4("_BaseColor"), XCEngine::Math::Vector4(1.0f, 0.5f, 0.25f, 1.0f));
|
|
EXPECT_FLOAT_EQ(material->GetFloat("_Metallic"), 0.33f);
|
|
EXPECT_EQ(material->GetInt("_Mode"), 2);
|
|
EXPECT_TRUE(material->HasProperty("_MainTex"));
|
|
EXPECT_EQ(material->GetTextureBindingCount(), 0u);
|
|
|
|
delete material;
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, RejectsSemanticKeysWhenShaderSchemaRequiresExactPropertyNames) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_semantic_alias_test";
|
|
fs::remove_all(rootPath);
|
|
fs::create_directories(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "semantic_alias.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"baseColor\": [0.2, 0.4, 0.6, 0.8]\n"
|
|
" },\n"
|
|
" \"textures\": {\n"
|
|
" \"baseColorTexture\": \"checker.bmp\"\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
EXPECT_FALSE(result);
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, LoadMaterialParsesKeywordArrayAgainstShaderSchema) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_keywords_test";
|
|
fs::remove_all(rootPath);
|
|
|
|
const fs::path shaderPath = WriteKeywordMaterialShaderAuthoring(rootPath);
|
|
const fs::path materialPath = rootPath / "keyword.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"keywords\": [\"XC_MAIN_LIGHT_SHADOWS\", \"XC_ALPHA_TEST\", \"XC_ALPHA_TEST\", \"_\"]\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
ASSERT_NE(material->GetShader(), nullptr);
|
|
ASSERT_EQ(material->GetKeywordCount(), 2u);
|
|
EXPECT_EQ(material->GetKeyword(0), "XC_ALPHA_TEST");
|
|
EXPECT_EQ(material->GetKeyword(1), "XC_MAIN_LIGHT_SHADOWS");
|
|
EXPECT_TRUE(material->IsKeywordEnabled("XC_ALPHA_TEST"));
|
|
EXPECT_TRUE(material->IsKeywordEnabled("XC_MAIN_LIGHT_SHADOWS"));
|
|
|
|
delete material;
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
TEST(MaterialLoader, RejectsUnknownTextureBindingAgainstShaderSchema) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path rootPath = fs::temp_directory_path() / "xc_material_loader_unknown_texture_test";
|
|
fs::remove_all(rootPath);
|
|
fs::create_directories(rootPath);
|
|
|
|
const fs::path shaderPath = WriteSchemaMaterialShaderAuthoring(rootPath);
|
|
ASSERT_FALSE(shaderPath.empty());
|
|
const fs::path materialPath = rootPath / "unknown_texture.material";
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + shaderPath.generic_string() + "\",\n"
|
|
" \"textures\": {\n"
|
|
" \"unknownTexture\": \"checker.bmp\"\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(materialPath.generic_string().c_str());
|
|
EXPECT_FALSE(result);
|
|
|
|
fs::remove_all(rootPath);
|
|
}
|
|
|
|
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<Material>("Assets/relative.material");
|
|
ASSERT_TRUE(materialHandle.IsValid());
|
|
EXPECT_EQ(materialHandle->GetRenderQueue(), static_cast<XCEngine::Core::int32>(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, AssetDatabaseMaterialArtifactPreservesImplicitRenderStateFlag) {
|
|
namespace fs = std::filesystem;
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_implicit_render_state_artifact";
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
const fs::path materialPath = assetsDir / "implicit.material";
|
|
|
|
fs::remove_all(projectRoot);
|
|
fs::create_directories(assetsDir);
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"renderQueue\": \"Geometry\"\n"
|
|
"}\n");
|
|
|
|
AssetDatabase database;
|
|
database.Initialize(projectRoot.string().c_str());
|
|
|
|
AssetDatabase::ResolvedAsset resolvedAsset;
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/implicit.material", ResourceType::Material, resolvedAsset));
|
|
ASSERT_TRUE(resolvedAsset.artifactReady);
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(resolvedAsset.artifactMainPath.CStr());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
EXPECT_FALSE(material->HasRenderStateOverride());
|
|
|
|
delete material;
|
|
database.Shutdown();
|
|
fs::remove_all(projectRoot);
|
|
}
|
|
|
|
TEST(MaterialLoader, AssetDatabaseMaterialArtifactRoundTripsKeywords) {
|
|
namespace fs = std::filesystem;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_keyword_artifact_test";
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
const fs::path materialPath = assetsDir / "keyword.material";
|
|
|
|
fs::remove_all(projectRoot);
|
|
fs::create_directories(assetsDir);
|
|
WriteKeywordMaterialShaderAuthoring(projectRoot);
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"Assets/Shaders/keyword.shader\",\n"
|
|
" \"keywords\": [\"XC_MAIN_LIGHT_SHADOWS\", \"XC_ALPHA_TEST\"]\n"
|
|
"}\n");
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
manager.RefreshProjectAssets();
|
|
|
|
AssetDatabase database;
|
|
database.Initialize(projectRoot.string().c_str());
|
|
|
|
AssetDatabase::ResolvedAsset resolvedAsset;
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/keyword.material", ResourceType::Material, resolvedAsset));
|
|
ASSERT_TRUE(resolvedAsset.artifactReady);
|
|
EXPECT_TRUE(fs::exists(resolvedAsset.artifactMainPath.CStr()));
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(resolvedAsset.artifactMainPath.CStr());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
ASSERT_NE(material->GetShader(), nullptr);
|
|
ASSERT_EQ(material->GetKeywordCount(), 2u);
|
|
EXPECT_EQ(material->GetKeyword(0), "XC_ALPHA_TEST");
|
|
EXPECT_EQ(material->GetKeyword(1), "XC_MAIN_LIGHT_SHADOWS");
|
|
|
|
delete material;
|
|
database.Shutdown();
|
|
manager.SetResourceRoot("");
|
|
manager.Shutdown();
|
|
fs::remove_all(projectRoot);
|
|
}
|
|
|
|
TEST(MaterialLoader, AssetDatabaseMaterialArtifactRoundTripsBuiltinShaderWithoutMaterialPassHint) {
|
|
namespace fs = std::filesystem;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_artifact_builtin_hint_strip_test";
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
const fs::path materialPath = assetsDir / "builtin_unlit.material";
|
|
|
|
fs::remove_all(projectRoot);
|
|
fs::create_directories(assetsDir);
|
|
|
|
WriteTextFile(
|
|
materialPath,
|
|
"{\n"
|
|
" \"shader\": \"" + std::string(GetBuiltinUnlitShaderPath().CStr()) + "\",\n"
|
|
" \"properties\": {\n"
|
|
" \"_BaseColor\": [1.0, 1.0, 1.0, 1.0]\n"
|
|
" }\n"
|
|
"}\n");
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
manager.RefreshProjectAssets();
|
|
|
|
AssetDatabase database;
|
|
database.Initialize(projectRoot.string().c_str());
|
|
|
|
AssetDatabase::ResolvedAsset resolvedAsset;
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/builtin_unlit.material", ResourceType::Material, resolvedAsset));
|
|
ASSERT_TRUE(resolvedAsset.artifactReady);
|
|
|
|
MaterialLoader loader;
|
|
LoadResult result = loader.Load(resolvedAsset.artifactMainPath.CStr());
|
|
ASSERT_TRUE(result);
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
auto* material = static_cast<Material*>(result.resource);
|
|
ASSERT_NE(material, nullptr);
|
|
ASSERT_NE(material->GetShader(), nullptr);
|
|
EXPECT_EQ(material->GetFloat4("_BaseColor"), XCEngine::Math::Vector4(1.0f, 1.0f, 1.0f, 1.0f));
|
|
|
|
delete material;
|
|
database.Shutdown();
|
|
manager.SetResourceRoot("");
|
|
manager.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<Material>("Assets/textured.material");
|
|
ASSERT_TRUE(materialHandle.IsValid());
|
|
ASSERT_EQ(materialHandle->GetTextureBindingCount(), 1u);
|
|
EXPECT_EQ(materialHandle->GetTextureBindingName(0), "baseColorTexture");
|
|
EXPECT_TRUE(materialHandle->GetTextureBindingAssetRef(0).IsValid());
|
|
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, ResourceManagerPreservesProjectRelativeTexturePathWithoutDuplicatingAssetsPrefix) {
|
|
namespace fs = std::filesystem;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_project_relative_texture_path_test";
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
const fs::path materialPath = assetsDir / "textured.material";
|
|
const fs::path textureDir = assetsDir / "Models" / "backpack";
|
|
const fs::path texturePath = textureDir / "checker.bmp";
|
|
|
|
fs::remove_all(projectRoot);
|
|
fs::create_directories(textureDir);
|
|
fs::copy_file(
|
|
GetMeshFixturePath("checker.bmp"),
|
|
texturePath,
|
|
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\": \"Assets/Models/backpack/checker.bmp\"\n";
|
|
materialFile << " }\n";
|
|
materialFile << "}\n";
|
|
}
|
|
|
|
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_TRUE(materialHandle->GetTextureBindingAssetRef(0).IsValid());
|
|
EXPECT_EQ(
|
|
fs::path(materialHandle->GetTextureBindingPath(0).CStr()).lexically_normal().generic_string(),
|
|
texturePath.lexically_normal().generic_string());
|
|
EXPECT_EQ(
|
|
fs::path(materialHandle->GetTextureBindingPath(0).CStr()).lexically_normal().generic_string().find("Assets/Assets"),
|
|
std::string::npos);
|
|
|
|
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, AssetDatabaseReimportsMaterialWhenShaderDependencyChanges) {
|
|
namespace fs = std::filesystem;
|
|
using namespace std::chrono_literals;
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
manager.Initialize();
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_shader_dependency_reimport_test";
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
const fs::path shaderDir = assetsDir / "Shaders";
|
|
const fs::path shaderPath = shaderDir / "lit.shader";
|
|
const fs::path materialPath = assetsDir / "textured.material";
|
|
|
|
fs::remove_all(projectRoot);
|
|
fs::create_directories(shaderDir);
|
|
|
|
WriteTextFile(
|
|
shaderPath,
|
|
R"(Shader "MaterialDependencyShader"
|
|
{
|
|
SubShader
|
|
{
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
HLSLPROGRAM
|
|
#pragma vertex MainVS
|
|
#pragma fragment MainPS
|
|
float4 MainVS() : SV_POSITION { return 0; }
|
|
float4 MainPS() : SV_TARGET { return 1; }
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|
|
)");
|
|
|
|
{
|
|
std::ofstream materialFile(materialPath);
|
|
ASSERT_TRUE(materialFile.is_open());
|
|
materialFile << "{\n";
|
|
materialFile << " \"shader\": \"Assets/Shaders/lit.shader\",\n";
|
|
materialFile << " \"renderQueue\": \"geometry\"\n";
|
|
materialFile << "}\n";
|
|
}
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
|
|
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);
|
|
{
|
|
std::ofstream shaderFile(shaderPath, std::ios::app);
|
|
ASSERT_TRUE(shaderFile.is_open());
|
|
shaderFile << "\n// force shader dependency reimport\n";
|
|
ASSERT_TRUE(static_cast<bool>(shaderFile));
|
|
}
|
|
|
|
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();
|
|
|
|
manager.SetResourceRoot("");
|
|
manager.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);
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
manager.RefreshProjectAssets();
|
|
|
|
{
|
|
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, "");
|
|
|
|
MaterialArtifactHeader header;
|
|
header.renderQueue = static_cast<XCEngine::Core::int32>(MaterialRenderQueue::Geometry);
|
|
header.textureBindingCount = 1;
|
|
output.write(reinterpret_cast<const char*>(&header), sizeof(header));
|
|
|
|
AssetRef textureRef;
|
|
ASSERT_TRUE(manager.TryGetAssetRef("Assets/checker.bmp", ResourceType::Texture, textureRef));
|
|
ASSERT_TRUE(textureRef.IsValid());
|
|
const String encodedTextureRef =
|
|
textureRef.assetGuid.ToString() + "," +
|
|
String(std::to_string(textureRef.localID).c_str()) + "," +
|
|
String(std::to_string(static_cast<int>(textureRef.resourceType)).c_str());
|
|
|
|
WriteArtifactString(output, "baseColorTexture");
|
|
WriteArtifactString(output, encodedTextureRef);
|
|
WriteArtifactString(output, "");
|
|
}
|
|
|
|
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_TRUE(material->GetTextureBindingAssetRef(0).IsValid());
|
|
EXPECT_TRUE(material->GetTextureBindingPath(0).Empty());
|
|
|
|
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);
|
|
EXPECT_EQ(
|
|
fs::path(material->GetTextureBindingPath(0).CStr()).lexically_normal().generic_string(),
|
|
fs::path("Assets/checker.bmp").lexically_normal().generic_string());
|
|
|
|
delete material;
|
|
manager.SetResourceRoot("");
|
|
manager.Shutdown();
|
|
fs::remove_all(projectRoot);
|
|
}
|
|
|
|
} // namespace
|