Add cull material scene integration test

This commit is contained in:
2026-03-27 13:15:19 +08:00
parent b06932724c
commit 4b21b5d3d1
4 changed files with 504 additions and 0 deletions

View File

@@ -5,3 +5,4 @@ project(XCEngine_RenderingIntegrationTests)
add_subdirectory(textured_quad_scene) add_subdirectory(textured_quad_scene)
add_subdirectory(backpack_scene) add_subdirectory(backpack_scene)
add_subdirectory(transparent_material_scene) add_subdirectory(transparent_material_scene)
add_subdirectory(cull_material_scene)

View File

@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
project(rendering_integration_cull_material_scene)
set(ENGINE_ROOT_DIR ${CMAKE_SOURCE_DIR}/engine)
set(PACKAGE_DIR ${CMAKE_SOURCE_DIR}/tests/opengl/package)
get_filename_component(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. ABSOLUTE)
add_executable(rendering_integration_cull_material_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_cull_material_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_cull_material_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
target_compile_definitions(rendering_integration_cull_material_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_cull_material_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_cull_material_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_cull_material_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_cull_material_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_cull_material_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,443 @@
#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/Texture/Texture.h>
#include <XCEngine/RHI/RHITexture.h>
#include <XCEngine/Scene/Scene.h>
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
#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 = "cull_material_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "cull_material_scene_opengl.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
Mesh* CreateQuadMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "CullQuad";
params.path = "Tests/Rendering/CullQuad.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* CreateSolidTexture(const char* name, const char* path, const unsigned char rgba[4]) {
auto* texture = new Texture();
IResource::ConstructParams params = {};
params.name = name;
params.path = path;
params.guid = ResourceGUID::Generate(params.path);
texture->Initialize(params);
texture->Create(
1,
1,
1,
1,
XCEngine::Resources::TextureType::Texture2D,
XCEngine::Resources::TextureFormat::RGBA8_UNORM,
rgba,
4);
return texture;
}
Material* CreateMaterial(
const char* name,
const char* path,
Texture* texture,
const MaterialRenderState& renderState) {
auto* material = new Material();
IResource::ConstructParams params = {};
params.name = name;
params.path = path;
params.guid = ResourceGUID::Generate(params.path);
material->Initialize(params);
material->SetTexture("_BaseColorTexture", ResourceHandle<Texture>(texture));
material->SetRenderQueue(MaterialRenderQueue::Geometry);
material->SetRenderState(renderState);
return material;
}
GameObject* CreateQuadObject(
Scene& scene,
const char* name,
Mesh* mesh,
Material* material,
const Vector3& position,
const Vector3& scale,
const Quaternion& rotation) {
GameObject* gameObject = scene.CreateGameObject(name);
gameObject->GetTransform()->SetLocalPosition(position);
gameObject->GetTransform()->SetLocalScale(scale);
gameObject->GetTransform()->SetLocalRotation(rotation);
auto* meshFilter = gameObject->AddComponent<MeshFilterComponent>();
auto* meshRenderer = gameObject->AddComponent<MeshRendererComponent>();
meshFilter->SetMesh(ResourceHandle<Mesh>(mesh));
meshRenderer->SetMaterial(0, ResourceHandle<Material>(material));
return gameObject;
}
const char* GetScreenshotFilename(RHIType backendType) {
return backendType == RHIType::D3D12 ? kD3D12Screenshot : kOpenGLScreenshot;
}
int GetComparisonThreshold(RHIType backendType) {
(void)backendType;
return 0;
}
class CullMaterialSceneTest : public RHIIntegrationFixture {
protected:
void SetUp() override;
void TearDown() override;
void RenderFrame() override;
private:
void BuildScene();
RHIResourceView* GetCurrentBackBufferView();
std::unique_ptr<Scene> mScene;
std::unique_ptr<SceneRenderer> mSceneRenderer;
std::vector<RHIResourceView*> mBackBufferViews;
std::vector<Texture*> mTextures;
std::vector<Material*> mMaterials;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
Mesh* mMesh = nullptr;
};
void CullMaterialSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mSceneRenderer = std::make_unique<SceneRenderer>();
mScene = std::make_unique<Scene>("CullMaterialScene");
mMesh = CreateQuadMesh();
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 CullMaterialSceneTest::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();
for (Material* material : mMaterials) {
delete material;
}
mMaterials.clear();
for (Texture* texture : mTextures) {
delete texture;
}
mTextures.clear();
delete mMesh;
mMesh = nullptr;
RHIIntegrationFixture::TearDown();
}
void CullMaterialSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mMesh, nullptr);
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
auto* camera = cameraObject->AddComponent<CameraComponent>();
camera->SetPrimary(true);
camera->SetProjectionType(CameraProjectionType::Orthographic);
camera->SetOrthographicSize(3.0f);
camera->SetNearClipPlane(0.1f);
camera->SetFarClipPlane(10.0f);
camera->SetClearColor(XCEngine::Math::Color(0.08f, 0.09f, 0.12f, 1.0f));
const unsigned char greenPixel[4] = { 72, 214, 122, 255 };
const unsigned char redPixel[4] = { 230, 84, 84, 255 };
const unsigned char yellowPixel[4] = { 238, 204, 64, 255 };
const unsigned char bluePixel[4] = { 74, 148, 244, 255 };
Texture* backCullFrontFacingTexture = CreateSolidTexture(
"BackCullFrontFacingTexture",
"Tests/Rendering/CullScene/back_front.texture",
greenPixel);
Texture* backCullBackFacingTexture = CreateSolidTexture(
"BackCullBackFacingTexture",
"Tests/Rendering/CullScene/back_back.texture",
redPixel);
Texture* frontCullFrontFacingTexture = CreateSolidTexture(
"FrontCullFrontFacingTexture",
"Tests/Rendering/CullScene/front_front.texture",
yellowPixel);
Texture* frontCullBackFacingTexture = CreateSolidTexture(
"FrontCullBackFacingTexture",
"Tests/Rendering/CullScene/front_back.texture",
bluePixel);
mTextures = {
backCullFrontFacingTexture,
backCullBackFacingTexture,
frontCullFrontFacingTexture,
frontCullBackFacingTexture
};
MaterialRenderState backCullState = {};
backCullState.depthTestEnable = true;
backCullState.depthWriteEnable = true;
backCullState.depthFunc = MaterialComparisonFunc::LessEqual;
backCullState.cullMode = MaterialCullMode::Back;
MaterialRenderState frontCullState = backCullState;
frontCullState.cullMode = MaterialCullMode::Front;
Material* backCullFrontFacingMaterial = CreateMaterial(
"BackCullFrontFacingMaterial",
"Tests/Rendering/CullScene/back_front.mat",
backCullFrontFacingTexture,
backCullState);
Material* backCullBackFacingMaterial = CreateMaterial(
"BackCullBackFacingMaterial",
"Tests/Rendering/CullScene/back_back.mat",
backCullBackFacingTexture,
backCullState);
Material* frontCullFrontFacingMaterial = CreateMaterial(
"FrontCullFrontFacingMaterial",
"Tests/Rendering/CullScene/front_front.mat",
frontCullFrontFacingTexture,
frontCullState);
Material* frontCullBackFacingMaterial = CreateMaterial(
"FrontCullBackFacingMaterial",
"Tests/Rendering/CullScene/front_back.mat",
frontCullBackFacingTexture,
frontCullState);
mMaterials = {
backCullFrontFacingMaterial,
backCullBackFacingMaterial,
frontCullFrontFacingMaterial,
frontCullBackFacingMaterial
};
const Vector3 quadScale(1.15f, 1.15f, 1.0f);
const Quaternion frontFacing = Quaternion::Identity();
const Quaternion backFacing = Quaternion::FromAxisAngle(Vector3::Up(), 3.1415926535f);
CreateQuadObject(
*mScene,
"BackCullFrontFacing",
mMesh,
backCullFrontFacingMaterial,
Vector3(-2.1f, 1.25f, 3.0f),
quadScale,
frontFacing);
CreateQuadObject(
*mScene,
"BackCullBackFacing",
mMesh,
backCullBackFacingMaterial,
Vector3(2.1f, 1.25f, 3.0f),
quadScale,
backFacing);
CreateQuadObject(
*mScene,
"FrontCullFrontFacing",
mMesh,
frontCullFrontFacingMaterial,
Vector3(-2.1f, -1.25f, 3.0f),
quadScale,
frontFacing);
CreateQuadObject(
*mScene,
"FrontCullBackFacing",
mMesh,
frontCullBackFacingMaterial,
Vector3(2.1f, -1.25f, 3.0f),
quadScale,
backFacing);
}
RHIResourceView* CullMaterialSceneTest::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 CullMaterialSceneTest::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(CullMaterialSceneTest, RenderCullMaterialScene) {
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, CullMaterialSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, CullMaterialSceneTest, ::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();
}