#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::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 = "cull_material_scene_d3d12.ppm"; constexpr const char* kOpenGLScreenshot = "cull_material_scene_opengl.ppm"; constexpr const char* kVulkanScreenshot = "cull_material_scene_vulkan.ppm"; constexpr uint32_t kFrameWidth = 1280; constexpr uint32_t kFrameHeight = 720; Mesh* CreateQuadMesh() { auto* mesh = new Mesh(); IResource::ConstructParams params = {}; params.name = "CullQuad"; params.path = "Tests/Rendering/CullQuad.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, 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(MaterialRenderQueue::Geometry); 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) { 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 CullMaterialSceneTest : public RHIIntegrationFixture { protected: void SetUp() override; void TearDown() override; void RenderFrame() override; private: void BuildScene(); RHIResourceView* GetCurrentBackBufferView(); std::unique_ptr mScene; std::unique_ptr mSceneRenderer; std::vector mBackBufferViews; std::vector mTextures; std::vector mMaterials; RHITexture* mDepthTexture = nullptr; RHIResourceView* mDepthView = nullptr; Mesh* mMesh = nullptr; }; void CullMaterialSceneTest::SetUp() { RHIIntegrationFixture::SetUp(); mSceneRenderer = std::make_unique(); mScene = std::make_unique("CullMaterialScene"); mMesh = CreateQuadMesh(); 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 CullMaterialSceneTest::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(); for (Material* material : mMaterials) { delete material; } mMaterials.clear(); for (Texture* texture : mTextures) { delete texture; } mTextures.clear(); delete mMesh; mMesh = nullptr; RHIIntegrationFixture::TearDown(); } void CullMaterialSceneTest::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(3.0f); camera->SetNearClipPlane(0.1f); camera->SetFarClipPlane(10.0f); camera->SetClearColor(XCEngine::Math::Color(0.08f, 0.09f, 0.12f, 1.0f)); const unsigned char greenPixel[4] = { 72, 214, 122, 255 }; const unsigned char redPixel[4] = { 230, 84, 84, 255 }; const unsigned char yellowPixel[4] = { 238, 204, 64, 255 }; const unsigned char bluePixel[4] = { 74, 148, 244, 255 }; Texture* backCullFrontFacingTexture = CreateSolidTexture( "BackCullFrontFacingTexture", "Tests/Rendering/CullScene/back_front.texture", greenPixel); Texture* backCullBackFacingTexture = CreateSolidTexture( "BackCullBackFacingTexture", "Tests/Rendering/CullScene/back_back.texture", redPixel); Texture* frontCullFrontFacingTexture = CreateSolidTexture( "FrontCullFrontFacingTexture", "Tests/Rendering/CullScene/front_front.texture", yellowPixel); Texture* frontCullBackFacingTexture = CreateSolidTexture( "FrontCullBackFacingTexture", "Tests/Rendering/CullScene/front_back.texture", bluePixel); mTextures = { backCullFrontFacingTexture, backCullBackFacingTexture, frontCullFrontFacingTexture, frontCullBackFacingTexture }; MaterialRenderState backCullState = {}; backCullState.depthTestEnable = true; backCullState.depthWriteEnable = true; backCullState.depthFunc = MaterialComparisonFunc::LessEqual; backCullState.cullMode = MaterialCullMode::Back; MaterialRenderState frontCullState = backCullState; frontCullState.cullMode = MaterialCullMode::Front; Material* backCullFrontFacingMaterial = CreateMaterial( "BackCullFrontFacingMaterial", "Tests/Rendering/CullScene/back_front.mat", backCullFrontFacingTexture, backCullState); Material* backCullBackFacingMaterial = CreateMaterial( "BackCullBackFacingMaterial", "Tests/Rendering/CullScene/back_back.mat", backCullBackFacingTexture, backCullState); Material* frontCullFrontFacingMaterial = CreateMaterial( "FrontCullFrontFacingMaterial", "Tests/Rendering/CullScene/front_front.mat", frontCullFrontFacingTexture, frontCullState); Material* frontCullBackFacingMaterial = CreateMaterial( "FrontCullBackFacingMaterial", "Tests/Rendering/CullScene/front_back.mat", frontCullBackFacingTexture, frontCullState); mMaterials = { backCullFrontFacingMaterial, backCullBackFacingMaterial, frontCullFrontFacingMaterial, frontCullBackFacingMaterial }; const Vector3 quadScale(1.15f, 1.15f, 1.0f); const Quaternion frontFacing = Quaternion::Identity(); const Quaternion backFacing = Quaternion::FromAxisAngle(Vector3::Up(), 3.1415926535f); CreateQuadObject( *mScene, "BackCullFrontFacing", mMesh, backCullFrontFacingMaterial, Vector3(-2.1f, 1.25f, 3.0f), quadScale, frontFacing); CreateQuadObject( *mScene, "BackCullBackFacing", mMesh, backCullBackFacingMaterial, Vector3(2.1f, 1.25f, 3.0f), quadScale, backFacing); CreateQuadObject( *mScene, "FrontCullFrontFacing", mMesh, frontCullFrontFacingMaterial, Vector3(-2.1f, -1.25f, 3.0f), quadScale, frontFacing); CreateQuadObject( *mScene, "FrontCullBackFacing", mMesh, frontCullBackFacingMaterial, Vector3(2.1f, -1.25f, 3.0f), quadScale, backFacing); } RHIResourceView* CullMaterialSceneTest::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 CullMaterialSceneTest::RenderFrame() { 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(); ASSERT_TRUE(mSceneRenderer->Render(*mScene, nullptr, renderContext, surface)); commandList->Close(); void* commandLists[] = { commandList }; GetCommandQueue()->ExecuteCommandLists(1, commandLists); } TEST_P(CullMaterialSceneTest, RenderCullMaterialScene) { 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, CullMaterialSceneTest, ::testing::Values(RHIType::D3D12)); INSTANTIATE_TEST_SUITE_P(OpenGL, CullMaterialSceneTest, ::testing::Values(RHIType::OpenGL)); #if defined(XCENGINE_SUPPORT_VULKAN) INSTANTIATE_TEST_SUITE_P(Vulkan, CullMaterialSceneTest, ::testing::Values(RHIType::Vulkan)); #endif GTEST_API_ int main(int argc, char** argv) { return RunRenderingIntegrationTestMain(argc, argv); }