From 96a1158e3e7a01676de1ba4c5d123dcb37c70bf3 Mon Sep 17 00:00:00 2001 From: Duy Tran Khanh Date: Sun, 3 Jan 2021 14:15:34 +0700 Subject: [PATCH] Stills try to get Minecraft run on separate thread --- .../main/java/net/kdt/pojavlaunch/Tools.java | 6 +- Natives/AppDelegate.h | 1 + Natives/AppDelegate.m | 13 ++- Natives/egl_bridge.c | 8 +- Natives/egl_bridge_ios.h | 2 + Natives/egl_bridge_ios.m | 87 +++++++++++++++++-- 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/JavaApp/src/main/java/net/kdt/pojavlaunch/Tools.java b/JavaApp/src/main/java/net/kdt/pojavlaunch/Tools.java index 0a25ed43..f43c70df 100644 --- a/JavaApp/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/JavaApp/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -83,19 +83,19 @@ public final class Tools } } - // new Thread(() -> { try { + new Thread(() -> { try { URLClassLoader loader = new URLClassLoader(urlList.toArray(new URL[0]), Tools.class.getClassLoader()); Class clazz = loader.loadClass(versionInfo.mainClass); Method method = clazz.getMethod("main", String[].class); method.invoke(null, new Object[]{launchArgs}); - /* + } catch (Throwable th) { throw new RuntimeException(th); } }).start(); - */ + // JREUtils.launchJavaVM(javaArgList); } diff --git a/Natives/AppDelegate.h b/Natives/AppDelegate.h index 4cb1df65..95d6308c 100644 --- a/Natives/AppDelegate.h +++ b/Natives/AppDelegate.h @@ -9,5 +9,6 @@ @property (strong, nonatomic) UIWindow *window; +GLKView* obtainGLKView(); @end diff --git a/Natives/AppDelegate.m b/Natives/AppDelegate.m index d55a4271..015e5e83 100644 --- a/Natives/AppDelegate.m +++ b/Natives/AppDelegate.m @@ -16,13 +16,18 @@ @implementation AppDelegate +GLKView* mGLKView; + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"Hello from app launch!"); // self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; + if (context == nil) { + context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + } GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; view.context = context; view.delegate = self; @@ -35,9 +40,15 @@ int width_c = (int) roundf([[UIScreen mainScreen] bounds].size.width); int height_c = (int) roundf([[UIScreen mainScreen] bounds].size.height); + + mGLKView = view; callback_AppDelegate_didFinishLaunching(width_c, height_c); return YES; } +GLKView* obtainGLKView() { + return mGLKView; +} + @end diff --git a/Natives/egl_bridge.c b/Natives/egl_bridge.c index acefb0e0..57b2ebf8 100644 --- a/Natives/egl_bridge.c +++ b/Natives/egl_bridge.c @@ -42,7 +42,7 @@ JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setenv(JNIEnv *en } JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_saveGLContext(JNIEnv* env, jclass clazz) { - CURR_GL_CONTEXT = getCurrentContext(); + CURR_GL_CONTEXT = initCurrentContext(); if (CURR_GL_CONTEXT == NULL) { debug("OpenGLES context is NULL..."); assert(CURR_GL_CONTEXT != NULL); @@ -63,7 +63,11 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_GLFW_nativeEglInit(JNIEnv* env, j JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_GLFW_nativeEglMakeCurrent(JNIEnv* env, jclass clazz, jlong window) { debug("ES2Bridge: making current\n"); - jboolean ret = 1; // makeCurrentContext(CURR_GL_CONTEXT); + jboolean ret = makeCurrentContextShared(CURR_GL_CONTEXT); + + if (!ret) { + return ret; + } void *libGL = dlopen("libGL.dylib", RTLD_GLOBAL); debug("libGL = %p", libGL); diff --git a/Natives/egl_bridge_ios.h b/Natives/egl_bridge_ios.h index 438d8b7b..4c25292c 100644 --- a/Natives/egl_bridge_ios.h +++ b/Natives/egl_bridge_ios.h @@ -4,6 +4,8 @@ void *createContext(); void *getCurrentContext(); +void *initCurrentContext(); jboolean makeCurrentContext(void *context); +jboolean makeCurrentContextShared(void *context); jboolean clearCurrentContext(); void flushBuffer(); diff --git a/Natives/egl_bridge_ios.m b/Natives/egl_bridge_ios.m index e1ece082..89d82407 100644 --- a/Natives/egl_bridge_ios.m +++ b/Natives/egl_bridge_ios.m @@ -26,6 +26,7 @@ */ #import +#import #import #import "egl_bridge_ios.h" @@ -42,7 +43,8 @@ #endif void *createContext() { - return (__bridge void *) [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + EAGLContext *ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + return ptr_to_jlong(ctx); } void *getCurrentContext() { @@ -50,18 +52,82 @@ void *getCurrentContext() { return ptr_to_jlong(ctx); } +typedef GLKView* Main_obtainGLKView(); + +GLuint RenderBuffer; +GLuint FrameBuffer; +GLuint DepthBuffer; +void *initCurrentContext() { + Main_obtainGLKView *obtainGLKView = (Main_obtainGLKView *) dlsym(RTLD_DEFAULT, "obtainGLKView"); + if (!obtainGLKView) { + NSLog(@"Unable to locate obtainGLKView"); + } + GLKView* view = obtainGLKView(); + + EAGLContext *ctx = [EAGLContext currentContext]; + + glGenFramebuffersOES(1, &FrameBuffer); + glGenRenderbuffersOES(1, &RenderBuffer); + glGenRenderbuffersOES(1, &DepthBuffer); + + glBindFramebufferOES(GL_FRAMEBUFFER_OES, FrameBuffer); + glBindRenderbufferOES(GL_RENDERBUFFER_OES, RenderBuffer); + + if (![MainContext renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)View.layer]) + { + NSLog(@"error calling MainContext renderbufferStorage"); + return; + } + + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, RenderBuffer); + + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &Width); + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &Height); + + glBindRenderbufferOES(GL_RENDERBUFFER_OES, DepthBuffer); + glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, Width, Height); + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, DepthBuffer); + + glFlush(); + + if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { + NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); + } + + glBindFramebufferOES(GL_FRAMEBUFFER_OES, FrameBuffer); + + return ptr_to_jlong(ctx); +} + +jboolean makeCurrentContextShared(void *context) { + EAGLSharegroup *group = ((__bridge EAGLContext *) context).sharegroup; + EAGLContext sharedContext = nil; + + if (!group) { + NSLog(@"Could not get sharegroup from the main context"); + } + + sharedContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3 sharegroup:group]; + if (!sharedContext) { + sharedContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:group]; + if (!sharedContext) { + NSLog(@"Could not create sharedContext"); + return JNI_FALSE; + } + } + + if (!makeCurrentContext((__bridge void *) sharedContext)){ + NSLog(@"Could not make current sharedContext"); + return JNI_FALSE; + } +} + jboolean makeCurrentContext(void *context) { - // fprintf(stderr, "IOSWindowSystemInterface : makeCurrentContext %x\n", context); if ([EAGLContext setCurrentContext:(__bridge EAGLContext *)jlong_to_ptr(context)] == YES) { -/* - glClearColor(0.4f, 0.4f, 0.4f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - flushBuffer(); -*/ return JNI_TRUE; } - return JNI_FALSE; + return JNI_TRUE; } jboolean clearCurrentContext() { @@ -73,5 +139,10 @@ jboolean clearCurrentContext() { } void flushBuffer() { + glBindRenderbufferOES(GL_RENDERBUFFER_OES, RenderBuffer); + [[EAGLContext currentContext] presentRenderbuffer:GL_RENDERBUFFER]; + + // prepare new buffer + glBindFramebufferOES(GL_FRAMEBUFFER_OES, FrameBuffer); }