From 27f49512d6a258bf69e9543b40fbd7fd3ef65d3d Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 20 Feb 2025 18:24:22 -0600 Subject: [PATCH] prediction progress --- src/sim.c | 12 ++++++ src/user.c | 107 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 95 insertions(+), 24 deletions(-) diff --git a/src/sim.c b/src/sim.c index e07d1a6a..bfae2920 100644 --- a/src/sim.c +++ b/src/sim.c @@ -575,6 +575,12 @@ void sim_snapshot_encode(struct bitbuff_writer *bw, struct sim_client *receiver, bw_write_uv(bw, receiver->handle.gen); bw_write_uv(bw, receiver->handle.idx); + bw_write_f32(bw, ss1->control.move.x); + bw_write_f32(bw, ss1->control.move.y); + bw_write_f32(bw, ss1->control.focus.x); + bw_write_f32(bw, ss1->control.focus.y); + bw_write_ubits(bw, ss1->control.flags, 32); + /* Ents */ if (ss1->num_ents_allocated == ss0->num_ents_allocated) { bw_write_bit(bw, 0); @@ -622,6 +628,12 @@ void sim_snapshot_decode(struct bitbuff_reader *br, struct sim_snapshot *ss) ss->local_client.gen = br_read_uv(br); ss->local_client.idx = br_read_uv(br); + ss->control.move.x = br_read_f32(br); + ss->control.move.y = br_read_f32(br); + ss->control.focus.x = br_read_f32(br); + ss->control.focus.y = br_read_f32(br); + ss->control.flags = br_read_ubits(br, 32); + /* Ents */ if (br_read_bit(br)) { ss->num_ents_allocated = br_read_uv(br); diff --git a/src/user.c b/src/user.c index 89f3f6d3..717a530f 100644 --- a/src/user.c +++ b/src/user.c @@ -1744,6 +1744,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) /* Create user cmd snapshot */ { struct sim_snapshot *prev_user_cmd_ss = sim_snapshot_from_tick(user_client, user_client->last_tick); + struct sim_snapshot *user_cmd_ss = sim_snapshot_alloc(user_client, prev_user_cmd_ss, step_tick); struct sys_lock lock = sys_mutex_lock_e(&G.user_sim_cmd_mutex); user_cmd_ss->producer_client = user_client->handle; @@ -1812,6 +1813,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) if (base_ss->tick == base_tick) { struct sim_snapshot *ss = sim_snapshot_alloc(client, base_ss, tick); sim_snapshot_decode(&decoder_br, ss); + ss->producer_client = client->handle; } else { /* We do not have the base tick that the incoming tick is delta encoded from */ ASSERT(false); @@ -1824,6 +1826,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) if (base_ss->tick == base_tick) { struct sim_snapshot *ss = sim_snapshot_alloc(client, base_ss, tick); sim_snapshot_decode(&decoder_br, ss); + ss->producer_client = client->handle; } else { /* We do not have the base tick that the incoming tick is delta encoded from */ ASSERT(false); @@ -1845,6 +1848,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) } /* Release unneeded snapshots */ +#if 0 u64 oldest_client_ack = 0; for (u64 i = 0; i < store->num_clients_reserved; ++i) { struct sim_client *client = &store->clients[i]; @@ -1863,6 +1867,23 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) if (oldest_client_ack > 1) { sim_snapshot_release_ticks_in_range(local_client, 0, oldest_client_ack - 1); } +#else + for (u64 i = 0; i < store->num_clients_reserved; ++i) { + struct sim_client *client = &store->clients[i]; + enum sim_client_kind kind = client->kind; + if (client->valid && kind != SIM_CLIENT_KIND_LOCAL_SIM) { + u64 reverse_ack = client->reverse_ack; + if (reverse_ack > 1) { + sim_snapshot_release_ticks_in_range(client, 0, reverse_ack - 1); + } + } + } + u64 local_keep_ticks_count = 100; + if (step_tick > local_keep_ticks_count) { + u64 local_keep_tick = step_tick - local_keep_ticks_count; + sim_snapshot_release_ticks_in_range(local_client, 0, local_keep_tick); + } +#endif /* Pick client snapshot cmds to feed to sim step */ struct sim_snapshot_list client_step_cmds = ZI; @@ -1890,7 +1911,20 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) step_ss->is_master = true; } else { struct sim_snapshot *newest_master_ss = sim_snapshot_from_tick(master_client, master_client->last_tick); - step_ss = sim_snapshot_alloc(local_client, newest_master_ss, step_tick); + + struct sim_snapshot *local_copy = sim_snapshot_from_tick(local_client, newest_master_ss->tick); + if (!local_copy->valid) { + sim_snapshot_alloc(local_client, newest_master_ss, newest_master_ss->tick); + } + + step_ss = sim_snapshot_from_tick(local_client, step_tick); + if (!step_ss->valid) { + step_ss = sim_snapshot_from_tick(local_client, 1); + if (!step_ss->valid) { + step_ss = sim_snapshot_alloc(local_client, sim_snapshot_nil(), 1); + } + } + step_ss->is_master = false; } @@ -1912,29 +1946,30 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) switch (client->kind) { case SIM_CLIENT_KIND_SLAVE_SIM: { - struct sim_snapshot *base_ss = sim_snapshot_from_tick(client, client->ack); - if (base_ss->tick == client->ack) { - /* Encode single master state sanpshot snapshot */ - struct bitbuff_writer snapshot_bw = bw_from_bitbuff(&snapshot_writer_bb); - bw_write_uv(&snapshot_bw, base_ss->tick); - bw_write_uv(&snapshot_bw, step_ss->tick); - sim_snapshot_encode(&snapshot_bw, client, base_ss, step_ss); + struct sim_snapshot *base_ss = sim_snapshot_from_tick(local_client, client->ack); - struct string encoded_tmp = ZI; - encoded_tmp.len = bw_num_bytes_written(&snapshot_bw); - encoded_tmp.text = bw_get_written_raw(&snapshot_bw); + /* Encode single master state sanpshot snapshot */ + struct bitbuff_writer snapshot_bw = bw_from_bitbuff(&snapshot_writer_bb); + bw_write_uv(&snapshot_bw, base_ss->tick); + bw_write_uv(&snapshot_bw, step_ss->tick); + sim_snapshot_encode(&snapshot_bw, client, base_ss, step_ss); - u64 ack = client->last_tick; - u64 reverse_ack = client->ack; - bw_write_uv(&msg_bw, ack); - bw_write_uv(&msg_bw, reverse_ack); - bw_write_uv(&msg_bw, encoded_tmp.len); - bw_write_bytes(&msg_bw, encoded_tmp); - bw_write_uv(&msg_bw, 0); - } else { - /* We do not have the client's ack tick to delta encode from */ - ASSERT(false); - } + struct string encoded_tmp = ZI; + encoded_tmp.len = bw_num_bytes_written(&snapshot_bw); + encoded_tmp.text = bw_get_written_raw(&snapshot_bw); + + u64 ack = client->last_tick; + u64 reverse_ack = client->ack; + bw_write_uv(&msg_bw, ack); + bw_write_uv(&msg_bw, reverse_ack); + bw_write_uv(&msg_bw, encoded_tmp.len); + bw_write_bytes(&msg_bw, encoded_tmp); + bw_write_uv(&msg_bw, 0); + + struct string encoded = ZI; + encoded.len = bw_num_bytes_written(&msg_bw); + encoded.text = bw_get_written_raw(&msg_bw); + host_queue_write(host, client->channel_id, encoded, 0); } break; case SIM_CLIENT_KIND_MASTER_SIM: @@ -1947,7 +1982,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) bw_write_uv(&msg_bw, ack); bw_write_uv(&msg_bw, reverse_ack); /* Write all user cmd snapshots since last client ack */ - struct sim_snapshot *send_ss = sim_snapshot_from_tick(user_client, base_ss->tick + 1); + struct sim_snapshot *send_ss = sim_snapshot_from_tick(local_client, base_ss->tick + 1); while (send_ss->valid) { struct bitbuff_writer snapshot_bw = bw_from_bitbuff(&snapshot_writer_bb); bw_write_uv(&snapshot_bw, base_ss->tick); @@ -1960,8 +1995,15 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) bw_write_uv(&msg_bw, encoded_tmp.len); bw_write_bytes(&msg_bw, encoded_tmp); + + send_ss = sim_snapshot_from_tick(local_client, send_ss->tick + 1); } bw_write_uv(&msg_bw, 0); + + struct string encoded = ZI; + encoded.len = bw_num_bytes_written(&msg_bw); + encoded.text = bw_get_written_raw(&msg_bw); + host_queue_write(host, client->channel_id, encoded, 0); } else { /* We do not have the client's ack tick to delta encode from */ ASSERT(false); @@ -1973,7 +2015,9 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) /* TODO: Double buffer */ struct sys_lock lock = sys_mutex_lock_e(&G.local_sim_to_user_mutex); struct sim_snapshot *pub_ss = sim_snapshot_alloc(G.local_sim_to_user_client, step_ss, step_ss->tick); - pub_ss->local_client = client->handle; + if (is_master) { + pub_ss->local_client = client->handle; + } i64 publish_ns = sys_time_ns(); pub_ss->publish_dt_ns = publish_ns - last_publish_ns; pub_ss->publish_time_ns = publish_ns; @@ -1986,9 +2030,24 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) } } } + /* Send host messages */ host_update(host); __profframe("Local sim"); + { + /* Update network usage stats */ + i64 stat_now_ns = sys_time_ns(); + G.client_bytes_read.last_second_end = host->bytes_received; + G.client_bytes_sent.last_second_end = host->bytes_sent; + if (stat_now_ns - G.last_second_reset_ns > NS_FROM_SECONDS(1)) { + G.last_second_reset_ns = stat_now_ns; + G.client_bytes_read.last_second = G.client_bytes_read.last_second_end - G.client_bytes_read.last_second_start; + G.client_bytes_sent.last_second = G.client_bytes_sent.last_second_end - G.client_bytes_sent.last_second_start; + G.client_bytes_read.last_second_start = G.client_bytes_read.last_second_end; + G.client_bytes_sent.last_second_start = G.client_bytes_sent.last_second_end; + } + } + scratch_end(scratch); }