#include #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::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 = "offscreen_scene_d3d12.ppm"; constexpr const char* kOpenGLScreenshot = "offscreen_scene_opengl.ppm"; constexpr const char* kVulkanScreenshot = "offscreen_scene_vulkan.ppm"; constexpr uint32_t kFrameWidth = 1280; constexpr uint32_t kFrameHeight = 720; Mesh* CreateQuadMesh() { auto* mesh = new Mesh(); IResource::ConstructParams params = {}; params.name = "OffscreenQuad"; params.path = "Tests/Rendering/OffscreenQuad.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, MaterialRenderQueue renderQueue, 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)); material->SetRenderQueue(renderQueue); 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 = Quaternion::Identity()) { GameObject* gameObject = scene.CreateGameObject(name); gameObject->GetTransform()->SetLocalPosition(position); gameObject->GetTransform()->SetLocalScale(scale); gameObject->GetTransform()->SetLocalRotation(rotation); auto* meshFilter = gameObject->AddComponent(); auto* meshRenderer = gameObject->AddComponent(); meshFilter->SetMesh(ResourceHandle(mesh)); meshRenderer->SetMaterial(0, ResourceHandle(material)); return gameObject; } 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) { (void)backendType; return 0; } class OffscreenSceneTest : public RHIIntegrationFixture { protected: void SetUp() override; void TearDown() override; void RenderFrame() override; bool RequiresSwapChainAcquire() const override { return false; } private: void BuildScene(); RHIResourceView* GetCurrentBackBufferView(); std::unique_ptr mScene; std::unique_ptr mSceneRenderer; std::vector mBackBufferViews; std::vector mTextures; std::vector mMaterials; RHITexture* mOffscreenColorTexture = nullptr; RHIResourceView* mOffscreenColorView = nullptr; RHITexture* mOffscreenDepthTexture = nullptr; RHIResourceView* mOffscreenDepthView = nullptr; Mesh* mMesh = nullptr; ResourceStates mBackBufferState = ResourceStates::Present; }; void OffscreenSceneTest::SetUp() { RHIIntegrationFixture::SetUp(); mSceneRenderer = std::make_unique(); mScene = std::make_unique("OffscreenScene"); mMesh = CreateQuadMesh(); BuildScene(); TextureDesc colorDesc = {}; colorDesc.width = kFrameWidth; colorDesc.height = kFrameHeight; colorDesc.depth = 1; colorDesc.mipLevels = 1; colorDesc.arraySize = 1; colorDesc.format = static_cast(Format::R8G8B8A8_UNorm); colorDesc.textureType = static_cast(XCEngine::RHI::TextureType::Texture2D); colorDesc.sampleCount = 1; colorDesc.sampleQuality = 0; colorDesc.flags = 0; mOffscreenColorTexture = GetDevice()->CreateTexture(colorDesc); ASSERT_NE(mOffscreenColorTexture, nullptr); ResourceViewDesc colorViewDesc = {}; colorViewDesc.format = static_cast(Format::R8G8B8A8_UNorm); colorViewDesc.dimension = ResourceViewDimension::Texture2D; colorViewDesc.mipLevel = 0; mOffscreenColorView = GetDevice()->CreateRenderTargetView(mOffscreenColorTexture, colorViewDesc); ASSERT_NE(mOffscreenColorView, 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; mOffscreenDepthTexture = GetDevice()->CreateTexture(depthDesc); ASSERT_NE(mOffscreenDepthTexture, nullptr); ResourceViewDesc depthViewDesc = {}; depthViewDesc.format = static_cast(Format::D24_UNorm_S8_UInt); depthViewDesc.dimension = ResourceViewDimension::Texture2D; depthViewDesc.mipLevel = 0; mOffscreenDepthView = GetDevice()->CreateDepthStencilView(mOffscreenDepthTexture, depthViewDesc); ASSERT_NE(mOffscreenDepthView, nullptr); mBackBufferViews.resize(2, nullptr); mBackBufferState = ResourceStates::Present; } void OffscreenSceneTest::TearDown() { mSceneRenderer.reset(); if (mOffscreenDepthView != nullptr) { mOffscreenDepthView->Shutdown(); delete mOffscreenDepthView; mOffscreenDepthView = nullptr; } if (mOffscreenDepthTexture != nullptr) { mOffscreenDepthTexture->Shutdown(); delete mOffscreenDepthTexture; mOffscreenDepthTexture = nullptr; } if (mOffscreenColorView != nullptr) { mOffscreenColorView->Shutdown(); delete mOffscreenColorView; mOffscreenColorView = nullptr; } if (mOffscreenColorTexture != nullptr) { mOffscreenColorTexture->Shutdown(); delete mOffscreenColorTexture; mOffscreenColorTexture = 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 OffscreenSceneTest::BuildScene() { ASSERT_NE(mScene, nullptr); ASSERT_NE(mMesh, nullptr); GameObject* cameraObject = mScene->CreateGameObject("MainCamera"); auto* camera = cameraObject->AddComponent(); camera->SetPrimary(true); camera->SetProjectionType(CameraProjectionType::Orthographic); camera->SetOrthographicSize(2.0f); camera->SetNearClipPlane(0.1f); camera->SetFarClipPlane(10.0f); camera->SetClearColor(XCEngine::Math::Color(0.03f, 0.05f, 0.08f, 1.0f)); const unsigned char bluePixel[4] = { 70, 110, 224, 255 }; const unsigned char whitePixel[4] = { 244, 244, 232, 255 }; const unsigned char redPixel[4] = { 240, 90, 90, 160 }; const unsigned char greenPixel[4] = { 88, 236, 140, 160 }; Texture* backgroundTexture = CreateSolidTexture( "BackgroundTexture", "Tests/Rendering/Offscreen/background.texture", bluePixel); Texture* centerTexture = CreateSolidTexture( "CenterTexture", "Tests/Rendering/Offscreen/center.texture", whitePixel); Texture* farTransparentTexture = CreateSolidTexture( "FarTransparentTexture", "Tests/Rendering/Offscreen/far.texture", redPixel); Texture* nearTransparentTexture = CreateSolidTexture( "NearTransparentTexture", "Tests/Rendering/Offscreen/near.texture", greenPixel); mTextures = { backgroundTexture, centerTexture, farTransparentTexture, nearTransparentTexture }; MaterialRenderState opaqueState = {}; opaqueState.depthTestEnable = true; opaqueState.depthWriteEnable = true; opaqueState.depthFunc = MaterialComparisonFunc::LessEqual; opaqueState.cullMode = MaterialCullMode::None; MaterialRenderState transparentState = {}; transparentState.blendEnable = true; transparentState.srcBlend = MaterialBlendFactor::SrcAlpha; transparentState.dstBlend = MaterialBlendFactor::InvSrcAlpha; transparentState.srcBlendAlpha = MaterialBlendFactor::One; transparentState.dstBlendAlpha = MaterialBlendFactor::Zero; transparentState.depthTestEnable = true; transparentState.depthWriteEnable = false; transparentState.depthFunc = MaterialComparisonFunc::LessEqual; transparentState.cullMode = MaterialCullMode::None; Material* backgroundMaterial = CreateMaterial( "BackgroundMaterial", "Tests/Rendering/Offscreen/background.mat", backgroundTexture, MaterialRenderQueue::Geometry, opaqueState); Material* centerMaterial = CreateMaterial( "CenterMaterial", "Tests/Rendering/Offscreen/center.mat", centerTexture, MaterialRenderQueue::Geometry, opaqueState); Material* farTransparentMaterial = CreateMaterial( "FarTransparentMaterial", "Tests/Rendering/Offscreen/far.mat", farTransparentTexture, MaterialRenderQueue::Transparent, transparentState); Material* nearTransparentMaterial = CreateMaterial( "NearTransparentMaterial", "Tests/Rendering/Offscreen/near.mat", nearTransparentTexture, MaterialRenderQueue::Transparent, transparentState); mMaterials = { backgroundMaterial, centerMaterial, farTransparentMaterial, nearTransparentMaterial }; CreateQuadObject( *mScene, "Background", mMesh, backgroundMaterial, Vector3(0.0f, 0.0f, 4.0f), Vector3(2.9f, 1.85f, 1.0f)); CreateQuadObject( *mScene, "CenterOpaque", mMesh, centerMaterial, Vector3(0.0f, 0.0f, 3.0f), Vector3(0.42f, 1.18f, 1.0f)); CreateQuadObject( *mScene, "FarTransparent", mMesh, farTransparentMaterial, Vector3(-0.18f, 0.0f, 3.45f), Vector3(1.02f, 1.02f, 1.0f), Quaternion::FromAxisAngle(Vector3::Up(), 0.56f)); CreateQuadObject( *mScene, "NearTransparent", mMesh, nearTransparentMaterial, Vector3(0.18f, 0.0f, 2.65f), Vector3(1.02f, 1.02f, 1.0f), Quaternion::FromAxisAngle(Vector3::Up(), -0.56f)); } RHIResourceView* OffscreenSceneTest::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 OffscreenSceneTest::RenderFrame() { ASSERT_NE(mScene, nullptr); ASSERT_NE(mSceneRenderer, nullptr); ASSERT_NE(mOffscreenColorView, nullptr); ASSERT_NE(mOffscreenDepthView, nullptr); RHICommandList* commandList = GetCommandList(); ASSERT_NE(commandList, nullptr); RHIResourceView* backBufferView = GetCurrentBackBufferView(); ASSERT_NE(backBufferView, nullptr); commandList->Reset(); RenderSurface offscreenSurface(kFrameWidth, kFrameHeight); offscreenSurface.SetColorAttachment(mOffscreenColorView); offscreenSurface.SetDepthAttachment(mOffscreenDepthView); offscreenSurface.SetColorStateBefore(ResourceStates::Common); offscreenSurface.SetColorStateAfter(ResourceStates::CopySrc); RenderContext renderContext = {}; renderContext.device = GetDevice(); renderContext.commandList = commandList; renderContext.commandQueue = GetCommandQueue(); renderContext.backendType = GetBackendType(); ASSERT_TRUE(mSceneRenderer->Render(*mScene, nullptr, renderContext, offscreenSurface)); commandList->TransitionBarrier(backBufferView, mBackBufferState, ResourceStates::CopyDst); commandList->CopyResource(backBufferView, mOffscreenColorView); commandList->TransitionBarrier(mOffscreenColorView, ResourceStates::CopySrc, ResourceStates::Common); commandList->TransitionBarrier(backBufferView, ResourceStates::CopyDst, ResourceStates::RenderTarget); mBackBufferState = ResourceStates::RenderTarget; commandList->Close(); void* commandLists[] = { commandList }; GetCommandQueue()->ExecuteCommandLists(1, commandLists); } TEST_P(OffscreenSceneTest, RenderOffscreenScene) { RHICommandQueue* commandQueue = GetCommandQueue(); 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; } } } } // namespace INSTANTIATE_TEST_SUITE_P(D3D12, OffscreenSceneTest, ::testing::Values(RHIType::D3D12)); INSTANTIATE_TEST_SUITE_P(OpenGL, OffscreenSceneTest, ::testing::Values(RHIType::OpenGL)); #if defined(XCENGINE_SUPPORT_VULKAN) INSTANTIATE_TEST_SUITE_P(Vulkan, OffscreenSceneTest, ::testing::Values(RHIType::Vulkan)); #endif GTEST_API_ int main(int argc, char** argv) { Logger::Get().Initialize(); Logger::Get().AddSink(std::make_unique()); Logger::Get().SetMinimumLevel(LogLevel::Debug); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }