Skip to content

Commit

Permalink
flood sunlight priority
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamYuan committed Nov 1, 2023
1 parent 7f5d90f commit dce71a3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
21 changes: 15 additions & 6 deletions client/include/client/ChunkFloodSunlightTask.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ template <>
class ChunkTaskData<ChunkTaskType::kFloodSunlight> final : public ChunkTaskDataBase<ChunkTaskType::kFloodSunlight> {
private:
std::vector<InnerPos2> m_xz_updates;
bool m_high_priority{false};

public:
inline static constexpr ChunkTaskType kType = ChunkTaskType::kFloodSunlight;

inline void Push(InnerPos2 xz_update) { m_xz_updates.push_back(xz_update); }
inline void Push(std::span<const InnerPos2> xz_updates) {
inline void Push(InnerPos2 xz_update, bool high_priority = false) {
m_xz_updates.push_back(xz_update);
m_high_priority |= high_priority;
}
inline void Push(std::span<const InnerPos2> xz_updates, bool high_priority = false) {
m_xz_updates.insert(m_xz_updates.end(), xz_updates.begin(), xz_updates.end());
m_high_priority |= high_priority;
}
inline ChunkTaskPriority GetPriority() const {
return m_high_priority ? ChunkTaskPriority::kHigh : ChunkTaskPriority::kLow;
}
inline constexpr ChunkTaskPriority GetPriority() const { return ChunkTaskPriority::kLow; }
inline bool IsQueued() const { return !m_xz_updates.empty(); }
std::optional<ChunkTaskRunnerData<ChunkTaskType::kFloodSunlight>> Pop(const ChunkTaskPoolLocked &task_pool,
const ChunkPos3 &chunk_pos);
Expand All @@ -26,17 +33,19 @@ template <> class ChunkTaskRunnerData<ChunkTaskType::kFloodSunlight> {
private:
std::shared_ptr<Chunk> m_chunk_ptr, m_up_chunk_ptr;
std::vector<InnerPos2> m_xz_updates;
bool m_high_priority;

public:
inline static constexpr ChunkTaskType kType = ChunkTaskType::kFloodSunlight;

inline ChunkTaskRunnerData(std::shared_ptr<Chunk> chunk_ptr, std::shared_ptr<Chunk> up_chunk_ptr,
std::vector<InnerPos2> &&xz_updates)
std::vector<InnerPos2> &&xz_updates, bool high_priority)
: m_chunk_ptr{std::move(chunk_ptr)}, m_up_chunk_ptr{std::move(up_chunk_ptr)},
m_xz_updates{std::move(xz_updates)} {}
m_xz_updates{std::move(xz_updates)}, m_high_priority{high_priority} {}
inline const ChunkPos3 &GetChunkPos() const { return m_chunk_ptr->GetPosition(); }
inline const std::shared_ptr<Chunk> &GetChunkPtr() const { return m_chunk_ptr; }
inline const std::shared_ptr<Chunk> &GetUpChunkPtr() const { return m_up_chunk_ptr; }
inline bool IsHighPriority() const { return m_high_priority; }
inline const auto &GetXZUpdates() const { return m_xz_updates; }
};

Expand All @@ -45,7 +54,7 @@ private:
public:
inline static constexpr ChunkTaskType kType = ChunkTaskType::kFloodSunlight;

static void Run(ChunkTaskPool *p_task_pool, ChunkTaskRunnerData<ChunkTaskType::kFloodSunlight> &&data);
void Run(ChunkTaskPool *p_task_pool, ChunkTaskRunnerData<ChunkTaskType::kFloodSunlight> &&data);
};

} // namespace hc::client
18 changes: 13 additions & 5 deletions client/include/client/ChunkSetSunlightTask.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ template <>
class ChunkTaskData<ChunkTaskType::kSetSunlight> final : public ChunkTaskDataBase<ChunkTaskType::kSetSunlight> {
private:
std::vector<std::pair<InnerPos2, InnerPos1>> m_set_sunlights;
bool m_high_priority = false;

public:
inline static constexpr ChunkTaskType kType = ChunkTaskType::kSetSunlight;

inline void Push(InnerPos2 inner_pos, InnerPos1 sunlight_height) {
inline void Push(InnerPos2 inner_pos, InnerPos1 sunlight_height, bool high_priority = false) {
m_set_sunlights.emplace_back(inner_pos, sunlight_height);
m_high_priority |= high_priority;
}
inline void Push(std::span<const std::pair<InnerPos2, InnerPos1>> set_sunlights) {
inline void Push(std::span<const std::pair<InnerPos2, InnerPos1>> set_sunlights, bool high_priority = false) {
m_set_sunlights.insert(m_set_sunlights.end(), set_sunlights.begin(), set_sunlights.end());
m_high_priority |= high_priority;
}
inline ChunkTaskPriority GetPriority() const {
return m_high_priority ? ChunkTaskPriority::kHigh : ChunkTaskPriority::kLow;
}
inline constexpr ChunkTaskPriority GetPriority() const { return ChunkTaskPriority::kLow; }
inline bool IsQueued() const { return !m_set_sunlights.empty(); }
std::optional<ChunkTaskRunnerData<ChunkTaskType::kSetSunlight>> Pop(const ChunkTaskPoolLocked &task_pool,
const ChunkPos3 &chunk_pos);
Expand All @@ -28,16 +33,19 @@ template <> class ChunkTaskRunnerData<ChunkTaskType::kSetSunlight> {
private:
std::shared_ptr<Chunk> m_chunk_ptr;
std::vector<std::pair<InnerPos2, InnerPos1>> m_set_sunlights;
bool m_high_priority;

public:
inline static constexpr ChunkTaskType kType = ChunkTaskType::kSetSunlight;

inline ChunkTaskRunnerData(std::shared_ptr<Chunk> chunk_ptr,
std::vector<std::pair<InnerPos2, InnerPos1>> &&set_sunlights)
: m_chunk_ptr{std::move(chunk_ptr)}, m_set_sunlights{std::move(set_sunlights)} {}
std::vector<std::pair<InnerPos2, InnerPos1>> &&set_sunlights, bool high_priority)
: m_chunk_ptr{std::move(chunk_ptr)}, m_set_sunlights{std::move(set_sunlights)}, m_high_priority{high_priority} {
}
inline const ChunkPos3 &GetChunkPos() const { return m_chunk_ptr->GetPosition(); }
inline const std::shared_ptr<Chunk> &GetChunkPtr() const { return m_chunk_ptr; }
inline const auto &GetSetSunlights() const { return m_set_sunlights; }
inline bool IsHighPriority() const { return m_high_priority; }
};

template <> class ChunkTaskRunner<ChunkTaskType::kSetSunlight> {
Expand Down
5 changes: 3 additions & 2 deletions client/include/client/ChunkUpdatePool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class ChunkUpdatePool {
m_block_updates.find_fn(chunk_pos, [&ret](auto &data) { ret = data; });
return ret;
}
void SetSunlightUpdate(ChunkPos3 chunk_pos, InnerPos2 inner_pos, InnerPos1 sunlight_height);
void SetSunlightUpdateBulk(ChunkPos3 chunk_pos, std::span<const std::pair<InnerPos2, InnerPos1>> sunlights);
void SetSunlightUpdate(ChunkPos3 chunk_pos, InnerPos2 inner_pos, InnerPos1 sunlight_height, bool active);
void SetSunlightUpdateBulk(ChunkPos3 chunk_pos, std::span<const std::pair<InnerPos2, InnerPos1>> sunlights,
bool active);

inline std::optional<InnerPos1> GetSunlightUpdate(ChunkPos3 chunk_pos, InnerPos2 inner_pos) const {
std::optional<InnerPos1> ret = std::nullopt;
Expand Down
9 changes: 6 additions & 3 deletions client/src/ChunkFloodSunlightTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ ChunkTaskData<ChunkTaskType::kFloodSunlight>::Pop(const ChunkTaskPoolLocked &tas

auto xz_updates = std::move(m_xz_updates);
m_xz_updates.clear();
auto high_priority = m_high_priority;
m_high_priority = false;

return ChunkTaskRunnerData<ChunkTaskType::kFloodSunlight>{std::move(chunk), std::move(up_chunk),
std::move(xz_updates)};
std::move(xz_updates), high_priority};
}

void ChunkTaskRunner<ChunkTaskType::kFloodSunlight>::Run(ChunkTaskPool *p_task_pool,
Expand Down Expand Up @@ -62,11 +64,12 @@ void ChunkTaskRunner<ChunkTaskType::kFloodSunlight>::Run(ChunkTaskPool *p_task_p
xz_next_updates.push_back(xz);
}
}
p_task_pool->GetWorld().m_chunk_update_pool.SetSunlightUpdateBulk(chunk->GetPosition(), set_sunlights);
p_task_pool->GetWorld().m_chunk_update_pool.SetSunlightUpdateBulk(chunk->GetPosition(), set_sunlights,
data.IsHighPriority());

auto down_chunk_pos = chunk->GetPosition();
--down_chunk_pos.y;
p_task_pool->Push<ChunkTaskType::kFloodSunlight>(down_chunk_pos, xz_next_updates);
p_task_pool->Push<ChunkTaskType::kFloodSunlight>(down_chunk_pos, xz_next_updates, false);
}

} // namespace hc::client
2 changes: 1 addition & 1 deletion client/src/ChunkSetBlockTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void ChunkTaskRunner<ChunkTaskType::kSetBlock>::Run(ChunkTaskPool *p_task_pool,

// Trigger sunlight flood
auto flood_sunlight_vec = std::vector<InnerPos2>{flood_sunlights.begin(), flood_sunlights.end()};
p_task_pool->Push<ChunkTaskType::kFloodSunlight>(chunk->GetPosition(), std::span{flood_sunlight_vec});
p_task_pool->Push<ChunkTaskType::kFloodSunlight>(chunk->GetPosition(), std::span{flood_sunlight_vec}, true);
}

} // namespace hc::client
7 changes: 4 additions & 3 deletions client/src/ChunkSetSunlightTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ ChunkTaskData<ChunkTaskType::kSetSunlight>::Pop(const ChunkTaskPoolLocked &task_
return std::nullopt;
auto set_sunlights = std::move(m_set_sunlights);
m_set_sunlights.clear();

return ChunkTaskRunnerData<ChunkTaskType::kSetSunlight>{std::move(chunk), std::move(set_sunlights)};
bool high_priority = m_high_priority;
m_high_priority = false;
return ChunkTaskRunnerData<ChunkTaskType::kSetSunlight>{std::move(chunk), std::move(set_sunlights), high_priority};
}

void ChunkTaskRunner<ChunkTaskType::kSetSunlight>::Run(ChunkTaskPool *p_task_pool,
Expand Down Expand Up @@ -56,7 +57,7 @@ void ChunkTaskRunner<ChunkTaskType::kSetSunlight>::Run(ChunkTaskPool *p_task_poo
ChunkPos3 nei_pos;
Chunk::NeighbourIndex2CmpXYZ(i, glm::value_ptr(nei_pos));
nei_pos += chunk->GetPosition();
p_task_pool->Push<ChunkTaskType::kMesh>(nei_pos);
p_task_pool->Push<ChunkTaskType::kMesh>(nei_pos, data.IsHighPriority());
}
}

Expand Down
13 changes: 7 additions & 6 deletions client/src/ChunkUpdatePool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ void ChunkUpdatePool::SetBlockUpdateBulk(ChunkPos3 chunk_pos,
InnerPosCompare{});
}

void ChunkUpdatePool::SetSunlightUpdate(ChunkPos3 chunk_pos, InnerPos2 inner_pos, InnerPos1 sunlight_height) {
void ChunkUpdatePool::SetSunlightUpdate(ChunkPos3 chunk_pos, InnerPos2 inner_pos, InnerPos1 sunlight_height,
bool active) {
m_sunlight_updates.uprase_fn(
chunk_pos,
[this, &chunk_pos, inner_pos, sunlight_height](auto &data, libcuckoo::UpsertContext) {
[this, &chunk_pos, inner_pos, sunlight_height, active](auto &data, libcuckoo::UpsertContext) {
data[inner_pos] = sunlight_height;
m_world.m_chunk_task_pool.Push<ChunkTaskType::kSetSunlight>(chunk_pos, inner_pos, sunlight_height);
m_world.m_chunk_task_pool.Push<ChunkTaskType::kSetSunlight>(chunk_pos, inner_pos, sunlight_height, active);
return false;
},
InnerPosCompare{});
}
void ChunkUpdatePool::SetSunlightUpdateBulk(ChunkPos3 chunk_pos,
std::span<const std::pair<InnerPos2, InnerPos1>> sunlights) {
std::span<const std::pair<InnerPos2, InnerPos1>> sunlights, bool active) {
m_sunlight_updates.uprase_fn(
chunk_pos,
[this, &chunk_pos, sunlights](auto &data, libcuckoo::UpsertContext) {
[this, &chunk_pos, sunlights, active](auto &data, libcuckoo::UpsertContext) {
for (const auto &i : sunlights)
data[i.first] = i.second;
m_world.m_chunk_task_pool.Push<ChunkTaskType::kSetSunlight>(chunk_pos, sunlights);
m_world.m_chunk_task_pool.Push<ChunkTaskType::kSetSunlight>(chunk_pos, sunlights, active);
return false;
},
InnerPosCompare{});
Expand Down

0 comments on commit dce71a3

Please sign in to comment.