From 969a779be2e91c9c5482c8f1dde63c1d407771c8 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Fri, 23 Mar 2012 16:29:03 +0000 Subject: [PATCH] Depth/stencil bits query, more GL constants supported --- .../src/processing/core/PFramebuffer.java | 24 +++---- android/core/src/processing/core/PGL.java | 71 ++++++++++--------- .../processing/core/PGraphicsAndroid3D.java | 44 +++++++----- 3 files changed, 76 insertions(+), 63 deletions(-) diff --git a/android/core/src/processing/core/PFramebuffer.java b/android/core/src/processing/core/PFramebuffer.java index 1fe02e77b..b2335f1be 100644 --- a/android/core/src/processing/core/PFramebuffer.java +++ b/android/core/src/processing/core/PFramebuffer.java @@ -49,7 +49,7 @@ public class PFramebuffer implements PConstants { protected int depthBits; protected int stencilBits; - protected boolean combinedDepthStencil; + protected boolean packedDepthStencil; protected boolean multisample; protected int nsamples; @@ -71,7 +71,7 @@ public class PFramebuffer implements PConstants { } PFramebuffer(PApplet parent, int w, int h, int samples, int colorBuffers, - int depthBits, int stencilBits, boolean combinedDepthStencil, + int depthBits, int stencilBits, boolean packedDepthStencil, boolean screen) { this.parent = parent; pg = (PGraphicsAndroid3D)parent.g; @@ -110,18 +110,18 @@ public class PFramebuffer implements PConstants { if (depthBits < 1 && stencilBits < 1) { this.depthBits = 0; this.stencilBits = 0; - this.combinedDepthStencil = false; + this.packedDepthStencil = false; } else { - if (combinedDepthStencil) { + if (packedDepthStencil) { // When combined depth/stencil format is required, the depth and stencil bits // are overriden and the 24/8 combination for a 32 bits surface is used. this.depthBits = 24; this.stencilBits = 8; - this.combinedDepthStencil = true; + this.packedDepthStencil = true; } else { this.depthBits = depthBits; this.stencilBits = stencilBits; - this.combinedDepthStencil = false; + this.packedDepthStencil = false; } } @@ -173,11 +173,7 @@ public class PFramebuffer implements PConstants { } public void bind() { - if (screenFb) { - pgl.glBindFramebuffer(PGL.GL_FRAMEBUFFER, 0); - } else { - pgl.glBindFramebuffer(PGL.GL_FRAMEBUFFER, glFboID); - } + pgl.glBindFramebuffer(PGL.GL_FRAMEBUFFER, glFboID); } public void disableDepthTest() { @@ -285,8 +281,8 @@ public class PFramebuffer implements PConstants { createColorBufferMultisample(); } - if (combinedDepthStencil) { - createCombinedDepthStencilBuffer(); + if (packedDepthStencil) { + createPackedDepthStencilBuffer(); } else { if (0 < depthBits) { createDepthBuffer(); @@ -354,7 +350,7 @@ public class PFramebuffer implements PConstants { } - protected void createCombinedDepthStencilBuffer() { + protected void createPackedDepthStencilBuffer() { if (screenFb) return; if (width == 0 || height == 0) { diff --git a/android/core/src/processing/core/PGL.java b/android/core/src/processing/core/PGL.java index f3cd9eec7..afadfcdfd 100644 --- a/android/core/src/processing/core/PGL.java +++ b/android/core/src/processing/core/PGL.java @@ -90,14 +90,14 @@ public class PGL { /** Maximum dimension of a texture used to hold font data. **/ public static final int MAX_FONT_TEX_SIZE = 256; - - public static int DEFAULT_DEPTH_BITS = 16; - public static int DEFAULT_STENCIL_BITS = 8; - + /////////////////////////////////////////////////////////////////////////////////// // OpenGL constants + // The values for constants not defined in the GLES20 interface can be found in this file: + // http://androidxref.com/source/raw/development/tools/glesv2debugger/src/com/android/glesv2debugger/GLEnum.java + public static final int GL_FALSE = GLES20.GL_FALSE; public static final int GL_TRUE = GLES20.GL_TRUE; @@ -140,14 +140,14 @@ public class PGL { public static final int GL_REPEAT = GLES20.GL_REPEAT; public static final int GL_RGBA8 = -1; - public static final int GL_DEPTH24_STENCIL8 = -1; + public static final int GL_DEPTH24_STENCIL8 = 0x88F0; public static final int GL_DEPTH_COMPONENT16 = GLES20.GL_DEPTH_COMPONENT16; - public static final int GL_DEPTH_COMPONENT24 = -1; - public static final int GL_DEPTH_COMPONENT32 = -1; + public static final int GL_DEPTH_COMPONENT24 = 0x81A6; + public static final int GL_DEPTH_COMPONENT32 = 0x81A7; - public static final int GL_STENCIL_INDEX1 = -1; - public static final int GL_STENCIL_INDEX4 = -1; + public static final int GL_STENCIL_INDEX1 = 0x8D46; + public static final int GL_STENCIL_INDEX4 = 0x8D47; public static final int GL_STENCIL_INDEX8 = GLES20.GL_STENCIL_INDEX8; public static final int GL_ARRAY_BUFFER = GLES20.GL_ARRAY_BUFFER; @@ -159,7 +159,7 @@ public class PGL { public static final int GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = GLES20.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; public static final int GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GLES20.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; public static final int GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS = GLES20.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; - public static final int GL_FRAMEBUFFER_INCOMPLETE_FORMATS = -1; + public static final int GL_FRAMEBUFFER_INCOMPLETE_FORMATS = 0x8CDA; public static final int GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = -1; public static final int GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER = -1; public static final int GL_FRAMEBUFFER_UNSUPPORTED = GLES20.GL_FRAMEBUFFER_UNSUPPORTED; @@ -184,8 +184,10 @@ public class PGL { public static final int GL_MAX_TEXTURE_SIZE = GLES20.GL_MAX_TEXTURE_SIZE; public static final int GL_MAX_SAMPLES = -1; public static final int GL_ALIASED_LINE_WIDTH_RANGE = GLES20.GL_ALIASED_LINE_WIDTH_RANGE; - public static final int GL_ALIASED_POINT_SIZE_RANGE = GLES20.GL_ALIASED_POINT_SIZE_RANGE; - + public static final int GL_ALIASED_POINT_SIZE_RANGE = GLES20.GL_ALIASED_POINT_SIZE_RANGE; + public static final int GL_DEPTH_BITS = GLES20.GL_DEPTH_BITS; + public static final int GL_STENCIL_BITS = GLES20.GL_STENCIL_BITS; + public static final int GLU_TESS_WINDING_NONZERO = PGLU.GLU_TESS_WINDING_NONZERO; public static final int GLU_TESS_WINDING_ODD = PGLU.GLU_TESS_WINDING_ODD; @@ -228,9 +230,9 @@ public class PGL { public static final int GL_LINK_STATUS = GLES20.GL_LINK_STATUS; public static final int GL_VALIDATE_STATUS = GLES20.GL_VALIDATE_STATUS; - public static final int GL_MULTISAMPLE = -1; - public static final int GL_POINT_SMOOTH = -1; - public static final int GL_LINE_SMOOTH = -1; + public static final int GL_MULTISAMPLE = 0x809D; + public static final int GL_POINT_SMOOTH = 0x0B10; + public static final int GL_LINE_SMOOTH = 0x0B20; public static final int GL_POLYGON_SMOOTH = -1; // Some EGL constants needed to initialize an GLES2 context. @@ -333,7 +335,7 @@ public class PGL { public void updatePrimary() { if (!initialized) { - String ext = GLES20.glGetString(GLES20.GL_EXTENSIONS); + String ext = GLES20.glGetString(GLES20.GL_EXTENSIONS); if (-1 < ext.indexOf("texture_non_power_of_two")) { texWidth = pg.width; texHeight = pg.height; @@ -358,24 +360,35 @@ public class PGL { GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); GLES20.glGenFramebuffers(1, fbo, 0); - GLES20.glGenRenderbuffers(1, depth, 0); - GLES20.glGenRenderbuffers(1, stencil, 0); + GLES20.glGenRenderbuffers(1, depth, 0); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fbo[0]); - + GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depth[0]); GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, texWidth, texHeight); GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, depth[0]); - GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, stencil[0]); - GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_STENCIL_INDEX8, texWidth, texHeight); - GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_STENCIL_ATTACHMENT, GLES20.GL_RENDERBUFFER, stencil[0]); + int[] temp = new int[1]; + GLES20.glGetIntegerv(GLES20.GL_STENCIL_BITS, temp, 0); + int stencilBits = temp[0]; + if (stencilBits == 8) { + GLES20.glGenRenderbuffers(1, stencil, 0); + GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, stencil[0]); + GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_STENCIL_INDEX8, texWidth, texHeight); + GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_STENCIL_ATTACHMENT, GLES20.GL_RENDERBUFFER, stencil[0]); + } GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); backTex = 1; frontTex = 0; + // The screen framebuffer is the FBO just created. We need + // to update the screenFramebuffer object so when the + // framebuffer is popped back to the screen, the correct + // id is set. + PGraphicsAndroid3D.screenFramebuffer.glFboID = fbo[0]; + initialized = true; } } @@ -396,10 +409,9 @@ public class PGL { // Simplest scenario: clear mode means we clear both the color and depth buffers. // No need for saving front color buffer, etc. GLES20.glClearColor(0, 0, 0, 0); - GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); - } - - if (!clear) { + GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); + PGraphicsAndroid3D.screenFramebuffer.glFboID = 0; + } else { GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fbo[0]); GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textures[frontTex], 0); validateFramebuffer(); @@ -415,6 +427,7 @@ public class PGL { // Render previous draw texture as background. drawTexture(GLES20.GL_TEXTURE_2D, textures[backTex], texWidth, texHeight, 0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height); } + PGraphicsAndroid3D.screenFramebuffer.glFboID = fbo[0]; } if (firstOnscreenFrame) { @@ -448,9 +461,6 @@ public class PGL { public void endOffscreenDraw(boolean clear0) { - if (!clear0) { - GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fbo[0]); - } } @@ -1453,9 +1463,6 @@ public class PGL { alphaBits = a; depthBits = d; stencilBits = s; - - DEFAULT_DEPTH_BITS = d; - DEFAULT_STENCIL_BITS = s; } } } diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index 2812e1ffb..f90b1d8cb 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -91,13 +91,16 @@ public class PGraphicsAndroid3D extends PGraphics { static public boolean npotTexSupported; static public boolean mipmapGeneration; static public boolean fboMultisampleSupported; - static public boolean blendEqSupported; + static public boolean packedDepthStencilSupported; + static public boolean blendEqSupported; /** Some hardware limits */ static public int maxTextureSize; static public int maxSamples; static public float maxPointSize; static public float maxLineWidth; + static public int depthBits; + static public int stencilBits; /** OpenGL information strings */ static public String OPENGL_VENDOR; @@ -1363,6 +1366,14 @@ public class PGraphicsAndroid3D extends PGraphics { return; } + if (!glParamsRead) { + getGLParameters(); + } + + if (!settingsInited) { + defaultSettings(); + } + if (primarySurface) { pgl.updatePrimary(); } else { @@ -1388,14 +1399,6 @@ public class PGraphicsAndroid3D extends PGraphics { pgl.updateOffscreen(pg.pgl); } - if (!glParamsRead) { - getGLParameters(); - } - - if (!settingsInited) { - defaultSettings(); - } - // We are ready to go! report("top beginDraw()"); @@ -5602,8 +5605,8 @@ public class PGraphicsAndroid3D extends PGraphics { if (PGraphicsAndroid3D.fboMultisampleSupported && 1 < antialias) { offscreenFramebufferMultisample = new PFramebuffer(parent, texture.glWidth, texture.glHeight, antialias, 0, - PGL.DEFAULT_DEPTH_BITS, PGL.DEFAULT_STENCIL_BITS, - PGL.DEFAULT_DEPTH_BITS == 24 && PGL.DEFAULT_STENCIL_BITS == 8, false); + depthBits, stencilBits, + depthBits == 24 && stencilBits == 8 && packedDepthStencilSupported, false); offscreenFramebufferMultisample.clear(); offscreenMultisample = true; @@ -5617,8 +5620,8 @@ public class PGraphicsAndroid3D extends PGraphics { } else { antialias = 0; offscreenFramebuffer = new PFramebuffer(parent, texture.glWidth, texture.glHeight, 1, 1, - PGL.DEFAULT_DEPTH_BITS, PGL.DEFAULT_STENCIL_BITS, - PGL.DEFAULT_DEPTH_BITS == 24 && PGL.DEFAULT_STENCIL_BITS == 8, false); + depthBits, stencilBits, + depthBits == 24 && stencilBits == 8 && packedDepthStencilSupported, false); offscreenMultisample = false; } @@ -5633,10 +5636,11 @@ public class PGraphicsAndroid3D extends PGraphics { OPENGL_VERSION = pgl.glGetString(PGL.GL_VERSION); OPENGL_EXTENSIONS = pgl.glGetString(PGL.GL_EXTENSIONS); - npotTexSupported = -1 < OPENGL_EXTENSIONS.indexOf("texture_non_power_of_two"); - mipmapGeneration = -1 < OPENGL_EXTENSIONS.indexOf("generate_mipmap"); - fboMultisampleSupported = -1 < OPENGL_EXTENSIONS.indexOf("framebuffer_multisample"); - + npotTexSupported = -1 < OPENGL_EXTENSIONS.indexOf("texture_non_power_of_two"); + mipmapGeneration = -1 < OPENGL_EXTENSIONS.indexOf("generate_mipmap"); + fboMultisampleSupported = -1 < OPENGL_EXTENSIONS.indexOf("framebuffer_multisample"); + packedDepthStencilSupported = -1 < OPENGL_EXTENSIONS.indexOf("packed_depth_stencil"); + try { pgl.glBlendEquation(PGL.GL_FUNC_ADD); blendEqSupported = true; @@ -5658,6 +5662,12 @@ public class PGraphicsAndroid3D extends PGraphics { pgl.glGetIntegerv(PGL.GL_ALIASED_POINT_SIZE_RANGE, temp, 0); maxPointSize = temp[1]; + pgl.glGetIntegerv(PGL.GL_DEPTH_BITS, temp, 0); + depthBits = temp[0]; + + pgl.glGetIntegerv(PGL.GL_STENCIL_BITS, temp, 0); + stencilBits = temp[0]; + glParamsRead = true; }