@@ -1459,25 +1459,44 @@ public void updateFrameBufferAttachment(FrameBuffer fb, RenderBuffer rb) {
14591459 rb .getId ());
14601460 }
14611461 }
1462+
1463+ private void bindFrameBuffer (FrameBuffer fb ) {
1464+ if (fb == null ) {
1465+ if (context .boundFBO != 0 ) {
1466+ glfbo .glBindFramebufferEXT (GLFbo .GL_FRAMEBUFFER_EXT , 0 );
1467+ statistics .onFrameBufferUse (null , true );
1468+ context .boundFBO = 0 ;
1469+ context .boundFB = null ;
1470+ }
1471+ } else {
1472+ assert fb .getId () != -1 && fb .getId () != 0 ;
1473+ if (context .boundFBO != fb .getId ()) {
1474+ glfbo .glBindFramebufferEXT (GLFbo .GL_FRAMEBUFFER_EXT , fb .getId ());
1475+ context .boundFBO = fb .getId ();
1476+ context .boundFB = fb ;
1477+ statistics .onFrameBufferUse (fb , true );
1478+ } else {
1479+ statistics .onFrameBufferUse (fb , false );
1480+ }
1481+ }
1482+ }
14621483
14631484 public void updateFrameBuffer (FrameBuffer fb ) {
1485+ if (fb .getNumColorBuffers () == 0 && fb .getDepthBuffer () == null ) {
1486+ throw new IllegalArgumentException ("The framebuffer: " + fb
1487+ + "\n Doesn't have any color/depth buffers" );
1488+ }
1489+
14641490 int id = fb .getId ();
14651491 if (id == -1 ) {
1466- // create FBO
14671492 glfbo .glGenFramebuffersEXT (intBuf1 );
14681493 id = intBuf1 .get (0 );
14691494 fb .setId (id );
14701495 objManager .registerObject (fb );
1471-
14721496 statistics .onNewFrameBuffer ();
14731497 }
14741498
1475- if (context .boundFBO != id ) {
1476- glfbo .glBindFramebufferEXT (GLFbo .GL_FRAMEBUFFER_EXT , id );
1477- // binding an FBO automatically sets draw buf to GL_COLOR_ATTACHMENT0
1478- context .boundDrawBuf = 0 ;
1479- context .boundFBO = id ;
1480- }
1499+ bindFrameBuffer (fb );
14811500
14821501 FrameBuffer .RenderBuffer depthBuf = fb .getDepthBuffer ();
14831502 if (depthBuf != null ) {
@@ -1488,7 +1507,8 @@ public void updateFrameBuffer(FrameBuffer fb) {
14881507 FrameBuffer .RenderBuffer colorBuf = fb .getColorBuffer (i );
14891508 updateFrameBufferAttachment (fb , colorBuf );
14901509 }
1491-
1510+
1511+ setReadDrawBuffers (fb );
14921512 checkFrameBufferError ();
14931513
14941514 fb .clearUpdateNeeded ();
@@ -1516,93 +1536,45 @@ public Vector2f[] getFrameBufferSamplePositions(FrameBuffer fb) {
15161536 }
15171537
15181538 public void setMainFrameBufferOverride (FrameBuffer fb ) {
1539+ mainFbOverride = null ;
1540+ if (context .boundFBO == 0 ) {
1541+ // Main FB is now set to fb, make sure its bound
1542+ setFrameBuffer (fb );
1543+ }
15191544 mainFbOverride = fb ;
15201545 }
15211546
1522- public void setFrameBuffer (FrameBuffer fb ) {
1523- if (fb == null && mainFbOverride != null ) {
1524- fb = mainFbOverride ;
1525- }
1526-
1527- if (context .boundFB == fb ) {
1528- if (fb == null || !fb .isUpdateNeeded ()) {
1529- return ;
1530- }
1531- }
1532-
1533- if (!caps .contains (Caps .FrameBuffer )) {
1534- throw new RendererException ("Framebuffer objects are not supported"
1535- + " by the video hardware" );
1536- }
1537-
1538- // generate mipmaps for last FB if needed
1539- if (context .boundFB != null ) {
1540- for (int i = 0 ; i < context .boundFB .getNumColorBuffers (); i ++) {
1541- RenderBuffer rb = context .boundFB .getColorBuffer (i );
1542- Texture tex = rb .getTexture ();
1543- if (tex != null
1544- && tex .getMinFilter ().usesMipMapLevels ()) {
1545- setTexture (0 , rb .getTexture ());
1546-
1547- int textureType = convertTextureType (tex .getType (), tex .getImage ().getMultiSamples (), rb .getFace ());
1548- glfbo .glGenerateMipmapEXT (textureType );
1549- }
1550- }
1547+ public void setReadDrawBuffers (FrameBuffer fb ) {
1548+ if (gl2 == null ) {
1549+ return ;
15511550 }
1552-
1551+
1552+ final int NONE = -2 ;
1553+ final int INITIAL = -1 ;
1554+ final int MRT_OFF = 100 ;
1555+
15531556 if (fb == null ) {
1554- // unbind any fbos
1555- if (context .boundFBO != 0 ) {
1556- glfbo .glBindFramebufferEXT (GLFbo .GL_FRAMEBUFFER_EXT , 0 );
1557- statistics .onFrameBufferUse (null , true );
1558-
1559- context .boundFBO = 0 ;
1557+ // Set Read/Draw buffers to initial value.
1558+ if (context .boundDrawBuf != INITIAL ) {
1559+ gl2 .glDrawBuffer (context .initialDrawBuf );
1560+ context .boundDrawBuf = INITIAL ;
15601561 }
1561- // select back buffer
1562- if (gl2 != null ) {
1563- if (context .boundDrawBuf != -1 ) {
1564- gl2 .glDrawBuffer (context .initialDrawBuf );
1565- context .boundDrawBuf = -1 ;
1566- }
1567- if (context .boundReadBuf != -1 ) {
1568- gl2 .glReadBuffer (context .initialReadBuf );
1569- context .boundReadBuf = -1 ;
1570- }
1562+ if (context .boundReadBuf != INITIAL ) {
1563+ gl2 .glReadBuffer (context .initialReadBuf );
1564+ context .boundReadBuf = INITIAL ;
15711565 }
1572-
1573- context .boundFB = null ;
15741566 } else {
1575- if (fb .getNumColorBuffers () == 0 && fb .getDepthBuffer () == null ) {
1576- throw new IllegalArgumentException ("The framebuffer: " + fb
1577- + "\n Doesn't have any color/depth buffers" );
1578- }
1579-
1580- if (fb .isUpdateNeeded ()) {
1581- updateFrameBuffer (fb );
1582- }
1583-
1584- // update viewport to reflect framebuffer's resolution
1585- setViewPort (0 , 0 , fb .getWidth (), fb .getHeight ());
1586-
1587- if (context .boundFBO != fb .getId ()) {
1588- glfbo .glBindFramebufferEXT (GLFbo .GL_FRAMEBUFFER_EXT , fb .getId ());
1589- statistics .onFrameBufferUse (fb , true );
1590-
1591- context .boundFBO = fb .getId ();
1592- } else {
1593- statistics .onFrameBufferUse (fb , false );
1594- }
15951567 if (fb .getNumColorBuffers () == 0 ) {
15961568 // make sure to select NONE as draw buf
1597- // no color buffer attached. select NONE
1569+ // no color buffer attached.
15981570 if (gl2 != null ) {
1599- if (context .boundDrawBuf != - 2 ) {
1571+ if (context .boundDrawBuf != NONE ) {
16001572 gl2 .glDrawBuffer (GL .GL_NONE );
1601- context .boundDrawBuf = - 2 ;
1573+ context .boundDrawBuf = NONE ;
16021574 }
1603- if (context .boundReadBuf != - 2 ) {
1575+ if (context .boundReadBuf != NONE ) {
16041576 gl2 .glReadBuffer (GL .GL_NONE );
1605- context .boundReadBuf = - 2 ;
1577+ context .boundReadBuf = NONE ;
16061578 }
16071579 }
16081580 } else {
@@ -1622,15 +1594,15 @@ public void setFrameBuffer(FrameBuffer fb) {
16221594 + " by the video hardware!" );
16231595 }
16241596
1625- if (context .boundDrawBuf != 100 + fb .getNumColorBuffers ()) {
1597+ if (context .boundDrawBuf != MRT_OFF + fb .getNumColorBuffers ()) {
16261598 intBuf16 .clear ();
16271599 for (int i = 0 ; i < fb .getNumColorBuffers (); i ++) {
16281600 intBuf16 .put (GLFbo .GL_COLOR_ATTACHMENT0_EXT + i );
16291601 }
16301602
16311603 intBuf16 .flip ();
16321604 glext .glDrawBuffers (intBuf16 );
1633- context .boundDrawBuf = 100 + fb .getNumColorBuffers ();
1605+ context .boundDrawBuf = MRT_OFF + fb .getNumColorBuffers ();
16341606 }
16351607 } else {
16361608 RenderBuffer rb = fb .getColorBuffer (fb .getTargetIndex ());
@@ -1643,8 +1615,56 @@ public void setFrameBuffer(FrameBuffer fb) {
16431615 }
16441616 }
16451617 }
1618+ }
1619+
1620+ }
1621+
1622+ public void setFrameBuffer (FrameBuffer fb ) {
1623+ if (fb == null && mainFbOverride != null ) {
1624+ fb = mainFbOverride ;
1625+ }
1626+
1627+ if (context .boundFB == fb ) {
1628+ if (fb == null || !fb .isUpdateNeeded ()) {
1629+ return ;
1630+ }
1631+ }
1632+
1633+ if (!caps .contains (Caps .FrameBuffer )) {
1634+ throw new RendererException ("Framebuffer objects are not supported"
1635+ + " by the video hardware" );
1636+ }
1637+
1638+ // generate mipmaps for last FB if needed
1639+ if (context .boundFB != null ) {
1640+ for (int i = 0 ; i < context .boundFB .getNumColorBuffers (); i ++) {
1641+ RenderBuffer rb = context .boundFB .getColorBuffer (i );
1642+ Texture tex = rb .getTexture ();
1643+ if (tex != null
1644+ && tex .getMinFilter ().usesMipMapLevels ()) {
1645+ setTexture (0 , rb .getTexture ());
1646+
1647+ int textureType = convertTextureType (tex .getType (), tex .getImage ().getMultiSamples (), rb .getFace ());
1648+ glfbo .glGenerateMipmapEXT (textureType );
1649+ }
1650+ }
1651+ }
1652+
1653+ if (fb == null ) {
1654+ bindFrameBuffer (null );
1655+ setReadDrawBuffers (null );
1656+ } else {
1657+ if (fb .isUpdateNeeded ()) {
1658+ updateFrameBuffer (fb );
1659+ } else {
1660+ bindFrameBuffer (fb );
1661+ setReadDrawBuffers (fb );
1662+ }
1663+
1664+ // update viewport to reflect framebuffer's resolution
1665+ setViewPort (0 , 0 , fb .getWidth (), fb .getHeight ());
16461666
1647- assert fb .getId () >= 0 ;
1667+ assert fb .getId () > 0 ;
16481668 assert context .boundFBO == fb .getId ();
16491669
16501670 context .boundFB = fb ;
0 commit comments