From 5d4d3ced7f7e4b3aa64013e2d9b2658f5a74e8fa Mon Sep 17 00:00:00 2001 From: proller Date: Tue, 15 Oct 2024 20:02:24 +0200 Subject: [PATCH 1/2] fix --- src/client/fm_client.cpp | 5 +-- src/client/fm_far_container.cpp | 42 +++++++++++++++++------- src/client/game.cpp | 8 ++--- src/fm_map.cpp | 21 ++++++------ src/threading/concurrent_unordered_map.h | 5 ++- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/client/fm_client.cpp b/src/client/fm_client.cpp index 142a83c49..f6528791a 100644 --- a/src/client/fm_client.cpp +++ b/src/client/fm_client.cpp @@ -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 @@ -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; } }); diff --git a/src/client/fm_far_container.cpp b/src/client/fm_far_container.cpp index fa35fddd9..ce92791df 100644 --- a/src/client/fm_far_container.cpp +++ b/src/client/fm_far_container.cpp @@ -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; }; diff --git a/src/client/game.cpp b/src/client/game.cpp index 67bef2520..64fa8d602 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -4461,14 +4461,14 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime, 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) { diff --git a/src/fm_map.cpp b/src/fm_map.cpp index 21d8f2b97..c5138ff75 100644 --- a/src/fm_map.cpp +++ b/src/fm_map.cpp @@ -24,9 +24,7 @@ along with Freeminer. If not, see . #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" @@ -37,8 +35,11 @@ along with Freeminer. If not, see . #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 @@ -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; } } @@ -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; } } @@ -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) @@ -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) diff --git a/src/threading/concurrent_unordered_map.h b/src/threading/concurrent_unordered_map.h index f6532d593..609ef26e4 100644 --- a/src/threading/concurrent_unordered_map.h +++ b/src/threading/concurrent_unordered_map.h @@ -64,7 +64,6 @@ class concurrent_unordered_map_ : public std::unordered_map(args)...); it != full_type::end()) { return it->second; @@ -73,14 +72,14 @@ class concurrent_unordered_map_ : public std::unordered_map - 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)...); it != full_type::end()) { return it->second; } - return nothing; + return def; } template From c53c4df8e4fd26ae822e400061714c56344be359 Mon Sep 17 00:00:00 2001 From: proller Date: Tue, 15 Oct 2024 21:04:06 +0200 Subject: [PATCH 2/2] fix --- src/mapgen/earth/hgt.cpp | 21 ++++++++++++--------- src/network/clientpackethandler.cpp | 6 ++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/mapgen/earth/hgt.cpp b/src/mapgen/earth/hgt.cpp index 478022d19..2dddfc88e 100644 --- a/src/mapgen/earth/hgt.cpp +++ b/src/mapgen/earth/hgt.cpp @@ -158,14 +158,14 @@ height::height_t hgts::get(height_hgt::ll_t lat, height_hgt::ll_t lon) } } { - auto hgt = std::make_unique(); - const auto lat_dec = hgt->lat_start(lat); - const auto lon_dec = hgt->lon_start(lon); - // DUMP("place dummy", lat, lon, lat_dec, lon_dec, map1[lat_dec].contains(lon_dec)); + const static auto hgt_dummy = std::make_shared(); + const auto lat_dec = hgt_dummy->lat_start(lat); + const auto lon_dec = hgt_dummy->lon_start(lon); + DUMP("place dummy", lat, lon, lat_dec, lon_dec, map1[lat_dec].contains(lon_dec)); if (!map1[lat_dec].contains(lon_dec)) - map1[lat_dec][lon_dec] = std::move(hgt); + map1[lat_dec][lon_dec] = hgt_dummy; if (!map90[lat90].contains(lon90)) - map90[lat90][lon90] = std::make_unique(); + map90[lat90][lon90] = hgt_dummy; return map1[lat_dec][lon_dec]->get(lat, lon); } } @@ -895,7 +895,8 @@ bool height_gebco_tif::load(ll_t lat, ll_t lon) return false; } -std::tuple height_gebco_tif::ll_to_xy(ll_t lat, ll_t lon) +std::tuple height_gebco_tif::ll_to_xy( + ll_t lat, ll_t lon) { const height::ll_t lat_seconds = (lat_loaded - lat) * 60 * 60; const height::ll_t lon_seconds = (lon - lon_loaded) * 60 * 60; @@ -921,7 +922,8 @@ int16_t height_gebco_tif::read(uint16_t y, uint16_t x) return heights[pos]; } -std::tuple height_hgt::ll_to_xy(height::ll_t lat, height::ll_t lon) +std::tuple height_hgt::ll_to_xy( + height::ll_t lat, height::ll_t lon) { const height::ll_t lat_seconds = (lat - (ll_t)lat_loaded) * 60 * 60; @@ -931,7 +933,8 @@ std::tuple height_hgt::ll_to_xy(heig return {x, y, lat_seconds, lon_seconds}; } -std::tuple height_tif::ll_to_xy(height::ll_t lat, height::ll_t lon) +std::tuple height_tif::ll_to_xy( + height::ll_t lat, height::ll_t lon) { const ll_t lat_seconds = (lat - (ll_t)lat_loaded) * 60 * 60; const ll_t lon_seconds = (lon - (ll_t)lon_loaded) * 60 * 60; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index b5c9b24a5..e9e68dbfc 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -336,12 +336,14 @@ void Client::handleCommand_BlockData(NetworkPacket* pkt) /* Create a new block */ - block = sector->createBlankBlock(p).get(); + block = sector->createBlankBlockNoInsert(p); if (!block->deSerialize(istr, m_server_ser_ver, false)){ delete block; return; - }; + } block->deSerializeNetworkSpecific(istr); + + sector->insertBlock(block); ++m_new_meshes; }