SpriteKit multithreading

I have split this question into two parts. Another project specific question about multithreading. And one question about how multi-threaded communication is in SpriteKit in general.

First question: I am currently working on a test project to learn more about optimizations in SpriteKit. In my test project, I have over 2000 spriteNodes, each with some basic AI moving node with actions.

I see a significant performance boost when setting Alpha = 0 on nodes that are not currently in the screen area. But of course running this check in the update function on more than 2000 nodes increases the load. Is it possible to use multithreading for some kind of loading of the main thread. For example, to perform this check on a different thread.

General question: I couldn't find very much information about multithreading in SpriteKit, so id would like to better understand the whole topic, here are some guiding questions I have. But if you want and have knowledge, feel free to expand outside of this area with any information you deem important.

  • When would it make sense to use SpriteKit multithreading?
  • What are the main limitations and problems with using multithreading.
  • How to create a basic multithreaded example. Can I use swift to generate code? where can i put the code? I began?
  • What should I remember about multithreading and render loop.
  • Is there any difference between threads. Are they designed for different purposes?
  • Do you know a good article or study resource to learn more about multithreading?
+3


source to share


2 answers


Use central central dispatch or SKAction.run (queue). It's easy to set up, but difficult to get syncronized with SpriteKit for "every frame" purposes. Multithreading will be useful for SK for things that are needed "later" like preload levels, web checking, etc.

SpriteKit is already pretty optimized considering it's a high-level sdk; you are probably better off figuring out specific ways to optimize your code than messing around with multiple threads.

Example: balancing CPU and GPU vs Memory Usage ... It might be possible to offload the CoreAnimation task (which uses the GPU) for something like drawing shapes / particles, but I haven't tried that.

Here's a good answer from someone who knows a lot more than they talk about than me:



fooobar.com/questions/707417 / ...

Here's also a good tutorial that explains multithreading and how to do it with GCD (but SKAction is most likely preferable):

https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1

+2


source


Ok, so I am doing some research and now I have a basic understanding of multithreading in SpriteKit. I decided to summarize it all so that other people who know little about the concept can get started.

Basic overview. CPU and multithreading

Multithreading is a way to make the processor run on more than one process at the same time. Each process will have its own Thread. This now allows us to multitask. like surfing the internet and watching a movie at the same time. If these processes run in one thread. The movie will freeze on reloading the web page until the reload is complete.

Threads and processor cores are two different things. The CPU core is the hardware that does the computation, while the Thread is more like a pipeline feeding the processor with things to compute. So from the above example there will be 2 pipes. One from the web browser, and one from the player, which loads the CPU with information for calculation. The CPU will then alternate between the two pipes doing the calculations for both, giving them equal attention. This does not mean that using two threads will improve CPU performance. but it allows two applications to share processor resources.

Multithreading and SpriteKit

If multithreading allows us to use the CPU at the same time and do multiple computations. how would i improve performance in SpriteKit?

Well. If your device has only 1 processor core. All we can really do is prioritize tasks to ensure that our main stream gets the most attention. And we want to give the main thread the most attention (from the CPU) to keep the FPS high, because that is the thread that our render loop is using. Therefore, if we have some computation that does not necessarily complete immediately, we could run it on a different thread with a lower priority.

BUT! if your device has 2 processor cores like most IOS devices. You can really improve the performance of your game. As stated earlier, our render loop works on our main theme. you want this stream to have enough resources to complete all of its computations before its time to display the next frame. (or you will lose your frame rate). The main thread only connects to the ONE core . So when you reach 100% CPU usage with almost 0 FPS. you are actually using only half of the processor resources available on the device.

But with multithreading, you can take some load of the main thread and put it on the second core. In a test project, I managed to get 150% CPU usage at 60fps! Its strange to have a 150% processor, but actually showcases the combined use of two cores. if you have 3 cores the max size will be 300%.

I know this sounds too good to be true, and in fact it is, as you will learn in the next section.



Limitations and disadvantages with multithreading

Its perfectly possible to use the resources available from the second core. But there are some calculations that need to be done on the main thread. If we look at the render loop. All light blue areas should work on the main thread. evaluate actions, simulate physics, constraints and rendering. And this is often the hardest computation in a project, and quickly becomes a performance bottleneck. However, depending on the structure of your project, many other calculations, such as measuring distances, refreshing positions, AI decisions, etc., could be placed on a second core to take some load off the main thread.enter image description here

How do I implement multithreading in my project.

The easiest way to do multithreading in SpriteKit is to use what's called: Grand Central Dispatch (GCD). This API comes out of the box, you don't need to include anything in your project. The GCD does all the heavy lifting to manage your flows in a safe and efficient way.

Getting started with GDC is very easy. The code below will run myFunction on a different thread.

run(SKAction.run(myFunction, queue: DispatchQueue.global(qos: .background)))

func myFunction(){
// Do some work here
}

      

You can also define dispatchQueue directly inside the function. or anywhere in your code. All of your work inside the dispatch block will run asynchronously on the appropriate thread selected by the GDC.

func myFunction(){

    DispatchQueue.global(qos: .background).async {
       //Do some work here...
    }

}

      

**** CONTINUED ***

+2


source







All Articles