diff --git a/src/clientmap.cpp b/src/clientmap.cpp index 4d14cc1a2..64d5656d4 100644 --- a/src/clientmap.cpp +++ b/src/clientmap.cpp @@ -157,43 +157,21 @@ static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac, return false; } -void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) +void ClientMap::updateDrawList(video::IVideoDriver* driver) { + ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG); + g_profiler->add("CM::updateDrawList() count", 1); + INodeDefManager *nodemgr = m_gamedef->ndef(); - //m_dout<::Iterator + i = m_drawlist.getIterator(); + i.atEnd() == false; i++) { - m_last_drawn_sectors.clear(); + MapBlock *block = i.getNode()->getValue(); + block->refDrop(); } - - /* - Get time for measuring timeout. - - Measuring time is very useful for long delays when the - machine is swapping a lot. - */ - int time1 = time(0); - - /* - Get animation parameters - */ - float animation_time = m_client->getAnimationTime(); - int crack = m_client->getCrackLevel(); - u32 daynight_ratio = m_client->getEnv().getDayNightRatio(); + m_drawlist.clear(); m_camera_mutex.Lock(); v3f camera_position = m_camera_position; @@ -201,17 +179,15 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) f32 camera_fov = m_camera_fov; m_camera_mutex.Unlock(); - /* - Get all blocks and draw all visible ones - */ + // Use a higher fov to accomodate faster camera movements. + // Blocks are cropped better when they are drawn. + // Or maybe they aren't? Well whatever. + camera_fov *= 1.2; v3s16 cam_pos_nodes = floatToInt(camera_position, BS); - v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1); - v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d; v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d; - // Take a fair amount as we will be dropping more out later // Umm... these additions are a bit strange but they are needed. v3s16 p_blocks_min( @@ -223,13 +199,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) p_nodes_max.Y / MAP_BLOCKSIZE + 1, p_nodes_max.Z / MAP_BLOCKSIZE + 1); - u32 vertex_count = 0; - u32 meshbuffer_count = 0; - - // For limiting number of mesh animations per frame - u32 mesh_animate_count = 0; - u32 mesh_animate_count_far = 0; - // Number of blocks in rendering range u32 blocks_in_range = 0; // Number of blocks occlusion culled @@ -242,18 +211,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) // Blocks that were drawn and had a mesh u32 blocks_drawn = 0; // Blocks which had a corresponding meshbuffer for this pass - u32 blocks_had_pass_meshbuf = 0; + //u32 blocks_had_pass_meshbuf = 0; // Blocks from which stuff was actually drawn - u32 blocks_without_stuff = 0; - - /* - Collect a set of blocks for drawing - */ - - core::map drawset; - - { - ScopeProfiler sp(g_profiler, prefix+"collecting blocks for drawing", SPT_AVG); + //u32 blocks_without_stuff = 0; for(core::map::Iterator si = m_sectors.getIterator(); @@ -380,36 +340,10 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) && d > m_control.wanted_min_range * BS) continue; - // Mesh animation - { - //JMutexAutoLock lock(block->mesh_mutex); - MapBlockMesh *mapBlockMesh = block->mesh; - // Pretty random but this should work somewhat nicely - bool faraway = d >= BS*50; - //bool faraway = d >= m_control.wanted_range * BS; - if(mapBlockMesh->isAnimationForced() || - !faraway || - mesh_animate_count_far < (m_control.range_all ? 200 : 50)) - { - bool animated = mapBlockMesh->animate( - faraway, - animation_time, - crack, - daynight_ratio); - if(animated) - mesh_animate_count++; - if(animated && faraway) - mesh_animate_count_far++; - } - else - { - mapBlockMesh->decreaseAnimationForceTimer(); - } - } - // Add to set - drawset[block->getPos()] = block; - + block->refGrab(); + m_drawlist[block->getPos()] = block; + sector_blocks_drawn++; blocks_drawn++; @@ -418,8 +352,127 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) if(sector_blocks_drawn != 0) m_last_drawn_sectors[sp] = true; } - } // ScopeProfiler + g_profiler->avg("CM: blocks in range", blocks_in_range); + g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled); + if(blocks_in_range != 0) + g_profiler->avg("CM: blocks in range without mesh (frac)", + (float)blocks_in_range_without_mesh/blocks_in_range); + g_profiler->avg("CM: blocks drawn", blocks_drawn); +} + +struct MeshBufList +{ + video::SMaterial m; + core::list bufs; +}; + +struct MeshBufListList +{ + core::list lists; + + void clear() + { + lists.clear(); + } + + void add(scene::IMeshBuffer *buf) + { + for(core::list::Iterator i = lists.begin(); + i != lists.end(); i++){ + MeshBufList &l = *i; + if(l.m == buf->getMaterial()){ + l.bufs.push_back(buf); + return; + } + } + MeshBufList l; + l.m = buf->getMaterial(); + l.bufs.push_back(buf); + lists.push_back(l); + } +}; + +void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) +{ + DSTACK(__FUNCTION_NAME); + + bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT; + + std::string prefix; + if(pass == scene::ESNRP_SOLID) + prefix = "CM: solid: "; + else + prefix = "CM: transparent: "; + + /* + This is called two times per frame, reset on the non-transparent one + */ + if(pass == scene::ESNRP_SOLID) + { + m_last_drawn_sectors.clear(); + } + + /* + Get time for measuring timeout. + + Measuring time is very useful for long delays when the + machine is swapping a lot. + */ + int time1 = time(0); + + /* + Get animation parameters + */ + float animation_time = m_client->getAnimationTime(); + int crack = m_client->getCrackLevel(); + u32 daynight_ratio = m_client->getEnv().getDayNightRatio(); + + m_camera_mutex.Lock(); + v3f camera_position = m_camera_position; + v3f camera_direction = m_camera_direction; + f32 camera_fov = m_camera_fov; + m_camera_mutex.Unlock(); + + /* + Get all blocks and draw all visible ones + */ + + v3s16 cam_pos_nodes = floatToInt(camera_position, BS); + + v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1); + + v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d; + v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d; + + // Take a fair amount as we will be dropping more out later + // Umm... these additions are a bit strange but they are needed. + v3s16 p_blocks_min( + p_nodes_min.X / MAP_BLOCKSIZE - 3, + p_nodes_min.Y / MAP_BLOCKSIZE - 3, + p_nodes_min.Z / MAP_BLOCKSIZE - 3); + v3s16 p_blocks_max( + p_nodes_max.X / MAP_BLOCKSIZE + 1, + p_nodes_max.Y / MAP_BLOCKSIZE + 1, + p_nodes_max.Z / MAP_BLOCKSIZE + 1); + + u32 vertex_count = 0; + u32 meshbuffer_count = 0; + + // For limiting number of mesh animations per frame + u32 mesh_animate_count = 0; + u32 mesh_animate_count_far = 0; + + // Blocks that had mesh that would have been drawn according to + // rendering range (if max blocks limit didn't kick in) + u32 blocks_would_have_drawn = 0; + // Blocks that were drawn and had a mesh + u32 blocks_drawn = 0; + // Blocks which had a corresponding meshbuffer for this pass + u32 blocks_had_pass_meshbuf = 0; + // Blocks from which stuff was actually drawn + u32 blocks_without_stuff = 0; + /* Draw the selected MapBlocks */ @@ -427,10 +480,90 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) { ScopeProfiler sp(g_profiler, prefix+"drawing blocks", SPT_AVG); - int timecheck_counter = 0; + MeshBufListList drawbufs; + for(core::map::Iterator - i = drawset.getIterator(); + i = m_drawlist.getIterator(); i.atEnd() == false; i++) + { + MapBlock *block = i.getNode()->getValue(); + + // If the mesh of the block happened to get deleted, ignore it + if(block->mesh == NULL) + continue; + + float d = 0.0; + if(isBlockInSight(block->getPos(), camera_position, + camera_direction, camera_fov, + 100000*BS, &d) == false) + { + continue; + } + + // Mesh animation + { + //JMutexAutoLock lock(block->mesh_mutex); + MapBlockMesh *mapBlockMesh = block->mesh; + assert(mapBlockMesh); + // Pretty random but this should work somewhat nicely + bool faraway = d >= BS*50; + //bool faraway = d >= m_control.wanted_range * BS; + if(mapBlockMesh->isAnimationForced() || + !faraway || + mesh_animate_count_far < (m_control.range_all ? 200 : 50)) + { + bool animated = mapBlockMesh->animate( + faraway, + animation_time, + crack, + daynight_ratio); + if(animated) + mesh_animate_count++; + if(animated && faraway) + mesh_animate_count_far++; + } + else + { + mapBlockMesh->decreaseAnimationForceTimer(); + } + } + + /* + Get the meshbuffers of the block + */ + { + //JMutexAutoLock lock(block->mesh_mutex); + + MapBlockMesh *mapBlockMesh = block->mesh; + assert(mapBlockMesh); + + scene::SMesh *mesh = mapBlockMesh->getMesh(); + assert(mesh); + + u32 c = mesh->getMeshBufferCount(); + for(u32 i=0; igetMeshBuffer(i); + const video::SMaterial& material = buf->getMaterial(); + video::IMaterialRenderer* rnd = + driver->getMaterialRenderer(material.MaterialType); + bool transparent = (rnd && rnd->isTransparent()); + if(transparent == is_transparent_pass) + { + if(buf->getVertexCount() == 0) + errorstream<<"Block ["< &lists = drawbufs.lists; + + int timecheck_counter = 0; + for(core::list::Iterator i = lists.begin(); + i != lists.end(); i++) { { timecheck_counter++; @@ -447,9 +580,20 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) } } } - - MapBlock *block = i.getNode()->getValue(); + MeshBufList &list = *i; + + driver->setMaterial(list.m); + + for(core::list::Iterator j = list.bufs.begin(); + j != list.bufs.end(); j++) + { + scene::IMeshBuffer *buf = *j; + driver->drawMeshBuffer(buf); + vertex_count += buf->getVertexCount(); + meshbuffer_count++; + } +#if 0 /* Draw the faces of the block */ @@ -494,17 +638,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) else blocks_without_stuff++; } +#endif } } // ScopeProfiler // Log only on solid pass because values are the same if(pass == scene::ESNRP_SOLID){ - g_profiler->avg("CM: blocks in range", blocks_in_range); - g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled); - if(blocks_in_range != 0) - g_profiler->avg("CM: blocks in range without mesh (frac)", - (float)blocks_in_range_without_mesh/blocks_in_range); - g_profiler->avg("CM: blocks drawn", blocks_drawn); g_profiler->avg("CM: animated meshes", mesh_animate_count); g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far); } diff --git a/src/clientmap.h b/src/clientmap.h index 29ebed159..f8a69630e 100644 --- a/src/clientmap.h +++ b/src/clientmap.h @@ -113,7 +113,8 @@ public: { return m_box; } - + + void updateDrawList(video::IVideoDriver* driver); void renderMap(video::IVideoDriver* driver, s32 pass); int getBackgroundBrightness(float max_d, u32 daylight_factor, @@ -141,6 +142,8 @@ private: v3f m_camera_direction; f32 m_camera_fov; JMutex m_camera_mutex; + + core::map m_drawlist; core::map m_last_drawn_sectors; }; diff --git a/src/game.cpp b/src/game.cpp index 7d93e3db2..a1a197219 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1235,6 +1235,9 @@ void the_game( float object_hit_delay_timer = 0.0; float time_from_last_punch = 10; + float update_draw_list_timer = 0.0; + v3f update_draw_list_last_cam_dir; + bool invert_mouse = g_settings->getBool("invert_mouse"); bool respawn_menu_active = false; @@ -2697,7 +2700,19 @@ void the_game( item = mlist->getItem(client.getPlayerItem()); camera.wield(item); } - + + /* + Update block draw list every 200ms or when camera direction has + changed much + */ + update_draw_list_timer += dtime; + if(update_draw_list_timer >= 0.2 || + update_draw_list_last_cam_dir.getDistanceFrom(camera_direction) > 0.2){ + update_draw_list_timer = 0; + client.getEnv().getClientMap().updateDrawList(driver); + update_draw_list_last_cam_dir = camera_direction; + } + /* Drawing begins */ diff --git a/src/map.cpp b/src/map.cpp index 824553b37..39c6d292b 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1468,8 +1468,8 @@ void Map::timerUpdate(float dtime, float unload_timeout, MapBlock *block = (*i); block->incrementUsageTimer(dtime); - - if(block->getUsageTimer() > unload_timeout) + + if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout) { v3s16 p = block->getPos(); diff --git a/src/mapblock.cpp b/src/mapblock.cpp index b2da768f5..2ae6e9bd7 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -56,7 +56,8 @@ MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy): m_generated(false), m_timestamp(BLOCK_TIMESTAMP_UNDEFINED), m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED), - m_usage_timer(0) + m_usage_timer(0), + m_refcount(0) { data = NULL; if(dummy == false) diff --git a/src/mapblock.h b/src/mapblock.h index 7f901e5d3..d56d93dda 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -430,6 +430,22 @@ public: { return m_usage_timer; } + + /* + See m_refcount + */ + void refGrab() + { + m_refcount++; + } + void refDrop() + { + m_refcount--; + } + int refGet() + { + return m_refcount; + } /* Node Timers @@ -566,6 +582,12 @@ private: Map will unload the block when this reaches a timeout. */ float m_usage_timer; + + /* + Reference count; currently used for determining if this block is in + the list of blocks to be drawn. + */ + int m_refcount; }; inline bool blockpos_over_limit(v3s16 p)