]> git.lizzy.rs Git - minetest.git/blobdiff - src/client/clientmap.cpp
8x block meshes (#13133)
[minetest.git] / src / client / clientmap.cpp
index 23234c3651b05487dea6e7a5487fa9ae701ab1c4..146531d8b3ca740c124d5bc8dcb8257d0f808a0a 100644 (file)
@@ -28,9 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include "camera.h"               // CameraModes
 #include "util/basic_macros.h"
-#include <algorithm>
 #include "client/renderingengine.h"
 
+#include <queue>
+
 // struct MeshBufListList
 void MeshBufListList::clear()
 {
@@ -60,6 +61,10 @@ void MeshBufListList::add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
        list.emplace_back(l);
 }
 
+static void on_settings_changed(const std::string &name, void *data)
+{
+       static_cast<ClientMap*>(data)->onSettingChanged(name);
+}
 // ClientMap
 
 ClientMap::ClientMap(
@@ -98,7 +103,19 @@ ClientMap::ClientMap(
        m_cache_bilinear_filter   = g_settings->getBool("bilinear_filter");
        m_cache_anistropic_filter = g_settings->getBool("anisotropic_filter");
        m_cache_transparency_sorting_distance = g_settings->getU16("transparency_sorting_distance");
+       m_enable_raytraced_culling = g_settings->getBool("enable_raytraced_culling");
+       g_settings->registerChangedCallback("enable_raytraced_culling", on_settings_changed, this);
+}
 
+void ClientMap::onSettingChanged(const std::string &name)
+{
+       if (name == "enable_raytraced_culling")
+               m_enable_raytraced_culling = g_settings->getBool("enable_raytraced_culling");
+}
+
+ClientMap::~ClientMap()
+{
+       g_settings->deregisterChangedCallback("enable_raytraced_culling", on_settings_changed, this);
 }
 
 void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset)
@@ -146,12 +163,8 @@ void ClientMap::OnRegisterSceneNode()
        }
 
        ISceneNode::OnRegisterSceneNode();
-
-       if (!m_added_to_shadow_renderer) {
-               m_added_to_shadow_renderer = true;
-               if (auto shadows = m_rendering_engine->get_shadow_renderer())
-                       shadows->addNodeToShadowList(this);
-       }
+       // It's not needed to register this node to the shadow renderer
+       // we have other way to find it
 }
 
 void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
@@ -184,6 +197,54 @@ void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
                        p_nodes_max.Z / MAP_BLOCKSIZE + 1);
 }
 
+class MapBlockFlags
+{
+public:
+       static constexpr u16 CHUNK_EDGE = 8;
+       static constexpr u16 CHUNK_MASK = CHUNK_EDGE - 1;
+       static constexpr std::size_t CHUNK_VOLUME = CHUNK_EDGE * CHUNK_EDGE * CHUNK_EDGE; // volume of a chunk
+
+       MapBlockFlags(v3s16 min_pos, v3s16 max_pos)
+                       : min_pos(min_pos), volume((max_pos - min_pos) / CHUNK_EDGE + 1)
+       {
+               chunks.resize(volume.X * volume.Y * volume.Z);
+       }
+
+       class Chunk
+       {
+       public:
+               inline u8 &getBits(v3s16 pos)
+               {
+                       std::size_t address = getAddress(pos);
+                       return bits[address];
+               }
+
+       private:
+               inline std::size_t getAddress(v3s16 pos) {
+                       std::size_t address = (pos.X & CHUNK_MASK) + (pos.Y & CHUNK_MASK) * CHUNK_EDGE + (pos.Z & CHUNK_MASK) * (CHUNK_EDGE * CHUNK_EDGE);
+                       return address;
+               }
+
+               std::array<u8, CHUNK_VOLUME> bits;
+       };
+
+       Chunk &getChunk(v3s16 pos)
+       {
+               v3s16 delta = (pos - min_pos) / CHUNK_EDGE;
+               std::size_t address = delta.X + delta.Y * volume.X + delta.Z * volume.X * volume.Y;
+               Chunk *chunk = chunks[address].get();
+               if (!chunk) {
+                       chunk = new Chunk();
+                       chunks[address].reset(chunk);
+               }
+               return *chunk;
+       }
+private:
+       std::vector<std::unique_ptr<Chunk>> chunks;
+       v3s16 min_pos;
+       v3s16 volume;
+};
+
 void ClientMap::updateDrawList()
 {
        ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
@@ -202,12 +263,12 @@ void ClientMap::updateDrawList()
        v3s16 p_blocks_max;
        getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
 
-       // Number of blocks currently loaded by the client
-       u32 blocks_loaded = 0;
-       // Number of blocks with mesh in rendering range
-       u32 blocks_in_range_with_mesh = 0;
        // Number of blocks occlusion culled
        u32 blocks_occlusion_culled = 0;
+       // Blocks visited by the algorithm
+       u32 blocks_visited = 0;
+       // Block sides that were not traversed
+       u32 sides_skipped = 0;
 
        // No occlusion culling when free_move is on and camera is inside ground
        bool occlusion_culling_enabled = true;
@@ -224,8 +285,227 @@ void ClientMap::updateDrawList()
 
        // Uncomment to debug occluded blocks in the wireframe mode
        // TODO: Include this as a flag for an extended debugging setting
-       //if (occlusion_culling_enabled && m_control.show_wireframe)
-       //    occlusion_culling_enabled = porting::getTimeS() & 1;
+       // if (occlusion_culling_enabled && m_control.show_wireframe)
+       //      occlusion_culling_enabled = porting::getTimeS() & 1;
+
+       std::queue<v3s16> blocks_to_consider;
+
+       // Bits per block:
+       // [ visited | 0 | 0 | 0 | 0 | Z visible | Y visible | X visible ]
+       MapBlockFlags blocks_seen(p_blocks_min, p_blocks_max);
+
+       // 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
+
+       std::set<v3s16> shortlist;
+
+       // Recursively walk the space and pick mapblocks for drawing
+       while (blocks_to_consider.size() > 0) {
+
+               v3s16 block_coord = blocks_to_consider.front();
+               blocks_to_consider.pop();
+
+               auto &flags = blocks_seen.getChunk(block_coord).getBits(block_coord);
+
+               // Only visit each block once (it may have been queued up to three times)
+               if ((flags & 0x80) == 0x80)
+                       continue;
+               flags |= 0x80;
+
+               blocks_visited++;
+
+               // 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);
+
+               MapBlockMesh *mesh = block ? block->mesh : nullptr;
+
+
+               // Calculate the coordinates for range and frutum culling
+               v3f mesh_sphere_center;
+               f32 mesh_sphere_radius;
+
+               v3s16 block_pos_nodes = block_coord * MAP_BLOCKSIZE;
+
+               if (mesh) {
+                       mesh_sphere_center = intToFloat(block_pos_nodes, BS)
+                                       + mesh->getBoundingSphereCenter();
+                       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;
+               }
+
+               // First, perform a simple distance check.
+               if (!m_control.range_all &&
+                       mesh_sphere_center.getDistanceFrom(intToFloat(cam_pos_nodes, BS)) >
+                               m_control.wanted_range * BS + mesh_sphere_radius)
+                       continue; // Out of range, skip.
+
+               // Frustum culling
+               // Only do coarse culling here, to account for fast camera movement.
+               // 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))
+                       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;
+
+               // Occluded near sides will further occlude the far sides
+               u8 visible_outer_sides = flags & 0x07;
+
+               // 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)) {
+                       blocks_occlusion_culled++;
+                       continue;
+               }
+
+               // 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();
+               }
+
+               // Decide which sides to traverse next or to block away
+
+               // First, find the near sides that would occlude the far sides
+               // * A near side can itself be occluded by a nearby block (the test above ^^)
+               // * A near side can be visible but fully opaque by itself (e.g. ground at the 0 level)
+
+               // mesh solid sides are +Z-Z+Y-Y+X-X
+               // if we are inside the block's coordinates on an axis, 
+               // treat these sides as opaque, as they should not allow to reach the far sides
+               u8 block_inner_sides = (look.X == 0 ? 3 : 0) |
+                       (look.Y == 0 ? 12 : 0) |
+                       (look.Z == 0 ? 48 : 0);
+
+               // get the mask for the sides that are relevant based on the direction
+               u8 near_inner_sides = (look.X > 0 ? 1 : 2) |
+                               (look.Y > 0 ? 4 : 8) |
+                               (look.Z > 0 ? 16 : 32);
+               
+               // This bitset is +Z-Z+Y-Y+X-X (See MapBlockMesh), and axis is XYZ.
+               // Get he block's transparent sides
+               u8 transparent_sides = (occlusion_culling_enabled && block) ? ~block->solid_sides : 0x3F;
+
+               // compress block transparent sides to ZYX mask of see-through axes
+               u8 near_transparency =  (block_inner_sides == 0x3F) ? near_inner_sides : (transparent_sides & near_inner_sides);
+
+               // when we are inside the camera block, do not block any sides
+               if (block_inner_sides == 0x3F)
+                       block_inner_sides = 0;
+
+               near_transparency &= ~block_inner_sides & 0x3F;
+
+               near_transparency |= (near_transparency >> 1);
+               near_transparency = (near_transparency & 1) |
+                               ((near_transparency >> 1) & 2) |
+                               ((near_transparency >> 2) & 4);
+
+               // combine with known visible sides that matter
+               near_transparency &= visible_outer_sides;
+
+               // The rule for any far side to be visible:
+               // * Any of the adjacent near sides is transparent (different axes)
+               // * The opposite near side (same axis) is transparent, if it is the dominant axis of the look vector
+
+               // 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;
+
+               // dominant axis flag
+               u8 dominant_axis = (abs(precise_look.X) > abs(precise_look.Y) && abs(precise_look.X) > abs(precise_look.Z)) |
+                                       ((abs(precise_look.Y) > abs(precise_look.Z) && abs(precise_look.Y) > abs(precise_look.X)) << 1) |
+                                       ((abs(precise_look.Z) > abs(precise_look.X) && abs(precise_look.Z) > abs(precise_look.Y)) << 2);
+
+               // Queue next blocks for processing:
+               // - Examine "far" sides of the current blocks, i.e. never move towards the camera
+               // - Only traverse the sides that are not occluded
+               // - Only traverse the sides that are not opaque
+               // When queueing, mark the relevant side on the next block as 'visible'
+               for (s16 axis = 0; axis < 3; axis++) {
+
+                       // Select a bit from transparent_sides for the side
+                       u8 far_side_mask = 1 << (2 * axis);
+
+                       // axis flag
+                       u8 my_side = 1 << axis;
+                       u8 adjacent_sides = my_side ^ 0x07;
+
+                       auto traverse_far_side = [&](s8 next_pos_offset) {
+                               // far side is visible if adjacent near sides are transparent, or if opposite side on dominant axis is transparent
+                               bool side_visible = ((near_transparency & adjacent_sides) | (near_transparency & my_side & dominant_axis)) != 0;
+                               side_visible = side_visible && ((far_side_mask & transparent_sides) != 0);
+
+                               v3s16 next_pos = block_coord;
+                               next_pos[axis] += next_pos_offset;
+
+                               // 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);
+                                       next_flags |= my_side;
+                                       blocks_to_consider.push(next_pos);
+                               }
+                               else {
+                                       sides_skipped++;
+                               }
+                       };
+
+
+                       // Test the '-' direction of the axis
+                       if (look[axis] <= 0 && block_coord[axis] > p_blocks_min[axis])
+                               traverse_far_side(-1);
+
+                       // 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);
+               }
+       }
+
+       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);
+       g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
+}
+
+void ClientMap::touchMapBlocks()
+{
+       v3s16 cam_pos_nodes = floatToInt(m_camera_position, BS);
+
+       v3s16 p_blocks_min;
+       v3s16 p_blocks_max;
+       getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
+
+       // Number of blocks currently loaded by the client
+       u32 blocks_loaded = 0;
+       // Number of blocks with mesh in rendering range
+       u32 blocks_in_range_with_mesh = 0;
 
        for (const auto &sector_it : m_sectors) {
                MapSector *sector = sector_it.second;
@@ -245,8 +525,6 @@ void ClientMap::updateDrawList()
                        Loop through blocks in sector
                */
 
-               u32 sector_blocks_drawn = 0;
-
                for (MapBlock *block : sectorblocks) {
                        /*
                                Compare block position to camera position, skip
@@ -258,49 +536,21 @@ void ClientMap::updateDrawList()
                                continue;
                        }
 
-                       v3s16 block_coord = block->getPos();
-                       v3s16 block_position = block->getPosRelative() + MAP_BLOCKSIZE / 2;
-
-                       // First, perform a simple distance check, with a padding of one extra block.
+                       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 &&
-                                       block_position.getDistanceFrom(cam_pos_nodes) > m_control.wanted_range)
+                               mesh_sphere_center.getDistanceFrom(intToFloat(cam_pos_nodes, BS)) >
+                                       m_control.wanted_range * BS + mesh_sphere_radius)
                                continue; // Out of range, skip.
 
                        // Keep the block alive as long as it is in range.
                        block->resetUsageTimer();
                        blocks_in_range_with_mesh++;
-
-                       // Frustum culling
-                       // Only do coarse culling here, to account for fast camera movement.
-                       // This is needed because this function is not called every frame.
-                       constexpr float frustum_cull_extra_radius = 300.0f;
-                       v3f mesh_sphere_center = intToFloat(block->getPosRelative(), BS)
-                                       + block->mesh->getBoundingSphereCenter();
-                       f32 mesh_sphere_radius = block->mesh->getBoundingRadius();
-                       if (is_frustum_culled(mesh_sphere_center,
-                                       mesh_sphere_radius + frustum_cull_extra_radius))
-                               continue;
-
-                       // Occlusion culling
-                       if (occlusion_culling_enabled && isBlockOccluded(block, cam_pos_nodes)) {
-                               blocks_occlusion_culled++;
-                               continue;
-                       }
-
-                       // Add to set
-                       block->refGrab();
-                       m_drawlist[block_coord] = block;
-
-                       sector_blocks_drawn++;
-               } // foreach sectorblocks
-
-               if (sector_blocks_drawn != 0)
-                       m_last_drawn_sectors.insert(sp);
+               }
        }
-
        g_profiler->avg("MapBlock meshes in range [#]", blocks_in_range_with_mesh);
-       g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled);
-       g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
        g_profiler->avg("MapBlocks loaded [#]", blocks_loaded);
 }
 
@@ -361,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;
 
@@ -484,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);
@@ -527,8 +781,8 @@ static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
        {
                v3s16 p = floatToInt(p0 /*+ dir * 3*BS*/, BS);
                MapNode n = map->getNode(p);
-               if(ndef->get(n).param_type == CPT_LIGHT &&
-                               !ndef->get(n).sunlight_propagates)
+               if(ndef->getLightingFlags(n).has_light &&
+                               !ndef->getLightingFlags(n).sunlight_propagates)
                        allow_allowing_non_sunlight_propagates = true;
        }
        // If would start at CONTENT_IGNORE, start closer
@@ -549,15 +803,13 @@ static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
 
                v3s16 p = floatToInt(pf, BS);
                MapNode n = map->getNode(p);
+               ContentLightingFlags f = ndef->getLightingFlags(n);
                if (allow_allowing_non_sunlight_propagates && i == 0 &&
-                               ndef->get(n).param_type == CPT_LIGHT &&
-                               !ndef->get(n).sunlight_propagates) {
+                               f.has_light && !f.sunlight_propagates) {
                        allow_non_sunlight_propagates = true;
                }
 
-               if (ndef->get(n).param_type != CPT_LIGHT ||
-                               (!ndef->get(n).sunlight_propagates &&
-                                       !allow_non_sunlight_propagates)){
+               if (!f.has_light || (!f.sunlight_propagates && !allow_non_sunlight_propagates)){
                        nonlight_seen = true;
                        noncount++;
                        if(noncount >= 4)
@@ -566,10 +818,10 @@ static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
                }
 
                if (distance >= sunlight_min_d && !*sunlight_seen && !nonlight_seen)
-                       if (n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
+                       if (n.getLight(LIGHTBANK_DAY, f) == LIGHT_SUN)
                                *sunlight_seen = true;
                noncount = 0;
-               brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef));
+               brightness_sum += decode_light(n.getLightBlend(daylight_factor, f));
                brightness_count++;
        }
        *result = 0;
@@ -653,8 +905,9 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
        int ret = 0;
        if(brightness_count == 0){
                MapNode n = getNode(floatToInt(m_camera_position, BS));
-               if(m_nodedef->get(n).param_type == CPT_LIGHT){
-                       ret = decode_light(n.getLightBlend(daylight_factor, m_nodedef));
+               ContentLightingFlags f = m_nodedef->getLightingFlags(n);
+               if(f.has_light){
+                       ret = decode_light(n.getLightBlend(daylight_factor, f));
                } else {
                        ret = oldvalue;
                }
@@ -723,7 +976,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
                return;
        }
 
-       for (auto &i : m_drawlist_shadow) {
+       for (const auto &i : m_drawlist_shadow) {
                // only process specific part of the list & break early
                ++count;
                if (count <= low_bound)
@@ -811,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);
@@ -898,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);