diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index dde84424c..849b528db 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -350,12 +350,6 @@ public abstract class PGL { } - protected void checkPrimary() { - if (!primaryPGL) { - throw new RuntimeException(NONPRIMARY_ERROR); - } - } - /** * Return the native canvas the OpenGL context associated to this PGL object * is rendering to (if any). @@ -376,8 +370,6 @@ public abstract class PGL { protected void deleteSurface() { - checkPrimary(); - if (threadIsCurrent() && fboLayerCreated) { deleteTextures(2, glColorTex); deleteFramebuffers(1, glColorFbo); @@ -395,13 +387,11 @@ public abstract class PGL { protected int getReadFramebuffer() { - checkPrimary(); return fboLayerInUse ? glColorFbo.get(0) : 0; } protected int getDrawFramebuffer() { - checkPrimary(); if (fboLayerInUse) return 1 < numSamples ? glMultiFbo.get(0) : glColorFbo.get(0); else return 0; @@ -409,31 +399,26 @@ public abstract class PGL { protected int getDefaultDrawBuffer() { - checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } protected int getDefaultReadBuffer() { - checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } - protected boolean isFBOBacked() { - checkPrimary(); + protected boolean isFBOBacked() {; return fboLayerInUse; } protected void requestFBOLayer() { - checkPrimary(); fboLayerRequested = true; } protected boolean isMultisampled() { - checkPrimary(); return 1 < numSamples; } @@ -467,7 +452,6 @@ public abstract class PGL { protected Texture wrapBackTexture(Texture texture) { - checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -485,7 +469,6 @@ public abstract class PGL { protected Texture wrapFrontTexture(Texture texture) { - checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -502,7 +485,6 @@ public abstract class PGL { protected void bindFrontTexture() { - checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -512,7 +494,6 @@ public abstract class PGL { protected void unbindFrontTexture() { - checkPrimary(); if (textureIsBound(TEXTURE_2D, glColorTex.get(frontTex))) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -528,7 +509,6 @@ public abstract class PGL { protected void syncBackTexture() { - checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { bindFramebuffer(READ_FRAMEBUFFER, glMultiFbo.get(0)); @@ -546,7 +526,6 @@ public abstract class PGL { protected void beginDraw(boolean clear0) { - checkPrimary(); if (needFBOLayer(clear0)) { if (!fboLayerCreated) createFBOLayer(); @@ -597,7 +576,6 @@ public abstract class PGL { protected void endDraw(boolean clear0) { - checkPrimary(); if (fboLayerInUse) { syncBackTexture(); @@ -655,7 +633,6 @@ public abstract class PGL { private void createFBOLayer() { - System.out.println("Creating FBO layer"); String ext = getString(EXTENSIONS); if (-1 < ext.indexOf("texture_non_power_of_two")) { fboWidth = pg.width; diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index b86ece976..1dd4640bf 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -508,7 +508,9 @@ public class PGraphicsOpenGL extends PGraphics { "Stroke path is too long, some bevel triangles won't be added"; static final String TESSELLATION_ERROR = "Tessellation Error: %1$s"; - + static final String TEXTURE_SIZE_ERROR = + "The backing texture for the primary surface has a width/height different " + + "from the one given in size()."; ////////////////////////////////////////////////////////////// @@ -5348,7 +5350,7 @@ public class PGraphicsOpenGL extends PGraphics { * Don't use this inside glBegin/glEnd otherwise it'll * throw an GL_INVALID_OPERATION error. */ - public void report(String where) { + protected void report(String where) { if (!hints[DISABLE_OPENGL_ERRORS]) { int err = pgl.getError(); if (err != 0) { @@ -5499,11 +5501,12 @@ public class PGraphicsOpenGL extends PGraphics { // non-multisampled FBO, texture is actually the color buffer used by the // color FBO, so with the copy operation we should be done updating the // (off)screen buffer. - // First, copy the pixels to the texture. We don't need to invert the // pixel copy because the texture will be drawn inverted. + int tw = PApplet.min(texture.glWidth - x, w); + int th = PApplet.min(texture.glHeight - y, h); pgl.copyToTexture(texture.glTarget, texture.glFormat, texture.glName, - x, y, w, h, nativePixelBuffer); + x, y, tw, th, nativePixelBuffer); beginPixelsOp(OP_WRITE); drawTexture(x, y, w, h); endPixelsOp(); @@ -6257,6 +6260,10 @@ public class PGraphicsOpenGL extends PGraphics { if (pgl.isFBOBacked()) { texture = pgl.wrapBackTexture(texture); ptexture = pgl.wrapFrontTexture(ptexture); + + if (texture.glWidth != width || texture.glHeight != height) { + PGraphics.showWarning(TEXTURE_SIZE_ERROR); + } } } diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index fb618f50c..2725879b0 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -79,39 +79,6 @@ public class PJOGL extends PGL { /** Selected GL profile */ public static GLProfile profile; - static { - if (PROFILE == 2) { - try { - profile = GLProfile.getGL2ES1(); - } catch (GLException ex) { - profile = GLProfile.getMaxFixedFunc(true); - } - } else if (PROFILE == 3) { - try { - profile = GLProfile.getGL2GL3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL3()) { - PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); - } - } else if (PROFILE == 4) { - try { - profile = GLProfile.getGL4ES3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL4()) { - PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); - } - } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); - - if (2 < PROFILE) { - texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); - tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); - texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); - } - } // ........................................................ @@ -183,11 +150,17 @@ public class PJOGL extends PGL { /** The AWT-OpenGL canvas */ protected GLCanvas canvasAWT; - /** The NEWT-OpenGL canvas */ - protected NewtCanvasAWT canvasNEWT; + /** The shared AWT-OpenGL canvas */ + protected static GLCanvas sharedCanvasAWT; /** The NEWT window */ - protected GLWindow window; + protected GLWindow windowNEWT; + + /** The shared NEWT window */ + protected static GLWindow sharedWindowNEWT; + + /** The NEWT-OpenGL canvas */ + protected NewtCanvasAWT canvasNEWT; /** The listener that fires the frame rendering in Processing */ protected PGLListener listener; @@ -254,7 +227,6 @@ public class PJOGL extends PGL { @Override protected void setFps(float fps) { - checkPrimary(); if (!setFps || targetFps != fps) { if (60 < fps) { // Disables v-sync @@ -272,16 +244,50 @@ public class PJOGL extends PGL { @Override protected void initSurface(int antialias) { - checkPrimary(); + if (profile == null) { + if (PROFILE == 2) { + try { + profile = GLProfile.getGL2ES1(); + } catch (GLException ex) { + profile = GLProfile.getMaxFixedFunc(true); + } + } else if (PROFILE == 3) { + try { + profile = GLProfile.getGL2GL3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL3()) { + PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); + } + } else if (PROFILE == 4) { + try { + profile = GLProfile.getGL4ES3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL4()) { + PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); + } + } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); + + if (2 < PROFILE) { + texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); + tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); + texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); + } + } if (canvasAWT != null && canvasNEWT != null) { // Restarting... if (canvasAWT != null) { + sharedCanvasAWT = null; canvasAWT.removeGLEventListener(listener); pg.parent.removeListeners(canvasAWT); pg.parent.remove(canvasAWT); } else if (canvasNEWT != null) { - window.removeGLEventListener(listener); + sharedWindowNEWT = null; + windowNEWT.removeGLEventListener(listener); pg.parent.remove(canvasNEWT); } sinkFBO = backFBO = frontFBO = null; @@ -328,6 +334,11 @@ public class PJOGL extends PGL { if (WINDOW_TOOLKIT == AWT) { canvasAWT = new GLCanvas(caps); + if (sharedCanvasAWT == null) { + sharedCanvasAWT = canvasAWT; + } else { + canvasAWT.setSharedAutoDrawable(sharedCanvasAWT); + } canvasAWT.setBounds(0, 0, pg.width, pg.height); canvasAWT.setBackground(new Color(pg.backgroundColor, true)); canvasAWT.setFocusable(true); @@ -339,8 +350,13 @@ public class PJOGL extends PGL { canvas = canvasAWT; canvasNEWT = null; } else if (WINDOW_TOOLKIT == NEWT) { - window = GLWindow.create(caps); - canvasNEWT = new NewtCanvasAWT(window); + windowNEWT = GLWindow.create(caps); + if (sharedWindowNEWT == null) { + sharedWindowNEWT = windowNEWT; + } else { + windowNEWT.setSharedAutoDrawable(sharedWindowNEWT); + } + canvasNEWT = new NewtCanvasAWT(windowNEWT); canvasNEWT.setBounds(0, 0, pg.width, pg.height); canvasNEWT.setBackground(new Color(pg.backgroundColor, true)); canvasNEWT.setFocusable(true); @@ -364,7 +380,6 @@ public class PJOGL extends PGL { @Override protected void reinitSurface() { - checkPrimary(); sinkFBO = backFBO = frontFBO = null; fboLayerCreated = false; fboLayerInUse = false; @@ -383,18 +398,18 @@ public class PJOGL extends PGL { } else if (WINDOW_TOOLKIT == NEWT) { if (EVENTS_TOOLKIT == NEWT) { NEWTMouseListener mouseListener = new NEWTMouseListener(); - window.addMouseListener(mouseListener); + windowNEWT.addMouseListener(mouseListener); NEWTKeyListener keyListener = new NEWTKeyListener(); - window.addKeyListener(keyListener); + windowNEWT.addKeyListener(keyListener); NEWTWindowListener winListener = new NEWTWindowListener(); - window.addWindowListener(winListener); + windowNEWT.addWindowListener(winListener); } else if (EVENTS_TOOLKIT == AWT) { pg.parent.removeListeners(canvasNEWT); pg.parent.addListeners(canvasNEWT); } listener = new PGLListener(); - window.addGLEventListener(listener); + windowNEWT.addGLEventListener(listener); } if (canvas != null) { @@ -411,16 +426,14 @@ public class PJOGL extends PGL { canvasAWT.removeGLEventListener(listener); pg.parent.removeListeners(canvasAWT); } else if (canvasNEWT != null) { - window.removeGLEventListener(listener); + windowNEWT.removeGLEventListener(listener); } GLProfile.shutdown(); - System.out.println("bye bye"); } @Override protected int getReadFramebuffer() { - checkPrimary(); if (fboLayerInUse) { return glColorFbo.get(0); } else if (capabilities.isFBO()) { @@ -433,7 +446,6 @@ public class PJOGL extends PGL { @Override protected int getDrawFramebuffer() { - checkPrimary(); if (fboLayerInUse) { if (1 < numSamples) { return glMultiFbo.get(0); @@ -450,7 +462,6 @@ public class PJOGL extends PGL { @Override protected int getDefaultDrawBuffer() { - checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -465,7 +476,6 @@ public class PJOGL extends PGL { @Override protected int getDefaultReadBuffer() { - checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -480,7 +490,6 @@ public class PJOGL extends PGL { @Override protected boolean isFBOBacked() { - checkPrimary(); return super.isFBOBacked() || capabilities.isFBO(); } @@ -499,7 +508,6 @@ public class PJOGL extends PGL { @Override protected Texture wrapBackTexture(Texture texture) { - checkPrimary(); if (texture == null || changedBackTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -527,7 +535,6 @@ public class PJOGL extends PGL { @Override protected Texture wrapFrontTexture(Texture texture) { - checkPrimary(); if (texture == null || changedFrontTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -555,7 +562,6 @@ public class PJOGL extends PGL { @Override protected void bindFrontTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -568,7 +574,6 @@ public class PJOGL extends PGL { @Override protected void unbindFrontTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); if (textureIsBound(TEXTURE_2D, frontTexAttach.getName())) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -587,7 +592,6 @@ public class PJOGL extends PGL { @Override protected void syncBackTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { backFBO.syncSamplingSink(gl); @@ -656,7 +660,6 @@ public class PJOGL extends PGL { @Override protected void requestDraw() { - checkPrimary(); boolean canDraw = pg.parent.canDraw(); if (pg.initialized && (canDraw || prevCanDraw)) { try { @@ -664,7 +667,7 @@ public class PJOGL extends PGL { if (WINDOW_TOOLKIT == AWT) { canvasAWT.display(); } else if (WINDOW_TOOLKIT == NEWT) { - window.display(); + windowNEWT.display(); } try { drawLatch.await(DRAW_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); @@ -689,11 +692,10 @@ public class PJOGL extends PGL { @Override protected void swapBuffers() { - checkPrimary(); if (WINDOW_TOOLKIT == AWT) { canvasAWT.swapBuffers(); } else if (WINDOW_TOOLKIT == NEWT) { - window.swapBuffers(); + windowNEWT.swapBuffers(); } }