Fix stopwatch
This commit is contained in:
parent
5d83a54613
commit
3d48df02e9
1 changed files with 36 additions and 13 deletions
|
|
@ -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) {
|
||||||
this.timerQuery = this.gl.createQuery()!;
|
throw new Error(`Could not start stopwatch in state ${this.state}`);
|
||||||
this.gl.beginQuery(this.timerExtension.TIME_ELAPSED_EXT, this.timerQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.timerQuery = this.gl.createQuery()!;
|
||||||
|
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) {
|
||||||
this.gl.endQuery(this.timerExtension.TIME_ELAPSED_EXT);
|
throw new Error(`Could not stop stopwatch in state ${this.state}`);
|
||||||
this.isReady = false;
|
}
|
||||||
|
|
||||||
|
this.gl.endQuery(this.timerExtension.TIME_ELAPSED_EXT);
|
||||||
|
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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue