4

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

user16217248
  • 3,119
  • 19
  • 19
  • 37
blackirishman
  • 903
  • 1
  • 10
  • 23
  • Does the behavior change if you explicitly set the `MTKView`'s `paused` property upon re-showing the window? – warrenm Dec 10 '18 at 19:43
  • No it does not. I opened a ticket with apple and submitted sample code – blackirishman Dec 11 '18 at 19:44
  • Haven't heard back from Apple after I submitted code. At this point I am reinstantiating my MTKview when I reopen the window. It would be nice to have one for the lifetime of the app, but... – blackirishman Dec 17 '18 at 13:28
  • The internal displaylink stops running when I close the window that houses the view. I tried removing the view before closing the window, but that had no effect. Consequently, I created my own display link which I can start and stop to drive the MTKView. Apple hasn't gotten back to me. – blackirishman Dec 28 '18 at 16:11
  • I think we’d need to see how you’re (re-)creating your window (from a nib, fully programmatically, etc) to even begin to offer assistance here. – warrenm Dec 28 '18 at 17:58

2 Answers2

1

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

blackirishman
  • 903
  • 1
  • 10
  • 23
  • I think you need to be careful when creating the `CVDisplayLink` in order to avoid slowdown when multiple monitors are connected. I create mine using the the version that takes the single `CGDirectDisplayID` in an effort to combat this (known and ongoing) issue, but I don't know if it's worked yet. – trojanfoe Feb 22 '19 at 15:25
  • In fact I threw away `MTKView` altogether and created my own `NSView` subclass to have full control. – trojanfoe Feb 22 '19 at 15:26
  • I use "CVDisplayLinkCreateWithCGDisplay" to instantiate my displayLink and subscribe to windowChangedScreen notifications. – blackirishman Feb 23 '19 at 20:13
0

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.

vtruant
  • 273
  • 1
  • 12