From 7d50170a6050b022a7024a7040702cdf6012fc3e Mon Sep 17 00:00:00 2001 From: FMS-Cat Date: Mon, 22 Mar 2021 02:39:02 +0900 Subject: [PATCH] performance: improve GPUTimer performance? --- src/heck/GPUTimer.ts | 19 ++++++++++++++----- src/scene.ts | 5 +++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/heck/GPUTimer.ts b/src/heck/GPUTimer.ts index 78a784c..1671ab3 100644 --- a/src/heck/GPUTimer.ts +++ b/src/heck/GPUTimer.ts @@ -6,6 +6,8 @@ export class GPUTimer { public stack: Promise[]; public ext: any; + private __loopTasks: Set<() => void>; + public constructor() { const queries = new Array( 1024 ).fill( 1 ).map( () => gl.createQuery()! ); this.queries = new Pool( queries ); @@ -13,6 +15,14 @@ export class GPUTimer { this.stack = []; this.ext = glCat.getExtension( 'EXT_disjoint_timer_query_webgl2' ); + + this.__loopTasks = new Set(); + } + + public update(): void { + for ( const task of this.__loopTasks ) { + task(); + } } public async measure( func: () => void ): Promise { @@ -49,17 +59,16 @@ export class GPUTimer { public check( query: WebGLQuery ): Promise { return new Promise( ( resolve ) => { - const loop = () => { + const task = () => { const isAvailable = gl.getQueryParameter( query, gl.QUERY_RESULT_AVAILABLE ); if ( isAvailable ) { + this.__loopTasks.delete( task ); resolve( gl.getQueryParameter( query, gl.QUERY_RESULT ) * 0.001 * 0.001 ); - } else { - setTimeout( loop, 1 ); } - } + }; - loop(); + this.__loopTasks.add( task ); } ); } } diff --git a/src/scene.ts b/src/scene.ts index 3716ca8..bdc160f 100644 --- a/src/scene.ts +++ b/src/scene.ts @@ -20,6 +20,7 @@ import { music } from './globals/music'; import { randomTexture } from './globals/randomTexture'; import { BufferRenderTarget } from './heck/BufferRenderTarget'; import { CanvasRenderTarget } from './heck/CanvasRenderTarget'; +import { Component } from './heck/components/Component'; import { Lambda } from './heck/components/Lambda'; import { Dog } from './heck/Dog'; import { Entity } from './heck/Entity'; @@ -39,6 +40,10 @@ dog.root.components.push( new Lambda( { totalFrame ++; isInitialFrame = false; + if ( process.env.DEV ) { + Component.gpuTimer!.update(); + } + randomTexture.update(); automaton.update( music.time ); },