Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 15, 2024
1 parent 857246d commit 35c9865
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/client/fm_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ void Client::handleCommand_BlockDataFm(NetworkPacket *pkt)
if (far_blocks_storage.find(bpos) != far_blocks_storage.end()) {
return;
}
far_blocks_storage.insert_or_assign(block->getPos(), block);
}
far_blocks_storage.insert_or_assign(block->getPos(), block);
++m_new_farmeshes;

//todo: step ordered thread pool
Expand All @@ -262,7 +262,8 @@ void Client::handleCommand_BlockDataFm(NetworkPacket *pkt)
if (it->second->far_step != block->far_step) {
return;
}
block->far_iteration = it->second->far_iteration.load(std::memory_order::relaxed);
block->far_iteration =
it->second->far_iteration.load(std::memory_order::relaxed);
far_blocks.at(bpos) = block;
}
});
Expand Down
42 changes: 30 additions & 12 deletions src/client/fm_far_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,53 @@ FarContainer::FarContainer(Client *client) : m_client{client}
{
}

const MapNode &FarContainer::getNodeRefUnsafe(const v3pos_t &pos)
namespace
{
auto bpos = getNodeBlockPos(pos);
thread_local MapBlockP block_cache{};
thread_local v3bpos_t block_cache_p;
}

int fmesh_step = getFarStep(m_client->getEnv().getClientMap().getControl(),
const MapNode &FarContainer::getNodeRefUnsafe(const v3pos_t &pos)
{
const auto bpos = getNodeBlockPos(pos);
const auto fmesh_step = getFarStep(m_client->getEnv().getClientMap().getControl(),
getNodeBlockPos(m_client->getEnv().getClientMap().far_blocks_last_cam_pos),
bpos);

const auto &shift = fmesh_step; // + cell_size_pow;
v3bpos_t bpos_aligned((bpos.X >> shift) << shift, (bpos.Y >> shift) << shift,
const v3bpos_t bpos_aligned((bpos.X >> shift) << shift, (bpos.Y >> shift) << shift,
(bpos.Z >> shift) << shift);

const auto &storage =
m_client->getEnv().getClientMap().far_blocks_storage[fmesh_step];
if (const auto &block = storage.at_or(bpos_aligned)) {
MapBlockP block;

if (block_cache && bpos_aligned == block_cache_p) {
block = block_cache;
}

if (!block && fmesh_step < FARMESH_STEP_MAX) {
const auto &storage =
m_client->getEnv().getClientMap().far_blocks_storage[fmesh_step];

block = storage.get(bpos_aligned);
}

if (block) {
v3pos_t relpos = pos - bpos_aligned * MAP_BLOCKSIZE;

const auto &relpos_shift = fmesh_step; // + 1;
auto relpos_shifted = v3pos_t(relpos.X >> relpos_shift, relpos.Y >> relpos_shift,
relpos.Z >> relpos_shift);
const auto relpos_shifted = v3pos_t(relpos.X >> relpos_shift,
relpos.Y >> relpos_shift, relpos.Z >> relpos_shift);
const auto &n = block->getNodeNoLock(relpos_shifted);
if (n.getContent() != CONTENT_IGNORE) {
return n;
}

block_cache_p = bpos_aligned;
block_cache = block;
}

const auto &v = m_mg->visible_content(pos);
if (v.getContent()) {
if (const auto &v = m_mg->visible_content(pos); v.getContent()) {
return v;
}

return m_mg->visible_transparent;
};
8 changes: 4 additions & 4 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4463,14 +4463,14 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
auto fog_was = runData.fog_range;

if (draw_control->range_all && sky->getFogDistance() < 0) {
runData.fog_range = 100000 * BS;
runData.fog_range = FARMESH_LIMIT * BS;
} else if (!runData.headless_optimize) {
runData.fog_range = draw_control->wanted_range * BS
+ 0.0 * MAP_BLOCKSIZE * BS;

thread_local static const auto farmesh = g_settings->getS32("farmesh");
if (runData.fog_range < farmesh) {
runData.fog_range = farmesh;
thread_local static const auto farmesh_bs = g_settings->getS32("farmesh") * BS;
if (runData.fog_range < farmesh_bs) {
runData.fog_range = farmesh_bs ;
}

if (client->use_weather) {
Expand Down
21 changes: 11 additions & 10 deletions src/fm_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
#include "irrlichttypes.h"
#include "map.h"
#include "mapblock.h"
#include "log_types.h"
#include "profiler.h"

#include "nodedef.h"
#include "environment.h"
#include "emerge.h"
Expand All @@ -37,8 +35,11 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
#include "voxelalgorithms.h"

#if HAVE_THREAD_LOCAL
thread_local MapBlockP m_block_cache = nullptr;
thread_local v3pos_t m_block_cache_p;
namespace
{
thread_local MapBlockP block_cache{};
thread_local v3bpos_t block_cache_p;
}
#endif

// TODO: REMOVE THIS func and use Map::getBlock
Expand All @@ -59,11 +60,11 @@ MapBlockP Map::getBlock(v3bpos_t p, bool trylock, bool nocache)
auto lock = maybe_shared_lock(m_block_cache_mutex, try_to_lock);
if (lock.owns_lock())
#endif
if (m_block_cache && p == m_block_cache_p) {
if (block_cache && p == block_cache_p) {
#ifndef NDEBUG
g_profiler->add("Map: getBlock cache hit", 1);
#endif
return m_block_cache;
return block_cache;
}
}

Expand All @@ -84,8 +85,8 @@ MapBlockP Map::getBlock(v3bpos_t p, bool trylock, bool nocache)
if (lock.owns_lock())
#endif
{
m_block_cache_p = p;
m_block_cache = block;
block_cache_p = p;
block_cache = block;
}
}

Expand All @@ -102,7 +103,7 @@ void Map::getBlockCacheFlush()
#if ENABLE_THREADS && !HAVE_THREAD_LOCAL
auto lock = unique_lock(m_block_cache_mutex);
#endif
m_block_cache = nullptr;
block_cache = nullptr;
}

MapBlock *Map::createBlankBlockNoInsert(const v3pos_t &p)
Expand Down Expand Up @@ -175,7 +176,7 @@ void Map::eraseBlock(const MapBlockP block)
#if ENABLE_THREADS && !HAVE_THREAD_LOCAL
auto lock = unique_lock(m_block_cache_mutex);
#endif
m_block_cache = nullptr;
block_cache = nullptr;
}

MapNode Map::getNodeTry(const v3pos_t &p)
Expand Down
5 changes: 2 additions & 3 deletions src/threading/concurrent_unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class concurrent_unordered_map_ : public std::unordered_map<Key, T, Hash, Pred,
const mapped_type &get(Args &&...args) const
{
const auto lock = LOCKER::lock_shared_rec();

if (const auto &it = full_type::find(std::forward<Args>(args)...);
it != full_type::end()) {
return it->second;
Expand All @@ -73,14 +72,14 @@ class concurrent_unordered_map_ : public std::unordered_map<Key, T, Hash, Pred,
}

template <typename... Args>
const mapped_type &at_or(Args &&...args) const
const mapped_type &at_or(Args &&...args, const mapped_type &def) const
{
const auto lock = LOCKER::lock_shared_rec();
if (const auto &it = full_type::find(std::forward<Args>(args)...);
it != full_type::end()) {
return it->second;
}
return nothing;
return def;
}

template <typename... Args>
Expand Down

0 comments on commit 35c9865

Please sign in to comment.