From 6669be0a54b0a3c2787270dd9ca64930440af9ca Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 9 Sep 2024 16:18:28 +0200 Subject: [PATCH] realtime render timing improvements --- ledder/Display.ts | 2 +- ledder/Scheduler.ts | 10 +++---- ledder/animations/Countdown.ts | 3 ++- ledder/server/RenderRealtime.ts | 48 ++++++++++++++++++++++++++++++--- ledder/server/WsContext.ts | 2 +- uploadprod.sh | 3 ++- 6 files changed, 56 insertions(+), 12 deletions(-) diff --git a/ledder/Display.ts b/ledder/Display.ts index 5efac5bc..595150dd 100644 --- a/ledder/Display.ts +++ b/ledder/Display.ts @@ -14,7 +14,7 @@ export default abstract class Display { * information for the renderer: */ //maximum fps this driver supports - minFrameTimeMicros=~~(1000000/60) + minFrameTimeMicros=~~(1000000/120) //default fps defaultFrameTimeMicros=~~(1000000/60) diff --git a/ledder/Scheduler.ts b/ledder/Scheduler.ts index 516b4a9c..632275c9 100644 --- a/ledder/Scheduler.ts +++ b/ledder/Scheduler.ts @@ -15,7 +15,7 @@ export default class Scheduler { private frameNr: number private intervals: Set - private frameTimeMicros: number + public __frameTimeMicros: number private defaultFrameTimeMicros: number private onCleanupCallbacks: any[] private childScheduler: Scheduler @@ -108,7 +108,7 @@ export default class Scheduler { if (this.childScheduler) return await this.childScheduler.__step(realtime) else - return this.frameTimeMicros + return this.__frameTimeMicros } @@ -147,9 +147,9 @@ export default class Scheduler { */ public setFrameTimeuS(frameTimeMicros) { if (frameTimeMicros < this.defaultFrameTimeMicros) - this.frameTimeMicros = this.defaultFrameTimeMicros + this.__frameTimeMicros = this.defaultFrameTimeMicros else - this.frameTimeMicros = ~~frameTimeMicros + this.__frameTimeMicros = ~~frameTimeMicros } @@ -250,7 +250,7 @@ export default class Scheduler { this.childScheduler = new Scheduler() this.childScheduler.__setDefaultFrameTime(this.defaultFrameTimeMicros) - this.childScheduler.setFrameTimeuS(this.frameTimeMicros) + this.childScheduler.setFrameTimeuS(this.__frameTimeMicros) return (this.childScheduler) } diff --git a/ledder/animations/Countdown.ts b/ledder/animations/Countdown.ts index f42b3f04..b0ec4fbb 100644 --- a/ledder/animations/Countdown.ts +++ b/ledder/animations/Countdown.ts @@ -107,9 +107,10 @@ export default class Countdown extends Animator { } + + countBox.clear() countBox.add(new DrawText(controlX.value, controlY.value, font, text, color)) - //countBox.center(box) }) diff --git a/ledder/server/RenderRealtime.ts b/ledder/server/RenderRealtime.ts index 6ca8e8e9..dd62a8d0 100644 --- a/ledder/server/RenderRealtime.ts +++ b/ledder/server/RenderRealtime.ts @@ -11,12 +11,27 @@ export class RenderRealtime extends Render { private keepRendering: boolean + //statistics + private lastStatUpdate:number; + private lateFrames: number; + private idleMS: number; + //frames that are TOO early for the hardware's max update speed: + private droppedFrames:number; + + start() { if (!this.keepRendering) { this.keepRendering = true this.nextTimeMicros = Date.now() * 1000 this.lastFrameMicros = 0 + this.lastStatUpdate=Date.now() + + this.lateFrames=0 + this.idleMS =0 + this.droppedFrames=0; + + this.renderInterval() } } @@ -27,18 +42,45 @@ export class RenderRealtime extends Render { if (!this.keepRendering) return - this.nextTimeMicros += await this.scheduler.__step(true) - if (this.nextTimeMicros - this.lastFrameMicros >= this.display.minFrameTimeMicros) { + let nowUS=Date.now()*1000 + + this.nextTimeMicros+= await this.scheduler.__step(true) + + //max 10 frames difference + if (Math.abs(this.nextTimeMicros-nowUS)>this.scheduler.__frameTimeMicros*10) { + //reset + this.nextTimeMicros = nowUS+this.scheduler.__frameTimeMicros; + } + + if ( nowUS-this.lastFrameMicros >= this.display.minFrameTimeMicros) { try { this.display.render(this.box) } catch (e) { console.error("Exception while rendering:", e) } this.display.frame(this.nextTimeMicros) - this.lastFrameMicros = this.nextTimeMicros + this.lastFrameMicros = nowUS + } + else + { + this.droppedFrames++; + } + + if (nowUS-this.lastStatUpdate>1000000) + { + + console.log(`RenderRealtime: ${this.lateFrames} late. ${this.droppedFrames} dropped. ${ Math.round( ( 1000-this.idleMS)/10) }% busy.`) + this.lastStatUpdate=nowUS + this.droppedFrames=0; + this.lateFrames=0; + this.idleMS=0 } const intervalmS = (this.nextTimeMicros / 1000) - Date.now() + this.idleMS=this.idleMS+intervalmS + if (intervalmS<=0) + this.lateFrames++ + // console.log(intervalmS) setTimeout(() => this.renderInterval(), intervalmS) } diff --git a/ledder/server/WsContext.ts b/ledder/server/WsContext.ts index 987a4e22..3bae17cb 100644 --- a/ledder/server/WsContext.ts +++ b/ledder/server/WsContext.ts @@ -60,7 +60,7 @@ export class WsContext { this.statsInterval = setInterval(() => { - console.log(`Stats ${this.id}: ${this.renderLoop.getStats()}`) + console.log(`Preview stats ${this.id}: ${this.renderLoop.getStats()}`) }, 3000) diff --git a/uploadprod.sh b/uploadprod.sh index 56301508..c1fc7532 100755 --- a/uploadprod.sh +++ b/uploadprod.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/bin/sh + IP=$1