2017-04-15 09:55:52 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2017-04-15 09:55:52 +02:00
|
|
|
|
2017-06-27 11:54:40 +02:00
|
|
|
#include <ctime>
|
2017-06-06 16:29:28 +02:00
|
|
|
#include <mutex>
|
2017-04-15 09:55:52 +02:00
|
|
|
#include "mapblock_mesh.h"
|
|
|
|
#include "threading/mutex_auto_lock.h"
|
|
|
|
#include "util/thread.h"
|
|
|
|
|
|
|
|
struct CachedMapBlockData
|
|
|
|
{
|
2017-06-18 19:55:15 +02:00
|
|
|
v3s16 p = v3s16(-1337, -1337, -1337);
|
|
|
|
MapNode *data = nullptr; // A copy of the MapBlock's data member
|
|
|
|
int refcount_from_queue = 0;
|
2017-06-27 11:54:40 +02:00
|
|
|
std::time_t last_used_timestamp = std::time(0);
|
2017-04-15 09:55:52 +02:00
|
|
|
|
2017-08-19 09:29:55 +02:00
|
|
|
CachedMapBlockData() = default;
|
2017-04-15 09:55:52 +02:00
|
|
|
~CachedMapBlockData();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct QueuedMeshUpdate
|
|
|
|
{
|
2017-06-18 19:55:15 +02:00
|
|
|
v3s16 p = v3s16(-1337, -1337, -1337);
|
|
|
|
bool ack_block_to_server = false;
|
|
|
|
bool urgent = false;
|
|
|
|
int crack_level = -1;
|
2017-04-15 09:55:52 +02:00
|
|
|
v3s16 crack_pos;
|
2017-06-18 19:55:15 +02:00
|
|
|
MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
|
2017-04-15 09:55:52 +02:00
|
|
|
|
2017-08-19 09:29:55 +02:00
|
|
|
QueuedMeshUpdate() = default;
|
2017-04-15 09:55:52 +02:00
|
|
|
~QueuedMeshUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
A thread-safe queue of mesh update tasks and a cache of MapBlock data
|
|
|
|
*/
|
|
|
|
class MeshUpdateQueue
|
|
|
|
{
|
2017-04-18 17:13:50 +02:00
|
|
|
enum UpdateMode
|
|
|
|
{
|
2017-04-15 09:55:52 +02:00
|
|
|
FORCE_UPDATE,
|
|
|
|
SKIP_UPDATE_IF_ALREADY_CACHED,
|
|
|
|
};
|
2017-04-18 17:13:50 +02:00
|
|
|
|
2017-04-15 09:55:52 +02:00
|
|
|
public:
|
|
|
|
MeshUpdateQueue(Client *client);
|
|
|
|
|
|
|
|
~MeshUpdateQueue();
|
|
|
|
|
|
|
|
// Caches the block at p and its neighbors (if needed) and queues a mesh
|
|
|
|
// update for the block at p
|
|
|
|
void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
|
|
|
|
|
|
|
|
// Returned pointer must be deleted
|
|
|
|
// Returns NULL if queue is empty
|
2017-04-18 17:13:50 +02:00
|
|
|
QueuedMeshUpdate *pop();
|
2017-04-15 09:55:52 +02:00
|
|
|
|
|
|
|
u32 size()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
return m_queue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Client *m_client;
|
2017-04-18 17:13:50 +02:00
|
|
|
std::vector<QueuedMeshUpdate *> m_queue;
|
2017-04-15 09:55:52 +02:00
|
|
|
std::set<v3s16> m_urgents;
|
2017-04-18 17:13:50 +02:00
|
|
|
std::map<v3s16, CachedMapBlockData *> m_cache;
|
2017-06-06 16:29:28 +02:00
|
|
|
std::mutex m_mutex;
|
2017-04-15 09:55:52 +02:00
|
|
|
|
|
|
|
// TODO: Add callback to update these when g_settings changes
|
|
|
|
bool m_cache_enable_shaders;
|
|
|
|
bool m_cache_use_tangent_vertices;
|
|
|
|
bool m_cache_smooth_lighting;
|
|
|
|
int m_meshgen_block_cache_size;
|
|
|
|
|
2017-04-18 17:13:50 +02:00
|
|
|
CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
|
|
|
|
size_t *cache_hit_counter = NULL);
|
|
|
|
CachedMapBlockData *getCachedBlock(const v3s16 &p);
|
2017-04-15 09:55:52 +02:00
|
|
|
void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
|
|
|
|
void cleanupCache();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MeshUpdateResult
|
|
|
|
{
|
2017-06-18 19:55:15 +02:00
|
|
|
v3s16 p = v3s16(-1338, -1338, -1338);
|
|
|
|
MapBlockMesh *mesh = nullptr;
|
|
|
|
bool ack_block_to_server = false;
|
2017-04-15 09:55:52 +02:00
|
|
|
|
2017-08-19 09:29:55 +02:00
|
|
|
MeshUpdateResult() = default;
|
2017-04-15 09:55:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MeshUpdateThread : public UpdateThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MeshUpdateThread(Client *client);
|
|
|
|
|
|
|
|
// Caches the block at p and its neighbors (if needed) and queues a mesh
|
|
|
|
// update for the block at p
|
|
|
|
void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
|
|
|
|
|
|
|
|
v3s16 m_camera_offset;
|
|
|
|
MutexedQueue<MeshUpdateResult> m_queue_out;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MeshUpdateQueue m_queue_in;
|
|
|
|
|
|
|
|
// TODO: Add callback to update these when g_settings changes
|
|
|
|
int m_generation_interval;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void doUpdate();
|
|
|
|
};
|