I'm using a MTKView to draw Metal content. It's configured as follows:
mtkView = MTKView(frame: self.view.frame, device: device)
mtkView.colorPixelFormat = .bgra8Unorm
mtkView.delegate = self
mtkView.sampleCount = 4
mtkView.isPaused = true
mtkView.enableSetNeedsDisplay = true
setFrameSize is overriden to trigger a redisplay.
Whenever the view resizes it scales its old content before it redraws everything. This gives a jittering feeling.
I tried setting the contentGravity property of the MTKView's layer to a non-resizing value, but that totally messes up the scale and position of the content. It seems MTKView doesn't want me to fiddle with that parameter.
How can I make sure that during a resize the content is always properly redrawn?
In my usage of Metal and
MTKView, I tried various combinations ofpresentsWithTransactionandwaitUntilScheduledwithout success. I still experienced occasional frames of stretched content in between frames of properly rendered content during live resize.Finally, I dropped
MTKViewaltogether and made my own NSView subclass that usesCAMetalLayerand resize looks good now (without any use ofpresentsWithTransactionorwaitUntilScheduled). One key bit is that I needed to set the layer'sautoresizingMaskto get thedisplayLayermethod to be called every frame during window resize.Here's the header file:
Here's the implementation:
For reference, I do all my Metal drawing in MTKView's
drawRectmethod.