@@ -214,30 +214,40 @@ define([
214
214
}
215
215
216
216
// TODO : should make useWebGL2 a context flag instead?
217
- var useWebGL2 = true ;
217
+ var defaultToWebGL2 = true ;
218
218
var webGL2Supported = ( typeof WebGL2RenderingContext !== 'undefined' ) ;
219
- if ( useWebGL2 && webGL2Supported ) {
220
- this . _originalGLContext = canvas . getContext ( 'webgl2' , webglOptions ) || canvas . getContext ( 'experimental-webgl2' , webglOptions ) || undefined ;
219
+ var webGL2 = false ;
220
+ var glContext ;
221
+
222
+ if ( defaultToWebGL2 && webGL2Supported ) {
223
+ glContext = canvas . getContext ( 'webgl2' , webglOptions ) || canvas . getContext ( 'experimental-webgl2' , webglOptions ) || undefined ;
224
+ if ( defined ( glContext ) ) {
225
+ webGL2 = true ;
226
+ }
221
227
}
222
- if ( ! defined ( this . _originalGLContext ) ) {
223
- this . _originalGLContext = canvas . getContext ( 'webgl' , webglOptions ) || canvas . getContext ( 'experimental-webgl' , webglOptions ) || undefined ;
228
+ if ( ! defined ( glContext ) ) {
229
+ glContext = canvas . getContext ( 'webgl' , webglOptions ) || canvas . getContext ( 'experimental-webgl' , webglOptions ) || undefined ;
224
230
}
225
- if ( ! defined ( this . _originalGLContext ) ) {
231
+ if ( ! defined ( glContext ) ) {
226
232
throw new RuntimeError ( 'The browser supports WebGL, but initialization failed.' ) ;
227
233
}
228
234
235
+ this . _originalGLContext = glContext ;
236
+ this . _webgl2 = webGL2 ;
229
237
this . _id = createGuid ( ) ;
230
238
231
239
// Validation and logging disabled by default for speed.
232
- this . validateFramebuffer = false ;
240
+ this . validateFramebuffer = true ;
233
241
this . validateShaderProgram = false ;
234
242
this . logShaderCompilation = false ;
235
243
236
244
this . _throwOnWebGLError = false ;
245
+ // TODO : fix this later
246
+ var gl = this . _gl = wrapGL ( this . _originalGLContext , throwOnError ) ;
237
247
238
248
this . _shaderCache = new ShaderCache ( this ) ;
239
249
240
- var gl = this . _gl = this . _originalGLContext ;
250
+ // var gl = this._gl = this._originalGLContext;
241
251
242
252
this . _redBits = gl . getParameter ( gl . RED_BITS ) ;
243
253
this . _greenBits = gl . getParameter ( gl . GREEN_BITS ) ;
@@ -291,8 +301,18 @@ define([
291
301
this . _instancedArrays = getExtension ( gl , [ 'ANGLE_instanced_arrays' ] ) ;
292
302
293
303
this . _drawBuffers = getExtension ( gl , [ 'WEBGL_draw_buffers' ] ) ;
294
- ContextLimits . _maximumDrawBuffers = defined ( this . _drawBuffers ) ? gl . getParameter ( this . _drawBuffers . MAX_DRAW_BUFFERS_WEBGL ) : 1 ;
295
- ContextLimits . _maximumColorAttachments = defined ( this . _drawBuffers ) ? gl . getParameter ( this . _drawBuffers . MAX_COLOR_ATTACHMENTS_WEBGL ) : 1 ; // min when supported: 4
304
+ if ( this . drawBuffers ) {
305
+ if ( this . _webgl2 ) {
306
+ ContextLimits . _maximumDrawBuffers = gl . getParameter ( WebGLConstants . MAX_DRAW_BUFFERS ) ;
307
+ ContextLimits . _maximumColorAttachments = gl . getParameter ( WebGLConstants . MAX_COLOR_ATTACHMENTS ) ;
308
+ } else {
309
+ ContextLimits . _maximumDrawBuffers = gl . getParameter ( this . _drawBuffers . MAX_DRAW_BUFFERS_WEBGL ) ;
310
+ ContextLimits . _maximumColorAttachments = gl . getParameter ( this . _drawBuffers . MAX_COLOR_ATTACHMENTS_WEBGL ) ; // min when supported: 4
311
+ }
312
+ } else {
313
+ ContextLimits . _maximumDrawBuffers = 1 ;
314
+ ContextLimits . _maximumColorAttachments = 1 ;
315
+ }
296
316
297
317
this . _debugShaders = getExtension ( gl , [ 'WEBGL_debug_shaders' ] ) ;
298
318
@@ -481,7 +501,7 @@ define([
481
501
*/
482
502
standardDerivatives : {
483
503
get : function ( ) {
484
- return ! ! this . _standardDerivatives ;
504
+ return ! ! this . _standardDerivatives || this . _webgl2 ;
485
505
}
486
506
} ,
487
507
@@ -495,7 +515,7 @@ define([
495
515
*/
496
516
elementIndexUint : {
497
517
get : function ( ) {
498
- return ! ! this . _elementIndexUint ;
518
+ return ! ! this . _elementIndexUint || this . _webgl2 ;
499
519
}
500
520
} ,
501
521
@@ -508,7 +528,7 @@ define([
508
528
*/
509
529
depthTexture : {
510
530
get : function ( ) {
511
- return ! ! this . _depthTexture ;
531
+ return ! ! this . _depthTexture || this . _webgl2 ;
512
532
}
513
533
} ,
514
534
@@ -521,7 +541,7 @@ define([
521
541
*/
522
542
floatingPointTexture : {
523
543
get : function ( ) {
524
- return ! ! this . _textureFloat ;
544
+ return ! ! this . _textureFloat || this . _webgl2 ;
525
545
}
526
546
} ,
527
547
@@ -541,7 +561,46 @@ define([
541
561
*/
542
562
vertexArrayObject : {
543
563
get : function ( ) {
544
- return ! ! this . _vertexArrayObject ;
564
+ return ! ! this . _vertexArrayObject || this . _webgl2 ;
565
+ }
566
+ } ,
567
+
568
+ createVertexArray : {
569
+ get : function ( ) {
570
+ var that = this ;
571
+ return function ( ) {
572
+ if ( that . _webgl2 ) {
573
+ that . _gl . createVertexArray ( ) ;
574
+ } else {
575
+ that . _vertexArrayObject . createVertexArrayOES ( ) ;
576
+ }
577
+ } ;
578
+ }
579
+ } ,
580
+
581
+ bindVertexArray : {
582
+ get : function ( ) {
583
+ var that = this ;
584
+ return function ( vertexArray ) {
585
+ if ( that . _webgl2 ) {
586
+ that . _gl . bindVertexArray ( vertexArray ) ;
587
+ } else {
588
+ that . _vertexArrayObject . bindVertexArrayOES ( vertexArray ) ;
589
+ }
590
+ } ;
591
+ }
592
+ } ,
593
+
594
+ deleteVertexArray : {
595
+ get : function ( ) {
596
+ var that = this ;
597
+ return function ( vertexArray ) {
598
+ if ( that . _webgl2 ) {
599
+ that . _gl . deleteVertexArray ( vertexArray ) ;
600
+ } else {
601
+ that . _vertexArrayObject . deleteVertexArrayOES ( vertexArray ) ;
602
+ }
603
+ } ;
545
604
}
546
605
} ,
547
606
@@ -556,7 +615,7 @@ define([
556
615
*/
557
616
fragmentDepth : {
558
617
get : function ( ) {
559
- return ! ! this . _fragDepth ;
618
+ return ! ! this . _fragDepth || this . _webgl2 ;
560
619
}
561
620
} ,
562
621
@@ -569,7 +628,46 @@ define([
569
628
*/
570
629
instancedArrays : {
571
630
get : function ( ) {
572
- return ! ! this . _instancedArrays ;
631
+ return ! ! this . _instancedArrays || this . _webgl2 ;
632
+ }
633
+ } ,
634
+
635
+ drawElementsInstanced : {
636
+ get : function ( ) {
637
+ var that = this ;
638
+ return function ( mode , count , type , offset , instanceCount ) {
639
+ if ( that . _webgl2 ) {
640
+ that . _gl . drawElementsInstanced ( mode , count , type , offset , instanceCount ) ;
641
+ } else {
642
+ that . _instancedArrays . drawElementsInstancedANGLE ( mode , count , type , offset , instanceCount ) ;
643
+ }
644
+ } ;
645
+ }
646
+ } ,
647
+
648
+ drawArraysInstanced : {
649
+ get : function ( ) {
650
+ var that = this ;
651
+ return function ( mode , first , count , instanceCount ) {
652
+ if ( that . _webgl2 ) {
653
+ that . _gl . drawArraysInstanced ( mode , first , count , instanceCount ) ;
654
+ } else {
655
+ that . _instancedArrays . drawArraysInstancedANGLE ( mode , first , count , instanceCount ) ;
656
+ }
657
+ } ;
658
+ }
659
+ } ,
660
+
661
+ vertexAttribDivisor : {
662
+ get : function ( ) {
663
+ var that = this ;
664
+ return function ( index , divisor ) {
665
+ if ( that . _webgl2 ) {
666
+ that . _gl . vertexAttribDivisor ( index , divisor ) ;
667
+ } else {
668
+ that . _instancedArrays . vertexAttribDivisorANGLE ( index , divisor ) ;
669
+ }
670
+ } ;
573
671
}
574
672
} ,
575
673
@@ -585,7 +683,20 @@ define([
585
683
*/
586
684
drawBuffers : {
587
685
get : function ( ) {
588
- return ! ! this . _drawBuffers ;
686
+ return ! ! this . _drawBuffers || this . _webgl2 ;
687
+ }
688
+ } ,
689
+
690
+ drawBuffersGL : {
691
+ get : function ( ) {
692
+ var that = this ;
693
+ return function ( buffers ) {
694
+ if ( that . _webgl2 ) {
695
+ that . _gl . drawBuffers ( buffers ) ;
696
+ } else {
697
+ that . _drawBuffers . drawBuffersWEBGL ( buffers ) ;
698
+ }
699
+ } ;
589
700
}
590
701
} ,
591
702
@@ -759,7 +870,7 @@ define([
759
870
}
760
871
761
872
if ( context . drawBuffers ) {
762
- context . _drawBuffers . drawBuffersWEBGL ( buffers ) ;
873
+ context . drawBuffersGL ( buffers ) ;
763
874
}
764
875
}
765
876
}
@@ -877,14 +988,14 @@ define([
877
988
if ( instanceCount === 0 ) {
878
989
context . _gl . drawElements ( primitiveType , count , indexBuffer . indexDatatype , offset ) ;
879
990
} else {
880
- context . _instancedArrays . drawElementsInstancedANGLE ( primitiveType , count , indexBuffer . indexDatatype , offset , instanceCount ) ;
991
+ context . drawElementsInstanced ( primitiveType , count , indexBuffer . indexDatatype , offset , instanceCount ) ;
881
992
}
882
993
} else {
883
994
count = defaultValue ( count , va . numberOfVertices ) ;
884
995
if ( instanceCount === 0 ) {
885
996
context . _gl . drawArrays ( primitiveType , offset , count ) ;
886
997
} else {
887
- context . _instancedArrays . drawArraysInstancedANGLE ( primitiveType , offset , count , instanceCount ) ;
998
+ context . drawArraysInstanced ( primitiveType , offset , count , instanceCount ) ;
888
999
}
889
1000
}
890
1001
@@ -919,7 +1030,7 @@ define([
919
1030
920
1031
var buffers = scratchBackBufferArray ;
921
1032
if ( this . drawBuffers ) {
922
- this . _drawBuffers . drawBuffersWEBGL ( scratchBackBufferArray ) ;
1033
+ this . drawBuffersGL ( buffers ) ;
923
1034
}
924
1035
925
1036
var length = this . _maxFrameTextureUnitIndex ;
0 commit comments