mesh_generator_thread.h 3.3 KB

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