From 4c46811a250e547f0db6511d34bfb84a36cce996 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Tue, 3 Oct 2023 16:02:49 +0200 Subject: [PATCH] delete meshes relative to deleted chunks --- include/renderer.hpp | 13 +++++++++++++ src/chunkmanager.cpp | 15 ++++----------- src/debugwindow.cpp | 11 ++++------- src/renderer.cpp | 20 +++++++++++++++----- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/include/renderer.hpp b/include/renderer.hpp index 33549d9..cbe0b36 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -26,8 +26,20 @@ namespace renderer{ buffers_allocated=true; } + + void deallocateBuffers(){ + // Allocate buffers + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &extentsBuffer); + glDeleteBuffers(1, &texinfoBuffer); + glDeleteVertexArrays(1, &VAO); + + buffers_allocated=false; + } } RenderInfo; + typedef oneapi::tbb::concurrent_queue IndexQueue; + void init(GLFWwindow* window); void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info); void render(); @@ -39,6 +51,7 @@ namespace renderer{ Shader* getRenderShader(); ChunkMeshDataQueue& getMeshDataQueue(); + IndexQueue& getDeleteIndexQueue(); }; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 4a1e8cf..3bff859 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -108,6 +108,7 @@ namespace chunkmanager Chunk::Chunk* c = a->second; if(chunks.erase(a)){ nUnloaded++; + renderer::getDeleteIndexQueue().push(index); delete c; } else std::cout << "failed to delete " << index << std::endl; } else std::cout << "no such element found to delete\n"; @@ -200,17 +201,9 @@ namespace chunkmanager }); - /*std::cout << "time: " << glfwGetTime() << "\ntotal: " << chunks.size() << "\ngenerated: " << nGenerated << "\nmeshed: " - << nMeshed << "\nunloaded from prev loop: " << nUnloaded << "\nnew marked for - unload: " << nMarkUnload << std::endl; - - debug::window::set_parameter("update_chunks_total", (int) chunks.size()); - debug::window::set_parameter("update_chunks_buckets", (int) chunks.bucket_count()); - debug::window::set_parameter("update_chunks_freed", nUnloaded); - debug::window::set_parameter("update_chunks_generated", nGenerated); - debug::window::set_parameter("update_chunks_meshed", nMeshed); - debug::window::set_parameter("update_chunks_explored", nExplored); - debug::window::set_parameter("update_chunks_tobedeleted", 0);*/ + std::cout << "time: " << glfwGetTime() << "\ntotal: " << chunks.size() << "\ngenerated: " << nGenerated << "\nmeshed: " + << nMeshed << "\nunloaded from prev loop: " << nUnloaded << "\nnew marked for unload: " << nMarkUnload << std::endl; + } } diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp index f00bc18..b0d6f94 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -99,18 +99,15 @@ namespace debug{ } if(ImGui::CollapsingHeader("Mesh")){ - ImGui::Text("Total chunks updated: %d", + ImGui::Text("Total chunk meshed: %d", std::any_cast(parameters.at("render_chunks_total"))); + ImGui::Text("Of which renderable (not empty): %d", + std::any_cast(parameters.at("render_chunks_renderable"))); ImGui::Text("Chunks rendered: %d", std::any_cast(parameters.at("render_chunks_rendered"))); ImGui::Text("Frustum culled: %d", std::any_cast(parameters.at("render_chunks_culled"))); - ImGui::Text("Chunks out of view: %d", - std::any_cast(parameters.at("render_chunks_oof"))); - if(parameters.find("render_chunks_deleted") != parameters.end()) - ImGui::Text("Chunks deleted: %d", - std::any_cast(parameters.at("render_chunks_deleted"))); - ImGui::Text("Vertices in the scene: %d", + ImGui::Text("Total vertices in the scene: %d", std::any_cast(parameters.at("render_chunks_vertices"))); ImGui::Checkbox("Wireframe", std::any_cast(parameters.at("wireframe_return"))); diff --git a/src/renderer.cpp b/src/renderer.cpp index e15c197..1e7b70f 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -18,6 +18,7 @@ namespace renderer{ RenderTable ChunksToRender; ChunkMeshDataQueue MeshDataQueue; + IndexQueue MeshDataToDelete; Shader* theShader, *quadShader; GLuint chunkTexture; @@ -30,6 +31,7 @@ namespace renderer{ Shader* getRenderShader() { return theShader; } ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + IndexQueue& getDeleteIndexQueue(){ return MeshDataToDelete; } void init(GLFWwindow* window){ // Setup rendering @@ -140,8 +142,6 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); - // TODO: works but some stuff is rendered wrong (trees floating or inside the terrain, - // missing or malformed chunks) ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ RenderTable::accessor a; @@ -166,17 +166,26 @@ namespace renderer{ } // TODO: implement removal of chunks from rendering - std::cout << "chunks to render: " << ChunksToRender.size(); + int32_t queue_index; + while(MeshDataToDelete.try_pop(queue_index)){ + RenderTable::accessor a; + if(ChunksToRender.find(a, queue_index)){ + RenderInfo* render_info = a->second; + render_info->deallocateBuffers(); + delete render_info; + ChunksToRender.erase(a); + } + } // Render the chunks // parallel_for cannot be used since all the rendering needs to happen in a single thread for(RenderTable::iterator i = ChunksToRender.begin(); i != ChunksToRender.end(); i++){ RenderInfo* render_info = i->second; - total++; if(render_info->num_vertices > 0) { + total++; // Increase total vertex count vertices += render_info->num_vertices; @@ -217,8 +226,9 @@ namespace renderer{ } } - debug::window::set_parameter("render_chunks_total", total); + debug::window::set_parameter("render_chunks_total", (int)(ChunksToRender.size())); debug::window::set_parameter("render_chunks_rendered", toGpu); + debug::window::set_parameter("render_chunks_renderable", total); debug::window::set_parameter("render_chunks_culled", total-toGpu); debug::window::set_parameter("render_chunks_vertices", vertices);