Hi, I'm having trouble understanding the threading...
# korge
m
Hi, I'm having trouble understanding the threading model in Korge. Currently it seems to me that Korge is single threaded, I may be wrong. I was wondering if there is any doc which explains it or can someone help me out
a
Korge relies on coroutines. Unless you a running korge in a single core, multi threading should be supported. But I think the whole screen paint must run on the main thread
👍 1
d
Korge core is single threaded right now. There is an event loop in the main thread that executes scheduled stuff. You can still offload stuff in threads/workers or with other coroutine contexts, but rendering and view handling should happen in the main thread since the library is not thread safe, and AG (that wraps OpenGL at this point) doesn’t support context sharing right now. We don't have plans to make it multithreading in the short or mid terms. Despite of that, korge already offloads some stuff to other threads. For example, image decoding happens in a worker so you can load images asynchronously without blocking the game (you can for example put a spinner while the game is loading resources)
👍 2
m
Understood. Can I run Korge on any other single thread apart from main thread? Also is it possible to have more than 1 KorgeView simultaneously?
d
on which platform? If it is AWT, it is possible to have several views, as there is a GLCanvas component. But probably will run on the AWT thread
m
I am targeting mobile for now
iOS and Android
d
@Pablo Caviglia managed to integrate KorGE on existing iOS/Android apps. I think it was only one view at a time, but I suppose it should be possible to have several of them at once. KorGE doesn’t support this yet directly, but there are workarounds. We have plans to support this use-case directly without workarounds and provide a repo with samples for each platform (iOS, Android, JS and JVM) in a not too dilated timeframe. But there is no ETA
p
Yes, what @Deactivated User says. I have only tried with the one-view-at-the-same-time approach… I dont have idea if it can work fine with several views because I dont know that much the internals of Korge. Probably it’s not that easy because I believe korge was developed with another way of using it in mind. Should be a matter of trying, I can give it a try later.
d
As far as I’m concerned, there are no global/static variables and everything of the state is local or in the coroutine context, it could work as long as there is a separate coroutine context. But I’m not fully sure about the event loop. I don’t remember if there is a single loop on iOS/Android. But if it doesn’t work we could support that with a reasonable amount of work.
p
makes sense
let me give it a try and get back with something
👍 1
🙏 2
m
On iOS, MyIosGameWindow2 is singleton. Correct me if I'm wrong but will a new instance be required for each view?
d
Yeah, as stated, right now KorGE is not directly prepared for that, what I meant is that there might be no major issues for supporting this with a few adjustments. Pablo used KorGE in different iOS/controllers views, but not several games at the same time
👍 1
Can you provide info about why would you want to have several game views at once so we can understand the use-case properly?
m
My use case involves showing one korge view and using another for offscreen rendering to let's say a gif/video. Using a different thread for exporting makes sense because we can run the event loop at different rate (which will be much faster than realtime)
d
Rendering OpenGL/view stuff or other things?
m
Rendering OpenGL/view only. Like a gif of last few seconds of a game level.
d
I see. But it would be the same content of the main game? or a different content?
m
It would be slightly different. I'm think of saving the game state and replaying them with modifications
d
I see. Because if it is the same content, you can just generate an image of each frame, and offload the encoding part to a separate thread
m
I though of saving the bitmaps as we go but I need to add some views below toplevel view in scene
d
Is that a watermark? you could add that overlay with the image of the frame itself
m
I want to have something like this. Notice the blue parallelogram is present in exported gif but it is below the green square.
d
I see. Maybe you can make the blue stuff visible, with visible=true, render the stage to a bitmap, then visible=false
I would try that before and check the performance. Still, feel free to open a feature request at github. I think being able to support several views at once is something we should be able to support within a reasonable amount of time
m
That could just work. Thank you very much!
👍 1
I will try it out and would also log a feature request considering it might be useful to be used in apps apart from games like https://twitter.com/romainguy/status/1270764527773048832
👌 1
I tried showing 2 views at the same time in iOS and so far so good. Its working fine for me 😃. I just had to create a new instance of
IosGameWindow
🦜 1
👏 1
d
So in the end it worked out of the box? Good to know 🙂
p
that’s efficacy @Mayank!
🙏 1
m
@Deactivated User Yep, out of the box! Great work on the library 👌
🎉 2
I would like to test this on android too. Is there any ETA on which https://github.com/korlibs/korgw/pull/30 will be merged?
d
That code is broken right now
m
I see. As per my experience so far multiple views should work seamlessly in Android too. You have been very helpful. Thanks!
👍 1
I tried running Korge in another thread, here are my observations: 1. 1 Korge instance runs on any 1 thread, doesn't matter if its main thread or not 2. multiple Korge instances can run on same thread (main or bg) 3. multiple Korge instance cannot run on different threads Regarding 3rd point, the reason was Immutability Exception in Kotlin Native. First it was
configureLoggerFromProperties
in Korge invoke. I cloned Korge and commented that. Then exception started coming from another place. This time
resourcesVfs()
in Views.kt. I cloned korio and added @ThreadLocal which solved the problem. After that exception was coming from other places where logger was used. As it required to clone logger repo and I was busy with some other work and I didn't know how many more modifications will be required so I stopped. @Deactivated User Can we achieve this easily or would it require many changes? Thread safe structures are not required just that each thread having their own instances should do the trick
d
@Mayank if you want to propose changes that affect several repositories, you can try the korge-next repository that is the one with all the repos integrated and you can run samples directly with
./gradlew :samples:hello-world:runJvm
then make a PR with changes covering the repos. I synchronize each repo individually from time to time.
m
That's great! I would love to contribute. Is there any contribution guide?
d
https://korlibs.soywiz.com/contributing/ Not sure if that's fully up-to-date. But the simplest way, especially in your case is to fork this repo: https://github.com/korlibs/korge-next That repo includes every single library and required dependency for everything and even some samples. Then make relevant changes and try stuff with the command I put. Then make a PR. If you have any doubts about the code-base, feel free to ask here
m
Cool. I'll try to contribute something this weekend Thanks! 🙂
👍 1