mesh_generator_thread.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /*
  23. CachedMapBlockData
  24. */
  25. CachedMapBlockData::~CachedMapBlockData()
  26. {
  27. assert(refcount_from_queue == 0);
  28. delete[] data;
  29. }
  30. /*
  31. QueuedMeshUpdate
  32. */
  33. QueuedMeshUpdate::~QueuedMeshUpdate()
  34. {
  35. delete data;
  36. }
  37. /*
  38. MeshUpdateQueue
  39. */
  40. MeshUpdateQueue::MeshUpdateQueue(Client *client):
  41. m_client(client)
  42. {
  43. m_cache_enable_shaders = g_settings->getBool("enable_shaders");
  44. m_cache_use_tangent_vertices = m_cache_enable_shaders && (
  45. g_settings->getBool("enable_bumpmapping") ||
  46. g_settings->getBool("enable_parallax_occlusion"));
  47. m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
  48. m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
  49. }
  50. MeshUpdateQueue::~MeshUpdateQueue()
  51. {
  52. MutexAutoLock lock(m_mutex);
  53. for (auto &i : m_cache) {
  54. delete i.second;
  55. }
  56. for (QueuedMeshUpdate *q : m_queue) {
  57. delete q;
  58. }
  59. }
  60. void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
  61. {
  62. MutexAutoLock lock(m_mutex);
  63. cleanupCache();
  64. /*
  65. Cache the block data (force-update the center block, don't update the
  66. neighbors but get them if they aren't already cached)
  67. */
  68. std::vector<CachedMapBlockData*> cached_blocks;
  69. size_t cache_hit_counter = 0;
  70. cached_blocks.reserve(3*3*3);
  71. v3s16 dp;
  72. for (dp.X = -1; dp.X <= 1; dp.X++)
  73. for (dp.Y = -1; dp.Y <= 1; dp.Y++)
  74. for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
  75. v3s16 p1 = p + dp;
  76. CachedMapBlockData *cached_block;
  77. if (dp == v3s16(0, 0, 0))
  78. cached_block = cacheBlock(map, p1, FORCE_UPDATE);
  79. else
  80. cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
  81. &cache_hit_counter);
  82. cached_blocks.push_back(cached_block);
  83. }
  84. g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
  85. 100.0f * cache_hit_counter / cached_blocks.size());
  86. /*
  87. Mark the block as urgent if requested
  88. */
  89. if (urgent)
  90. m_urgents.insert(p);
  91. /*
  92. Find if block is already in queue.
  93. If it is, update the data and quit.
  94. */
  95. for (QueuedMeshUpdate *q : m_queue) {
  96. if (q->p == p) {
  97. // NOTE: We are not adding a new position to the queue, thus
  98. // refcount_from_queue stays the same.
  99. if(ack_block_to_server)
  100. q->ack_block_to_server = true;
  101. q->crack_level = m_client->getCrackLevel();
  102. q->crack_pos = m_client->getCrackPos();
  103. return;
  104. }
  105. }
  106. /*
  107. Add the block
  108. */
  109. QueuedMeshUpdate *q = new QueuedMeshUpdate;
  110. q->p = p;
  111. q->ack_block_to_server = ack_block_to_server;
  112. q->crack_level = m_client->getCrackLevel();
  113. q->crack_pos = m_client->getCrackPos();
  114. m_queue.push_back(q);
  115. // This queue entry is a new reference to the cached blocks
  116. for (CachedMapBlockData *cached_block : cached_blocks) {
  117. cached_block->refcount_from_queue++;
  118. }
  119. }
  120. // Returned pointer must be deleted
  121. // Returns NULL if queue is empty
  122. QueuedMeshUpdate *MeshUpdateQueue::pop()
  123. {
  124. MutexAutoLock lock(m_mutex);
  125. bool must_be_urgent = !m_urgents.empty();
  126. for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
  127. i != m_queue.end(); ++i) {
  128. QueuedMeshUpdate *q = *i;
  129. if(must_be_urgent && m_urgents.count(q->p) == 0)
  130. continue;
  131. m_queue.erase(i);
  132. m_urgents.erase(q->p);
  133. fillDataFromMapBlockCache(q);
  134. return q;
  135. }
  136. return NULL;
  137. }
  138. CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
  139. size_t *cache_hit_counter)
  140. {
  141. std::map<v3s16, CachedMapBlockData*>::iterator it =
  142. m_cache.find(p);
  143. if (it != m_cache.end()) {
  144. // Already in cache
  145. CachedMapBlockData *cached_block = it->second;
  146. if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
  147. if (cache_hit_counter)
  148. (*cache_hit_counter)++;
  149. return cached_block;
  150. }
  151. MapBlock *b = map->getBlockNoCreateNoEx(p);
  152. if (b) {
  153. if (cached_block->data == NULL)
  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 = NULL;
  161. }
  162. return cached_block;
  163. }
  164. // Not yet in cache
  165. CachedMapBlockData *cached_block = new CachedMapBlockData();
  166. m_cache[p] = cached_block;
  167. MapBlock *b = map->getBlockNoCreateNoEx(p);
  168. if (b) {
  169. cached_block->data =
  170. new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
  171. memcpy(cached_block->data, b->getData(),
  172. MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
  173. }
  174. return cached_block;
  175. }
  176. CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
  177. {
  178. std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
  179. if (it != m_cache.end()) {
  180. return it->second;
  181. }
  182. return NULL;
  183. }
  184. void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
  185. {
  186. MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
  187. m_cache_use_tangent_vertices);
  188. q->data = data;
  189. data->fillBlockDataBegin(q->p);
  190. std::time_t t_now = std::time(0);
  191. // Collect data for 3*3*3 blocks from cache
  192. v3s16 dp;
  193. for (dp.X = -1; dp.X <= 1; dp.X++)
  194. for (dp.Y = -1; dp.Y <= 1; dp.Y++)
  195. for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
  196. v3s16 p = q->p + dp;
  197. CachedMapBlockData *cached_block = getCachedBlock(p);
  198. if (cached_block) {
  199. cached_block->refcount_from_queue--;
  200. cached_block->last_used_timestamp = t_now;
  201. if (cached_block->data)
  202. data->fillBlockData(dp, cached_block->data);
  203. }
  204. }
  205. data->setCrack(q->crack_level, q->crack_pos);
  206. data->setSmoothLighting(m_cache_smooth_lighting);
  207. }
  208. void MeshUpdateQueue::cleanupCache()
  209. {
  210. const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
  211. sizeof(MapNode) / 1000;
  212. g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
  213. mapblock_kB * m_cache.size());
  214. // The cache size is kept roughly below cache_soft_max_size, not letting
  215. // anything get older than cache_seconds_max or deleted before 2 seconds.
  216. const int cache_seconds_max = 10;
  217. const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
  218. int cache_seconds = MYMAX(2, cache_seconds_max -
  219. m_cache.size() / (cache_soft_max_size / cache_seconds_max));
  220. int t_now = time(0);
  221. for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
  222. it != m_cache.end(); ) {
  223. CachedMapBlockData *cached_block = it->second;
  224. if (cached_block->refcount_from_queue == 0 &&
  225. cached_block->last_used_timestamp < t_now - cache_seconds) {
  226. m_cache.erase(it++);
  227. delete cached_block;
  228. } else {
  229. ++it;
  230. }
  231. }
  232. }
  233. /*
  234. MeshUpdateThread
  235. */
  236. MeshUpdateThread::MeshUpdateThread(Client *client):
  237. UpdateThread("Mesh"),
  238. m_queue_in(client)
  239. {
  240. m_generation_interval = g_settings->getU16("mesh_generation_interval");
  241. m_generation_interval = rangelim(m_generation_interval, 0, 50);
  242. }
  243. void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
  244. bool urgent)
  245. {
  246. // Allow the MeshUpdateQueue to do whatever it wants
  247. m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
  248. deferUpdate();
  249. }
  250. void MeshUpdateThread::doUpdate()
  251. {
  252. QueuedMeshUpdate *q;
  253. while ((q = m_queue_in.pop())) {
  254. if (m_generation_interval)
  255. sleep_ms(m_generation_interval);
  256. ScopeProfiler sp(g_profiler, "Client: Mesh making");
  257. MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
  258. MeshUpdateResult r;
  259. r.p = q->p;
  260. r.mesh = mesh_new;
  261. r.ack_block_to_server = q->ack_block_to_server;
  262. m_queue_out.push_back(r);
  263. delete q;
  264. }
  265. }