mesh_generator_thread.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <unordered_map>
  20. #include <unordered_set>
  21. #include "mapblock_mesh.h"
  22. #include "threading/mutex_auto_lock.h"
  23. #include "util/thread.h"
  24. #include <vector>
  25. #include <memory>
  26. struct QueuedMeshUpdate
  27. {
  28. v3s16 p = v3s16(-1337, -1337, -1337);
  29. bool ack_block_to_server = false;
  30. int crack_level = -1;
  31. v3s16 crack_pos;
  32. MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
  33. std::vector<MapBlock *> map_blocks;
  34. bool urgent = false;
  35. QueuedMeshUpdate() = default;
  36. ~QueuedMeshUpdate();
  37. };
  38. /*
  39. A thread-safe queue of mesh update tasks and a cache of MapBlock data
  40. */
  41. class MeshUpdateQueue
  42. {
  43. enum UpdateMode
  44. {
  45. FORCE_UPDATE,
  46. SKIP_UPDATE_IF_ALREADY_CACHED,
  47. };
  48. public:
  49. MeshUpdateQueue(Client *client);
  50. ~MeshUpdateQueue();
  51. // Caches the block at p and its neighbors (if needed) and queues a mesh
  52. // update for the block at p
  53. bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
  54. // Returned pointer must be deleted
  55. // Returns NULL if queue is empty
  56. QueuedMeshUpdate *pop();
  57. // Marks a position as finished, unblocking the next update
  58. void done(v3s16 pos);
  59. u32 size()
  60. {
  61. MutexAutoLock lock(m_mutex);
  62. return m_queue.size();
  63. }
  64. private:
  65. Client *m_client;
  66. std::vector<QueuedMeshUpdate *> m_queue;
  67. std::unordered_set<v3s16> m_urgents;
  68. std::unordered_set<v3s16> m_inflight_blocks;
  69. std::mutex m_mutex;
  70. // TODO: Add callback to update these when g_settings changes
  71. bool m_cache_enable_shaders;
  72. bool m_cache_smooth_lighting;
  73. int m_meshgen_block_cache_size;
  74. void fillDataFromMapBlocks(QueuedMeshUpdate *q);
  75. void cleanupCache();
  76. };
  77. struct MeshUpdateResult
  78. {
  79. v3s16 p = v3s16(-1338, -1338, -1338);
  80. MapBlockMesh *mesh = nullptr;
  81. u8 solid_sides = 0;
  82. bool ack_block_to_server = false;
  83. bool urgent = false;
  84. std::vector<MapBlock *> map_blocks;
  85. MeshUpdateResult() = default;
  86. };
  87. class MeshUpdateManager;
  88. class MeshUpdateWorkerThread : public UpdateThread
  89. {
  90. public:
  91. MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
  92. protected:
  93. virtual void doUpdate();
  94. private:
  95. MeshUpdateQueue *m_queue_in;
  96. MeshUpdateManager *m_manager;
  97. v3s16 *m_camera_offset;
  98. // TODO: Add callback to update these when g_settings changes
  99. int m_generation_interval;
  100. };
  101. class MeshUpdateManager
  102. {
  103. public:
  104. MeshUpdateManager(Client *client);
  105. // Caches the block at p and its neighbors (if needed) and queues a mesh
  106. // update for the block at p
  107. void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
  108. bool update_neighbors = false);
  109. void putResult(const MeshUpdateResult &r);
  110. bool getNextResult(MeshUpdateResult &r);
  111. v3s16 m_camera_offset;
  112. void start();
  113. void stop();
  114. void wait();
  115. bool isRunning();
  116. private:
  117. void deferUpdate();
  118. MeshUpdateQueue m_queue_in;
  119. MutexedQueue<MeshUpdateResult> m_queue_out;
  120. MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
  121. std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;
  122. };