From d58d71f969a0cda125518b4a65e3cf521d7f0f01 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Fri, 23 Nov 2012 21:14:54 +0000 Subject: [PATCH] start clean-up of surface handling (onscreen and offscreen) --- core/src/processing/opengl/PGL.java | 90 +++++++++++++++++-- .../processing/opengl/PGraphicsOpenGL.java | 65 ++++++++++++-- core/src/processing/opengl/Texture.java | 37 ++++++++ 3 files changed, 178 insertions(+), 14 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index f4332eb50..e6350549e 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -630,6 +630,12 @@ public class PGL { } + + + + + // CLEANUP! + protected int primaryReadFramebuffer() { if (capabilities.isFBO()) { return context.getDefaultReadFramebuffer(); @@ -638,6 +644,7 @@ public class PGL { } } + protected int primaryDrawFramebuffer() { if (capabilities.isFBO()) { return context.getDefaultDrawFramebuffer(); @@ -662,26 +669,88 @@ public class PGL { } } - protected boolean primaryIsFboBacked() { + + + + protected boolean isFBOBacked() { return capabilities.isFBO(); } - protected int getFboTexTarget() { - return GL.GL_TEXTURE_2D; - } - - protected int getFboTexName() { + protected int getBackTexName() { return backTex.getName(); } - protected int getFboWidth() { + protected int getBackTexTarget() { + return GL.GL_TEXTURE_2D; + } + + protected int getBackTexFormat() { + return backTex.format; + } + + protected int getBackTexWidth() { return backTex.getWidth(); } - protected int getFboHeight() { + protected int getBackTexHeight() { return backTex.getHeight(); } + protected int getBackTexMinFilter() { + return backTex.minFilter; + } + + protected int getBackTexMagFilter() { + return backTex.magFilter; + } + + protected int getBackTexWrapS() { + return backTex.wrapS; + } + + protected int getBackTexWrapT() { + return backTex.wrapT; + } + + protected int getFrontTexName() { + return frontTex.getName(); + } + + protected int getFrontTexTarget() { + return GL.GL_TEXTURE_2D; + } + + protected int getFrontTexFormat() { + return frontTex.format; + } + + protected int getFrontTexWidth() { + return frontTex.getWidth(); + } + + protected int getFrontTexHeight() { + return frontTex.getHeight(); + } + + protected int getFrontTexMinFilter() { + return frontTex.minFilter; + } + + protected int getFrontTexMagFilter() { + return frontTex.magFilter; + } + + protected int getFrontTexWrapS() { + return frontTex.wrapS; + } + + protected int getFrontTexWrapT() { + return frontTex.wrapT; + } + + + + /* protected void bindPrimaryColorFBO() { if (multisample) { @@ -775,6 +844,11 @@ public class PGL { } + + + + + /////////////////////////////////////////////////////////// // Frame rendering diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index adb7d8ba4..204d1a2ba 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -390,10 +390,12 @@ public class PGraphicsOpenGL extends PGraphics { // Screen surface: - /** A handy reference to the PTexture bound to the drawing surface - * (off or on-screen) */ + /** Texture containing the current frame */ protected Texture texture; + /** Texture containing the previous frame */ + protected Texture ptexture; + /** Used to create a temporary copy of the color buffer of this * rendering surface when applying a filter */ // protected Texture textureCopy; @@ -1893,7 +1895,7 @@ public class PGraphicsOpenGL extends PGraphics { if (op == OP_READ) { setFramebuffer(readFramebuffer); pgl.readBuffer(pgl.primaryReadBuffer()); - if (pgl.primaryIsFboBacked()) { + if (pgl.isFBOBacked()) { pgl.forceUpdate(); } } else { @@ -5392,10 +5394,10 @@ public class PGraphicsOpenGL extends PGraphics { if (primarySurface) { loadTextureImpl(Texture.POINT, false); - if (pgl.primaryIsFboBacked()) { + if (pgl.isFBOBacked()) { pgl.forceUpdate(); - texture.set(pgl.getFboTexTarget(), pgl.getFboTexName(), - pgl.getFboWidth(), pgl.getFboHeight(), width, height); + texture.set(pgl.getBackTexTarget(), pgl.getBackTexName(), + pgl.getBackTexWidth(), pgl.getBackTexHeight(), width, height); } else { // Here we go the slow route: we first copy the contents of the color // buffer into a pixels array (but we keep it in native format) and @@ -5511,6 +5513,57 @@ public class PGraphicsOpenGL extends PGraphics { texture.colorBufferOf(this); pgPrimary.setCache(this, texture); } + + + +/* + texture.glName = pgl.getBackTexName(); + ptexture.glName = pgl.getFrontTexName(); + + if (width == 0 || height == 0) return; + if (texture == null || texture.contextIsOutdated()) { + if (primarySurface) { + if (pgl.isFBOBacked()) { + texture = new Texture(parent); + texture.init(pgl.getBackTexName(), + pgl.getBackTexTarget(), pgl.getBackTexFormat(), + pgl.getBackTexWidth(), pgl.getBackTexHeight(), + pgl.getBackTexMinFilter(), pgl.getBackTexMagFilter(), + pgl.getBackTexWrapS(), pgl.getBackTexWrapT()); + texture.invertedY(true); + texture.colorBufferOf(this); + pgPrimary.setCache(this, texture); + + ptexture = new Texture(parent); + ptexture.init(pgl.getFrontTexName(), + pgl.getFrontTexTarget(), pgl.getFrontTexFormat(), + pgl.getFrontTexWidth(), pgl.getFrontTexHeight(), + pgl.getFrontTexMinFilter(), pgl.getFrontTexMagFilter(), + pgl.getFrontTexWrapS(), pgl.getFrontTexWrapT()); + ptexture.invertedY(true); + ptexture.colorBufferOf(this); + } else { + Texture.Parameters params = new Texture.Parameters(ARGB, Texture.POINT, false); + texture = new Texture(parent, width, height, params); + texture.invertedY(true); + texture.colorBufferOf(this); + pgPrimary.setCache(this, texture); + + ptexture = null; + } + } else { + Texture.Parameters params = new Texture.Parameters(ARGB, Texture.BILINEAR, false); + texture = new Texture(parent, width, height, params); + texture.invertedY(true); + texture.colorBufferOf(this); + pgPrimary.setCache(this, texture); + + ptexture = new Texture(parent, width, height, params); + ptexture.invertedY(true); + ptexture.colorBufferOf(this); + } + } +*/ } diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index 5badd5b5a..f6708c7a5 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -108,6 +108,18 @@ public class Texture implements PConstants { // Constructors. + public Texture(PApplet parent) { + this.parent = parent; + + pg = (PGraphicsOpenGL)parent.g; + pgl = pg.pgl; + context = pgl.createEmptyContext(); + + pgDraw = null; + + glName = 0; + } + /** * Creates an instance of PTexture with size width x height. The texture is @@ -226,6 +238,31 @@ public class Texture implements PConstants { return 0 < glName; } + /** + * Initializes the texture using GL parameters + */ + public void init(int glName, int glTarget, int glFormat, int glWidth, int glHeight, + int glMinFilter, int glMagFilter, int glWrapS, int glWrapT) { + this.glName = glName; + this.glTarget = glTarget; + this.glFormat = glFormat; + this.glWidth = glWidth; + this.glHeight = glHeight; + this.glMinFilter = glMinFilter; + this.glMagFilter = glMagFilter; + this.glWrapS = glWrapS; + this.glWrapT = glWrapT; + + width = glWidth; + height = glHeight; + maxTexcoordU = 1; + maxTexcoordV = 1; + + usingMipmaps = glMinFilter == PGL.LINEAR_MIPMAP_NEAREST || + glMinFilter == PGL.LINEAR_MIPMAP_LINEAR; + + usingRepeat = glWrapS == PGL.REPEAT || glWrapT == PGL.REPEAT; + } ////////////////////////////////////////////////////////////