SpriteKit, put the process on another kernel?

Imagine you

class NattyScene: SKScene { 

you have a custom field for nodes or something else that happens in each frame. Now imagine that you have a calculation, a center of gravity might be a good example ...

 var globalCOG: CGPoint func updateCOG() { .. say, get all the .position of all Spaceship .. globalCOG = .. some point } 

It would be wise to put this in a different thread, suggesting problems like

  • it threadsafe / fast to read.
  • this other supposed thread on a different core knows about SpriteKit frames (so you can calculate it before the failure in normal mode, etc., maybe you might prefer to skip the frame or something else - the usual programming panorama with a streaming game)
  • you can thread safe / blahblah write COG global back to the rest of SpriteKit

What is the deal about this?

  • This is actually a modern idiom for this in Swift4 / SpriteKit
  • How to make him switch to another physical core?

Any ideas?


 override func update(_ currentTime: TimeInterval) { let x = safely get info from a thread on another physical core on the device print("now that cool") } 

on another core ...

  func loopy() { let x = safely look at info on NattyScene let globalCOG = calculation } 

Please note that KOD pointed to DispatchQueue , which is great - but is there any way to make sure that it really resides on a different kernel?

+5
source share
1 answer

A good question, unfortunately, I do not think that this is possible for two main reasons.

Reason 1

You do not have such low-level access in iOS.

The OS is the one who decides which thread runs on the kernel. It also has the ability to turn the kernel on and off depending on several conditions that go beyond the scope of your application.

eg. When in Grand Central Dispatch you write

DispatchQueue.global(qos: .background).async { }

You have no guarantee that the closure will be performed on another core.

Reason 2

Starting a SpriteKit startup loop does a ton of things

  • trigger update
  • evaluates actions
  • imitates physics
  • applies restrictions

Your idea involves the following steps in the call update phase

  • moving in the background thread
  • perform some heavy calculations
  • return the result to the main thread

But at the moment, you have no guarantee that the Run Game loop is still in the call update phase. It may be in evaluates actions , or it may even work in the next frame. On the other hand, no more than 16 milliseconds is required for each frame.

Possible Solution

Instead of targeting another CPU core, you can take full advantage of the 64 bits allowed by the current core. Check out this Q / A about SceneKit for the benefits of SIMD.

+4
source

Source: https://habr.com/ru/post/1273226/


All Articles