From 5dc05167de6872eceefa067306247185c3d9e522 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Wed, 15 Dec 2021 21:00:06 -0500 Subject: [PATCH] Le DeleteChunk est maintenant en multi-thread et peux enfin keep up avec le reste! --- SQCSim2021/chunk.cpp | 4 ++++ SQCSim2021/chunk.h | 2 ++ SQCSim2021/define.h | 2 ++ SQCSim2021/engine.cpp | 3 +++ SQCSim2021/vertexbuffer.cpp | 10 +++++++--- SQCSim2021/vertexbuffer.h | 2 ++ SQCSim2021/world.cpp | 36 ++++++++++++++++++++++++++++++------ SQCSim2021/world.h | 1 + 8 files changed, 51 insertions(+), 9 deletions(-) diff --git a/SQCSim2021/chunk.cpp b/SQCSim2021/chunk.cpp index 2942b36..5423526 100644 --- a/SQCSim2021/chunk.cpp +++ b/SQCSim2021/chunk.cpp @@ -130,6 +130,10 @@ void Chunk::FlushMeshToVBO() { delete[] m_vd; } +void Chunk::FlushVBO() { + m_vertexBuffer.Flush(); +} + void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) { float u, v, s; // Update mesh diff --git a/SQCSim2021/chunk.h b/SQCSim2021/chunk.h index e578999..fe7afad 100644 --- a/SQCSim2021/chunk.h +++ b/SQCSim2021/chunk.h @@ -36,6 +36,8 @@ class Chunk { void Update(BlockInfo* blockinfo[BTYPE_LAST], World* world); void FlushMeshToVBO(); + + void FlushVBO(); void Render() const; bool IsDirty() const; diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index 4b05c2d..63987e3 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -28,6 +28,7 @@ #define THREADS_GENERATE_CHUNKS 1 #define THREADS_UPDATE_CHUNKS 1 +#define THREADS_DELETE_CHUNKS 1 #define VIEW_DISTANCE 256 #define TEXTURE_SIZE 128 @@ -44,6 +45,7 @@ #define THREADS_GENERATE_CHUNKS 12 #define THREADS_UPDATE_CHUNKS 8 +#define THREADS_DELETE_CHUNKS 10 #define VIEW_DISTANCE 1024 #define TEXTURE_SIZE 512 diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 70bfe0f..f35392e 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -120,6 +120,9 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) { ss << " Rendered Chunks : " << m_renderCount; PrintText(10, Height() - 35, ss.str()); ss.str(""); + ss << " To-Be-Deleted Chunks : " << m_world.GettbDeleted(); + PrintText(10, Height() - 45, ss.str()); + ss.str(""); ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l ’ operateur << pour afficher la position PrintText(10, 10, ss.str()); ss.str(""); diff --git a/SQCSim2021/vertexbuffer.cpp b/SQCSim2021/vertexbuffer.cpp index ff48360..e8f2d31 100644 --- a/SQCSim2021/vertexbuffer.cpp +++ b/SQCSim2021/vertexbuffer.cpp @@ -16,10 +16,7 @@ bool VertexBuffer::IsValid() const { return m_isValid; } -//std::mutex VertexBuffer::m_opgl; - void VertexBuffer::SetMeshData(VertexData* vd, int vertexCount) { - //const std::lock_guard prout(VertexBuffer::m_opgl); assert(vertexCount <= USHRT_MAX); if(vertexCount == 0) return; @@ -77,3 +74,10 @@ int VertexBuffer::Count() const { return m_vertexCount; } +void VertexBuffer::Flush() { + if (m_isValid) { + glDeleteBuffers(1, &m_vertexVboId); + glDeleteBuffers(1, &m_indexVboId); + } + m_isValid = false; +} diff --git a/SQCSim2021/vertexbuffer.h b/SQCSim2021/vertexbuffer.h index eed8811..b44f8f8 100644 --- a/SQCSim2021/vertexbuffer.h +++ b/SQCSim2021/vertexbuffer.h @@ -31,6 +31,8 @@ class VertexBuffer int Count() const; + void Flush(); + private: diff --git a/SQCSim2021/world.cpp b/SQCSim2021/world.cpp index 60f09af..594be18 100644 --- a/SQCSim2021/world.cpp +++ b/SQCSim2021/world.cpp @@ -54,7 +54,7 @@ void World::TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]) { if (ax - x >= 0) { m_chunks.Set(ax - x, ay, m_chunks.Remove(ax, ay)); - if (ax == WORLD_SIZE_X - 1 && m_chunks.Get(ax - x, ay)) + if (ax == WORLD_SIZE_X - 1 && m_chunks.Get(ax - x, ay)) m_chunks.Get(ax - x, ay)->MakeDirty(); } else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay)); @@ -65,7 +65,7 @@ void World::TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]) { if (ax - x < WORLD_SIZE_X) { m_chunks.Set(ax - x, ay, m_chunks.Remove(ax, ay)); - if (ax == 0 && m_chunks.Get(ax - x, ay)) + if (ax == 0 && m_chunks.Get(ax - x, ay)) m_chunks.Get(ax - x, ay)->MakeDirty(); } else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay)); @@ -77,7 +77,7 @@ void World::TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]) { if (ay - y >= 0) { m_chunks.Set(ax, ay - y, m_chunks.Remove(ax, ay)); - if (ay == WORLD_SIZE_Y - 1 && m_chunks.Get(ax, ay - y)) + if (ay == WORLD_SIZE_Y - 1 && m_chunks.Get(ax, ay - y)) m_chunks.Get(ax, ay - y)->MakeDirty(); } else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay)); @@ -88,7 +88,7 @@ void World::TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]) { if (ay - y < WORLD_SIZE_Y) { m_chunks.Set(ax, ay - y, m_chunks.Remove(ax, ay)); - if (ay == 0 && m_chunks.Get(ax, ay - y)) + if (ay == 0 && m_chunks.Get(ax, ay - y)) m_chunks.Get(ax, ay - y)->MakeDirty(); } else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay)); @@ -109,6 +109,11 @@ void World::CleanUpWorld(int& deleteframes, bool clear = false) { } } if (!m_tbDeleted.empty() && !deleteframes) { + int deleted = 0; + while (deleted < THREADS_DELETE_CHUNKS) { + + + } delete m_tbDeleted.back(); m_tbDeleted.pop_back(); deleteframes = FRAMES_DELETE_CHUNKS; @@ -302,6 +307,7 @@ void World::UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTY int threads = 0; std::future genThList[THREADS_GENERATE_CHUNKS]; std::future updateThList[THREADS_UPDATE_CHUNKS]; + std::future delThList[THREADS_DELETE_CHUNKS]; if (frameGenerate > 0) --frameGenerate; if (frameUpdate > 0) --frameUpdate; @@ -446,5 +452,23 @@ void World::UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTY } } - CleanUpWorld(frameDelete); -} \ No newline at end of file + threads = 0; + + while (!m_tbDeleted.empty() && !frameDelete) { + if (m_tbDeleted.back()) { + m_tbDeleted.back()->FlushVBO(); + delThList[threads] = + std::async(std::launch::async, + [](Chunk* chunk) { delete chunk; }, m_tbDeleted.back()); + m_tbDeleted.pop_back(); + if (++threads > THREADS_DELETE_CHUNKS) frameDelete = FRAMES_DELETE_CHUNKS; + } + else m_tbDeleted.pop_back(); + } + + for (int x = 0; x < threads; ++x) + delThList[x].wait(); + +} + +int World::GettbDeleted() const { return m_tbDeleted.size(); } \ No newline at end of file diff --git a/SQCSim2021/world.h b/SQCSim2021/world.h index 9d87348..5c7c82f 100644 --- a/SQCSim2021/world.h +++ b/SQCSim2021/world.h @@ -40,6 +40,7 @@ public: void ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block); void ChangeBlockAtPosition(BlockType blockType, Vector3f pos); void CleanUpWorld(int& deleteframes, bool clear); + int GettbDeleted() const; private: Array2d m_chunks = Array2d(WORLD_SIZE_X, WORLD_SIZE_Y); std::vector m_tbDeleted;