浏览代码

iOS gl3 and some improvements (#1473)

* Update GLImageFormats.java

* Modified JmeBatchRenderBackend to clear the inner buffer of the image in the atlases instead of setting a predefined byte buffer
on disposal that made all atlases in the backend use the same buffer and generated rendering issues.

* First impl of testcasefor multiple atlases issue. Still missing to add more images to the screens so it really uses more atlases

* Manual merge pending stuff from jme3 base

* Manual merge

* Implemented new methods in IosGL wrapper for GLES30
Created native methods in JmeIosGLES

* Fixed some imports and typos

* Added comment

* Fixed compilation if using java >= 1.9. The bytecode was generated with v52 but when executing it failed to find methods of nio.*Buffer because of an implementation change after java 1.9

* Added glTexParameterf and fixed black screen rendering on iOS when using any additional framebuffer (scene processors and filters)

* Added constant GL_FRAMEBUFFER_BINDING

* More gl functions implemented

* Removed useless comments

* Fixed release option to match current gradle version defined in gradlew

* Fixed formatting to match jME standards

Co-authored-by: joliver82 <[email protected]>
joliver82 4 年之前
父节点
当前提交
eb59aed44f

+ 7 - 0
common.gradle

@@ -12,6 +12,13 @@ version = jmeFullVersion
 sourceCompatibility = '1.8'
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
 
+if(JavaVersion.current() >= JavaVersion.VERSION_1_9) {
+    compileJava {
+	options.compilerArgs.addAll(['--release', '8'])
+        //Replace previous with "options.release = 8" if updated to gradle 6.6 or newer
+    }
+}
+
 gradle.projectsEvaluated {
     tasks.withType(JavaCompile) { // compile-time options:
         options.compilerArgs << '-Xlint:unchecked'

+ 2 - 1
jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java

@@ -195,6 +195,7 @@ public interface GL {
     public static final int GL_VERTEX_SHADER = 0x8B31;
     public static final int GL_ZERO = 0x0;
     public static final int GL_UNPACK_ROW_LENGTH = 0x0CF2;
+    public static final int GL_FRAMEBUFFER_BINDING = 0x8CA6;
 
     public void resetStats();
 
@@ -1300,4 +1301,4 @@ public interface GL {
      * @param height the viewport height.
      */
     public void glViewport(int x, int y, int width, int height);
-}
+}

+ 14 - 3
jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java

@@ -91,6 +91,7 @@ public final class GLRenderer implements Renderer {
     private final EnumMap<Limits, Integer> limits = new EnumMap<Limits, Integer>(Limits.class);
 
     private FrameBuffer mainFbOverride = null;
+    private int defaultFBO = 0;
     private final Statistics statistics = new Statistics();
     private int vpX, vpY, vpW, vpH;
     private int clipX, clipY, clipW, clipH;
@@ -630,6 +631,16 @@ public final class GLRenderer implements Renderer {
                 gl2.glEnable(GL2.GL_POINT_SPRITE);
             }
         }
+
+	IntBuffer tmp = BufferUtils.createIntBuffer(16);
+	gl.glGetInteger(GL.GL_FRAMEBUFFER_BINDING, tmp);
+	tmp.rewind();
+	int fbOnLoad = tmp.get();
+	if(fbOnLoad > 0)
+	{
+            // Overriding default FB to fbOnLoad. Mostly iOS fix for scene processors and filters
+	    defaultFBO = fbOnLoad;
+	}
     }
 
     @Override
@@ -1856,10 +1867,10 @@ public final class GLRenderer implements Renderer {
     
     private void bindFrameBuffer(FrameBuffer fb) {
         if (fb == null) {
-            if (context.boundFBO != 0) {
-                glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, 0);
+            if (context.boundFBO != defaultFBO) {
+                glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, defaultFBO);
                 statistics.onFrameBufferUse(null, true);
-                context.boundFBO = 0;
+                context.boundFBO = defaultFBO;
                 context.boundFB = null;
             }
         } else {

+ 3 - 0
jme3-ios/build.gradle

@@ -2,6 +2,9 @@ if (!hasProperty('mainClass')) {
     ext.mainClass = ''
 }
 
+sourceCompatibility = JavaVersion.VERSION_1_8
+targetCompatibility = JavaVersion.VERSION_1_8
+
 dependencies {
     compile project(':jme3-core')
     compile project(':jme3-plugins')

+ 80 - 26
jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java

@@ -33,8 +33,11 @@ package com.jme3.renderer.ios;
 
 import com.jme3.renderer.RendererException;
 import com.jme3.renderer.opengl.GL;
+import com.jme3.renderer.opengl.GL2;
+import com.jme3.renderer.opengl.GLES_30;
 import com.jme3.renderer.opengl.GLExt;
 import com.jme3.renderer.opengl.GLFbo;
+import com.jme3.util.BufferUtils;
 import java.nio.Buffer;
 import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
@@ -43,13 +46,14 @@ import java.nio.IntBuffer;
 import java.nio.ShortBuffer;
 
 /**
- * Implements OpenGL ES 2.0 for iOS. 
+ * Implements OpenGL ES 2.0 and 3.0 for iOS. 
  * 
- * @author Kirill Vainer
+ * @author Kirill Vainer, Jesus Oliver
  */
-public class IosGL implements GL, GLExt, GLFbo {
+public class IosGL implements GL, GL2, GLES_30, GLExt, GLFbo {
     
     private final int[] temp_array = new int[16];
+    private final IntBuffer tmpBuff = BufferUtils.createIntBuffer(1);
     
     @Override
     public void resetStats() {
@@ -129,7 +133,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glBeginQuery(int target, int query) {
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glBeginQuery(target, query);
     }
 
     @Override
@@ -314,7 +318,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glEndQuery(int target) {
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glEndQuery(target);
     }
 
     @Override
@@ -333,7 +337,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glGenQueries(int num, IntBuffer buff) {
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glGenQueries(num, buff);
     }
 
     @Override
@@ -343,9 +347,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glGetBoolean(int pname, ByteBuffer params) {
-        // TODO: fix me!!!
-        // JmeIosGLES.glGetBoolean(pname, params);
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glGetBoolean(pname, params);
     }
 
     @Override
@@ -374,12 +376,14 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public long glGetQueryObjectui64(int query, int pname) {
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glGetQueryObjectuiv(query, pname, temp_array);
+        return temp_array[0];
     }
 
     @Override
     public int glGetQueryObjectiv(int query, int pname) {
-        throw new UnsupportedOperationException("Today is not a good day for this");
+        JmeIosGLES.glGetQueryiv(query, pname, temp_array);
+        return temp_array[0];
     }
 
     @Override
@@ -406,11 +410,11 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public boolean glIsEnabled(int cap) {
-        // TODO: fix me!!!
+        // kept this always returning true for compatibility
         if (cap == GLExt.GL_MULTISAMPLE_ARB) {
             return true;
         } else {
-            throw new UnsupportedOperationException();
+            return JmeIosGLES.glIsEnabled(cap);
         }
     }
 
@@ -454,25 +458,22 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glStencilFuncSeparate(int face, int func, int ref, int mask) {
-        // TODO: fix me!!!
-        // JmeIosGLES.glStencilFuncSeparate(face, func, ref, mask);
+        JmeIosGLES.glStencilFuncSeparate(face, func, ref, mask);
     }
 
     @Override
     public void glStencilOpSeparate(int face, int sfail, int dpfail, int dppass) {
-        // TODO: fix me!!!
-        // JmeIosGLES.glStencilOpSeparate(face, sfail, dpfail, dppass);
+        JmeIosGLES.glStencilOpSeparate(face, sfail, dpfail, dppass);
     }
 
     @Override
     public void glTexImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, ByteBuffer data) {
-        JmeIosGLES.glTexImage2D(target, level, format, width, height, 0, format, type, data);
+        JmeIosGLES.glTexImage2D(target, level, internalFormat, width, height, 0, format, type, data);
     }
 
     @Override
     public void glTexParameterf(int target, int pname, float param) {
-        // TODO: fix me!!!
-        // JmeIosGLES.glTexParameterf(target, pname, param);
+        JmeIosGLES.glTexParameterf(target, pname, param);
     }
 
     @Override
@@ -583,7 +584,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glBlitFramebufferEXT(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) {
-        throw new UnsupportedOperationException("FBO blit not available on iOS");
+        JmeIosGLES.glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
     }
 
     @Override
@@ -598,17 +599,17 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glDrawArraysInstancedARB(int mode, int first, int count, int primcount) {
-        throw new UnsupportedOperationException("Instancing not available on iOS");
+        JmeIosGLES.glDrawArraysInstanced(mode, first, count, primcount);
     }
 
     @Override
     public void glDrawBuffers(IntBuffer bufs) {
-        throw new UnsupportedOperationException("MRT not available on iOS");
+        JmeIosGLES.glDrawBuffers(getLimitBytes(bufs), bufs);
     }
 
     @Override
     public void glDrawElementsInstancedARB(int mode, int indices_count, int type, long indices_buffer_offset, int primcount) {
-        throw new UnsupportedOperationException("Instancing not available on iOS");
+        JmeIosGLES.glDrawElementsInstanced(mode, indices_count, type, indices_buffer_offset, primcount);
     }
 
     @Override
@@ -628,7 +629,7 @@ public class IosGL implements GL, GLExt, GLFbo {
 
     @Override
     public void glVertexAttribDivisorARB(int index, int divisor) {
-        throw new UnsupportedOperationException("Instancing not available on iOS");
+        JmeIosGLES.glVertexAttribDivisor(index, divisor);
     }
 
     @Override
@@ -717,6 +718,59 @@ public class IosGL implements GL, GLExt, GLFbo {
     
     @Override
     public void glFramebufferTextureLayerEXT(int target, int attachment, int texture, int level, int layer) {
-        throw new UnsupportedOperationException("OpenGL ES 2 does not support texture arrays");
+        JmeIosGLES.glFramebufferTextureLayer(target, attachment, texture, level, layer);
+    }
+
+    // New methods from GL2 interface which are supported in GLES30
+    @Override
+    public void glAlphaFunc(int func, float ref) {
+    }
+    
+    @Override
+    public void glPointSize(float size) {
+    }
+
+    @Override
+    public void glPolygonMode(int face, int mode) {
+    }
+
+    // Wrapper to DrawBuffers as there's no DrawBuffer method in GLES
+    @Override
+    public void glDrawBuffer(int mode) {
+        ((Buffer)tmpBuff).clear();
+        tmpBuff.put(0, mode);
+        tmpBuff.rewind();
+        glDrawBuffers(tmpBuff);
     }
+
+    @Override
+    public void glReadBuffer(int mode) {
+        JmeIosGLES.glReadBuffer(mode);
+    }
+
+    @Override
+    public void glCompressedTexImage3D(int target, int level, int internalFormat, int width, int height, int depth,
+                                           int border, ByteBuffer data) {
+        JmeIosGLES.glCompressedTexImage3D(target, level, internalFormat, width, height, depth, border, getLimitBytes(data), data);
+    }
+
+    @Override
+    public void glCompressedTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width,
+                                              int height, int depth, int format, ByteBuffer data) {
+        JmeIosGLES.glCompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, getLimitBytes(data), data);
+    }
+
+    @Override
+    public void glTexImage3D(int target, int level, int internalFormat, int width, int height, int depth, int border,
+                                 int format, int type, ByteBuffer data) {
+        JmeIosGLES.glTexImage3D(target, level, internalFormat, width, height, depth, border, format, type, data);
+    }
+
+    @Override
+    public void glTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width, int height,
+                                    int depth, int format, int type, ByteBuffer data) {
+        JmeIosGLES.glTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, data);
+    }
+
+
 }

+ 33 - 3
jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java

@@ -3,14 +3,15 @@ package com.jme3.renderer.ios;
 import com.jme3.renderer.RendererException;
 
 import java.nio.Buffer;
+import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 import java.util.logging.Logger;
 
 /**
- * The <code>iOS GLES interface</code> iOS alternative to Android's GLES20 class
+ * The <code>iOS GLES interface</code> iOS alternative to Android's GLES20 and GLES30 classes
  * 
- * @author Kostyantyn Hushchyn
+ * @author Kostyantyn Hushchyn, Jesus Oliver
  */
 public class JmeIosGLES {
     private static final Logger logger = Logger.getLogger(JmeIosGLES.class.getName());
@@ -184,6 +185,7 @@ public class JmeIosGLES {
 	public static native void glGenTextures(int n, int[] textures, int offset);
 	public static native void glGenerateMipmap(int target);
 	public static native int glGetAttribLocation(int program, String name);
+	public static native void glGetBoolean(int pname, ByteBuffer params);
 	public static native int glGetError();
 	public static native void glGetFramebufferAttachmentParameteriv(int target, int attachment, int pname, int[] params, int offset);
 	public static native void glGetIntegerv (int pname, int[] params, int offset);
@@ -193,6 +195,7 @@ public class JmeIosGLES {
 	public static native void glGetShaderiv(int shader, int pname, int[] params, int offset);
 	public static native String glGetString(int name);
 	public static native int glGetUniformLocation(int program, String name);
+	public static native boolean glIsEnabled(int cap);
 	public static native boolean glIsFramebuffer(int framebuffer);
 	public static native boolean glIsRenderbuffer(int renderbuffer);
 	public static native void glLineWidth(float width);
@@ -204,8 +207,11 @@ public class JmeIosGLES {
 	public static native void glRenderbufferStorage(int target, int internalformat, int width, int height);
 	public static native void glScissor(int x, int y, int width, int height);
 	public static native void glShaderSource(int shader, String string);
+	public static native void glStencilFuncSeparate(int face, int func, int ref, int mask);
+	public static native void glStencilOpSeparate(int face, int sfail, int dpfail, int dppass);
 	public static native void glTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, Buffer pixels);
 	public static native void glTexParameteri(int target, int pname, int param);
+	public static native void glTexParameterf(int target, int pname, float param);
 	public static native void glTexSubImage2D(int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, Buffer pixels);
 	public static native void glUniform1f(int location, float x);
 	public static native void glUniform1fv(int location, int count, FloatBuffer v);
@@ -232,7 +238,31 @@ public class JmeIosGLES {
 	public static native void glVertexAttribPointer2(int indx, int size, int type, boolean normalized, int stride, int offset);
 	public static native void glViewport(int x, int y, int width, int height);
 	
+	// New methods for GLES3
+	public static native void glBeginQuery(int target, int query);
+	public static native void glEndQuery(int target);
+	public static native void glGenQueries(int num, IntBuffer buff);
+	public static native void glGetQueryObjectuiv(int query, int pname, int[] params);
+	public static native void glGetQueryiv(int query, int pname, int[] params);
+	public static native void glBlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter);
+	public static native void glDrawArraysInstanced(int mode, int first, int count, int primcount);
+	public static native void glDrawBuffers(int size, IntBuffer data); //TODO: use buffer or intbuffer?
+	public static native void glDrawElementsInstanced(int mode, int indices_count, int type, long indices_buffer_offset, int primcount);
+	public static native void glVertexAttribDivisor(int index, int divisor);
+	public static native void glFramebufferTextureLayer(int target, int attachment, int texture, int level, int layer);
+
+	// New methods from GL2 interface which are supported in GLES30
+	public static native void glReadBuffer(int mode);
+	public static native void glCompressedTexImage3D(int target, int level, int internalFormat, int width, int height, int depth,
+                                           int border, int size, ByteBuffer data);
+	public static native void glCompressedTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width,
+										   int height, int depth, int format, int size, ByteBuffer data);
+	public static native void glTexImage3D(int target, int level, int internalFormat, int width, int height, int depth, int border,
+										   int format, int type, ByteBuffer data);
+	public static native void glTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width, int height,
+                                    int depth, int format, int type, ByteBuffer data);
 	
+									
     public static void checkGLError() {
         if (!ENABLE_ERROR_CHECKING) {
             return;
@@ -271,4 +301,4 @@ public class JmeIosGLES {
     }
     */
 
-}
+}