• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By davemacdo
      I'm a formally trained composer (doctorate from Michigan State) who writes what most people would call avant-garde concert music. I love weird abstract projects, and I would like to work with somebody making a weird, abstract, artsy game. 
      You can find more about me and my music on my site. I have worked with acoustic and electronic sounds, including some procedurally generated and interactive computer music. 
      In particular, I would like to work on a project that lets me use Fmod to prepare an adaptive score for a game built on Unity or Unreal. I've been a music professor and would like to get experience working in this medium so that I can be a better mentor for my students. Send me a DM or email <davidjohnmacdonald@gmail.com> if you would like to discuss working on a project together. 
    • By Tuner_z

      Name: One Level: Stickman Jailbreak
      Price: Free
      Developer: RTU Studio
      Platform: Android
      Language: C# (Unity3D)
      Google Play: https://play.google.com/store/apps/details?id=com.RTU.OneLevel
       
      Hello!
      I want to show you my game! "One level: Stickman Jailbreak" is a puzzle game with unusual gameplay where you must help the character to escape from prison. You just need to take the key and get out alive. The game has only one level, and there are many ways to complete it. Not everything is as simple as it might seem at first glance, so there are clues in the game.
       
      Short description:
      Nobody escapes from here!
       
      Description:
      Tommy got into trouble again! Our hero is behind bars. But he's not going to stay in jail for a long time and he decides to escape. Tommy steals a key and gets out of the jail cell. But our friend doesn't go free: Tommy suddenly finds himself in the same room from which he just escaped! The conditions for escaping change every time. In order to go free Tommy will have to solve logical puzzles and you can help him in this!
      At first it will be easy, but the tension will increase, and the tasks will become more complicated with each level. You should use your brain for all 100%, but if your skill is not enough, you can use a hint or ask for help from friends!
      You can solve the puzzles alone or with your friends and spend time well!
       
      Features: 
      Features:
      - 48 unique levels;
      - the game is translated into 10 languages: English, French, German, Spanish, Italian, Portuguese, Russian, Japanese, Chinese, Korean;
      - the function of "help from friend";
      - hints;
      - instructions.
       
      Trailer:
       
      Screenshots:





       
    • By Brandon Marx
      Hello forum,
      I have some decent amount of experience in Unity making games for Software Engineering projects in college, these were very specific projects however and I still am fairly new to building games. I wanted to make a game that uses the shadows of objects for collision detecting (i.e. shooting a gun at a characters shadow causes that character damage. What is the best engine to do this in (game will be 3D), and does anyone have any advice on how to approach this concept? I consider myself fairly experienced in programming, but game dev is just an entirely different beast.
    • By juicyz
      Hey all,
      I've been slowly working on my game called AotW for a while now.  I have come to the conclusions that it would be nice to cooperate with 1 or 2 others to help finish it.  Ive been trying to keep my GDD up to date with my ideas and development so that would give a better overview of the game when the time comes.  Currently I have a basic skeleton of the RPG elements needed but everything can still be discussed and talked about and we can transform my idea to something the group likes.
      The premise of the game is a Diablo-like procedurally generated map with RPG elements that include sockets, inventory, classes, abilities, crafting, loot, items, sockets, and enchanting.  This will be done in a 2D iso view as I can't do 3D art and I enjoy 2D games a lot.
       
      I don't plan on releasing this as this is more of a hobby project for me and I have a full-time job.  Though I'd like to start putting more hours into development and having others definitely will be motivation.  I also want to be able to say I have finally "finished" a game idea to some degree.  If the time comes and we want to release it, then we can go ahead and do so but that's not my purpose or plan. 
       
      Discord:
      Juicyz#3683
       
       
      Thanks,
      Juicyz
    • By Arnold // Golden Donkey Productions
      Hi, I've been working on this issue for a while and haven't yet found an answer.
      Does anyone know the best way to convert unity's LAT & LONG into a vector 3 position that I could use in a virtual world (if it's even possible). 
      Thankyou in advance
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Multithreading my game engine -- slower than expected performance

This topic is 3552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game engine, vastly simplified, does two things: simulate the game world, then render the game world. Note that I'm including getting input and some other subsystems as part of the "simulate" step. Based on this forum discussion, I decided to try moving the rendering to a second thread. Unfortunately, the performance is worse running in multithreaded mode than in single threaded mode on my dual core CPU... by a fair margin. Let me explain in a little more detail what's going on. Here's what the execution looks like in single threaded mode:
|---sim---|---sync---|-------render-------|
and repeat. Note: the "sync" represents the time spent sending the simulation updates to the rendering system. Here's what the execution looks like in multi-threaded mode:
main thread:   |---sim---|---wait---|---sync---|
render thread: |-------render-------|---wait---|
and repeat. Note that the main thread's simulation step finishes much faster than the rendering, and then has to wait for the rendering to finish before the main thread starts a sync (sending data to the render system). When the main thread is doing a sync, the render thread has to wait. Performance data indicates that the single threaded mode is using 100% of one core, as expected -- and I get about 65 FPS. In multithreaded mode, each thread uses a little over 50% of its core, spending the rest of the time waiting as per the diagram above -- and I get about 45 FPS. I was expecting the multithreaded version to do a fair amount of waiting due to the fact that right now my simulation of the game world isn't very CPU intensive, and also that my sync step is poorly optimized and takes longer than it should. I was expecting that the multithreaded version would be maybe marginally faster than the single threaded version, and then as I added complexity to the sim and sped up the sync, the multithreaded version would start to really out-pace the single threaded version. However, at this point the multithreaded version is so much slower than the single threaded version that I'm wondering if I've done something terribly, terribly wrong in the way I architected my multithreaded version. So, I thought I'd ask the gamedev folks: Am I doing something fundamentally wrong with my multithreaded architecture? Here it is again:
main thread:   |---sim---|---wait---|---sync---|
render thread: |-------render-------|---wait---|
The way I'm implementing the waiting is with SDL (libsdl.org) semaphores. I'm on a dual core linux 32-bit system. Also, just another data point, if I make the simulation much simpler and reduce the scene to something very simple (which reduces sync and render times), I can get upwards of 400 FPS out of the multithreaded mode and maybe 600 FPS from single threaded mode. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Do any of your steps do anything extra in multithreaded mode that they don't need to do in single-threaded mode (other than the semaphores)?

It doesn't seem like semaphores alone would cause that much penalty, unless you're accidentally setting up a situation where the sim step holds onto a semaphore that the render step wants (or vice versa):


|---sim----|-------wait--------|---sync---|
|---wait---|------render-------|---wait---|

or maybe

|---sync---|------wait---------|---sim----|
|---wait---|-----render--------|---wait---|

Share this post


Link to post
Share on other sites
I'll try to quickly sketch out the "right" way to do a multithreaded game engine:

Game logic runs continuously, pushing deltas to a message queue after each step.
Renderer runs continuously, grabbing and applying deltas before each frame.

This requires the game logic to use an entirely independent data set, and be completely decoupled from the renderer -- which is as it should be. The game logic will determine how objects in the game world move around, then send any changes to the renderer, which rearranges the scene as necessary.

No waiting! Unless you want to cap ticks per second for whatever reason. The data structure you use for IPC obviously needs to be threadsafe, which can be accomplished by any number of techniques. The lockless queue (see Google) is probably your friend in this case.

For details, take a look at this thread on the OGRE forums, and pay careful attention to what xavier says:
http://www.ogre3d.org/phpBB2/viewtopic.php?t=26496

It eventually gets pretty deep into the implementation details, including preallocation and reuse of message objects.

Share this post


Link to post
Share on other sites
Quote:
Do any of your steps do anything extra in multithreaded mode that they don't need to do in single-threaded mode (other than the semaphores)?


No, even the syncing uses the same code.

Quote:
it doesn't seem like semaphores alone would cause that much penalty


I agree. The high performance that I can get with no sim and very little data to sync or render indicates to me that the semaphores themselves probably add little overhead.

I'll look at my code a little more closely this afternoon to make sure I don't have a simple error somewhere that's causing excessive waiting beyond what I showed in the ASCII diagram.

Share this post


Link to post
Share on other sites
Quote:
Original post by venzon

main thread:   |---sim---|---wait---|---sync---|
render thread: |-------render-------|---wait---|


The way I'm implementing the waiting is with SDL (libsdl.org) semaphores. I'm on a dual core linux 32-bit system. Also, just another data point, if I make the simulation much simpler and reduce the scene to something very simple (which reduces sync and render times), I can get upwards of 400 FPS out of the multithreaded mode and maybe 600 FPS from single threaded mode.


Why are you waiting. The code should look something like this:

main thread: |-sim1-|-sim2-|-sim3-|-sim4-|-sim5-|-sim6|
render thread: |-------render0-------|-------render3-------|
Simply put, renderer takes latest complete simulation step, and renders that.

You may need to duplicate the state, one that's being simulated, and another which is being rendered.

Whether you pass the data between threads, or use read-only shared state is a matter of choice.

Of course, it's perfectly possible you have trivial coding error.

Share this post


Link to post
Share on other sites
A quick update. After restarting my system I realized that my earlier multithreaded performance data was invalid because I had a task running in the background on one of the cores that wasn't as idle as I thought. Woops. Now I see 90% utilization on one core and 30% on the other in multithreading mode, with 70 FPS. Single threading still shows high 60s. This is more in line with my expectations, since the rendering at the moment takes much longer than the simulation or the sync. Put pseudo-mathmatically, I expect the time per rendered frame (with my current architecture) to be (assuming Tsim < Trender):

Tsingle = Tsim + Tsync + Trender
Tmulti = Tsync + Trender

so:

Tmulti - Tsingle = Tsync - Tsim

Antheus and drakostar, a note on my simulation: I use a fixed timestep of 10 ms (game time). Each sim step will do multiple 10 ms updates until the game time matches wall clock time (which doesn't take very long because the sim is quick). After that it sits and waits until the render finishes so it can send it the latest data, then it repeats and does more updates until the game time matches wall clock time again. So, the fixed timestep of the sim means it will be doing waiting in one form or another. But, I think I understand the essence of your points, which is that I can get better performance by eliminating that sync portion that ties up both threads and running things continuously. I'll definitely look into that.

Share this post


Link to post
Share on other sites
Alright, I added a second buffer to the render thread and eliminated the lock-step sync, so now it looks like this:

main thread:   |---sim---|---sync---|---wait---|
render thread: |-------------render------------|

and repeat.

My processor usage is up to:
100% core usage from the render thread
34% core usage from the main thread

This is nifty because now I should be able to add considerable complexity to the simulation side without affecting the performance in multithreaded mode at all. Thanks for the help guys!

As a side note, my game is a racing simulation (vdrift.net) so I have a handy way to scale simulation complexity that won't tick off single-threaded users: allow racing against more AI cars if you have more cores!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement