@@ -24,12 +24,18 @@ export enum Type {
2424}
2525
2626export interface Features {
27- 'WEBGL_DISJOINT_QUERY_TIMER' ?: boolean ;
27+ // Whether the disjoint_query_timer extension is an available extension.
28+ 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED' ?: boolean ;
29+ // Whether the timer object from the disjoint_query_timer extension gives
30+ // timing information that is reliable.
31+ 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE' ?: boolean ;
32+ // 0: No WebGL, 1: WebGL 1.0, 2: WebGL 2.0.
2833 'WEBGL_VERSION' ?: number ;
2934}
3035
3136export const URL_PROPERTIES : URLProperty [ ] = [
32- { name : 'WEBGL_DISJOINT_QUERY_TIMER' , type : Type . BOOLEAN } ,
37+ { name : 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED' , type : Type . BOOLEAN } ,
38+ { name : 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE' , type : Type . BOOLEAN } ,
3339 { name : 'WEBGL_VERSION' , type : Type . NUMBER }
3440] ;
3541
@@ -38,50 +44,51 @@ export interface URLProperty {
3844 type : Type ;
3945}
4046
41- function isWebGL2Enabled ( ) {
47+ function getWebGLRenderingContext ( webGLVersion : number ) : WebGLRenderingContext {
48+ if ( webGLVersion === 0 ) {
49+ throw new Error ( 'Cannot get WebGL rendering context, WebGL is disabled.' ) ;
50+ }
51+
4252 const tempCanvas = document . createElement ( 'canvas' ) ;
43- const gl = tempCanvas . getContext ( 'webgl2' ) as WebGLRenderingContext ;
53+ if ( webGLVersion === 1 ) {
54+ return ( tempCanvas . getContext ( 'webgl' ) ||
55+ tempCanvas . getContext ( 'experimental-webgl' ) ) as
56+ WebGLRenderingContext ;
57+ }
58+ return tempCanvas . getContext ( 'webgl2' ) as WebGLRenderingContext ;
59+ }
60+
61+ function loseContext ( gl : WebGLRenderingContext ) {
4462 if ( gl != null ) {
4563 const loseContextExtension = gl . getExtension ( 'WEBGL_lose_context' ) ;
4664 if ( loseContextExtension == null ) {
4765 throw new Error (
4866 'Extension WEBGL_lose_context not supported on this browser.' ) ;
4967 }
5068 loseContextExtension . loseContext ( ) ;
51- return true ;
5269 }
53- return false ;
5470}
5571
56- function isWebGL1Enabled ( ) {
57- const tempCanvas = document . createElement ( 'canvas' ) ;
58- const gl =
59- ( tempCanvas . getContext ( 'webgl' ) ||
60- tempCanvas . getContext ( 'experimental-webgl' ) ) as WebGLRenderingContext ;
72+ function isWebGLVersionEnabled ( webGLVersion : 1 | 2 ) {
73+ const gl = getWebGLRenderingContext ( webGLVersion ) ;
6174 if ( gl != null ) {
62- const loseContextExtension = gl . getExtension ( 'WEBGL_lose_context' ) ;
63- if ( loseContextExtension == null ) {
64- throw new Error (
65- 'Extension WEBGL_lose_context not supported on this browser.' ) ;
66- }
67- loseContextExtension . loseContext ( ) ;
75+ loseContext ( gl ) ;
6876 return true ;
6977 }
7078 return false ;
7179}
7280
73- function evaluateFeature < K extends keyof Features > ( feature : K ) : Features [ K ] {
74- if ( feature === 'WEBGL_DISJOINT_QUERY_TIMER' ) {
75- return ! device_util . isMobile ( ) ;
76- } else if ( feature === 'WEBGL_VERSION' ) {
77- if ( isWebGL2Enabled ( ) ) {
78- return 2 ;
79- } else if ( isWebGL1Enabled ( ) ) {
80- return 1 ;
81- }
82- return 0 ;
81+ function isWebGLDisjointQueryTimerEnabled ( webGLVersion : number ) {
82+ const gl = getWebGLRenderingContext ( webGLVersion ) ;
83+
84+ const extensionName = webGLVersion === 1 ? 'EXT_disjoint_timer_query' :
85+ 'EXT_disjoint_timer_query_webgl2' ;
86+ const ext = gl . getExtension ( extensionName ) ;
87+ const isExtEnabled = ext != null ;
88+ if ( gl != null ) {
89+ loseContext ( gl ) ;
8390 }
84- throw new Error ( `Unknown feature ${ feature } .` ) ;
91+ return isExtEnabled ;
8592}
8693
8794export class Environment {
@@ -98,10 +105,33 @@ export class Environment {
98105 return this . features [ feature ] ;
99106 }
100107
101- this . features [ feature ] = evaluateFeature ( feature ) ;
108+ this . features [ feature ] = this . evaluateFeature ( feature ) ;
102109
103110 return this . features [ feature ] ;
104111 }
112+
113+ private evaluateFeature < K extends keyof Features > ( feature : K ) : Features [ K ] {
114+ if ( feature === 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED' ) {
115+ const webGLVersion = this . get ( 'WEBGL_VERSION' ) ;
116+
117+ if ( webGLVersion === 0 ) {
118+ return false ;
119+ }
120+
121+ return isWebGLDisjointQueryTimerEnabled ( webGLVersion ) ;
122+ } else if ( feature === 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE' ) {
123+ return this . get ( 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED' ) &&
124+ ! device_util . isMobile ( ) ;
125+ } else if ( feature === 'WEBGL_VERSION' ) {
126+ if ( isWebGLVersionEnabled ( 2 ) ) {
127+ return 2 ;
128+ } else if ( isWebGLVersionEnabled ( 1 ) ) {
129+ return 1 ;
130+ }
131+ return 0 ;
132+ }
133+ throw new Error ( `Unknown feature ${ feature } .` ) ;
134+ }
105135}
106136
107137// Expects flags from URL in the format ?dljsflags=FLAG1:1,FLAG2:true.
0 commit comments