Skip to content

Commit 85bdd49

Browse files
author
Dan Liebault
committed
Amélioration de la gestion des chunks en parallèle
Ajout de la synchronisation des threads avec `<barrier>`, initialisation de `mp_terrain_generator` dans le constructeur de `ChunkManager`, et préallocation de mémoire pour les chunks dans `CreateWorld`. Remplacement de `CreateNewChunk` par une insertion dans une `std::map` et création de chunks en parallèle. Introduction de la méthode `create_chunk` pour encapsuler la logique de création de chunks, déclarée comme privée dans `ChunkManager.h`.
1 parent f9d84c3 commit 85bdd49

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ChunkManager.h"
22

33
#include <utility>
4+
#include <barrier>
45

56
ChunkManager::ChunkManager(std::shared_ptr<GameObject> pworld, std::shared_ptr<Material> p_world_mat, std::shared_ptr<Camera> p_camera) :
67
mp_world(std::move(pworld)), mp_world_mat(std::move(p_world_mat)), m_worldmenu(m_load_radius)
@@ -13,17 +14,44 @@ ChunkManager::ChunkManager(std::shared_ptr<GameObject> pworld, std::shared_ptr<M
1314

1415
void ChunkManager::CreateWorld()
1516
{
17+
// preallocate memory for chunks
18+
std::map<ChunkKey, std::unique_ptr<Chunk>> chunks;
1619
for (int32_t x = m_render_min.x; x <= m_render_max.x; x++)
1720
{
1821
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
1922
{
2023
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
2124
{
22-
CreateNewChunk(x, y, z);
25+
const ChunkKey key = { .x = x, .y = y, .z = z};
26+
chunks.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, nullptr));
2327
}
2428
}
2529
}
2630

31+
// compute chunks in parallel
32+
const int64_t num_threads = (m_render_max.x - m_render_min.x) + 2;
33+
std::barrier sync(num_threads, [this, &chunks]() noexcept { m_chunk_map = std::move(chunks); });
34+
35+
for (int32_t x = m_render_min.x; x <= m_render_max.x; x++)
36+
{
37+
std::thread t([&chunks, x, &sync, this]() {
38+
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
39+
{
40+
41+
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
42+
{
43+
// CreateNewChunk(x, y, z);
44+
chunks.at({ x, y, z }) = create_chunk(x, y, z);
45+
}
46+
}
47+
48+
sync.arrive_and_wait();
49+
});
50+
t.detach();
51+
}
52+
53+
sync.arrive_and_wait(); // wait for all chunks to be created
54+
2755
for (int32_t x = m_render_min.x; x <= m_render_max.x; x++)
2856
{
2957
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
@@ -194,3 +222,10 @@ WorldMenu& ChunkManager::GetMenu()
194222
{
195223
return m_worldmenu;
196224
}
225+
226+
std::unique_ptr<Chunk> ChunkManager::create_chunk(const int32_t& x, const int32_t& y, const int32_t& z) const
227+
{
228+
const ChunkKey key = { .x = x, .y = y, .z = z};
229+
auto p_chunk = mp_terrain_generator->compute_world_chunk(x, y, z);
230+
return std::move(p_chunk);
231+
}

R3DVoxel/VoxelEngine/ChunkManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ChunkManager
2424
WorldMenu& GetMenu();
2525

2626
private:
27+
std::unique_ptr<Chunk> create_chunk(const int32_t& x, const int32_t& y, const int32_t& z) const;
28+
2729
std::unique_ptr<TerrainGenerator> mp_terrain_generator;
2830

2931
std::shared_ptr<GameObject> mp_world;

0 commit comments

Comments
 (0)