Remove GC for OOB entities + perf improvements

isolation_bkp/dynres
Dominik Madarász 2021-10-27 10:55:07 +02:00
parent ff9f483da1
commit e7f55985db
7 changed files with 238 additions and 281 deletions

View File

@ -3,7 +3,7 @@
static debug_draw_queue draw_queue = {0}; static debug_draw_queue draw_queue = {0};
#ifdef ECO2D_PROD #ifndef _DEBUG
static bool draw_is_enabled = false; static bool draw_is_enabled = false;
#else #else
static bool draw_is_enabled = true; static bool draw_is_enabled = true;

View File

@ -193,7 +193,6 @@ static debug_item items[] = {
{ .kind = DITEM_RAW, .val = PROF_RENDER, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_RENDER, .proc = DrawProfilerDelta },
{ .kind = DITEM_RAW, .val = PROF_UPDATE_SYSTEMS, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_UPDATE_SYSTEMS, .proc = DrawProfilerDelta },
{ .kind = DITEM_RAW, .val = PROF_ENTITY_LERP, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_ENTITY_LERP, .proc = DrawProfilerDelta },
{ .kind = DITEM_RAW, .val = PROF_ENTITY_REMOVAL, .proc = DrawProfilerDelta },
{ .kind = DITEM_RAW, .val = PROF_INTEGRATE_POS, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_INTEGRATE_POS, .proc = DrawProfilerDelta },
{ .kind = DITEM_END }, { .kind = DITEM_END },
}, },

View File

@ -217,10 +217,6 @@ void game_update() {
network_server_tick(); network_server_tick();
} }
} }
if (game_mode != GAMEKIND_HEADLESS) {
game_world_cleanup_entities();
}
} }
void game_render() { void game_render() {
@ -244,35 +240,6 @@ void game_action_send_keystate(float x,
pkt_send_keystate_send(active_viewer->view_id, x, y, mx, my, use, sprint, ctrl, drop, selected_item, swap, swap_from, swap_to); pkt_send_keystate_send(active_viewer->view_id, x, y, mx, my, use, sprint, ctrl, drop, selected_item, swap, swap_from, swap_to);
} }
#define GAME_ENT_REMOVAL_TIME 10000
#define GAME_ENT_REMOVAL_TRESHOLD 500
void game_world_cleanup_entities(void) {
// TODO(zaklaus): not the best approach to do a cleanup, let memory stay for a while as it might be reused later on anyway.
#if 1
profile(PROF_ENTITY_REMOVAL) {
static uint64_t last_removal_time = 0;
if (last_removal_time > zpl_time_rel_ms()) return;
last_removal_time = zpl_time_rel_ms() + GAME_ENT_REMOVAL_TIME;
for (int i = 0; i < zpl_buffer_count(world_viewers); i += 1){
entity_view_tbl *view = &world_viewers[i].entities;
uint32_t deletions = 0;
for (int j = 0; j < zpl_array_count(view->entries); j += 1) {
if (deletions > GAME_ENT_REMOVAL_TRESHOLD) return;
entity_view *e = &view->entries[j].value;
if (e->tran_effect == ETRAN_REMOVE) {
entity_view_tbl_remove(view, e->ent_id);
deletions++;
}
}
}
}
#endif
}
void game_request_close() { void game_request_close() {
platform_request_close(); platform_request_close();
} }

View File

@ -1,47 +1,46 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
#include "world_view.h" #include "world_view.h"
typedef enum { typedef enum {
GAMEKIND_SINGLE, GAMEKIND_SINGLE,
GAMEKIND_CLIENT, GAMEKIND_CLIENT,
GAMEKIND_HEADLESS, GAMEKIND_HEADLESS,
FORCE_GAMEKIND_UINT8 = UINT8_MAX FORCE_GAMEKIND_UINT8 = UINT8_MAX
} game_kind; } game_kind;
void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled); void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled);
void game_shutdown(); void game_shutdown();
void game_request_close(); void game_request_close();
uint8_t game_is_running(); uint8_t game_is_running();
int8_t game_is_networked(); int8_t game_is_networked();
float game_time(); float game_time();
game_kind game_get_kind(void); game_kind game_get_kind(void);
//~ NOTE(zaklaus): game events //~ NOTE(zaklaus): game events
void game_input(); void game_input();
void game_update(); void game_update();
void game_render(); void game_render();
//~ NOTE(zaklaus): world view management //~ NOTE(zaklaus): world view management
world_view *game_world_view_get_active(void); world_view *game_world_view_get_active(void);
world_view *game_world_view_get(uint16_t idx); world_view *game_world_view_get(uint16_t idx);
void game_world_view_set_active_by_idx(uint16_t idx); void game_world_view_set_active_by_idx(uint16_t idx);
void game_world_view_set_active(world_view *view); void game_world_view_set_active(world_view *view);
void game_world_view_cycle_active(int8_t dir); void game_world_view_cycle_active(int8_t dir);
void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_view * value)); void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_view * value));
entity_view *game_world_view_active_get_entity(uint64_t ent_id); entity_view *game_world_view_active_get_entity(uint64_t ent_id);
void game_world_cleanup_entities(void);
//~ NOTE(zaklaus): viewer -> host actions
//~ NOTE(zaklaus): viewer -> host actions void game_action_send_keystate(float x,
void game_action_send_keystate(float x, float y,
float y, float mx,
float mx, float my,
float my, uint8_t use,
uint8_t use, uint8_t sprint,
uint8_t sprint, uint8_t ctrl,
uint8_t ctrl, uint8_t drop,
uint8_t drop, uint8_t selected_item,
uint8_t selected_item, uint8_t swap,
uint8_t swap, uint8_t swap_from,
uint8_t swap_from,
uint8_t swap_to); uint8_t swap_to);

View File

@ -1,62 +1,61 @@
#include "profiler.h" #include "profiler.h"
#include "raylib.h" #include "raylib.h"
#include <assert.h> #include <assert.h>
#define PROF_COLLATE_WINDOW 0.5 #define PROF_COLLATE_WINDOW 0.5
// NOTE(zaklaus): KEEP ORDER IN SYNC WITH profiler_kind ENUM !!! // NOTE(zaklaus): KEEP ORDER IN SYNC WITH profiler_kind ENUM !!!
static profiler profilers[] = { static profiler profilers[] = {
{ .id = PROF_TOTAL_TIME, .name = "measured time" }, { .id = PROF_TOTAL_TIME, .name = "measured time" },
{ .id = PROF_MAIN_LOOP, .name = "main loop" }, { .id = PROF_MAIN_LOOP, .name = "main loop" },
{ .id = PROF_WORLD_WRITE, .name = "world write" }, { .id = PROF_WORLD_WRITE, .name = "world write" },
{ .id = PROF_RENDER, .name = "render" }, { .id = PROF_RENDER, .name = "render" },
{ .id = PROF_UPDATE_SYSTEMS, .name = "update systems" }, { .id = PROF_UPDATE_SYSTEMS, .name = "update systems" },
{ .id = PROF_ENTITY_LERP, .name = "entity lerp" }, { .id = PROF_ENTITY_LERP, .name = "entity lerp" },
{ .id = PROF_ENTITY_REMOVAL, .name = "entity removal" }, { .id = PROF_INTEGRATE_POS, .name = "entity movement" },
{ .id = PROF_INTEGRATE_POS, .name = "entity movement" }, };
};
static_assert((sizeof(profilers)/sizeof(profilers[0])) == MAX_PROF, "mismatched profilers");
static_assert((sizeof(profilers)/sizeof(profilers[0])) == MAX_PROF, "mismatched profilers");
void profiler_reset(profiler_kind id) {
void profiler_reset(profiler_kind id) { profilers[id].num_invocations = 0;
profilers[id].num_invocations = 0; profilers[id].total_time = 0.0;
profilers[id].total_time = 0.0; }
}
void profiler_start(profiler_kind id) {
void profiler_start(profiler_kind id) { profilers[id].start_time = GetTime();
profilers[id].start_time = GetTime(); }
}
void profiler_stop(profiler_kind id) {
void profiler_stop(profiler_kind id) { profilers[id].num_invocations += 1;
profilers[id].num_invocations += 1; profilers[id].total_time += GetTime() - profilers[id].start_time;
profilers[id].total_time += GetTime() - profilers[id].start_time; profilers[id].start_time = 0.0;
profilers[id].start_time = 0.0; }
}
void profiler_collate() {
void profiler_collate() { static double frame_counter = 0.0;
static double frame_counter = 0.0; static uint64_t frames = 0;
static uint64_t frames = 0;
frame_counter += GetFrameTime();
frame_counter += GetFrameTime(); frames++;
frames++;
if (frame_counter >= PROF_COLLATE_WINDOW) {
if (frame_counter >= PROF_COLLATE_WINDOW) { profilers[PROF_TOTAL_TIME].delta_time = frame_counter / (double)frames;
profilers[PROF_TOTAL_TIME].delta_time = frame_counter / (double)frames;
for (uint32_t i = PROF_MAIN_LOOP; i < MAX_PROF; i += 1) {
for (uint32_t i = PROF_MAIN_LOOP; i < MAX_PROF; i += 1) { profiler *p = &profilers[i];
profiler *p = &profilers[i]; p->delta_time = p->num_invocations == 0 ? 0.0 : p->total_time / (double)p->num_invocations;
p->delta_time = p->num_invocations == 0 ? 0.0 : p->total_time / (double)p->num_invocations; }
}
frame_counter = 0.0;
frame_counter = 0.0; frames = 0;
frames = 0; }
} }
}
double profiler_delta(profiler_kind id) {
double profiler_delta(profiler_kind id) { return profilers[id].delta_time;
return profilers[id].delta_time; }
}
char const *profiler_name(profiler_kind id) {
char const *profiler_name(profiler_kind id) { return profilers[id].name;
return profilers[id].name; }
}

View File

@ -1,37 +1,36 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
typedef enum { typedef enum {
PROF_TOTAL_TIME, PROF_TOTAL_TIME,
PROF_MAIN_LOOP, PROF_MAIN_LOOP,
PROF_WORLD_WRITE, PROF_WORLD_WRITE,
PROF_RENDER, PROF_RENDER,
PROF_UPDATE_SYSTEMS, PROF_UPDATE_SYSTEMS,
PROF_ENTITY_LERP, PROF_ENTITY_LERP,
PROF_ENTITY_REMOVAL, PROF_INTEGRATE_POS,
PROF_INTEGRATE_POS,
MAX_PROF,
MAX_PROF, PROF_FORCE_UINT8 = UINT8_MAX
PROF_FORCE_UINT8 = UINT8_MAX } profiler_kind;
} profiler_kind;
typedef struct {
typedef struct { profiler_kind id;
profiler_kind id; char const *name;
char const *name;
uint32_t num_invocations;
uint32_t num_invocations; double start_time;
double start_time; double delta_time;
double delta_time; double total_time;
double total_time; } profiler;
} profiler;
void profiler_reset(profiler_kind id);
void profiler_reset(profiler_kind id); void profiler_start(profiler_kind id);
void profiler_start(profiler_kind id); void profiler_stop(profiler_kind id);
void profiler_stop(profiler_kind id); void profiler_collate(void);
void profiler_collate(void);
double profiler_delta(profiler_kind id);
double profiler_delta(profiler_kind id); char const *profiler_name(profiler_kind id);
char const *profiler_name(profiler_kind id);
#define profile(id) defer(profiler_start(id), profiler_stop(id))
#define profile(id) defer(profiler_start(id), profiler_stop(id))

View File

@ -1,101 +1,95 @@
#include "zpl.h" #include "zpl.h"
#include "world_view.h" #include "world_view.h"
#include "entity_view.h" #include "entity_view.h"
#include "prediction.h" #include "prediction.h"
#include "librg.h" #include "librg.h"
#include "world/world.h" #include "world/world.h"
#include "game.h" #include "game.h"
int32_t tracker_read_remove(librg_world *w, librg_event *e) { int32_t tracker_read_remove(librg_world *w, librg_event *e) {
int64_t entity_id = librg_event_entity_get(w, e); int64_t entity_id = librg_event_entity_get(w, e);
world_view *view = (world_view*)librg_world_userdata_get(w); world_view *view = (world_view*)librg_world_userdata_get(w);
entity_view_remove_chunk_texture(&view->entities, entity_id);
if (view != game_world_view_get_active()) { entity_view_destroy(&view->entities, entity_id);
entity_view_destroy(&view->entities, entity_id); return 0;
} else { }
entity_view_mark_for_removal(&view->entities, entity_id);
} int32_t tracker_read_update(librg_world *w, librg_event *e) {
int64_t entity_id = librg_event_entity_get(w, e);
entity_view_remove_chunk_texture(&view->entities, entity_id); size_t actual_length = librg_event_size_get(w, e);
return 0; char *buffer = librg_event_buffer_get(w, e);
} world_view *view = (world_view*)librg_world_userdata_get(w);
int32_t tracker_read_update(librg_world *w, librg_event *e) { entity_view data = entity_view_unpack_struct(buffer, actual_length);
int64_t entity_id = librg_event_entity_get(w, e); entity_view *d = entity_view_get(&view->entities, entity_id);
size_t actual_length = librg_event_size_get(w, e); #if 1
char *buffer = librg_event_buffer_get(w, e); if (d && d->layer_id < view->active_layer_id) {
world_view *view = (world_view*)librg_world_userdata_get(w); if (zpl_time_rel_ms() - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) {
d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1);
entity_view data = entity_view_unpack_struct(buffer, actual_length); }
entity_view *d = entity_view_get(&view->entities, entity_id); // NOTE(zaklaus): reject updates from slower layers
#if 1 else return 0;
if (d && d->layer_id < view->active_layer_id) { }
if (zpl_time_rel_ms() - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) { #endif
d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1);
} data.last_update = zpl_time_rel_ms();
// NOTE(zaklaus): reject updates from slower layers data.layer_id = view->active_layer_id;
else return 0; predict_receive_update(d, &data);
} entity_view_update_or_create(&view->entities, entity_id, data);
#endif entity_view_remove_chunk_texture(&view->entities, entity_id);
entity_view_update_chunk_texture(&view->entities, entity_id, view);
data.last_update = zpl_time_rel_ms(); return 0;
data.layer_id = view->active_layer_id; }
predict_receive_update(d, &data);
entity_view_update_or_create(&view->entities, entity_id, data); int32_t tracker_read_create(librg_world *w, librg_event *e) {
entity_view_remove_chunk_texture(&view->entities, entity_id); int64_t entity_id = librg_event_entity_get(w, e);
entity_view_update_chunk_texture(&view->entities, entity_id, view); size_t actual_length = librg_event_size_get(w, e);
return 0; char *buffer = librg_event_buffer_get(w, e);
} world_view *view = (world_view*)librg_world_userdata_get(w);
int32_t tracker_read_create(librg_world *w, librg_event *e) { entity_view data = entity_view_unpack_struct(buffer, actual_length);
int64_t entity_id = librg_event_entity_get(w, e); data.ent_id = entity_id;
size_t actual_length = librg_event_size_get(w, e); data.layer_id = view->active_layer_id;
char *buffer = librg_event_buffer_get(w, e); data.tran_time = 0.0f;
world_view *view = (world_view*)librg_world_userdata_get(w); data.color = rand(); // TODO(zaklaus): feed from server
if (data.flag & EFLAG_INTERP) {
entity_view data = entity_view_unpack_struct(buffer, actual_length); data.tx = data.x;
data.ent_id = entity_id; data.ty = data.y;
data.layer_id = view->active_layer_id; data.theading = data.heading;
data.tran_time = 0.0f; }
data.color = rand(); // TODO(zaklaus): feed from server entity_view_update_or_create(&view->entities, entity_id, data);
if (data.flag & EFLAG_INTERP) { entity_view_mark_for_fadein(&view->entities, entity_id);
data.tx = data.x; entity_view_update_chunk_texture(&view->entities, entity_id, view);
data.ty = data.y; return 0;
data.theading = data.heading; }
}
entity_view_update_or_create(&view->entities, entity_id, data); world_view world_view_create(uint16_t view_id) {
entity_view_mark_for_fadein(&view->entities, entity_id); world_view view = {0};
entity_view_update_chunk_texture(&view->entities, entity_id, view); view.view_id = view_id;
return 0; view.tracker = librg_world_create();
} entity_view_init(&view.entities);
return view;
world_view world_view_create(uint16_t view_id) { }
world_view view = {0};
view.view_id = view_id; void world_view_init(world_view *view, uint32_t seed, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount) {
view.tracker = librg_world_create(); view->seed = seed;
entity_view_init(&view.entities); view->owner_id = ent_id;
return view; view->chunk_size = chunk_size;
} view->chunk_amount = chunk_amount;
view->dim = WORLD_BLOCK_SIZE * chunk_size * chunk_amount;
void world_view_init(world_view *view, uint32_t seed, uint64_t ent_id, uint16_t chunk_size, uint16_t chunk_amount) { view->size = view->dim * view->dim;
view->seed = seed;
view->owner_id = ent_id; librg_config_chunksize_set(view->tracker, WORLD_BLOCK_SIZE * chunk_size, WORLD_BLOCK_SIZE * chunk_size, 1);
view->chunk_size = chunk_size; librg_config_chunkamount_set(view->tracker, chunk_amount, chunk_amount, 1);
view->chunk_amount = chunk_amount; librg_config_chunkoffset_set(view->tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, 0);
view->dim = WORLD_BLOCK_SIZE * chunk_size * chunk_amount;
view->size = view->dim * view->dim; librg_event_set(view->tracker, LIBRG_READ_CREATE, tracker_read_create);
librg_event_set(view->tracker, LIBRG_READ_REMOVE, tracker_read_remove);
librg_config_chunksize_set(view->tracker, WORLD_BLOCK_SIZE * chunk_size, WORLD_BLOCK_SIZE * chunk_size, 1); librg_event_set(view->tracker, LIBRG_READ_UPDATE, tracker_read_update);
librg_config_chunkamount_set(view->tracker, chunk_amount, chunk_amount, 1); librg_world_userdata_set(view->tracker, view);
librg_config_chunkoffset_set(view->tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, 0); }
librg_event_set(view->tracker, LIBRG_READ_CREATE, tracker_read_create); void world_view_destroy(world_view *view) {
librg_event_set(view->tracker, LIBRG_READ_REMOVE, tracker_read_remove); librg_world_destroy(view->tracker);
librg_event_set(view->tracker, LIBRG_READ_UPDATE, tracker_read_update); entity_view_free(&view->entities);
librg_world_userdata_set(view->tracker, view); }
}
void world_view_destroy(world_view *view) {
librg_world_destroy(view->tracker);
entity_view_free(&view->entities);
}