Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 11, 2024
1 parent 5d19eb5 commit 42f3bd0
Show file tree
Hide file tree
Showing 27 changed files with 345 additions and 252 deletions.
2 changes: 1 addition & 1 deletion src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Client::Client(
control.cell_size = m_mesh_grid.cell_size;
control.cell_size_pow = log(control.cell_size) / log(2);
control.farmesh_quality = g_settings->getU16("farmesh_quality");
control.farmesh_quality_pow = log(control.farmesh_quality) / log(2);
control.farmesh_quality_pow = log(control.farmesh_quality) / log(2);
control.farmesh_stable = g_settings->getU16("farmesh_stable");
}

Expand Down
51 changes: 24 additions & 27 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,16 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)

unordered_map_v3pos<bool> occlude_cache;

std::vector<std::pair<v3bpos_t, MapBlockP>> vector;
{
const auto lock = m_blocks.lock_shared_rec();
vector.reserve(m_blocks.size());
for (const auto &it : m_blocks) {
vector.emplace_back(it);
}
}

for(const auto & [bp, block] : m_blocks) {
for(const auto & [bp, block] : vector) {

if (!block)
continue;
Expand All @@ -856,7 +864,7 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
*/

const auto mesh = block->getLodMesh(mesh_step, true);
{
{
++blocks_in_range;

const int smesh_size = !mesh ? -1 : mesh->getMesh()->getMeshBufferCount();
Expand All @@ -869,13 +877,14 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
blocks_in_range_without_mesh++;
if (m_mesh_queued < maxq || range_blocks <= 2) {
if (!mesh || speedf < BS * MAP_BLOCKSIZE) {
const auto bts = block->getTimestamp();
if (block->mesh_requested_timestamp < bts ||
if (const auto bts = block->getTimestamp();
block->mesh_requested_timestamp < bts ||
block->mesh_requested_step != mesh_step) {
block->mesh_requested_timestamp = bts;
block->mesh_requested_timestamp = bts;
block->mesh_requested_step = mesh_step;
m_client->addUpdateMeshTask(bp, false);
++m_mesh_queued;
//DUMP("goup", bp, m_mesh_queued);
m_client->addUpdateMeshTask(bp, false);
++m_mesh_queued;
}
}
}
Expand Down Expand Up @@ -957,7 +966,7 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
// First, perform a simple distance check.
if (!m_control.range_all &&
radius_box(mesh_sphere_center, m_camera_position) >
m_control.wanted_range * BS + mesh_sphere_radius)
m_control.wanted_range * BS + mesh_sphere_radius)
continue; // Out of range, skip.
}

Expand Down Expand Up @@ -1037,8 +1046,7 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)

if(range_blocks * MAP_BLOCKSIZE > farthest_drawn)
farthest_drawn = range_blocks * MAP_BLOCKSIZE;
}

}
}
//m_drawlist_last = draw_nearest.size();

Expand All @@ -1055,7 +1063,7 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
: m_far_blocks_delete_2;
m_far_blocks_delete.clear();
size_t farblocks_drawn = 0;
auto lock = m_far_blocks.lock_unique_rec();
const auto lock = m_far_blocks.lock_unique_rec();
for (auto it = m_far_blocks.begin(); it != m_far_blocks.end();) {
const auto &block = it->second;
if (m_far_blocks_clean_timestamp > 0 &&
Expand All @@ -1064,22 +1072,8 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
it = m_far_blocks.erase(it);
} else if (block->getTimestamp() >= m_far_blocks_use_timestamp) {
if (!blocks_skip_farmesh.contains(it->first)) {
int mesh_step = getFarStep(m_control,
getNodeBlockPos(m_far_blocks_last_cam_pos),
it->first); // m_camera_position_node
if (mesh_step > 1 &&
!inFarGrid(it->first,
getNodeBlockPos(m_far_blocks_last_cam_pos), mesh_step,
m_control)) {
} else {
const auto mesh = block->getFarMesh(mesh_step);
if (!mesh) {
//m_client->farmesh_remake.insert_or_assign(it->first, false);
} else {
drawlist.emplace(it->first, block);
++farblocks_drawn;
}
}
drawlist.emplace(it->first, block);
++farblocks_drawn;
}
++it;
} else {
Expand Down Expand Up @@ -1353,6 +1347,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
g_profiler->avg(prefix + "material swaps [#]", material_swaps);

if(is_transparent_pass)
m_far_blocks_delete.clear();
}

static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
Expand Down
47 changes: 37 additions & 10 deletions src/client/fm_client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <exception>
#include <future>
#include "client.h"
#include "client/fm_far_calc.h"
#include "client/mapblock_mesh.h"
#include "clientmap.h"
#include "emerge.h"
Expand Down Expand Up @@ -51,7 +53,7 @@ void Client::sendGetBlocks()
if (!farmesh_server)
return;

auto &far_blocks = *m_env.getClientMap().m_far_blocks_use;
auto &far_blocks = m_env.getClientMap().m_far_blocks_ask;
const auto lock = far_blocks.lock_unique_rec();

if (far_blocks.empty()) {
Expand Down Expand Up @@ -151,7 +153,7 @@ void Client::createFarMesh(MapBlockP &block)
const auto &m_client = this;
const auto &blockpos_actual = block->getPos();
const auto &m_camera_offset = m_camera->getOffset();
const auto step = block->far_step;
const auto &step = block->far_step;
MeshMakeData mdat(m_client, false, 0, step, &m_client->far_container);
mdat.m_blockpos = blockpos_actual;
auto mbmsh = std::make_shared<MapBlockMesh>(&mdat, m_camera_offset);
Expand Down Expand Up @@ -216,7 +218,6 @@ void Client::handleCommand_BlockDatas(NetworkPacket *pkt)
block->humidity = h;

if (!step) {

if (m_localdb) {
ServerMap::saveBlock(block.get(), m_localdb);
}
Expand All @@ -230,14 +231,40 @@ void Client::handleCommand_BlockDatas(NetworkPacket *pkt)
} else {
static thread_local const auto farmesh_server =
g_settings->getU16("farmesh_server");
if (farmesh_server) {
far_container.far_blocks[step].insert_or_assign(bpos, block);
auto &far_blocks = getEnv().getClientMap().m_far_blocks;
if (far_blocks.contains(bpos)) {
const auto &block = far_blocks.at(bpos);
block->farmesh_need_remake = m_uptime;
//block->setTimestampNoChangedFlag(-2);
if (!farmesh_server)
return;

auto &far_blocks_storage = getEnv().getClientMap().far_blocks_storage[step];
{
const auto lock = far_blocks_storage.lock_unique_rec();
if (far_blocks_storage.find(bpos) != far_blocks_storage.end()) {
return;
}
}
far_blocks_storage.insert_or_assign(block->getPos(), block);
++m_new_farmeshes;

//todo: step ordered thread pool
std::async(std::launch::async, [this, block]() mutable {
createFarMesh(block);
auto &client_map = getEnv().getClientMap();
const auto &control = client_map.getControl();
const auto bpos = block->getPos();
int fmesh_step_ = getFarStep(control,
getNodeBlockPos(client_map.m_far_blocks_last_cam_pos),
block->getPos());
if (!inFarGrid(block->getPos(),
getNodeBlockPos(client_map.m_far_blocks_last_cam_pos),
fmesh_step_, control)) {
return;
}
auto &far_blocks = client_map.m_far_blocks;
if (const auto &it = far_blocks.find(bpos); it != far_blocks.end()) {
if (it->second->far_step != block->far_step) {
return;
}
far_blocks.at(bpos) = block;
}
});
}
}
46 changes: 25 additions & 21 deletions src/client/fm_far_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int getLodStep(const MapDrawControl &draw_control, const v3bpos_t &playerblockpo
int range = radius_box(playerblockpos, blockpos);
/* todo: make stable, depend on speed increase/decrease
const auto speed_blocks = speedf / (BS * MAP_BLOCKSIZE);
if (range > 1 && speed_blocks > 1) {
if (range > 1 && speed_blocks > 1) {
range += speed_blocks;
}
*/
Expand Down Expand Up @@ -66,7 +66,6 @@ int getLodStep(const MapDrawControl &draw_control, const v3bpos_t &playerblockpo
return 0;
};

#if 0
int getFarStepBad(const MapDrawControl &draw_control, const v3bpos_t &playerblockpos,
const v3bpos_t &blockpos)
{
Expand Down Expand Up @@ -96,9 +95,8 @@ int getFarStepBad(const MapDrawControl &draw_control, const v3bpos_t &playerbloc
skip = FARMESH_STEP_MAX;
return skip;
};
#endif

auto align(auto pos, const auto amount)
auto align_shift(auto pos, const auto amount)
{
(pos.X >>= amount) <<= amount;
(pos.Y >>= amount) <<= amount;
Expand All @@ -110,7 +108,7 @@ v3bpos_t playerBlockAlign(
const MapDrawControl &draw_control, const v3bpos_t &playerblockpos)
{
const auto step_pow2 = draw_control.cell_size_pow + draw_control.farmesh_quality_pow;
return align(playerblockpos, step_pow2) + (step_pow2 >> 1);
return align_shift(playerblockpos, step_pow2) + (step_pow2 >> 1);
}

#if 1
Expand All @@ -136,24 +134,31 @@ struct child_t
};

std::optional<child_t> find(const v3tpos_t &block_pos, const v3tpos_t &player_pos,
const child_t &child, const int cell_size_pow, uint16_t farmesh_quality)
const child_t &child, const int cell_size_pow, uint16_t farmesh_quality,
uint16_t depth = 0)
{
if (!(block_pos.X >= child.pos.X && block_pos.X < child.pos.X + child.size &&
block_pos.Y >= child.pos.Y && block_pos.Y < child.pos.Y + child.size &&
block_pos.Z >= child.pos.Z && block_pos.Z < child.pos.Z + child.size))
return {};

if (child.size < (1 << (1 + cell_size_pow)))
block_pos.Z >= child.pos.Z && block_pos.Z < child.pos.Z + child.size)) {
if (depth) {
return {};
} else {
return child;
}
}
if (child.size < (1 << (cell_size_pow))) {
return child;
}

auto distance =
std::max({std::abs((tpos_t)player_pos.X - child.pos.X - (child.size >> 1)),
std::abs((tpos_t)player_pos.Y - child.pos.Y - (child.size >> 1)),
std::abs((tpos_t)player_pos.Z - child.pos.Z - (child.size >> 1))});

if (farmesh_quality)
if (farmesh_quality) {
distance /= farmesh_quality;
if (distance >= child.size) {
}
if (distance > child.size) {
return child;
}
const tpos_t childSize = child.size >> 1;
Expand Down Expand Up @@ -181,8 +186,8 @@ std::optional<child_t> find(const v3tpos_t &block_pos, const v3tpos_t &player_po
child.pos.Z + childSize),
.size = childSize},
}) {
const auto res =
find(block_pos, player_pos, child, cell_size_pow, farmesh_quality);
const auto res = find(
block_pos, player_pos, child, cell_size_pow, farmesh_quality, depth + 1);
if (res) {
return res;
}
Expand All @@ -203,7 +208,7 @@ const auto external_pow = tree_pow - 2;
int getFarStepCellSize(const MapDrawControl &draw_control, const v3bpos_t &ppos,
const v3bpos_t &blockpos, uint8_t cell_size_pow)
{
const auto blockpos_aligned_cell = align(blockpos, draw_control.cell_size_pow);
const auto blockpos_aligned_cell = align_shift(blockpos, cell_size_pow);

const auto start = child_t{.pos = v3tpos_t(
// TODO: cast to type larger than pos_t_type
Expand All @@ -216,8 +221,7 @@ int getFarStepCellSize(const MapDrawControl &draw_control, const v3bpos_t &ppos,
.size = tree_size};
const auto res = find(
{blockpos_aligned_cell.X, blockpos_aligned_cell.Y, blockpos_aligned_cell.Z},
{ppos.X, ppos.Y, ppos.Z}, start, draw_control.cell_size_pow,
draw_control.farmesh_quality);
{ppos.X, ppos.Y, ppos.Z}, start, cell_size_pow, draw_control.farmesh_quality);
if (res) {
/*
#if !USE_POS32
Expand All @@ -230,7 +234,7 @@ int getFarStepCellSize(const MapDrawControl &draw_control, const v3bpos_t &ppos,
return {};
#endif
*/
const auto step = int(log(res->size) / log(2)) - draw_control.cell_size_pow;
const auto step = int(log(res->size) / log(2)) - cell_size_pow;
return step;
}
return 0; // TODO! fix intersection with cell_size_pow
Expand All @@ -246,8 +250,7 @@ int getFarStep(const MapDrawControl &draw_control, const v3bpos_t &ppos,
v3bpos_t getFarActual(const v3bpos_t &blockpos, const v3bpos_t &ppos, int step,
const MapDrawControl &draw_control)
{
const auto cell_size_pow = int(log(draw_control.cell_size) / log(2));
const auto blockpos_aligned_cell = align(blockpos, cell_size_pow);
const auto blockpos_aligned_cell = align_shift(blockpos, draw_control.cell_size_pow);

const auto start =
child_t{.pos = v3tpos_t((((tpos_t)ppos.X >> tree_align) << tree_align) -
Expand All @@ -259,7 +262,8 @@ v3bpos_t getFarActual(const v3bpos_t &blockpos, const v3bpos_t &ppos, int step,
.size = tree_size};
const auto res = find(
{blockpos_aligned_cell.X, blockpos_aligned_cell.Y, blockpos_aligned_cell.Z},
{ppos.X, ppos.Y, ppos.Z}, start, cell_size_pow, draw_control.farmesh_quality);
{ppos.X, ppos.Y, ppos.Z}, start, draw_control.cell_size_pow,
draw_control.farmesh_quality);

if (res) {
#if USE_POS32
Expand Down
2 changes: 2 additions & 0 deletions src/client/fm_far_calc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int getFarStepCellSize(const MapDrawControl &draw_control, const v3bpos_t &ppos,
const v3bpos_t &blockpos, uint8_t cell_size_pow);
int getFarStep(const MapDrawControl &draw_control, const v3bpos_t &playerblockpos,
const v3bpos_t &block_pos);
int getFarStepBad(const MapDrawControl &draw_control, const v3bpos_t &playerblockpos,
const v3bpos_t &block_pos);
bool inFarGrid(const v3bpos_t &blockpos, const v3bpos_t &playerblockpos, int step,
const MapDrawControl &draw_control);
v3bpos_t getFarActual(const v3bpos_t &blockpos, const v3bpos_t &playerblockpos, int step,
Expand Down
5 changes: 3 additions & 2 deletions src/client/fm_far_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const MapNode &FarContainer::getNodeRefUnsafe(const v3pos_t &pos)
v3bpos_t bpos_aligned((bpos.X >> shift) << shift, (bpos.Y >> shift) << shift,
(bpos.Z >> shift) << shift);

if (const auto &it = far_blocks[fmesh_step].find(bpos_aligned);
it != far_blocks[fmesh_step].end()) {
const auto &storage =
m_client->getEnv().getClientMap().far_blocks_storage[fmesh_step];
if (const auto &it = storage.find(bpos_aligned); it != storage.end()) {
const auto &block = it->second;
v3pos_t relpos = pos - bpos_aligned * MAP_BLOCKSIZE;

Expand Down
3 changes: 0 additions & 3 deletions src/client/fm_far_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class FarContainer : public NodeContainer
public:
Mapgen *m_mg{};

std::array<concurrent_unordered_map<v3bpos_t, MapBlockP>, FARMESH_STEP_MAX>
far_blocks;

FarContainer(Client *client);
const MapNode &getNodeRefUnsafe(const v3pos_t &p) override;
};
Loading

0 comments on commit 42f3bd0

Please sign in to comment.