Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGL ES 2.0 - Interaction between the game loop and the rendering thread  (Read 23910 times)
0 Members and 1 Guest are viewing this topic.
Offline Deku

Junior Newbie


Exp: 4 years



« Posted 2017-01-12 18:15:59 »

Hi all,
I've been a lurker of the site for a while now so I thought I'd set up an account and see if I can get some community input on game engine design. I've been working on basic engines on-and-off for a while now with some LWJGL-based Java engines running off of Box2D and others off of a custom 2D collision and physics engine and I'm in the process of porting over a lot of my previous work to an Android system. I've been at this for a couple of weeks now and I have a rudimentary engine running, rendering with OpenGL ES 2.0. Input, updates, rendering, glitchy physics, the works.

But I'm pretty stumped on what is the best architecture for a game engine on Android. I have my game loop and rendering occurring in separate threads currently and I'm fighting against concurrent modification exceptions every now and again. I've heard of three methods around threaded engine issues so far:
1. Synchronize all object calls that might cause concurrent access issues between the two threads. I tried this, it was messy and lead to framerate stutter as both threads were essentially waiting for each other constantly (kind of against the point of threads)
2. Run everything from the render loop. Create new threads for things such as I/O or whatever, but all game logic could just run on one thread. This doesn't seem very attractive as a solution though?
3. Create some sort of "intermediate buffer" which stores all renderable object positions. Meaning that thread access to this would be more optimized, rather than synchronizing a full update loop for example. Also means that the render thread won't run against values which are mid-update and lead occasional artifacts such as tearing between sprites.

If anyone has heard any opinion on the best way to layout an Android game engine, please let me know. I'm finding myself pretty stumped on this so far. Currently going to look into option 3 over the week until I have something better to work off of.
Offline SHC
« Reply #1 - Posted 2017-01-12 23:21:59 »

From my experience, Android is a lot different from traditional desktop systems. Unlike the desktop, you have two threads, the android UI thread and the GLThread created for you by the GLSurfaceView. I see that you are trying to separate rendering and logic, but I'd say it's not worth the effort on Android.

I agree that parallelization gives a performance boost, but only if done correctly. And most of the time, there will be other reasons of you experience the performance issues. I'd not go for it unless there are no other areas of optimization left.

Coming to the question, I guess you want to run some tasks on the UI thread, like operations that switch the device orientation etc., and some other you want to handle in the GLThread, like in the case of inputs.

1  
surfaceView.queueEvent(() -> postTouchEvent(finger, false, 0, 0));

That will make your code run on the GLThread. Now for operations like changing the device orientation, you can use the activity methods like this.

1  
2  
activity.runOnUiThread(() ->
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE));

This is very similar to swing's SwingUtilities.invokeLater method. Now let us get to the game loop. In the renderer of the GLSurfaceView, there is no separation of logic and render. When you set the render mode to continuous, it repeatedly calls the onDrawFrame method, so keep both the update and render calls directly in it.

For reference, you can checkout the code in my android backend: https://github.com/sriharshachilakapati/SilenceEngine/tree/master/backend-android/src/main/java/com/shc/silenceengine/backend/android

Offline Deku

Junior Newbie


Exp: 4 years



« Reply #2 - Posted 2017-01-13 03:24:10 »

Thanks for the reply. I haven't had the chance to properly check out your Android backend since I've just checked back here but it looks to be an invaluable resource. In the meantime while I was away for the last few hours I went ahead and developed a buffer list for rendering objects to work with my current architecture. So I'm still doing all game logic on a separate thread. I'm going to have to let it stew for some time and make sure it gets thoroughly tested out but so far its looking to be a great solution. The only thing I'm worried about is possible performance issues but I'm not seeing them so far.
In the future I would definitely take your advice to avoid going down this route, it certainly is not worth the effort so far. As a warning to anyone who finds themselves in similar issues, you're better off running the game logic in the render thread. The introduction of additional threads has proved as nothing more than a headache, even if I seem to have everything in a working order now.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EgonOlsen
« Reply #3 - Posted 2017-01-13 14:11:40 »

I'm doing all my game logic in the rendering thread. The actual rendering happens in parallel on the GPU anyway (at least to a degree), so there's not much to gain (if anything) by doing the logic in another thread.
For most games, the logic isn't that time consuming anyway. I once tried to separate them into two threads and the performance benefit was null, nada, niente...none at all. My current game (https://play.google.com/store/apps/details?id=com.threed.jpct.games.rpg) supports the option to do some heavier calculations in a different thread, but it's disabled in production code, because it just isn't worth it.

Offline Deku

Junior Newbie


Exp: 4 years



« Reply #4 - Posted 2017-01-13 17:02:45 »

Did some tune up on the architecture today and still not seeing any issues (related to the threading at least). The code seems to be pretty sound. But as the others have pointed out, and I'm seeing the same, there's no sense of optimization from this format.
So far, was not worth the effort it took to figure out and implement and it makes the code far less intuitive to read since I had to push so many values related to rendering out of the game objects they belong in and into corresponding buffer objects.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (306 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (302 views)
2017-08-02 12:18:09

Sralse (293 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1108 views)
2017-04-05 03:38:00

CopyableCougar4 (1685 views)
2017-03-24 15:39:42

theagentd (1434 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!