mesh_generator_thread.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #pragma once
  17. #include <ctime>
  18. #include <mutex>
  19. #include "mapblock_mesh.h"
  20. #include "threading/mutex_auto_lock.h"
  21. #include "util/thread.h"
  22. struct CachedMapBlockData
  23. {
  24. v3s16 p = v3s16(-1337, -1337, -1337);
  25. MapNode *data = nullptr; // A copy of the MapBlock's data member
  26. int refcount_from_queue = 0;
  27. std::time_t last_used_timestamp = std::time(0);
  28. CachedMapBlockData() = default;
  29. ~CachedMapBlockData();
  30. };
  31. struct QueuedMeshUpdate
  32. {
  33. v3s16 p = v3s16(-1337, -1337, -1337);
  34. bool ack_block_to_server = false;
  35. int crack_level = -1;
  36. v3s16 crack_pos;
  37. MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
  38. QueuedMeshUpdate() = default;
  39. ~QueuedMeshUpdate();
  40. };
  41. /*
  42. A thread-safe queue of mesh update tasks and a cache of MapBlock data
  43. */
  44. class MeshUpdateQueue
  45. {
  46. enum UpdateMode
  47. {
  48. FORCE_UPDATE,
  49. SKIP_UPDATE_IF_ALREADY_CACHED,
  50. };
  51. public:
  52. MeshUpdateQueue(Client *client);
  53. ~MeshUpdateQueue();
  54. // Caches the block at p and its neighbors (if needed) and queues a mesh
  55. // update for the block at p
  56. bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
  57. // Returned pointer must be deleted
  58. // Returns NULL if queue is empty
  59. QueuedMeshUpdate *pop();
  60. u32 size()
  61. {
  62. MutexAutoLock lock(m_mutex);
  63. return m_queue.size();
  64. }
  65. private:
  66. Client *m_client;
  67. std::vector<QueuedMeshUpdate *> m_queue;
  68. std::set<v3s16> m_urgents;
  69. std::map<v3s16, CachedMapBlockData *> m_cache;
  70. std::mutex m_mutex;
  71. // TODO: Add callback to update these when g_settings changes
  72. bool m_cache_enable_shaders;
  73. bool m_cache_smooth_lighting;
  74. int m_meshgen_block_cache_size;
  75. CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
  76. size_t *cache_hit_counter = NULL);
  77. CachedMapBlockData *getCachedBlock(const v3s16 &p);
  78. void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
  79. void cleanupCache();
  80. };
  81. struct MeshUpdateResult
  82. {
  83. v3s16 p = v3s16(-1338, -1338, -1338);
  84. MapBlockMesh *mesh = nullptr;
  85. bool ack_block_to_server = false;
  86. MeshUpdateResult() = default;
  87. };
  88. class MeshUpdateThread : public UpdateThread
  89. {
  90. public:
  91. MeshUpdateThread(Client *client);
  92. // Caches the block at p and its neighbors (if needed) and queues a mesh
  93. // update for the block at p
  94. void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
  95. bool update_neighbors = false);
  96. v3s16 m_camera_offset;
  97. MutexedQueue<MeshUpdateResult> m_queue_out;
  98. private:
  99. MeshUpdateQueue m_queue_in;
  100. // TODO: Add callback to update these when g_settings changes
  101. int m_generation_interval;
  102. protected:
  103. virtual void doUpdate();
  104. };