From 86f906912eb3bd8421a451c49fa5e1d4c21839be Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 13:02:10 +0200 Subject: [PATCH] renderer: also delete meshes of old chunks --- include/renderer.hpp | 3 +++ src/chunkmanager.cpp | 1 + src/renderer.cpp | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/renderer.hpp b/include/renderer.hpp index 2b560c9..c0cdcba 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -39,6 +39,7 @@ namespace renderer{ } } RenderInfo; + typedef oneapi::tbb::concurrent_queue IndexQueue; void init(GLFWwindow* window); void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info); @@ -51,6 +52,8 @@ namespace renderer{ Shader* getRenderShader(); ChunkMeshDataQueue& getMeshDataQueue(); + IndexQueue& getDeleteIndexQueue(); + }; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index fd76077..82f3caf 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -116,6 +116,7 @@ namespace chunkmanager // Using the key doesn't work if(chunks.erase(a)){ nUnloaded++; + renderer::getDeleteIndexQueue().push(index); delete c; } else { c->setState(Chunk::CHUNK_STATE_IN_DELETING_QUEUE, false); diff --git a/src/renderer.cpp b/src/renderer.cpp index ed20533..5daa398 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -17,12 +17,14 @@ namespace renderer{ RenderTable ChunksToRender; ChunkMeshDataQueue MeshDataQueue; + IndexQueue MeshDataToDelete; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + IndexQueue& getDeleteIndexQueue(){ return MeshDataToDelete; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; @@ -139,6 +141,7 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); + /* Process incoming mesh data */ ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ RenderTable::accessor a; @@ -161,7 +164,18 @@ namespace renderer{ chunkmesher::getMeshDataQueue().push(m); } - // TODO: implement removal of chunks from rendering + /* Process chunks to be removed */ + chunk_index_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