Add final color scene integration coverage

This commit is contained in:
2026-04-06 16:31:54 +08:00
parent 2d030a97da
commit 2ec24666c0
6 changed files with 459 additions and 1 deletions

View File

@@ -51,6 +51,7 @@ bool D3D12Shader::Compile(const void* sourceData, size_t sourceSize, const char*
if (m_error) {
const char* errorMsg = static_cast<const char*>(m_error->GetBufferPointer());
OutputDebugStringA(errorMsg);
fprintf(stderr, "[SHADER ERROR] %s\n", errorMsg);
}
return false;
}
@@ -180,4 +181,4 @@ ShaderType D3D12Shader::GetType() const {
}
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -118,21 +118,33 @@ const char* BuiltinFinalColorPass::GetName() const {
bool BuiltinFinalColorPass::Execute(const RenderPassContext& context) {
if (!context.renderContext.IsValid() || context.sourceColorView == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass requires a valid render context and source color view");
return false;
}
const std::vector<RHI::RHIResourceView*>& colorAttachments = context.surface.GetColorAttachments();
if (colorAttachments.empty() || colorAttachments[0] == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass requires a valid destination color attachment");
return false;
}
const Math::RectInt renderArea = context.surface.GetRenderArea();
if (renderArea.width <= 0 || renderArea.height <= 0) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass received an invalid render area");
return false;
}
const RHI::Format renderTargetFormat = colorAttachments[0]->GetFormat();
if (!EnsureInitialized(context.renderContext, renderTargetFormat)) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass failed to initialize GPU resources");
return false;
}
@@ -249,6 +261,9 @@ bool BuiltinFinalColorPass::CreateResources(
const RenderContext& renderContext,
RHI::Format renderTargetFormat) {
if (!renderContext.IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass received an invalid render context during resource creation");
return false;
}
@@ -323,6 +338,9 @@ bool BuiltinFinalColorPass::CreateResources(
pipelineLayoutDesc.setLayoutCount = 3;
m_pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc);
if (m_pipelineLayout == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass failed to create pipeline layout");
DestroyResources();
return false;
}
@@ -339,6 +357,9 @@ bool BuiltinFinalColorPass::CreateResources(
samplerDesc.maxLod = 1000.0f;
m_sampler = m_device->CreateSampler(samplerDesc);
if (m_sampler == nullptr) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass failed to create linear clamp sampler");
DestroyResources();
return false;
}
@@ -381,6 +402,9 @@ bool BuiltinFinalColorPass::CreateResources(
RHI::DescriptorHeapType::Sampler,
true,
m_samplerSet)) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass failed to allocate descriptor sets");
DestroyResources();
return false;
}
@@ -395,6 +419,9 @@ bool BuiltinFinalColorPass::CreateResources(
finalColorPass->name,
m_renderTargetFormat));
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
Debug::Logger::Get().Error(
Debug::LogCategory::Rendering,
"BuiltinFinalColorPass failed to create a valid graphics pipeline state");
DestroyResources();
return false;
}

View File

@@ -19,3 +19,4 @@ add_subdirectory(offscreen_scene)
add_subdirectory(skybox_scene)
add_subdirectory(post_process_scene)
add_subdirectory(camera_post_process_scene camera_post_process_scene_builtin)
add_subdirectory(final_color_scene)

View File

@@ -0,0 +1,63 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
project(rendering_integration_final_color_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)
find_package(Vulkan QUIET)
add_executable(rendering_integration_final_color_scene
main.cpp
${CMAKE_SOURCE_DIR}/tests/RHI/integration/fixtures/RHIIntegrationFixture.cpp
${PACKAGE_DIR}/src/glad.c
)
target_include_directories(rendering_integration_final_color_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_final_color_scene PRIVATE
d3d12
dxgi
d3dcompiler
winmm
opengl32
XCEngine
GTest::gtest
)
if(TARGET Vulkan::Vulkan)
target_link_libraries(rendering_integration_final_color_scene PRIVATE Vulkan::Vulkan)
target_compile_definitions(rendering_integration_final_color_scene PRIVATE XCENGINE_SUPPORT_VULKAN)
endif()
target_compile_definitions(rendering_integration_final_color_scene PRIVATE
UNICODE
_UNICODE
XCENGINE_SUPPORT_OPENGL
XCENGINE_SUPPORT_D3D12
)
add_custom_command(TARGET rendering_integration_final_color_scene POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/integration/compare_ppm.py
$<TARGET_FILE_DIR:rendering_integration_final_color_scene>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
$<TARGET_FILE_DIR:rendering_integration_final_color_scene>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rendering_integration_final_color_scene>/
)
include(GoogleTest)
gtest_discover_tests(rendering_integration_final_color_scene)

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,362 @@
#include <gtest/gtest.h>
#include "../RenderingIntegrationMain.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/Core/Math/Vector4.h>
#include <XCEngine/Rendering/Execution/SceneRenderer.h>
#include <XCEngine/Rendering/Planning/FinalColorSettings.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Resources/BuiltinResources.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Shader/Shader.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::Math;
using namespace XCEngine::Rendering;
using namespace XCEngine::Resources;
using namespace XCEngine::RHI;
using namespace XCEngine::RHI::Integration;
namespace {
constexpr const char* kD3D12Screenshot = "final_color_scene_d3d12.ppm";
constexpr const char* kOpenGLScreenshot = "final_color_scene_opengl.ppm";
constexpr const char* kVulkanScreenshot = "final_color_scene_vulkan.ppm";
constexpr uint32_t kFrameWidth = 1280;
constexpr uint32_t kFrameHeight = 720;
Mesh* CreateQuadMesh() {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = "FinalColorQuad";
params.path = "Tests/Rendering/FinalColorQuad.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 = "FinalColorChecker";
params.path = "Tests/Rendering/FinalColorChecker.texture";
params.guid = ResourceGUID::Generate(params.path);
texture->Initialize(params);
const unsigned char pixels[16] = {
255, 96, 64, 255,
64, 255, 96, 255,
48, 96, 255, 255,
255, 240, 96, 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 = "FinalColorQuadMaterial";
params.path = "Tests/Rendering/FinalColorQuad.material";
params.guid = ResourceGUID::Generate(params.path);
material->Initialize(params);
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinUnlitShaderPath()));
material->SetShaderPass("Unlit");
material->SetTexture("_MainTex", ResourceHandle<Texture>(texture));
return material;
}
const char* GetScreenshotFilename(RHIType backendType) {
switch (backendType) {
case RHIType::D3D12:
return kD3D12Screenshot;
case RHIType::Vulkan:
return kVulkanScreenshot;
case RHIType::OpenGL:
default:
return kOpenGLScreenshot;
}
}
int GetComparisonThreshold(RHIType backendType) {
return backendType == RHIType::D3D12 ? 1 : 6;
}
class FinalColorSceneTest : 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;
RHITexture* mDepthTexture = nullptr;
RHIResourceView* mDepthView = nullptr;
Mesh* mMesh = nullptr;
Material* mMaterial = nullptr;
Texture* mTexture = nullptr;
CameraComponent* mCamera = nullptr;
};
void FinalColorSceneTest::SetUp() {
RHIIntegrationFixture::SetUp();
mSceneRenderer = std::make_unique<SceneRenderer>();
mScene = std::make_unique<Scene>("FinalColorScene");
mMesh = CreateQuadMesh();
mTexture = CreateCheckerTexture();
mMaterial = CreateQuadMaterial(mTexture);
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 FinalColorSceneTest::TearDown() {
if (GetCommandQueue() != nullptr) {
GetCommandQueue()->WaitForIdle();
}
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 mMaterial;
mMaterial = nullptr;
delete mTexture;
mTexture = nullptr;
delete mMesh;
mMesh = nullptr;
mCamera = nullptr;
RHIIntegrationFixture::TearDown();
}
void FinalColorSceneTest::BuildScene() {
ASSERT_NE(mScene, nullptr);
GameObject* cameraObject = mScene->CreateGameObject("MainCamera");
mCamera = cameraObject->AddComponent<CameraComponent>();
ASSERT_NE(mCamera, nullptr);
mCamera->SetPrimary(true);
mCamera->SetFieldOfView(50.0f);
mCamera->SetNearClipPlane(0.1f);
mCamera->SetFarClipPlane(20.0f);
mCamera->SetClearColor(XCEngine::Math::Color(0.06f, 0.08f, 0.12f, 1.0f));
FinalColorOverrideSettings finalColorOverrides = {};
finalColorOverrides.overrideOutputTransferMode = true;
finalColorOverrides.outputTransferMode = FinalColorOutputTransferMode::LinearToSRGB;
finalColorOverrides.overrideExposureMode = true;
finalColorOverrides.exposureMode = FinalColorExposureMode::Fixed;
finalColorOverrides.overrideExposureValue = true;
finalColorOverrides.exposureValue = 1.15f;
finalColorOverrides.overrideFinalColorScale = true;
finalColorOverrides.finalColorScale = Vector4(0.92f, 0.88f, 0.82f, 1.0f);
mCamera->SetFinalColorOverrides(finalColorOverrides);
GameObject* quadObject = mScene->CreateGameObject("Quad");
quadObject->GetTransform()->SetLocalPosition(Vector3(0.0f, 0.0f, 3.0f));
quadObject->GetTransform()->SetLocalScale(Vector3(1.6f, 1.6f, 1.0f));
auto* meshFilter = quadObject->AddComponent<MeshFilterComponent>();
auto* meshRenderer = quadObject->AddComponent<MeshRendererComponent>();
meshFilter->SetMesh(ResourceHandle<Mesh>(mMesh));
meshRenderer->SetMaterial(0, ResourceHandle<Material>(mMaterial));
}
RHIResourceView* FinalColorSceneTest::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 FinalColorSceneTest::RenderFrame() {
ASSERT_NE(mScene, nullptr);
ASSERT_NE(mSceneRenderer, nullptr);
ASSERT_NE(mCamera, nullptr);
ASSERT_NE(mDepthView, nullptr);
RHICommandList* commandList = GetCommandList();
ASSERT_NE(commandList, nullptr);
RHIResourceView* backBufferView = GetCurrentBackBufferView();
ASSERT_NE(backBufferView, nullptr);
commandList->Reset();
RenderSurface surface(kFrameWidth, kFrameHeight);
surface.SetColorAttachment(backBufferView);
surface.SetDepthAttachment(mDepthView);
surface.SetColorStateBefore(ResourceStates::Present);
surface.SetColorStateAfter(ResourceStates::Present);
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(FinalColorSceneTest, RenderFinalColorScene) {
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, FinalColorSceneTest, ::testing::Values(RHIType::D3D12));
INSTANTIATE_TEST_SUITE_P(OpenGL, FinalColorSceneTest, ::testing::Values(RHIType::OpenGL));
#if defined(XCENGINE_SUPPORT_VULKAN)
INSTANTIATE_TEST_SUITE_P(Vulkan, FinalColorSceneTest, ::testing::Values(RHIType::Vulkan));
#endif
GTEST_API_ int main(int argc, char** argv) {
return RunRenderingIntegrationTestMain(argc, argv);
}