major optimisations and changes in logic

isolation_bkp/dynres
Dominik Madarász 2022-09-14 06:45:01 +00:00 committed by GitHub
parent 4197f2c82f
commit 77b98f71be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 59 additions and 41 deletions

View File

@ -15,7 +15,7 @@ if(MSVC)
endif() endif()
if (EMSCRIPTEN) if (EMSCRIPTEN)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 --profiling -s ASSERTIONS=1 -s WASM=1 -s INITIAL_MEMORY=268435456 -s FORCE_FILESYSTEM=1 --preload-file ${CMAKE_SOURCE_DIR}/art@art/ --shell-file ${CMAKE_SOURCE_DIR}/web/eco2d.html") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 --profiling -s ASSERTIONS=1 -s WASM=1 -s INITIAL_MEMORY=268435456 -s FORCE_FILESYSTEM=1 --preload-file ${CMAKE_SOURCE_DIR}/art@art/ --shell-file ${CMAKE_SOURCE_DIR}/web/eco2d.html --post-js ${CMAKE_SOURCE_DIR}/web/eco2d-post.js")
set(CMAKE_EXECUTABLE_SUFFIX ".html") set(CMAKE_EXECUTABLE_SUFFIX ".html")
endif () endif ()

View File

@ -23,6 +23,8 @@ typedef struct {
static int64_t assets_frame_counter = 1; static int64_t assets_frame_counter = 1;
static double assets_frame_next_draw = 0.0; static double assets_frame_next_draw = 0.0;
#include <time.h>
int32_t assets_setup(void) { int32_t assets_setup(void) {
for (uint32_t i=0; i<ASSETS_COUNT; i++) { for (uint32_t i=0; i<ASSETS_COUNT; i++) {
asset *b = &assets[i]; asset *b = &assets[i];
@ -43,13 +45,12 @@ int32_t assets_setup(void) {
default: break; default: break;
} }
} }
assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS;
assets_frame_next_draw = zpl_time_rel() + ASSET_FRAME_RENDER_MS;
return 0; return 0;
} }
int32_t assets_frame(void) { int32_t assets_frame(void) {
if (assets_frame_next_draw < zpl_time_rel()) { if (assets_frame_next_draw < get_cached_time()) {
for (uint32_t i=0; i<ASSETS_COUNT; i++) { for (uint32_t i=0; i<ASSETS_COUNT; i++) {
asset *b = &assets[i]; asset *b = &assets[i];
@ -63,7 +64,7 @@ int32_t assets_frame(void) {
} }
} }
assets_frame_next_draw = zpl_time_rel() + ASSET_FRAME_RENDER_MS; assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS;
assets_frame_counter += ASSET_FRAME_SKIP; assets_frame_counter += ASSET_FRAME_SKIP;
} }

View File

@ -22,7 +22,7 @@ static asset assets[] = {
ASSET_TEX(ASSET_DEV), ASSET_TEX(ASSET_DEV),
ASSET_TEX(ASSET_GROUND), ASSET_TEX(ASSET_GROUND),
ASSET_TEX(ASSET_DIRT), ASSET_TEX(ASSET_DIRT),
ASSET_ANI(ASSET_WATER), ASSET_TEX(ASSET_WATER),
ASSET_TEX(ASSET_LAVA), ASSET_TEX(ASSET_LAVA),
ASSET_TEX(ASSET_WALL), ASSET_TEX(ASSET_WALL),
ASSET_TEX(ASSET_HILL), ASSET_TEX(ASSET_HILL),

View File

@ -116,7 +116,7 @@ void debug_replay_start(void) {
if (records) zpl_array_free(records); if (records) zpl_array_free(records);
zpl_array_init_reserve(records, zpl_heap(), UINT16_MAX); zpl_array_init_reserve(records, zpl_heap(), UINT16_MAX);
last_record_time = zpl_time_rel(); last_record_time = get_cached_time();
SetTargetFPS(60); SetTargetFPS(60);
} }
@ -155,7 +155,7 @@ void debug_replay_run(void) {
if (mime) return; if (mime) return;
is_playing = true; is_playing = true;
record_pos = 0; record_pos = 0;
playback_time = zpl_time_rel(); playback_time = get_cached_time();
zpl_array_init(temp_actors, zpl_heap()); zpl_array_init(temp_actors, zpl_heap());
plr = camera_get().ent_id; plr = camera_get().ent_id;
@ -181,10 +181,10 @@ void ActSpawnBelt(void);
void debug_replay_update(void) { void debug_replay_update(void) {
if (!is_playing) return; if (!is_playing) return;
if (playback_time >= zpl_time_rel()) return; if (playback_time >= get_cached_time()) return;
replay_record *r = &records[record_pos]; replay_record *r = &records[record_pos];
playback_time = zpl_time_rel() + r->delay; playback_time = get_cached_time() + r->delay;
switch (r->kind) { switch (r->kind) {
case RPKIND_KEY: { case RPKIND_KEY: {
@ -249,7 +249,7 @@ void debug_replay_update(void) {
void debug_replay_record_keystate(pkt_send_keystate state) { void debug_replay_record_keystate(pkt_send_keystate state) {
if (!is_recording) return; if (!is_recording) return;
double record_time = zpl_time_rel(); double record_time = get_cached_time();
replay_record rec = { replay_record rec = {
.kind = RPKIND_KEY, .kind = RPKIND_KEY,
@ -258,13 +258,13 @@ void debug_replay_record_keystate(pkt_send_keystate state) {
}; };
zpl_array_append(records, rec); zpl_array_append(records, rec);
last_record_time = zpl_time_rel(); last_record_time = get_cached_time();
} }
void debug_replay_special_action(replay_kind kind) { void debug_replay_special_action(replay_kind kind) {
ZPL_ASSERT(kind != RPKIND_KEY); ZPL_ASSERT(kind != RPKIND_KEY);
if (!is_recording || is_playing) return; if (!is_recording || is_playing) return;
double record_time = zpl_time_rel(); double record_time = get_cached_time();
replay_record rec = { replay_record rec = {
.kind = kind, .kind = kind,
@ -272,5 +272,5 @@ void debug_replay_special_action(replay_kind kind) {
}; };
zpl_array_append(records, rec); zpl_array_append(records, rec);
last_record_time = zpl_time_rel(); last_record_time = get_cached_time();
} }

View File

@ -79,7 +79,7 @@ void entity_update_action_timers() {
static double last_update_time = 0.0f; static double last_update_time = 0.0f;
if (!ecs_streaminfo) { if (!ecs_streaminfo) {
ecs_streaminfo = ecs_query_new(world_ecs(), "components.StreamInfo"); ecs_streaminfo = ecs_query_new(world_ecs(), "components.StreamInfo");
last_update_time = zpl_time_rel(); last_update_time = get_cached_time();
} }
ecs_iter_t it = ecs_query_iter(world_ecs(), ecs_streaminfo); ecs_iter_t it = ecs_query_iter(world_ecs(), ecs_streaminfo);
@ -88,17 +88,17 @@ void entity_update_action_timers() {
StreamInfo *si = ecs_field(&it, StreamInfo, 1); StreamInfo *si = ecs_field(&it, StreamInfo, 1);
for (int32_t i = 0; i < it.count; i++) { for (int32_t i = 0; i < it.count; i++) {
if (si[i].last_update < zpl_time_rel()) { if (si[i].last_update < get_cached_time()) {
si[i].last_update = zpl_time_rel() + si[i].tick_delay; si[i].last_update = get_cached_time() + si[i].tick_delay;
si[i].tick_delay += (zpl_time_rel() - last_update_time) * 0.5f; si[i].tick_delay += (get_cached_time() - last_update_time) * 0.5f;
} }
} }
} }
last_update_time = zpl_time_rel(); last_update_time = get_cached_time();
} }
bool entity_can_stream(uint64_t ent_id) { bool entity_can_stream(uint64_t ent_id) {
StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo); StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo);
return (si->last_update < zpl_time_rel()); return (si->last_update < get_cached_time());
} }

View File

@ -122,7 +122,7 @@ void flecs_dash_init() {
} }
float game_time() { float game_time() {
return (float)zpl_time_rel(); return (float)get_cached_time();
} }
void game_init(const char *ip, uint16_t port, 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(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
@ -232,15 +232,15 @@ void game_update() {
if (game_mode == GAMEKIND_HEADLESS) { if (game_mode == GAMEKIND_HEADLESS) {
network_server_tick(); network_server_tick();
static uint64_t ms_report = 2500; static float ms_report = 2.5f;
if (ms_report < zpl_time_rel_ms()) { if (ms_report < get_cached_time()) {
ms_report = zpl_time_rel_ms() + 5000; ms_report = get_cached_time() + 5.f;
zpl_printf("delta: %f ms.\n", (zpl_time_rel() - last_update)*1000.0f); zpl_printf("delta: %f ms.\n", (get_cached_time() - last_update)*1000.0f);
} }
} }
} }
last_update = zpl_time_rel(); last_update = get_cached_time();
} }
void game_render() { void game_render() {

View File

@ -33,11 +33,11 @@ Texture2D GenColorEco(Color color) {
Texture2D texgen_build_anim(asset_id id, int64_t counter) { Texture2D texgen_build_anim(asset_id id, int64_t counter) {
(void)counter; (void)counter;
switch (id) { switch (id) {
case ASSET_WATER: { // case ASSET_WATER: {
Image img = LoadImageEco("water"); // Image img = LoadImageEco("water");
ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2)); // ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2));
return Image2TexEco(img); // return Image2TexEco(img);
}break; // }break;
default: return GenColorEco(PINK); break; default: return GenColorEco(PINK); break;
} }
@ -61,6 +61,7 @@ Texture2D texgen_build_sprite(asset_id id) {
case ASSET_LAVA: return LoadTexEco("lava"); case ASSET_LAVA: return LoadTexEco("lava");
case ASSET_WOOD: return LoadTexEco("wood"); case ASSET_WOOD: return LoadTexEco("wood");
case ASSET_TREE: return LoadTexEco("tree"); case ASSET_TREE: return LoadTexEco("tree");
case ASSET_WATER: return LoadTexEco("water");
case ASSET_BELT: case ASSET_BELT:
case ASSET_BELT_RIGHT: return LoadTexEco("belt_right"); case ASSET_BELT_RIGHT: return LoadTexEco("belt_right");

View File

@ -80,6 +80,7 @@ int main(int argc, char** argv) {
#if !defined(PLATFORM_WEB) #if !defined(PLATFORM_WEB)
while (game_is_running()) { while (game_is_running()) {
reset_cached_time();
profile (PROF_MAIN_LOOP) { profile (PROF_MAIN_LOOP) {
game_input(); game_input();
game_update(); game_update();
@ -102,6 +103,7 @@ int main(int argc, char** argv) {
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
void UpdateDrawFrame(void) { void UpdateDrawFrame(void) {
reset_cached_time();
profile (PROF_MAIN_LOOP) { profile (PROF_MAIN_LOOP) {
game_input(); game_input();
game_update(); game_update();
@ -111,3 +113,12 @@ void UpdateDrawFrame(void) {
profiler_collate(); profiler_collate();
} }
#endif #endif
static float temp_time = 0.0f;
float get_cached_time(void) {
return temp_time;
}
void reset_cached_time(void) {
temp_time = zpl_time_rel();
}

View File

@ -120,7 +120,7 @@ network_client_fetch_stats(void) {
static float incoming_bandwidth = 0.0f; static float incoming_bandwidth = 0.0f;
static float outgoing_bandwidth = 0.0f; static float outgoing_bandwidth = 0.0f;
if (next_measure < zpl_time_rel()) { if (next_measure < get_cached_time()) {
#define MAX_RATE_SAMPLES 8 #define MAX_RATE_SAMPLES 8
static uint64_t last_total_sent = 0; static uint64_t last_total_sent = 0;
static uint64_t last_total_recv = 0; static uint64_t last_total_recv = 0;
@ -145,7 +145,7 @@ network_client_fetch_stats(void) {
incoming_bandwidth = stats.incoming_bandwidth /= MAX_RATE_SAMPLES; incoming_bandwidth = stats.incoming_bandwidth /= MAX_RATE_SAMPLES;
outgoing_bandwidth = stats.outgoing_bandwidth /= MAX_RATE_SAMPLES; outgoing_bandwidth = stats.outgoing_bandwidth /= MAX_RATE_SAMPLES;
next_measure = zpl_time_rel() + 1.0; next_measure = get_cached_time() + 1.0;
} else { } else {
stats.incoming_bandwidth = incoming_bandwidth; stats.incoming_bandwidth = incoming_bandwidth;
stats.outgoing_bandwidth = outgoing_bandwidth; stats.outgoing_bandwidth = outgoing_bandwidth;

View File

@ -41,7 +41,7 @@ int32_t pkt_send_librg_update_handler(pkt_header *header) {
int32_t state = librg_world_read(view->tracker, header->view_id, uc.item.as.bin.start, uc.item.as.bin.length, NULL); int32_t state = librg_world_read(view->tracker, header->view_id, uc.item.as.bin.start, uc.item.as.bin.length, NULL);
if (state < 0) zpl_printf("[ERROR] world read error: %d\n", state); if (state < 0) zpl_printf("[ERROR] world read error: %d\n", state);
float now = (float)zpl_time_rel(); float now = (float)get_cached_time();
view->delta_time[layer_id] = now - view->last_update[layer_id]; view->delta_time[layer_id] = now - view->last_update[layer_id];
view->last_update[layer_id] = now; view->last_update[layer_id] = now;

View File

@ -10,6 +10,9 @@
#define ZPL_ENABLE_MATH #define ZPL_ENABLE_MATH
#include "zpl.h" #include "zpl.h"
float get_cached_time(void);
void reset_cached_time(void);
#define defer_var ZPL_CONCAT(_i_,__LINE__) #define defer_var ZPL_CONCAT(_i_,__LINE__)
#define defer(s,e) for ( \ #define defer(s,e) for ( \
uint32_t defer_var = (s, 0); \ uint32_t defer_var = (s, 0); \

View File

@ -307,8 +307,8 @@ int32_t world_destroy(void) {
#define WORLD_LIBRG_BUFSIZ 2000000 #define WORLD_LIBRG_BUFSIZ 2000000
static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) { static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) {
if (world.tracker_update[ticker] > (float)zpl_time_rel()) return; if (world.tracker_update[ticker] > (float)(get_cached_time())) return;
world.tracker_update[ticker] = (float)zpl_time_rel() + freq; world.tracker_update[ticker] = (float)(get_cached_time()) + freq;
profile(PROF_WORLD_WRITE) { profile(PROF_WORLD_WRITE) {
ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update); ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update);

View File

@ -26,7 +26,7 @@ int32_t tracker_read_update(librg_world *w, librg_event *e) {
entity_view *d = entity_view_get(&view->entities, entity_id); entity_view *d = entity_view_get(&view->entities, entity_id);
#if 1 #if 1
if (d && d->layer_id < view->active_layer_id) { if (d && d->layer_id < view->active_layer_id) {
if (zpl_time_rel_ms() - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) { if ((get_cached_time()*1000.0f) - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) {
d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1); d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1);
} }
// NOTE(zaklaus): reject updates from slower layers // NOTE(zaklaus): reject updates from slower layers
@ -34,7 +34,7 @@ int32_t tracker_read_update(librg_world *w, librg_event *e) {
} }
#endif #endif
data.last_update = zpl_time_rel_ms(); data.last_update = get_cached_time()*1000.0f;
data.layer_id = view->active_layer_id; data.layer_id = view->active_layer_id;
predict_receive_update(d, &data); predict_receive_update(d, &data);
entity_view_update_or_create(&view->entities, entity_id, data); entity_view_update_or_create(&view->entities, entity_id, data);

View File

@ -0,0 +1,2 @@
// Hack to enforce CLOCK_REALTIME, which is significantly faster for our purposes.
_emscripten_get_now = () => Date.now();