mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-12 23:23:35 +00:00
adding async chunk generation
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
#include <Block.h>
|
||||
#include <World.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#define CHUNK_POS_TO_INDEX(px, py, pz) (px * CHUNK_SIZE.x * CHUNK_SIZE.z + pz * CHUNK_SIZE.z + py)
|
||||
|
||||
Chunk::Chunk(World& world, Scop::Vec2i offset) : m_data(CHUNK_VOLUME, 0), m_offset(offset), m_position(std::move(offset) * Scop::Vec2i{ CHUNK_SIZE.x, CHUNK_SIZE.z }), m_world(world)
|
||||
@@ -21,7 +19,7 @@ void Chunk::GenerateChunk()
|
||||
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
|
||||
{
|
||||
// Implement noise here
|
||||
std::uint32_t height = 4 + std::sin(x) + std::cos(z);
|
||||
std::uint32_t height = std::min(static_cast<std::uint32_t>(4 + (std::sin(x) + std::cos(z)) * 2), CHUNK_SIZE.y);
|
||||
for(std::uint32_t y = 0; y < height; y++)
|
||||
m_data[CHUNK_POS_TO_INDEX(x, y, z)] = 1;
|
||||
}
|
||||
@@ -125,7 +123,7 @@ void Chunk::UploadMesh()
|
||||
mesh->AddSubMesh({ std::move(m_mesh_data), std::move(m_mesh_index_data) });
|
||||
|
||||
Scop::Actor& actor = m_world.GetScene().CreateActor(mesh);
|
||||
//actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
|
||||
actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
|
||||
actor.SetScale(Scop::Vec3f{ 2.0f });
|
||||
actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
|
||||
p_actor = &actor;
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
#define CHUNK_H
|
||||
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
|
||||
#include <ScopGraphics.h>
|
||||
#include <ScopMaths.h>
|
||||
|
||||
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 32 };
|
||||
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 32, 32 };
|
||||
constexpr std::uint32_t CHUNK_VOLUME = CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z;
|
||||
|
||||
class Chunk
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <unistd.h>
|
||||
#include <filesystem>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <ScopMaths.h>
|
||||
|
||||
@@ -42,4 +45,36 @@ namespace std
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class ThreadSafeQueue
|
||||
{
|
||||
public:
|
||||
inline void Push(T item)
|
||||
{
|
||||
const std::unique_lock<std::mutex> lock(m_mutex);
|
||||
m_queue.push(item);
|
||||
m_cond.notify_one();
|
||||
}
|
||||
|
||||
inline T Pop()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
m_cond.wait(lock, [this]() { return !m_queue.empty(); });
|
||||
T item = m_queue.front();
|
||||
m_queue.pop();
|
||||
return item;
|
||||
}
|
||||
|
||||
bool IsEmpty() const
|
||||
{
|
||||
const std::unique_lock<std::mutex> lock(m_mutex);
|
||||
return m_queue.empty();
|
||||
}
|
||||
|
||||
private:
|
||||
std::queue<T> m_queue;
|
||||
mutable std::mutex m_mutex;
|
||||
std::condition_variable m_cond;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <future>
|
||||
|
||||
#include <ScopCore.h>
|
||||
|
||||
#include <World.h>
|
||||
@@ -7,13 +9,32 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrat
|
||||
{
|
||||
Scop::Vec2ui32 map_size;
|
||||
Scop::MaterialTextures material_params;
|
||||
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "prototype.bmp", map_size), map_size.x, map_size.y);
|
||||
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "dirt.bmp", map_size), map_size.x, map_size.y);
|
||||
p_block_material = std::make_shared<Scop::Material>(material_params);
|
||||
|
||||
auto narrator_update = [this](Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& input, float delta)
|
||||
{
|
||||
GenerateWorld();
|
||||
Upload();
|
||||
Scop::FirstPerson3D* camera = reinterpret_cast<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
|
||||
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
|
||||
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(CHUNK_SIZE.z);
|
||||
Scop::Vec2i current_chunk_position{ x_chunk, z_chunk };
|
||||
|
||||
if(m_generation_status != GenerationState::Ready || current_chunk_position != m_previous_chunk_position)
|
||||
{
|
||||
if(m_generation_status == GenerationState::Ready)
|
||||
{
|
||||
UnloadChunks(current_chunk_position);
|
||||
auto _ = std::async(std::launch::async, &World::GenerateWorld, this, current_chunk_position);
|
||||
m_generation_status = GenerationState::Working;
|
||||
}
|
||||
else if(m_generation_status == GenerationState::Finished)
|
||||
{
|
||||
m_generation_status = GenerationState::Ready;
|
||||
m_previous_chunk_position = current_chunk_position;
|
||||
}
|
||||
}
|
||||
if(m_generation_status != GenerationState::Working)
|
||||
Upload();
|
||||
};
|
||||
|
||||
m_narrator.AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
|
||||
@@ -27,23 +48,14 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrat
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void World::GenerateWorld()
|
||||
void World::UnloadChunks(Scop::Vec2i current_chunk_position)
|
||||
{
|
||||
Scop::FirstPerson3D* camera = reinterpret_cast<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
|
||||
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
|
||||
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(CHUNK_SIZE.z);
|
||||
Scop::Vec2i current_chunk_position{ x_chunk, z_chunk };
|
||||
|
||||
if(current_chunk_position == m_previous_chunk_position)
|
||||
return;
|
||||
|
||||
for(auto it = m_chunks.begin(); it != m_chunks.end();)
|
||||
{
|
||||
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 < x_dist || RENDER_DISTANCE < z_dist)
|
||||
if(RENDER_DISTANCE_HALF < x_dist || RENDER_DISTANCE_HALF < z_dist)
|
||||
{
|
||||
if(it->second.GetActor())
|
||||
m_scene.RemoveActor(*it->second.GetActor());
|
||||
@@ -52,25 +64,41 @@ void World::GenerateWorld()
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
for(std::int32_t x = x_chunk - RENDER_DISTANCE; x <= x_chunk + RENDER_DISTANCE; x++)
|
||||
void World::GenerateWorld(Scop::Vec2i current_chunk_position)
|
||||
{
|
||||
m_generation_status = GenerationState::Working;
|
||||
std::vector<std::future<void>> 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 z = z_chunk - RENDER_DISTANCE; z <= z_chunk + RENDER_DISTANCE; z++)
|
||||
for(std::int32_t z = current_chunk_position.y - RENDER_DISTANCE_HALF; z <= current_chunk_position.y + RENDER_DISTANCE_HALF; z++)
|
||||
{
|
||||
auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z });
|
||||
if(res.second)
|
||||
res.first->second.GenerateChunk();
|
||||
{
|
||||
futures.push_back(std::async(std::launch::async, &Chunk::GenerateChunk, &res.first->second));
|
||||
if(!res.first->second.GetActor())
|
||||
m_chunks_to_upload.Push(res.first->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& chunk : m_chunks)
|
||||
chunk.second.GenerateMesh();
|
||||
|
||||
m_previous_chunk_position = current_chunk_position;
|
||||
for(auto& future: futures)
|
||||
future.wait();
|
||||
m_generation_status = GenerationState::Finished;
|
||||
}
|
||||
|
||||
void World::Upload()
|
||||
{
|
||||
for(auto& chunk : m_chunks)
|
||||
chunk.second.UploadMesh();
|
||||
if(m_chunks_to_upload.IsEmpty())
|
||||
return;
|
||||
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();
|
||||
}
|
||||
Scop::RenderCore::Get().WaitQueueIdle(KVF_GRAPHICS_QUEUE);
|
||||
Scop::RenderCore::Get().ShouldStackSubmits(false);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef WORLD_H
|
||||
#define WORLD_H
|
||||
|
||||
#include <atomic>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <ScopGraphics.h>
|
||||
@@ -8,7 +9,16 @@
|
||||
#include <Chunk.h>
|
||||
#include <Utils.h>
|
||||
|
||||
constexpr std::uint8_t RENDER_DISTANCE = 1;
|
||||
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
|
||||
{
|
||||
Ready,
|
||||
Working,
|
||||
Finished,
|
||||
};
|
||||
|
||||
class World
|
||||
{
|
||||
@@ -22,15 +32,18 @@ class World
|
||||
~World() = default;
|
||||
|
||||
private:
|
||||
void GenerateWorld();
|
||||
void UnloadChunks(Scop::Vec2i current_chunk_position);
|
||||
void GenerateWorld(Scop::Vec2i current_chunk_position);
|
||||
void Upload();
|
||||
|
||||
private:
|
||||
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
|
||||
ThreadSafeQueue<std::reference_wrapper<Chunk>> m_chunks_to_upload;
|
||||
std::shared_ptr<Scop::Material> p_block_material;
|
||||
Scop::Narrator& m_narrator;
|
||||
Scop::Scene& m_scene;
|
||||
Scop::Vec2i m_previous_chunk_position;
|
||||
std::atomic<GenerationState> m_generation_status = GenerationState::Ready;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user