Kotlin Multiplatform Mobile Compose GL-based View Support

I am trying to create an OpenGL library for KMM Compose. This wraps GLSurfaceView on Android and GLKView/GLKViewController on iOS. I have large parts of this working including the entire OpenGL API itself. But that was when I was using a Swift layout instead of Compose layout on iOS. I am now making the iOS side use Compose and things are crashing… (the Android-Compose version works just fine).

I am using nearly the same components as I did before, but the GLKViewController is now wrapped in UIKitViewController so it is Composable. After 200-300 frames the program crashes.

When debugging with Xcode, the exception is a BAD_ACCESS exception. When I have the frame counter variable, the BAD_ACCESS exception happens on the Kotlin variable used to count the frames. When that variable is not used, it happens in main. When turning on Zombie Objects, the error does change, indicating to me that there is some object being cleaned up but that is actually still in use.

A minimal testing app is available at
github.com/coderforlife/kmm-gl-test. It is based on the KMM Compose template and should draw a green rectangle that is 50% of the screen size. This does not include the cross-platform GL library, but exhibits the bug with the GLKView/GLKViewController.

Note: the purpose of this 3D graphics is NOT full-screen and the entire app is not using OpenGL thus why this is also supporting Compose. If it was full-screen and always OpenGL then it would just jump straight into the single component and never need Compose.

1 Like

Im interested in this project. Is this going to be released as open source?

Yes! I do plan on releasing it as a library. The core library is kind of modeled after WebGL (although it has a different way of doing the render loop) and a little bit of ThreeJS (although nothing high-level) and does not require Compose. That part already works on iOS and Android. Some parts are still a bit rough (arrays which Javascript handles in a nice way actually, but I am trying to think of how to make work well in Kotlin without union types along with some async/sync stuff since Kotlin Native is changing things with Workers) but most of it is fairly good.

I am working on the high level part that requires Compose and I am slowly adding parts of the ThreeJS API to it. This part is still very rough but I have a working demo on Android. Just wish I could have it running on iOS without crashing…