#include #include "../RenderingIntegrationMain.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h" #include #include 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 = "post_process_scene_d3d12.ppm"; constexpr const char* kOpenGLScreenshot = "post_process_scene_opengl.ppm"; constexpr const char* kVulkanScreenshot = "post_process_scene_vulkan.ppm"; constexpr uint32_t kFrameWidth = 1280; constexpr uint32_t kFrameHeight = 720; Mesh* CreateQuadMesh() { auto* mesh = new Mesh(); IResource::ConstructParams params = {}; params.name = "PostProcessQuad"; params.path = "Tests/Rendering/PostProcessQuad.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, 2, 1, 2, 3, 1 }; 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 = "PostProcessChecker"; params.path = "Tests/Rendering/PostProcessChecker.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 = "PostProcessQuadMaterial"; params.path = "Tests/Rendering/PostProcessQuad.material"; params.guid = ResourceGUID::Generate(params.path); material->Initialize(params); material->SetShader(ResourceManager::Get().Load(GetBuiltinUnlitShaderPath())); material->SetTexture("_MainTex", ResourceHandle(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 ? 0 : 6; } class PostProcessSceneTest : public RHIIntegrationFixture { protected: void SetUp() override; void TearDown() override; void RenderFrame() override; private: void BuildScene(); RHIResourceView* GetCurrentBackBufferView(); std::unique_ptr mScene; std::unique_ptr mCameraRenderer; RenderPassSequence mPostProcessPasses; std::vector mBackBufferViews; RHITexture* mSceneColorTexture = nullptr; RHIResourceView* mSceneColorTargetView = nullptr; RHIResourceView* mSceneColorShaderView = nullptr; RHITexture* mDepthTexture = nullptr; RHIResourceView* mDepthView = nullptr; Mesh* mMesh = nullptr; Material* mMaterial = nullptr; Texture* mTexture = nullptr; CameraComponent* mCamera = nullptr; }; void PostProcessSceneTest::SetUp() { RHIIntegrationFixture::SetUp(); mCameraRenderer = std::make_unique(); mScene = std::make_unique("PostProcessScene"); mMesh = CreateQuadMesh(); mTexture = CreateCheckerTexture(); mMaterial = CreateQuadMaterial(mTexture); mPostProcessPasses.AddPass(std::make_unique( Vector4(1.0f, 0.75f, 0.75f, 1.0f))); mPostProcessPasses.AddPass(std::make_unique( Vector4(0.55f, 0.95f, 1.0f, 1.0f))); BuildScene(); TextureDesc sceneColorDesc = {}; sceneColorDesc.width = kFrameWidth; sceneColorDesc.height = kFrameHeight; sceneColorDesc.depth = 1; sceneColorDesc.mipLevels = 1; sceneColorDesc.arraySize = 1; sceneColorDesc.format = static_cast(Format::R8G8B8A8_UNorm); sceneColorDesc.textureType = static_cast(XCEngine::RHI::TextureType::Texture2D); sceneColorDesc.sampleCount = 1; sceneColorDesc.sampleQuality = 0; sceneColorDesc.flags = 0; mSceneColorTexture = GetDevice()->CreateTexture(sceneColorDesc); ASSERT_NE(mSceneColorTexture, nullptr); ResourceViewDesc sceneColorViewDesc = {}; sceneColorViewDesc.format = static_cast(Format::R8G8B8A8_UNorm); sceneColorViewDesc.dimension = ResourceViewDimension::Texture2D; sceneColorViewDesc.mipLevel = 0; mSceneColorTargetView = GetDevice()->CreateRenderTargetView(mSceneColorTexture, sceneColorViewDesc); ASSERT_NE(mSceneColorTargetView, nullptr); mSceneColorShaderView = GetDevice()->CreateShaderResourceView(mSceneColorTexture, sceneColorViewDesc); ASSERT_NE(mSceneColorShaderView, nullptr); TextureDesc depthDesc = {}; depthDesc.width = kFrameWidth; depthDesc.height = kFrameHeight; depthDesc.depth = 1; depthDesc.mipLevels = 1; depthDesc.arraySize = 1; depthDesc.format = static_cast(Format::D24_UNorm_S8_UInt); depthDesc.textureType = static_cast(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(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 PostProcessSceneTest::TearDown() { if (GetCommandQueue() != nullptr) { GetCommandQueue()->WaitForIdle(); } mPostProcessPasses.Shutdown(); mCameraRenderer.reset(); if (mDepthView != nullptr) { mDepthView->Shutdown(); delete mDepthView; mDepthView = nullptr; } if (mDepthTexture != nullptr) { mDepthTexture->Shutdown(); delete mDepthTexture; mDepthTexture = nullptr; } if (mSceneColorShaderView != nullptr) { mSceneColorShaderView->Shutdown(); delete mSceneColorShaderView; mSceneColorShaderView = nullptr; } if (mSceneColorTargetView != nullptr) { mSceneColorTargetView->Shutdown(); delete mSceneColorTargetView; mSceneColorTargetView = nullptr; } if (mSceneColorTexture != nullptr) { mSceneColorTexture->Shutdown(); delete mSceneColorTexture; mSceneColorTexture = 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 PostProcessSceneTest::BuildScene() { ASSERT_NE(mScene, nullptr); GameObject* cameraObject = mScene->CreateGameObject("MainCamera"); mCamera = cameraObject->AddComponent(); 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)); 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(); auto* meshRenderer = quadObject->AddComponent(); meshFilter->SetMesh(ResourceHandle(mMesh)); meshRenderer->SetMaterial(0, ResourceHandle(mMaterial)); } RHIResourceView* PostProcessSceneTest::GetCurrentBackBufferView() { const int backBufferIndex = GetCurrentBackBufferIndex(); if (backBufferIndex < 0) { return nullptr; } if (static_cast(backBufferIndex) >= mBackBufferViews.size()) { mBackBufferViews.resize(static_cast(backBufferIndex) + 1, nullptr); } if (mBackBufferViews[backBufferIndex] == nullptr) { ResourceViewDesc viewDesc = {}; viewDesc.format = static_cast(Format::R8G8B8A8_UNorm); viewDesc.dimension = ResourceViewDimension::Texture2D; viewDesc.mipLevel = 0; mBackBufferViews[backBufferIndex] = GetDevice()->CreateRenderTargetView(GetCurrentBackBuffer(), viewDesc); } return mBackBufferViews[backBufferIndex]; } void PostProcessSceneTest::RenderFrame() { ASSERT_NE(mScene, nullptr); ASSERT_NE(mCameraRenderer, nullptr); ASSERT_NE(mCamera, nullptr); ASSERT_NE(mSceneColorTargetView, nullptr); ASSERT_NE(mSceneColorShaderView, nullptr); ASSERT_NE(mDepthView, nullptr); RHICommandList* commandList = GetCommandList(); ASSERT_NE(commandList, nullptr); RHIResourceView* backBufferView = GetCurrentBackBufferView(); ASSERT_NE(backBufferView, nullptr); commandList->Reset(); RenderSurface mainSceneSurface(kFrameWidth, kFrameHeight); mainSceneSurface.SetColorAttachment(mSceneColorTargetView); mainSceneSurface.SetDepthAttachment(mDepthView); mainSceneSurface.SetColorStateBefore(ResourceStates::Common); mainSceneSurface.SetColorStateAfter(ResourceStates::PixelShaderResource); RenderSurface finalSurface(kFrameWidth, kFrameHeight); finalSurface.SetColorAttachment(backBufferView); finalSurface.SetColorStateBefore(ResourceStates::Present); finalSurface.SetColorStateAfter(ResourceStates::Present); RenderContext renderContext = {}; renderContext.device = GetDevice(); renderContext.commandList = commandList; renderContext.commandQueue = GetCommandQueue(); renderContext.backendType = GetBackendType(); CameraRenderRequest request = {}; request.scene = mScene.get(); request.camera = mCamera; request.context = renderContext; request.surface = finalSurface; request.postProcess.sourceSurface = mainSceneSurface; request.postProcess.sourceColorView = mSceneColorShaderView; request.postProcess.destinationSurface = finalSurface; request.postProcess.passes = &mPostProcessPasses; ASSERT_TRUE(mCameraRenderer->Render(request)); commandList->Close(); void* commandLists[] = { commandList }; GetCommandQueue()->ExecuteCommandLists(1, commandLists); } TEST_P(PostProcessSceneTest, RenderPostProcessScene) { 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(comparisonThreshold))); break; } swapChain->Present(0, 0); } } } // namespace INSTANTIATE_TEST_SUITE_P(D3D12, PostProcessSceneTest, ::testing::Values(RHIType::D3D12)); INSTANTIATE_TEST_SUITE_P(OpenGL, PostProcessSceneTest, ::testing::Values(RHIType::OpenGL)); #if defined(XCENGINE_SUPPORT_VULKAN) INSTANTIATE_TEST_SUITE_P(Vulkan, PostProcessSceneTest, ::testing::Values(RHIType::Vulkan)); #endif GTEST_API_ int main(int argc, char** argv) { return RunRenderingIntegrationTestMain(argc, argv); }