SpriteKit,把一个进程放在另一个核心上?

SpriteKit, put a process on another core?

想象一下你的

 class NattyScene: SKScene {

您可能有一个自定义的节点字段,或者每帧都会发生的其他事情。现在想象你有一些计算,一个很好的例子可能是重心......

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

假设像

这样的问题,把它放在另一个线程上会很有意义

这是怎么回事?

有什么想法吗?


override func update(_ currentTime: TimeInterval) {

    let x = safely get info from a thread on another physical core on the device
    print("now that's cool")
}

在另一个核心上...

 func loopy() {

    let x = safely look at info on NattyScene
    let globalCOG = calculation
}

请注意,KOD 已指向 DispatchQueue,这很好 - 但有什么方法可以确保它真的在另一个核心上吗?

问得好,不幸的是我认为这不可能,主要原因有两个。

原因 1

您在 iOS 中没有这种低级别访问权限。

OS 是决定哪个线程在哪个内核上运行的人。 它还具有根据应用程序范围之外的多种条件打开和关闭内核的能力。

E.g. When in Grand Central Dispatch you write

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

You have no guarantee the closure will be executed on a different core.

原因 2

游戏运行 SpriteKit 循环确实执行了一堆东西

  1. 调用更新
  2. 评估操作
  3. 模拟物理
  4. 应用约束

您的想法确实意味着在 call update 阶段

中执行以下步骤
  1. 在 "background" 线程上移动
  2. 执行一些繁重的计算
  3. 将结果返回主线程

但此时您无法保证游戏 运行 循环仍处于 call update 阶段。它可能在 evaluates actions 中,甚至可能在下一帧工作。 另一方面,渲染每一帧的时间不超过 16 毫秒

可能的解决方案

您可以充分利用当前内核允许的 64 位,而不是针对另一个 CPU 内核。 查看 关于 SceneKit,其中列出了 SIMD 的优点。