2026-03-26 20:43:17 +08:00
|
|
|
#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/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>
|
2026-03-26 22:28:11 +08:00
|
|
|
#include <XCEngine/RHI/RHITexture.h>
|
2026-03-26 20:43:17 +08:00
|
|
|
#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 = "textured_quad_scene_d3d12.ppm";
|
|
|
|
|
constexpr const char* kOpenGLScreenshot = "textured_quad_scene_opengl.ppm";
|
2026-03-26 22:28:11 +08:00
|
|
|
constexpr uint32_t kFrameWidth = 1280;
|
|
|
|
|
constexpr uint32_t kFrameHeight = 720;
|
2026-03-26 20:43:17 +08:00
|
|
|
|
|
|
|
|
Mesh* CreateQuadMesh() {
|
|
|
|
|
auto* mesh = new Mesh();
|
|
|
|
|
IResource::ConstructParams params = {};
|
|
|
|
|
params.name = "TexturedQuad";
|
|
|
|
|
params.path = "Tests/Rendering/TexturedQuad.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 = "Checker";
|
|
|
|
|
params.path = "Tests/Rendering/Checker.texture";
|
|
|
|
|
params.guid = ResourceGUID::Generate(params.path);
|
|
|
|
|
texture->Initialize(params);
|
|
|
|
|
|
|
|
|
|
const unsigned char pixels[16] = {
|
|
|
|
|
255, 32, 32, 255,
|
|
|
|
|
32, 255, 32, 255,
|
|
|
|
|
32, 32, 255, 255,
|
|
|
|
|
255, 255, 32, 255
|
|
|
|
|
};
|
|
|
|
|
texture->Create(
|
|
|
|
|
2,
|
|
|
|
|
2,
|
|
|
|
|
1,
|
|
|
|
|
1,
|
|
|
|
|
XCEngine::Resources::TextureType::Texture2D,
|
|
|
|
|
XCEngine::Resources::TextureFormat::RGBA8_UNORM,
|
|
|
|
|
pixels,
|
|
|
|
|
sizeof(pixels));
|
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material* CreateMaterial(Texture* texture) {
|
|
|
|
|
auto* material = new Material();
|
|
|
|
|
IResource::ConstructParams params = {};
|
|
|
|
|
params.name = "QuadMaterial";
|
|
|
|
|
params.path = "Tests/Rendering/Quad.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 ? 5 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TexturedQuadSceneTest : public RHIIntegrationFixture {
|
|
|
|
|
protected:
|
|
|
|
|
void SetUp() override;
|
|
|
|
|
void TearDown() override;
|
|
|
|
|
void RenderFrame() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
RHIResourceView* GetCurrentBackBufferView();
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Scene> mScene;
|
|
|
|
|
std::unique_ptr<SceneRenderer> mSceneRenderer;
|
|
|
|
|
std::vector<RHIResourceView*> mBackBufferViews;
|
2026-03-26 22:28:11 +08:00
|
|
|
RHITexture* mDepthTexture = nullptr;
|
|
|
|
|
RHIResourceView* mDepthView = nullptr;
|
2026-03-26 20:43:17 +08:00
|
|
|
Mesh* mMesh = nullptr;
|
|
|
|
|
Material* mMaterial = nullptr;
|
|
|
|
|
Texture* mTexture = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void TexturedQuadSceneTest::SetUp() {
|
|
|
|
|
RHIIntegrationFixture::SetUp();
|
|
|
|
|
|
|
|
|
|
mSceneRenderer = std::make_unique<SceneRenderer>();
|
|
|
|
|
mScene = std::make_unique<Scene>("TexturedQuadScene");
|
|
|
|
|
|
|
|
|
|
mMesh = CreateQuadMesh();
|
|
|
|
|
mTexture = CreateCheckerTexture();
|
|
|
|
|
mMaterial = CreateMaterial(mTexture);
|
|
|
|
|
|
|
|
|
|
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
|
|
|
|
|
auto* camera = cameraObject->AddComponent<CameraComponent>();
|
|
|
|
|
camera->SetPrimary(true);
|
|
|
|
|
camera->SetClearColor(XCEngine::Math::Color(0.08f, 0.08f, 0.10f, 1.0f));
|
|
|
|
|
|
|
|
|
|
GameObject* quadObject = mScene->CreateGameObject("Quad");
|
|
|
|
|
quadObject->GetTransform()->SetLocalPosition(Vector3(0.0f, 0.0f, 3.0f));
|
|
|
|
|
auto* meshFilter = quadObject->AddComponent<MeshFilterComponent>();
|
|
|
|
|
auto* meshRenderer = quadObject->AddComponent<MeshRendererComponent>();
|
|
|
|
|
meshFilter->SetMesh(ResourceHandle<Mesh>(mMesh));
|
|
|
|
|
meshRenderer->SetMaterial(0, ResourceHandle<Material>(mMaterial));
|
|
|
|
|
|
2026-03-26 22:28:11 +08:00
|
|
|
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);
|
|
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
mBackBufferViews.resize(2, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TexturedQuadSceneTest::TearDown() {
|
|
|
|
|
mSceneRenderer.reset();
|
|
|
|
|
|
2026-03-26 22:28:11 +08:00
|
|
|
if (mDepthView != nullptr) {
|
|
|
|
|
mDepthView->Shutdown();
|
|
|
|
|
delete mDepthView;
|
|
|
|
|
mDepthView = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mDepthTexture != nullptr) {
|
|
|
|
|
mDepthTexture->Shutdown();
|
|
|
|
|
delete mDepthTexture;
|
|
|
|
|
mDepthTexture = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
for (RHIResourceView*& backBufferView : mBackBufferViews) {
|
|
|
|
|
if (backBufferView != nullptr) {
|
|
|
|
|
backBufferView->Shutdown();
|
|
|
|
|
delete backBufferView;
|
|
|
|
|
backBufferView = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mBackBufferViews.clear();
|
|
|
|
|
|
|
|
|
|
mScene.reset();
|
|
|
|
|
|
|
|
|
|
delete mMaterial;
|
|
|
|
|
mMaterial = nullptr;
|
|
|
|
|
delete mMesh;
|
|
|
|
|
mMesh = nullptr;
|
|
|
|
|
delete mTexture;
|
|
|
|
|
mTexture = nullptr;
|
|
|
|
|
|
|
|
|
|
RHIIntegrationFixture::TearDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHIResourceView* TexturedQuadSceneTest::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 TexturedQuadSceneTest::RenderFrame() {
|
|
|
|
|
ASSERT_NE(mScene, nullptr);
|
|
|
|
|
ASSERT_NE(mSceneRenderer, nullptr);
|
|
|
|
|
|
|
|
|
|
RHICommandList* commandList = GetCommandList();
|
|
|
|
|
ASSERT_NE(commandList, nullptr);
|
|
|
|
|
|
|
|
|
|
commandList->Reset();
|
|
|
|
|
|
2026-03-26 22:28:11 +08:00
|
|
|
RenderSurface surface(kFrameWidth, kFrameHeight);
|
2026-03-26 20:43:17 +08:00
|
|
|
surface.SetColorAttachment(GetCurrentBackBufferView());
|
2026-03-26 22:28:11 +08:00
|
|
|
surface.SetDepthAttachment(mDepthView);
|
2026-03-26 20:43:17 +08:00
|
|
|
|
|
|
|
|
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(TexturedQuadSceneTest, RenderTexturedQuadScene) {
|
|
|
|
|
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, TexturedQuadSceneTest, ::testing::Values(RHIType::D3D12));
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(OpenGL, TexturedQuadSceneTest, ::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();
|
|
|
|
|
}
|