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 <jwmhjwmh@gmail.com>
Co-authored-by: Lars <larsh@apache.org>
Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
x2048 2023-01-31 17:30:59 +01:00 committed by GitHub
parent cded6a3945
commit 69fc206109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 266 additions and 118 deletions

@ -41,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h" #include "filesys.h"
#include "mapblock_mesh.h" #include "mapblock_mesh.h"
#include "mapblock.h" #include "mapblock.h"
#include "mapsector.h"
#include "minimap.h" #include "minimap.h"
#include "modchannels.h" #include "modchannels.h"
#include "content/mods.h" #include "content/mods.h"
@ -555,19 +556,27 @@ void Client::step(float dtime)
{ {
num_processed_meshes++; num_processed_meshes++;
MinimapMapblock *minimap_mapblock = NULL; std::vector<MinimapMapblock*> minimap_mapblocks;
bool do_mapper_update = true; 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) { if (block) {
// Delete the old mesh // Delete the old mesh
delete block->mesh; delete block->mesh;
block->mesh = nullptr; block->mesh = nullptr;
if (r.mesh) { if (r.mesh) {
block->solid_sides = r.solid_sides; minimap_mapblocks = r.mesh->moveMinimapMapblocks();
minimap_mapblock = r.mesh->moveMinimapMapblock(); if (minimap_mapblocks.empty())
if (minimap_mapblock == NULL)
do_mapper_update = false; do_mapper_update = false;
bool is_empty = true; bool is_empty = true;
@ -588,16 +597,32 @@ void Client::step(float dtime)
delete r.mesh; delete r.mesh;
} }
if (m_minimap && do_mapper_update) for (auto p : r.solid_sides) {
m_minimap->addBlock(r.p, minimap_mapblock); 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) { if (blocks_to_ack.size() == 255) {
sendGotBlocks(blocks_to_ack); sendGotBlocks(blocks_to_ack);
blocks_to_ack.clear(); blocks_to_ack.clear();
} }
blocks_to_ack.emplace_back(r.p); blocks_to_ack.emplace_back(p);
} }
for (auto block : r.map_blocks) for (auto block : r.map_blocks)

@ -298,6 +298,8 @@ void ClientMap::updateDrawList()
blocks_to_consider.push(camera_block); blocks_to_consider.push(camera_block);
blocks_seen.getChunk(camera_block).getBits(camera_block) = 0x07; // mark all sides as visible blocks_seen.getChunk(camera_block).getBits(camera_block) = 0x07; // mark all sides as visible
std::set<v3s16> shortlist;
// Recursively walk the space and pick mapblocks for drawing // Recursively walk the space and pick mapblocks for drawing
while (blocks_to_consider.size() > 0) { while (blocks_to_consider.size() > 0) {
@ -369,11 +371,13 @@ void ClientMap::updateDrawList()
continue; continue;
} }
// The block is visible, add to the draw list // Block meshes are stored in blocks where all coordinates are even (lowest bit set to 0)
if (mesh) { // Add them to the de-dup set.
// Add to 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(); block->refGrab();
m_drawlist[block_coord] = block;
} }
// Decide which sides to traverse next or to block away // 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 occlusion culled [#]", blocks_occlusion_culled);
g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped); g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped);
g_profiler->avg("MapBlocks examined [#]", blocks_visited); g_profiler->avg("MapBlocks examined [#]", blocks_visited);
@ -597,7 +611,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
MapBlock *block = i.second; MapBlock *block = i.second;
MapBlockMesh *block_mesh = block->mesh; 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) if (!block_mesh)
continue; continue;
@ -720,7 +738,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
material.TextureLayer[ShadowRenderer::TEXTURE_LAYER_SHADOW].Texture = nullptr; 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); m.setTranslation(block_wpos - offset);
driver->setTransform(video::ETS_WORLD, m); driver->setTransform(video::ETS_WORLD, m);
@ -1046,7 +1064,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
++material_swaps; ++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); m.setTranslation(block_wpos - offset);
driver->setTransform(video::ETS_WORLD, m); 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); 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() void ClientMap::updateTransparentMeshBuffers()
{ {
ScopeProfiler sp(g_profiler, "CM::updateTransparentMeshBuffers", SPT_AVG); ScopeProfiler sp(g_profiler, "CM::updateTransparentMeshBuffers", SPT_AVG);

@ -140,6 +140,8 @@ public:
void onSettingChanged(const std::string &name); void onSettingChanged(const std::string &name);
protected:
void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
private: private:
// update the vertex order in transparent mesh buffers // update the vertex order in transparent mesh buffers

@ -1619,9 +1619,9 @@ void MapblockMeshGenerator::drawNode()
*/ */
void MapblockMeshGenerator::generate() void MapblockMeshGenerator::generate()
{ {
for (p.Z = 0; p.Z < MAP_BLOCKSIZE; p.Z++) for (p.Z = 0; p.Z < data->side_length; p.Z++)
for (p.Y = 0; p.Y < MAP_BLOCKSIZE; p.Y++) for (p.Y = 0; p.Y < data->side_length; p.Y++)
for (p.X = 0; p.X < MAP_BLOCKSIZE; p.X++) { for (p.X = 0; p.X < data->side_length; p.X++) {
n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p); n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
f = &nodedef->get(n); f = &nodedef->get(n);
drawNode(); drawNode();

@ -49,7 +49,7 @@ void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos)
m_vmanip.clear(); m_vmanip.clear();
VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE, 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); 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); 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) void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
{ {
if (crack_level >= 0) if (crack_level >= 0)
@ -883,7 +866,7 @@ static void updateFastFaceRow(
// Unroll this variable which has a significant build cost // Unroll this variable which has a significant build cost
TileSpec next_tile; 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 // If tiling can be done, this is set to false in the next step
bool next_is_different = true; bool next_is_different = true;
@ -894,7 +877,7 @@ static void updateFastFaceRow(
// If at last position, there is nothing to compare to and // If at last position, there is nothing to compare to and
// the face must be drawn anyway // the face must be drawn anyway
if (j != MAP_BLOCKSIZE - 1) { if (j != data->side_length - 1) {
p += translate_dir; p += translate_dir;
getTileInfo(data, p, face_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+ Go through every y,z and get top(y+) faces in rows of x+
*/ */
for (s16 y = 0; y < MAP_BLOCKSIZE; y++) for (s16 y = 0; y < data->side_length; y++)
for (s16 z = 0; z < MAP_BLOCKSIZE; z++) for (s16 z = 0; z < data->side_length; z++)
updateFastFaceRow(data, updateFastFaceRow(data,
v3s16(0, y, z), v3s16(0, y, z),
v3s16(1, 0, 0), //dir 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+ Go through every x,y and get right(x+) faces in rows of z+
*/ */
for (s16 x = 0; x < MAP_BLOCKSIZE; x++) for (s16 x = 0; x < data->side_length; x++)
for (s16 y = 0; y < MAP_BLOCKSIZE; y++) for (s16 y = 0; y < data->side_length; y++)
updateFastFaceRow(data, updateFastFaceRow(data,
v3s16(x, y, 0), v3s16(x, y, 0),
v3s16(0, 0, 1), //dir 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+ Go through every y,z and get back(z+) faces in rows of x+
*/ */
for (s16 z = 0; z < MAP_BLOCKSIZE; z++) for (s16 z = 0; z < data->side_length; z++)
for (s16 y = 0; y < MAP_BLOCKSIZE; y++) for (s16 y = 0; y < data->side_length; y++)
updateFastFaceRow(data, updateFastFaceRow(data,
v3s16(0, y, z), v3s16(0, y, z),
v3s16(1, 0, 0), //dir v3s16(1, 0, 0), //dir
@ -1009,7 +992,7 @@ static void applyTileColor(PreMeshBuffer &pmb)
MapBlockBspTree MapBlockBspTree
*/ */
void MapBlockBspTree::buildTree(const std::vector<MeshTriangle> *triangles) void MapBlockBspTree::buildTree(const std::vector<MeshTriangle> *triangles, u16 side_length)
{ {
this->triangles = triangles; this->triangles = triangles;
@ -1024,7 +1007,7 @@ void MapBlockBspTree::buildTree(const std::vector<MeshTriangle> *triangles)
if (!indexes.empty()) { if (!indexes.empty()) {
// Start in the center of the block with increment of one quarter in each direction // 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 { } else {
root = -1; root = -1;
} }
@ -1183,7 +1166,6 @@ void PartialMeshBuffer::afterDraw() const
*/ */
MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
m_minimap_mapblock(NULL),
m_tsrc(data->m_client->getTextureSource()), m_tsrc(data->m_client->getTextureSource()),
m_shdrsrc(data->m_client->getShaderSource()), m_shdrsrc(data->m_client->getShaderSource()),
m_animation_force_timer(0), // force initial animation 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_shaders = data->m_use_shaders;
m_enable_vbo = g_settings->getBool("enable_vbo"); m_enable_vbo = g_settings->getBool("enable_vbo");
if (data->m_client->getMinimap()) { v3s16 bp = data->m_blockpos;
m_minimap_mapblock = new MinimapMapblock; // Only generate minimap mapblocks at even coordinates.
m_minimap_mapblock->getMinimapNodes( if (((bp.X | bp.Y | bp.Z) & 1) == 0 && data->m_client->getMinimap()) {
&data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE); 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) // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
@ -1226,7 +1221,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
Convert FastFaces to MeshCollector 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) // avg 0ms (100ms spikes when loading textures the first time)
@ -1386,7 +1382,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
} }
//std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl; //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
m_bsp_tree.buildTree(&m_transparent_triangles); m_bsp_tree.buildTree(&m_transparent_triangles, data->side_length);
// Check if animation is required for this mesh // Check if animation is required for this mesh
m_has_animation = m_has_animation =
@ -1408,7 +1404,8 @@ MapBlockMesh::~MapBlockMesh()
#endif #endif
m->drop(); m->drop();
} }
delete m_minimap_mapblock; for (MinimapMapblock *block : m_minimap_mapblocks)
delete block;
} }
bool MapBlockMesh::animate(bool faraway, float time, int crack, 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); return video::SColor(r, b, b, b);
} }
u8 get_solid_sides(MeshMakeData *data) std::unordered_map<v3s16, u8> get_solid_sides(MeshMakeData *data)
{ {
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE; std::unordered_map<v3s16, u8> results;
const NodeDefManager *ndef = data->m_client->ndef(); 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++) u8 result = 0x3F; // all sides solid;
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++) { for (s16 i = 0; i < MAP_BLOCKSIZE && result != 0; i++)
const MapNode &top = data->m_vmanip.getNodeRefUnsafe(blockpos_nodes + positions[k]); for (s16 j = 0; j < MAP_BLOCKSIZE && result != 0; j++) {
if (ndef->get(top).solidness != 2) v3s16 positions[6] = {
result &= ~(1 << k); 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;
} }

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "voxel.h" #include "voxel.h"
#include <array> #include <array>
#include <map> #include <map>
#include <unordered_map>
class Client; class Client;
class IShaderSource; class IShaderSource;
@ -42,6 +43,7 @@ struct MeshMakeData
v3s16 m_blockpos = v3s16(-1337,-1337,-1337); v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337); v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
bool m_smooth_lighting = false; bool m_smooth_lighting = false;
u16 side_length = MAP_BLOCKSIZE;
Client *m_client; Client *m_client;
bool m_use_shaders; bool m_use_shaders;
@ -54,12 +56,6 @@ struct MeshMakeData
void fillBlockDataBegin(const v3s16 &blockpos); void fillBlockDataBegin(const v3s16 &blockpos);
void fillBlockData(const v3s16 &block_offset, MapNode *data); 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 Set the (node) position of a crack
*/ */
@ -108,7 +104,7 @@ class MapBlockBspTree
public: public:
MapBlockBspTree() {} MapBlockBspTree() {}
void buildTree(const std::vector<MeshTriangle> *triangles); void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
void traverse(v3f viewpoint, std::vector<s32> &output) const void traverse(v3f viewpoint, std::vector<s32> &output) const
{ {
@ -203,11 +199,11 @@ public:
return m_mesh[layer]; return m_mesh[layer];
} }
MinimapMapblock *moveMinimapMapblock() std::vector<MinimapMapblock*> moveMinimapMapblocks()
{ {
MinimapMapblock *p = m_minimap_mapblock; std::vector<MinimapMapblock*> minimap_mapblocks;
m_minimap_mapblock = NULL; minimap_mapblocks.swap(m_minimap_mapblocks);
return p; return minimap_mapblocks;
} }
bool isAnimationForced() const bool isAnimationForced() const
@ -245,7 +241,7 @@ private:
}; };
scene::IMesh *m_mesh[MAX_TILE_LAYERS]; scene::IMesh *m_mesh[MAX_TILE_LAYERS];
MinimapMapblock *m_minimap_mapblock; std::vector<MinimapMapblock*> m_minimap_mapblocks;
ITextureSource *m_tsrc; ITextureSource *m_tsrc;
IShaderSource *m_shdrsrc; 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 /// Return bitset of the sides of the mapblock that consist of solid nodes only
/// Bits: /// Bits:
/// 0 0 -Z +Z -X +X -Y +Y /// 0 0 -Z +Z -X +X -Y +Y
u8 get_solid_sides(MeshMakeData *data); std::unordered_map<v3s16, u8> get_solid_sides(MeshMakeData *data);

@ -25,6 +25,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "map.h" #include "map.h"
#include "util/directiontables.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 QueuedMeshUpdate
*/ */
@ -66,28 +77,31 @@ bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool
MutexAutoLock lock(m_mutex); 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 Mark the block as urgent if requested
*/ */
if (urgent) if (urgent)
m_urgents.insert(p); m_urgents.insert(mesh_position);
/* /*
Find if block is already in queue. Find if block is already in queue.
If it is, update the data and quit. If it is, update the data and quit.
*/ */
for (QueuedMeshUpdate *q : m_queue) { 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 // NOTE: We are not adding a new position to the queue, thus
// refcount_from_queue stays the same. // refcount_from_queue stays the same.
if(ack_block_to_server) 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_level = m_client->getCrackLevel();
q->crack_pos = m_client->getCrackPos(); q->crack_pos = m_client->getCrackPos();
q->urgent |= urgent; 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]) { 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) { if (block) {
block->refGrab(); block->refGrab();
q->map_blocks[i] = block; 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 Make a list of blocks necessary for mesh generation and lock the blocks in memory.
neighbors but get them if they aren't already cached)
*/ */
std::vector<MapBlock *> cached_blocks; std::vector<MapBlock *> map_blocks;
cached_blocks.reserve(3*3*3); map_blocks.reserve(4*4*4);
cached_blocks.push_back(main_block); for (v3s16 dp : g_64dirs) {
main_block->refGrab(); MapBlock *block = map->getBlockNoCreateNoEx(mesh_position + dp);
for (v3s16 dp : g_26dirs) { map_blocks.push_back(block);
MapBlock *block = map->getBlockNoCreateNoEx(p + dp);
cached_blocks.push_back(block);
if (block) if (block)
block->refGrab(); block->refGrab();
} }
@ -117,12 +128,13 @@ bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool
Add the block Add the block
*/ */
QueuedMeshUpdate *q = new QueuedMeshUpdate; QueuedMeshUpdate *q = new QueuedMeshUpdate;
q->p = p; q->p = mesh_position;
q->ack_block_to_server = ack_block_to_server; if(ack_block_to_server)
q->ack_list.push_back(p);
q->crack_level = m_client->getCrackLevel(); q->crack_level = m_client->getCrackLevel();
q->crack_pos = m_client->getCrackPos(); q->crack_pos = m_client->getCrackPos();
q->urgent = urgent; q->urgent = urgent;
q->map_blocks = std::move(cached_blocks); q->map_blocks = std::move(map_blocks);
m_queue.push_back(q); m_queue.push_back(q);
return true; return true;
@ -170,12 +182,14 @@ void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q)
{ {
MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders); MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders);
q->data = data; q->data = data;
data->side_length = 2 * MAP_BLOCKSIZE;
data->fillBlockDataBegin(q->p); data->fillBlockDataBegin(q->p);
for (MapBlock *block : q->map_blocks) for (std::size_t i = 0; i < 64; i++) {
if (block) MapBlock *block = q->map_blocks[i];
data->fillBlockData(block->getPos() - q->p, block->getData()); data->fillBlockData(g_64dirs[i], block ? block->getData() : block_placeholder.data);
}
data->setCrack(q->crack_level, q->crack_pos); data->setCrack(q->crack_level, q->crack_pos);
data->setSmoothLighting(m_cache_smooth_lighting); data->setSmoothLighting(m_cache_smooth_lighting);
@ -208,7 +222,7 @@ void MeshUpdateWorkerThread::doUpdate()
r.p = q->p; r.p = q->p;
r.mesh = mesh_new; r.mesh = mesh_new;
r.solid_sides = get_solid_sides(q->data); 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.urgent = q->urgent;
r.map_blocks = q->map_blocks; r.map_blocks = q->map_blocks;

@ -28,11 +28,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/thread.h" #include "util/thread.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <unordered_map>
struct QueuedMeshUpdate struct QueuedMeshUpdate
{ {
v3s16 p = v3s16(-1337, -1337, -1337); v3s16 p = v3s16(-1337, -1337, -1337);
bool ack_block_to_server = false; std::vector<v3s16> ack_list;
int crack_level = -1; int crack_level = -1;
v3s16 crack_pos; v3s16 crack_pos;
MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop() MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
@ -96,8 +97,8 @@ struct MeshUpdateResult
{ {
v3s16 p = v3s16(-1338, -1338, -1338); v3s16 p = v3s16(-1338, -1338, -1338);
MapBlockMesh *mesh = nullptr; MapBlockMesh *mesh = nullptr;
u8 solid_sides = 0; std::unordered_map<v3s16, u8> solid_sides;
bool ack_block_to_server = false; std::vector<v3s16> ack_list;
bool urgent = false; bool urgent = false;
std::vector<MapBlock *> map_blocks; std::vector<MapBlock *> map_blocks;

@ -46,7 +46,7 @@ void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *verti
u32 vertex_count = p.vertices.size(); u32 vertex_count = p.vertices.size();
for (u32 i = 0; i < numVertices; i++) { 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); vertices[i].Color, scale * vertices[i].TCoords);
m_bounding_radius_sq = std::max(m_bounding_radius_sq, m_bounding_radius_sq = std::max(m_bounding_radius_sq,
(vertices[i].Pos - m_center_pos).getLengthSQ()); (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; video::SColor color = c;
if (!light_source) if (!light_source)
applyFacesShading(color, vertices[i].Normal); 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, p.vertices.emplace_back(vpos, vertices[i].Normal, color,
scale * vertices[i].TCoords); scale * vertices[i].TCoords);
m_bounding_radius_sq = std::max(m_bounding_radius_sq, m_bounding_radius_sq = std::max(m_bounding_radius_sq,

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <array> #include <array>
#include <vector> #include <vector>
#include "irrlichttypes.h" #include "irrlichttypes.h"
#include "irr_v3d.h"
#include <S3DVertex.h> #include <S3DVertex.h>
#include "client/tile.h" #include "client/tile.h"
@ -40,9 +41,11 @@ struct MeshCollector
// bounding sphere radius and center // bounding sphere radius and center
f32 m_bounding_radius_sq = 0.0f; f32 m_bounding_radius_sq = 0.0f;
v3f m_center_pos; v3f m_center_pos;
v3f offset;
// center_pos: pos to use for bounding-sphere, in BS-space // 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 // clang-format off
void append(const TileSpec &material, void append(const TileSpec &material,

@ -318,7 +318,7 @@ static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
std::vector<ItemPartColor> *colors, const ContentFeatures &f) std::vector<ItemPartColor> *colors, const ContentFeatures &f)
{ {
MeshMakeData mesh_make_data(client, false); MeshMakeData mesh_make_data(client, false);
MeshCollector collector(v3f(0.0f * BS)); MeshCollector collector(v3f(0.0f * BS), v3f());
mesh_make_data.setSmoothLighting(false); mesh_make_data.setSmoothLighting(false);
MapblockMeshGenerator gen(&mesh_make_data, &collector, MapblockMeshGenerator gen(&mesh_make_data, &collector,
client->getSceneManager()->getMeshManipulator()); client->getSceneManager()->getMeshManipulator());

@ -325,6 +325,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
u32 deleted_blocks_count = 0; u32 deleted_blocks_count = 0;
u32 saved_blocks_count = 0; u32 saved_blocks_count = 0;
u32 block_count_all = 0; u32 block_count_all = 0;
u32 locked_blocks = 0;
const auto start_time = porting::getTimeUs(); const auto start_time = porting::getTimeUs();
beginSave(); beginSave();
@ -396,8 +397,10 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
MapBlock *block = b.block; MapBlock *block = b.block;
if (block->refGet() != 0) if (block->refGet() != 0) {
locked_blocks++;
continue; continue;
}
v3s16 p = block->getPos(); v3s16 p = block->getPos();
@ -442,7 +445,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
<<" blocks from memory"; <<" blocks from memory";
if(save_before_unloading) if(save_before_unloading)
infostream<<", of which "<<saved_blocks_count<<" were written"; infostream<<", of which "<<saved_blocks_count<<" were written";
infostream<<", "<<block_count_all<<" blocks in memory"; infostream<<", "<<block_count_all<<" blocks in memory, " << locked_blocks << " locked";
infostream<<"."<<std::endl; infostream<<"."<<std::endl;
if(saved_blocks_count != 0){ if(saved_blocks_count != 0){
PrintInfo(infostream); // ServerMap/ClientMap: PrintInfo(infostream); // ServerMap/ClientMap:

@ -110,6 +110,78 @@ const v3s16 g_27dirs[27] =
v3s16(0,0,0), v3s16(0,0,0),
}; };
const v3s16 g_64dirs[64] =
{
// +right, +top, +back
v3s16( -1, -1, -1),
v3s16( -1, 0, -1),
v3s16( -1, 1, -1),
v3s16( -1, 2, -1),
v3s16( -1, -1, 0),
v3s16( -1, 0, 0),
v3s16( -1, 1, 0),
v3s16( -1, 2, 0),
v3s16( -1, -1, 1),
v3s16( -1, 0, 1),
v3s16( -1, 1, 1),
v3s16( -1, 2, 1),
v3s16( -1, -1, 2),
v3s16( -1, 0, 2),
v3s16( -1, 1, 2),
v3s16( -1, 2, 2),
v3s16( 0, -1, -1),
v3s16( 0, 0, -1),
v3s16( 0, 1, -1),
v3s16( 0, 2, -1),
v3s16( 0, -1, 0),
v3s16( 0, 0, 0),
v3s16( 0, 1, 0),
v3s16( 0, 2, 0),
v3s16( 0, -1, 1),
v3s16( 0, 0, 1),
v3s16( 0, 1, 1),
v3s16( 0, 2, 1),
v3s16( 0, -1, 2),
v3s16( 0, 0, 2),
v3s16( 0, 1, 2),
v3s16( 0, 2, 2),
v3s16( 1, -1, -1),
v3s16( 1, 0, -1),
v3s16( 1, 1, -1),
v3s16( 1, 2, -1),
v3s16( 1, -1, 0),
v3s16( 1, 0, 0),
v3s16( 1, 1, 0),
v3s16( 1, 2, 0),
v3s16( 1, -1, 1),
v3s16( 1, 0, 1),
v3s16( 1, 1, 1),
v3s16( 1, 2, 1),
v3s16( 1, -1, 2),
v3s16( 1, 0, 2),
v3s16( 1, 1, 2),
v3s16( 1, 2, 2),
v3s16( 2, -1, -1),
v3s16( 2, 0, -1),
v3s16( 2, 1, -1),
v3s16( 2, 2, -1),
v3s16( 2, -1, 0),
v3s16( 2, 0, 0),
v3s16( 2, 1, 0),
v3s16( 2, 2, 0),
v3s16( 2, -1, 1),
v3s16( 2, 0, 1),
v3s16( 2, 1, 1),
v3s16( 2, 2, 1),
v3s16( 2, -1, 2),
v3s16( 2, 0, 2),
v3s16( 2, 1, 2),
v3s16( 2, 2, 2),
};
const u8 wallmounted_to_facedir[6] = { const u8 wallmounted_to_facedir[6] = {
20, 20,
0, 0,

@ -31,6 +31,9 @@ extern const v3s16 g_26dirs[26];
// 26th is (0,0,0) // 26th is (0,0,0)
extern const v3s16 g_27dirs[27]; extern const v3s16 g_27dirs[27];
// all positions around an octablock in sector-first order
extern const v3s16 g_64dirs[64];
extern const u8 wallmounted_to_facedir[6]; extern const u8 wallmounted_to_facedir[6];
extern const v3s16 wallmounted_dirs[8]; extern const v3s16 wallmounted_dirs[8];