Commit 079888cf authored by Shu's avatar Shu 🍠
Browse files

Robin hood and less leak

parent 23dc6070
......@@ -41,7 +41,7 @@ file(GLOB INCLUDE_SOURCES "include/imgui-1.76/*.cpp" "include/FastNoiseSIMD/*.cp
add_executable(univerxel ${SOURCES} ${INCLUDE_SOURCES})
target_compile_features(univerxel PUBLIC cxx_std_17)
target_link_libraries(univerxel ${ALL_LIBS})
target_include_directories(univerxel PRIVATE "include/imgui-1.76/" "include/FastNoiseSIMD/" "include/toml++/" "include/Remotery/lib/")
target_include_directories(univerxel PRIVATE "include/imgui-1.76/" "include/FastNoiseSIMD/" "include/toml++/" "include/Remotery/lib/" "include/robin_hood")
if(PROFILING)
target_compile_definitions(univerxel PRIVATE RMT_ENABLED=1 RMT_USE_OPENGL=1)
else(PROFILING)
......
......@@ -6,6 +6,7 @@
#include "../data/geometry/Faces.hpp"
#include <memory>
#include <toml.h>
#include <robin_hood.h>
class World;
class Chunk;
......@@ -21,10 +22,10 @@ namespace contouring {
virtual void update(const camera_pos &pos) = 0;
/// Chunk data change
virtual void onUpdate(const chunk_pos &pos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>>& data, Faces neighbors) = 0;
virtual void onUpdate(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>>& data, Faces neighbors) = 0;
/// Chunk existante ping
/// @note notify for chunks entering view while moving
virtual void onNotify(const chunk_pos &pos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) = 0;
virtual void onNotify(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) = 0;
/// Display ImGui config
virtual void onGui() = 0;
/// Get options
......
......@@ -31,7 +31,7 @@ namespace contouring {
return glm::length2(center - point) <= keepDistance * keepDistance;
}
std::unordered_map<chunk_pos, Buffer *> buffers;
robin_hood::unordered_map<chunk_pos, Buffer *> buffers;
chunk_pos center = chunk_pos(INT_MAX);
int loadDistance = 3;
......
......@@ -12,8 +12,8 @@ namespace contouring {
virtual ~Dummy() { }
void update(const camera_pos &) override { }
void onUpdate(const chunk_pos &, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &, Faces) override {}
void onNotify(const chunk_pos &, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &) override { }
void onUpdate(const chunk_pos &, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &, Faces) override {}
void onNotify(const chunk_pos &, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &) override { }
void onGui() override { }
std::string getOptions() override { return ""; }
void getModels(std::vector<std::pair<glm::mat4, Buffer *const>> &, const std::optional<Frustum>&, float) override { }
......
......@@ -35,7 +35,7 @@ namespace contouring {
}
}
void FlatSurroundingBox::enqueue(const chunk_pos &pos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) {
void FlatSurroundingBox::enqueue(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) {
rmt_ScopedCPUSample(EnqueueContouring, RMTSF_Aggregate);
const auto dist2 = glm::length2(pos - center);
if (dist2 <= loadDistance * loadDistance) {
......@@ -46,7 +46,7 @@ namespace contouring {
}
}
void FlatSurroundingBox::onUpdate(const chunk_pos &pos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data, Faces neighbors) {
void FlatSurroundingBox::onUpdate(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data, Faces neighbors) {
enqueue(pos, data);
if (neighbors && Faces::Right)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Right)], data);
......@@ -67,7 +67,7 @@ namespace contouring {
enqueue(pos + g_face_offsets[static_cast<int>(Face::Backward)], data);
}
void FlatSurroundingBox::onNotify(const chunk_pos &pos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) {
void FlatSurroundingBox::onNotify(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &data) {
if (buffers.find(pos) == buffers.end()) {
enqueue(pos, data);
}
......
......@@ -22,10 +22,10 @@ namespace contouring {
void onGui() override;
/// Chunk data change
void onUpdate(const chunk_pos &, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &, Faces) override;
void onUpdate(const chunk_pos &, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &, Faces) override;
/// Chunk existante ping
/// @note notify for chunks entering view while moving
void onNotify(const chunk_pos &, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &) override;
void onNotify(const chunk_pos &, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &) override;
protected:
safe_priority_queue_map<chunk_pos, surrounding::chunks, int> loadQueue;
......@@ -40,7 +40,7 @@ namespace contouring {
bool running = true;
std::vector<std::thread> workers;
void enqueue(const chunk_pos &, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &);
void enqueue(const chunk_pos &, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &);
private:
static inline bool isTransparent(const surrounding::chunks &surrounding, const std::pair<ushort, ushort> &idx);
......
......@@ -3,7 +3,7 @@
#include "../world/Chunk.hpp"
namespace contouring::surrounding {
bool load(chunks &out, const chunk_pos &chunkPos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &chunks) {
bool load(chunks &out, const chunk_pos &chunkPos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &chunks) {
{
const auto it = chunks.find(chunkPos);
if (it == chunks.end())
......
......@@ -2,13 +2,14 @@
#include "../data/geometry/Faces.hpp"
#include <memory>
#include <robin_hood.h>
class Chunk;
namespace contouring::surrounding {
const auto CENTER = 6;
typedef std::array<std::shared_ptr<const Chunk>, CENTER+1> chunks;
bool load(chunks &out, const chunk_pos &chunkPos, const std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &chunks);
bool load(chunks &out, const chunk_pos &chunkPos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> &chunks);
std::pair<ushort, ushort> getNeighborIdx(ushort idx, Face face);
}
......@@ -3,7 +3,7 @@
#include <tuple>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <robin_hood.h>
#include <mutex>
#include <condition_variable>
#include <algorithm>
......@@ -17,7 +17,7 @@ private:
}
std::vector<std::pair<K, W>> heap;
std::unordered_map<K, V> map;
robin_hood::unordered_map<K, V> map;
std::mutex mutex;
std::condition_variable cv;
......@@ -79,7 +79,7 @@ private:
}
std::vector<std::pair<K, W>> heap;
std::unordered_set<K> set;
robin_hood::unordered_set<K> set;
std::mutex mutex;
std::condition_variable cv;
......
......@@ -14,6 +14,8 @@ Chunk::Chunk(const chunk_pos& pos, Generator& rnd) {
voxels[i].Material = voxels[i].Density > 0 ? 1 + std::clamp(static_cast<int>(std::lrint((materialSet[i] + 1) / 2 * (materials::count - 1))),
0, materials::count - 1) : 0; //NOTE: map (approx -1, 1) to (1, mat_max)
}
FastNoiseSIMD::FreeNoiseSet(densitySet);
FastNoiseSIMD::FreeNoiseSet(materialSet);
}
Chunk::~Chunk() { }
......
......@@ -34,7 +34,7 @@ World::LoadPool::~LoadPool() {
}
}
inline void World::LoadPool::push(const chunk_pos &pos, int weight) { loadQueue.push(pos, weight); }
inline bool World::LoadPool::pop(std::pair<chunk_pos, std::shared_ptr<Chunk>> &out) { return loadedQueue.pop(out); }
inline bool World::LoadPool::pop(robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>> &out) { return loadedQueue.pop(out); }
inline size_t World::LoadPool::size() { return loadQueue.size(); }
void World::update(const camera_pos& pos, World::report& rep) {
......@@ -70,7 +70,7 @@ void World::update(const camera_pos& pos, World::report& rep) {
{
rmt_ScopedCPUSample(Unload, 0);
for (size_t i = 0; i < 32 && !unloadQueue.empty(); i++) {
chunks.extract(unloadQueue.pop());
chunks.erase(unloadQueue.pop());
//TODO: save to file
}
}
......@@ -96,7 +96,7 @@ void World::update(const camera_pos& pos, World::report& rep) {
// Loaded chunks
{
rmt_ScopedCPUSample(Load, 0);
std::pair<chunk_pos, std::shared_ptr<Chunk>> loaded;
robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>> loaded;
while (loadPool.pop(loaded)) {
chunks.insert(loaded);
contouring->onUpdate(loaded.first, chunks, Faces::All);
......
......@@ -48,7 +48,7 @@ public:
private:
chunk_pos last_pos = chunk_pos(INT_MAX);
std::unordered_map<chunk_pos, std::shared_ptr<Chunk>> chunks;
robin_hood::unordered_map<chunk_pos, std::shared_ptr<Chunk>> chunks;
/// Generating worker pool
class LoadPool {
......@@ -58,7 +58,7 @@ private:
Generator generator;
inline void push(const chunk_pos &pos, int weight);
inline bool pop(std::pair<chunk_pos, std::shared_ptr<Chunk>> &out);
inline bool pop(robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>> &out);
inline size_t size();
private:
......@@ -66,7 +66,7 @@ private:
bool running = true;
safe_priority_queue<chunk_pos, int> loadQueue;
safe_queue<std::pair<chunk_pos, std::shared_ptr<Chunk>>> loadedQueue;
safe_queue<robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>>> loadedQueue;
};
LoadPool loadPool;
unique_queue<chunk_pos> unloadQueue;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment