Files
XCEngine/tests/RHI/integration/minimal/main.cpp

118 lines
3.7 KiB
C++
Raw Normal View History

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#include "../fixtures/RHIIntegrationFixture.h"
#include "XCEngine/Debug/Logger.h"
#include "XCEngine/Debug/ConsoleLogSink.h"
using namespace XCEngine::RHI;
using namespace XCEngine::RHI::Integration;
using namespace XCEngine::Debug;
namespace {
class MinimalTest : public RHIIntegrationFixture {
protected:
void RenderFrame() override;
};
const char* GetScreenshotFilename(RHIType type) {
2026-03-27 12:05:12 +08:00
switch (type) {
case RHIType::D3D12:
return "minimal_d3d12.ppm";
case RHIType::OpenGL:
return "minimal_opengl.ppm";
case RHIType::Vulkan:
return "minimal_vulkan.ppm";
default:
return "minimal_unknown.ppm";
}
}
int GetComparisonThreshold(RHIType type) {
return type == RHIType::OpenGL ? 5 : 0;
}
void MinimalTest::RenderFrame() {
RHICommandList* cmdList = GetCommandList();
RHICommandQueue* cmdQueue = GetCommandQueue();
Log("[TEST] RenderFrame: calling Reset");
cmdList->Reset();
Log("[TEST] RenderFrame: calling SetRenderTargetForClear");
SetRenderTargetForClear();
Viewport viewport = { 0.0f, 0.0f, 1280.0f, 720.0f, 0.0f, 1.0f };
Rect scissorRect = { 0, 0, 1280, 720 };
cmdList->SetViewport(viewport);
cmdList->SetScissorRect(scissorRect);
float clearColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
Log("[TEST] RenderFrame: calling Clear");
cmdList->Clear(clearColor[0], clearColor[1], clearColor[2], clearColor[3], 1);
Log("[TEST] RenderFrame: calling EndRender");
EndRender();
Log("[TEST] RenderFrame: calling Close");
cmdList->Close();
Log("[TEST] RenderFrame: calling ExecuteCommandLists");
void* cmdLists[] = { cmdList };
cmdQueue->ExecuteCommandLists(1, cmdLists);
Log("[TEST] RenderFrame: done");
}
TEST_P(MinimalTest, RenderClear) {
RHICommandQueue* cmdQueue = 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) {
cmdQueue->WaitForPreviousFrame();
}
Log("[TEST] MainLoop: frame %d", frameCount);
BeginRender();
RenderFrame();
if (frameCount >= targetFrameCount) {
cmdQueue->WaitForIdle();
Log("[TEST] MainLoop: frame %d reached, capturing %s", frameCount, screenshotFilename);
ASSERT_TRUE(TakeScreenshot(screenshotFilename));
ASSERT_TRUE(CompareWithGoldenTemplate(screenshotFilename, "GT.ppm", static_cast<float>(comparisonThreshold)));
Log("[TEST] MainLoop: frame %d compare passed", frameCount);
break;
}
Log("[TEST] MainLoop: calling Present, index before=%d", swapChain->GetCurrentBackBufferIndex());
swapChain->Present(0, 0);
Log("[TEST] MainLoop: Present done, index after=%d", swapChain->GetCurrentBackBufferIndex());
}
}
} // namespace
INSTANTIATE_TEST_SUITE_P(D3D12, MinimalTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, MinimalTest, ::testing::Values(RHIType::OpenGL));
2026-03-27 12:05:12 +08:00
#if defined(XCENGINE_SUPPORT_VULKAN)
INSTANTIATE_TEST_SUITE_P(Vulkan, MinimalTest, ::testing::Values(RHIType::Vulkan));
#endif
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();
}