diff --git a/core/src/processing/opengl/FontTexture.java b/core/src/processing/opengl/FontTexture.java index d6e2beb73..950e2f0c0 100644 --- a/core/src/processing/opengl/FontTexture.java +++ b/core/src/processing/opengl/FontTexture.java @@ -264,9 +264,10 @@ class FontTexture implements PConstants { } if (outdated) { for (int i = 0; i < textures.length; i++) { - PGraphicsOpenGL.removeTextureObject(textures[i].glName, - textures[i].context); - textures[i].glName = 0; + textures[i].dispose(); +// PGraphicsOpenGL.removeTextureObject(textures[i].glName, +// textures[i].context); +// textures[i].glName = 0; } } return outdated; diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index be9a90236..0ba4c01d1 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -25,6 +25,8 @@ package processing.opengl; import processing.core.*; import java.awt.Font; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; import java.net.URL; import java.nio.*; import java.util.*; @@ -72,29 +74,46 @@ public class PGraphicsOpenGL extends PGraphics { // VBOs for immediate rendering: - public int glPolyVertex; - public int glPolyColor; - public int glPolyNormal; - public int glPolyTexcoord; - public int glPolyAmbient; - public int glPolySpecular; - public int glPolyEmissive; - public int glPolyShininess; - public int glPolyIndex; + protected VertexBuffer bufPolyVertex; + protected VertexBuffer bufPolyColor; + protected VertexBuffer bufPolyNormal; + protected VertexBuffer bufPolyTexcoord; + protected VertexBuffer bufPolyAmbient; + protected VertexBuffer bufPolySpecular; + protected VertexBuffer bufPolyEmissive; + protected VertexBuffer bufPolyShininess; + protected VertexBuffer bufPolyIndex; +// public int glPolyVertex; +// public int glPolyColor; +// public int glPolyNormal; +// public int glPolyTexcoord; +// public int glPolyAmbient; +// public int glPolySpecular; +// public int glPolyEmissive; +// public int glPolyShininess; +// public int glPolyIndex; protected boolean polyBuffersCreated = false; protected int polyBuffersContext; - public int glLineVertex; - public int glLineColor; - public int glLineAttrib; - public int glLineIndex; + protected VertexBuffer bufLineVertex; + protected VertexBuffer bufLineColor; + protected VertexBuffer bufLineAttrib; + protected VertexBuffer bufLineIndex; +// public int glLineVertex; +// public int glLineColor; +// public int glLineAttrib; +// public int glLineIndex; protected boolean lineBuffersCreated = false; protected int lineBuffersContext; - public int glPointVertex; - public int glPointColor; - public int glPointAttrib; - public int glPointIndex; + protected VertexBuffer bufPointVertex; + protected VertexBuffer bufPointColor; + protected VertexBuffer bufPointAttrib; + protected VertexBuffer bufPointIndex; +// public int glPointVertex; +// public int glPointColor; +// public int glPointAttrib; +// public int glPointIndex; protected boolean pointBuffersCreated = false; protected int pointBuffersContext; @@ -136,20 +155,20 @@ public class PGraphicsOpenGL extends PGraphics { // GL resources: - static protected HashMap glTextureObjects = - new HashMap(); - static protected HashMap glVertexBuffers = - new HashMap(); +// static protected HashMap glTextureObjects = +// new HashMap(); +// static protected HashMap glVertexBuffers = +// new HashMap(); static protected HashMap glFrameBuffers = new HashMap(); static protected HashMap glRenderBuffers = new HashMap(); - static protected HashMap glslPrograms = - new HashMap(); - static protected HashMap glslVertexShaders = - new HashMap(); - static protected HashMap glslFragmentShaders = - new HashMap(); +// static protected HashMap glslPrograms = +// new HashMap(); +// static protected HashMap glslVertexShaders = +// new HashMap(); +// static protected HashMap glslFragmentShaders = +// new HashMap(); // ........................................................ @@ -623,9 +642,9 @@ public class PGraphicsOpenGL extends PGraphics { // pgl.swapBuffers(); } - finalizePolyBuffers(); - finalizeLineBuffers(); - finalizePointBuffers(); +// finalizePolyBuffers(); +// finalizeLineBuffers(); +// finalizePointBuffers(); deleteSurfaceTextures(); if (primaryGraphics) { @@ -646,28 +665,28 @@ public class PGraphicsOpenGL extends PGraphics { } } - @Override - protected void finalize() throws Throwable { - try { - finalizePolyBuffers(); - finalizeLineBuffers(); - finalizePointBuffers(); - - deleteSurfaceTextures(); - if (!primaryGraphics) { - if (offscreenFramebuffer != null) { - offscreenFramebuffer.dispose(); - offscreenFramebuffer = null; - } - if (multisampleFramebuffer != null) { - multisampleFramebuffer.dispose(); - multisampleFramebuffer = null; - } - } - } finally { - super.finalize(); - } - } +// @Override +// protected void finalize() throws Throwable { +// try { +// finalizePolyBuffers(); +// finalizeLineBuffers(); +// finalizePointBuffers(); +// +// deleteSurfaceTextures(); +// if (!primaryGraphics) { +// if (offscreenFramebuffer != null) { +// offscreenFramebuffer.dispose(); +// offscreenFramebuffer = null; +// } +// if (multisampleFramebuffer != null) { +// multisampleFramebuffer.dispose(); +// multisampleFramebuffer = null; +// } +// } +// } finally { +// super.finalize(); +// } +// } protected void setFlushMode(int mode) { flushMode = mode; @@ -738,6 +757,254 @@ public class PGraphicsOpenGL extends PGraphics { ////////////////////////////////////////////////////////////// // RESOURCE HANDLING + // Using the technique alternative to finalization described in: + // http://www.oracle.com/technetwork/articles/java/finalization-137655.html + + static final private int MAX_DRAIN_GLRES_ITERATIONS = 10; + + protected static class GLResourceTexture extends WeakReference { + int glName; + + private PGL pgl; + private int context; + + static private ReferenceQueue refQueue = new ReferenceQueue(); + static private List refList = new ArrayList(); + + static void drainRefQueueBounded() { + ReferenceQueue refQueue = GLResourceTexture.referenceQueue(); + int iterations = 0; + while (iterations < MAX_DRAIN_GLRES_ITERATIONS) { + GLResourceTexture res = (GLResourceTexture)refQueue.poll(); + if (res == null) { + break; + } + System.out.println("Disposing texture resource " + iterations + " " + res.hashCode()); + res.dispose(); + ++iterations; + } + } + + static ReferenceQueue referenceQueue() { + return refQueue; + } + + public GLResourceTexture(Texture tex) { + super(tex, refQueue); + + drainRefQueueBounded(); + + this.pgl = tex.pgl; + pgl.genTextures(1, intBuffer); + tex.glName = intBuffer.get(0); + + this.glName = tex.glName; + + this.context = tex.context; + + refList.add(this); + } + + private void disposeNative() { + if (pgl != null) { + if (glName != 0) { + intBuffer.put(0, glName); + pgl.deleteTextures(1, intBuffer); + glName = 0; + } + pgl = null; + } + } + + void dispose() { + refList.remove(this); + disposeNative(); + } + + @Override + public boolean equals(Object obj) { + GLResourceTexture other = (GLResourceTexture)obj; + return other.glName == glName && + other.context == context; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + glName; + result = 31 * result + context; + return result; + } + } + + + protected static class GLResourceVertexBuffer extends WeakReference { + int glId; + + private PGL pgl; + private int context; + + static private ReferenceQueue refQueue = new ReferenceQueue(); + static private List refList = new ArrayList(); + + static void drainRefQueueBounded() { + ReferenceQueue refQueue = GLResourceVertexBuffer.referenceQueue(); + int iterations = 0; + while (iterations < MAX_DRAIN_GLRES_ITERATIONS) { + GLResourceVertexBuffer res = (GLResourceVertexBuffer)refQueue.poll(); + if (res == null) { + break; + } + System.out.println("Disposing VertexBuffer resource " + iterations + " " + res.hashCode()); + res.dispose(); + ++iterations; + } + } + + static ReferenceQueue referenceQueue() { + return refQueue; + } + + public GLResourceVertexBuffer(VertexBuffer vbo) { + super(vbo, refQueue); + + drainRefQueueBounded(); + + this.pgl = vbo.pgl; + pgl.genBuffers(1, intBuffer); + vbo.glId = intBuffer.get(0); + + this.glId = vbo.glId; + + this.context = vbo.context; + + refList.add(this); + } + + private void disposeNative() { + if (pgl != null) { + if (glId != 0) { + intBuffer.put(0, glId); + pgl.deleteBuffers(1, intBuffer); + glId = 0; + } + pgl = null; + } + } + + void dispose() { + refList.remove(this); + disposeNative(); + } + + @Override + public boolean equals(Object obj) { + GLResourceTexture other = (GLResourceTexture)obj; + return other.glName == glId && + other.context == context; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + glId; + result = 31 * result + context; + return result; + } + } + + + protected static class GLResourceShader extends WeakReference { + int glProgram; + int glVertex; + int glFragment; + + private PGL pgl; + private int context; + + static private ReferenceQueue refQueue = new ReferenceQueue(); + static private List refList = new ArrayList(); + + static void drainRefQueueBounded() { + ReferenceQueue refQueue = GLResourceShader.referenceQueue(); + int iterations = 0; + while (iterations < MAX_DRAIN_GLRES_ITERATIONS) { + GLResourceShader res = (GLResourceShader)refQueue.poll(); + if (res == null) { + break; + } + System.out.println("Disposing shader resource " + res.hashCode()); + res.dispose(); + ++iterations; + } + } + + static ReferenceQueue referenceQueue() { + return refQueue; + } + + public GLResourceShader(PShader sh) { + super(sh, refQueue); + + drainRefQueueBounded(); + + this.pgl = sh.pgl; + sh.glProgram = pgl.createProgram(); + sh.glVertex = pgl.createShader(PGL.VERTEX_SHADER); + sh.glFragment = pgl.createShader(PGL.FRAGMENT_SHADER); + + this.glProgram = sh.glProgram; + this.glVertex = sh.glVertex; + this.glFragment = sh.glFragment; + + this.context = sh.context; + + refList.add(this); + } + + private void disposeNative() { + if (pgl != null) { + if (glFragment != 0) { + pgl.deleteShader(glFragment); + glFragment = 0; + } + if (glVertex != 0) { + pgl.deleteShader(glVertex); + glVertex = 0; + } + if (glProgram != 0) { + pgl.deleteProgram(glProgram); + glProgram = 0; + } + pgl = null; + } + } + + void dispose() { + refList.remove(this); + disposeNative(); + } + + @Override + public boolean equals(Object obj) { + GLResourceShader other = (GLResourceShader)obj; + return other.glProgram == glProgram && + other.glVertex == glVertex && + other.glFragment == glFragment && + other.context == context; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + glProgram; + result = 31 * result + glVertex; + result = 31 * result + glFragment; + result = 31 * result + context; + return result; + } + } + // http://www.oracle.com/technetwork/articles/java/finalization-137655.html @@ -768,6 +1035,7 @@ public class PGraphicsOpenGL extends PGraphics { // Texture Objects ----------------------------------------------------------- + /* protected static int createTextureObject(int context, PGL pgl) { deleteFinalizedTextureObjects(pgl); @@ -829,9 +1097,11 @@ public class PGraphicsOpenGL extends PGraphics { glTextureObjects.remove(res); } } +*/ // Vertex Buffer Objects ----------------------------------------------------- + /* protected static int createVertexBufferObject(int context, PGL pgl) { deleteFinalizedVertexBufferObjects(pgl); @@ -893,6 +1163,7 @@ public class PGraphicsOpenGL extends PGraphics { glVertexBuffers.remove(res); } } + */ // FrameBuffer Objects ------------------------------------------------------- @@ -1024,6 +1295,7 @@ public class PGraphicsOpenGL extends PGraphics { } } + /* // GLSL Program Objects ------------------------------------------------------ protected static int createGLSLProgramObject(int context, PGL pgl) { @@ -1205,17 +1477,18 @@ public class PGraphicsOpenGL extends PGraphics { glslFragmentShaders.remove(res); } } + */ // All OpenGL resources ------------------------------------------------------ protected static void deleteFinalizedGLResources(PGL pgl) { - deleteFinalizedTextureObjects(pgl); - deleteFinalizedVertexBufferObjects(pgl); +// deleteFinalizedTextureObjects(pgl); +// deleteFinalizedVertexBufferObjects(pgl); deleteFinalizedFrameBufferObjects(pgl); deleteFinalizedRenderBufferObjects(pgl); - deleteFinalizedGLSLProgramObjects(pgl); - deleteFinalizedGLSLVertShaderObjects(pgl); - deleteFinalizedGLSLFragShaderObjects(pgl); +// deleteFinalizedGLSLProgramObjects(pgl); +// deleteFinalizedGLSLVertShaderObjects(pgl); +// deleteFinalizedGLSLFragShaderObjects(pgl); } @@ -1272,50 +1545,61 @@ public class PGraphicsOpenGL extends PGraphics { if (!polyBuffersCreated || polyBuffersContextIsOutdated()) { polyBuffersContext = pgl.getCurrentContext(); - int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; - int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; - int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; +// int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; +// int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; +// int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - glPolyVertex = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); - pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - - glPolyColor = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glPolyNormal = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); - pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - - glPolyTexcoord = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); - pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); - - glPolyAmbient = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glPolySpecular = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glPolyEmissive = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glPolyShininess = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); - pgl.bufferData(PGL.ARRAY_BUFFER, sizef, null, PGL.STATIC_DRAW); + bufPolyVertex = new VertexBuffer(this, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + bufPolyColor = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyNormal = new VertexBuffer(this, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + bufPolyTexcoord = new VertexBuffer(this, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); + bufPolyAmbient = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolySpecular = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyEmissive = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyShininess = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - - glPolyIndex = createVertexBufferObject(polyBuffersContext, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); - pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); - + bufPolyIndex = new VertexBuffer(this, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); + +// glPolyVertex = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); +// pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); +// +// glPolyColor = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glPolyNormal = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); +// pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); +// +// glPolyTexcoord = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); +// pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); +// +// glPolyAmbient = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glPolySpecular = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glPolyEmissive = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glPolyShininess = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizef, null, PGL.STATIC_DRAW); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); +// glPolyIndex = createVertexBufferObject(polyBuffersContext, pgl); +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); +// pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); + polyBuffersCreated = true; } @@ -1340,47 +1624,47 @@ public class PGraphicsOpenGL extends PGraphics { int sizei = size * PGL.SIZEOF_INT; tessGeo.updatePolyVerticesBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.polyVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyColorsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyColorsBuffer, PGL.STATIC_DRAW); if (lit) { tessGeo.updatePolyAmbientBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyAmbient.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyAmbientBuffer, PGL.STATIC_DRAW); tessGeo.updatePolySpecularBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolySpecular.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polySpecularBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyEmissiveBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyEmissive.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyEmissiveBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyShininessBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyShininess.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizef, tessGeo.polyShininessBuffer, PGL.STATIC_DRAW); } if (lit || needNormals) { tessGeo.updatePolyNormalsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyNormal.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, tessGeo.polyNormalsBuffer, PGL.STATIC_DRAW); } if (tex || needTexCoords) { tessGeo.updatePolyTexCoordsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyTexcoord.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.polyTexCoordsBuffer, PGL.STATIC_DRAW); } @@ -1388,13 +1672,13 @@ public class PGraphicsOpenGL extends PGraphics { for (String name: polyAttribs.keySet()) { VertexAttribute attrib = polyAttribs.get(name); tessGeo.updateAttribBuffer(name); - pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.glName); + pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.buf.glId); pgl.bufferData(PGL.ARRAY_BUFFER, attrib.sizeInBytes(size), tessGeo.polyAttribBuffers.get(name), PGL.STATIC_DRAW); } tessGeo.updatePolyIndicesBuffer(); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPolyIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, tessGeo.polyIndicesBuffer, PGL.STATIC_DRAW); @@ -1412,93 +1696,99 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void finalizePolyBuffers() { - if (glPolyVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyVertex, polyBuffersContext); - glPolyVertex = 0; - } - - if (glPolyColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyColor, polyBuffersContext); - glPolyColor = 0; - } - - if (glPolyNormal != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyNormal, polyBuffersContext); - glPolyNormal = 0; - } - - if (glPolyTexcoord != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyTexcoord, polyBuffersContext); - glPolyTexcoord = 0; - } - - if (glPolyAmbient != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyAmbient, polyBuffersContext); - glPolyAmbient = 0; - } - - if (glPolySpecular != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolySpecular, polyBuffersContext); - glPolySpecular = 0; - } - - if (glPolyEmissive != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyEmissive, polyBuffersContext); - glPolyEmissive = 0; - } - - if (glPolyShininess != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyShininess, polyBuffersContext); - glPolyShininess = 0; - } - - for (String name: polyAttribs.keySet()) { - VertexAttribute attrib = polyAttribs.get(name); - if (attrib.glName != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(attrib.glName, polyBuffersContext); - attrib.glName = 0; - } - } - - if (glPolyIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyIndex, polyBuffersContext); - glPolyIndex = 0; - } - - polyBuffersCreated = false; - } +// protected void finalizePolyBuffers() { +// if (glPolyVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyVertex, polyBuffersContext); +// glPolyVertex = 0; +// } +// +// if (glPolyColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyColor, polyBuffersContext); +// glPolyColor = 0; +// } +// +// if (glPolyNormal != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyNormal, polyBuffersContext); +// glPolyNormal = 0; +// } +// +// if (glPolyTexcoord != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyTexcoord, polyBuffersContext); +// glPolyTexcoord = 0; +// } +// +// if (glPolyAmbient != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyAmbient, polyBuffersContext); +// glPolyAmbient = 0; +// } +// +// if (glPolySpecular != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolySpecular, polyBuffersContext); +// glPolySpecular = 0; +// } +// +// if (glPolyEmissive != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyEmissive, polyBuffersContext); +// glPolyEmissive = 0; +// } +// +// if (glPolyShininess != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyShininess, polyBuffersContext); +// glPolyShininess = 0; +// } +// +// for (String name: polyAttribs.keySet()) { +// VertexAttribute attrib = polyAttribs.get(name); +// if (attrib.glName != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(attrib.glName, polyBuffersContext); +// attrib.glName = 0; +// } +// } +// +// if (glPolyIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyIndex, polyBuffersContext); +// glPolyIndex = 0; +// } +// +// polyBuffersCreated = false; +// } protected void createLineBuffers() { if (!lineBuffersCreated || lineBufferContextIsOutdated()) { lineBuffersContext = pgl.getCurrentContext(); - int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; - int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; - int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - - glLineVertex = createVertexBufferObject(lineBuffersContext, pgl); - - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); - pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - - glLineColor = createVertexBufferObject(lineBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glLineAttrib = createVertexBufferObject(lineBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); - pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, null, PGL.STATIC_DRAW); +// int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; +// int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; +// int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; + bufLineVertex = new VertexBuffer(this, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + bufLineColor = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufLineAttrib = new VertexBuffer(this, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - - glLineIndex = createVertexBufferObject(lineBuffersContext, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); - pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); - + bufLineIndex = new VertexBuffer(this, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); +// glLineVertex = createVertexBufferObject(lineBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); +// pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); +// +// glLineColor = createVertexBufferObject(lineBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glLineAttrib = createVertexBufferObject(lineBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); +// pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, null, PGL.STATIC_DRAW); +// +// pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); +// +// glLineIndex = createVertexBufferObject(lineBuffersContext, pgl); +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); +// pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); +// +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); + lineBuffersCreated = true; } } @@ -1511,23 +1801,25 @@ public class PGraphicsOpenGL extends PGraphics { int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; + + tessGeo.updateLineVerticesBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updateLineColorsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.lineColorsBuffer, PGL.STATIC_DRAW); tessGeo.updateLineDirectionsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineDirectionsBuffer, PGL.STATIC_DRAW); tessGeo.updateLineIndicesBuffer(); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufLineIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.lineIndexCount * PGL.SIZEOF_INDEX, tessGeo.lineIndicesBuffer, PGL.STATIC_DRAW); @@ -1545,59 +1837,66 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void finalizeLineBuffers() { - if (glLineVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineVertex, lineBuffersContext); - glLineVertex = 0; - } - - if (glLineColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineColor, lineBuffersContext); - glLineColor = 0; - } - - if (glLineAttrib != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineAttrib, lineBuffersContext); - glLineAttrib = 0; - } - - if (glLineIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineIndex, lineBuffersContext); - glLineIndex = 0; - } - - lineBuffersCreated = false; - } +// protected void finalizeLineBuffers() { +// if (glLineVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineVertex, lineBuffersContext); +// glLineVertex = 0; +// } +// +// if (glLineColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineColor, lineBuffersContext); +// glLineColor = 0; +// } +// +// if (glLineAttrib != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineAttrib, lineBuffersContext); +// glLineAttrib = 0; +// } +// +// if (glLineIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineIndex, lineBuffersContext); +// glLineIndex = 0; +// } +// +// lineBuffersCreated = false; +// } protected void createPointBuffers() { if (!pointBuffersCreated || pointBuffersContextIsOutdated()) { pointBuffersContext = pgl.getCurrentContext(); - int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; - int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; - int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - - glPointVertex = createVertexBufferObject(pointBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); - pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - - glPointColor = createVertexBufferObject(pointBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); - pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - - glPointAttrib = createVertexBufferObject(pointBuffersContext, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); - pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); +// int sizef = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_FLOAT; +// int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; +// int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; + bufPointVertex = new VertexBuffer(this, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + bufPointColor = new VertexBuffer(this, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPointAttrib = new VertexBuffer(this, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - - glPointIndex = createVertexBufferObject(pointBuffersContext, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); - pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); - + bufPointIndex = new VertexBuffer(this, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); +// glPointVertex = createVertexBufferObject(pointBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); +// pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); +// +// glPointColor = createVertexBufferObject(pointBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); +// pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); +// +// glPointAttrib = createVertexBufferObject(pointBuffersContext, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); +// pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); +// +// pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); +// +// glPointIndex = createVertexBufferObject(pointBuffersContext, pgl); +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); +// pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); +// +// pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); + pointBuffersCreated = true; } } @@ -1611,22 +1910,22 @@ public class PGraphicsOpenGL extends PGraphics { int sizei = size * PGL.SIZEOF_INT; tessGeo.updatePointVerticesBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.pointVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updatePointColorsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.pointColorsBuffer, PGL.STATIC_DRAW); tessGeo.updatePointOffsetsBuffer(); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.pointOffsetsBuffer, PGL.STATIC_DRAW); tessGeo.updatePointIndicesBuffer(); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPointIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.pointIndexCount * PGL.SIZEOF_INDEX, tessGeo.pointIndicesBuffer, PGL.STATIC_DRAW); @@ -1644,29 +1943,29 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void finalizePointBuffers() { - if (glPointVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointVertex, pointBuffersContext); - glPointVertex = 0; - } - - if (glPointColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointColor, pointBuffersContext); - glPointColor = 0; - } - - if (glPointAttrib != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointAttrib, pointBuffersContext); - glPointAttrib = 0; - } - - if (glPointIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointIndex, pointBuffersContext); - glPointIndex = 0; - } - - pointBuffersCreated = false; - } +// protected void finalizePointBuffers() { +// if (glPointVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointVertex, pointBuffersContext); +// glPointVertex = 0; +// } +// +// if (glPointColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointColor, pointBuffersContext); +// glPointColor = 0; +// } +// +// if (glPointAttrib != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointAttrib, pointBuffersContext); +// glPointAttrib = 0; +// } +// +// if (glPointIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointIndex, pointBuffersContext); +// glPointIndex = 0; +// } +// +// pointBuffersCreated = false; +// } // @Override @@ -2354,7 +2653,7 @@ public class PGraphicsOpenGL extends PGraphics { } VertexAttribute attrib = polyAttribs.get(name); if (attrib == null) { - attrib = new VertexAttribute(name, type, size); + attrib = new VertexAttribute(this, name, type, size); polyAttribs.put(name, attrib); inGeo.initAttrib(attrib); tessGeo.initAttrib(attrib); @@ -2626,31 +2925,31 @@ public class PGraphicsOpenGL extends PGraphics { cache.indexOffset[n] + cache.indexCount[n] - ioffset; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(glPolyVertex, 4, PGL.FLOAT, 0, + shader.setVertexAttribute(bufPolyVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(glPolyColor, 4, PGL.UNSIGNED_BYTE, 0, + shader.setColorAttribute(bufPolyColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); if (lights) { - shader.setNormalAttribute(glPolyNormal, 3, PGL.FLOAT, 0, + shader.setNormalAttribute(bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); - shader.setAmbientAttribute(glPolyAmbient, 4, PGL.UNSIGNED_BYTE, 0, + shader.setAmbientAttribute(bufPolyAmbient.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setSpecularAttribute(glPolySpecular, 4, PGL.UNSIGNED_BYTE, 0, + shader.setSpecularAttribute(bufPolySpecular.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setEmissiveAttribute(glPolyEmissive, 4, PGL.UNSIGNED_BYTE, 0, + shader.setEmissiveAttribute(bufPolyEmissive.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setShininessAttribute(glPolyShininess, 1, PGL.FLOAT, 0, + shader.setShininessAttribute(bufPolyShininess.glId, 1, PGL.FLOAT, 0, voffset * PGL.SIZEOF_FLOAT); } if (lights || needNormals) { - shader.setNormalAttribute(glPolyNormal, 3, PGL.FLOAT, 0, + shader.setNormalAttribute(bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); } if (tex != null || needTexCoords) { - shader.setTexcoordAttribute(glPolyTexcoord, 2, PGL.FLOAT, 0, + shader.setTexcoordAttribute(bufPolyTexcoord.glId, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); shader.setTexture(tex); } @@ -2658,12 +2957,12 @@ public class PGraphicsOpenGL extends PGraphics { for (VertexAttribute attrib: polyAttribs.values()) { if (!attrib.active(shader)) continue; attrib.bind(pgl); - shader.setAttributeVBO(attrib.glLoc, attrib.glName, + shader.setAttributeVBO(attrib.glLoc, attrib.buf.glId, attrib.tessSize, attrib.type, attrib.isColor(), 0, attrib.sizeInBytes(voffset)); } - shader.draw(glPolyIndex, icount, ioffset); + shader.draw(bufPolyIndex.glId, icount, ioffset); } for (VertexAttribute attrib: polyAttribs.values()) { @@ -2699,31 +2998,31 @@ public class PGraphicsOpenGL extends PGraphics { int ioffset = 3*ti; int icount = 3; - shader.setVertexAttribute(glPolyVertex, 4, PGL.FLOAT, 0, + shader.setVertexAttribute(bufPolyVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(glPolyColor, 4, PGL.UNSIGNED_BYTE, 0, + shader.setColorAttribute(bufPolyColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); if (lights) { - shader.setNormalAttribute(glPolyNormal, 3, PGL.FLOAT, 0, + shader.setNormalAttribute(bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); - shader.setAmbientAttribute(glPolyAmbient, 4, PGL.UNSIGNED_BYTE, 0, + shader.setAmbientAttribute(bufPolyAmbient.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setSpecularAttribute(glPolySpecular, 4, PGL.UNSIGNED_BYTE, 0, + shader.setSpecularAttribute(bufPolySpecular.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setEmissiveAttribute(glPolyEmissive, 4, PGL.UNSIGNED_BYTE, 0, + shader.setEmissiveAttribute(bufPolyEmissive.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setShininessAttribute(glPolyShininess, 1, PGL.FLOAT, 0, + shader.setShininessAttribute(bufPolyShininess.glId, 1, PGL.FLOAT, 0, voffset * PGL.SIZEOF_FLOAT); } if (lights || needNormals) { - shader.setNormalAttribute(glPolyNormal, 3, PGL.FLOAT, 0, + shader.setNormalAttribute(bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); } if (tex != null || needTexCoords) { - shader.setTexcoordAttribute(glPolyTexcoord, 2, PGL.FLOAT, 0, + shader.setTexcoordAttribute(bufPolyTexcoord.glId, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); shader.setTexture(tex); } @@ -2731,12 +3030,12 @@ public class PGraphicsOpenGL extends PGraphics { for (VertexAttribute attrib: polyAttribs.values()) { if (!attrib.active(shader)) continue; attrib.bind(pgl); - shader.setAttributeVBO(attrib.glLoc, attrib.glName, + shader.setAttributeVBO(attrib.glLoc, attrib.buf.glId, attrib.tessSize, attrib.type, attrib.isColor(), 0, attrib.sizeInBytes(voffset)); } - shader.draw(glPolyIndex, icount, ioffset); + shader.draw(bufPolyIndex.glId, icount, ioffset); for (VertexAttribute attrib: polyAttribs.values()) { if (attrib.active(shader)) attrib.unbind(pgl); @@ -2965,14 +3264,14 @@ public class PGraphicsOpenGL extends PGraphics { int icount = cache.indexCount[n]; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(glLineVertex, 4, PGL.FLOAT, 0, + shader.setVertexAttribute(bufLineVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(glLineColor, 4, PGL.UNSIGNED_BYTE, 0, + shader.setColorAttribute(bufLineColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setLineAttribute(glLineAttrib, 4, PGL.FLOAT, 0, + shader.setLineAttribute(bufLineAttrib.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.draw(glLineIndex, icount, ioffset); + shader.draw(bufLineIndex.glId, icount, ioffset); } shader.unbind(); @@ -3066,14 +3365,14 @@ public class PGraphicsOpenGL extends PGraphics { int icount = cache.indexCount[n]; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(glPointVertex, 4, PGL.FLOAT, 0, + shader.setVertexAttribute(bufPointVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(glPointColor, 4, PGL.UNSIGNED_BYTE, 0, + shader.setColorAttribute(bufPointColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setPointAttribute(glPointAttrib, 2, PGL.FLOAT, 0, + shader.setPointAttribute(bufPointAttrib.glId, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); - shader.draw(glPointIndex, icount, ioffset); + shader.draw(bufPointIndex.glId, icount, ioffset); } shader.unbind(); @@ -7079,13 +7378,14 @@ public class PGraphicsOpenGL extends PGraphics { static final int COLOR = 2; static final int OTHER = 3; + PGraphicsOpenGL pg; String name; int kind; // POSITION, NORMAL, COLOR, OTHER int type; // GL_INT, GL_FLOAT, GL_BOOL int size; // number of elements (1, 2, 3, or 4) int tessSize; int elementSize; - int glName; + VertexBuffer buf; int glLoc; float[] fvalues; @@ -7098,7 +7398,8 @@ public class PGraphicsOpenGL extends PGraphics { int lastModified; boolean active; - VertexAttribute(String name, int type, int size) { + VertexAttribute(PGraphicsOpenGL pg, String name, int type, int size) { + this.pg = pg; this.name = name; this.type = type; this.size = size; @@ -7127,7 +7428,7 @@ public class PGraphicsOpenGL extends PGraphics { bvalues = new byte[size]; } - glName = 0; + buf = null; glLoc = -1; modified = false; @@ -7175,21 +7476,25 @@ public class PGraphicsOpenGL extends PGraphics { } boolean bufferCreated() { - return 0 < glName; + return buf != null && 0 < buf.glId; } void createBuffer(PGL pgl) { - int ctx = pgl.getCurrentContext(); - glName = createVertexBufferObject(ctx, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glName); - pgl.bufferData(PGL.ARRAY_BUFFER, size * INIT_VERTEX_BUFFER_SIZE * elementSize, - null, PGL.STATIC_DRAW); +// int ctx = pgl.getCurrentContext(); + buf = new VertexBuffer(pg, PGL.ARRAY_BUFFER, size, elementSize, false); +// +// glName = createVertexBufferObject(ctx, pgl); +// pgl.bindBuffer(PGL.ARRAY_BUFFER, glName); +// pgl.bufferData(PGL.ARRAY_BUFFER, size * INIT_VERTEX_BUFFER_SIZE * elementSize, +// null, PGL.STATIC_DRAW); } void deleteBuffer(PGL pgl) { - if (glName != 0) { - int ctx = pgl.getCurrentContext(); - PGraphicsOpenGL.deleteVertexBufferObject(glName, ctx, pgl); + if (buf.glId != 0) { +// int ctx = pgl.getCurrentContext(); + intBuffer.put(0, buf.glId); + if (pgl.threadIsCurrent()) pgl.deleteBuffers(1, intBuffer); +// PGraphicsOpenGL.deleteVertexBufferObject(buf.glId, ctx, pgl); } } diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index 03a5f6c4f..25dc248a8 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -24,6 +24,7 @@ package processing.opengl; import processing.core.*; +import processing.opengl.PGraphicsOpenGL.GLResourceShader; import java.net.URL; import java.nio.FloatBuffer; @@ -88,6 +89,7 @@ public class PShader implements PConstants { public int glProgram; public int glVertex; public int glFragment; + private GLResourceShader glres; protected URL vertexURL; protected URL fragmentURL; @@ -303,22 +305,22 @@ public class PShader implements PConstants { } - @Override - protected void finalize() throws Throwable { - try { - if (glVertex != 0) { - PGraphicsOpenGL.finalizeGLSLVertShaderObject(glVertex, context); - } - if (glFragment != 0) { - PGraphicsOpenGL.finalizeGLSLFragShaderObject(glFragment, context); - } - if (glProgram != 0) { - PGraphicsOpenGL.finalizeGLSLProgramObject(glProgram, context); - } - } finally { - super.finalize(); - } - } +// @Override +// protected void finalize() throws Throwable { +// try { +// if (glVertex != 0) { +// PGraphicsOpenGL.finalizeGLSLVertShaderObject(glVertex, context); +// } +// if (glFragment != 0) { +// PGraphicsOpenGL.finalizeGLSLFragShaderObject(glFragment, context); +// } +// if (glProgram != 0) { +// PGraphicsOpenGL.finalizeGLSLProgramObject(glProgram, context); +// } +// } finally { +// super.finalize(); +// } +// } public void setVertexShader(String vertFilename) { @@ -917,7 +919,11 @@ public class PShader implements PConstants { protected void create() { context = pgl.getCurrentContext(); - glProgram = PGraphicsOpenGL.createGLSLProgramObject(context, pgl); + glres = new GLResourceShader(this); + +// glProgram = PGraphicsOpenGL.createGLSLProgramObject(context, pgl); +// glVertex = PGraphicsOpenGL.createGLSLVertShaderObject(context, pgl); +// glFragment = PGraphicsOpenGL.createGLSLFragShaderObject(context, pgl); } @@ -961,13 +967,10 @@ public class PShader implements PConstants { protected boolean contextIsOutdated() { boolean outdated = !pgl.contextIsCurrent(context); if (outdated) { - PGraphicsOpenGL.removeGLSLProgramObject(glProgram, context); - PGraphicsOpenGL.removeGLSLVertShaderObject(glVertex, context); - PGraphicsOpenGL.removeGLSLFragShaderObject(glFragment, context); - - glProgram = 0; - glVertex = 0; - glFragment = 0; +// PGraphicsOpenGL.removeGLSLProgramObject(glProgram, context); +// PGraphicsOpenGL.removeGLSLVertShaderObject(glVertex, context); +// PGraphicsOpenGL.removeGLSLFragShaderObject(glFragment, context); + dispose(); } return outdated; } @@ -988,7 +991,7 @@ public class PShader implements PConstants { * @param shaderSource a string containing the shader's code */ protected boolean compileVertexShader() { - glVertex = PGraphicsOpenGL.createGLSLVertShaderObject(context, pgl); +// glVertex = PGraphicsOpenGL.createGLSLVertShaderObject(context, pgl); pgl.shaderSource(glVertex, PApplet.join(vertexShaderSource, "\n")); pgl.compileShader(glVertex); @@ -1009,7 +1012,7 @@ public class PShader implements PConstants { * @param shaderSource a string containing the shader's code */ protected boolean compileFragmentShader() { - glFragment = PGraphicsOpenGL.createGLSLFragShaderObject(context, pgl); +// glFragment = PGraphicsOpenGL.createGLSLFragShaderObject(context, pgl); pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n")); pgl.compileShader(glFragment); @@ -1027,18 +1030,25 @@ public class PShader implements PConstants { protected void dispose() { - if (glVertex != 0) { - PGraphicsOpenGL.deleteGLSLVertShaderObject(glVertex, context, pgl); + if (glres != null) { + glres.dispose(); glVertex = 0; - } - if (glFragment != 0) { - PGraphicsOpenGL.deleteGLSLFragShaderObject(glFragment, context, pgl); glFragment = 0; - } - if (glProgram != 0) { - PGraphicsOpenGL.deleteGLSLProgramObject(glProgram, context, pgl); glProgram = 0; + glres = null; } +// if (glVertex != 0) { +// PGraphicsOpenGL.deleteGLSLVertShaderObject(glVertex, context, pgl); +// glVertex = 0; +// } +// if (glFragment != 0) { +// PGraphicsOpenGL.deleteGLSLFragShaderObject(glFragment, context, pgl); +// glFragment = 0; +// } +// if (glProgram != 0) { +// PGraphicsOpenGL.deleteGLSLProgramObject(glProgram, context, pgl); +// glProgram = 0; +// } } diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index 7d506e8fa..f3c130383 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -100,25 +100,42 @@ public class PShapeOpenGL extends PShape { // OpenGL buffers - public int glPolyVertex; - public int glPolyColor; - public int glPolyNormal; - public int glPolyTexcoord; - public int glPolyAmbient; - public int glPolySpecular; - public int glPolyEmissive; - public int glPolyShininess; - public int glPolyIndex; + protected VertexBuffer bufPolyVertex; + protected VertexBuffer bufPolyColor; + protected VertexBuffer bufPolyNormal; + protected VertexBuffer bufPolyTexcoord; + protected VertexBuffer bufPolyAmbient; + protected VertexBuffer bufPolySpecular; + protected VertexBuffer bufPolyEmissive; + protected VertexBuffer bufPolyShininess; + protected VertexBuffer bufPolyIndex; +// public int glPolyVertex; +// public int glPolyColor; +// public int glPolyNormal; +// public int glPolyTexcoord; +// public int glPolyAmbient; +// public int glPolySpecular; +// public int glPolyEmissive; +// public int glPolyShininess; +// public int glPolyIndex; - public int glLineVertex; - public int glLineColor; - public int glLineAttrib; - public int glLineIndex; + protected VertexBuffer bufLineVertex; + protected VertexBuffer bufLineColor; + protected VertexBuffer bufLineAttrib; + protected VertexBuffer bufLineIndex; +// public int glLineVertex; +// public int glLineColor; +// public int glLineAttrib; +// public int glLineIndex; - public int glPointVertex; - public int glPointColor; - public int glPointAttrib; - public int glPointIndex; + protected VertexBuffer bufPointVertex; + protected VertexBuffer bufPointColor; + protected VertexBuffer bufPointAttrib; + protected VertexBuffer bufPointIndex; +// public int glPointVertex; +// public int glPointColor; +// public int glPointAttrib; +// public int glPointIndex; // Testing this field, not use as it might go away... public int glUsage = PGL.STATIC_DRAW; @@ -312,25 +329,25 @@ public class PShapeOpenGL extends PShape { pgl = pg.pgl; context = pgl.createEmptyContext(); - glPolyVertex = 0; - glPolyColor = 0; - glPolyNormal = 0; - glPolyTexcoord = 0; - glPolyAmbient = 0; - glPolySpecular = 0; - glPolyEmissive = 0; - glPolyShininess = 0; - glPolyIndex = 0; + bufPolyVertex = null; + bufPolyColor = null; + bufPolyNormal = null; + bufPolyTexcoord = null; + bufPolyAmbient = null; + bufPolySpecular = null; + bufPolyEmissive = null; + bufPolyShininess = null; + bufPolyIndex = null; - glLineVertex = 0; - glLineColor = 0; - glLineAttrib = 0; - glLineIndex = 0; + bufLineVertex = null; + bufLineColor = null; + bufLineAttrib = null; + bufLineIndex = null; - glPointVertex = 0; - glPointColor = 0; - glPointAttrib = 0; - glPointIndex = 0; + bufPointVertex = null; + bufPointColor = null; + bufPointAttrib = null; + bufPointIndex = null; this.tessellator = pg.tessellator; this.root = this; @@ -499,100 +516,100 @@ public class PShapeOpenGL extends PShape { } - @Override - protected void finalize() throws Throwable { - try { - finalizePolyBuffers(); - finalizeLineBuffers(); - finalizePointBuffers(); - } finally { - super.finalize(); - } - } +// @Override +// protected void finalize() throws Throwable { +// try { +// finalizePolyBuffers(); +// finalizeLineBuffers(); +// finalizePointBuffers(); +// } finally { +// super.finalize(); +// } +// } - protected void finalizePolyBuffers() { - if (glPolyVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyVertex, context); - } - - if (glPolyColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyColor, context); - } - - if (glPolyNormal != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyNormal, context); - } - - if (glPolyTexcoord != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyTexcoord, context); - } - - if (glPolyAmbient != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyAmbient, context); - } - - if (glPolySpecular != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolySpecular, context); - } - - if (glPolyEmissive != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyEmissive, context); - } - - if (glPolyShininess != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyShininess, context); - } - - for (VertexAttribute attrib: polyAttribs.values()) { - if (attrib.glName != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(attrib.glName, context); - } - } +// protected void finalizePolyBuffers() { +// if (glPolyVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyVertex, context); +// } +// +// if (glPolyColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyColor, context); +// } +// +// if (glPolyNormal != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyNormal, context); +// } +// +// if (glPolyTexcoord != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyTexcoord, context); +// } +// +// if (glPolyAmbient != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyAmbient, context); +// } +// +// if (glPolySpecular != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolySpecular, context); +// } +// +// if (glPolyEmissive != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyEmissive, context); +// } +// +// if (glPolyShininess != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyShininess, context); +// } +// +// for (VertexAttribute attrib: polyAttribs.values()) { +// if (attrib.glName != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(attrib.glName, context); +// } +// } +// +// +// if (glPolyIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPolyIndex, context); +// } +// } - if (glPolyIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPolyIndex, context); - } - } - - - protected void finalizeLineBuffers() { - if (glLineVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineVertex, context); - } - - if (glLineColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineColor, context); - } - - if (glLineAttrib != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineAttrib, context); - } - - if (glLineIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glLineIndex, context); - } - } - - - protected void finalizePointBuffers() { - if (glPointVertex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointVertex, context); - } - - if (glPointColor != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointColor, context); - } - - if (glPointAttrib != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointAttrib, context); - } - - if (glPointIndex != 0) { - PGraphicsOpenGL.finalizeVertexBufferObject(glPointIndex, context); - } - } +// protected void finalizeLineBuffers() { +// if (glLineVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineVertex, context); +// } +// +// if (glLineColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineColor, context); +// } +// +// if (glLineAttrib != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineAttrib, context); +// } +// +// if (glLineIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glLineIndex, context); +// } +// } +// +// +// protected void finalizePointBuffers() { +// if (glPointVertex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointVertex, context); +// } +// +// if (glPointColor != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointColor, context); +// } +// +// if (glPointAttrib != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointAttrib, context); +// } +// +// if (glPointIndex != 0) { +// PGraphicsOpenGL.finalizeVertexBufferObject(glPointIndex, context); +// } +// } /////////////////////////////////////////////////////////// @@ -1181,7 +1198,7 @@ public class PShapeOpenGL extends PShape { } VertexAttribute attrib = polyAttribs.get(name); if (attrib == null) { - attrib = new VertexAttribute(name, type, size); + attrib = new VertexAttribute(pg, name, type, size); polyAttribs.put(name, attrib); inGeo.initAttrib(attrib); } @@ -3872,58 +3889,66 @@ public class PShapeOpenGL extends PShape { int sizei = size * PGL.SIZEOF_INT; tessGeo.updatePolyVerticesBuffer(); - if (glPolyVertex == 0) - glPolyVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); + if (bufPolyVertex == null) +// glPolyVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.polyVerticesBuffer, glUsage); tessGeo.updatePolyColorsBuffer(); - if (glPolyColor == 0) - glPolyColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); + if (bufPolyColor == null) +// glPolyColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyColorsBuffer, glUsage); tessGeo.updatePolyNormalsBuffer(); - if (glPolyNormal == 0) - glPolyNormal = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); + if (bufPolyNormal == null) +// glPolyNormal = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyNormal = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyNormal.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, tessGeo.polyNormalsBuffer, glUsage); tessGeo.updatePolyTexCoordsBuffer(); - if (glPolyTexcoord == 0) - glPolyTexcoord = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); + if (bufPolyTexcoord == null) +// glPolyTexcoord = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyTexcoord = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyTexcoord.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.polyTexCoordsBuffer, glUsage); tessGeo.updatePolyAmbientBuffer(); - if (glPolyAmbient == 0) - glPolyAmbient = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); + if (bufPolyAmbient == null) +// glPolyAmbient = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyAmbient = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyAmbient.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyAmbientBuffer, glUsage); tessGeo.updatePolySpecularBuffer(); - if (glPolySpecular == 0) - glPolySpecular = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); + if (bufPolySpecular == null) +// glPolySpecular = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolySpecular = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolySpecular.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polySpecularBuffer, glUsage); tessGeo.updatePolyEmissiveBuffer(); - if (glPolyEmissive == 0) - glPolyEmissive = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); + if (bufPolyEmissive == null) +// glPolyEmissive = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyEmissive = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyEmissive.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyEmissiveBuffer, glUsage); tessGeo.updatePolyShininessBuffer(); - if (glPolyShininess == 0) - glPolyShininess = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); + if (bufPolyShininess == null) +// glPolyShininess = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyShininess = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyShininess.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizef, tessGeo.polyShininessBuffer, glUsage); @@ -3931,7 +3956,7 @@ public class PShapeOpenGL extends PShape { VertexAttribute attrib = polyAttribs.get(name); tessGeo.updateAttribBuffer(attrib.name); if (!attrib.bufferCreated()) attrib.createBuffer(pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.glName); + pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.buf.glId); pgl.bufferData(PGL.ARRAY_BUFFER, attrib.sizeInBytes(size), tessGeo.polyAttribBuffers.get(name), PGL.STATIC_DRAW); } @@ -3939,9 +3964,10 @@ public class PShapeOpenGL extends PShape { pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); tessGeo.updatePolyIndicesBuffer(); - if (glPolyIndex == 0) - glPolyIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); + if (bufPolyIndex == null) +// glPolyIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPolyIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPolyIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, tessGeo.polyIndicesBuffer, glUsage); @@ -3956,32 +3982,36 @@ public class PShapeOpenGL extends PShape { int sizei = size * PGL.SIZEOF_INT; tessGeo.updateLineVerticesBuffer(); - if (glLineVertex == 0) - glLineVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); + if (bufLineVertex == null) +// glLineVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufLineVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineVerticesBuffer, glUsage); tessGeo.updateLineColorsBuffer(); - if (glLineColor == 0) - glLineColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); + if (bufLineColor == null) +// glLineColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufLineColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.lineColorsBuffer, glUsage); tessGeo.updateLineDirectionsBuffer(); - if (glLineAttrib == 0) - glLineAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); + if (bufLineAttrib == null) +// glLineAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufLineAttrib = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineDirectionsBuffer, glUsage); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); tessGeo.updateLineIndicesBuffer(); - if (glLineIndex == 0) - glLineIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); + if (bufLineIndex == null) +// glLineIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufLineIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufLineIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.lineIndexCount * PGL.SIZEOF_INDEX, tessGeo.lineIndicesBuffer, glUsage); @@ -3996,32 +4026,36 @@ public class PShapeOpenGL extends PShape { int sizei = size * PGL.SIZEOF_INT; tessGeo.updatePointVerticesBuffer(); - if (glPointVertex == 0) - glPointVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); + if (bufPointVertex == null) +// glPointVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPointVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.pointVerticesBuffer, glUsage); tessGeo.updatePointColorsBuffer(); - if (glPointColor == 0) - glPointColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); + if (bufPointColor == null) +// glPointColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPointColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.pointColorsBuffer, glUsage); tessGeo.updatePointOffsetsBuffer(); - if (glPointAttrib == 0) - glPointAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); + if (bufPointAttrib == null) +// glPointAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPointAttrib = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.pointOffsetsBuffer, glUsage); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); tessGeo.updatePointIndicesBuffer(); - if (glPointIndex == 0) - glPointIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); + if (bufPointIndex == null) +// glPointIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); + bufPointIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPointIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.pointIndexCount * PGL.SIZEOF_INDEX, tessGeo.pointIndicesBuffer, glUsage); @@ -4037,53 +4071,74 @@ public class PShapeOpenGL extends PShape { // doesn't get deleted by OpenGL. The VBOs were already // automatically disposed when the old context was // destroyed. - PGraphicsOpenGL.removeVertexBufferObject(glPolyVertex, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyColor, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyNormal, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyTexcoord, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyAmbient, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolySpecular, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyEmissive, context); - PGraphicsOpenGL.removeVertexBufferObject(glPolyShininess, context); + +// PGraphicsOpenGL.removeVertexBufferObject(glPolyVertex, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyColor, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyNormal, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyTexcoord, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyAmbient, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolySpecular, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyEmissive, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyShininess, context); + + bufPolyVertex.dispose(); + bufPolyColor.dispose(); + bufPolyNormal.dispose(); + bufPolyTexcoord.dispose(); + bufPolyAmbient.dispose(); + bufPolySpecular.dispose(); + bufPolyEmissive.dispose(); + bufPolyShininess.dispose(); for (VertexAttribute attrib: polyAttribs.values()) { - PGraphicsOpenGL.removeVertexBufferObject(attrib.glName, context); + attrib.buf.dispose(); +// PGraphicsOpenGL.removeVertexBufferObject(attrib.glName, context); } - PGraphicsOpenGL.removeVertexBufferObject(glPolyIndex, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPolyIndex, context); + bufPolyIndex.dispose(); - PGraphicsOpenGL.removeVertexBufferObject(glLineVertex, context); - PGraphicsOpenGL.removeVertexBufferObject(glLineColor, context); - PGraphicsOpenGL.removeVertexBufferObject(glLineAttrib, context); - PGraphicsOpenGL.removeVertexBufferObject(glLineIndex, context); +// PGraphicsOpenGL.removeVertexBufferObject(glLineVertex, context); +// PGraphicsOpenGL.removeVertexBufferObject(glLineColor, context); +// PGraphicsOpenGL.removeVertexBufferObject(glLineAttrib, context); +// PGraphicsOpenGL.removeVertexBufferObject(glLineIndex, context); + bufLineVertex.dispose(); + bufLineColor.dispose(); + bufLineAttrib.dispose(); + bufLineIndex.dispose(); + +// PGraphicsOpenGL.removeVertexBufferObject(glPointVertex, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPointColor, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPointAttrib, context); +// PGraphicsOpenGL.removeVertexBufferObject(glPointIndex, context); + bufPointVertex.dispose(); + bufPointColor.dispose(); + bufPointAttrib.dispose(); + bufPointIndex.dispose(); - PGraphicsOpenGL.removeVertexBufferObject(glPointVertex, context); - PGraphicsOpenGL.removeVertexBufferObject(glPointColor, context); - PGraphicsOpenGL.removeVertexBufferObject(glPointAttrib, context); - PGraphicsOpenGL.removeVertexBufferObject(glPointIndex, context); // The OpenGL resources have been already deleted // when the context changed. We only need to zero // them to avoid deleting them again when the GC // runs the finalizers of the disposed object. - glPolyVertex = 0; - glPolyColor = 0; - glPolyNormal = 0; - glPolyTexcoord = 0; - glPolyAmbient = 0; - glPolySpecular = 0; - glPolyEmissive = 0; - glPolyShininess = 0; - for (VertexAttribute attrib: polyAttribs.values()) attrib.glName = 0; - glPolyIndex = 0; - - glLineVertex = 0; - glLineColor = 0; - glLineAttrib = 0; - glLineIndex = 0; - - glPointVertex = 0; - glPointColor = 0; - glPointAttrib = 0; - glPointIndex = 0; +// glPolyVertex = 0; +// glPolyColor = 0; +// glPolyNormal = 0; +// glPolyTexcoord = 0; +// glPolyAmbient = 0; +// glPolySpecular = 0; +// glPolyEmissive = 0; +// glPolyShininess = 0; +// for (VertexAttribute attrib: polyAttribs.values()) attrib.glName = 0; +// glPolyIndex = 0; +// +// glLineVertex = 0; +// glLineColor = 0; +// glLineAttrib = 0; +// glLineIndex = 0; +// +// glPointVertex = 0; +// glPointColor = 0; +// glPointAttrib = 0; +// glPointIndex = 0; } return outdated; } @@ -4096,109 +4151,109 @@ public class PShapeOpenGL extends PShape { // Deletion methods - protected void dispose() { - deletePolyBuffers(); - deleteLineBuffers(); - deletePointBuffers(); - } - - - protected void deletePolyBuffers() { - if (glPolyVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyVertex, context, pgl); - glPolyVertex = 0; - } - - if (glPolyColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyColor, context, pgl); - glPolyColor = 0; - } - - if (glPolyNormal != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyNormal, context, pgl); - glPolyNormal = 0; - } - - if (glPolyTexcoord != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyTexcoord, context, pgl); - glPolyTexcoord = 0; - } - - if (glPolyAmbient != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyAmbient, context, pgl); - glPolyAmbient = 0; - } - - if (glPolySpecular != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolySpecular, context, pgl); - glPolySpecular = 0; - } - - if (glPolyEmissive != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyEmissive, context, pgl); - glPolyEmissive = 0; - } - - if (glPolyShininess != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyShininess, context, pgl); - glPolyShininess = 0; - } - - for (VertexAttribute attrib: polyAttribs.values()) { - attrib.deleteBuffer(pgl); - } - - if (glPolyIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyIndex, context, pgl); - glPolyIndex = 0; - } - } - - - protected void deleteLineBuffers() { - if (glLineVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineVertex, context, pgl); - glLineVertex = 0; - } - - if (glLineColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineColor, context, pgl); - glLineColor = 0; - } - - if (glLineAttrib != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineAttrib, context, pgl); - glLineAttrib = 0; - } - - if (glLineIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineIndex, context, pgl); - glLineIndex = 0; - } - } - - - protected void deletePointBuffers() { - if (glPointVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointVertex, context, pgl); - glPointVertex = 0; - } - - if (glPointColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointColor, context, pgl); - glPointColor = 0; - } - - if (glPointAttrib != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointAttrib, context, pgl); - glPointAttrib = 0; - } - - if (glPointIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointIndex, context, pgl); - glPointIndex = 0; - } - } +// protected void dispose() { +// deletePolyBuffers(); +// deleteLineBuffers(); +// deletePointBuffers(); +// } +// +// +// protected void deletePolyBuffers() { +// if (glPolyVertex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyVertex, context, pgl); +// glPolyVertex = 0; +// } +// +// if (glPolyColor != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyColor, context, pgl); +// glPolyColor = 0; +// } +// +// if (glPolyNormal != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyNormal, context, pgl); +// glPolyNormal = 0; +// } +// +// if (glPolyTexcoord != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyTexcoord, context, pgl); +// glPolyTexcoord = 0; +// } +// +// if (glPolyAmbient != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyAmbient, context, pgl); +// glPolyAmbient = 0; +// } +// +// if (glPolySpecular != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolySpecular, context, pgl); +// glPolySpecular = 0; +// } +// +// if (glPolyEmissive != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyEmissive, context, pgl); +// glPolyEmissive = 0; +// } +// +// if (glPolyShininess != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyShininess, context, pgl); +// glPolyShininess = 0; +// } +// +// for (VertexAttribute attrib: polyAttribs.values()) { +// attrib.deleteBuffer(pgl); +// } +// +// if (glPolyIndex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPolyIndex, context, pgl); +// glPolyIndex = 0; +// } +// } +// +// +// protected void deleteLineBuffers() { +// if (glLineVertex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glLineVertex, context, pgl); +// glLineVertex = 0; +// } +// +// if (glLineColor != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glLineColor, context, pgl); +// glLineColor = 0; +// } +// +// if (glLineAttrib != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glLineAttrib, context, pgl); +// glLineAttrib = 0; +// } +// +// if (glLineIndex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glLineIndex, context, pgl); +// glLineIndex = 0; +// } +// } +// +// +// protected void deletePointBuffers() { +// if (glPointVertex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPointVertex, context, pgl); +// glPointVertex = 0; +// } +// +// if (glPointColor != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPointColor, context, pgl); +// glPointColor = 0; +// } +// +// if (glPointAttrib != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPointAttrib, context, pgl); +// glPointAttrib = 0; +// } +// +// if (glPointIndex != 0) { +// PGraphicsOpenGL.deleteVertexBufferObject(glPointIndex, context, pgl); +// glPointIndex = 0; +// } +// } /////////////////////////////////////////////////////////// @@ -4349,7 +4404,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyVertices(int offset, int size) { tessGeo.updatePolyVerticesBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyVertex.glId); tessGeo.polyVerticesBuffer.position(4 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 4 * offset * PGL.SIZEOF_FLOAT, 4 * size * PGL.SIZEOF_FLOAT, tessGeo.polyVerticesBuffer); @@ -4360,7 +4415,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyColors(int offset, int size) { tessGeo.updatePolyColorsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyColor.glId); tessGeo.polyColorsBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT, tessGeo.polyColorsBuffer); @@ -4371,7 +4426,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyNormals(int offset, int size) { tessGeo.updatePolyNormalsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyNormal.glId); tessGeo.polyNormalsBuffer.position(3 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 3 * offset * PGL.SIZEOF_FLOAT, 3 * size * PGL.SIZEOF_FLOAT, tessGeo.polyNormalsBuffer); @@ -4382,7 +4437,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyTexCoords(int offset, int size) { tessGeo.updatePolyTexCoordsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyTexcoord.glId); tessGeo.polyTexCoordsBuffer.position(2 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 2 * offset * PGL.SIZEOF_FLOAT, 2 * size * PGL.SIZEOF_FLOAT, tessGeo.polyTexCoordsBuffer); @@ -4393,7 +4448,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyAmbient(int offset, int size) { tessGeo.updatePolyAmbientBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyAmbient.glId); tessGeo.polyAmbientBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT, tessGeo.polyAmbientBuffer); @@ -4404,7 +4459,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolySpecular(int offset, int size) { tessGeo.updatePolySpecularBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolySpecular.glId); tessGeo.polySpecularBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT, tessGeo.polySpecularBuffer); @@ -4415,7 +4470,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyEmissive(int offset, int size) { tessGeo.updatePolyEmissiveBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyEmissive.glId); tessGeo.polyEmissiveBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT, tessGeo.polyEmissiveBuffer); @@ -4426,7 +4481,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyShininess(int offset, int size) { tessGeo.updatePolyShininessBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyShininess.glId); tessGeo.polyShininessBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_FLOAT, size * PGL.SIZEOF_FLOAT, tessGeo.polyShininessBuffer); @@ -4437,7 +4492,7 @@ public class PShapeOpenGL extends PShape { protected void copyPolyAttrib(VertexAttribute attrib, int offset, int size) { tessGeo.updateAttribBuffer(attrib.name, offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.glName); + pgl.bindBuffer(PGL.ARRAY_BUFFER, attrib.buf.glId); Buffer buf = tessGeo.polyAttribBuffers.get(attrib.name); buf.position(attrib.size * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, attrib.sizeInBytes(offset), @@ -4449,7 +4504,7 @@ public class PShapeOpenGL extends PShape { protected void copyLineVertices(int offset, int size) { tessGeo.updateLineVerticesBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineVertex.glId); tessGeo.lineVerticesBuffer.position(4 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 4 * offset * PGL.SIZEOF_FLOAT, 4 * size * PGL.SIZEOF_FLOAT, tessGeo.lineVerticesBuffer); @@ -4460,7 +4515,7 @@ public class PShapeOpenGL extends PShape { protected void copyLineColors(int offset, int size) { tessGeo.updateLineColorsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineColor.glId); tessGeo.lineColorsBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT, tessGeo.lineColorsBuffer); @@ -4471,7 +4526,7 @@ public class PShapeOpenGL extends PShape { protected void copyLineAttributes(int offset, int size) { tessGeo.updateLineDirectionsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineAttrib.glId); tessGeo.lineDirectionsBuffer.position(4 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 4 * offset * PGL.SIZEOF_FLOAT, 4 * size * PGL.SIZEOF_FLOAT, tessGeo.lineDirectionsBuffer); @@ -4482,7 +4537,7 @@ public class PShapeOpenGL extends PShape { protected void copyPointVertices(int offset, int size) { tessGeo.updatePointVerticesBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointVertex.glId); tessGeo.pointVerticesBuffer.position(4 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 4 * offset * PGL.SIZEOF_FLOAT, 4 * size * PGL.SIZEOF_FLOAT, tessGeo.pointVerticesBuffer); @@ -4493,7 +4548,7 @@ public class PShapeOpenGL extends PShape { protected void copyPointColors(int offset, int size) { tessGeo.updatePointColorsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointColor.glId); tessGeo.pointColorsBuffer.position(offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, offset * PGL.SIZEOF_INT, size * PGL.SIZEOF_INT,tessGeo.pointColorsBuffer); @@ -4504,7 +4559,7 @@ public class PShapeOpenGL extends PShape { protected void copyPointAttributes(int offset, int size) { tessGeo.updatePointOffsetsBuffer(offset, size); - pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); + pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointAttrib.glId); tessGeo.pointOffsetsBuffer.position(2 * offset); pgl.bufferSubData(PGL.ARRAY_BUFFER, 2 * offset * PGL.SIZEOF_FLOAT, 2 * size * PGL.SIZEOF_FLOAT, tessGeo.pointOffsetsBuffer); @@ -4923,30 +4978,30 @@ public class PShapeOpenGL extends PShape { int icount = cache.indexCount[n]; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(root.glPolyVertex, 4, PGL.FLOAT, + shader.setVertexAttribute(root.bufPolyVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(root.glPolyColor, 4, PGL.UNSIGNED_BYTE, + shader.setColorAttribute(root.bufPolyColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); if (g.lights) { - shader.setNormalAttribute(root.glPolyNormal, 3, PGL.FLOAT, + shader.setNormalAttribute(root.bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); - shader.setAmbientAttribute(root.glPolyAmbient, 4, PGL.UNSIGNED_BYTE, + shader.setAmbientAttribute(root.bufPolyAmbient.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setSpecularAttribute(root.glPolySpecular, 4, PGL.UNSIGNED_BYTE, + shader.setSpecularAttribute(root.bufPolySpecular.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setEmissiveAttribute(root.glPolyEmissive, 4, PGL.UNSIGNED_BYTE, + shader.setEmissiveAttribute(root.bufPolyEmissive.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setShininessAttribute(root.glPolyShininess, 1, PGL.FLOAT, + shader.setShininessAttribute(root.bufPolyShininess.glId, 1, PGL.FLOAT, 0, voffset * PGL.SIZEOF_FLOAT); } if (g.lights || needNormals) { - shader.setNormalAttribute(root.glPolyNormal, 3, PGL.FLOAT, + shader.setNormalAttribute(root.bufPolyNormal.glId, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); } if (tex != null || needTexCoords) { - shader.setTexcoordAttribute(root.glPolyTexcoord, 2, PGL.FLOAT, + shader.setTexcoordAttribute(root.bufPolyTexcoord.glId, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); shader.setTexture(tex); } @@ -4954,12 +5009,12 @@ public class PShapeOpenGL extends PShape { for (VertexAttribute attrib: polyAttribs.values()) { if (!attrib.active(shader)) continue; attrib.bind(pgl); - shader.setAttributeVBO(attrib.glLoc, attrib.glName, + shader.setAttributeVBO(attrib.glLoc, attrib.buf.glId, attrib.tessSize, attrib.type, attrib.isColor(), 0, attrib.sizeInBytes(voffset)); } - shader.draw(root.glPolyIndex, icount, ioffset); + shader.draw(root.bufPolyIndex.glId, icount, ioffset); } for (VertexAttribute attrib: polyAttribs.values()) { @@ -5076,14 +5131,14 @@ public class PShapeOpenGL extends PShape { int icount = cache.indexCount[n]; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(root.glLineVertex, 4, PGL.FLOAT, + shader.setVertexAttribute(root.bufLineVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(root.glLineColor, 4, PGL.UNSIGNED_BYTE, + shader.setColorAttribute(root.bufLineColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setLineAttribute(root.glLineAttrib, 4, PGL.FLOAT, + shader.setLineAttribute(root.bufLineAttrib.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.draw(root.glLineIndex, icount, ioffset); + shader.draw(root.bufLineIndex.glId, icount, ioffset); } shader.unbind(); @@ -5173,14 +5228,14 @@ public class PShapeOpenGL extends PShape { int icount = cache.indexCount[n]; int voffset = cache.vertexOffset[n]; - shader.setVertexAttribute(root.glPointVertex, 4, PGL.FLOAT, + shader.setVertexAttribute(root.bufPointVertex.glId, 4, PGL.FLOAT, 0, 4 * voffset * PGL.SIZEOF_FLOAT); - shader.setColorAttribute(root.glPointColor, 4, PGL.UNSIGNED_BYTE, + shader.setColorAttribute(root.bufPointColor.glId, 4, PGL.UNSIGNED_BYTE, 0, 4 * voffset * PGL.SIZEOF_BYTE); - shader.setPointAttribute(root.glPointAttrib, 2, PGL.FLOAT, + shader.setPointAttribute(root.bufPointAttrib.glId, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); - shader.draw(root.glPointIndex, icount, ioffset); + shader.draw(root.bufPointIndex.glId, icount, ioffset); } shader.unbind(); diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index 8dc6e155a..f9d36b193 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -24,6 +24,7 @@ package processing.opengl; import processing.core.PApplet; import processing.core.PConstants; import processing.core.PGraphics; +import processing.opengl.PGraphicsOpenGL.GLResourceTexture; import java.lang.reflect.Method; import java.nio.ByteBuffer; @@ -84,6 +85,7 @@ public class Texture implements PConstants { public int glWrapT; public int glWidth; public int glHeight; + private GLResourceTexture glres; protected PGraphicsOpenGL pg; protected PGL pgl; // The interface between Processing and OpenGL. @@ -167,16 +169,16 @@ public class Texture implements PConstants { } - @Override - protected void finalize() throws Throwable { - try { - if (glName != 0) { - PGraphicsOpenGL.finalizeTextureObject(glName, context); - } - } finally { - super.finalize(); - } - } +// @Override +// protected void finalize() throws Throwable { +// try { +// if (glName != 0) { +// PGraphicsOpenGL.finalizeTextureObject(glName, context); +// } +// } finally { +// super.finalize(); +// } +// } //////////////////////////////////////////////////////////// @@ -1148,7 +1150,7 @@ public class Texture implements PConstants { } context = pgl.getCurrentContext(); - glName = PGraphicsOpenGL.createTextureObject(context, pgl); + glres = new GLResourceTexture(this); pgl.bindTexture(glTarget, glName); pgl.texParameteri(glTarget, PGL.TEXTURE_MIN_FILTER, glMinFilter); @@ -1182,24 +1184,22 @@ public class Texture implements PConstants { * Marks the texture object for deletion. */ protected void dispose() { - if (glName != 0) { - PGraphicsOpenGL.finalizeTextureObject(glName, context); + if (glres != null) { + glres.dispose(); + glres = null; glName = 0; } +// if (glName != 0) { +// PGraphicsOpenGL.finalizeTextureObject(glName, context); +// glName = 0; +// } } protected boolean contextIsOutdated() { boolean outdated = !pgl.contextIsCurrent(context); if (outdated) { - // Removing the texture object from the renderer's list so it - // doesn't get deleted by OpenGL. The texture object was - // automatically disposed when the old context was destroyed. - PGraphicsOpenGL.removeTextureObject(glName, context); - - // And then set the id to zero, so it doesn't try to be - // deleted when the object's finalizer is invoked by the GC. - glName = 0; + dispose(); } return outdated; } diff --git a/core/src/processing/opengl/VertexBuffer.java b/core/src/processing/opengl/VertexBuffer.java new file mode 100644 index 000000000..db390c219 --- /dev/null +++ b/core/src/processing/opengl/VertexBuffer.java @@ -0,0 +1,63 @@ +package processing.opengl; + +import processing.opengl.PGraphicsOpenGL.GLResourceVertexBuffer; + +public class VertexBuffer { + static protected final int INIT_VERTEX_BUFFER_SIZE = 256; + static protected final int INIT_INDEX_BUFFER_SIZE = 512; + + public int glId; + int target; + int elementSize; + int ncoords; + boolean index; + + protected PGL pgl; // The interface between Processing and OpenGL. + protected int context; // The context that created this texture. + private GLResourceVertexBuffer glres; + + VertexBuffer(PGraphicsOpenGL pg, int target, int ncoords, int esize) { + this(pg, target, ncoords, esize, false); + } + + VertexBuffer(PGraphicsOpenGL pg, int target, int ncoords, int esize, boolean index) { + pgl = pg.pgl; + context = pgl.createEmptyContext(); + + this.target = target; + this.ncoords = ncoords; + this.elementSize = esize; + this.index = index; + create(); + init(); + } + + protected void create() { + context = pgl.getCurrentContext(); + glres = new GLResourceVertexBuffer(this); + } + + protected void init() { + int size = index ? ncoords * INIT_INDEX_BUFFER_SIZE * elementSize : + ncoords * INIT_VERTEX_BUFFER_SIZE * elementSize; + pgl.bindBuffer(target, glId); + pgl.bufferData(target, size, null, PGL.STATIC_DRAW); + } + + protected void dispose() { + if (glres != null) { + glres.dispose(); + glId = 0; + glres = null; + } + } + + protected boolean contextIsOutdated() { + boolean outdated = !pgl.contextIsCurrent(context); + if (outdated) { + dispose(); + } + return outdated; + } + +}