test: add camera stack rendering integration

This commit is contained in:
2026-04-01 13:23:25 +08:00
parent a908e61ecb
commit d85b2ca056
4 changed files with 1909 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ project(XCEngine_RenderingIntegrationTests)
add_subdirectory(textured_quad_scene)
add_subdirectory(backpack_scene)
add_subdirectory(backpack_lit_scene)
add_subdirectory(camera_stack_scene)
add_subdirectory(transparent_material_scene)
add_subdirectory(cull_material_scene)
add_subdirectory(depth_sort_scene)

View File

@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
project(rendering_integration_camera_stack_scene)
set(ENGINE_ROOT_DIR ${CMAKE_SOURCE_DIR}/engine)
set(PACKAGE_DIR ${CMAKE_SOURCE_DIR}/mvs/OpenGL/package)
get_filename_component(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. ABSOLUTE)
add_executable(rendering_integration_camera_stack_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_camera_stack_scene PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures
${ENGINE_ROOT_DIR}/include
${PACKAGE_DIR}/include
${PROJECT_ROOT_DIR}/engine/src
)
target_link_libraries(rendering_integration_camera_stack_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
target_compile_definitions(rendering_integration_camera_stack_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_camera_stack_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/tests/RHI/integration/backpack/Res
$<TARGET_FILE_DIR:rendering_integration_camera_stack_scene>/Res
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_camera_stack_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_camera_stack_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_camera_stack_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/assimp/bin/assimp-vc143-mt.dll
$<TARGET_FILE_DIR:rendering_integration_camera_stack_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_camera_stack_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,434 @@
#define NOMINMAX
#include <windows.h>
#include <gtest/gtest.h>
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
#include <XCEngine/Core/Asset/IResource.h>
#include <XCEngine/Core/Math/Color.h>
#include <XCEngine/Core/Math/Quaternion.h>
#include <XCEngine/Core/Math/Vector2.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Debug/ConsoleLogSink.h>
#include <XCEngine/Debug/Logger.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Rendering/SceneRenderer.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Mesh/MeshImportSettings.h>
#include <XCEngine/Resources/Mesh/MeshLoader.h>
#include <XCEngine/Resources/Texture/Texture.h>
#include <XCEngine/RHI/RHITexture.h>
#include <XCEngine/Scene/Scene.h>
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
#include <algorithm>
#include <filesystem>
#include <memory>
#include <vector>
using namespace XCEngine::Components;
using namespace XCEngine::Debug;
using namespace XCEngine::Math;
using namespace XCEngine::Rendering;
using namespace XCEngine::Resources;
using namespace XCEngine::RHI;
using namespace XCEngine::RHI::Integration;
namespace {
constexpr const char* kD3D12Screenshot = "camera_stack_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "camera_stack_scene_opengl.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
constexpr uint8_t kBaseLayer = 0;
constexpr uint8_t kOverlayLayer = 1;
std::filesystem::path GetExecutableDirectory() {
char exePath[MAX_PATH] = {};
const DWORD length = GetModuleFileNameA(nullptr, exePath, MAX_PATH);
if (length == 0 || length >= MAX_PATH) {
return std::filesystem::current_path();
}
return std::filesystem::path(exePath).parent_path();
}
std::filesystem::path ResolveRuntimePath(const char* relativePath) {
return GetExecutableDirectory() / relativePath;
}
Mesh* CreateQuadMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "CameraStackQuad";
params.path = "Tests/Rendering/CameraStackQuad.mesh";
params.guid = ResourceGUID::Generate(params.path);
mesh->Initialize(params);
StaticMeshVertex vertices[4] = {};
vertices[0].position = Vector3(-1.0f, -1.0f, 0.0f);
vertices[0].uv0 = Vector2(0.0f, 1.0f);
vertices[1].position = Vector3(-1.0f, 1.0f, 0.0f);
vertices[1].uv0 = Vector2(0.0f, 0.0f);
vertices[2].position = Vector3(1.0f, -1.0f, 0.0f);
vertices[2].uv0 = Vector2(1.0f, 1.0f);
vertices[3].position = Vector3(1.0f, 1.0f, 0.0f);
vertices[3].uv0 = Vector2(1.0f, 0.0f);
const uint32_t indices[6] = { 0, 1, 2, 2, 1, 3 };
mesh->SetVertexData(
vertices,
sizeof(vertices),
4,
sizeof(StaticMeshVertex),
VertexAttribute::Position | VertexAttribute::UV0);
mesh->SetIndexData(indices, sizeof(indices), 6, true);
MeshSection section = {};
section.baseVertex = 0;
section.vertexCount = 4;
section.startIndex = 0;
section.indexCount = 6;
section.materialID = 0;
mesh->AddSection(section);
return mesh;
}
Texture* CreateCheckerTexture() {
auto* texture = new Texture();
IResource::ConstructParams params = {};
params.name = "CameraStackChecker";
params.path = "Tests/Rendering/CameraStackChecker.texture";
params.guid = ResourceGUID::Generate(params.path);
texture->Initialize(params);
const unsigned char pixels[16] = {
250, 120, 32, 255,
24, 36, 220, 255,
32, 200, 120, 255,
250, 240, 80, 255
};
texture->Create(
2,
2,
1,
1,
XCEngine::Resources::TextureType::Texture2D,
XCEngine::Resources::TextureFormat::RGBA8_UNORM,
pixels,
sizeof(pixels));
return texture;
}
Material* CreateQuadMaterial(Texture* texture) {
auto* material = new Material();
IResource::ConstructParams params = {};
params.name = "CameraStackQuadMaterial";
params.path = "Tests/Rendering/CameraStackQuad.material";
params.guid = ResourceGUID::Generate(params.path);
material->Initialize(params);
material->SetTexture("_BaseColorTexture", ResourceHandle<Texture>(texture));
return material;
}
const char* GetScreenshotFilename(RHIType backendType) {
return backendType == RHIType::D3D12 ? kD3D12Screenshot : kOpenGLScreenshot;
}
int GetComparisonThreshold(RHIType backendType) {
return backendType == RHIType::OpenGL ? 8 : 8;
}
class CameraStackSceneTest : public RHIIntegrationFixture {
protected:
void SetUp() override;
void TearDown() override;
void RenderFrame() override;
private:
void LoadBackpackMesh();
void BuildScene();
RHIResourceView* GetCurrentBackBufferView();
std::unique_ptr<Scene> mScene;
std::unique_ptr<SceneRenderer> mSceneRenderer;
std::vector<RHIResourceView*> mBackBufferViews;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
Mesh* mBackpackMesh = nullptr;
Mesh* mQuadMesh = nullptr;
Material* mQuadMaterial = nullptr;
Texture* mQuadTexture = nullptr;
};
void CameraStackSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mSceneRenderer = std::make_unique<SceneRenderer>();
mScene = std::make_unique<Scene>("CameraStackScene");
LoadBackpackMesh();
mQuadMesh = CreateQuadMesh();
mQuadTexture = CreateCheckerTexture();
mQuadMaterial = CreateQuadMaterial(mQuadTexture);
BuildScene();
TextureDesc depthDesc = {};
depthDesc.width = kFrameWidth;
depthDesc.height = kFrameHeight;
depthDesc.depth = 1;
depthDesc.mipLevels = 1;
depthDesc.arraySize = 1;
depthDesc.format = static_cast<uint32_t>(Format::D24_UNorm_S8_UInt);
depthDesc.textureType = static_cast<uint32_t>(XCEngine::RHI::TextureType::Texture2D);
depthDesc.sampleCount = 1;
depthDesc.sampleQuality = 0;
depthDesc.flags = 0;
mDepthTexture = GetDevice()->CreateTexture(depthDesc);
ASSERT_NE(mDepthTexture, nullptr);
ResourceViewDesc depthViewDesc = {};
depthViewDesc.format = static_cast<uint32_t>(Format::D24_UNorm_S8_UInt);
depthViewDesc.dimension = ResourceViewDimension::Texture2D;
depthViewDesc.mipLevel = 0;
mDepthView = GetDevice()->CreateDepthStencilView(mDepthTexture, depthViewDesc);
ASSERT_NE(mDepthView, nullptr);
mBackBufferViews.resize(2, nullptr);
}
void CameraStackSceneTest::TearDown() {
mSceneRenderer.reset();
if (mDepthView != nullptr) {
mDepthView->Shutdown();
delete mDepthView;
mDepthView = nullptr;
}
if (mDepthTexture != nullptr) {
mDepthTexture->Shutdown();
delete mDepthTexture;
mDepthTexture = nullptr;
}
for (RHIResourceView*& backBufferView : mBackBufferViews) {
if (backBufferView != nullptr) {
backBufferView->Shutdown();
delete backBufferView;
backBufferView = nullptr;
}
}
mBackBufferViews.clear();
mScene.reset();
delete mQuadMaterial;
mQuadMaterial = nullptr;
delete mQuadTexture;
mQuadTexture = nullptr;
delete mQuadMesh;
mQuadMesh = nullptr;
if (mBackpackMesh != nullptr) {
for (size_t materialIndex = 0; materialIndex < mBackpackMesh->GetMaterials().Size(); ++materialIndex) {
delete mBackpackMesh->GetMaterials()[materialIndex];
}
for (size_t textureIndex = 0; textureIndex < mBackpackMesh->GetTextures().Size(); ++textureIndex) {
delete mBackpackMesh->GetTextures()[textureIndex];
}
delete mBackpackMesh;
mBackpackMesh = nullptr;
}
RHIIntegrationFixture::TearDown();
}
void CameraStackSceneTest::LoadBackpackMesh() {
const std::filesystem::path meshPath = ResolveRuntimePath("Res/models/backpack/backpack.obj");
MeshLoader loader;
MeshImportSettings settings;
LoadResult result = loader.Load(meshPath.string().c_str(), &settings);
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
mBackpackMesh = static_cast<Mesh*>(result.resource);
ASSERT_NE(mBackpackMesh, nullptr);
ASSERT_TRUE(mBackpackMesh->IsValid());
ASSERT_GT(mBackpackMesh->GetVertexCount(), 0u);
ASSERT_GT(mBackpackMesh->GetSections().Size(), 0u);
}
void CameraStackSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mBackpackMesh, nullptr);
ASSERT_NE(mQuadMesh, nullptr);
ASSERT_NE(mQuadMaterial, nullptr);
GameObject* baseCameraObject = mScene->CreateGameObject("BaseCamera");
auto* baseCamera = baseCameraObject->AddComponent<CameraComponent>();
baseCamera->SetPrimary(true);
baseCamera->SetDepth(0.0f);
baseCamera->SetFieldOfView(45.0f);
baseCamera->SetNearClipPlane(0.1f);
baseCamera->SetFarClipPlane(100.0f);
baseCamera->SetStackType(CameraStackType::Base);
baseCamera->SetCullingMask(1u << kBaseLayer);
baseCamera->SetClearColor(XCEngine::Math::Color(0.03f, 0.05f, 0.09f, 1.0f));
GameObject* overlayCameraObject = mScene->CreateGameObject("OverlayCamera");
auto* overlayCamera = overlayCameraObject->AddComponent<CameraComponent>();
overlayCamera->SetPrimary(false);
overlayCamera->SetDepth(1.0f);
overlayCamera->SetProjectionType(CameraProjectionType::Orthographic);
overlayCamera->SetOrthographicSize(1.0f);
overlayCamera->SetNearClipPlane(0.1f);
overlayCamera->SetFarClipPlane(10.0f);
overlayCamera->SetStackType(CameraStackType::Overlay);
overlayCamera->SetCullingMask(1u << kOverlayLayer);
const Vector3 boundsMin = mBackpackMesh->GetBounds().GetMin();
const Vector3 boundsMax = mBackpackMesh->GetBounds().GetMax();
const Vector3 center(
(boundsMin.x + boundsMax.x) * 0.5f,
(boundsMin.y + boundsMax.y) * 0.5f,
(boundsMin.z + boundsMax.z) * 0.5f);
const float sizeX = std::max(boundsMax.x - boundsMin.x, 0.001f);
const float sizeY = std::max(boundsMax.y - boundsMin.y, 0.001f);
const float sizeZ = std::max(boundsMax.z - boundsMin.z, 0.001f);
const float maxExtent = std::max(sizeX, std::max(sizeY, sizeZ));
const float uniformScale = 1.8f / maxExtent;
GameObject* root = mScene->CreateGameObject("BackpackRoot");
root->GetTransform()->SetLocalPosition(Vector3(0.0f, 0.08f, 3.0f));
GameObject* rotateY = mScene->CreateGameObject("BackpackRotateY");
rotateY->SetParent(root, false);
rotateY->GetTransform()->SetLocalRotation(Quaternion::FromAxisAngle(Vector3::Up(), -0.35f));
GameObject* rotateX = mScene->CreateGameObject("BackpackRotateX");
rotateX->SetParent(rotateY, false);
rotateX->GetTransform()->SetLocalRotation(Quaternion::FromAxisAngle(Vector3::Right(), -0.18f));
GameObject* scale = mScene->CreateGameObject("BackpackScale");
scale->SetParent(rotateX, false);
scale->GetTransform()->SetLocalScale(Vector3(uniformScale, uniformScale, uniformScale));
GameObject* backpackObject = mScene->CreateGameObject("BackpackMesh");
backpackObject->SetParent(scale, false);
backpackObject->SetLayer(kBaseLayer);
backpackObject->GetTransform()->SetLocalPosition(Vector3(-center.x, -center.y, -center.z));
auto* backpackMeshFilter = backpackObject->AddComponent<MeshFilterComponent>();
auto* backpackMeshRenderer = backpackObject->AddComponent<MeshRendererComponent>();
backpackMeshFilter->SetMesh(ResourceHandle<Mesh>(mBackpackMesh));
backpackMeshRenderer->ClearMaterials();
GameObject* overlayQuadObject = mScene->CreateGameObject("OverlayQuad");
overlayQuadObject->SetLayer(kOverlayLayer);
overlayQuadObject->GetTransform()->SetLocalPosition(Vector3(1.0f, 0.58f, 2.0f));
overlayQuadObject->GetTransform()->SetLocalScale(Vector3(0.35f, 0.35f, 1.0f));
auto* overlayMeshFilter = overlayQuadObject->AddComponent<MeshFilterComponent>();
auto* overlayMeshRenderer = overlayQuadObject->AddComponent<MeshRendererComponent>();
overlayMeshFilter->SetMesh(ResourceHandle<Mesh>(mQuadMesh));
overlayMeshRenderer->SetMaterial(0, ResourceHandle<Material>(mQuadMaterial));
}
RHIResourceView* CameraStackSceneTest::GetCurrentBackBufferView() {
const int backBufferIndex = GetCurrentBackBufferIndex();
if (backBufferIndex < 0) {
return nullptr;
}
if (static_cast<size_t>(backBufferIndex) >= mBackBufferViews.size()) {
mBackBufferViews.resize(static_cast<size_t>(backBufferIndex) + 1, nullptr);
}
if (mBackBufferViews[backBufferIndex] == nullptr) {
ResourceViewDesc viewDesc = {};
viewDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
viewDesc.dimension = ResourceViewDimension::Texture2D;
viewDesc.mipLevel = 0;
mBackBufferViews[backBufferIndex] = GetDevice()->CreateRenderTargetView(GetCurrentBackBuffer(), viewDesc);
}
return mBackBufferViews[backBufferIndex];
}
void CameraStackSceneTest::RenderFrame() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mSceneRenderer, nullptr);
RHICommandList* commandList = GetCommandList();
ASSERT_NE(commandList, nullptr);
commandList->Reset();
RenderSurface surface(kFrameWidth, kFrameHeight);
surface.SetColorAttachment(GetCurrentBackBufferView());
surface.SetDepthAttachment(mDepthView);
RenderContext renderContext = {};
renderContext.device = GetDevice();
renderContext.commandList = commandList;
renderContext.commandQueue = GetCommandQueue();
renderContext.backendType = GetBackendType();
ASSERT_TRUE(mSceneRenderer->Render(*mScene, nullptr, renderContext, surface));
commandList->Close();
void* commandLists[] = { commandList };
GetCommandQueue()->ExecuteCommandLists(1, commandLists);
}
TEST_P(CameraStackSceneTest, RenderCameraStackScene) {
RHICommandQueue* commandQueue = GetCommandQueue();
RHISwapChain* swapChain = GetSwapChain();
const int targetFrameCount = 30;
const char* screenshotFilename = GetScreenshotFilename(GetBackendType());
const int comparisonThreshold = GetComparisonThreshold(GetBackendType());
for (int frameCount = 0; frameCount <= targetFrameCount; ++frameCount) {
if (frameCount > 0) {
commandQueue->WaitForPreviousFrame();
}
BeginRender();
RenderFrame();
if (frameCount >= targetFrameCount) {
commandQueue->WaitForIdle();
ASSERT_TRUE(TakeScreenshot(screenshotFilename));
ASSERT_TRUE(CompareWithGoldenTemplate(
screenshotFilename,
"GT.ppm",
static_cast<float>(comparisonThreshold)));
break;
}
swapChain->Present(0, 0);
}
}
} // namespace
INSTANTIATE_TEST_SUITE_P(D3D12, CameraStackSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, CameraStackSceneTest, ::testing::Values(RHIType::OpenGL));
GTEST_API_ int main(int argc, char** argv) {
Logger::Get().Initialize();
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
Logger::Get().SetMinimumLevel(LogLevel::Debug);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}