diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 962261ed4..83c83c6c9 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -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] diff --git a/src/client/client.cpp b/src/client/client.cpp index 6f2647d50..28fdad72a 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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; diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index d987a2ee3..62963f7a2 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -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 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 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 §or_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 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; +} diff --git a/src/client/clientmap.h b/src/client/clientmap.h index 37931d4e7..b9f7b4dfa 100644 --- a/src/client/clientmap.h +++ b/src/client/clientmap.h @@ -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(); diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index f5abd2777..53d4d0c46 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -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 get_solid_sides(MeshMakeData *data) +u8 get_solid_sides(MeshMakeData *data) { std::unordered_map 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; } diff --git a/src/client/mapblock_mesh.h b/src/client/mapblock_mesh.h index 288f5cae7..64433673b 100644 --- a/src/client/mapblock_mesh.h +++ b/src/client/mapblock_mesh.h @@ -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 get_solid_sides(MeshMakeData *data); +u8 get_solid_sides(MeshMakeData *data); diff --git a/src/client/mesh_generator_thread.h b/src/client/mesh_generator_thread.h index 3097f3704..c4f87cee2 100644 --- a/src/client/mesh_generator_thread.h +++ b/src/client/mesh_generator_thread.h @@ -97,7 +97,7 @@ struct MeshUpdateResult { v3s16 p = v3s16(-1338, -1338, -1338); MapBlockMesh *mesh = nullptr; - std::unordered_map solid_sides; + u8 solid_sides; std::vector ack_list; bool urgent = false; std::vector map_blocks; diff --git a/src/util/numeric.h b/src/util/numeric.h index f0d4fe482..1aeed9ce2 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -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