diff --git a/java/libraries/opengl/src/processing/opengl/PFontTexture.java b/java/libraries/opengl/src/processing/opengl/PFontTexture.java index 8dab59c60..e153cdc20 100644 --- a/java/libraries/opengl/src/processing/opengl/PFontTexture.java +++ b/java/libraries/opengl/src/processing/opengl/PFontTexture.java @@ -227,7 +227,7 @@ class PFontTexture implements PConstants { } if (outdated) { for (int i = 0; i < textures.length; i++) { - pg.removeTextureObject(textures[i].glID); + pg.removeTextureObject(textures[i].glID, textures[i].context.code()); textures[i].glID = 0; } } diff --git a/java/libraries/opengl/src/processing/opengl/PFramebuffer.java b/java/libraries/opengl/src/processing/opengl/PFramebuffer.java index e8ed0650d..e44ccac07 100644 --- a/java/libraries/opengl/src/processing/opengl/PFramebuffer.java +++ b/java/libraries/opengl/src/processing/opengl/PFramebuffer.java @@ -82,6 +82,7 @@ public class PFramebuffer implements PConstants { this.parent = parent; pg = (PGraphicsOpenGL)parent.g; pgl = pg.pgl; + context = pgl.createEmptyContext(); glFboID = 0; glDepthBufferID = 0; @@ -143,19 +144,19 @@ public class PFramebuffer implements PConstants { protected void finalize() throws Throwable { try { if (glFboID != 0) { - pg.finalizeFrameBufferObject(glFboID); + pg.finalizeFrameBufferObject(glFboID, context.code()); } if (glDepthBufferID != 0) { - pg.finalizeRenderBufferObject(glDepthBufferID); + pg.finalizeRenderBufferObject(glDepthBufferID, context.code()); } if (glStencilBufferID != 0) { - pg.finalizeRenderBufferObject(glStencilBufferID); + pg.finalizeRenderBufferObject(glStencilBufferID, context.code()); } if (glColorBufferMultisampleID != 0) { - pg.finalizeRenderBufferObject(glColorBufferMultisampleID); + pg.finalizeRenderBufferObject(glColorBufferMultisampleID, context.code()); } if (glDepthStencilBufferID != 0) { - pg.finalizeRenderBufferObject(glDepthStencilBufferID); + pg.finalizeRenderBufferObject(glDepthStencilBufferID, context.code()); } } finally { super.finalize(); @@ -274,12 +275,12 @@ public class PFramebuffer implements PConstants { protected void allocate() { release(); // Just in the case this object is being re-allocated. - context = pgl.getContext(); + context = pgl.getCurrentContext(); if (screenFb) { glFboID = 0; } else { - glFboID = pg.createFrameBufferObject(); + glFboID = pg.createFrameBufferObject(context.code()); } // create the rest of the stuff... @@ -302,23 +303,23 @@ public class PFramebuffer implements PConstants { protected void release() { if (glFboID != 0) { - pg.finalizeFrameBufferObject(glFboID); + pg.finalizeFrameBufferObject(glFboID, context.code()); glFboID = 0; } if (glDepthBufferID != 0) { - pg.finalizeRenderBufferObject(glDepthBufferID); + pg.finalizeRenderBufferObject(glDepthBufferID, context.code()); glDepthBufferID = 0; } if (glStencilBufferID != 0) { - pg.finalizeRenderBufferObject(glStencilBufferID); + pg.finalizeRenderBufferObject(glStencilBufferID, context.code()); glStencilBufferID = 0; } if (glColorBufferMultisampleID != 0) { - pg.finalizeRenderBufferObject(glColorBufferMultisampleID); + pg.finalizeRenderBufferObject(glColorBufferMultisampleID, context.code()); glColorBufferMultisampleID = 0; } if (glDepthStencilBufferID != 0) { - pg.finalizeRenderBufferObject(glDepthStencilBufferID); + pg.finalizeRenderBufferObject(glDepthStencilBufferID, context.code()); glDepthStencilBufferID = 0; } } @@ -327,11 +328,11 @@ public class PFramebuffer implements PConstants { protected boolean contextIsOutdated() { boolean outdated = !pgl.contextIsCurrent(context); if (outdated) { - pg.removeFrameBufferObject(glFboID); - pg.removeRenderBufferObject(glDepthBufferID); - pg.removeRenderBufferObject(glStencilBufferID); - pg.removeRenderBufferObject(glDepthStencilBufferID); - pg.removeRenderBufferObject(glColorBufferMultisampleID); + pg.removeFrameBufferObject(glFboID, context.code()); + pg.removeRenderBufferObject(glDepthBufferID, context.code()); + pg.removeRenderBufferObject(glStencilBufferID, context.code()); + pg.removeRenderBufferObject(glDepthStencilBufferID, context.code()); + pg.removeRenderBufferObject(glColorBufferMultisampleID, context.code()); glFboID = 0; glDepthBufferID = 0; @@ -353,7 +354,7 @@ public class PFramebuffer implements PConstants { pg.pushFramebuffer(); pg.setFramebuffer(this); - glColorBufferMultisampleID = pg.createRenderBufferObject(); + glColorBufferMultisampleID = pg.createRenderBufferObject(context.code()); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glColorBufferMultisampleID); pgl.glRenderbufferStorageMultisample(PGL.GL_RENDERBUFFER, nsamples, PGL.GL_RGBA8, width, height); pgl.glFramebufferRenderbuffer(PGL.GL_FRAMEBUFFER, PGL.GL_COLOR_ATTACHMENT0, PGL.GL_RENDERBUFFER, glColorBufferMultisampleID); @@ -372,7 +373,7 @@ public class PFramebuffer implements PConstants { pg.pushFramebuffer(); pg.setFramebuffer(this); - glDepthStencilBufferID = pg.createRenderBufferObject(); + glDepthStencilBufferID = pg.createRenderBufferObject(context.code()); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glDepthStencilBufferID); if (multisample) { @@ -398,7 +399,7 @@ public class PFramebuffer implements PConstants { pg.pushFramebuffer(); pg.setFramebuffer(this); - glDepthBufferID = pg.createRenderBufferObject(); + glDepthBufferID = pg.createRenderBufferObject(context.code()); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glDepthBufferID); int glConst = PGL.GL_DEPTH_COMPONENT16; @@ -432,7 +433,7 @@ public class PFramebuffer implements PConstants { pg.pushFramebuffer(); pg.setFramebuffer(this); - glStencilBufferID = pg.createRenderBufferObject(); + glStencilBufferID = pg.createRenderBufferObject(context.code()); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glStencilBufferID); int glConst = PGL.GL_STENCIL_INDEX1; diff --git a/java/libraries/opengl/src/processing/opengl/PGL.java b/java/libraries/opengl/src/processing/opengl/PGL.java index e9ae6b364..59d01b93b 100644 --- a/java/libraries/opengl/src/processing/opengl/PGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGL.java @@ -1123,20 +1123,48 @@ public class PGL { // Context interface + + public Context createEmptyContext() { + return new Context(); + } + - public Context getContext() { + public Context getCurrentContext() { return new Context(context); } + public class Context { protected GLContext context; + Context() { + context = null; + } + Context(GLContext context) { this.context = context; } - boolean same(GLContext context) { - return this.context.hashCode() == context.hashCode(); + boolean current() { + return equal(context); + } + + boolean equal(GLContext context) { + if (this.context == null || context == null) { + // A null context means a still non-created resource, + // so it is considered equal to the argument. + return true; + } else { + return this.context.hashCode() == context.hashCode(); + } + } + + int code() { + if (context == null) { + return -1; + } else { + return context.hashCode(); + } } } @@ -1233,7 +1261,7 @@ public class PGL { public boolean contextIsCurrent(Context other) { - return other != null && other.same(context); + return other == null || other.current(); } diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index 28e316ef4..7e764b095 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -134,13 +134,13 @@ public class PGraphicsOpenGL extends PGraphics { // GL objects: - 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 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(); // ........................................................ @@ -517,456 +517,494 @@ public class PGraphicsOpenGL extends PGraphics { // RESOURCE HANDLING + + protected class GLResource { + int id; + int context; + GLResource(int id, int context) { + this.id = id; + this.context = context; + } + + public boolean equals(Object obj) { + GLResource other = (GLResource)obj; + return other.id == id && other.context == context; + } + } + // Texture Objects ------------------------------------------- - protected int createTextureObject() { + protected int createTextureObject(int context) { deleteFinalizedTextureObjects(); int[] temp = new int[1]; pgl.glGenTextures(1, temp, 0); int id = temp[0]; - if (glTextureObjects.containsKey(id)) { + GLResource res = new GLResource(id, context); + + if (glTextureObjects.containsKey(res)) { showWarning("Adding same texture twice"); } else { - glTextureObjects.put(id, false); + glTextureObjects.put(res, false); } return id; } - protected void deleteTextureObject(int id) { - if (glTextureObjects.containsKey(id)) { - int[] temp = new int[1]; - temp[0] = id; + protected void deleteTextureObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glTextureObjects.containsKey(res)) { + int[] temp = { id }; pgl.glDeleteTextures(1, temp, 0); - glTextureObjects.remove(id); + glTextureObjects.remove(res); } } protected void deleteAllTextureObjects() { - for (Integer id : glTextureObjects.keySet()) { - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glTextureObjects.keySet()) { + int[] temp = { res.id }; pgl.glDeleteTextures(1, temp, 0); } glTextureObjects.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeTextureObject(int id) { - if (glTextureObjects.containsKey(id)) { - glTextureObjects.put(id, true); + synchronized protected void finalizeTextureObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glTextureObjects.containsKey(res)) { + glTextureObjects.put(res, true); } else { showWarning("Trying to finalize non-existing texture"); } } protected void deleteFinalizedTextureObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glTextureObjects.keySet()) { - if (glTextureObjects.get(id)) { - finalized.add(id); - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glTextureObjects.keySet()) { + if (glTextureObjects.get(res)) { + finalized.add(res); + int[] temp = { res.id }; pgl.glDeleteTextures(1, temp, 0); } } - for (Integer id : finalized) { - glTextureObjects.remove(id); + for (GLResource res : finalized) { + glTextureObjects.remove(res); } } - protected void removeTextureObject(int id) { - if (glTextureObjects.containsKey(id)) { - glTextureObjects.remove(id); + protected void removeTextureObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glTextureObjects.containsKey(res)) { + glTextureObjects.remove(res); } } // Vertex Buffer Objects ---------------------------------------------- - protected int createVertexBufferObject() { + protected int createVertexBufferObject(int context) { deleteFinalizedVertexBufferObjects(); int[] temp = new int[1]; pgl.glGenBuffers(1, temp, 0); int id = temp[0]; - - if (glVertexBuffers.containsKey(id)) { + + GLResource res = new GLResource(id, context); + + if (glVertexBuffers.containsKey(res)) { showWarning("Adding same VBO twice"); } else { - glVertexBuffers.put(id, false); + glVertexBuffers.put(res, false); } return id; } - protected void deleteVertexBufferObject(int id) { - if (glVertexBuffers.containsKey(id)) { - int[] temp = new int[1]; - temp[0] = id; + protected void deleteVertexBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glVertexBuffers.containsKey(res)) { + int[] temp = { id }; pgl.glDeleteBuffers(1, temp, 0); - glVertexBuffers.remove(id); + glVertexBuffers.remove(res); } } protected void deleteAllVertexBufferObjects() { - for (Integer id : glVertexBuffers.keySet()) { - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glVertexBuffers.keySet()) { + int[] temp = { res.id }; pgl.glDeleteBuffers(1, temp, 0); } glVertexBuffers.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeVertexBufferObject(int id) { - if (glVertexBuffers.containsKey(id)) { - glVertexBuffers.put(id, true); + synchronized protected void finalizeVertexBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glVertexBuffers.containsKey(res)) { + glVertexBuffers.put(res, true); } else { showWarning("Trying to finalize non-existing VBO"); } } protected void deleteFinalizedVertexBufferObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glVertexBuffers.keySet()) { - if (glVertexBuffers.get(id)) { - finalized.add(id); - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glVertexBuffers.keySet()) { + if (glVertexBuffers.get(res)) { + finalized.add(res); + int[] temp = { res.id }; pgl.glDeleteBuffers(1, temp, 0); } } - for (Integer id : finalized) { - glVertexBuffers.remove(id); + for (GLResource res : finalized) { + glVertexBuffers.remove(res); } } - protected void removeVertexBufferObject(int id) { - if (glVertexBuffers.containsKey(id)) { - glVertexBuffers.remove(id); + protected void removeVertexBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glVertexBuffers.containsKey(res)) { + glVertexBuffers.remove(res); } } // FrameBuffer Objects ----------------------------------------- - protected int createFrameBufferObject() { + protected int createFrameBufferObject(int context) { deleteFinalizedFrameBufferObjects(); int[] temp = new int[1]; pgl.glGenFramebuffers(1, temp, 0); int id = temp[0]; - - if (glFrameBuffers.containsKey(id)) { + + GLResource res = new GLResource(id, context); + + if (glFrameBuffers.containsKey(res)) { showWarning("Adding same FBO twice"); } else { - glFrameBuffers.put(id, false); + glFrameBuffers.put(res, false); } return id; } - protected void deleteFrameBufferObject(int id) { - if (glFrameBuffers.containsKey(id)) { - int[] temp = new int[1]; - temp[0] = id; + protected void deleteFrameBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glFrameBuffers.containsKey(res)) { + int[] temp = { id }; pgl.glDeleteFramebuffers(1, temp, 0); - glFrameBuffers.remove(id); + glFrameBuffers.remove(res); } } protected void deleteAllFrameBufferObjects() { - for (Integer id : glFrameBuffers.keySet()) { - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glFrameBuffers.keySet()) { + int[] temp = { res.id }; pgl.glDeleteFramebuffers(1, temp, 0); } glFrameBuffers.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeFrameBufferObject(int id) { - if (glFrameBuffers.containsKey(id)) { - glFrameBuffers.put(id, true); + synchronized protected void finalizeFrameBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glFrameBuffers.containsKey(res)) { + glFrameBuffers.put(res, true); } else { showWarning("Trying to finalize non-existing FBO"); } } protected void deleteFinalizedFrameBufferObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glFrameBuffers.keySet()) { - if (glFrameBuffers.get(id)) { - finalized.add(id); - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glFrameBuffers.keySet()) { + if (glFrameBuffers.get(res)) { + finalized.add(res); + int[] temp = { res.id }; pgl.glDeleteFramebuffers(1, temp, 0); } } - for (Integer id : finalized) { - glFrameBuffers.remove(id); + for (GLResource res : finalized) { + glFrameBuffers.remove(res); } } - protected void removeFrameBufferObject(int id) { - if (glFrameBuffers.containsKey(id)) { - glFrameBuffers.remove(id); + protected void removeFrameBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glFrameBuffers.containsKey(res)) { + glFrameBuffers.remove(res); } } // RenderBuffer Objects ----------------------------------------------- - protected int createRenderBufferObject() { + protected int createRenderBufferObject(int context) { deleteFinalizedRenderBufferObjects(); int[] temp = new int[1]; pgl.glGenRenderbuffers(1, temp, 0); int id = temp[0]; + + GLResource res = new GLResource(id, context); - if (glRenderBuffers.containsKey(id)) { + if (glRenderBuffers.containsKey(res)) { showWarning("Adding same renderbuffer twice"); } else { - glRenderBuffers.put(id, false); + glRenderBuffers.put(res, false); } return id; } - protected void deleteRenderBufferObject(int id) { - if (glRenderBuffers.containsKey(id)) { - int[] temp = new int[1]; - temp[0] = id; + protected void deleteRenderBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glRenderBuffers.containsKey(res)) { + int[] temp = { id }; pgl.glDeleteRenderbuffers(1, temp, 0); - glRenderBuffers.remove(id); + glRenderBuffers.remove(res); } } protected void deleteAllRenderBufferObjects() { - for (Integer id : glRenderBuffers.keySet()) { - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glRenderBuffers.keySet()) { + int[] temp = { res.id }; pgl.glDeleteRenderbuffers(1, temp, 0); } glRenderBuffers.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeRenderBufferObject(int id) { - if (glRenderBuffers.containsKey(id)) { - glRenderBuffers.put(id, true); + synchronized protected void finalizeRenderBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glRenderBuffers.containsKey(res)) { + glRenderBuffers.put(res, true); } else { showWarning("Trying to finalize non-existing renderbuffer"); } } protected void deleteFinalizedRenderBufferObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glRenderBuffers.keySet()) { - if (glRenderBuffers.get(id)) { - finalized.add(id); - int[] temp = new int[1]; - temp[0] = id.intValue(); + for (GLResource res : glRenderBuffers.keySet()) { + if (glRenderBuffers.get(res)) { + finalized.add(res); + int[] temp = { res.id }; pgl.glDeleteRenderbuffers(1, temp, 0); } } - for (Integer id : finalized) { - glRenderBuffers.remove(id); + for (GLResource res : finalized) { + glRenderBuffers.remove(res); } } - protected void removeRenderBufferObject(int id) { - if (glRenderBuffers.containsKey(id)) { - glRenderBuffers.remove(id); + protected void removeRenderBufferObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glRenderBuffers.containsKey(res)) { + glRenderBuffers.remove(res); } } // GLSL Program Objects ----------------------------------------------- - protected int createGLSLProgramObject() { + protected int createGLSLProgramObject(int context) { deleteFinalizedGLSLProgramObjects(); int id = pgl.glCreateProgram(); - if (glslPrograms.containsKey(id)) { + GLResource res = new GLResource(id, context); + + if (glslPrograms.containsKey(res)) { showWarning("Adding same glsl program twice"); } else { - glslPrograms.put(id, false); + glslPrograms.put(res, false); } return id; } - protected void deleteGLSLProgramObject(int id) { - if (glslPrograms.containsKey(id)) { - pgl.glDeleteProgram(id); - glslPrograms.remove(id); + protected void deleteGLSLProgramObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslPrograms.containsKey(res)) { + pgl.glDeleteProgram(res.id); + glslPrograms.remove(res); } } protected void deleteAllGLSLProgramObjects() { - for (Integer id : glslPrograms.keySet()) { - pgl.glDeleteProgram(id); + for (GLResource res : glslPrograms.keySet()) { + pgl.glDeleteProgram(res.id); } glslPrograms.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeGLSLProgramObject(int id) { - if (glslPrograms.containsKey(id)) { - glslPrograms.put(id, true); + synchronized protected void finalizeGLSLProgramObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslPrograms.containsKey(res)) { + glslPrograms.put(res, true); } else { showWarning("Trying to finalize non-existing glsl program"); } } protected void deleteFinalizedGLSLProgramObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glslPrograms.keySet()) { - if (glslPrograms.get(id)) { - finalized.add(id); - pgl.glDeleteProgram(id); + for (GLResource res : glslPrograms.keySet()) { + if (glslPrograms.get(res)) { + finalized.add(res); + pgl.glDeleteProgram(res.id); } } - for (Integer id : finalized) { - glslPrograms.remove(id); + for (GLResource res : finalized) { + glslPrograms.remove(res); } } - protected void removeGLSLProgramObject(int id) { - if (glslPrograms.containsKey(id)) { - glslPrograms.remove(id); + protected void removeGLSLProgramObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslPrograms.containsKey(res)) { + glslPrograms.remove(res); } } // GLSL Vertex Shader Objects ----------------------------------------------- - protected int createGLSLVertShaderObject() { + protected int createGLSLVertShaderObject(int context) { deleteFinalizedGLSLVertShaderObjects(); int id = pgl.glCreateShader(PGL.GL_VERTEX_SHADER); - if (glslVertexShaders.containsKey(id)) { + GLResource res = new GLResource(id, context); + + if (glslVertexShaders.containsKey(res)) { showWarning("Adding same glsl vertex shader twice"); } else { - glslVertexShaders.put(id, false); + glslVertexShaders.put(res, false); } return id; } - protected void deleteGLSLVertShaderObject(int id) { - if (glslVertexShaders.containsKey(id)) { - pgl.glDeleteShader(id); - glslVertexShaders.remove(id); + protected void deleteGLSLVertShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslVertexShaders.containsKey(res)) { + pgl.glDeleteShader(res.id); + glslVertexShaders.remove(res); } } protected void deleteAllGLSLVertShaderObjects() { - for (Integer id : glslVertexShaders.keySet()) { - pgl.glDeleteShader(id); + for (GLResource res : glslVertexShaders.keySet()) { + pgl.glDeleteShader(res.id); } glslVertexShaders.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeGLSLVertShaderObject(int id) { - if (glslVertexShaders.containsKey(id)) { - glslVertexShaders.put(id, true); + synchronized protected void finalizeGLSLVertShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslVertexShaders.containsKey(res)) { + glslVertexShaders.put(res, true); } else { showWarning("Trying to finalize non-existing glsl vertex shader"); } } protected void deleteFinalizedGLSLVertShaderObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glslVertexShaders.keySet()) { - if (glslVertexShaders.get(id)) { - finalized.add(id); - pgl.glDeleteShader(id); + for (GLResource res : glslVertexShaders.keySet()) { + if (glslVertexShaders.get(res)) { + finalized.add(res); + pgl.glDeleteShader(res.id); } } - for (Integer id : finalized) { - glslVertexShaders.remove(id); + for (GLResource res : finalized) { + glslVertexShaders.remove(res); } } - protected void removeGLSLVertShaderObject(int id) { - if (glslVertexShaders.containsKey(id)) { - glslVertexShaders.remove(id); + protected void removeGLSLVertShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslVertexShaders.containsKey(res)) { + glslVertexShaders.remove(res); } } // GLSL Fragment Shader Objects ----------------------------------------------- - protected int createGLSLFragShaderObject() { + protected int createGLSLFragShaderObject(int context) { deleteFinalizedGLSLFragShaderObjects(); int id = pgl.glCreateShader(PGL.GL_FRAGMENT_SHADER); - if (glslFragmentShaders.containsKey(id)) { + GLResource res = new GLResource(id, context); + + if (glslFragmentShaders.containsKey(res)) { showWarning("Adding same glsl fragment shader twice"); } else { - glslFragmentShaders.put(id, false); + glslFragmentShaders.put(res, false); } return id; } - protected void deleteGLSLFragShaderObject(int id) { - if (glslFragmentShaders.containsKey(id)) { - pgl.glDeleteShader(id); - glslFragmentShaders.remove(id); + protected void deleteGLSLFragShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslFragmentShaders.containsKey(res)) { + pgl.glDeleteShader(res.id); + glslFragmentShaders.remove(res); } } protected void deleteAllGLSLFragShaderObjects() { - for (Integer id : glslFragmentShaders.keySet()) { - pgl.glDeleteShader(id); + for (GLResource res : glslFragmentShaders.keySet()) { + pgl.glDeleteShader(res.id); } glslFragmentShaders.clear(); } // This is synchronized because it is called from the GC thread. - synchronized protected void finalizeGLSLFragShaderObject(int id) { - if (glslFragmentShaders.containsKey(id)) { - glslFragmentShaders.put(id, true); + synchronized protected void finalizeGLSLFragShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslFragmentShaders.containsKey(res)) { + glslFragmentShaders.put(res, true); } else { showWarning("Trying to finalize non-existing glsl fragment shader"); } } protected void deleteFinalizedGLSLFragShaderObjects() { - Set finalized = new HashSet(); + Set finalized = new HashSet(); - for (Integer id : glslFragmentShaders.keySet()) { - if (glslFragmentShaders.get(id)) { - finalized.add(id); - pgl.glDeleteShader(id); + for (GLResource res : glslFragmentShaders.keySet()) { + if (glslFragmentShaders.get(res)) { + finalized.add(res); + pgl.glDeleteShader(res.id); } } - for (Integer id : finalized) { - glslFragmentShaders.remove(id); + for (GLResource res : finalized) { + glslFragmentShaders.remove(res); } } - protected void removeGLSLFragShaderObject(int id) { - if (glslFragmentShaders.containsKey(id)) { - glslFragmentShaders.remove(id); + protected void removeGLSLFragShaderObject(int id, int context) { + GLResource res = new GLResource(id, context); + if (glslFragmentShaders.containsKey(res)) { + glslFragmentShaders.remove(res); } } @@ -983,15 +1021,15 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void deleteAllGLResources() { - deleteAllTextureObjects(); - deleteAllVertexBufferObjects(); - deleteAllFrameBufferObjects(); - deleteAllRenderBufferObjects(); - deleteAllGLSLProgramObjects(); - deleteAllGLSLVertShaderObjects(); - deleteAllGLSLFragShaderObjects(); - } +// protected void deleteAllGLResources() { +// deleteAllTextureObjects(); +// deleteAllVertexBufferObjects(); +// deleteAllFrameBufferObjects(); +// deleteAllRenderBufferObjects(); +// deleteAllGLSLProgramObjects(); +// deleteAllGLSLVertShaderObjects(); +// deleteAllGLSLFragShaderObjects(); +// } ////////////////////////////////////////////////////////////// @@ -1028,47 +1066,47 @@ public class PGraphicsOpenGL extends PGraphics { protected void createFillBuffers() { if (!fillBuffersCreated || fillBuffersContextIsOutdated()) { - fillBuffersContext = pgl.getContext(); + fillBuffersContext = pgl.getCurrentContext(); int sizef = PGL.MAX_TESS_VERTICES * PGL.SIZEOF_FLOAT; int sizei = PGL.MAX_TESS_VERTICES * PGL.SIZEOF_INT; int sizex = PGL.MAX_TESS_INDICES * PGL.SIZEOF_INDEX; - glFillVertexBufferID = createVertexBufferObject(); + glFillVertexBufferID = createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glFillColorBufferID = createVertexBufferObject(); + glFillColorBufferID = createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillNormalBufferID = createVertexBufferObject(); + glFillNormalBufferID = createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillNormalBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glFillTexCoordBufferID = createVertexBufferObject(); + glFillTexCoordBufferID = createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillTexCoordBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 2 * sizef, null, PGL.GL_STATIC_DRAW); - glFillAmbientBufferID = pg.createVertexBufferObject(); + glFillAmbientBufferID = pg.createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillAmbientBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillSpecularBufferID = pg.createVertexBufferObject(); + glFillSpecularBufferID = pg.createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillSpecularBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillEmissiveBufferID = pg.createVertexBufferObject(); + glFillEmissiveBufferID = pg.createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillEmissiveBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillShininessBufferID = pg.createVertexBufferObject(); + glFillShininessBufferID = pg.createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillShininessBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glFillIndexBufferID = createVertexBufferObject(); + glFillIndexBufferID = createVertexBufferObject(fillBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -1114,10 +1152,10 @@ public class PGraphicsOpenGL extends PGraphics { pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillTexCoordBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 2 * sizef, FloatBuffer.wrap(tessGeo.fillTexcoords, 0, 2 * size), PGL.GL_STATIC_DRAW); } - + pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, tessGeo.fillIndexCount * PGL.SIZEOF_INDEX, - ShortBuffer.wrap(tessGeo.fillIndices, 0, tessGeo.fillIndexCount), PGL.GL_STATIC_DRAW); + ShortBuffer.wrap(tessGeo.fillIndices, 0, tessGeo.fillIndexCount), PGL.GL_STATIC_DRAW); } @@ -1134,31 +1172,31 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteFillBuffers() { if (fillBuffersCreated) { - deleteVertexBufferObject(glFillVertexBufferID); + deleteVertexBufferObject(glFillVertexBufferID, fillBuffersContext.code()); glFillVertexBufferID = 0; - deleteVertexBufferObject(glFillColorBufferID); + deleteVertexBufferObject(glFillColorBufferID, fillBuffersContext.code()); glFillColorBufferID = 0; - deleteVertexBufferObject(glFillNormalBufferID); + deleteVertexBufferObject(glFillNormalBufferID, fillBuffersContext.code()); glFillNormalBufferID = 0; - deleteVertexBufferObject(glFillTexCoordBufferID); + deleteVertexBufferObject(glFillTexCoordBufferID, fillBuffersContext.code()); glFillTexCoordBufferID = 0; - deleteVertexBufferObject(glFillAmbientBufferID); + deleteVertexBufferObject(glFillAmbientBufferID, fillBuffersContext.code()); glFillAmbientBufferID = 0; - deleteVertexBufferObject(glFillSpecularBufferID); + deleteVertexBufferObject(glFillSpecularBufferID, fillBuffersContext.code()); glFillSpecularBufferID = 0; - deleteVertexBufferObject(glFillEmissiveBufferID); + deleteVertexBufferObject(glFillEmissiveBufferID, fillBuffersContext.code()); glFillEmissiveBufferID = 0; - deleteVertexBufferObject(glFillShininessBufferID); + deleteVertexBufferObject(glFillShininessBufferID, fillBuffersContext.code()); glFillShininessBufferID = 0; - deleteVertexBufferObject(glFillIndexBufferID); + deleteVertexBufferObject(glFillIndexBufferID, fillBuffersContext.code()); glFillIndexBufferID = 0; fillBuffersCreated = false; @@ -1168,28 +1206,28 @@ public class PGraphicsOpenGL extends PGraphics { protected void createLineBuffers() { if (!lineBuffersCreated || lineBufferContextIsOutdated()) { - lineBuffersContext = pgl.getContext(); + lineBuffersContext = pgl.getCurrentContext(); int sizef = PGL.MAX_TESS_VERTICES * PGL.SIZEOF_FLOAT; int sizex = PGL.MAX_TESS_INDICES * PGL.SIZEOF_INDEX; int sizei = PGL.MAX_TESS_INDICES * PGL.SIZEOF_INT; - glLineVertexBufferID = createVertexBufferObject(); + glLineVertexBufferID = createVertexBufferObject(lineBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glLineColorBufferID = createVertexBufferObject(); + glLineColorBufferID = createVertexBufferObject(lineBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glLineDirWidthBufferID = createVertexBufferObject(); + glLineDirWidthBufferID = createVertexBufferObject(lineBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineDirWidthBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 4 * sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glLineIndexBufferID = createVertexBufferObject(); + glLineIndexBufferID = createVertexBufferObject(lineBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glLineIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -1235,16 +1273,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteLineBuffers() { if (lineBuffersCreated) { - deleteVertexBufferObject(glLineVertexBufferID); + deleteVertexBufferObject(glLineVertexBufferID, lineBuffersContext.code()); glLineVertexBufferID = 0; - deleteVertexBufferObject(glLineColorBufferID); + deleteVertexBufferObject(glLineColorBufferID, lineBuffersContext.code()); glLineColorBufferID = 0; - deleteVertexBufferObject(glLineDirWidthBufferID); + deleteVertexBufferObject(glLineDirWidthBufferID, lineBuffersContext.code()); glLineDirWidthBufferID = 0; - deleteVertexBufferObject(glLineIndexBufferID); + deleteVertexBufferObject(glLineIndexBufferID, lineBuffersContext.code()); glLineIndexBufferID = 0; lineBuffersCreated = false; @@ -1254,27 +1292,27 @@ public class PGraphicsOpenGL extends PGraphics { protected void createPointBuffers() { if (!pointBuffersCreated || pointBuffersContextIsOutdated()) { - pointBuffersContext = pgl.getContext(); + pointBuffersContext = pgl.getCurrentContext(); int sizef = PGL.MAX_TESS_VERTICES * PGL.SIZEOF_FLOAT; int sizex = PGL.MAX_TESS_INDICES * PGL.SIZEOF_INDEX; int sizei = PGL.MAX_TESS_INDICES * PGL.SIZEOF_INT; - glPointVertexBufferID = createVertexBufferObject(); + glPointVertexBufferID = createVertexBufferObject(pointBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glPointColorBufferID = createVertexBufferObject(); + glPointColorBufferID = createVertexBufferObject(pointBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glPointSizeBufferID = createVertexBufferObject(); + glPointSizeBufferID = createVertexBufferObject(pointBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointSizeBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 2 * sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glPointIndexBufferID = createVertexBufferObject(); + glPointIndexBufferID = createVertexBufferObject(pointBuffersContext.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glPointIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -1320,16 +1358,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deletePointBuffers() { if (pointBuffersCreated) { - deleteVertexBufferObject(glPointVertexBufferID); + deleteVertexBufferObject(glPointVertexBufferID, pointBuffersContext.code()); glPointVertexBufferID = 0; - deleteVertexBufferObject(glPointColorBufferID); + deleteVertexBufferObject(glPointColorBufferID, pointBuffersContext.code()); glPointColorBufferID = 0; - deleteVertexBufferObject(glPointSizeBufferID); + deleteVertexBufferObject(glPointSizeBufferID, pointBuffersContext.code()); glPointSizeBufferID = 0; - deleteVertexBufferObject(glPointIndexBufferID); + deleteVertexBufferObject(glPointIndexBufferID, pointBuffersContext.code()); glPointIndexBufferID = 0; pointBuffersCreated = false; @@ -2385,7 +2423,7 @@ public class PGraphicsOpenGL extends PGraphics { // If the user has been manipulating individual pixels, // the changes need to be copied to the screen before // drawing any new geometry. - renderPixels(); + flushPixels(); setgetPixels = false; } @@ -2399,15 +2437,15 @@ public class PGraphicsOpenGL extends PGraphics { } if (hasFill) { - renderFill(); + flushFill(); } if (hasPoints) { - renderPoints(); + flushPoints(); } if (hasLines) { - renderLines(); + flushLines(); } if (flushMode == FLUSH_WHEN_FULL && !hints[DISABLE_TRANSFORM_CACHE]) { @@ -2420,13 +2458,13 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void renderPixels() { + protected void flushPixels() { drawPixels(mx1, my1, mx2 - mx1 + 1, my2 - my1 + 1); modified = false; } - protected void renderPoints() { + protected void flushPoints() { updatePointBuffers(); PointShader shader = getPointShader(); @@ -2442,7 +2480,7 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void renderLines() { + protected void flushLines() { updateLineBuffers(); LineShader shader = getLineShader(); @@ -2459,7 +2497,7 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void renderFill() { + protected void flushFill() { updateFillBuffers(lights, texCache.hasTexture); texCache.beginRender(); @@ -2496,42 +2534,6 @@ public class PGraphicsOpenGL extends PGraphics { } - // Utility function to render current tessellated geometry, under the assumption that - // the texture is already bound. - protected void renderTexFill(PTexture tex) { - if (!fillBuffersCreated) { - createFillBuffers(); - fillBuffersCreated = true; - } - updateFillBuffers(lights, true); - - FillShader shader = getFillShader(lights, true); - shader.start(); - - shader.setVertexAttribute(glFillVertexBufferID, 3, PGL.GL_FLOAT, 0, 0); - shader.setColorAttribute(glFillColorBufferID, 4, PGL.GL_UNSIGNED_BYTE, 0, 0); - shader.setTexCoordAttribute(glFillTexCoordBufferID, 2, PGL.GL_FLOAT, 0, 0); - shader.setTexture(tex); - - if (lights) { - shader.setNormalAttribute(glFillNormalBufferID, 3, PGL.GL_FLOAT, 0, 0); - shader.setAmbientAttribute(glFillAmbientBufferID, 4, PGL.GL_UNSIGNED_BYTE, 0, 0); - shader.setSpecularAttribute(glFillSpecularBufferID, 4, PGL.GL_UNSIGNED_BYTE, 0, 0); - shader.setEmissiveAttribute(glFillEmissiveBufferID, 4, PGL.GL_UNSIGNED_BYTE, 0, 0); - shader.setShininessAttribute(glFillShininessBufferID, 1, PGL.GL_FLOAT, 0, 0); - } - - int size = tessGeo.fillIndexCount; - int sizex = size * PGL.SIZEOF_INDEX; - pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); - pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, ShortBuffer.wrap(tessGeo.fillIndices, 0, size), PGL.GL_STATIC_DRAW); - pgl.glDrawElements(PGL.GL_TRIANGLES, size, PGL.INDEX_TYPE, 0); - pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0); - - shader.stop(); - } - - ////////////////////////////////////////////////////////////// // BEZIER CURVE VERTICES diff --git a/java/libraries/opengl/src/processing/opengl/PShader.java b/java/libraries/opengl/src/processing/opengl/PShader.java index 88e99114a..b0dd78cb0 100644 --- a/java/libraries/opengl/src/processing/opengl/PShader.java +++ b/java/libraries/opengl/src/processing/opengl/PShader.java @@ -54,7 +54,8 @@ public class PShader { public PShader() { parent = null; pg = null; - pgl = null; + pgl = null; + context = null; this.vertexURL = null; this.fragmentURL = null; @@ -70,7 +71,8 @@ public class PShader { this(); this.parent = parent; pg = (PGraphicsOpenGL) parent.g; - pgl = pg.pgl; + pgl = pg.pgl; + context = pgl.createEmptyContext(); } /** @@ -114,13 +116,13 @@ public class PShader { protected void finalize() throws Throwable { try { if (vertexShader != 0) { - pg.finalizeGLSLVertShaderObject(vertexShader); + pg.finalizeGLSLVertShaderObject(vertexShader, context.code()); } if (fragmentShader != 0) { - pg.finalizeGLSLFragShaderObject(fragmentShader); + pg.finalizeGLSLFragShaderObject(fragmentShader, context.code()); } if (programObject != 0) { - pg.finalizeGLSLProgramObject(programObject); + pg.finalizeGLSLProgramObject(programObject, context.code()); } } finally { super.finalize(); @@ -297,8 +299,8 @@ public class PShader { protected void init() { if (programObject == 0 || contextIsOutdated()) { - context = pgl.getContext(); - programObject = pg.createGLSLProgramObject(); + context = pgl.getCurrentContext(); + programObject = pg.createGLSLProgramObject(context.code()); boolean hasVert = false; if (vertexFilename != null) { @@ -358,9 +360,9 @@ public class PShader { protected boolean contextIsOutdated() { boolean outdated = !pgl.contextIsCurrent(context); if (outdated) { - pg.removeGLSLProgramObject(programObject); - pg.removeGLSLVertShaderObject(vertexShader); - pg.removeGLSLFragShaderObject(fragmentShader); + pg.removeGLSLProgramObject(programObject, context.code()); + pg.removeGLSLVertShaderObject(vertexShader, context.code()); + pg.removeGLSLFragShaderObject(fragmentShader, context.code()); programObject = 0; vertexShader = 0; @@ -424,7 +426,7 @@ public class PShader { * @param shaderSource a string containing the shader's code */ protected boolean compileVertexShader() { - vertexShader = pg.createGLSLVertShaderObject(); + vertexShader = pg.createGLSLVertShaderObject(context.code()); pgl.glShaderSource(vertexShader, vertexShaderSource); pgl.glCompileShader(vertexShader); @@ -444,7 +446,7 @@ public class PShader { * @param shaderSource a string containing the shader's code */ protected boolean compileFragmentShader() { - fragmentShader = pg.createGLSLFragShaderObject(); + fragmentShader = pg.createGLSLFragShaderObject(context.code()); pgl.glShaderSource(fragmentShader, fragmentShaderSource); pgl.glCompileShader(fragmentShader); @@ -462,15 +464,15 @@ public class PShader { protected void release() { if (vertexShader != 0) { - pg.deleteGLSLVertShaderObject(vertexShader); + pg.deleteGLSLVertShaderObject(vertexShader, context.code()); vertexShader = 0; } if (fragmentShader != 0) { - pg.deleteGLSLFragShaderObject(fragmentShader); + pg.deleteGLSLFragShaderObject(fragmentShader, context.code()); fragmentShader = 0; } if (programObject != 0) { - pg.deleteGLSLProgramObject(programObject); + pg.deleteGLSLProgramObject(programObject, context.code()); programObject = 0; } } diff --git a/java/libraries/opengl/src/processing/opengl/PShape3D.java b/java/libraries/opengl/src/processing/opengl/PShape3D.java index 0f6447e02..7afd2f8b5 100644 --- a/java/libraries/opengl/src/processing/opengl/PShape3D.java +++ b/java/libraries/opengl/src/processing/opengl/PShape3D.java @@ -259,6 +259,7 @@ public class PShape3D extends PShape { public PShape3D(PApplet parent, int family) { pg = (PGraphicsOpenGL)parent.g; pgl = pg.pgl; + context = pgl.createEmptyContext(); prevMode = mode = DYNAMIC; @@ -399,75 +400,75 @@ public class PShape3D extends PShape { protected void finalizeFillBuffers() { if (glFillVertexBufferID != 0) { - pg.finalizeVertexBufferObject(glFillVertexBufferID); + pg.finalizeVertexBufferObject(glFillVertexBufferID, context.code()); } if (glFillColorBufferID != 0) { - pg.finalizeVertexBufferObject(glFillColorBufferID); + pg.finalizeVertexBufferObject(glFillColorBufferID, context.code()); } if (glFillNormalBufferID != 0) { - pg.finalizeVertexBufferObject(glFillNormalBufferID); + pg.finalizeVertexBufferObject(glFillNormalBufferID, context.code()); } if (glFillTexCoordBufferID != 0) { - pg.finalizeVertexBufferObject(glFillTexCoordBufferID); + pg.finalizeVertexBufferObject(glFillTexCoordBufferID, context.code()); } if (glFillAmbientBufferID != 0) { - pg.finalizeVertexBufferObject(glFillAmbientBufferID); + pg.finalizeVertexBufferObject(glFillAmbientBufferID, context.code()); } if (glFillSpecularBufferID != 0) { - pg.finalizeVertexBufferObject(glFillSpecularBufferID); + pg.finalizeVertexBufferObject(glFillSpecularBufferID, context.code()); } if (glFillEmissiveBufferID != 0) { - pg.finalizeVertexBufferObject(glFillEmissiveBufferID); + pg.finalizeVertexBufferObject(glFillEmissiveBufferID, context.code()); } if (glFillShininessBufferID != 0) { - pg.finalizeVertexBufferObject(glFillShininessBufferID); + pg.finalizeVertexBufferObject(glFillShininessBufferID, context.code()); } if (glFillIndexBufferID != 0) { - pg.finalizeVertexBufferObject(glFillIndexBufferID); + pg.finalizeVertexBufferObject(glFillIndexBufferID, context.code()); } } protected void finalizeLineBuffers() { if (glLineVertexBufferID != 0) { - pg.finalizeVertexBufferObject(glLineVertexBufferID); + pg.finalizeVertexBufferObject(glLineVertexBufferID, context.code()); } if (glLineColorBufferID != 0) { - pg.finalizeVertexBufferObject(glLineColorBufferID); + pg.finalizeVertexBufferObject(glLineColorBufferID, context.code()); } if (glLineDirWidthBufferID != 0) { - pg.finalizeVertexBufferObject(glLineDirWidthBufferID); + pg.finalizeVertexBufferObject(glLineDirWidthBufferID, context.code()); } if (glLineIndexBufferID != 0) { - pg.finalizeVertexBufferObject(glLineIndexBufferID); + pg.finalizeVertexBufferObject(glLineIndexBufferID, context.code()); } } protected void finalizePointBuffers() { if (glPointVertexBufferID != 0) { - pg.finalizeVertexBufferObject(glPointVertexBufferID); + pg.finalizeVertexBufferObject(glPointVertexBufferID, context.code()); } if (glPointColorBufferID != 0) { - pg.finalizeVertexBufferObject(glPointColorBufferID); + pg.finalizeVertexBufferObject(glPointColorBufferID, context.code()); } if (glPointSizeBufferID != 0) { - pg.finalizeVertexBufferObject(glPointSizeBufferID); + pg.finalizeVertexBufferObject(glPointSizeBufferID, context.code()); } if (glPointIndexBufferID != 0) { - pg.finalizeVertexBufferObject(glPointIndexBufferID); + pg.finalizeVertexBufferObject(glPointIndexBufferID, context.code()); } } @@ -2945,7 +2946,7 @@ public class PShape3D extends PShape { protected void copyGeometryToRoot() { if (root == this && parent == null) { - context = pgl.getContext(); + context = pgl.getCurrentContext(); // Now that we know, we can initialize the buffers with the correct size. if (0 < tess.fillVertexCount && 0 < tess.fillIndexCount) { @@ -2994,25 +2995,25 @@ public class PShape3D extends PShape { // doesn't get deleted by OpenGL. The VBOs were already // automatically disposed when the old context was // destroyed. - pg.removeVertexBufferObject(glFillVertexBufferID); - pg.removeVertexBufferObject(glFillColorBufferID); - pg.removeVertexBufferObject(glFillNormalBufferID); - pg.removeVertexBufferObject(glFillTexCoordBufferID); - pg.removeVertexBufferObject(glFillAmbientBufferID); - pg.removeVertexBufferObject(glFillSpecularBufferID); - pg.removeVertexBufferObject(glFillEmissiveBufferID); - pg.removeVertexBufferObject(glFillShininessBufferID); - pg.removeVertexBufferObject(glFillIndexBufferID); + pg.removeVertexBufferObject(glFillVertexBufferID, context.code()); + pg.removeVertexBufferObject(glFillColorBufferID, context.code()); + pg.removeVertexBufferObject(glFillNormalBufferID, context.code()); + pg.removeVertexBufferObject(glFillTexCoordBufferID, context.code()); + pg.removeVertexBufferObject(glFillAmbientBufferID, context.code()); + pg.removeVertexBufferObject(glFillSpecularBufferID, context.code()); + pg.removeVertexBufferObject(glFillEmissiveBufferID, context.code()); + pg.removeVertexBufferObject(glFillShininessBufferID, context.code()); + pg.removeVertexBufferObject(glFillIndexBufferID, context.code()); - pg.removeVertexBufferObject(glLineVertexBufferID); - pg.removeVertexBufferObject(glLineColorBufferID); - pg.removeVertexBufferObject(glLineDirWidthBufferID); - pg.removeVertexBufferObject(glLineIndexBufferID); + pg.removeVertexBufferObject(glLineVertexBufferID, context.code()); + pg.removeVertexBufferObject(glLineColorBufferID, context.code()); + pg.removeVertexBufferObject(glLineDirWidthBufferID, context.code()); + pg.removeVertexBufferObject(glLineIndexBufferID, context.code()); - pg.removeVertexBufferObject(glPointVertexBufferID); - pg.removeVertexBufferObject(glPointColorBufferID); - pg.removeVertexBufferObject(glPointSizeBufferID); - pg.removeVertexBufferObject(glPointIndexBufferID); + pg.removeVertexBufferObject(glPointVertexBufferID, context.code()); + pg.removeVertexBufferObject(glPointColorBufferID, context.code()); + pg.removeVertexBufferObject(glPointSizeBufferID, context.code()); + pg.removeVertexBufferObject(glPointIndexBufferID, context.code()); // The OpenGL resources have been already deleted // when the context changed. We only need to zero @@ -3047,41 +3048,41 @@ public class PShape3D extends PShape { int sizei = nvert * PGL.SIZEOF_INT; int sizex = nind * PGL.SIZEOF_INDEX; - glFillVertexBufferID = pg.createVertexBufferObject(); + glFillVertexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glFillColorBufferID = pg.createVertexBufferObject(); + glFillColorBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillNormalBufferID = pg.createVertexBufferObject(); + glFillNormalBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillNormalBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glFillTexCoordBufferID = pg.createVertexBufferObject(); + glFillTexCoordBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillTexCoordBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 2 * sizef, null, PGL.GL_STATIC_DRAW); - glFillAmbientBufferID = pg.createVertexBufferObject(); + glFillAmbientBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillAmbientBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillSpecularBufferID = pg.createVertexBufferObject(); + glFillSpecularBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillSpecularBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillEmissiveBufferID = pg.createVertexBufferObject(); + glFillEmissiveBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillEmissiveBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glFillShininessBufferID = pg.createVertexBufferObject(); + glFillShininessBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glFillShininessBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glFillIndexBufferID = pg.createVertexBufferObject(); + glFillIndexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -3391,21 +3392,21 @@ public class PShape3D extends PShape { int sizei = nvert * PGL.SIZEOF_INT; int sizex = nind * PGL.SIZEOF_INDEX; - glLineVertexBufferID = pg.createVertexBufferObject(); + glLineVertexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glLineColorBufferID = pg.createVertexBufferObject(); + glLineColorBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glLineDirWidthBufferID = pg.createVertexBufferObject(); + glLineDirWidthBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glLineDirWidthBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 4 * sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glLineIndexBufferID = pg.createVertexBufferObject(); + glLineIndexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glLineIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -3485,21 +3486,21 @@ public class PShape3D extends PShape { int sizei = nvert * PGL.SIZEOF_INT; int sizex = nind * PGL.SIZEOF_INDEX; - glPointVertexBufferID = pg.createVertexBufferObject(); + glPointVertexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointVertexBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 3 * sizef, null, PGL.GL_STATIC_DRAW); - glPointColorBufferID = pg.createVertexBufferObject(); + glPointColorBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointColorBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, sizei, null, PGL.GL_STATIC_DRAW); - glPointSizeBufferID = pg.createVertexBufferObject(); + glPointSizeBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, glPointSizeBufferID); pgl.glBufferData(PGL.GL_ARRAY_BUFFER, 2 * sizef, null, PGL.GL_STATIC_DRAW); pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0); - glPointIndexBufferID = pg.createVertexBufferObject(); + glPointIndexBufferID = pg.createVertexBufferObject(context.code()); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glPointIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, null, PGL.GL_STATIC_DRAW); @@ -3590,47 +3591,47 @@ public class PShape3D extends PShape { protected void deleteFillBuffers() { if (glFillVertexBufferID != 0) { - pg.deleteVertexBufferObject(glFillVertexBufferID); + pg.deleteVertexBufferObject(glFillVertexBufferID, context.code()); glFillVertexBufferID = 0; } if (glFillColorBufferID != 0) { - pg.deleteVertexBufferObject(glFillColorBufferID); + pg.deleteVertexBufferObject(glFillColorBufferID, context.code()); glFillColorBufferID = 0; } if (glFillNormalBufferID != 0) { - pg.deleteVertexBufferObject(glFillNormalBufferID); + pg.deleteVertexBufferObject(glFillNormalBufferID, context.code()); glFillNormalBufferID = 0; } if (glFillTexCoordBufferID != 0) { - pg.deleteVertexBufferObject(glFillTexCoordBufferID); + pg.deleteVertexBufferObject(glFillTexCoordBufferID, context.code()); glFillTexCoordBufferID = 0; } if (glFillAmbientBufferID != 0) { - pg.deleteVertexBufferObject(glFillAmbientBufferID); + pg.deleteVertexBufferObject(glFillAmbientBufferID, context.code()); glFillAmbientBufferID = 0; } if (glFillSpecularBufferID != 0) { - pg.deleteVertexBufferObject(glFillSpecularBufferID); + pg.deleteVertexBufferObject(glFillSpecularBufferID, context.code()); glFillSpecularBufferID = 0; } if (glFillEmissiveBufferID != 0) { - pg.deleteVertexBufferObject(glFillEmissiveBufferID); + pg.deleteVertexBufferObject(glFillEmissiveBufferID, context.code()); glFillEmissiveBufferID = 0; } if (glFillShininessBufferID != 0) { - pg.deleteVertexBufferObject(glFillShininessBufferID); + pg.deleteVertexBufferObject(glFillShininessBufferID, context.code()); glFillShininessBufferID = 0; } if (glFillIndexBufferID != 0) { - pg.deleteVertexBufferObject(glFillIndexBufferID); + pg.deleteVertexBufferObject(glFillIndexBufferID, context.code()); glFillIndexBufferID = 0; } } @@ -3638,22 +3639,22 @@ public class PShape3D extends PShape { protected void deleteLineBuffers() { if (glLineVertexBufferID != 0) { - pg.deleteVertexBufferObject(glLineVertexBufferID); + pg.deleteVertexBufferObject(glLineVertexBufferID, context.code()); glLineVertexBufferID = 0; } if (glLineColorBufferID != 0) { - pg.deleteVertexBufferObject(glLineColorBufferID); + pg.deleteVertexBufferObject(glLineColorBufferID, context.code()); glLineColorBufferID = 0; } if (glLineDirWidthBufferID != 0) { - pg.deleteVertexBufferObject(glLineDirWidthBufferID); + pg.deleteVertexBufferObject(glLineDirWidthBufferID, context.code()); glLineDirWidthBufferID = 0; } if (glLineIndexBufferID != 0) { - pg.deleteVertexBufferObject(glLineIndexBufferID); + pg.deleteVertexBufferObject(glLineIndexBufferID, context.code()); glLineIndexBufferID = 0; } } @@ -3661,22 +3662,22 @@ public class PShape3D extends PShape { protected void deletePointBuffers() { if (glPointVertexBufferID != 0) { - pg.deleteVertexBufferObject(glPointVertexBufferID); + pg.deleteVertexBufferObject(glPointVertexBufferID, context.code()); glPointVertexBufferID = 0; } if (glPointColorBufferID != 0) { - pg.deleteVertexBufferObject(glPointColorBufferID); + pg.deleteVertexBufferObject(glPointColorBufferID, context.code()); glPointColorBufferID = 0; } if (glPointSizeBufferID != 0) { - pg.deleteVertexBufferObject(glPointSizeBufferID); + pg.deleteVertexBufferObject(glPointSizeBufferID, context.code()); glPointSizeBufferID = 0; } if (glPointIndexBufferID != 0) { - pg.deleteVertexBufferObject(glPointIndexBufferID); + pg.deleteVertexBufferObject(glPointIndexBufferID, context.code()); glPointIndexBufferID = 0; } } diff --git a/java/libraries/opengl/src/processing/opengl/PTexture.java b/java/libraries/opengl/src/processing/opengl/PTexture.java index c7655b653..a6ac6ffe5 100644 --- a/java/libraries/opengl/src/processing/opengl/PTexture.java +++ b/java/libraries/opengl/src/processing/opengl/PTexture.java @@ -102,6 +102,7 @@ public class PTexture implements PConstants { pg = (PGraphicsOpenGL)parent.g; pgl = pg.pgl; + context = pgl.createEmptyContext(); glID = 0; @@ -112,7 +113,7 @@ public class PTexture implements PConstants { protected void finalize() throws Throwable { try { if (glID != 0) { - pg.finalizeTextureObject(glID); + pg.finalizeTextureObject(glID, context.code()); } } finally { super.finalize(); @@ -754,8 +755,8 @@ public class PTexture implements PConstants { pgl.enableTexturing(glTarget); - context = pgl.getContext(); - glID = pg.createTextureObject(); + context = pgl.getCurrentContext(); + glID = pg.createTextureObject(context.code()); pgl.glBindTexture(glTarget, glID); pgl.glTexParameterf(glTarget, PGL.GL_TEXTURE_MIN_FILTER, glMinFilter); @@ -780,7 +781,7 @@ public class PTexture implements PConstants { */ protected void release() { if (glID != 0) { - pg.finalizeTextureObject(glID); + pg.finalizeTextureObject(glID, context.code()); glID = 0; } } @@ -792,7 +793,7 @@ public class PTexture implements PConstants { // 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. - pg.removeTextureObject(glID); + pg.removeTextureObject(glID, context.code()); // 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.