From dfaeaf7dcb62141fae4bf32a9406546180e7e237 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 11 Jul 2012 09:07:04 +0000 Subject: [PATCH] disabling use of multisampled FBO when numSamples == 1 --- android/core/src/processing/opengl/PGL.java | 5 +- .../opengl/src/processing/opengl/PGL.java | 139 +++++++++++------- 2 files changed, 88 insertions(+), 56 deletions(-) diff --git a/android/core/src/processing/opengl/PGL.java b/android/core/src/processing/opengl/PGL.java index 8937b8818..1a17ff63f 100644 --- a/android/core/src/processing/opengl/PGL.java +++ b/android/core/src/processing/opengl/PGL.java @@ -557,8 +557,6 @@ public class PGL { public void endOnscreenDraw(boolean clear0) { if (!clear0) { - GLES20.glDisable(GLES20.GL_BLEND); - // We are in the primary surface, and no clear mode, this means that the current // contents of the front buffer needs to be used in the next frame as the background. GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); @@ -567,7 +565,8 @@ public class PGL { GLES20.glClearColor(0, 0, 0, 0); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); - // Render current front texture to screen. + // Render current front texture to screen, without blending. + GLES20.glDisable(GLES20.GL_BLEND); drawTexture(GLES20.GL_TEXTURE_2D, colorTex[frontTex], texWidth, texHeight, 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height); // Swapping front and back textures. diff --git a/java/libraries/opengl/src/processing/opengl/PGL.java b/java/libraries/opengl/src/processing/opengl/PGL.java index 68d22211b..501a4861c 100644 --- a/java/libraries/opengl/src/processing/opengl/PGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGL.java @@ -640,36 +640,53 @@ public class PGL { gl.glClearColor(0, 0, 0, 0); gl.glClear(GL.GL_COLOR_BUFFER_BIT); - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0); + if (1 < numSamples) { + // We need multisampled FBO: + + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0); - // Now, creating mutisampled FBO with packed depth and stencil buffers. - gl.glGenFramebuffers(1, multiFBO, 0); - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); + // Now, creating mutisampled FBO with packed depth and stencil buffers. + gl.glGenFramebuffers(1, multiFBO, 0); + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); - // color render buffer... - gl.glGenRenderbuffers(1, colorRenderBuffer, 0); - gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, colorRenderBuffer[0]); - gl2x.glRenderbufferStorageMultisample(GL.GL_RENDERBUFFER, numSamples, GL.GL_RGBA8, fboWidth, fboHeight); - gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_RENDERBUFFER, colorRenderBuffer[0]); + // color render buffer... + gl.glGenRenderbuffers(1, colorRenderBuffer, 0); + gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, colorRenderBuffer[0]); + gl2x.glRenderbufferStorageMultisample(GL.GL_RENDERBUFFER, numSamples, GL.GL_RGBA8, fboWidth, fboHeight); + gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_RENDERBUFFER, colorRenderBuffer[0]); - // packed depth+stencil buffer... - gl.glGenRenderbuffers(1, packedDepthStencil, 0); - gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, packedDepthStencil[0]); - gl2x.glRenderbufferStorageMultisample(GL.GL_RENDERBUFFER, numSamples, GL.GL_DEPTH24_STENCIL8, fboWidth, fboHeight); - gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); - gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_STENCIL_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); + // packed depth+stencil buffer... + gl.glGenRenderbuffers(1, packedDepthStencil, 0); + gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, packedDepthStencil[0]); + gl2x.glRenderbufferStorageMultisample(GL.GL_RENDERBUFFER, numSamples, GL.GL_DEPTH24_STENCIL8, fboWidth, fboHeight); + gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); + gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_STENCIL_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); - // Clear all the buffers in the multisample FBO - gl.glClearDepth(1); - gl.glClearStencil(0); - gl.glClearColor(0, 0, 0, 0); - gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT); - - // All set with multisampled FBO! + // Clear all the buffers in the multisample FBO + gl.glClearDepth(1); + gl.glClearStencil(0); + gl.glClearColor(0, 0, 0, 0); + gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT); + + // All set with multisampled FBO! + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, colorFBO[0]); + } else { + // packed depth+stencil buffer... + gl.glGenRenderbuffers(1, packedDepthStencil, 0); + gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, packedDepthStencil[0]); + gl2x.glRenderbufferStorage(GL.GL_RENDERBUFFER, GL.GL_DEPTH24_STENCIL8, fboWidth, fboHeight); + gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); + gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER, GL.GL_STENCIL_ATTACHMENT, GL.GL_RENDERBUFFER, packedDepthStencil[0]); + + // Clear all the buffers in the color FBO + gl.glClearDepth(1); + gl.glClearStencil(0); + gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT); + } + // The screen framebuffer is the color FBO just created. We need - // to update the screenFramebuffer object so when the framebuffer - // is popped back to the screen, the correct id is set. - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, colorFBO[0]); + // to update the screenFramebuffer object so when the framebuffer + // is popped back to the screen, the correct id is set. PGraphicsOpenGL.screenFramebuffer.glFboID = colorFBO[0]; } else { // To make sure that the default screen buffer is used, specially after @@ -700,13 +717,15 @@ public class PGL { public void bindPrimaryColorFBO() { - // Blit the contents of the multisampled FBO into the color FBO, - // so the later is up to date. - gl.glBindFramebuffer(GL2.GL_READ_FRAMEBUFFER, multiFBO[0]); - gl.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, colorFBO[0]); - gl2x.glBlitFramebuffer(0, 0, fboWidth, fboHeight, - 0, 0, fboWidth, fboHeight, - GL.GL_COLOR_BUFFER_BIT, GL.GL_NEAREST); + if (1 < numSamples) { + // Blit the contents of the multisampled FBO into the color FBO, + // so the later is up to date. + gl.glBindFramebuffer(GL2.GL_READ_FRAMEBUFFER, multiFBO[0]); + gl.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, colorFBO[0]); + gl2x.glBlitFramebuffer(0, 0, fboWidth, fboHeight, + 0, 0, fboWidth, fboHeight, + GL.GL_COLOR_BUFFER_BIT, GL.GL_NEAREST); + } gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, colorFBO[0]); PGraphicsOpenGL.screenFramebuffer.glFboID = colorFBO[0]; @@ -714,17 +733,21 @@ public class PGL { public void bindPrimaryMultiFBO() { - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); - PGraphicsOpenGL.screenFramebuffer.glFboID = multiFBO[0]; + if (1 < numSamples) { + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); + PGraphicsOpenGL.screenFramebuffer.glFboID = multiFBO[0]; + } } protected void releaseScreenFBO() { gl.glDeleteTextures(1, colorTex, 0); gl.glDeleteFramebuffers(1, colorFBO, 0); - gl.glDeleteFramebuffers(1, multiFBO, 0); - gl.glDeleteRenderbuffers(1, colorRenderBuffer, 0); gl.glDeleteRenderbuffers(1, packedDepthStencil, 0); + if (1 < numSamples) { + gl.glDeleteFramebuffers(1, multiFBO, 0); + gl.glDeleteRenderbuffers(1, colorRenderBuffer, 0); + } } @@ -746,32 +769,42 @@ public class PGL { public void beginOnscreenDraw(boolean clear) { if (colorFBO[0] != 0) { - // Render the scene to the mutisampled buffer... - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); - gl2x.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0); - - // Now the screen buffer is the multisample FBO. - PGraphicsOpenGL.screenFramebuffer.glFboID = multiFBO[0]; + if (1 < numSamples) { + // Render the scene to the mutisampled buffer... + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, multiFBO[0]); + gl2x.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0); + + // Now the screen buffer is the multisample FBO. + PGraphicsOpenGL.screenFramebuffer.glFboID = multiFBO[0]; + } else { + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, colorFBO[0]); + gl2x.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0); + + PGraphicsOpenGL.screenFramebuffer.glFboID = colorFBO[0]; + } } } public void endOnscreenDraw(boolean clear0) { if (colorFBO[0] != 0) { - gl.glDisable(GL.GL_BLEND); + if (1 < numSamples) { + // Blit the contents of the multisampled FBO into the color FBO: + gl.glBindFramebuffer(GL2.GL_READ_FRAMEBUFFER, multiFBO[0]); + gl.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, colorFBO[0]); + gl2x.glBlitFramebuffer(0, 0, fboWidth, fboHeight, + 0, 0, fboWidth, fboHeight, + GL.GL_COLOR_BUFFER_BIT, GL.GL_NEAREST); + } - // Blit the contents of the multisampled FBO into the color FBO: - gl.glBindFramebuffer(GL2.GL_READ_FRAMEBUFFER, multiFBO[0]); - gl.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, colorFBO[0]); - gl2x.glBlitFramebuffer(0, 0, fboWidth, fboHeight, - 0, 0, fboWidth, fboHeight, - GL.GL_COLOR_BUFFER_BIT, GL.GL_NEAREST); - - // And finally write the color texture to the screen. - gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0); + // And finally write the color texture to the screen, without blending. + gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0); + gl.glClearDepth(1); gl.glClearColor(0, 0, 0, 0); - gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT); + gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT); + + gl.glDisable(GL.GL_BLEND); drawTexture(GL.GL_TEXTURE_2D, colorTex[0], fboWidth, fboHeight, 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height); // Leaving the color FBO currently bound as the screen FB.