Fix stopwatch

This commit is contained in:
schmelczerandras 2020-09-18 15:26:36 +02:00
parent 5d83a54613
commit 3d48df02e9

View file

@ -1,29 +1,44 @@
import { Insights } from '../../rendering/insights';
import { enableExtension } from './enable-extension'; import { enableExtension } from './enable-extension';
// https://www.khronos.org/registry/webgl/extensions/EXT_disjoint_timer_query_webgl2/ enum StopwatchState {
ready = 'ready',
running = 'running',
waitingForResults = 'waitingForResults',
}
export class WebGlStopwatch { export class WebGlStopwatch {
private state = StopwatchState.ready;
private resultsInNanoSeconds = 0;
private timerExtension: any; private timerExtension: any;
private timerQuery?: WebGLQuery; private timerQuery?: WebGLQuery;
private isReady = true;
private resultsInNanoSeconds?: number;
constructor(private gl: WebGL2RenderingContext) { constructor(private gl: WebGL2RenderingContext) {
this.timerExtension = enableExtension(gl, 'EXT_disjoint_timer_query_webgl2'); this.timerExtension = enableExtension(gl, 'EXT_disjoint_timer_query_webgl2');
} }
public start() { public start() {
if (this.isReady) { if (this.state != StopwatchState.ready) {
throw new Error(`Could not start stopwatch in state ${this.state}`);
}
this.timerQuery = this.gl.createQuery()!; this.timerQuery = this.gl.createQuery()!;
this.gl.beginQuery(this.timerExtension.TIME_ELAPSED_EXT, this.timerQuery); this.gl.beginQuery(this.timerExtension.TIME_ELAPSED_EXT, this.timerQuery);
} this.state = StopwatchState.running;
} }
public stop() { public stop() {
if (this.isReady) { if (this.state != StopwatchState.running) {
throw new Error(`Could not stop stopwatch in state ${this.state}`);
}
this.gl.endQuery(this.timerExtension.TIME_ELAPSED_EXT); this.gl.endQuery(this.timerExtension.TIME_ELAPSED_EXT);
this.isReady = false; this.state = StopwatchState.waitingForResults;
}
public tryGetResults(): boolean {
if (this.state != StopwatchState.waitingForResults) {
throw new Error(`Could not check for results in state ${this.state}`);
} }
const available = this.gl.getQueryParameter( const available = this.gl.getQueryParameter(
@ -38,10 +53,18 @@ export class WebGlStopwatch {
this.gl.QUERY_RESULT this.gl.QUERY_RESULT
); );
Insights.setValue('GPU draw time', `${this.resultsInMilliSeconds.toFixed(2)} ms`); this.state = StopwatchState.ready;
return true;
this.isReady = true;
} }
return false;
}
public get isReady(): boolean {
return this.state == StopwatchState.ready;
}
public get isRunning(): boolean {
return this.state == StopwatchState.running;
} }
public get resultsInMilliSeconds(): number { public get resultsInMilliSeconds(): number {