diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index ffbf87f5b..523b62ba9 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -69,7 +69,6 @@ public abstract class PGL { // Parameters - protected static boolean USE_FBOLAYER_BY_DEFAULT = false; public static int REQUESTED_DEPTH_BITS = 24; public static int REQUESTED_STENCIL_BITS = 8; public static int REQUESTED_ALPHA_BITS = 8; @@ -128,16 +127,17 @@ public abstract class PGL { * order to make sure the lines are always on top of the fill geometry */ protected static float STROKE_DISPLACEMENT = 0.999f; + protected static boolean DOUBLE_BUFFERED = true; // ........................................................ // FBO layer - protected boolean requestedFBOLayer = false; - protected boolean requestedFBOLayerReset = false; + protected boolean fboLayerEnabled = false; protected boolean fboLayerCreated = false; - protected boolean fboLayerInUse = false; - protected boolean firstFrame = true; + protected boolean fboLayerEnabledReq = false; + protected boolean fboLayerDisableReq = false; + protected boolean fbolayerResetReq = false; public int reqNumSamples; protected int numSamples; @@ -442,10 +442,6 @@ public PGL(PGraphicsOpenGL pg) { glMultiDepthStencil = allocateIntBuffer(1); glMultiDepth = allocateIntBuffer(1); glMultiStencil = allocateIntBuffer(1); - - fboLayerCreated = false; - fboLayerInUse = false; - firstFrame = false; } byteBuffer = allocateByteBuffer(1); @@ -494,39 +490,44 @@ static public int smoothToSamples(int smooth) { protected int getReadFramebuffer() { - return fboLayerInUse ? glColorFbo.get(0) : 0; + return fboLayerEnabled ? glColorFbo.get(0) : 0; } protected int getDrawFramebuffer() { - if (fboLayerInUse) return 1 < numSamples ? glMultiFbo.get(0) : - glColorFbo.get(0); + if (fboLayerEnabled) return 1 < numSamples ? glMultiFbo.get(0) : + glColorFbo.get(0); else return 0; } protected int getDefaultDrawBuffer() { - return fboLayerInUse ? COLOR_ATTACHMENT0 : BACK; + return fboLayerEnabled ? COLOR_ATTACHMENT0 : BACK; } protected int getDefaultReadBuffer() { - return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; + return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT; } protected boolean isFBOBacked() {; - return fboLayerInUse; + return fboLayerEnabled; + } + + + public void enableFBOLayer() { + fboLayerEnabledReq = true; } - public void requestFBOLayer() { - requestedFBOLayer = true; + public void disableFBOLayer() { + fboLayerDisableReq = true; } - public void requestFBOLayerReset() { - requestedFBOLayerReset = true; + public void resetFBOLayer() { + fbolayerResetReq = true; } @@ -639,7 +640,7 @@ public void initPresentMode(float x, float y) { presentMode = true; presentX = x; presentY = y; - requestFBOLayer(); + enableFBOLayer(); } @@ -693,12 +694,17 @@ protected void beginRender() { pclearColor = clearColor; clearColor = false; - if (requestedFBOLayer) { - if (requestedFBOLayerReset) { + if (fboLayerEnabledReq) { + fboLayerEnabled = true; + fboLayerEnabledReq = false; + } + + if (fboLayerEnabled) { + if (fbolayerResetReq) { destroyFBOLayer(); - requestedFBOLayerReset = false; + fbolayerResetReq = false; } - if (!fboLayerCreated) { + if (!fboLayerCreated && DOUBLE_BUFFERED) { createFBOLayer(); } @@ -711,7 +717,7 @@ protected void beginRender() { bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0)); } - if (firstFrame) { + if (sketch.frameCount == 0) { // No need to draw back color buffer because we are in the first frame. int argb = graphics.backgroundColor; float a = ((argb >> 24) & 0xff) / 255.0f; @@ -735,18 +741,12 @@ protected void beginRender() { 0, 0, (int)(scale * graphics.width), (int)(scale * graphics.height), 0, 0, graphics.width, graphics.height); } - - fboLayerInUse = true; - } else { - fboLayerInUse = false; } - - firstFrame = false; } protected void endRender(int windowColor) { - if (fboLayerInUse) { + if (fboLayerEnabled) { syncBackTexture(); // Draw the contents of the back texture to the screen framebuffer. @@ -804,8 +804,17 @@ protected void endRender(int windowColor) { int temp = frontTex; frontTex = backTex; backTex = temp; + + if (fboLayerDisableReq) { + fboLayerEnabled = false; + fboLayerDisableReq = false; + } } else if (!clearColor && 0 < sketch.frameCount || !sketch.isLooping()) { - requestFBOLayer(); + enableFBOLayer(); + } + + if (fboLayerEnabledReq && !fboLayerCreated && !DOUBLE_BUFFERED) { + createFBOLayer(); } } @@ -944,10 +953,7 @@ protected void destroyFBOLayer() { deleteRenderbuffers(1, glMultiDepth); deleteRenderbuffers(1, glMultiStencil); } - fboLayerCreated = false; - fboLayerInUse = false; -// firstFrame = false; } diff --git a/core/src/processing/opengl/PGLES.java b/core/src/processing/opengl/PGLES.java index b64cf40f6..29320eba1 100644 --- a/core/src/processing/opengl/PGLES.java +++ b/core/src/processing/opengl/PGLES.java @@ -79,6 +79,8 @@ public class PGLES extends PGL { // GLES static { + DOUBLE_BUFFERED = false; + MIN_DIRECT_BUFFER_SIZE = 1; INDEX_TYPE = GLES20.GL_UNSIGNED_SHORT; @@ -134,13 +136,7 @@ protected void setFrameRate(float fps) { } protected void initSurface(int antialias) { glview = (GLSurfaceView)sketch.getSurfaceView(); reqNumSamples = qualityToSamples(antialias); - registerListeners(); - - fboLayerCreated = false; - fboLayerInUse = false; - firstFrame = true; - setFps = false; } @@ -170,13 +166,13 @@ protected int getStencilBits() { @Override protected int getDefaultDrawBuffer() { - return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; + return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT; } @Override protected int getDefaultReadBuffer() { - return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; + return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT; } @@ -233,26 +229,27 @@ protected void initFBOLayer() { IntBuffer buf = null; buf = allocateDirectIntBuffer(fboWidth * fboHeight); - // Copy the contents of the front and back screen buffers to the textures - // of the FBO, so they are properly initialized. Note that the front buffer - // of the default framebuffer (the screen) contains the previous frame: - // https://www.opengl.org/wiki/Default_Framebuffer - // so it is copied to the front texture of the FBO layer: - if (pclearColor || 0 < pgeomCount || !sketch.isLooping()) { - readBuffer(FRONT); - } else { - // ...except when the previous frame has not been cleared and nothing was - // renderered while looping. In this case the back buffer, which holds the - // initial state of the previous frame, still contains the most up-to-date - // screen state. - readBuffer(BACK); - } +// // Copy the contents of the front and back screen buffers to the textures +// // of the FBO, so they are properly initialized. Note that the front buffer +// // of the default framebuffer (the screen) contains the previous frame: +// // https://www.opengl.org/wiki/Default_Framebuffer +// // so it is copied to the front texture of the FBO layer: +// if (pclearColor || 0 < pgeomCount || !sketch.isLooping()) { +// readBuffer(FRONT); +// } else { +// // ...except when the previous frame has not been cleared and nothing was +// // renderered while looping. In this case the back buffer, which holds the +// // initial state of the previous frame, still contains the most up-to-date +// // screen state. +// readBuffer(BACK); +// } + readBuffer(FRONT); readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf); bindTexture(TEXTURE_2D, glColorTex.get(frontTex)); texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf); - readBuffer(BACK); - readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf); +// readBuffer(BACK); +// readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf); bindTexture(TEXTURE_2D, glColorTex.get(backTex)); texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf); diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index dcbafcaf8..0edabf74b 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -704,7 +704,7 @@ public void requestDraw() { } -/* + /* @Override // Java only public PSurface createSurface() { // ignore @@ -5766,7 +5766,7 @@ public void filter(PShader shader) { boolean needEndDraw = false; if (primaryGraphics) { - pgl.requestFBOLayer(); + pgl.enableFBOLayer(); } else if (!drawing) { beginDraw(); needEndDraw = true; @@ -5842,7 +5842,7 @@ public void filter(PShader shader) { @Override public void copy(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh) { - if (primaryGraphics) pgl.requestFBOLayer(); + if (primaryGraphics) pgl.enableFBOLayer(); loadTexture(); if (filterTexture == null || filterTexture.contextIsOutdated()) { filterTexture = new Texture(this, texture.width, texture.height, diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index 790bb1154..0d460c124 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -1320,7 +1320,7 @@ protected void unbindTyped() { if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc); if (-1 < ppixelsLoc) { - pgl.requestFBOLayer(); + pgl.enableFBOLayer(); pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit); currentPG.unbindFrontTexture(); pgl.activeTexture(PGL.TEXTURE0);