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

Add a Comment