mesh_generator_thread.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Minetest
  3. Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "mesh_generator_thread.h"
  17. #include "settings.h"
  18. #include "profiler.h"
  19. #include "client.h"
  20. #include "mapblock.h"
  21. #include "map.h"
  22. #include "util/directiontables.h"
  23. /*
  24. CachedMapBlockData
  25. */
  26. CachedMapBlockData::~CachedMapBlockData()
  27. {
  28. assert(refcount_from_queue == 0);
  29. delete[] data;
  30. }
  31. /*
  32. QueuedMeshUpdate
  33. */
  34. QueuedMeshUpdate::~QueuedMeshUpdate()
  35. {
  36. delete data;
  37. }
  38. /*
  39. MeshUpdateQueue
  40. */
  41. MeshUpdateQueue::MeshUpdateQueue(Client *client):
  42. m_client(client)
  43. {
  44. m_cache_enable_shaders = g_settings->getBool("enable_shaders");
  45. m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
  46. m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
  47. }
  48. MeshUpdateQueue::~MeshUpdateQueue()
  49. {
  50. MutexAutoLock lock(m_mutex);
  51. for (auto &i : m_cache) {
  52. delete i.second;
  53. }
  54. for (QueuedMeshUpdate *q : m_queue) {
  55. delete q;
  56. }
  57. }
  58. bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
  59. {
  60. MutexAutoLock lock(m_mutex);
  61. cleanupCache();
  62. /*
  63. Cache the block data (force-update the center block, don't update the
  64. neighbors but get them if they aren't already cached)
  65. */
  66. std::vector<CachedMapBlockData*> cached_blocks;
  67. size_t cache_hit_counter = 0;
  68. CachedMapBlockData *cached_block = cacheBlock(map, p, FORCE_UPDATE);
  69. if (!cached_block->data)
  70. return false; // nothing to update
  71. cached_blocks.reserve(3*3*3);
  72. cached_blocks.push_back(cached_block);
  73. for (v3s16 dp : g_26dirs)
  74. cached_blocks.push_back(cacheBlock(map, p + dp,
  75. SKIP_UPDATE_IF_ALREADY_CACHED,
  76. &cache_hit_counter));
  77. g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
  78. 100.0f * cache_hit_counter / cached_blocks.size());
  79. /*
  80. Mark the block as urgent if requested
  81. */
  82. if (urgent)
  83. m_urgents.insert(p);
  84. /*
  85. Find if block is already in queue.
  86. If it is, update the data and quit.
  87. */
  88. for (QueuedMeshUpdate *q : m_queue) {
  89. if (q->p == p) {
  90. // NOTE: We are not adding a new position to the queue, thus
  91. // refcount_from_queue stays the same.
  92. if(ack_block_to_server)
  93. q->ack_block_to_server = true;
  94. q->crack_level = m_client->getCrackLevel();
  95. q->crack_pos = m_client->getCrackPos();
  96. return true;
  97. }
  98. }
  99. /*
  100. Add the block
  101. */
  102. QueuedMeshUpdate *q = new QueuedMeshUpdate;
  103. q->p = p;
  104. q->ack_block_to_server = ack_block_to_server;
  105. q->crack_level = m_client->getCrackLevel();
  106. q->crack_pos = m_client->getCrackPos();
  107. m_queue.push_back(q);
  108. // This queue entry is a new reference to the cached blocks
  109. for (CachedMapBlockData *cached_block : cached_blocks) {
  110. cached_block->refcount_from_queue++;
  111. }
  112. return true;
  113. }
  114. // Returned pointer must be deleted
  115. // Returns NULL if queue is empty
  116. QueuedMeshUpdate *MeshUpdateQueue::pop()
  117. {
  118. MutexAutoLock lock(m_mutex);
  119. bool must_be_urgent = !m_urgents.empty();
  120. for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
  121. i != m_queue.end(); ++i) {
  122. QueuedMeshUpdate *q = *i;
  123. if(must_be_urgent && m_urgents.count(q->p) == 0)
  124. continue;
  125. m_queue.erase(i);
  126. m_urgents.erase(q->p);
  127. fillDataFromMapBlockCache(q);
  128. return q;
  129. }
  130. return NULL;
  131. }
  132. CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
  133. size_t *cache_hit_counter)
  134. {
  135. CachedMapBlockData *cached_block = nullptr;
  136. std::map<v3s16, CachedMapBlockData*>::iterator it =
  137. m_cache.find(p);
  138. if (it != m_cache.end()) {
  139. cached_block = it->second;
  140. if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
  141. if (cache_hit_counter)
  142. (*cache_hit_counter)++;
  143. return cached_block;
  144. }
  145. }
  146. if (!cached_block) {
  147. // Not yet in cache
  148. cached_block = new CachedMapBlockData();
  149. m_cache[p] = cached_block;
  150. }
  151. MapBlock *b = map->getBlockNoCreateNoEx(p);
  152. if (b) {
  153. if (!cached_block->data)
  154. cached_block->data =
  155. new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
  156. memcpy(cached_block->data, b->getData(),
  157. MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
  158. } else {
  159. delete[] cached_block->data;
  160. cached_block->data = nullptr;
  161. }
  162. return cached_block;
  163. }
  164. CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
  165. {
  166. std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
  167. if (it != m_cache.end()) {
  168. return it->second;
  169. }
  170. return NULL;
  171. }
  172. void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
  173. {
  174. MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders);
  175. q->data = data;
  176. data->fillBlockDataBegin(q->p);
  177. std::time_t t_now = std::time(0);
  178. // Collect data for 3*3*3 blocks from cache
  179. for (v3s16 dp : g_27dirs) {
  180. v3s16 p = q->p + dp;
  181. CachedMapBlockData *cached_block = getCachedBlock(p);
  182. if (cached_block) {
  183. cached_block->refcount_from_queue--;
  184. cached_block->last_used_timestamp = t_now;
  185. if (cached_block->data)
  186. data->fillBlockData(dp, cached_block->data);
  187. }
  188. }
  189. data->setCrack(q->crack_level, q->crack_pos);
  190. data->setSmoothLighting(m_cache_smooth_lighting);
  191. }
  192. void MeshUpdateQueue::cleanupCache()
  193. {
  194. const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
  195. sizeof(MapNode) / 1000;
  196. g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
  197. mapblock_kB * m_cache.size());
  198. // The cache size is kept roughly below cache_soft_max_size, not letting
  199. // anything get older than cache_seconds_max or deleted before 2 seconds.
  200. const int cache_seconds_max = 10;
  201. const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
  202. int cache_seconds = MYMAX(2, cache_seconds_max -
  203. m_cache.size() / (cache_soft_max_size / cache_seconds_max));
  204. int t_now = time(0);
  205. for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
  206. it != m_cache.end(); ) {
  207. CachedMapBlockData *cached_block = it->second;
  208. if (cached_block->refcount_from_queue == 0 &&
  209. cached_block->last_used_timestamp < t_now - cache_seconds) {
  210. m_cache.erase(it++);
  211. delete cached_block;
  212. } else {
  213. ++it;
  214. }
  215. }
  216. }
  217. /*
  218. MeshUpdateThread
  219. */
  220. MeshUpdateThread::MeshUpdateThread(Client *client):
  221. UpdateThread("Mesh"),
  222. m_queue_in(client)
  223. {
  224. m_generation_interval = g_settings->getU16("mesh_generation_interval");
  225. m_generation_interval = rangelim(m_generation_interval, 0, 50);
  226. }
  227. void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
  228. bool urgent, bool update_neighbors)
  229. {
  230. static thread_local const bool many_neighbors =
  231. g_settings->getBool("smooth_lighting")
  232. && !g_settings->getFlag("performance_tradeoffs");
  233. if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) {
  234. warningstream << "Update requested for non-existent block at ("
  235. << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl;
  236. return;
  237. }
  238. if (update_neighbors) {
  239. if (many_neighbors) {
  240. for (v3s16 dp : g_26dirs)
  241. m_queue_in.addBlock(map, p + dp, false, urgent);
  242. } else {
  243. for (v3s16 dp : g_6dirs)
  244. m_queue_in.addBlock(map, p + dp, false, urgent);
  245. }
  246. }
  247. deferUpdate();
  248. }
  249. void MeshUpdateThread::doUpdate()
  250. {
  251. QueuedMeshUpdate *q;
  252. while ((q = m_queue_in.pop())) {
  253. if (m_generation_interval)
  254. sleep_ms(m_generation_interval);
  255. ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
  256. MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
  257. MeshUpdateResult r;
  258. r.p = q->p;
  259. r.mesh = mesh_new;
  260. r.ack_block_to_server = q->ack_block_to_server;
  261. m_queue_out.push_back(r);
  262. delete q;
  263. }
  264. }