From 69fc20610947610b7829f3bfad82e23ed705b764 Mon Sep 17 00:00:00 2001 From: x2048 Date: Tue, 31 Jan 2023 17:30:59 +0100 Subject: [PATCH] 8x block meshes (#13133) Reduce the number of drawcalls by generating a mesh per 8 blocks (2x2x2). Only blocks with even coordinates (lowest bit set to 0) will get a mesh. Note: This also removes the old 'loops' algorithm for building the draw list, because it produces visual artifacts and cannot be made compatible with the approach of having a mesh for every 8th block without hurting performance. Co-authored-by: Jude Melton-Houghton Co-authored-by: Lars Co-authored-by: sfan5 --- src/client/client.cpp | 43 ++++++++-- src/client/clientmap.cpp | 37 +++++++-- src/client/clientmap.h | 2 + src/client/content_mapblock.cpp | 6 +- src/client/mapblock_mesh.cpp | 118 ++++++++++++++------------- src/client/mapblock_mesh.h | 22 ++--- src/client/mesh_generator_thread.cpp | 56 ++++++++----- src/client/mesh_generator_thread.h | 7 +- src/client/meshgen/collector.cpp | 4 +- src/client/meshgen/collector.h | 5 +- src/client/wieldmesh.cpp | 2 +- src/map.cpp | 7 +- src/util/directiontables.cpp | 72 ++++++++++++++++ src/util/directiontables.h | 3 + 14 files changed, 266 insertions(+), 118 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index 9a7e1498f..66dee613f 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -41,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "mapblock_mesh.h" #include "mapblock.h" +#include "mapsector.h" #include "minimap.h" #include "modchannels.h" #include "content/mods.h" @@ -555,19 +556,27 @@ void Client::step(float dtime) { num_processed_meshes++; - MinimapMapblock *minimap_mapblock = NULL; + std::vector minimap_mapblocks; bool do_mapper_update = true; - MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p); + MapSector *sector = m_env.getMap().emergeSector(v2s16(r.p.X, r.p.Z)); + + MapBlock *block = sector->getBlockNoCreateNoEx(r.p.Y); + + // The block in question is not visible (perhaps it is culled at the server), + // create a blank block just to hold the 2x2x2 mesh. + // If the block becomes visible later it will replace the blank block. + if (!block && r.mesh) + block = sector->createBlankBlock(r.p.Y); + if (block) { // Delete the old mesh delete block->mesh; block->mesh = nullptr; if (r.mesh) { - block->solid_sides = r.solid_sides; - minimap_mapblock = r.mesh->moveMinimapMapblock(); - if (minimap_mapblock == NULL) + minimap_mapblocks = r.mesh->moveMinimapMapblocks(); + if (minimap_mapblocks.empty()) do_mapper_update = false; bool is_empty = true; @@ -588,16 +597,32 @@ void Client::step(float dtime) delete r.mesh; } - if (m_minimap && do_mapper_update) - m_minimap->addBlock(r.p, minimap_mapblock); + for (auto p : r.solid_sides) { + auto block = m_env.getMap().getBlockNoCreateNoEx(p.first); + if (block) + block->solid_sides = p.second; + } - if (r.ack_block_to_server) { + if (m_minimap && do_mapper_update) { + v3s16 ofs; + + // See also mapblock_mesh.cpp for the code that creates the array of minimap blocks. + for (ofs.Z = 0; ofs.Z <= 1; ofs.Z++) + for (ofs.Y = 0; ofs.Y <= 1; ofs.Y++) + for (ofs.X = 0; ofs.X <= 1; ofs.X++) { + size_t i = ofs.Z * 4 + ofs.Y * 2 + ofs.X; + if (i < minimap_mapblocks.size() && minimap_mapblocks[i]) + m_minimap->addBlock(r.p + ofs, minimap_mapblocks[i]); + } + } + + for (auto p : r.ack_list) { if (blocks_to_ack.size() == 255) { sendGotBlocks(blocks_to_ack); blocks_to_ack.clear(); } - blocks_to_ack.emplace_back(r.p); + blocks_to_ack.emplace_back(p); } for (auto block : r.map_blocks) diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index 34d99e07d..146531d8b 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -298,6 +298,8 @@ void ClientMap::updateDrawList() blocks_to_consider.push(camera_block); blocks_seen.getChunk(camera_block).getBits(camera_block) = 0x07; // mark all sides as visible + std::set shortlist; + // Recursively walk the space and pick mapblocks for drawing while (blocks_to_consider.size() > 0) { @@ -369,11 +371,13 @@ void ClientMap::updateDrawList() continue; } - // The block is visible, add to the draw list - if (mesh) { - // Add to set + // Block meshes are stored in blocks where all coordinates are even (lowest bit set to 0) + // Add them to the de-dup set. + shortlist.emplace(block_coord.X & ~1, block_coord.Y & ~1, block_coord.Z & ~1); + // All other blocks we can grab and add to the drawlist right away. + if (block && m_drawlist.emplace(block_coord, block).second) { + // only grab the ref if the block exists and was not in the list block->refGrab(); - m_drawlist[block_coord] = block; } // Decide which sides to traverse next or to block away @@ -474,6 +478,16 @@ void ClientMap::updateDrawList() } } + g_profiler->avg("MapBlocks shortlist [#]", shortlist.size()); + + for (auto pos : shortlist) { + MapBlock * block = getBlockNoCreateNoEx(pos); + if (block && m_drawlist.emplace(pos, block).second) { + // only grab the ref if the block exists and was not in the list + block->refGrab(); + } + } + g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled); g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped); g_profiler->avg("MapBlocks examined [#]", blocks_visited); @@ -597,7 +611,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) MapBlock *block = i.second; MapBlockMesh *block_mesh = block->mesh; - // If the mesh of the block happened to get deleted, ignore it + // Meshes are only stored every 8-th block (where all coordinates are even), + // but we keep all the visible blocks in the draw list to prevent client + // from dropping them. + // On top of that, in some cases block mesh can be removed + // before the block is removed from the draw list. if (!block_mesh) continue; @@ -720,7 +738,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) material.TextureLayer[ShadowRenderer::TEXTURE_LAYER_SHADOW].Texture = nullptr; } - v3f block_wpos = intToFloat(descriptor.m_pos * MAP_BLOCKSIZE, BS); + v3f block_wpos = intToFloat(descriptor.m_pos / 8 * 8 * MAP_BLOCKSIZE, BS); m.setTranslation(block_wpos - offset); driver->setTransform(video::ETS_WORLD, m); @@ -1046,7 +1064,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver, ++material_swaps; } - v3f block_wpos = intToFloat(descriptor.m_pos * MAP_BLOCKSIZE, BS); + v3f block_wpos = intToFloat(descriptor.m_pos / 8 * 8 * MAP_BLOCKSIZE, BS); m.setTranslation(block_wpos - offset); driver->setTransform(video::ETS_WORLD, m); @@ -1133,6 +1151,11 @@ void ClientMap::updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, g_profiler->avg("SHADOW MapBlocks loaded [#]", blocks_loaded); } +void ClientMap::reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) +{ + g_profiler->avg("CM::reportMetrics loaded blocks [#]", all_blocks); +} + void ClientMap::updateTransparentMeshBuffers() { ScopeProfiler sp(g_profiler, "CM::updateTransparentMeshBuffers", SPT_AVG); diff --git a/src/client/clientmap.h b/src/client/clientmap.h index cc68c1c15..41f281a8a 100644 --- a/src/client/clientmap.h +++ b/src/client/clientmap.h @@ -140,6 +140,8 @@ public: void onSettingChanged(const std::string &name); +protected: + void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override; private: // update the vertex order in transparent mesh buffers diff --git a/src/client/content_mapblock.cpp b/src/client/content_mapblock.cpp index 1cd1f1441..4db72c75f 100644 --- a/src/client/content_mapblock.cpp +++ b/src/client/content_mapblock.cpp @@ -1619,9 +1619,9 @@ void MapblockMeshGenerator::drawNode() */ void MapblockMeshGenerator::generate() { - for (p.Z = 0; p.Z < MAP_BLOCKSIZE; p.Z++) - for (p.Y = 0; p.Y < MAP_BLOCKSIZE; p.Y++) - for (p.X = 0; p.X < MAP_BLOCKSIZE; p.X++) { + for (p.Z = 0; p.Z < data->side_length; p.Z++) + for (p.Y = 0; p.Y < data->side_length; p.Y++) + for (p.X = 0; p.X < data->side_length; p.X++) { n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p); f = &nodedef->get(n); drawNode(); diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index 7eba29624..c0931a570 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -49,7 +49,7 @@ void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos) m_vmanip.clear(); VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE, - blockpos_nodes + v3s16(1,1,1) * MAP_BLOCKSIZE*2-v3s16(1,1,1)); + blockpos_nodes + v3s16(1,1,1) * (side_length + MAP_BLOCKSIZE /* extra layer of blocks around the mesh */) - v3s16(1,1,1)); m_vmanip.addArea(voxel_area); } @@ -63,23 +63,6 @@ void MeshMakeData::fillBlockData(const v3s16 &block_offset, MapNode *data) m_vmanip.copyFrom(data, data_area, v3s16(0,0,0), blockpos_nodes, data_size); } -void MeshMakeData::fill(MapBlock *block) -{ - fillBlockDataBegin(block->getPos()); - - fillBlockData(v3s16(0,0,0), block->getData()); - - // Get map for reading neighbor blocks - Map *map = block->getParent(); - - for (const v3s16 &dir : g_26dirs) { - v3s16 bp = m_blockpos + dir; - MapBlock *b = map->getBlockNoCreateNoEx(bp); - if(b) - fillBlockData(dir, b->getData()); - } -} - void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos) { if (crack_level >= 0) @@ -883,7 +866,7 @@ static void updateFastFaceRow( // Unroll this variable which has a significant build cost TileSpec next_tile; - for (u16 j = 0; j < MAP_BLOCKSIZE; j++) { + for (u16 j = 0; j < data->side_length; j++) { // If tiling can be done, this is set to false in the next step bool next_is_different = true; @@ -894,7 +877,7 @@ static void updateFastFaceRow( // If at last position, there is nothing to compare to and // the face must be drawn anyway - if (j != MAP_BLOCKSIZE - 1) { + if (j != data->side_length - 1) { p += translate_dir; getTileInfo(data, p, face_dir, @@ -957,8 +940,8 @@ static void updateAllFastFaceRows(MeshMakeData *data, /* Go through every y,z and get top(y+) faces in rows of x+ */ - for (s16 y = 0; y < MAP_BLOCKSIZE; y++) - for (s16 z = 0; z < MAP_BLOCKSIZE; z++) + for (s16 y = 0; y < data->side_length; y++) + for (s16 z = 0; z < data->side_length; z++) updateFastFaceRow(data, v3s16(0, y, z), v3s16(1, 0, 0), //dir @@ -969,8 +952,8 @@ static void updateAllFastFaceRows(MeshMakeData *data, /* Go through every x,y and get right(x+) faces in rows of z+ */ - for (s16 x = 0; x < MAP_BLOCKSIZE; x++) - for (s16 y = 0; y < MAP_BLOCKSIZE; y++) + for (s16 x = 0; x < data->side_length; x++) + for (s16 y = 0; y < data->side_length; y++) updateFastFaceRow(data, v3s16(x, y, 0), v3s16(0, 0, 1), //dir @@ -981,8 +964,8 @@ static void updateAllFastFaceRows(MeshMakeData *data, /* Go through every y,z and get back(z+) faces in rows of x+ */ - for (s16 z = 0; z < MAP_BLOCKSIZE; z++) - for (s16 y = 0; y < MAP_BLOCKSIZE; y++) + for (s16 z = 0; z < data->side_length; z++) + for (s16 y = 0; y < data->side_length; y++) updateFastFaceRow(data, v3s16(0, y, z), v3s16(1, 0, 0), //dir @@ -1009,7 +992,7 @@ static void applyTileColor(PreMeshBuffer &pmb) MapBlockBspTree */ -void MapBlockBspTree::buildTree(const std::vector *triangles) +void MapBlockBspTree::buildTree(const std::vector *triangles, u16 side_length) { this->triangles = triangles; @@ -1024,7 +1007,7 @@ void MapBlockBspTree::buildTree(const std::vector *triangles) if (!indexes.empty()) { // Start in the center of the block with increment of one quarter in each direction - root = buildTree(v3f(1, 0, 0), v3f((MAP_BLOCKSIZE + 1) * 0.5f * BS), MAP_BLOCKSIZE * 0.25f * BS, indexes, 0); + root = buildTree(v3f(1, 0, 0), v3f((side_length + 1) * 0.5f * BS), side_length * 0.25f * BS, indexes, 0); } else { root = -1; } @@ -1183,7 +1166,6 @@ void PartialMeshBuffer::afterDraw() const */ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): - m_minimap_mapblock(NULL), m_tsrc(data->m_client->getTextureSource()), m_shdrsrc(data->m_client->getShaderSource()), m_animation_force_timer(0), // force initial animation @@ -1195,10 +1177,23 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): m_enable_shaders = data->m_use_shaders; m_enable_vbo = g_settings->getBool("enable_vbo"); - if (data->m_client->getMinimap()) { - m_minimap_mapblock = new MinimapMapblock; - m_minimap_mapblock->getMinimapNodes( - &data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE); + v3s16 bp = data->m_blockpos; + // Only generate minimap mapblocks at even coordinates. + if (((bp.X | bp.Y | bp.Z) & 1) == 0 && data->m_client->getMinimap()) { + m_minimap_mapblocks.resize(8, nullptr); + v3s16 ofs; + + // See also client.cpp for the code that reads the array of minimap blocks. + for (ofs.Z = 0; ofs.Z <= 1; ofs.Z++) + for (ofs.Y = 0; ofs.Y <= 1; ofs.Y++) + for (ofs.X = 0; ofs.X <= 1; ofs.X++) { + v3s16 p = (bp + ofs) * MAP_BLOCKSIZE; + if (data->m_vmanip.getNodeNoEx(p).getContent() != CONTENT_IGNORE) { + MinimapMapblock *block = new MinimapMapblock; + m_minimap_mapblocks[ofs.Z * 4 + ofs.Y * 2 + ofs.X] = block; + block->getMinimapNodes(&data->m_vmanip, p); + } + } } // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated) @@ -1226,7 +1221,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): Convert FastFaces to MeshCollector */ - MeshCollector collector(m_bounding_sphere_center); + v3f offset = intToFloat((data->m_blockpos - data->m_blockpos / 8 * 8) * MAP_BLOCKSIZE, BS); + MeshCollector collector(m_bounding_sphere_center, offset); { // avg 0ms (100ms spikes when loading textures the first time) @@ -1386,7 +1382,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): } //std::cout<<"added "<side_length); // Check if animation is required for this mesh m_has_animation = @@ -1408,7 +1404,8 @@ MapBlockMesh::~MapBlockMesh() #endif m->drop(); } - delete m_minimap_mapblock; + for (MinimapMapblock *block : m_minimap_mapblocks) + delete block; } bool MapBlockMesh::animate(bool faraway, float time, int crack, @@ -1583,30 +1580,39 @@ video::SColor encode_light(u16 light, u8 emissive_light) return video::SColor(r, b, b, b); } -u8 get_solid_sides(MeshMakeData *data) +std::unordered_map get_solid_sides(MeshMakeData *data) { - v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE; - const NodeDefManager *ndef = data->m_client->ndef(); + std::unordered_map results; + v3s16 ofs; - u8 result = 0x3F; // all sides solid; + for (ofs.X = 0; ofs.X < 2; ofs.X++) + for (ofs.Y = 0; ofs.Y < 2; ofs.Y++) + for (ofs.Z = 0; ofs.Z < 2; ofs.Z++) { + v3s16 blockpos = data->m_blockpos + ofs; + v3s16 blockpos_nodes = blockpos * MAP_BLOCKSIZE; + const NodeDefManager *ndef = data->m_client->ndef(); - for (s16 i = 0; i < MAP_BLOCKSIZE && result != 0; i++) - for (s16 j = 0; j < MAP_BLOCKSIZE && result != 0; j++) { - v3s16 positions[6] = { - v3s16(0, i, j), - v3s16(MAP_BLOCKSIZE - 1, i, j), - v3s16(i, 0, j), - v3s16(i, MAP_BLOCKSIZE - 1, j), - v3s16(i, j, 0), - v3s16(i, j, MAP_BLOCKSIZE - 1) - }; + u8 result = 0x3F; // all sides solid; - for (u8 k = 0; k < 6; k++) { - const MapNode &top = data->m_vmanip.getNodeRefUnsafe(blockpos_nodes + positions[k]); - if (ndef->get(top).solidness != 2) - result &= ~(1 << k); + for (s16 i = 0; i < MAP_BLOCKSIZE && result != 0; i++) + for (s16 j = 0; j < MAP_BLOCKSIZE && result != 0; j++) { + v3s16 positions[6] = { + v3s16(0, i, j), + v3s16(MAP_BLOCKSIZE - 1, i, j), + v3s16(i, 0, j), + v3s16(i, MAP_BLOCKSIZE - 1, j), + v3s16(i, j, 0), + v3s16(i, j, MAP_BLOCKSIZE - 1) + }; + + for (u8 k = 0; k < 6; k++) { + const MapNode &top = data->m_vmanip.getNodeRefUnsafe(blockpos_nodes + positions[k]); + if (ndef->get(top).solidness != 2) + result &= ~(1 << k); + } } - } - return result; + results[blockpos] = result; + } + return results; } diff --git a/src/client/mapblock_mesh.h b/src/client/mapblock_mesh.h index 7f6185c3a..82e9a0f22 100644 --- a/src/client/mapblock_mesh.h +++ b/src/client/mapblock_mesh.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "voxel.h" #include #include +#include class Client; class IShaderSource; @@ -42,6 +43,7 @@ struct MeshMakeData v3s16 m_blockpos = v3s16(-1337,-1337,-1337); v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337); bool m_smooth_lighting = false; + u16 side_length = MAP_BLOCKSIZE; Client *m_client; bool m_use_shaders; @@ -54,12 +56,6 @@ struct MeshMakeData void fillBlockDataBegin(const v3s16 &blockpos); void fillBlockData(const v3s16 &block_offset, MapNode *data); - /* - Copy central data directly from block, and other data from - parent of block. - */ - void fill(MapBlock *block); - /* Set the (node) position of a crack */ @@ -108,7 +104,7 @@ class MapBlockBspTree public: MapBlockBspTree() {} - void buildTree(const std::vector *triangles); + void buildTree(const std::vector *triangles, u16 side_lingth); void traverse(v3f viewpoint, std::vector &output) const { @@ -203,11 +199,11 @@ public: return m_mesh[layer]; } - MinimapMapblock *moveMinimapMapblock() + std::vector moveMinimapMapblocks() { - MinimapMapblock *p = m_minimap_mapblock; - m_minimap_mapblock = NULL; - return p; + std::vector minimap_mapblocks; + minimap_mapblocks.swap(m_minimap_mapblocks); + return minimap_mapblocks; } bool isAnimationForced() const @@ -245,7 +241,7 @@ private: }; scene::IMesh *m_mesh[MAX_TILE_LAYERS]; - MinimapMapblock *m_minimap_mapblock; + std::vector m_minimap_mapblocks; ITextureSource *m_tsrc; IShaderSource *m_shdrsrc; @@ -344,4 +340,4 @@ void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *dat /// Return bitset of the sides of the mapblock that consist of solid nodes only /// Bits: /// 0 0 -Z +Z -X +X -Y +Y -u8 get_solid_sides(MeshMakeData *data); +std::unordered_map get_solid_sides(MeshMakeData *data); diff --git a/src/client/mesh_generator_thread.cpp b/src/client/mesh_generator_thread.cpp index adea057d6..6d1fe2bda 100644 --- a/src/client/mesh_generator_thread.cpp +++ b/src/client/mesh_generator_thread.cpp @@ -25,6 +25,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "map.h" #include "util/directiontables.h" +static class BlockPlaceholder { +public: + MapNode data[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE]; + + BlockPlaceholder() + { + for (std::size_t i = 0; i < MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE; i++) + data[i] = MapNode(CONTENT_IGNORE); + } + +} block_placeholder; /* QueuedMeshUpdate */ @@ -66,28 +77,31 @@ bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool MutexAutoLock lock(m_mutex); + // Mesh is placed at even positions at all coordinates + // (every 8-th block) and will cover 8 blocks + v3s16 mesh_position(p.X & ~1, p.Y & ~1, p.Z & ~1); /* Mark the block as urgent if requested */ if (urgent) - m_urgents.insert(p); + m_urgents.insert(mesh_position); /* Find if block is already in queue. If it is, update the data and quit. */ for (QueuedMeshUpdate *q : m_queue) { - if (q->p == p) { + if (q->p == mesh_position) { // NOTE: We are not adding a new position to the queue, thus // refcount_from_queue stays the same. if(ack_block_to_server) - q->ack_block_to_server = true; + q->ack_list.push_back(p); q->crack_level = m_client->getCrackLevel(); q->crack_pos = m_client->getCrackPos(); q->urgent |= urgent; - for (std::size_t i = 1; i < q->map_blocks.size(); i++) { + for (std::size_t i = 0; i < q->map_blocks.size(); i++) { if (!q->map_blocks[i]) { - MapBlock *block = map->getBlockNoCreateNoEx(q->p + g_26dirs[i - 1]); + MapBlock *block = map->getBlockNoCreateNoEx(q->p + g_64dirs[i]); if (block) { block->refGrab(); q->map_blocks[i] = block; @@ -99,16 +113,13 @@ bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool } /* - Cache the block data (force-update the center block, don't update the - neighbors but get them if they aren't already cached) + Make a list of blocks necessary for mesh generation and lock the blocks in memory. */ - std::vector cached_blocks; - cached_blocks.reserve(3*3*3); - cached_blocks.push_back(main_block); - main_block->refGrab(); - for (v3s16 dp : g_26dirs) { - MapBlock *block = map->getBlockNoCreateNoEx(p + dp); - cached_blocks.push_back(block); + std::vector map_blocks; + map_blocks.reserve(4*4*4); + for (v3s16 dp : g_64dirs) { + MapBlock *block = map->getBlockNoCreateNoEx(mesh_position + dp); + map_blocks.push_back(block); if (block) block->refGrab(); } @@ -117,12 +128,13 @@ bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool Add the block */ QueuedMeshUpdate *q = new QueuedMeshUpdate; - q->p = p; - q->ack_block_to_server = ack_block_to_server; + q->p = mesh_position; + if(ack_block_to_server) + q->ack_list.push_back(p); q->crack_level = m_client->getCrackLevel(); q->crack_pos = m_client->getCrackPos(); q->urgent = urgent; - q->map_blocks = std::move(cached_blocks); + q->map_blocks = std::move(map_blocks); m_queue.push_back(q); return true; @@ -170,12 +182,14 @@ void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q) { MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders); q->data = data; + data->side_length = 2 * MAP_BLOCKSIZE; data->fillBlockDataBegin(q->p); - for (MapBlock *block : q->map_blocks) - if (block) - data->fillBlockData(block->getPos() - q->p, block->getData()); + for (std::size_t i = 0; i < 64; i++) { + MapBlock *block = q->map_blocks[i]; + data->fillBlockData(g_64dirs[i], block ? block->getData() : block_placeholder.data); + } data->setCrack(q->crack_level, q->crack_pos); data->setSmoothLighting(m_cache_smooth_lighting); @@ -208,7 +222,7 @@ void MeshUpdateWorkerThread::doUpdate() r.p = q->p; r.mesh = mesh_new; r.solid_sides = get_solid_sides(q->data); - r.ack_block_to_server = q->ack_block_to_server; + r.ack_list = std::move(q->ack_list); r.urgent = q->urgent; r.map_blocks = q->map_blocks; diff --git a/src/client/mesh_generator_thread.h b/src/client/mesh_generator_thread.h index cfcb1df09..3097f3704 100644 --- a/src/client/mesh_generator_thread.h +++ b/src/client/mesh_generator_thread.h @@ -28,11 +28,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/thread.h" #include #include +#include struct QueuedMeshUpdate { v3s16 p = v3s16(-1337, -1337, -1337); - bool ack_block_to_server = false; + std::vector ack_list; int crack_level = -1; v3s16 crack_pos; MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop() @@ -96,8 +97,8 @@ struct MeshUpdateResult { v3s16 p = v3s16(-1338, -1338, -1338); MapBlockMesh *mesh = nullptr; - u8 solid_sides = 0; - bool ack_block_to_server = false; + std::unordered_map solid_sides; + std::vector ack_list; bool urgent = false; std::vector map_blocks; diff --git a/src/client/meshgen/collector.cpp b/src/client/meshgen/collector.cpp index c5f4eb976..86c188c3f 100644 --- a/src/client/meshgen/collector.cpp +++ b/src/client/meshgen/collector.cpp @@ -46,7 +46,7 @@ void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *verti u32 vertex_count = p.vertices.size(); for (u32 i = 0; i < numVertices; i++) { - p.vertices.emplace_back(vertices[i].Pos, vertices[i].Normal, + p.vertices.emplace_back(vertices[i].Pos + offset, vertices[i].Normal, vertices[i].Color, scale * vertices[i].TCoords); m_bounding_radius_sq = std::max(m_bounding_radius_sq, (vertices[i].Pos - m_center_pos).getLengthSQ()); @@ -84,7 +84,7 @@ void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *verti video::SColor color = c; if (!light_source) applyFacesShading(color, vertices[i].Normal); - auto vpos = vertices[i].Pos + pos; + auto vpos = vertices[i].Pos + pos + offset; p.vertices.emplace_back(vpos, vertices[i].Normal, color, scale * vertices[i].TCoords); m_bounding_radius_sq = std::max(m_bounding_radius_sq, diff --git a/src/client/meshgen/collector.h b/src/client/meshgen/collector.h index c390c53e7..9b49ce72e 100644 --- a/src/client/meshgen/collector.h +++ b/src/client/meshgen/collector.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include "irrlichttypes.h" +#include "irr_v3d.h" #include #include "client/tile.h" @@ -40,9 +41,11 @@ struct MeshCollector // bounding sphere radius and center f32 m_bounding_radius_sq = 0.0f; v3f m_center_pos; + v3f offset; // center_pos: pos to use for bounding-sphere, in BS-space - MeshCollector(const v3f center_pos) : m_center_pos(center_pos) {} + // offset: offset added to vertices + MeshCollector(const v3f center_pos, v3f offset = v3f()) : m_center_pos(center_pos), offset(offset) {} // clang-format off void append(const TileSpec &material, diff --git a/src/client/wieldmesh.cpp b/src/client/wieldmesh.cpp index 155b5ecc4..5030ea575 100644 --- a/src/client/wieldmesh.cpp +++ b/src/client/wieldmesh.cpp @@ -318,7 +318,7 @@ static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n, std::vector *colors, const ContentFeatures &f) { MeshMakeData mesh_make_data(client, false); - MeshCollector collector(v3f(0.0f * BS)); + MeshCollector collector(v3f(0.0f * BS), v3f()); mesh_make_data.setSmoothLighting(false); MapblockMeshGenerator gen(&mesh_make_data, &collector, client->getSceneManager()->getMeshManipulator()); diff --git a/src/map.cpp b/src/map.cpp index cfe5f126d..59d1a925d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -325,6 +325,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, u32 deleted_blocks_count = 0; u32 saved_blocks_count = 0; u32 block_count_all = 0; + u32 locked_blocks = 0; const auto start_time = porting::getTimeUs(); beginSave(); @@ -396,8 +397,10 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, MapBlock *block = b.block; - if (block->refGet() != 0) + if (block->refGet() != 0) { + locked_blocks++; continue; + } v3s16 p = block->getPos(); @@ -442,7 +445,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, <<" blocks from memory"; if(save_before_unloading) infostream<<", of which "<