Files
XCEngine/tests/editor/test_scene_viewport_shader_paths.cpp

114 lines
4.8 KiB
C++

#include <gtest/gtest.h>
#include "Viewport/SceneViewportResourcePaths.h"
#include "Viewport/SceneViewportShaderPaths.h"
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/BuiltinResources.h>
#include <XCEngine/Resources/Shader/ShaderLoader.h>
#include <filesystem>
#include <string>
namespace {
using XCEngine::Editor::GetSceneViewportCameraGizmoIconPath;
using XCEngine::Editor::GetSceneViewportDirectionalLightGizmoIconPath;
using XCEngine::Editor::GetSceneViewportInfiniteGridShaderPath;
using XCEngine::Editor::GetSceneViewportPointLightGizmoIconPath;
using XCEngine::Editor::GetSceneViewportSpotLightGizmoIconPath;
using XCEngine::Resources::GetBuiltinObjectIdOutlineShaderPath;
using XCEngine::Resources::LoadResult;
using XCEngine::Resources::ResourceHandle;
using XCEngine::Resources::ResourceManager;
using XCEngine::Resources::Shader;
using XCEngine::Resources::ShaderBackend;
using XCEngine::Resources::ShaderLoader;
using XCEngine::Resources::ShaderPass;
using XCEngine::Resources::ShaderStageVariant;
using XCEngine::Resources::ShaderType;
TEST(SceneViewportShaderPathsTest, ResolvePathsUnderEditorResources) {
const std::filesystem::path gridPath(GetSceneViewportInfiniteGridShaderPath().CStr());
const std::filesystem::path cameraIconPath(GetSceneViewportCameraGizmoIconPath().CStr());
const std::filesystem::path directionalLightIconPath(GetSceneViewportDirectionalLightGizmoIconPath().CStr());
const std::filesystem::path pointLightIconPath(GetSceneViewportPointLightGizmoIconPath().CStr());
const std::filesystem::path spotLightIconPath(GetSceneViewportSpotLightGizmoIconPath().CStr());
EXPECT_TRUE(gridPath.is_absolute());
EXPECT_TRUE(cameraIconPath.is_absolute());
EXPECT_TRUE(directionalLightIconPath.is_absolute());
EXPECT_TRUE(pointLightIconPath.is_absolute());
EXPECT_TRUE(spotLightIconPath.is_absolute());
EXPECT_TRUE(std::filesystem::exists(gridPath));
EXPECT_TRUE(std::filesystem::exists(cameraIconPath));
EXPECT_TRUE(std::filesystem::exists(directionalLightIconPath));
EXPECT_TRUE(std::filesystem::exists(pointLightIconPath));
EXPECT_TRUE(std::filesystem::exists(spotLightIconPath));
EXPECT_NE(gridPath.generic_string().find("editor/resources/shaders/scene-viewport"), std::string::npos);
EXPECT_NE(cameraIconPath.generic_string().find("editor/resources/Icons"), std::string::npos);
EXPECT_NE(directionalLightIconPath.generic_string().find("editor/resources/Icons"), std::string::npos);
EXPECT_NE(pointLightIconPath.generic_string().find("editor/resources/Icons"), std::string::npos);
EXPECT_NE(spotLightIconPath.generic_string().find("editor/resources/Icons"), std::string::npos);
EXPECT_EQ(directionalLightIconPath.filename().generic_string(), "directional_light_gizmo.png");
EXPECT_EQ(pointLightIconPath.filename().generic_string(), "point_light_gizmo.png");
EXPECT_EQ(spotLightIconPath.filename().generic_string(), "spot_light_gizmo.png");
}
TEST(SceneViewportShaderPathsTest, ShaderLoaderLoadsSceneViewportInfiniteGridShader) {
ShaderLoader loader;
const auto shaderPath = GetSceneViewportInfiniteGridShaderPath();
EXPECT_TRUE(loader.CanLoad(shaderPath));
LoadResult result = loader.Load(shaderPath);
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* shader = static_cast<Shader*>(result.resource);
ASSERT_NE(shader, nullptr);
ASSERT_TRUE(shader->IsValid());
const ShaderPass* pass = shader->FindPass("InfiniteGrid");
ASSERT_NE(pass, nullptr);
ASSERT_EQ(pass->variants.Size(), 2u);
ASSERT_EQ(pass->tags.Size(), 1u);
EXPECT_EQ(pass->tags[0].name, "LightMode");
EXPECT_EQ(pass->tags[0].value, "InfiniteGrid");
const ShaderStageVariant* fragment = shader->FindVariant(
"InfiniteGrid",
ShaderType::Fragment,
ShaderBackend::D3D12);
ASSERT_NE(fragment, nullptr);
EXPECT_NE(
std::string(fragment->sourceCode.CStr()).find("XC_EDITOR_SCENE_VIEW_INFINITE_GRID_D3D12_PS"),
std::string::npos);
delete shader;
}
TEST(SceneViewportShaderPathsTest, ResourceManagerLoadsSceneViewportOutlineShaderByResolvedPath) {
ResourceManager& manager = ResourceManager::Get();
manager.Shutdown();
const ResourceHandle<Shader> shaderHandle = manager.Load<Shader>(GetBuiltinObjectIdOutlineShaderPath());
ASSERT_TRUE(shaderHandle.IsValid());
const ShaderPass* pass = shaderHandle->FindPass("ObjectIdOutline");
ASSERT_NE(pass, nullptr);
ASSERT_EQ(pass->variants.Size(), 2u);
const ShaderStageVariant* fragment = shaderHandle->FindVariant(
"ObjectIdOutline",
ShaderType::Fragment,
ShaderBackend::D3D12);
ASSERT_NE(fragment, nullptr);
EXPECT_NE(
std::string(fragment->sourceCode.CStr()).find("XC_BUILTIN_OBJECT_ID_OUTLINE_D3D12_PS"),
std::string::npos);
manager.Shutdown();
}
} // namespace