MTKView delegate stops responding to - (void)drawInMTKView:(nonnull MTKView *)view calls

911 Views Asked by At

I have an MTKView in a window managed by a window controller. When I first instantiate the window controller and window, the MTKView calls my delegate's two calls and drawInMTKView is called at the preferred rate:

- (void)drawInMTKView:(nonnull MTKView *)view
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size

If close the window (not release the window just hide), when I re-open the window, the drawableSizeWillChange call is still invoked when I resize the MTKView, but the drawInMTKView is no longer called. The MTKView runs in its default mode with its own displaylink. As drawInMTKView is not called, it leaves be to believe that there is something wrong with the private displayLink variable in the MTKView.

I'm running Xcode 9.4.1 on OSX Mojave, recent 2017 MacBook Pro

2

There are 2 best solutions below

0
On

I had the same issue and I solved it by removing the MTKView from its superview. And put it back when the window is showing.

- (BOOL)windowShouldClose:(NSWindow *)sender
{
    [_metalPreview removeFromSuperview];

    return YES;
}

That way you don't need to create a custom CVDisplayLink.

3
On

I created my own CVDisplayLink callback and didn't use the one provided by MTKView to get around the possible issue with MTKView.