From 7bfa732afdc5c4b23f06a7480a1903111ca4aaf4 Mon Sep 17 00:00:00 2001 From: fleroviux Date: Fri, 10 May 2024 21:53:42 +0200 Subject: [PATCH] Zephyr: Next: implement cube benchmarking scene --- app/next/src/main_window.cpp | 95 +++++++++++++++++++++++++++++++++++- app/next/src/main_window.hpp | 1 + 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/app/next/src/main_window.cpp b/app/next/src/main_window.cpp index 359ce62..5c03260 100644 --- a/app/next/src/main_window.cpp +++ b/app/next/src/main_window.cpp @@ -7,6 +7,7 @@ #include "main_window.hpp" static const bool enable_validation_layers = true; +static const bool benchmark_scene_size = true; namespace zephyr { @@ -27,7 +28,11 @@ namespace zephyr { } void MainWindow::Setup() { - CreateScene(); + if(benchmark_scene_size) { + CreateBenchmarkScene(); + } else { + CreateScene(); + } #ifdef ZEPHYR_OPENGL CreateOpenGLEngine(); @@ -165,6 +170,94 @@ namespace zephyr { m_scene_root->Add(m_behemoth_scene); } + void MainWindow::CreateBenchmarkScene() { + m_scene_root = SceneNode::New(); + + // TODO(fleroviux): engine crashes when there is no camera in the scene! VERY BAD!!! + m_camera_node = m_scene_root->CreateChild("RenderCamera"); + m_camera_node->CreateComponent(45.0f, 16.f / 9.f, 0.01f, 100.f); + m_camera_node->GetTransform().GetPosition() = {0.f, 0.f, 5.f}; + + // TODO(fleroviux): fix cube geometry is rendered incorrectly. + + /** + * 4-------5 + * /| /| + * 0-------1 | + * | 6-----|-7 + * |/ |/ + * 2-------3 + */ + RenderGeometryLayout layout{}; + layout.AddAttribute(RenderGeometryAttribute::Position); + layout.AddAttribute(RenderGeometryAttribute::Color); + + std::shared_ptr cube_geometry = std::make_shared(layout, 8, 36); + + auto positions = cube_geometry->GetPositions(); + positions[0] = Vector3{-1.0, -1.0, 1.0}; + positions[1] = Vector3{ 1.0, -1.0, 1.0}; + positions[2] = Vector3{-1.0, 1.0, 1.0}; + positions[3] = Vector3{ 1.0, 1.0, 1.0}; + positions[4] = Vector3{-1.0, -1.0, -1.0}; + positions[5] = Vector3{ 1.0, -1.0, -1.0}; + positions[6] = Vector3{-1.0, 1.0, -1.0}; + positions[7] = Vector3{ 1.0, 1.0, -1.0}; + + auto colors = cube_geometry->GetColors(); + if(colors.IsValid()) { + colors[0] = Vector4{1.0, 0.0, 0.0, 1.0}; + colors[1] = Vector4{0.0, 1.0, 0.0, 1.0}; + colors[2] = Vector4{0.0, 0.0, 1.0, 1.0}; + colors[3] = Vector4{1.0, 0.0, 1.0, 1.0}; + colors[4] = Vector4{1.0, 1.0, 0.0, 1.0}; + colors[5] = Vector4{0.0, 1.0, 1.0, 1.0}; + colors[6] = Vector4{1.0, 1.0, 1.0, 1.0}; + colors[7] = Vector4{0.0, 0.0, 0.0, 1.0}; + } + + auto indices = cube_geometry->GetIndices(); + u32 index_data[] { + // front + 0, 1, 2, + 1, 3, 2, + + // back + 4, 5, 6, + 5, 7, 6, + + // left + 0, 4, 6, + 0, 6, 2, + + // right + 1, 5, 7, + 1, 7, 3, + + // top + 4, 1, 0, + 4, 5, 1, + + // bottom + 6, 3, 2, + 6, 7, 3 + }; + std::copy_n(index_data, sizeof(index_data) / sizeof(u32), indices.begin()); + + const int grid_size = 64; + + for(int x = -grid_size / 2; x < grid_size / 2; x++) { + for(int y = -grid_size / 2; y < grid_size / 2; y++) { + for(int z = -grid_size / 2; z < grid_size / 2; z++) { + std::shared_ptr cube = m_scene_root->CreateChild("Cube"); + cube->CreateComponent(cube_geometry); + cube->GetTransform().GetPosition() = {(f32)x, (f32)y, (f32)-z}; + cube->GetTransform().GetScale() = {0.1, 0.1, 0.1}; + } + } + } + } + void MainWindow::CleanupVulkan() { vkDestroySurfaceKHR(m_vk_instance->Handle(), m_vk_surface, nullptr); SDL_DestroyWindow(m_window); diff --git a/app/next/src/main_window.hpp b/app/next/src/main_window.hpp index 781bdee..a5d71f8 100644 --- a/app/next/src/main_window.hpp +++ b/app/next/src/main_window.hpp @@ -30,6 +30,7 @@ namespace zephyr { void MainLoop(); void RenderFrame(); void CreateScene(); + void CreateBenchmarkScene(); void CreateVulkanEngine(); void CleanupVulkan();