Skip to content

Commit

Permalink
passing correct delta time into devtools
Browse files Browse the repository at this point in the history
The correct delta time for the frame was not being passed into the devtoolsManager.updateCompleted() call. (This apparently was a known issue since there is a comment that says "FIXME(2022-08-20): Pass the correct FPS for display."
This commit adds a variable to track a timestamp for the last completed frame update and passes the delta between this timestamp and the current frame timestamp into the updateCompleted() call.
The resulting displayed FPS is accurate, but can be choppy due to the intermittent frame delays that are caused by waiting for timeFrameStart to catch up to timeNextUpdate. So, to smooth this out, this commit also includes a change to devToolsManager to display a rolling 10 frame average FPS.
  • Loading branch information
JohnLunsford committed Dec 13, 2023
1 parent 030d27c commit ae4dcf0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 16 additions & 2 deletions devtools/web/src/devtools-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ export class DevtoolsManager {
*/
private _bufferedData = new BufferedRuntimeData();

private _fpsBuffer : number[] = [0,0,0,0,0,0,0,0,0,0];
private _fpsBufferIdx = 0;
//calculate an average FPS for the last 10 frames
private _calcAvgFPS = () => {
let sum = this._fpsBuffer[0];
for (let i = 1; i < 10; i++)
sum += this._fpsBuffer[i];
return sum / 10;
}
private _nextFPSBufferIdx = () => {
(this._fpsBufferIdx == 9) ? this._fpsBufferIdx = 0 : this._fpsBufferIdx++;
return this._fpsBufferIdx;
}

/**
* Notifies the devtools that the web runtime has completed an update.
*/
Expand All @@ -66,12 +80,12 @@ export class DevtoolsManager {
deltaFrame: number
) => {
if (this._enabled) {
const fps = Math.floor(1_000 / deltaFrame);
this._fpsBuffer[this._nextFPSBufferIdx()] = Math.floor(1_000 / deltaFrame);
this._bufferedData.update(runtimeInfo.data);
this._notifyUpdateCompleted(
runtimeInfo.data,
runtimeInfo.wasmBufferByteLen,
fps
this._calcAvgFPS()
);
}
};
Expand Down
7 changes: 5 additions & 2 deletions runtimes/web/src/ui/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ export class App extends LitElement {

// When we should perform the next update
let timeNextUpdate = performance.now();
// Track the timestamp of the last frame
let lastTimeFrameStart = timeNextUpdate;

const onFrame = (timeFrameStart: number) => {
requestAnimationFrame(onFrame);
Expand Down Expand Up @@ -480,8 +482,9 @@ export class App extends LitElement {
runtime.composite();

if (import.meta.env.DEV) {
// FIXME(2022-08-20): Pass the correct FPS for display
devtoolsManager.updateCompleted(runtime, 1000/60);
// FIXED(2022-12-13): Pass the correct FPS for display
devtoolsManager.updateCompleted(runtime, timeFrameStart - lastTimeFrameStart);
lastTimeFrameStart = timeFrameStart;
}
}
}
Expand Down

0 comments on commit ae4dcf0

Please sign in to comment.