Skip to content

Commit

Permalink
realtime render timing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 9, 2024
1 parent ec41c40 commit 6669be0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ledder/Display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions ledder/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Scheduler {

private frameNr: number
private intervals: Set<Interval>
private frameTimeMicros: number
public __frameTimeMicros: number
private defaultFrameTimeMicros: number
private onCleanupCallbacks: any[]
private childScheduler: Scheduler
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class Scheduler {
if (this.childScheduler)
return await this.childScheduler.__step(realtime)
else
return this.frameTimeMicros
return this.__frameTimeMicros

}

Expand Down Expand Up @@ -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

}

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion ledder/animations/Countdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)


})
Expand Down
48 changes: 45 additions & 3 deletions ledder/server/RenderRealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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)

}
Expand Down
2 changes: 1 addition & 1 deletion ledder/server/WsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
3 changes: 2 additions & 1 deletion uploadprod.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/bin/sh


IP=$1

Expand Down

0 comments on commit 6669be0

Please sign in to comment.