macoscore-animationmetal

CAMetalDisplayLink does not work on separate thread


I am looking to implement CAMetalDisplayLink on a separate thread on a macOS application. I am basing my implementation on the following example project:

Achieving Smooth Frame Rates with Metal Display Link

This project allows you to configure whether a separate thread is used for rendering by setting RENDER_ON_MAIN_THREAD in GameConfig to 0. However, when I set it to use a separate thread nothing is rendered. Stepping through the code shows that a separate thread is created, but a CAMetalDisplayLinkUpdate is never received. Does anyone know why this does not work?


Solution

  • In runThread in GameView.m, change

    [runLoop runMode:_mode beforeDate:[NSDate distantFuture]];
    

    to

    [runLoop run];
    

    I don't know why the original code doesn't work (although it does use a lot of CPU), but this fixes the animation and the workload can be seen to move from the main thread to a secondary thread.