smooth out delta time

isolation_bkp/dynres
Dominik Madarász 2022-09-14 09:07:59 +00:00 committed by GitHub
parent 3436bed2ac
commit 674e8b1628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -20,6 +20,27 @@ size_t pkt_send_librg_update_encode(void *data, int32_t data_length, uint8_t lay
return pkt_pack_msg_size(&pc); return pkt_pack_msg_size(&pc);
} }
#define NUM_SAMPLES 128
static float smooth_time(float time) {
static float time_samples[NUM_SAMPLES] = {};
static int32_t curr_index = 0;
time_samples[curr_index] = time;
if (++curr_index == NUM_SAMPLES)
curr_index = 0;
float average = 0;
for (int32_t i = NUM_SAMPLES; i--; )
average += time_samples[i];
average /= NUM_SAMPLES;
time = zpl_min(time, average * 2);
return time;
}
#undef NUM_SAMPLES
int32_t pkt_send_librg_update_handler(pkt_header *header) { int32_t pkt_send_librg_update_handler(pkt_header *header) {
cw_unpack_context uc = {0}; cw_unpack_context uc = {0};
pkt_unpack_msg(&uc, header, 2); pkt_unpack_msg(&uc, header, 2);
@ -42,7 +63,7 @@ int32_t pkt_send_librg_update_handler(pkt_header *header) {
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)get_cached_time(); float now = (float)get_cached_time();
view->delta_time[layer_id] = now - view->last_update[layer_id]; view->delta_time[layer_id] = smooth_time(now - view->last_update[layer_id]);
view->last_update[layer_id] = now; view->last_update[layer_id] = now;
return state; return state;