From b548787f736124feb3be73bdf6bc735a8bbfc326 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Fri, 14 Dec 2012 22:49:25 +0000 Subject: [PATCH] several opengl fixes (among them, major memory leak due to incorrectly initialized static var) --- android/core/src/processing/opengl/PGL.java | 63 ++- .../processing/opengl/PGraphicsOpenGL.java | 516 +++++++++++------- .../core/src/processing/opengl/Texture.java | 58 +- 3 files changed, 392 insertions(+), 245 deletions(-) diff --git a/android/core/src/processing/opengl/PGL.java b/android/core/src/processing/opengl/PGL.java index a29783acf..8ed2caadf 100644 --- a/android/core/src/processing/opengl/PGL.java +++ b/android/core/src/processing/opengl/PGL.java @@ -52,6 +52,11 @@ import android.opengl.GLU; * */ public class PGL { + public static final boolean SAVE_SURFACE_TO_PIXELS = false; + public static final boolean USE_DIRECT_PIXEL_BUFFERS = false; + public static final boolean USE_DIRECT_VERTEX_BUFFERS = false; + public static final int MIN_DIRECT_BUFFER_SIZE = 1; + /** Size of a short (in bytes). */ protected static final int SIZEOF_SHORT = Short.SIZE / 8; @@ -349,8 +354,8 @@ public class PGL { // FBO layer public static boolean FORCE_SCREEN_FBO = false; - protected static boolean usingFBOlayer = false; - protected static boolean firstFrame = true; + protected boolean usingFBOlayer = false; + protected boolean firstFrame = true; protected static IntBuffer glColorFbo; protected static IntBuffer glColorTex; protected static IntBuffer glDepthStencil; @@ -367,7 +372,7 @@ public class PGL { protected static int texShaderProgram; protected static int texVertShader; protected static int texFragShader; - + protected static EGLContext texShaderContext; protected static int texVertLoc; protected static int texTCoordLoc; @@ -415,7 +420,6 @@ public class PGL { public PGL(PGraphicsOpenGL pg) { this.pg = pg; - renderer = new AndroidRenderer(); if (glu == null) { glu = new PGLU(); } @@ -437,6 +441,15 @@ public class PGL { } + protected void deleteSurface() { + deleteTextures(2, glColorTex); + deleteFramebuffers(1, glColorFbo); + deleteRenderbuffers(1, glDepthStencil); + deleteRenderbuffers(1, glDepth); + deleteRenderbuffers(1, glStencil); + } + + protected void update() { if (!initialized) { String ext = getString(EXTENSIONS); @@ -536,6 +549,7 @@ public class PGL { bindFramebuffer(FRAMEBUFFER, 0); + System.out.println("inited FBO layer"); initialized = true; } } @@ -617,7 +631,8 @@ public class PGL { protected Texture wrapBackTexture() { Texture tex = new Texture(pg.parent); - tex.init(glColorTex.get(backTex), TEXTURE_2D, RGBA, + tex.init(pg.width, pg.height, + glColorTex.get(backTex), TEXTURE_2D, RGBA, fboWidth, fboHeight, NEAREST, NEAREST, CLAMP_TO_EDGE, CLAMP_TO_EDGE); tex.invertedY(true); @@ -629,7 +644,8 @@ public class PGL { protected Texture wrapFrontTexture() { Texture tex = new Texture(pg.parent); - tex.init(glColorTex.get(frontTex), TEXTURE_2D, RGBA, + tex.init(pg.width, pg.height, + glColorTex.get(frontTex), TEXTURE_2D, RGBA, fboWidth, fboHeight, NEAREST, NEAREST, CLAMP_TO_EDGE, CLAMP_TO_EDGE); tex.invertedY(true); @@ -1573,7 +1589,12 @@ public class PGL { // Doing in patches of 16x16 pixels to avoid creating a (potentially) // very large transient array which in certain situations (memory- // constrained android devices) might lead to an out-of-memory error. - IntBuffer texels = PGL.allocateDirectIntBuffer(16 * 16); + IntBuffer texels; + if (USE_DIRECT_PIXEL_BUFFERS) { + texels = PGL.allocateDirectIntBuffer(16 * 16); + } else { + texels = IntBuffer.allocate(16 * 16); + } for (int y = 0; y < height; y += 16) { int h = PApplet.min(16, height - y); for (int x = 0; x < width; x += 16) { @@ -1610,7 +1631,8 @@ public class PGL { protected void drawTexture(int target, int id, int width, int height, int texX0, int texY0, int texX1, int texY1, int scrX0, int scrY0, int scrX1, int scrY1) { - if (!loadedTexShader) { + if (!loadedTexShader || + texShaderContext.hashCode() != context.hashCode()) { texVertShader = createShader(VERTEX_SHADER, texVertShaderSource); texFragShader = createShader(FRAGMENT_SHADER, texFragShaderSource); if (0 < texVertShader && 0 < texFragShader) { @@ -1621,6 +1643,7 @@ public class PGL { texTCoordLoc = getAttribLocation(texShaderProgram, "inTexcoord"); } loadedTexShader = true; + texShaderContext = context; } if (texData == null) { @@ -1639,7 +1662,9 @@ public class PGL { boolean depthMask = getDepthWriteMask(); depthMask(false); + pg.report("BEFORE USE PROGRAM"); useProgram(texShaderProgram); + pg.report("USE SHADER PROGRAM " + texShaderProgram); enableVertexAttribArray(texVertLoc); enableVertexAttribArray(texTCoordLoc); @@ -2143,26 +2168,29 @@ public class PGL { protected static ByteBuffer allocateDirectByteBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_BYTE). - order(ByteOrder.nativeOrder()); + int bytes = PApplet.max(MIN_DIRECT_BUFFER_SIZE, size) * SIZEOF_BYTE; + return ByteBuffer.allocateDirect(bytes).order(ByteOrder.nativeOrder()); } protected static ShortBuffer allocateDirectShortBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_SHORT). - order(ByteOrder.nativeOrder()).asShortBuffer(); + int bytes = PApplet.max(MIN_DIRECT_BUFFER_SIZE, size) * SIZEOF_SHORT; + return ByteBuffer.allocateDirect(bytes).order(ByteOrder.nativeOrder()). + asShortBuffer(); } protected static IntBuffer allocateDirectIntBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_INT). - order(ByteOrder.nativeOrder()).asIntBuffer(); + int bytes = PApplet.max(MIN_DIRECT_BUFFER_SIZE, size) * SIZEOF_INT; + return ByteBuffer.allocateDirect(bytes).order(ByteOrder.nativeOrder()). + asIntBuffer(); } protected static FloatBuffer allocateDirectFloatBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_FLOAT). - order(ByteOrder.nativeOrder()).asFloatBuffer(); + int bytes = PApplet.max(MIN_DIRECT_BUFFER_SIZE, size) * SIZEOF_FLOAT; + return ByteBuffer.allocateDirect(bytes).order(ByteOrder.nativeOrder()). + asFloatBuffer(); } @@ -2212,6 +2240,7 @@ public class PGL { public AndroidRenderer getRenderer() { + renderer = new AndroidRenderer(); return renderer; } @@ -2235,6 +2264,8 @@ public class PGL { gl = igl; glThread = Thread.currentThread(); pg.parent.handleDraw(); +// clearColor(1, 0, 0, 1); +// clear(COLOR_BUFFER_BIT); } public void onSurfaceChanged(GL10 igl, int iwidth, int iheight) { diff --git a/android/core/src/processing/opengl/PGraphicsOpenGL.java b/android/core/src/processing/opengl/PGraphicsOpenGL.java index 8485da8ce..a9edb5a75 100644 --- a/android/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/android/core/src/processing/opengl/PGraphicsOpenGL.java @@ -451,6 +451,12 @@ public class PGraphicsOpenGL extends PGraphics { /** IntBuffer wrapping the native pixels array. */ protected IntBuffer nativePixelBuffer; + /** texture used to apply a filter on the screen image. */ + protected Texture filterTexture; + + /** PImage that wraps filterTexture. */ + protected PImage filterImage; + /** Flag to indicate if the user is manipulating the * pixels array through the set()/get() methods */ protected boolean setgetPixels; @@ -521,13 +527,14 @@ public class PGraphicsOpenGL extends PGraphics { public PGraphicsOpenGL() { + System.err.println("Create PGL: " + pgPrimary + " " + pgCurrent); + pgl = new PGL(this); if (tessellator == null) { tessellator = new Tessellator(); } - intBuffer = PGL.allocateDirectIntBuffer(2); floatBuffer = PGL.allocateDirectFloatBuffer(2); @@ -570,6 +577,7 @@ public class PGraphicsOpenGL extends PGraphics { public void setPrimary(boolean primary) { super.setPrimary(primary); format = ARGB; + pgPrimary = this; } @@ -649,13 +657,20 @@ public class PGraphicsOpenGL extends PGraphics { @Override public void dispose() { // PGraphics super.dispose(); + deleteFinalizedGLResources(); deletePolyBuffers(); deleteLineBuffers(); deletePointBuffers(); + + pgl.deleteSurface(); + + System.err.println("Disposed renderer"); } + + protected void setFlushMode(int mode) { flushMode = mode; } @@ -718,10 +733,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = intBuffer.get(0); GLResource res = new GLResource(id, context); - - if (glTextureObjects.containsKey(res)) { - throw new RuntimeException("Adding same texture twice"); - } else { + if (!glTextureObjects.containsKey(res)) { glTextureObjects.put(res, false); } @@ -785,10 +797,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = intBuffer.get(0); GLResource res = new GLResource(id, context); - - if (glVertexBuffers.containsKey(res)) { - throw new RuntimeException("Adding same VBO twice"); - } else { + if (!glVertexBuffers.containsKey(res)) { glVertexBuffers.put(res, false); } @@ -852,10 +861,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = intBuffer.get(0); GLResource res = new GLResource(id, context); - - if (glFrameBuffers.containsKey(res)) { - throw new RuntimeException("Adding same FBO twice"); - } else { + if (!glFrameBuffers.containsKey(res)) { glFrameBuffers.put(res, false); } @@ -919,10 +925,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = intBuffer.get(0); GLResource res = new GLResource(id, context); - - if (glRenderBuffers.containsKey(res)) { - throw new RuntimeException("Adding same renderbuffer twice"); - } else { + if (!glRenderBuffers.containsKey(res)) { glRenderBuffers.put(res, false); } @@ -985,10 +988,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = pgl.createProgram(); GLResource res = new GLResource(id, context); - - if (glslPrograms.containsKey(res)) { - throw new RuntimeException("Adding same glsl program twice"); - } else { + if (!glslPrograms.containsKey(res)) { glslPrograms.put(res, false); } @@ -1048,10 +1048,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = pgl.createShader(PGL.VERTEX_SHADER); GLResource res = new GLResource(id, context); - - if (glslVertexShaders.containsKey(res)) { - throw new RuntimeException("Adding same glsl vertex shader twice"); - } else { + if (!glslVertexShaders.containsKey(res)) { glslVertexShaders.put(res, false); } @@ -1112,10 +1109,7 @@ public class PGraphicsOpenGL extends PGraphics { int id = pgl.createShader(PGL.FRAGMENT_SHADER); GLResource res = new GLResource(id, context); - - if (glslFragmentShaders.containsKey(res)) { - throw new RuntimeException("Adding same glsl fragment shader twice"); - } else { + if (!glslFragmentShaders.containsKey(res)) { glslFragmentShaders.put(res, false); } @@ -1624,9 +1618,9 @@ public class PGraphicsOpenGL extends PGraphics { updateOffscreen(); beginOffscreenDraw(); } - setDefaults(); pgCurrent = this; + drawing = true; report("bot beginDraw()"); @@ -1645,14 +1639,15 @@ public class PGraphicsOpenGL extends PGraphics { // Flushing any remaining geometry. flush(); - if (!pgPrimary.pgl.initialized || parent.frameCount == 0) { + if (PGL.SAVE_SURFACE_TO_PIXELS && + (!pgPrimary.pgl.initialized || parent.frameCount == 0)) { // Smooth was disabled/enabled at some point during drawing. We save // the current contents of the back buffer (because the buffers haven't // been swapped yet) to the pixels array. The frameCount == 0 condition // is to handle the situation when no smooth is called in setup in the // PDE, but the OpenGL appears to be recreated due to the size() nastiness. -// saveSurfaceToPixels(); -// restoreSurface = true; + saveSurfaceToPixels(); + restoreSurface = true; } if (primarySurface) { @@ -1669,7 +1664,9 @@ public class PGraphicsOpenGL extends PGraphics { // Done with an offscreen surface, going back to onscreen drawing. pgCurrent = pgPrimary; } + drawing = false; + pgCurrent = null; report("bot endDraw()"); } @@ -5041,7 +5038,11 @@ public class PGraphicsOpenGL extends PGraphics { protected void allocatePixels() { if ((pixels == null) || (pixels.length != width * height)) { pixels = new int[width * height]; - pixelBuffer = PGL.allocateDirectIntBuffer(width * height); + if (PGL.USE_DIRECT_PIXEL_BUFFERS) { + pixelBuffer = PGL.allocateDirectIntBuffer(width * height); + } else { + pixelBuffer = IntBuffer.wrap(pixels); + } } } @@ -5059,7 +5060,6 @@ public class PGraphicsOpenGL extends PGraphics { protected void readPixels() { beginPixelsOp(OP_READ); - pixelBuffer.rewind(); try { // The readPixels() call in inside a try/catch block because it appears // that (only sometimes) JOGL will run beginDraw/endDraw on the EDT @@ -5074,9 +5074,11 @@ public class PGraphicsOpenGL extends PGraphics { endPixelsOp(); try { // Idem... - pixelBuffer.position(0); - pixelBuffer.get(pixels); - pixelBuffer.rewind(); + if (PGL.USE_DIRECT_PIXEL_BUFFERS) { + pixelBuffer.position(0); + pixelBuffer.get(pixels); + pixelBuffer.rewind(); + } PGL.nativeToJavaARGB(pixels, width, height); } catch (ArrayIndexOutOfBoundsException e) { } @@ -5087,7 +5089,11 @@ public class PGraphicsOpenGL extends PGraphics { int len = w * h; if (nativePixels == null || nativePixels.length < len) { nativePixels = new int[len]; - nativePixelBuffer = PGL.allocateDirectIntBuffer(len); + if (PGL.USE_DIRECT_PIXEL_BUFFERS) { + nativePixelBuffer = PGL.allocateDirectIntBuffer(len); + } else { + nativePixelBuffer = IntBuffer.wrap(nativePixels); + } } try { @@ -5117,10 +5123,12 @@ public class PGraphicsOpenGL extends PGraphics { loadTextureImpl(POINT, false); } - // Put native pixels in direct buffer for copy. - nativePixelBuffer.position(0); - nativePixelBuffer.put(nativePixels); - nativePixelBuffer.rewind(); + if (PGL.USE_DIRECT_PIXEL_BUFFERS) { + // Put native pixels in direct buffer for copy. + nativePixelBuffer.position(0); + nativePixelBuffer.put(nativePixels); + nativePixelBuffer.rewind(); + } boolean needToDrawTex = primarySurface && (!pgl.isFBOBacked() || (pgl.isFBOBacked() && pgl.isMultisampled())) || @@ -5216,6 +5224,7 @@ public class PGraphicsOpenGL extends PGraphics { // In the case of MSAA, this is needed so the back buffer is in sync // with the rendering. pgl.syncBackTexture(); + PApplet.println("FBO backed"); } else { loadTextureImpl(Texture.POINT, false); @@ -5224,13 +5233,16 @@ public class PGraphicsOpenGL extends PGraphics { // then copy this array into the texture. if (nativePixels == null || nativePixels.length < width * height) { nativePixels = new int[width * height]; - nativePixelBuffer = PGL.allocateDirectIntBuffer(width * height); + if (PGL.USE_DIRECT_PIXEL_BUFFERS) { + nativePixelBuffer = PGL.allocateDirectIntBuffer(width * height); + } else { + nativePixelBuffer = IntBuffer.wrap(nativePixels); + } } beginPixelsOp(OP_READ); try { // Se comments in readPixels() for the reason for this try/catch. - nativePixelBuffer.rewind(); pgl.readPixels(0, 0, width, height, PGL.RGBA, PGL.UNSIGNED_BYTE, nativePixelBuffer); } catch (IndexOutOfBoundsException e) { @@ -5238,6 +5250,8 @@ public class PGraphicsOpenGL extends PGraphics { endPixelsOp(); texture.setNative(nativePixelBuffer, 0, 0, width, height); + + PApplet.println("not FBO backed"); } } else { // We need to copy the contents of the multisampled buffer to the @@ -5295,7 +5309,6 @@ public class PGraphicsOpenGL extends PGraphics { protected void loadTextureImpl(int sampling, boolean mipmap) { - if (width == 0 || height == 0) return; if (texture == null || texture.contextIsOutdated()) { Texture.Parameters params = new Texture.Parameters(ARGB, @@ -5410,6 +5423,13 @@ public class PGraphicsOpenGL extends PGraphics { } loadTexture(); + if (filterTexture == null || filterTexture.contextIsOutdated()) { + filterTexture = new Texture(parent, texture.width, texture.height, + texture.getParameters()); + filterTexture.invertedY(true); + filterImage = wrapTexture(filterTexture); + } + filterTexture.set(texture); // Disable writing to the depth buffer, so that after applying the filter we // can still use the depth information to keep adding geometry to the scene. @@ -5433,7 +5453,7 @@ public class PGraphicsOpenGL extends PGraphics { PolyTexShader prevTexShader = polyTexShader; polyTexShader = (PolyTexShader) shader; beginShape(QUADS); - texture(this); + texture(filterImage); vertex(0, 0, 0, 0); vertex(width, 0, 1, 0); vertex(width, height, 1, 1); @@ -5806,9 +5826,6 @@ public class PGraphicsOpenGL extends PGraphics { protected void initPrimary() { pgl.initSurface(quality); - if (pgPrimary == null) { - pgPrimary = this; - } if (texture != null) { pgPrimary.removeCache(this); texture = ptexture = null; @@ -5846,7 +5863,6 @@ public class PGraphicsOpenGL extends PGraphics { protected void initOffscreen() { // Getting the context and capabilities from the main renderer. - pgPrimary = (PGraphicsOpenGL)parent.g; loadTextureImpl(Texture.BILINEAR, false); // In case of reinitialization (for example, when the smooth level @@ -7275,14 +7291,6 @@ public class PGraphicsOpenGL extends PGraphics { hasTexture = false; } - void dispose() { - textures = null; - firstIndex = null; - lastIndex = null; - firstCache = null; - lastCache = null; - } - void beginRender() { tex0 = null; } @@ -7577,21 +7585,6 @@ public class PGraphicsOpenGL extends PGraphics { clear(); } - void dispose() { - breaks = null; - vertices = null; - colors = null; - normals = null; - texcoords = null; - strokeColors = null; - strokeWeights = null; - ambient = null; - specular = null; - emissive = null; - shininess = null; - edges = null; - } - void vertexCheck() { if (vertexCount == vertices.length / 3) { int newSize = vertexCount << 1; @@ -9024,26 +9017,6 @@ public class PGraphicsOpenGL extends PGraphics { // Allocate/dispose void allocate() { - polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); - polyColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * PGL.DEFAULT_TESS_VERTICES); - polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * PGL.DEFAULT_TESS_VERTICES); - polyAmbientBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - polySpecularBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - polyEmissiveBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - polyShininessBuffer = PGL.allocateDirectFloatBuffer(PGL.DEFAULT_TESS_VERTICES); - polyIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); - - lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); - lineColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); - lineIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); - - pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); - pointColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); - pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * PGL.DEFAULT_TESS_VERTICES); - pointIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); - polyVertices = new float[4 * PGL.DEFAULT_TESS_VERTICES]; polyColors = new int[PGL.DEFAULT_TESS_VERTICES]; polyNormals = new float[3 * PGL.DEFAULT_TESS_VERTICES]; @@ -9064,6 +9037,48 @@ public class PGraphicsOpenGL extends PGraphics { pointAttribs = new float[2 * PGL.DEFAULT_TESS_VERTICES]; pointIndices = new short[PGL.DEFAULT_TESS_VERTICES]; + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); + polyColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * PGL.DEFAULT_TESS_VERTICES); + polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * PGL.DEFAULT_TESS_VERTICES); + polyAmbientBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + polySpecularBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + polyEmissiveBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + polyShininessBuffer = PGL.allocateDirectFloatBuffer(PGL.DEFAULT_TESS_VERTICES); + polyIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); + + lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); + lineColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); + lineIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); + + pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); + pointColorsBuffer = PGL.allocateDirectIntBuffer(PGL.DEFAULT_TESS_VERTICES); + pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * PGL.DEFAULT_TESS_VERTICES); + pointIndicesBuffer = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); + } else { + polyVerticesBuffer = FloatBuffer.wrap(polyVertices); + polyColorsBuffer = IntBuffer.wrap(polyColors); + polyNormalsBuffer = FloatBuffer.wrap(polyNormals); + polyTexcoordsBuffer = FloatBuffer.wrap(polyTexcoords); + polyAmbientBuffer = IntBuffer.wrap(polyAmbient); + polySpecularBuffer = IntBuffer.wrap(polySpecular); + polyEmissiveBuffer = IntBuffer.wrap(polyEmissive); + polyShininessBuffer = FloatBuffer.wrap(polyShininess); + polyIndicesBuffer = ShortBuffer.wrap(polyIndices); + + lineVerticesBuffer = FloatBuffer.wrap(lineVertices); + lineColorsBuffer = IntBuffer.wrap(lineColors); + lineAttribsBuffer = FloatBuffer.wrap(lineAttribs); + lineIndicesBuffer = ShortBuffer.wrap(lineIndices); + + pointVerticesBuffer = FloatBuffer.wrap(pointVertices); + pointColorsBuffer = IntBuffer.wrap(pointColors); + pointAttribsBuffer = FloatBuffer.wrap(pointAttribs); + pointIndicesBuffer = ShortBuffer.wrap(pointIndices); + } + clear(); } @@ -9082,47 +9097,6 @@ public class PGraphicsOpenGL extends PGraphics { pointIndexCache.clear(); } - void dipose() { - polyVerticesBuffer = null; - polyColorsBuffer = null; - polyNormalsBuffer = null; - polyTexcoordsBuffer = null; - polyAmbientBuffer = null; - polySpecularBuffer = null; - polyEmissiveBuffer = null; - polyShininessBuffer = null; - polyIndicesBuffer = null; - - lineVerticesBuffer = null; - lineColorsBuffer = null; - lineAttribsBuffer = null; - lineIndicesBuffer = null; - - pointVerticesBuffer = null; - pointColorsBuffer = null; - pointAttribsBuffer = null; - pointIndicesBuffer = null; - - polyVertices = null; - polyColors = null; - polyNormals = null; - polyTexcoords = null; - polyAmbient = null; - polySpecular = null; - polyEmissive = null; - polyShininess = null; - polyIndices = null; - - lineVertices = null; - lineColors = null; - lineAttribs = null; - lineIndices = null; - - pointVertices = null; - pointColors = null; - pointAttribs = null; - pointIndices = null; - } void polyVertexCheck() { if (polyVertexCount == polyVertices.length / 4) { @@ -9517,139 +9491,207 @@ public class PGraphicsOpenGL extends PGraphics { // Expand arrays void expandPolyVertices(int n) { - polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); - float temp[] = new float[4 * n]; PApplet.arrayCopy(polyVertices, 0, temp, 0, 4 * polyVertexCount); polyVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); + } else { + polyVerticesBuffer = FloatBuffer.wrap(polyVertices); + } } void expandPolyColors(int n) { - polyColorsBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(polyColors, 0, temp, 0, polyVertexCount); polyColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyColorsBuffer = PGL.allocateDirectIntBuffer(n); + } else { + polyColorsBuffer = IntBuffer.wrap(polyColors); + } } void expandPolyNormals(int n) { - polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * n); - float temp[] = new float[3 * n]; PApplet.arrayCopy(polyNormals, 0, temp, 0, 3 * polyVertexCount); polyNormals = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * n); + } else { + polyNormalsBuffer = FloatBuffer.wrap(polyNormals); + } } void expandPolyTexcoords(int n) { - polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * n); - float temp[] = new float[2 * n]; PApplet.arrayCopy(polyTexcoords, 0, temp, 0, 2 * polyVertexCount); polyTexcoords = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * n); + } else { + polyTexcoordsBuffer = FloatBuffer.wrap(polyTexcoords); + } } void expandPolyAmbient(int n) { - polyAmbientBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(polyAmbient, 0, temp, 0, polyVertexCount); polyAmbient = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyAmbientBuffer = PGL.allocateDirectIntBuffer(n); + } else { + polyAmbientBuffer = IntBuffer.wrap(polyAmbient); + } } void expandPolySpecular(int n) { - polySpecularBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(polySpecular, 0, temp, 0, polyVertexCount); polySpecular = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polySpecularBuffer = PGL.allocateDirectIntBuffer(n); + } else { + polySpecularBuffer = IntBuffer.wrap(polySpecular); + } } void expandPolyEmissive(int n) { - polyEmissiveBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(polyEmissive, 0, temp, 0, polyVertexCount); polyEmissive = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyEmissiveBuffer = PGL.allocateDirectIntBuffer(n); + } else { + polyEmissiveBuffer = IntBuffer.wrap(polyEmissive); + } } void expandPolyShininess(int n) { - polyShininessBuffer = PGL.allocateDirectFloatBuffer(n); - float temp[] = new float[n]; PApplet.arrayCopy(polyShininess, 0, temp, 0, polyVertexCount); polyShininess = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyShininessBuffer = PGL.allocateDirectFloatBuffer(n); + } else { + polyShininessBuffer = FloatBuffer.wrap(polyShininess); + } } void expandPolyIndices(int n) { - polyIndicesBuffer = PGL.allocateDirectShortBuffer(n); - short temp[] = new short[n]; PApplet.arrayCopy(polyIndices, 0, temp, 0, polyIndexCount); polyIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyIndicesBuffer = PGL.allocateDirectShortBuffer(n); + } else { + polyIndicesBuffer = ShortBuffer.wrap(polyIndices); + } } void expandLineVertices(int n) { - lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); - float temp[] = new float[4 * n]; PApplet.arrayCopy(lineVertices, 0, temp, 0, 4 * lineVertexCount); lineVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); + } else { + lineVerticesBuffer = FloatBuffer.wrap(lineVertices); + } } void expandLineColors(int n) { - lineColorsBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(lineColors, 0, temp, 0, lineVertexCount); lineColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineColorsBuffer = PGL.allocateDirectIntBuffer(n); + } else { + lineColorsBuffer = IntBuffer.wrap(lineColors); + } } void expandLineAttribs(int n) { - lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * n); - float temp[] = new float[4 * n]; PApplet.arrayCopy(lineAttribs, 0, temp, 0, 4 * lineVertexCount); lineAttribs = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * n); + } else { + lineAttribsBuffer = FloatBuffer.wrap(lineAttribs); + } } void expandLineIndices(int n) { - lineIndicesBuffer = PGL.allocateDirectShortBuffer(n); - short temp[] = new short[n]; PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); lineIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineIndicesBuffer = PGL.allocateDirectShortBuffer(n); + } else { + lineIndicesBuffer = ShortBuffer.wrap(lineIndices); + } } void expandPointVertices(int n) { - pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); - float temp[] = new float[4 * n]; PApplet.arrayCopy(pointVertices, 0, temp, 0, 4 * pointVertexCount); pointVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * n); + } else { + pointVerticesBuffer = FloatBuffer.wrap(pointVertices); + } } void expandPointColors(int n) { - pointColorsBuffer = PGL.allocateDirectIntBuffer(n); - int temp[] = new int[n]; PApplet.arrayCopy(pointColors, 0, temp, 0, pointVertexCount); pointColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointColorsBuffer = PGL.allocateDirectIntBuffer(n); + } else { + pointColorsBuffer = IntBuffer.wrap(pointColors); + } } void expandPointAttribs(int n) { - pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * n); - float temp[] = new float[2 * n]; PApplet.arrayCopy(pointAttribs, 0, temp, 0, 2 * pointVertexCount); pointAttribs = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * n); + } else { + pointAttribsBuffer = FloatBuffer.wrap(pointAttribs); + } } void expandPointIndices(int n) { - pointIndicesBuffer = PGL.allocateDirectShortBuffer(n); - short temp[] = new short[n]; PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); pointIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointIndicesBuffer = PGL.allocateDirectShortBuffer(n); + } else { + pointIndicesBuffer = ShortBuffer.wrap(pointIndices); + } } // ----------------------------------------------------------------- @@ -9694,139 +9736,207 @@ public class PGraphicsOpenGL extends PGraphics { } void trimPolyVertices() { - polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * polyVertexCount); - float temp[] = new float[4 * polyVertexCount]; PApplet.arrayCopy(polyVertices, 0, temp, 0, 4 * polyVertexCount); polyVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * polyVertexCount); + } else { + polyVerticesBuffer = FloatBuffer.wrap(polyVertices); + } } void trimPolyColors() { - polyColorsBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); - int temp[] = new int[polyVertexCount]; PApplet.arrayCopy(polyColors, 0, temp, 0, polyVertexCount); polyColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyColorsBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); + } else { + polyColorsBuffer = IntBuffer.wrap(polyColors); + } } void trimPolyNormals() { - polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * polyVertexCount); - float temp[] = new float[3 * polyVertexCount]; PApplet.arrayCopy(polyNormals, 0, temp, 0, 3 * polyVertexCount); polyNormals = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyNormalsBuffer = PGL.allocateDirectFloatBuffer(3 * polyVertexCount); + } else { + polyNormalsBuffer = FloatBuffer.wrap(polyNormals); + } } void trimPolyTexcoords() { - polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * polyVertexCount); - float temp[] = new float[2 * polyVertexCount]; PApplet.arrayCopy(polyTexcoords, 0, temp, 0, 2 * polyVertexCount); polyTexcoords = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyTexcoordsBuffer = PGL.allocateDirectFloatBuffer(2 * polyVertexCount); + } else { + polyTexcoordsBuffer = FloatBuffer.wrap(polyTexcoords); + } } void trimPolyAmbient() { - polyAmbientBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); - int temp[] = new int[polyVertexCount]; PApplet.arrayCopy(polyAmbient, 0, temp, 0, polyVertexCount); polyAmbient = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyAmbientBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); + } else { + polyAmbientBuffer = IntBuffer.wrap(polyAmbient); + } } void trimPolySpecular() { - polySpecularBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); - int temp[] = new int[polyVertexCount]; PApplet.arrayCopy(polySpecular, 0, temp, 0, polyVertexCount); polySpecular = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polySpecularBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); + } else { + polySpecularBuffer = IntBuffer.wrap(polySpecular); + } } void trimPolyEmissive() { - polyEmissiveBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); - int temp[] = new int[polyVertexCount]; PApplet.arrayCopy(polyEmissive, 0, temp, 0, polyVertexCount); polyEmissive = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyEmissiveBuffer = PGL.allocateDirectIntBuffer(polyVertexCount); + } else { + polyEmissiveBuffer = IntBuffer.wrap(polyEmissive); + } } void trimPolyShininess() { - polyShininessBuffer = PGL.allocateDirectFloatBuffer(polyVertexCount); - float temp[] = new float[polyVertexCount]; PApplet.arrayCopy(polyShininess, 0, temp, 0, polyVertexCount); polyShininess = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyShininessBuffer = PGL.allocateDirectFloatBuffer(polyVertexCount); + } else { + polyShininessBuffer = FloatBuffer.wrap(polyShininess); + } } void trimPolyIndices() { - polyIndicesBuffer = PGL.allocateDirectShortBuffer(polyIndexCount); - short temp[] = new short[polyIndexCount]; PApplet.arrayCopy(polyIndices, 0, temp, 0, polyIndexCount); polyIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + polyIndicesBuffer = PGL.allocateDirectShortBuffer(polyIndexCount); + } else { + polyIndicesBuffer = ShortBuffer.wrap(polyIndices); + } } void trimLineVertices() { - lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * lineVertexCount); - float temp[] = new float[4 * lineVertexCount]; PApplet.arrayCopy(lineVertices, 0, temp, 0, 4 * lineVertexCount); lineVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * lineVertexCount); + } else { + lineVerticesBuffer = FloatBuffer.wrap(lineVertices); + } } void trimLineColors() { - lineColorsBuffer = PGL.allocateDirectIntBuffer(lineVertexCount); - int temp[] = new int[lineVertexCount]; PApplet.arrayCopy(lineColors, 0, temp, 0, lineVertexCount); lineColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineColorsBuffer = PGL.allocateDirectIntBuffer(lineVertexCount); + } else { + lineColorsBuffer = IntBuffer.wrap(lineColors); + } } void trimLineAttribs() { - lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * lineVertexCount); - float temp[] = new float[4 * lineVertexCount]; PApplet.arrayCopy(lineAttribs, 0, temp, 0, 4 * lineVertexCount); lineAttribs = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineAttribsBuffer = PGL.allocateDirectFloatBuffer(4 * lineVertexCount); + } else { + lineAttribsBuffer = FloatBuffer.wrap(lineAttribs); + } } void trimLineIndices() { - lineIndicesBuffer = PGL.allocateDirectShortBuffer(lineIndexCount); - short temp[] = new short[lineIndexCount]; PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); lineIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + lineIndicesBuffer = PGL.allocateDirectShortBuffer(lineIndexCount); + } else { + lineIndicesBuffer = ShortBuffer.wrap(lineIndices); + } } void trimPointVertices() { - pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * pointVertexCount); - float temp[] = new float[4 * pointVertexCount]; PApplet.arrayCopy(pointVertices, 0, temp, 0, 4 * pointVertexCount); pointVertices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointVerticesBuffer = PGL.allocateDirectFloatBuffer(4 * pointVertexCount); + } else { + pointVerticesBuffer = FloatBuffer.wrap(pointVertices); + } } void trimPointColors() { - pointColorsBuffer = PGL.allocateDirectIntBuffer(pointVertexCount); - int temp[] = new int[pointVertexCount]; PApplet.arrayCopy(pointColors, 0, temp, 0, pointVertexCount); pointColors = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointColorsBuffer = PGL.allocateDirectIntBuffer(pointVertexCount); + } else { + pointColorsBuffer = IntBuffer.wrap(pointColors); + } } void trimPointAttribs() { - pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * pointVertexCount); - float temp[] = new float[2 * pointVertexCount]; PApplet.arrayCopy(pointAttribs, 0, temp, 0, 2 * pointVertexCount); pointAttribs = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointAttribsBuffer = PGL.allocateDirectFloatBuffer(2 * pointVertexCount); + } else { + pointAttribsBuffer = FloatBuffer.wrap(pointAttribs); + } } void trimPointIndices() { - pointIndicesBuffer = PGL.allocateDirectShortBuffer(pointIndexCount); - short temp[] = new short[pointIndexCount]; PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); pointIndices = temp; + + if (PGL.USE_DIRECT_VERTEX_BUFFERS) { + pointIndicesBuffer = PGL.allocateDirectShortBuffer(pointIndexCount); + } else { + pointIndicesBuffer = ShortBuffer.wrap(pointIndices); + } } // ----------------------------------------------------------------- diff --git a/android/core/src/processing/opengl/Texture.java b/android/core/src/processing/opengl/Texture.java index 0b38e6192..d223025e8 100644 --- a/android/core/src/processing/opengl/Texture.java +++ b/android/core/src/processing/opengl/Texture.java @@ -207,7 +207,38 @@ public class Texture implements PConstants { public void init(int width, int height, Parameters params) { setParameters(params); setSize(width, height); - allocate(); + //allocate(); + } + + + /** + * Initializes the texture using GL parameters + */ + public void init(int width, int height, + int glName, int glTarget, int glFormat, + int glWidth, int glHeight, + int glMinFilter, int glMagFilter, + int glWrapS, int glWrapT) { + this.width = width; + this.height = height; + + 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; + + maxTexcoordU = (float)width / glWidth; + maxTexcoordV = (float)height / glHeight; + + usingMipmaps = glMinFilter == PGL.LINEAR_MIPMAP_NEAREST || + glMinFilter == PGL.LINEAR_MIPMAP_LINEAR; + + usingRepeat = glWrapS == PGL.REPEAT || glWrapT == PGL.REPEAT; } @@ -239,31 +270,6 @@ 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; - } ////////////////////////////////////////////////////////////