Precompile SKShader

Is there a way to force precompilation of SKShader()s?

I am initializing a basic shader in my SKScene properties, but the shader only gets compiled when it is actually attached and rendered to a sprite.

class GameScene: SKScene {
  let myShader = SKShader(fileNamed: "myEffect.fsh")
  override func update(_ currentTime: TimeInterval) {
    if touches.count > 0 {
      mySprite.shader = myShader
      // this is where the warning triggers for the first time
    }
  }
}

I know this because the scene pauses for a bit, immediately before the shader is rendered for the first time, and I also get the Metal warning (which seems to also be a known bug in recent releases - https://developer.apple.com/forums/thread/661774):

[Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:3:19: warning: unused variable 's'
constexpr sampler s(coord::normalized,

I was expecting the shader to compile when it is initialized with SKShader(fileNamed:), since Apple docs says:

Compiling a shader and the uniform data associated with it can be expensive. Because of this, you should:

  • Initialize shader objects when your game launches, not while the game is running.

https://developer.apple.com/documentation/spritekit/skshader

This has been tested in Xcode 15.5.1 on a Macbook Pro Mid 2014 running macOS Big Sur 11.5.2 (20G95), on simulators running iOS 12.1, 14.5, and iPhone Xs running iOS 14.8.

Precompile SKShader
 
 
Q