#define NOMINMAX #include #include #include "../RenderingIntegrationMain.h" #include #include #include #include #include #include #include #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 #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 = "directional_shadow_scene_d3d12.ppm"; constexpr const char* kOpenGLScreenshot = "directional_shadow_scene_opengl.ppm"; constexpr const char* kVulkanScreenshot = "directional_shadow_scene_vulkan.ppm"; constexpr uint32_t kFrameWidth = 1280; constexpr uint32_t kFrameHeight = 720; void AppendQuadFace( std::vector& vertices, std::vector& indices, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& normal) { const uint32_t baseIndex = static_cast(vertices.size()); StaticMeshVertex vertex = {}; vertex.normal = normal; vertex.position = v0; vertex.uv0 = Vector2(0.0f, 1.0f); vertices.push_back(vertex); vertex.position = v1; vertex.uv0 = Vector2(1.0f, 1.0f); vertices.push_back(vertex); vertex.position = v2; vertex.uv0 = Vector2(0.0f, 0.0f); vertices.push_back(vertex); vertex.position = v3; vertex.uv0 = Vector2(1.0f, 0.0f); vertices.push_back(vertex); indices.push_back(baseIndex + 0); indices.push_back(baseIndex + 2); indices.push_back(baseIndex + 1); indices.push_back(baseIndex + 1); indices.push_back(baseIndex + 2); indices.push_back(baseIndex + 3); } Mesh* CreateGroundMesh() { auto* mesh = new Mesh(); IResource::ConstructParams params = {}; params.name = "DirectionalShadowGroundMesh"; params.path = "Tests/Rendering/DirectionalShadowGround.mesh"; params.guid = ResourceGUID::Generate(params.path); mesh->Initialize(params); StaticMeshVertex vertices[4] = {}; vertices[0].position = Vector3(-12.0f, 0.0f, 1.0f); vertices[0].normal = Vector3::Up(); vertices[0].uv0 = Vector2(0.0f, 1.0f); vertices[1].position = Vector3(-12.0f, 0.0f, 20.0f); vertices[1].normal = Vector3::Up(); vertices[1].uv0 = Vector2(0.0f, 0.0f); vertices[2].position = Vector3(12.0f, 0.0f, 1.0f); vertices[2].normal = Vector3::Up(); vertices[2].uv0 = Vector2(1.0f, 1.0f); vertices[3].position = Vector3(12.0f, 0.0f, 20.0f); vertices[3].normal = Vector3::Up(); 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::Normal | VertexAttribute::UV0); mesh->SetIndexData(indices, sizeof(indices), 6, true); const Bounds bounds(Vector3(0.0f, 0.0f, 10.5f), Vector3(24.0f, 0.1f, 19.0f)); mesh->SetBounds(bounds); MeshSection section = {}; section.baseVertex = 0; section.vertexCount = 4; section.startIndex = 0; section.indexCount = 6; section.materialID = 0; section.bounds = bounds; mesh->AddSection(section); return mesh; } Mesh* CreateCubeMesh() { auto* mesh = new Mesh(); IResource::ConstructParams params = {}; params.name = "DirectionalShadowCubeMesh"; params.path = "Tests/Rendering/DirectionalShadowCube.mesh"; params.guid = ResourceGUID::Generate(params.path); mesh->Initialize(params); std::vector vertices; std::vector indices; vertices.reserve(24); indices.reserve(36); constexpr float half = 0.5f; AppendQuadFace( vertices, indices, Vector3(-half, -half, half), Vector3(half, -half, half), Vector3(-half, half, half), Vector3(half, half, half), Vector3::Forward()); AppendQuadFace( vertices, indices, Vector3(half, -half, -half), Vector3(-half, -half, -half), Vector3(half, half, -half), Vector3(-half, half, -half), Vector3::Back()); AppendQuadFace( vertices, indices, Vector3(-half, -half, -half), Vector3(-half, -half, half), Vector3(-half, half, -half), Vector3(-half, half, half), Vector3::Left()); AppendQuadFace( vertices, indices, Vector3(half, -half, half), Vector3(half, -half, -half), Vector3(half, half, half), Vector3(half, half, -half), Vector3::Right()); AppendQuadFace( vertices, indices, Vector3(-half, half, half), Vector3(half, half, half), Vector3(-half, half, -half), Vector3(half, half, -half), Vector3::Up()); AppendQuadFace( vertices, indices, Vector3(-half, -half, -half), Vector3(half, -half, -half), Vector3(-half, -half, half), Vector3(half, -half, half), Vector3::Down()); mesh->SetVertexData( vertices.data(), vertices.size() * sizeof(StaticMeshVertex), static_cast(vertices.size()), sizeof(StaticMeshVertex), VertexAttribute::Position | VertexAttribute::Normal | VertexAttribute::UV0); mesh->SetIndexData( indices.data(), indices.size() * sizeof(uint32_t), static_cast(indices.size()), true); const Bounds bounds(Vector3::Zero(), Vector3::One()); mesh->SetBounds(bounds); MeshSection section = {}; section.baseVertex = 0; section.vertexCount = static_cast(vertices.size()); section.startIndex = 0; section.indexCount = static_cast(indices.size()); section.materialID = 0; section.bounds = bounds; mesh->AddSection(section); return mesh; } Material* CreateForwardLitMaterial( const char* name, const char* path, const Vector4& baseColor) { auto* material = new Material(); IResource::ConstructParams params = {}; params.name = name; params.path = path; params.guid = ResourceGUID::Generate(params.path); material->Initialize(params); material->SetShader(ResourceManager::Get().Load(GetBuiltinForwardLitShaderPath())); material->SetShaderPass("ForwardLit"); material->SetFloat4("_BaseColor", baseColor); 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 ? 10 : 10; } class DirectionalShadowSceneTest : public RHIIntegrationFixture { protected: void SetUp() override; void TearDown() override; void RenderFrame() override; void RenderSceneFrame(); private: void BuildScene(); RHIResourceView* GetCurrentBackBufferView(); std::unique_ptr mScene; std::unique_ptr mSceneRenderer; std::vector mBackBufferViews; RHITexture* mDepthTexture = nullptr; RHIResourceView* mDepthView = nullptr; Mesh* mGroundMesh = nullptr; Mesh* mCubeMesh = nullptr; Material* mGroundMaterial = nullptr; Material* mCasterMaterial = nullptr; }; void DirectionalShadowSceneTest::SetUp() { RHIIntegrationFixture::SetUp(); mSceneRenderer = std::make_unique(); mScene = std::make_unique("DirectionalShadowScene"); mGroundMesh = CreateGroundMesh(); ASSERT_NE(mGroundMesh, nullptr); mCubeMesh = CreateCubeMesh(); ASSERT_NE(mCubeMesh, nullptr); mGroundMaterial = CreateForwardLitMaterial( "DirectionalShadowGround", "Tests/Rendering/DirectionalShadowGround.material", Vector4(0.92f, 0.93f, 0.96f, 1.0f)); mCasterMaterial = CreateForwardLitMaterial( "DirectionalShadowCaster", "Tests/Rendering/DirectionalShadowCaster.material", Vector4(0.72f, 0.22f, 0.16f, 1.0f)); BuildScene(); 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 DirectionalShadowSceneTest::TearDown() { 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 mGroundMaterial; mGroundMaterial = nullptr; delete mCasterMaterial; mCasterMaterial = nullptr; delete mGroundMesh; mGroundMesh = nullptr; delete mCubeMesh; mCubeMesh = nullptr; RHIIntegrationFixture::TearDown(); } void DirectionalShadowSceneTest::BuildScene() { ASSERT_NE(mScene, nullptr); ASSERT_NE(mGroundMesh, nullptr); ASSERT_NE(mCubeMesh, nullptr); ASSERT_NE(mGroundMaterial, nullptr); ASSERT_NE(mCasterMaterial, nullptr); GameObject* cameraObject = mScene->CreateGameObject("MainCamera"); auto* camera = cameraObject->AddComponent(); camera->SetPrimary(true); camera->SetFieldOfView(42.0f); camera->SetNearClipPlane(0.1f); camera->SetFarClipPlane(40.0f); camera->SetClearColor(XCEngine::Math::Color(0.03f, 0.03f, 0.05f, 1.0f)); cameraObject->GetTransform()->SetLocalPosition(Vector3(0.2f, 3.4f, -7.2f)); cameraObject->GetTransform()->SetLocalRotation( Quaternion::LookRotation(Vector3(-0.12f, -0.14f, 1.0f).Normalized())); GameObject* lightObject = mScene->CreateGameObject("MainDirectionalLight"); auto* light = lightObject->AddComponent(); light->SetLightType(LightType::Directional); light->SetColor(XCEngine::Math::Color(1.0f, 1.0f, 0.97f, 1.0f)); light->SetIntensity(2.3f); light->SetCastsShadows(true); lightObject->GetTransform()->SetLocalRotation( Quaternion::LookRotation(Vector3(0.84f, -0.52f, -0.14f).Normalized())); GameObject* groundObject = mScene->CreateGameObject("Ground"); auto* groundMeshFilter = groundObject->AddComponent(); auto* groundMeshRenderer = groundObject->AddComponent(); groundObject->GetTransform()->SetLocalPosition(Vector3(0.0f, -0.18f, 10.5f)); groundObject->GetTransform()->SetLocalScale(Vector3(12.0f, 0.18f, 9.5f)); groundMeshFilter->SetMesh(ResourceHandle(mCubeMesh)); groundMeshRenderer->SetMaterial(0, mGroundMaterial); groundMeshRenderer->SetCastShadows(false); groundMeshRenderer->SetReceiveShadows(true); GameObject* casterObject = mScene->CreateGameObject("CasterCube"); casterObject->GetTransform()->SetLocalPosition(Vector3(-1.2f, 1.71f, 8.8f)); casterObject->GetTransform()->SetLocalScale(Vector3(1.8f, 3.6f, 1.8f)); auto* casterMeshFilter = casterObject->AddComponent(); auto* casterMeshRenderer = casterObject->AddComponent(); casterMeshFilter->SetMesh(ResourceHandle(mCubeMesh)); casterMeshRenderer->SetMaterial(0, mCasterMaterial); casterMeshRenderer->SetCastShadows(true); casterMeshRenderer->SetReceiveShadows(true); } RHIResourceView* DirectionalShadowSceneTest::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 DirectionalShadowSceneTest::RenderFrame() { RenderSceneFrame(); } void DirectionalShadowSceneTest::RenderSceneFrame() { ASSERT_NE(mScene, nullptr); ASSERT_NE(mSceneRenderer, nullptr); RHICommandList* commandList = GetCommandList(); ASSERT_NE(commandList, nullptr); commandList->Reset(); RenderSurface surface(kFrameWidth, kFrameHeight); surface.SetColorAttachment(GetCurrentBackBufferView()); surface.SetDepthAttachment(mDepthView); RenderContext renderContext = {}; renderContext.device = GetDevice(); renderContext.commandList = commandList; renderContext.commandQueue = GetCommandQueue(); renderContext.backendType = GetBackendType(); std::vector requests = mSceneRenderer->BuildRenderRequests(*mScene, nullptr, renderContext, surface); ASSERT_FALSE(requests.empty()); ASSERT_TRUE(mSceneRenderer->Render(requests)); Log("[TEST] DirectionalShadowSceneTest: closing command list"); commandList->Close(); void* commandLists[] = { commandList }; Log("[TEST] DirectionalShadowSceneTest: executing command list"); GetCommandQueue()->ExecuteCommandLists(1, commandLists); Log("[TEST] DirectionalShadowSceneTest: execute submitted"); } TEST_P(DirectionalShadowSceneTest, RenderDirectionalShadowScene) { 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, DirectionalShadowSceneTest, ::testing::Values(RHIType::D3D12)); INSTANTIATE_TEST_SUITE_P(OpenGL, DirectionalShadowSceneTest, ::testing::Values(RHIType::OpenGL)); #if defined(XCENGINE_SUPPORT_VULKAN) INSTANTIATE_TEST_SUITE_P(Vulkan, DirectionalShadowSceneTest, ::testing::Values(RHIType::Vulkan)); #endif GTEST_API_ int main(int argc, char** argv) { return RunRenderingIntegrationTestMain(argc, argv); }