diff --git a/core/andres.txt b/core/andres.txt index 6b08c0caf..c3afb89b6 100644 --- a/core/andres.txt +++ b/core/andres.txt @@ -10,6 +10,8 @@ X Fixed color update issues in PShapeOpenGL. X Implement mask(PImage) using a shader. X Add DISABLE_PERSPECTIVE_CORRECTED_STROKE hint for both points and lines, X remove DISABLE_PERSPECTIVE_CORRECTED_LINES +X Back-buffer support in shaders: +X http://code.google.com/p/processing/issues/detail?id=1169 0208 video X Cleanup video API: @@ -30,8 +32,7 @@ X http://code.google.com/p/processing/issues/detail?id=1146 0208 todo pre-beta: -_ Back-buffer support in shaders: -_ http://code.google.com/p/processing/issues/detail?id=1169 +_ Add some shader examples (monjori, etc) _ Decide on the names of the uniform and attribute variables in the shaders diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 53f8f26e8..ec5108bef 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -13619,6 +13619,12 @@ public class PApplet extends Applet } + public void clear() { + if (recorder != null) recorder.clear(); + g.clear(); + } + + /** * ( begin auto-generated from background.xml ) * @@ -14059,6 +14065,11 @@ public class PApplet extends Applet } + public Object getNative() { + return g.getNative(); + } + + /** * ( begin auto-generated from PImage_get.xml ) * diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 0c751e4cd..a1f026837 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -795,7 +795,7 @@ public class PGL { } } - +/* protected boolean primaryIsDoubleBuffered() { // When using the multisampled FBO, the color // FBO is single buffered as it has only one @@ -803,7 +803,7 @@ public class PGL { //return glColorFbo[0] == 0; return true; } - +*/ protected boolean primaryIsFboBacked() { return glColorFbo[0] != 0; @@ -887,6 +887,28 @@ public class PGL { } } + protected void bindBackBufferTex() { + if (!texturingIsEnabled(GL.GL_TEXTURE_2D)) { + enableTexturing(GL.GL_TEXTURE_2D); + } + gl.glBindTexture(GL.GL_TEXTURE_2D, glColorTex[backTex]); + } + + + protected void unbindBackBufferTex() { + if (textureIsBound(GL.GL_TEXTURE_2D, glColorTex[backTex])) { + // We don't want to unbind another texture + // that might be bound instead of this one. + if (!texturingIsEnabled(GL.GL_TEXTURE_2D)) { + enableTexturing(GL.GL_TEXTURE_2D); + gl.glBindTexture(GL.GL_TEXTURE_2D, 0); + disableTexturing(GL.GL_TEXTURE_2D); + } else { + gl.glBindTexture(GL.GL_TEXTURE_2D, 0); + } + } + } + /////////////////////////////////////////////////////////// @@ -941,12 +963,31 @@ public class PGL { // Leaving the color FBO currently bound as the screen FB. gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, glColorFbo[0]); + + + + // Blitting the front texture into the back texture. + gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, + GL.GL_COLOR_ATTACHMENT0, + GL.GL_TEXTURE_2D, + glColorTex[backTex], 0); + drawTexture(GL.GL_TEXTURE_2D, glColorTex[frontTex], fboWidth, fboHeight, + 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height); + + // Leave the front texture as current + gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, + GL.GL_COLOR_ATTACHMENT0, + GL.GL_TEXTURE_2D, + glColorTex[frontTex], 0); + + + PGraphicsOpenGL.screenFramebuffer.glFbo = glColorFbo[0]; // Swapping front and back textures. - int temp = frontTex; - frontTex = backTex; - backTex = temp; +// int temp = frontTex; +// frontTex = backTex; +// backTex = temp; } } diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 3df5b2159..f32d404df 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -1554,9 +1554,7 @@ public class PGraphicsOpenGL extends PGraphics { if (primarySurface) { pgl.updatePrimary(); - if (pgl.primaryIsDoubleBuffered()) { - pgl.drawBuffer(pgl.primaryDrawBuffer()); - } + pgl.drawBuffer(pgl.primaryDrawBuffer()); } else { if (!pgl.initialized) { initOffscreen(); @@ -1860,15 +1858,25 @@ public class PGraphicsOpenGL extends PGraphics { pgl.depthMask(true); } - if (pgl.primaryIsDoubleBuffered()) { - pgl.drawBuffer(pgl.primaryDrawBuffer()); - } + pgl.drawBuffer(pgl.primaryDrawBuffer()); } protected void beginPixelsOp(int op) { if (primarySurface) { - if (pgl.primaryIsDoubleBuffered()) { + if (pgl.primaryIsFboBacked()) { + if (op == OP_READ) { + // We read from the color FBO, but the multisample FBO is currently + // bound, so: + offscreenNotCurrent = true; + pgl.bindPrimaryColorFBO(); + pgl.readBuffer(pgl.primaryDrawBuffer()); + } else { + // We write directly to the multisample FBO. + offscreenNotCurrent = false; + pgl.drawBuffer(pgl.primaryDrawBuffer()); + } + } else { // We read or write from the back buffer, where all the // drawing in the current frame is taking place. if (op == OP_READ) { @@ -1877,20 +1885,6 @@ public class PGraphicsOpenGL extends PGraphics { pgl.drawBuffer(pgl.primaryDrawBuffer()); } offscreenNotCurrent = false; - } else if (pgl.primaryIsFboBacked()) { - if (op == OP_READ) { - // We read from the color FBO, but the multisample FBO is currently - // bound, so: - offscreenNotCurrent = true; - pgl.bindPrimaryColorFBO(); - pgl.readBuffer(PGL.COLOR_ATTACHMENT0); - } else { - // We write directly to the multisample FBO. - offscreenNotCurrent = false; - pgl.drawBuffer(PGL.COLOR_ATTACHMENT0); - } - } else { - offscreenNotCurrent = false; } } else { // Making sure that the offscreen FBO is current. This allows to do calls @@ -5913,6 +5907,24 @@ public class PGraphicsOpenGL extends PGraphics { } + protected void bindBackTexture() { + if (primarySurface) { + pgl.bindBackBufferTex(); + } else { + + } + } + + + protected void unbindBackTexture() { + if (primarySurface) { + pgl.unbindBackBufferTex(); + } else { + + } + } + + /** * This utility method creates a texture for the provided image, and adds it * to the metadata cache of the image. @@ -6480,6 +6492,8 @@ public class PGraphicsOpenGL extends PGraphics { protected int projmodelviewMatrixLoc; protected int modelviewMatrixLoc; protected int projectionMatrixLoc; + protected int backbufferSamplerLoc; + protected int resolutionLoc; protected int inVertexLoc; protected int inColorLoc; @@ -6508,6 +6522,9 @@ public class PGraphicsOpenGL extends PGraphics { projmodelviewMatrixLoc = getUniformLoc("projmodelviewMatrix"); modelviewMatrixLoc = getUniformLoc("modelviewMatrix"); projectionMatrixLoc = getUniformLoc("projectionMatrix"); + + backbufferSamplerLoc = getUniformLoc("backbufferSampler"); + resolutionLoc = getUniformLoc("resolution"); } @Override @@ -6548,6 +6565,16 @@ public class PGraphicsOpenGL extends PGraphics { pgCurrent.updateGLProjection(); setUniformMatrix(projectionMatrixLoc, pgCurrent.glProjection); } + + float w = pgCurrent.width; + float h = pgCurrent.height; + setUniformValue(resolutionLoc, w, h); + + if (-1 < backbufferSamplerLoc) { + setUniformValue(backbufferSamplerLoc, lastTexUnit); + pgl.activeTexture(PGL.TEXTURE0 + lastTexUnit); + pgCurrent.bindBackTexture(); + } } @Override @@ -6555,6 +6582,12 @@ public class PGraphicsOpenGL extends PGraphics { if (-1 < inVertexLoc) pgl.disableVertexAttribArray(inVertexLoc); if (-1 < inColorLoc) pgl.disableVertexAttribArray(inColorLoc); + if (-1 < backbufferSamplerLoc) { + pgl.activeTexture(PGL.TEXTURE0 + lastTexUnit); + pgCurrent.unbindBackTexture(); + pgl.activeTexture(PGL.TEXTURE0); + } + pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); super.unbind(); @@ -6744,8 +6777,6 @@ public class PGraphicsOpenGL extends PGraphics { protected int textureSamplerLoc; protected int texcoordMatrixLoc; protected int texcoordOffsetLoc; - protected int backbufferSamplerLoc; - protected int resolutionLoc; protected float[] tcmat; @@ -6769,9 +6800,6 @@ public class PGraphicsOpenGL extends PGraphics { textureSamplerLoc = getUniformLoc("textureSampler"); texcoordMatrixLoc = getUniformLoc("texcoordMatrix"); texcoordOffsetLoc = getUniformLoc("texcoordOffset"); - - backbufferSamplerLoc = getUniformLoc("backbufferSampler"); - resolutionLoc = getUniformLoc("resolution"); } @Override @@ -6832,10 +6860,6 @@ public class PGraphicsOpenGL extends PGraphics { super.bind(); if (-1 < inTexcoordLoc) pgl.enableVertexAttribArray(inTexcoordLoc); - - if (-1 < backbufferSamplerLoc) { - // ... - } } @Override diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index 835ced1d7..d73644555 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -75,6 +75,7 @@ public class PShader { protected HashMap textures; protected int firstTexUnit; + protected int lastTexUnit; public PShader() { @@ -529,7 +530,7 @@ public class PShader { protected void consumeUniforms() { if (uniformValues != null && 0 < uniformValues.size()) { - int texUnit = firstTexUnit; + lastTexUnit = firstTexUnit; for (Integer loc: uniformValues.keySet()) { UniformValue val = uniformValues.get(loc); if (val.type == UniformValue.INT1) { @@ -592,12 +593,12 @@ public class PShader { } else if (val.type == UniformValue.SAMPLER2D) { PImage img = (PImage)val.value; Texture tex = pgMain.getTexture(img); - pgl.uniform1i(loc, texUnit); + pgl.uniform1i(loc, lastTexUnit); if (textures == null) { textures = new HashMap(); } - textures.put(texUnit, tex); - texUnit++; + textures.put(lastTexUnit, tex); + lastTexUnit++; } } uniformValues.clear();