@@ -262,6 +262,14 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
262
262
return null
263
263
}
264
264
265
+ _getActiveFramebuffer ( target ) {
266
+ if ( target === this . READ_FRAMEBUFFER ) {
267
+ return this . _activeFramebuffers . read
268
+ } else {
269
+ return this . _activeFramebuffers . draw
270
+ }
271
+ }
272
+
265
273
_getAttachments ( ) {
266
274
return this . _extensions . webgl_draw_buffers ? this . _extensions . webgl_draw_buffers . _ALL_ATTACHMENTS : DEFAULT_ATTACHMENTS
267
275
}
@@ -305,7 +313,7 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
305
313
}
306
314
307
315
_resizeDrawingBuffer ( width , height ) {
308
- const prevFramebuffer = this . _activeFramebuffer
316
+ const prevFramebuffer = this . _activeFramebuffers . draw
309
317
const prevTexture = this . _getActiveTexture ( this . TEXTURE_2D )
310
318
const prevRenderbuffer = this . _activeRenderbuffer
311
319
@@ -402,24 +410,6 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
402
410
}
403
411
}
404
412
405
- _tryDetachFramebuffer ( framebuffer , renderbuffer ) {
406
- // FIXME: Does the texture get unbound from *all* framebuffers, or just the
407
- // active FBO?
408
- if ( framebuffer && framebuffer . _linked ( renderbuffer ) ) {
409
- const attachments = this . _getAttachments ( )
410
- const framebufferAttachments = Object . keys ( framebuffer . _attachments )
411
- for ( let i = 0 ; i < framebufferAttachments . length ; ++ i ) {
412
- if ( framebuffer . _attachments [ attachments [ i ] ] === renderbuffer ) {
413
- this . framebufferTexture2D (
414
- this . FRAMEBUFFER ,
415
- attachments [ i ] | 0 ,
416
- this . TEXTURE_2D ,
417
- null )
418
- }
419
- }
420
- }
421
- }
422
-
423
413
_validBlendFunc ( factor ) {
424
414
return factor === this . ZERO ||
425
415
factor === this . ONE ||
@@ -591,18 +581,14 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
591
581
} else {
592
582
return
593
583
}
594
- const activeFramebuffer = this . _activeFramebuffer
595
- if ( activeFramebuffer !== framebuffer ) {
596
- if ( activeFramebuffer ) {
597
- activeFramebuffer . _refCount -= 1
598
- activeFramebuffer . _checkDelete ( )
599
- }
600
- if ( framebuffer ) {
601
- framebuffer . _refCount += 1
602
- }
603
- }
604
- if ( error === 0 ) {
605
- this . _activeFramebuffer = framebuffer
584
+
585
+ if ( target === this . FRAMEBUFFER ) {
586
+ this . _activeFramebuffers . draw = framebuffer
587
+ this . _activeFramebuffers . read = framebuffer
588
+ } else if ( target === this . READ_FRAMEBUFFER ) {
589
+ this . _activeFramebuffers . read = framebuffer
590
+ } else if ( target === this . DRAW_FRAMEBUFFER ) {
591
+ this . _activeFramebuffers . draw = framebuffer
606
592
}
607
593
}
608
594
@@ -1239,8 +1225,14 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1239
1225
return
1240
1226
}
1241
1227
1242
- if ( this . _activeFramebuffer === framebuffer ) {
1228
+ if ( this . _activeFramebuffers . draw === framebuffer && this . _activeFramebuffers . read === framebuffer ) {
1243
1229
this . bindFramebuffer ( this . FRAMEBUFFER , null )
1230
+ } else if ( this . _isWebGL2 ( ) ) {
1231
+ if ( this . _activeFramebuffers . read === framebuffer ) {
1232
+ this . bindFramebuffer ( this . READ_FRAMEBUFFER , null )
1233
+ } else if ( this . _activeFramebuffers . draw === framebuffer ) {
1234
+ this . bindFramebuffer ( this . DRAW_FRAMEBUFFER , null )
1235
+ }
1244
1236
}
1245
1237
1246
1238
framebuffer . _pendingDelete = true
@@ -1273,10 +1265,6 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1273
1265
this . bindRenderbuffer ( this . RENDERBUFFER , null )
1274
1266
}
1275
1267
1276
- const activeFramebuffer = this . _activeFramebuffer
1277
-
1278
- this . _tryDetachFramebuffer ( activeFramebuffer , renderbuffer )
1279
-
1280
1268
renderbuffer . _pendingDelete = true
1281
1269
renderbuffer . _checkDelete ( )
1282
1270
}
@@ -1316,28 +1304,6 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1316
1304
}
1317
1305
this . activeTexture ( this . TEXTURE0 + curActive )
1318
1306
1319
- // FIXME: Does the texture get unbound from *all* framebuffers, or just the
1320
- // active FBO?
1321
- const ctx = this
1322
- const activeFramebuffer = this . _activeFramebuffer
1323
- function tryDetach ( framebuffer ) {
1324
- if ( framebuffer && framebuffer . _linked ( texture ) ) {
1325
- const attachments = ctx . _getAttachments ( )
1326
- for ( let i = 0 ; i < attachments . length ; ++ i ) {
1327
- const attachment = attachments [ i ]
1328
- if ( framebuffer . _attachments [ attachment ] === texture ) {
1329
- ctx . framebufferTexture2D (
1330
- this . FRAMEBUFFER ,
1331
- attachment ,
1332
- this . TEXTURE_2D ,
1333
- null )
1334
- }
1335
- }
1336
- }
1337
- }
1338
-
1339
- tryDetach ( activeFramebuffer )
1340
-
1341
1307
// Mark texture for deletion
1342
1308
texture . _pendingDelete = true
1343
1309
texture . _checkDelete ( )
@@ -1493,14 +1459,8 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1493
1459
throw new TypeError ( 'framebufferRenderbuffer(GLenum, GLenum, GLenum, WebGLRenderbuffer)' )
1494
1460
}
1495
1461
1496
- if ( ! this . _validFramebufferAttachment ( attachment ) ||
1497
- renderbufferTarget !== this . RENDERBUFFER ) {
1498
- this . setError ( this . INVALID_ENUM )
1499
- return
1500
- }
1501
-
1502
- const framebuffer = this . _activeFramebuffer
1503
- if ( ! framebuffer ) {
1462
+ // Since we emulate the default framebuffer, we can't rely on ANGLE's validation.
1463
+ if ( ! this . _getActiveFramebuffer ( target ) ) {
1504
1464
this . setError ( this . INVALID_OPERATION )
1505
1465
return
1506
1466
}
@@ -1526,46 +1486,41 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1526
1486
throw new TypeError ( 'framebufferTexture2D(GLenum, GLenum, GLenum, WebGLTexture, GLint)' )
1527
1487
}
1528
1488
1529
- // Check parameters are ok
1530
- if ( ! this . _validFramebufferAttachment ( attachment ) ) {
1531
- this . setError ( this . INVALID_ENUM )
1489
+ // Check object ownership
1490
+ if ( texture && ! this . _checkWrapper ( texture , WebGLTexture ) ) {
1532
1491
return
1533
1492
}
1534
1493
1535
- if ( level !== 0 ) {
1536
- this . setError ( this . INVALID_VALUE )
1494
+ // Since we emulate the default framebuffer, we can't rely on ANGLE's validation.
1495
+ if ( ! this . _getActiveFramebuffer ( target ) ) {
1496
+ this . setError ( this . INVALID_OPERATION )
1537
1497
return
1538
1498
}
1539
1499
1540
- // Check object ownership
1541
- if ( texture && ! this . _checkWrapper ( texture , WebGLTexture ) ) {
1542
- return
1500
+ super . framebufferTexture2D ( target , attachment , textarget , texture ?. _ ?? null , level )
1501
+ }
1502
+
1503
+ framebufferTextureLayer ( target , attachment , texture , level , layer ) {
1504
+ target |= 0
1505
+ attachment |= 0
1506
+ level |= 0
1507
+ layer |= 0
1508
+ if ( ! checkObject ( texture ) ) {
1509
+ throw new TypeError ( 'framebufferTextureLayer(GLenum, GLenum, WebGLTexture, GLint, GLint)' )
1543
1510
}
1544
1511
1545
- // Check texture target is ok
1546
- if ( textarget === this . TEXTURE_2D ) {
1547
- if ( texture && texture . _binding !== this . TEXTURE_2D ) {
1548
- this . setError ( this . INVALID_OPERATION )
1549
- return
1550
- }
1551
- } else if ( this . _validCubeTarget ( textarget ) ) {
1552
- if ( texture && texture . _binding !== this . TEXTURE_CUBE_MAP ) {
1553
- this . setError ( this . INVALID_OPERATION )
1554
- return
1555
- }
1556
- } else {
1557
- this . setError ( this . INVALID_ENUM )
1512
+ // Check object ownership
1513
+ if ( texture && ! this . _checkWrapper ( texture , WebGLTexture ) ) {
1558
1514
return
1559
1515
}
1560
1516
1561
- // Check a framebuffer is actually bound
1562
- const framebuffer = this . _activeFramebuffer
1563
- if ( ! framebuffer ) {
1517
+ // Since we emulate the default framebuffer, we can't rely on ANGLE's validation.
1518
+ if ( ! this . _getActiveFramebuffer ( target ) ) {
1564
1519
this . setError ( this . INVALID_OPERATION )
1565
1520
return
1566
1521
}
1567
1522
1568
- super . framebufferTexture2D ( target , attachment , textarget , texture ?. _ ?? null , level )
1523
+ super . framebufferTextureLayer ( target , attachment , texture ?. _ ?? null , level , layer )
1569
1524
}
1570
1525
1571
1526
frontFace ( mode ) {
@@ -1651,7 +1606,9 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1651
1606
case this . CURRENT_PROGRAM :
1652
1607
return this . _activeProgram
1653
1608
case this . FRAMEBUFFER_BINDING :
1654
- return this . _activeFramebuffer
1609
+ return this . _activeFramebuffers . draw
1610
+ case this . READ_FRAMEBUFFER_BINDING :
1611
+ return this . _activeFramebuffers . read
1655
1612
case this . RENDERBUFFER_BINDING :
1656
1613
return this . _activeRenderbuffer
1657
1614
case this . TEXTURE_BINDING_2D :
@@ -1731,8 +1688,8 @@ class WebGLRenderingContextHelper extends NativeWebGLRenderingContext {
1731
1688
attachment |= 0
1732
1689
pname |= 0
1733
1690
1734
- // Note: there's an ANGLE bug where it doesn 't check for the default framebuffer in WebGL compat .
1735
- if ( ! this . _activeFramebuffer ) {
1691
+ // Since we emulate the default framebuffer, we can 't rely on ANGLE's validation .
1692
+ if ( ! this . _getActiveFramebuffer ( target ) ) {
1736
1693
this . setError ( this . INVALID_OPERATION )
1737
1694
return null
1738
1695
}
0 commit comments