implementing new gl resource disposal method based on WeakReferences

This commit is contained in:
codeanticode
2015-06-23 16:49:14 -04:00
parent 17e0f31c2f
commit 48e136bb79
6 changed files with 1144 additions and 710 deletions
+4 -3
View File
@@ -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;
File diff suppressed because it is too large Load Diff
+44 -34
View File
@@ -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;
// }
}
File diff suppressed because it is too large Load Diff
+21 -21
View File
@@ -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;
}
@@ -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;
}
}