From beb409e50add3f4785135072e142a1bb4e02f4eb Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sun, 18 May 2025 18:30:18 +0200 Subject: [PATCH] working on scene management --- Application/Chunk.cpp | 2 +- Application/World.cpp | 21 +++++------ Application/World.h | 2 -- ScopEngine/Runtime/Includes/Graphics/Scene.h | 10 +++--- .../Runtime/Includes/Renderer/RenderCore.h | 2 +- ScopEngine/Runtime/Sources/Graphics/Scene.cpp | 36 +++++++------------ .../Sources/Renderer/RenderPasses/2DPass.cpp | 16 ++++----- .../Renderer/RenderPasses/ForwardPass.cpp | 12 +++---- 8 files changed, 42 insertions(+), 59 deletions(-) diff --git a/Application/Chunk.cpp b/Application/Chunk.cpp index 8eb1533..7580e1e 100644 --- a/Application/Chunk.cpp +++ b/Application/Chunk.cpp @@ -117,7 +117,7 @@ void Chunk::GenerateMesh() void Chunk::UploadMesh() { - if(p_actor) + if(p_actor || m_mesh_data.empty() || m_mesh_index_data.empty()) return; std::shared_ptr mesh = std::make_shared(); mesh->AddSubMesh({ std::move(m_mesh_data), std::move(m_mesh_index_data) }); diff --git a/Application/World.cpp b/Application/World.cpp index f578cc0..a13b3fd 100644 --- a/Application/World.cpp +++ b/Application/World.cpp @@ -5,7 +5,7 @@ #include #include -World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrator()), m_previous_chunk_position(-1000, 10000) +World::World(Scop::Scene& scene) : m_scene(scene), m_previous_chunk_position(-1000, 10000) { Scop::Vec2ui32 map_size; Scop::MaterialTextures material_params; @@ -37,7 +37,7 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrat Upload(); }; - m_narrator.AttachScript(std::make_shared(std::function{}, narrator_update, std::function{})); + m_scene.CreateNarrator().AttachScript(std::make_shared(std::function{}, narrator_update, std::function{})); } [[nodiscard]] Scop::NonOwningPtr World::GetChunk(Scop::Vec2i position) @@ -55,7 +55,7 @@ void World::UnloadChunks(Scop::Vec2i current_chunk_position) Scop::Vec3i pos = it->first; float x_dist = std::abs(pos.x - current_chunk_position.x); float z_dist = std::abs(pos.z - current_chunk_position.y); - if(RENDER_DISTANCE_HALF < x_dist || RENDER_DISTANCE_HALF < z_dist) + if(RENDER_DISTANCE < x_dist || RENDER_DISTANCE < z_dist) { if(it->second.GetActor()) m_scene.RemoveActor(*it->second.GetActor()); @@ -69,22 +69,19 @@ void World::UnloadChunks(Scop::Vec2i current_chunk_position) void World::GenerateWorld(Scop::Vec2i current_chunk_position) { m_generation_status = GenerationState::Working; - std::vector> futures; - for(std::int32_t x = current_chunk_position.x - RENDER_DISTANCE_HALF; x <= current_chunk_position.x + RENDER_DISTANCE_HALF; x++) + for(std::int32_t x = current_chunk_position.x - RENDER_DISTANCE; x <= current_chunk_position.x + RENDER_DISTANCE; x++) { - for(std::int32_t z = current_chunk_position.y - RENDER_DISTANCE_HALF; z <= current_chunk_position.y + RENDER_DISTANCE_HALF; z++) + for(std::int32_t z = current_chunk_position.y - RENDER_DISTANCE; z <= current_chunk_position.y + RENDER_DISTANCE; z++) { auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z }); if(res.second) { - futures.push_back(std::async(std::launch::async, &Chunk::GenerateChunk, &res.first->second)); + res.first->second.GenerateChunk(); if(!res.first->second.GetActor()) m_chunks_to_upload.Push(res.first->second); } } } - for(auto& future: futures) - future.wait(); m_generation_status = GenerationState::Finished; } @@ -95,9 +92,9 @@ void World::Upload() Scop::RenderCore::Get().ShouldStackSubmits(true); for(std::size_t i = 0; i < CHUNKS_UPLOAD_PER_FRAME && !m_chunks_to_upload.IsEmpty(); i++) { - auto chunk = m_chunks_to_upload.Pop().get(); - chunk.GenerateMesh(); - chunk.UploadMesh(); + auto chunk = m_chunks_to_upload.Pop(); + chunk.get().GenerateMesh(); + chunk.get().UploadMesh(); } Scop::RenderCore::Get().WaitQueueIdle(KVF_GRAPHICS_QUEUE); Scop::RenderCore::Get().ShouldStackSubmits(false); diff --git a/Application/World.h b/Application/World.h index 243d74f..b496634 100644 --- a/Application/World.h +++ b/Application/World.h @@ -10,7 +10,6 @@ #include constexpr std::uint8_t RENDER_DISTANCE = 10; -constexpr std::uint8_t RENDER_DISTANCE_HALF = RENDER_DISTANCE / 2; constexpr std::uint8_t CHUNKS_UPLOAD_PER_FRAME = 1; enum class GenerationState: std::uint8_t @@ -40,7 +39,6 @@ class World std::unordered_map m_chunks; ThreadSafeQueue> m_chunks_to_upload; std::shared_ptr p_block_material; - Scop::Narrator& m_narrator; Scop::Scene& m_scene; Scop::Vec2i m_previous_chunk_position; std::atomic m_generation_status = GenerationState::Ready; diff --git a/ScopEngine/Runtime/Includes/Graphics/Scene.h b/ScopEngine/Runtime/Includes/Graphics/Scene.h index 0a25997..c058264 100644 --- a/ScopEngine/Runtime/Includes/Graphics/Scene.h +++ b/ScopEngine/Runtime/Includes/Graphics/Scene.h @@ -66,8 +66,8 @@ namespace Scop void SwitchToParent() const noexcept; [[nodiscard]] inline ForwardData& GetForwardData() noexcept { return m_forward; } - [[nodiscard]] inline const std::vector>& GetActors() const noexcept { return m_actors; } - [[nodiscard]] inline const std::vector>& GetSprites() const noexcept { return m_sprites; } + [[nodiscard]] inline const std::vector& GetActors() const noexcept { return m_actors; } + [[nodiscard]] inline const std::vector& GetSprites() const noexcept { return m_sprites; } [[nodiscard]] inline const std::string& GetName() const noexcept { return m_name; } [[nodiscard]] inline GraphicPipeline& GetPipeline() noexcept { return m_pipeline; } [[nodiscard]] inline std::shared_ptr GetCamera() const { return m_descriptor.camera; } @@ -90,9 +90,9 @@ namespace Scop DepthImage m_depth; SceneDescriptor m_descriptor; std::shared_ptr p_skybox; - std::vector> m_actors; - std::vector> m_sprites; - std::vector> m_narrators; + std::vector m_actors; + std::vector m_sprites; + std::vector m_narrators; std::vector m_scene_children; std::string m_name; NonOwningPtr p_parent; diff --git a/ScopEngine/Runtime/Includes/Renderer/RenderCore.h b/ScopEngine/Runtime/Includes/Renderer/RenderCore.h index 1f0aaa2..11559ae 100644 --- a/ScopEngine/Runtime/Includes/Renderer/RenderCore.h +++ b/ScopEngine/Runtime/Includes/Renderer/RenderCore.h @@ -12,7 +12,7 @@ namespace Scop { - constexpr const int MAX_FRAMES_IN_FLIGHT = 2; + constexpr const int MAX_FRAMES_IN_FLIGHT = 3; constexpr const int DEFAULT_VERTEX_SHADER_ID = 0; constexpr const int DEFAULT_FRAGMENT_SHADER_ID = 1; diff --git a/ScopEngine/Runtime/Sources/Graphics/Scene.cpp b/ScopEngine/Runtime/Sources/Graphics/Scene.cpp index 3a38b46..18eaef0 100644 --- a/ScopEngine/Runtime/Sources/Graphics/Scene.cpp +++ b/ScopEngine/Runtime/Sources/Graphics/Scene.cpp @@ -24,49 +24,37 @@ namespace Scop Actor& Scene::CreateActor(Model model) noexcept { - std::shared_ptr actor = std::make_shared(std::move(model)); - m_actors.push_back(actor); - return *actor; + return m_actors.emplace_back(std::move(model)); } Actor& Scene::CreateActor(std::string_view name, Model model) { - std::shared_ptr actor = std::make_shared(std::move(model)); - m_actors.push_back(actor); - return *actor; + return m_actors.emplace_back(std::move(model)); } Narrator& Scene::CreateNarrator() noexcept { - std::shared_ptr narrator = std::make_shared(); - m_narrators.push_back(narrator); - return *narrator; + return m_narrators.emplace_back(); } Narrator& Scene::CreateNarrator(std::string_view name) { - std::shared_ptr narrator = std::make_shared(); - m_narrators.push_back(narrator); - return *narrator; + return m_narrators.emplace_back(); } Sprite& Scene::CreateSprite(std::shared_ptr texture) noexcept { - std::shared_ptr sprite = std::make_shared(texture); - m_sprites.push_back(sprite); - return *sprite; + return m_sprites.emplace_back(texture); } Sprite& Scene::CreateSprite(std::string_view name, std::shared_ptr texture) { - std::shared_ptr sprite = std::make_shared(texture); - m_sprites.push_back(sprite); - return *sprite; + return m_sprites.emplace_back(texture); } void Scene::RemoveActor(Actor& actor) noexcept { - auto it = std::find_if(m_actors.begin(), m_actors.end(), [actor](const std::shared_ptr lhs) { return actor.GetUUID() == lhs->GetUUID(); }); + auto it = std::find_if(m_actors.begin(), m_actors.end(), [actor](const Actor& lhs) { return actor.GetUUID() == lhs.GetUUID(); }); if(it == m_actors.end()) { Error("Actor not found"); @@ -77,7 +65,7 @@ namespace Scop void Scene::RemoveNarrator(Narrator& narrator) noexcept { - auto it = std::find_if(m_narrators.begin(), m_narrators.end(), [narrator](const std::shared_ptr lhs) { return narrator.GetUUID() == lhs->GetUUID(); }); + auto it = std::find_if(m_narrators.begin(), m_narrators.end(), [narrator](const Narrator& lhs) { return narrator.GetUUID() == lhs.GetUUID(); }); if(it == m_narrators.end()) { Error("Narrator not found"); @@ -88,7 +76,7 @@ namespace Scop void Scene::RemoveSprite(Sprite& sprite) noexcept { - auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [sprite](const std::shared_ptr lhs) { return sprite.GetUUID() == lhs->GetUUID(); }); + auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [sprite](const Sprite& lhs) { return sprite.GetUUID() == lhs.GetUUID(); }); if(it == m_sprites.end()) { Error("Sprite not found"); @@ -148,11 +136,11 @@ namespace Scop void Scene::Update(Inputs& input, float timestep, float aspect) { for(auto actor : m_actors) - actor->Update(this, input, timestep); + actor.Update(this, input, timestep); for(auto narrator : m_narrators) - narrator->Update(this, input, timestep); + narrator.Update(this, input, timestep); for(auto sprite : m_sprites) - sprite->Update(this, input, timestep); + sprite.Update(this, input, timestep); if(m_descriptor.camera) m_descriptor.camera->Update(input, aspect, timestep); } diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/2DPass.cpp index 4d96b54..de2e030 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -85,18 +85,18 @@ namespace Scop VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); m_pipeline.BindPipeline(cmd, 0, {}); - for(auto sprite : scene.GetSprites()) + for(const auto& sprite : scene.GetSprites()) { SpriteData sprite_data; - sprite_data.position = Vec2f{ static_cast(sprite->GetPosition().x), static_cast(sprite->GetPosition().y) }; - sprite_data.color = sprite->GetColor(); - if(!sprite->IsSetInit()) - sprite->UpdateDescriptorSet(*p_texture_set); - sprite->Bind(frame_index, cmd); - std::array sets = { p_viewer_data_set->GetSet(frame_index), sprite->GetSet(frame_index) }; + sprite_data.position = Vec2f{ static_cast(sprite.GetPosition().x), static_cast(sprite.GetPosition().y) }; + sprite_data.color = sprite.GetColor(); + if(!sprite.IsSetInit()) + const_cast(sprite).UpdateDescriptorSet(*p_texture_set); + const_cast(sprite).Bind(frame_index, cmd); + std::array sets = { p_viewer_data_set->GetSet(frame_index), sprite.GetSet(frame_index) }; RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data); - sprite->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); + sprite.GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); } m_pipeline.EndPipeline(cmd); } diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp index e7c95e1..e252a80 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp @@ -43,19 +43,19 @@ namespace Scop VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); pipeline.BindPipeline(cmd, 0, {}); - for(auto actor : scene.GetActors()) + for(const auto& actor : scene.GetActors()) { - if(!actor->IsVisible()) + if(!actor.IsVisible()) continue; ModelData model_data; model_data.model_mat = Mat4f::Identity(); - model_data.model_mat.SetTranslation(actor->GetPosition() - actor->GetModel().GetCenter()); - model_data.model_mat.SetScale(actor->GetScale()); - model_data.model_mat = Mat4f::Translate(-actor->GetModel().GetCenter()) * Mat4f::Rotate(actor->GetOrientation()) * model_data.model_mat; + model_data.model_mat.SetTranslation(actor.GetPosition() - actor.GetModel().GetCenter()); + model_data.model_mat.SetScale(actor.GetScale()); + model_data.model_mat = Mat4f::Translate(-actor.GetModel().GetCenter()) * Mat4f::Rotate(actor.GetOrientation()) * model_data.model_mat; model_data.normal_mat = model_data.model_mat; model_data.normal_mat.Inverse().Transpose(); RenderCore::Get().vkCmdPushConstants(cmd, pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ModelData), &model_data); - actor->GetModel().Draw(cmd, *data.matrices_set, pipeline, *data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex()); + actor.GetModel().Draw(cmd, *data.matrices_set, pipeline, *data.albedo_set, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef(), renderer.GetCurrentFrameIndex()); } pipeline.EndPipeline(cmd); }