From 93ee78c6002dcf7096fbd1855a4ea173ef941f17 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 28 May 2013 14:17:23 -0400 Subject: [PATCH 1/3] keeping track of bound textures on all units --- core/src/processing/opengl/PGL.java | 65 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index d973952ea..40f5d69b5 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -287,8 +287,10 @@ public class PGL { /** Which texturing targets are enabled */ protected static boolean[] texturingTargets = { false, false }; - /** Which textures are bound to each target */ - protected static int[] boundTextures = { 0, 0 }; + /** Used to keep track of which textures are bound to each target */ + protected static int maxTexUnits; + protected static int activeTexUnit = 0; + protected static int[][] boundTextures; /////////////////////////////////////////////////////////// @@ -402,6 +404,9 @@ public class PGL { protected static final String MISSING_GLFUNC_ERROR = "GL function %1$s is not available on this hardware (or driver)"; + protected static final String TEXUNIT_ERROR = + "Number of texture units not supported by this hardware (or driver)"; + /////////////////////////////////////////////////////////// @@ -703,7 +708,7 @@ public class PGL { if (USE_JOGL_FBOLAYER) { Texture tex = new Texture(); tex.init(pg.width, pg.height, - backTexAttach.getName(), GL.GL_TEXTURE_2D, GL.GL_RGBA, + backTexAttach.getName(), TEXTURE_2D, RGBA, backTexAttach.getWidth(), backTexAttach.getHeight(), backTexAttach.minFilter, backTexAttach.magFilter, backTexAttach.wrapS, backTexAttach.wrapT); @@ -729,7 +734,7 @@ public class PGL { if (USE_JOGL_FBOLAYER) { Texture tex = new Texture(); tex.init(pg.width, pg.height, - backTexAttach.getName(), GL.GL_TEXTURE_2D, GL.GL_RGBA, + backTexAttach.getName(), TEXTURE_2D, RGBA, frontTexAttach.getWidth(), frontTexAttach.getHeight(), frontTexAttach.minFilter, frontTexAttach.magFilter, frontTexAttach.wrapS, frontTexAttach.wrapT); @@ -769,10 +774,10 @@ public class PGL { protected void bindFrontTexture() { if (USE_JOGL_FBOLAYER) { - if (!texturingIsEnabled(GL.GL_TEXTURE_2D)) { - enableTexturing(GL.GL_TEXTURE_2D); + if (!texturingIsEnabled(TEXTURE_2D)) { + enableTexturing(TEXTURE_2D); } - gl.glBindTexture(GL.GL_TEXTURE_2D, frontTexAttach.getName()); + bindTexture(TEXTURE_2D, frontTexAttach.getName()); } else { if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -784,15 +789,15 @@ public class PGL { protected void unbindFrontTexture() { if (USE_JOGL_FBOLAYER) { - if (textureIsBound(GL.GL_TEXTURE_2D, frontTexAttach.getName())) { + if (textureIsBound(TEXTURE_2D, frontTexAttach.getName())) { // We don't want to unbind another texture // that might be bound instead of this one. - if (!texturingIsEnabled(GL.GL_TEXTURE_2D)) { - enableTexturing(GL.GL_TEXTURE_2D); - gl.glBindTexture(GL.GL_TEXTURE_2D, 0); - disableTexturing(GL.GL_TEXTURE_2D); + if (!texturingIsEnabled(TEXTURE_2D)) { + enableTexturing(TEXTURE_2D); + bindTexture(TEXTURE_2D, 0); + disableTexturing(TEXTURE_2D); } else { - gl.glBindTexture(GL.GL_TEXTURE_2D, 0); + bindTexture(TEXTURE_2D, 0); } } } else { @@ -1046,7 +1051,7 @@ public class PGL { // the sketch uses "incremental drawing" (background() not called). frontFBO.bind(gl); gl.glDisable(GL.GL_BLEND); - drawTexture(GL.GL_TEXTURE_2D, backTexAttach.getName(), + drawTexture(TEXTURE_2D, backTexAttach.getName(), backTexAttach.getWidth(), backTexAttach.getHeight(), pg.width, pg.height, 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height); @@ -1330,10 +1335,12 @@ public class PGL { protected boolean textureIsBound(int target, int id) { + if (boundTextures == null) return false; + if (target == TEXTURE_2D) { - return boundTextures[0] == id; + return boundTextures[activeTexUnit][0] == id; } else if (target == TEXTURE_RECTANGLE) { - return boundTextures[1] == id; + return boundTextures[activeTexUnit][1] == id; } else { return false; } @@ -2075,6 +2082,12 @@ public class PGL { } + protected int getMaxTexUnits() { + getIntegerv(MAX_TEXTURE_IMAGE_UNITS, intBuffer); + return intBuffer.get(0); + } + + protected static ByteBuffer allocateDirectByteBuffer(int size) { int bytes = PApplet.max(MIN_DIRECT_BUFFER_SIZE, size) * SIZEOF_BYTE; return ByteBuffer.allocateDirect(bytes).order(ByteOrder.nativeOrder()); @@ -2794,8 +2807,9 @@ public class PGL { public static final int TEXTURE_MAX_ANISOTROPY = GL.GL_TEXTURE_MAX_ANISOTROPY_EXT; public static final int MAX_TEXTURE_MAX_ANISOTROPY = GL.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT; - public static final int MAX_VERTEX_TEXTURE_IMAGE_UNITS = GL2ES2.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS; - public static final int MAX_TEXTURE_IMAGE_UNITS = GL2ES2.GL_MAX_TEXTURE_IMAGE_UNITS; + public static final int MAX_VERTEX_TEXTURE_IMAGE_UNITS = GL2ES2.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS; + public static final int MAX_TEXTURE_IMAGE_UNITS = GL2ES2.GL_MAX_TEXTURE_IMAGE_UNITS; + public static final int MAX_COMBINED_TEXTURE_IMAGE_UNITS = GL2ES2.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS; public static final int NEAREST = GL.GL_NEAREST; public static final int LINEAR = GL.GL_LINEAR; @@ -3226,6 +3240,7 @@ public class PGL { public void activeTexture(int texture) { gl.glActiveTexture(texture); + activeTexUnit = texture - TEXTURE0; } public void texImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, Buffer data) { @@ -3274,10 +3289,20 @@ public class PGL { public void bindTexture(int target, int texture) { gl.glBindTexture(target, texture); + + if (boundTextures == null) { + maxTexUnits = getMaxTexUnits(); + boundTextures = new int[maxTexUnits][2]; + } + + if (maxTexUnits <= activeTexUnit) { + throw new RuntimeException(TEXUNIT_ERROR); + } + if (target == TEXTURE_2D) { - boundTextures[0] = texture; + boundTextures[activeTexUnit][0] = texture; } else if (target == TEXTURE_RECTANGLE) { - boundTextures[1] = texture; + boundTextures[activeTexUnit][1] = texture; } } From be83796795ea7862186fe600ccbdd4fc36d11609 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 28 May 2013 14:44:22 -0400 Subject: [PATCH 2/3] removed PGL.update(), and updatePrimary, updateOffscreen() in PGraphicsOpenGL. --- core/src/processing/opengl/PGL.java | 18 +++++------------- .../src/processing/opengl/PGraphicsOpenGL.java | 12 +----------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 40f5d69b5..ac429025d 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -295,6 +295,7 @@ public class PGL { /////////////////////////////////////////////////////////// // FBO layer + protected static boolean fboLayerRequested = false; protected static boolean fboLayerCreated = false; protected static boolean fboLayerInUse = false; @@ -588,19 +589,6 @@ public class PGL { } - protected void update() { - if (!setFps) setFps(targetFps); - - if (fboLayerRequested && !fboLayerCreated && !USE_JOGL_FBOLAYER) { - createFBOLayer(); - } -// if (USE_JOGL_FBOLAYER) return; -// if (!fboLayerCreated) { -// createFBOLayer(); -// } - } - - protected int getReadFramebuffer() { if (fboLayerInUse) { return glColorFbo.get(0); @@ -993,9 +981,13 @@ public class PGL { protected void beginDraw(boolean clear0) { + if (!setFps) setFps(targetFps); + if (USE_JOGL_FBOLAYER) return; if (needFBOLayer(clear0)) { + if (!fboLayerCreated) createFBOLayer(); + bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0)); framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D, glColorTex.get(backTex), 0); diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 314e1d4cf..8512aab4e 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -5852,13 +5852,7 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void updatePrimary() { - pgl.update(); - } - - protected void beginOnscreenDraw() { - updatePrimary(); pgl.beginDraw(clearColorBuffer); if (drawFramebuffer == null) { @@ -5939,7 +5933,7 @@ public class PGraphicsOpenGL extends PGraphics { } - protected void updateOffscreen() { + protected void beginOffscreenDraw() { if (!initialized) { initOffscreen(); } else { @@ -5964,11 +5958,7 @@ public class PGraphicsOpenGL extends PGraphics { } else { setFramebuffer(offscreenFramebuffer); } - } - - protected void beginOffscreenDraw() { - updateOffscreen(); // Render previous back texture (now is the front) as background drawPTexture(); From d1ac58a07690664eed8a08525b04087ad3f5a5f0 Mon Sep 17 00:00:00 2001 From: REAS Date: Tue, 28 May 2013 11:51:41 -0700 Subject: [PATCH 3/3] Reference additions for Lists and Dicts --- core/src/processing/data/FloatList.java | 13 ++++++++++--- core/src/processing/data/IntList.java | 11 +++++++---- core/src/processing/data/StringList.java | 7 +++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 24ab1d9ba..31479e4cc 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -8,6 +8,13 @@ import processing.core.PApplet; /** + * Helper class for a list of floats. Lists are designed to have some of the + * features of ArrayLists, but to maintain the simplicity and efficiency of + * working with arrays. + * + * Functions like sort() and shuffle() always act on the list itself. To get + * a sorted copy, use list.copy().sort(). + * * @webref data:composite */ public class FloatList implements Iterable { @@ -665,7 +672,7 @@ public class FloatList implements Iterable { * @webref floatlist:method * @brief Create a new array with a copy of all the values */ - public int[] array() { + public float[] array() { return array(null); } @@ -674,9 +681,9 @@ public class FloatList implements Iterable { * Copy as many values as possible into the specified array. * @param array */ - public int[] array(int[] array) { + public float[] array(float[] array) { if (array == null || array.length != count) { - array = new int[count]; + array = new float[count]; } System.arraycopy(data, 0, array, 0, count); return array; diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 3837bfd3d..58780421a 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -13,9 +13,12 @@ import processing.core.PApplet; /** - * Helper class for a list of ints. By design (for efficiency), functions like - * sort() and shuffle() always act on the list itself. To get a sorted copy, - * use list.copy().sort(). + * Helper class for a list of ints. Lists are designed to have some of the + * features of ArrayLists, but to maintain the simplicity and efficiency of + * working with arrays. + * + * Functions like sort() and shuffle() always act on the list itself. To get + * a sorted copy, use list.copy().sort(). * * @webref data:composite */ @@ -338,7 +341,7 @@ public class IntList implements Iterable { /** * @webref floatlist:method - * @brief Check if a number is a part of the data structure + * @brief Check if a number is a part of the list */ public boolean hasValue(int value) { // if (indexCache == null) { diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index 1abfd1ce9..abc1cae8d 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -7,6 +7,13 @@ import java.util.Random; import processing.core.PApplet; /** + * Helper class for a list of Strings. Lists are designed to have some of the + * features of ArrayLists, but to maintain the simplicity and efficiency of + * working with arrays. + * + * Functions like sort() and shuffle() always act on the list itself. To get + * a sorted copy, use list.copy().sort(). + * * @webref data:composite */ public class StringList implements Iterable {