Skip to content

Commit

Permalink
Add meshgen_threads setting
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Feb 13, 2022
1 parent 66db44b commit 0bf3c59
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/client/client_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct ClientConfig client_config = {
.mipmap = true,
.render_distance = 255.0,
.vsync = true,
.meshgen_threads = 4,
};

__attribute__((constructor)) static void client_config_init()
Expand All @@ -30,7 +31,12 @@ __attribute__((constructor)) static void client_config_init()
.type = CT_BOOL,
.key = "vsync",
.value = &client_config.vsync,
}
}, 4);
},
{
.type = CT_UINT,
.key = "meshgen_threads",
.value = &client_config.meshgen_threads,
},
}, 5);
}

1 change: 1 addition & 0 deletions src/client/client_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern struct ClientConfig {
bool mipmap;
double render_distance;
bool vsync;
unsigned int meshgen_threads;
} client_config;

#endif
9 changes: 6 additions & 3 deletions src/client/client_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include "client/blockmesh.h"
#include "client/facecache.h"
#include "client/client_config.h"
#include "client/client_map.h"
#include "client/client_player.h"
#include "client/debug_menu.h"
Expand Down Expand Up @@ -165,7 +166,8 @@ void client_map_init()
client_map.sync_thread = 0;
client_map_set_simulation_distance(10);

for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
client_map.meshgen_threads = malloc(sizeof *client_map.meshgen_threads * client_config.meshgen_threads);
for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
client_map.meshgen_threads[i] = 0;
}

Expand All @@ -179,7 +181,7 @@ void client_map_deinit()
// start meshgen and sync threads
void client_map_start()
{
for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
pthread_create(&client_map.meshgen_threads[i], NULL, &meshgen_thread, NULL);

pthread_create(&client_map.sync_thread, NULL, &sync_thread, NULL);
Expand All @@ -191,9 +193,10 @@ void client_map_stop()
client_map.cancel = true;
queue_cancel(client_map.queue);

for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
if (client_map.meshgen_threads[i])
pthread_join(client_map.meshgen_threads[i], NULL);
free(client_map.meshgen_threads);

if (client_map.sync_thread)
pthread_join(client_map.sync_thread, NULL);
Expand Down
15 changes: 7 additions & 8 deletions src/client/client_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <dragonstd/queue.h>
#include "map.h"
#include "client/object.h"
#define NUM_MESHGEN_THREADS 4

typedef enum
{
Expand All @@ -25,13 +24,13 @@ typedef struct

extern struct ClientMap
{
Map *map; // map object
Queue *queue; // MapBlock * queue (thread safe)
atomic_bool cancel; // used to notify meshgen and sync thread about quit
pthread_t meshgen_threads[NUM_MESHGEN_THREADS]; // consumer threads for meshgen queue
pthread_t sync_thread; // this thread requests new / changed blocks from server
u32 simulation_distance; // simulation distance sent by server
size_t blocks_count; // cached number of facecache positions to process every sync step (matches simulation distance)
Map *map; // map object
Queue *queue; // MapBlock * queue (thread safe)
atomic_bool cancel; // used to notify meshgen and sync thread about quit
pthread_t *meshgen_threads; // consumer threads for meshgen queue
pthread_t sync_thread; // this thread requests new / changed blocks from server
u32 simulation_distance; // simulation distance sent by server
size_t blocks_count; // cached number of facecache positions to process every sync step (matches simulation distance)
} client_map;

void client_map_init(); // ClientMap singleton constructor
Expand Down

0 comments on commit 0bf3c59

Please sign in to comment.