Scale culler steps proportionally to the mesh sizes (#13250)

This commit is contained in:
x2048 2023-03-11 14:10:26 +01:00 committed by GitHub
parent 1de8a1e962
commit 705195b43e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 63 deletions

@ -624,7 +624,8 @@ update_last_checked (Last update check) string
update_last_known (Last known version update) int 0
# Use raytraced occlusion culling in the new culler.
# This flag enables use of raytraced occlusion culling test
# This flag enables use of raytraced occlusion culling test for
# client mesh sizes smaller than 4x4x4 map blocks.
enable_raytraced_culling (Enable Raytraced Culling) bool true
[*Server]

@ -574,6 +574,7 @@ void Client::step(float dtime)
// Delete the old mesh
delete block->mesh;
block->mesh = nullptr;
block->solid_sides = r.solid_sides;
if (r.mesh) {
minimap_mapblocks = r.mesh->moveMinimapMapblocks();
@ -598,12 +599,6 @@ void Client::step(float dtime)
delete r.mesh;
}
for (auto p : r.solid_sides) {
auto block = m_env.getMap().getBlockNoCreateNoEx(p.first);
if (block)
block->solid_sides = p.second;
}
if (m_minimap && do_mapper_update) {
v3s16 ofs;

@ -270,13 +270,19 @@ void ClientMap::updateDrawList()
// Number of blocks occlusion culled
u32 blocks_occlusion_culled = 0;
// Number of blocks frustum culled
u32 blocks_frustum_culled = 0;
// Blocks visited by the algorithm
u32 blocks_visited = 0;
// Block sides that were not traversed
u32 sides_skipped = 0;
MeshGrid mesh_grid = m_client->getMeshGrid();
// No occlusion culling when free_move is on and camera is inside ground
bool occlusion_culling_enabled = true;
// No occlusion culling for chunk sizes of 4 and above
// because the current occlusion culling test is highly inefficient at these sizes
bool occlusion_culling_enabled = mesh_grid.cell_size < 4;
if (m_control.allow_noclip) {
MapNode n = getNode(cam_pos_nodes);
if (n.getContent() == CONTENT_IGNORE || m_nodedef->get(n).solidness == 2)
@ -295,16 +301,18 @@ void ClientMap::updateDrawList()
std::queue<v3s16> blocks_to_consider;
v3s16 camera_mesh = mesh_grid.getMeshPos(camera_block);
v3s16 camera_cell = mesh_grid.getCellPos(camera_block);
// Bits per block:
// [ visited | 0 | 0 | 0 | 0 | Z visible | Y visible | X visible ]
MapBlockFlags blocks_seen(p_blocks_min, p_blocks_max);
MapBlockFlags meshes_seen(mesh_grid.getCellPos(p_blocks_min), mesh_grid.getCellPos(p_blocks_max) + 1);
// Start breadth-first search with the block the camera is in
blocks_to_consider.push(camera_block);
blocks_seen.getChunk(camera_block).getBits(camera_block) = 0x07; // mark all sides as visible
blocks_to_consider.push(camera_mesh);
meshes_seen.getChunk(camera_cell).getBits(camera_cell) = 0x07; // mark all sides as visible
std::set<v3s16> shortlist;
MeshGrid mesh_grid = m_client->getMeshGrid();
// Recursively walk the space and pick mapblocks for drawing
while (blocks_to_consider.size() > 0) {
@ -312,7 +320,8 @@ void ClientMap::updateDrawList()
v3s16 block_coord = blocks_to_consider.front();
blocks_to_consider.pop();
auto &flags = blocks_seen.getChunk(block_coord).getBits(block_coord);
v3s16 cell_coord = mesh_grid.getCellPos(block_coord);
auto &flags = meshes_seen.getChunk(cell_coord).getBits(cell_coord);
// Only visit each block once (it may have been queued up to three times)
if ((flags & 0x80) == 0x80)
@ -324,14 +333,11 @@ void ClientMap::updateDrawList()
// Get the sector, block and mesh
MapSector *sector = this->getSectorNoGenerate(v2s16(block_coord.X, block_coord.Z));
if (!sector)
continue;
MapBlock *block = sector->getBlockNoCreateNoEx(block_coord.Y);
MapBlock *block = sector ? sector->getBlockNoCreateNoEx(block_coord.Y) : nullptr;
MapBlockMesh *mesh = block ? block->mesh : nullptr;
// Calculate the coordinates for range and frutum culling
// Calculate the coordinates for range and frustum culling
v3f mesh_sphere_center;
f32 mesh_sphere_radius;
@ -343,8 +349,8 @@ void ClientMap::updateDrawList()
mesh_sphere_radius = mesh->getBoundingRadius();
}
else {
mesh_sphere_center = intToFloat(block_pos_nodes, BS) + v3f((MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
mesh_sphere_radius = 0.0f;
mesh_sphere_center = intToFloat(block_pos_nodes, BS) + v3f((mesh_grid.cell_size * MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
mesh_sphere_radius = 0.87f * mesh_grid.cell_size * MAP_BLOCKSIZE * BS;
}
// First, perform a simple distance check.
@ -358,12 +364,14 @@ void ClientMap::updateDrawList()
// This is needed because this function is not called every frame.
float frustum_cull_extra_radius = 300.0f;
if (is_frustum_culled(mesh_sphere_center,
mesh_sphere_radius + frustum_cull_extra_radius))
mesh_sphere_radius + frustum_cull_extra_radius)) {
blocks_frustum_culled++;
continue;
}
// Calculate the vector from the camera block to the current block
// We use it to determine through which sides of the current block we can continue the search
v3s16 look = block_coord - camera_block;
v3s16 look = block_coord - camera_mesh;
// Occluded near sides will further occlude the far sides
u8 visible_outer_sides = flags & 0x07;
@ -371,7 +379,7 @@ void ClientMap::updateDrawList()
// Raytraced occlusion culling - send rays from the camera to the block's corners
if (occlusion_culling_enabled && m_enable_raytraced_culling &&
block && mesh &&
visible_outer_sides != 0x07 && isBlockOccluded(block, cam_pos_nodes)) {
visible_outer_sides != 0x07 && isMeshOccluded(block, mesh_grid.cell_size, cam_pos_nodes)) {
blocks_occlusion_culled++;
continue;
}
@ -380,7 +388,7 @@ void ClientMap::updateDrawList()
// Block meshes are stored in the corner block of a chunk
// (where all coordinate are divisible by the chunk size)
// Add them to the de-dup set.
shortlist.emplace(mesh_grid.getMeshPos(block_coord.X), mesh_grid.getMeshPos(block_coord.Y), mesh_grid.getMeshPos(block_coord.Z));
shortlist.emplace(block_coord.X, block_coord.Y, block_coord.Z);
// All other blocks we can grab and add to the keeplist right away.
if (block) {
m_keeplist.push_back(block);
@ -438,7 +446,7 @@ void ClientMap::updateDrawList()
// Calculate vector from camera to mapblock center. Because we only need relation between
// coordinates we scale by 2 to avoid precision loss.
v3s16 precise_look = 2 * (block_pos_nodes - cam_pos_nodes) + MAP_BLOCKSIZE - 1;
v3s16 precise_look = 2 * (block_pos_nodes - cam_pos_nodes) + mesh_grid.cell_size * MAP_BLOCKSIZE - 1;
// dominant axis flag
u8 dominant_axis = (abs(precise_look.X) > abs(precise_look.Y) && abs(precise_look.X) > abs(precise_look.Z)) |
@ -467,9 +475,11 @@ void ClientMap::updateDrawList()
v3s16 next_pos = block_coord;
next_pos[axis] += next_pos_offset;
v3s16 next_cell = mesh_grid.getCellPos(next_pos);
// If a side is a see-through, mark the next block's side as visible, and queue
if (side_visible) {
auto &next_flags = blocks_seen.getChunk(next_pos).getBits(next_pos);
auto &next_flags = meshes_seen.getChunk(next_cell).getBits(next_cell);
next_flags |= my_side;
blocks_to_consider.push(next_pos);
}
@ -481,13 +491,13 @@ void ClientMap::updateDrawList()
// Test the '-' direction of the axis
if (look[axis] <= 0 && block_coord[axis] > p_blocks_min[axis])
traverse_far_side(-1);
traverse_far_side(-mesh_grid.cell_size);
// Test the '+' direction of the axis
far_side_mask <<= 1;
if (look[axis] >= 0 && block_coord[axis] < p_blocks_max[axis])
traverse_far_side(+1);
traverse_far_side(+mesh_grid.cell_size);
}
}
@ -503,6 +513,7 @@ void ClientMap::updateDrawList()
}
g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled);
g_profiler->avg("MapBlocks frustum culled [#]", blocks_frustum_culled);
g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped);
g_profiler->avg("MapBlocks examined [#]", blocks_visited);
g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
@ -521,6 +532,8 @@ void ClientMap::touchMapBlocks()
// Number of blocks with mesh in rendering range
u32 blocks_in_range_with_mesh = 0;
v3f cam_pos_f = intToFloat(cam_pos_nodes, BS);
for (const auto &sector_it : m_sectors) {
MapSector *sector = sector_it.second;
v2s16 sp = sector->getPos();
@ -553,9 +566,10 @@ void ClientMap::touchMapBlocks()
v3f mesh_sphere_center = intToFloat(block->getPosRelative(), BS)
+ block->mesh->getBoundingSphereCenter();
f32 mesh_sphere_radius = block->mesh->getBoundingRadius();
// First, perform a simple distance check.
if (!m_control.range_all &&
mesh_sphere_center.getDistanceFrom(intToFloat(cam_pos_nodes, BS)) >
mesh_sphere_center.getDistanceFrom(cam_pos_f) >
m_control.wanted_range * BS + mesh_sphere_radius)
continue; // Out of range, skip.
@ -564,6 +578,7 @@ void ClientMap::touchMapBlocks()
blocks_in_range_with_mesh++;
}
}
g_profiler->avg("MapBlock meshes in range [#]", blocks_in_range_with_mesh);
g_profiler->avg("MapBlocks loaded [#]", blocks_loaded);
}
@ -1239,3 +1254,54 @@ void ClientMap::DrawDescriptor::draw(video::IVideoDriver* driver)
driver->drawMeshBuffer(m_buffer);
}
}
bool ClientMap::isMeshOccluded(MapBlock *mesh_block, u16 mesh_size, v3s16 cam_pos_nodes)
{
if (mesh_size == 1)
return isBlockOccluded(mesh_block, cam_pos_nodes);
v3s16 min_edge = mesh_block->getPosRelative();
v3s16 max_edge = min_edge + mesh_size * MAP_BLOCKSIZE -1;
bool check_axis[3] = { false, false, false };
u16 closest_side[3] = { 0, 0, 0 };
for (int axis = 0; axis < 3; axis++) {
if (cam_pos_nodes[axis] < min_edge[axis])
check_axis[axis] = true;
else if (cam_pos_nodes[axis] > max_edge[axis]) {
check_axis[axis] = true;
closest_side[axis] = mesh_size - 1;
}
}
std::vector<bool> processed_blocks(mesh_size * mesh_size * mesh_size);
// scan the side
for (u16 i = 0; i < mesh_size; i++)
for (u16 j = 0; j < mesh_size; j++) {
v3s16 offsets[3] = {
v3s16(closest_side[0], i, j),
v3s16(i, closest_side[1], j),
v3s16(i, j, closest_side[2])
};
for (int axis = 0; axis < 3; axis++) {
v3s16 offset = offsets[axis];
int block_index = offset.X + offset.Y * mesh_size + offset.Z * mesh_size * mesh_size;
if (check_axis[axis] && !processed_blocks[block_index]) {
processed_blocks[block_index] = true;
v3s16 block_pos = mesh_block->getPos() + offset;
MapBlock *block;
if (mesh_block->getPos() == block_pos)
block = mesh_block;
else
block = getBlockNoCreateNoEx(block_pos);
if (block && !isBlockOccluded(block, cam_pos_nodes))
return false;
}
}
}
return true;
}

@ -143,6 +143,7 @@ public:
protected:
void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
private:
bool isMeshOccluded(MapBlock *mesh_block, u16 mesh_size, v3s16 cam_pos_nodes);
// update the vertex order in transparent mesh buffers
void updateTransparentMeshBuffers();

@ -1169,6 +1169,7 @@ void PartialMeshBuffer::afterDraw() const
MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
m_tsrc(data->m_client->getTextureSource()),
m_shdrsrc(data->m_client->getShaderSource()),
m_bounding_sphere_center((data->side_length * 0.5f - 0.5f) * BS),
m_animation_force_timer(0), // force initial animation
m_last_crack(-1),
m_last_daynight_ratio((u32) -1)
@ -1573,40 +1574,31 @@ video::SColor encode_light(u16 light, u8 emissive_light)
return video::SColor(r, b, b, b);
}
std::unordered_map<v3s16, u8> get_solid_sides(MeshMakeData *data)
u8 get_solid_sides(MeshMakeData *data)
{
std::unordered_map<v3s16, u8> results;
v3s16 ofs;
const u16 mesh_chunk = data->side_length / MAP_BLOCKSIZE;
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
const NodeDefManager *ndef = data->m_client->ndef();
for (ofs.X = 0; ofs.X < mesh_chunk; ofs.X++)
for (ofs.Y = 0; ofs.Y < mesh_chunk; ofs.Y++)
for (ofs.Z = 0; ofs.Z < mesh_chunk; ofs.Z++) {
v3s16 blockpos = data->m_blockpos + ofs;
v3s16 blockpos_nodes = blockpos * MAP_BLOCKSIZE;
const NodeDefManager *ndef = data->m_client->ndef();
u8 result = 0x3F; // all sides solid;
u8 result = 0x3F; // all sides solid;
for (s16 i = 0; i < data->side_length && result != 0; i++)
for (s16 j = 0; j < data->side_length && result != 0; j++) {
v3s16 positions[6] = {
v3s16(0, i, j),
v3s16(data->side_length - 1, i, j),
v3s16(i, 0, j),
v3s16(i, data->side_length - 1, j),
v3s16(i, j, 0),
v3s16(i, j, data->side_length - 1)
};
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);
}
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);
}
results[blockpos] = result;
}
return results;
return result;
}

@ -247,8 +247,7 @@ private:
IShaderSource *m_shdrsrc;
f32 m_bounding_radius;
// MapblockMeshGenerator uses the same as mapblock center
v3f m_bounding_sphere_center = v3f((MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
v3f m_bounding_sphere_center;
bool m_enable_shaders;
bool m_enable_vbo;
@ -338,7 +337,7 @@ void final_color_blend(video::SColor *result,
void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
/// Return bitset of the sides of the mapblock that consist of solid nodes only
/// Return bitset of the sides of the mesh that consist of solid nodes only
/// Bits:
/// 0 0 -Z +Z -X +X -Y +Y
std::unordered_map<v3s16, u8> get_solid_sides(MeshMakeData *data);
u8 get_solid_sides(MeshMakeData *data);

@ -97,7 +97,7 @@ struct MeshUpdateResult
{
v3s16 p = v3s16(-1338, -1338, -1338);
MapBlockMesh *mesh = nullptr;
std::unordered_map<v3s16, u8> solid_sides;
u8 solid_sides;
std::vector<v3s16> ack_list;
bool urgent = false;
std::vector<MapBlock *> map_blocks;

@ -151,10 +151,22 @@ struct MeshGrid {
u32 getCellVolume() const { return cell_size * cell_size * cell_size; }
/// @brief returns coordinate of mesh cell given coordinate of a map block
s16 getCellPos(s16 p) const
{
return (p - (p < 0) * (cell_size - 1)) / cell_size;
}
/// @brief returns position of mesh cell in the grid given position of a map block
v3s16 getCellPos(v3s16 block_pos) const
{
return v3s16(getCellPos(block_pos.X), getCellPos(block_pos.Y), getCellPos(block_pos.Z));
}
/// @brief returns closest step of the grid smaller than p
s16 getMeshPos(s16 p) const
{
return ((p - (p < 0) * (cell_size - 1)) / cell_size * cell_size);
return getCellPos(p) * cell_size;
}
/// @brief Returns coordinates of the origin of the grid cell containing p