MetalTest
MetalTest copied to clipboard
Glitchless smooth window resizing in Metal
Metal Window Resizing Test Project
The goal of this project was to figure out a way to do glitchless smooth window resizing with Metal, I succeeded. MTKView
has a problem where the redrawing is not synchronized with the window resize so it draws stretched frames, but there's a way to use CAMetalLayer
to make it work.
This project draws views with a triangle with two vertices at fixed offsets from the left edge, and one vertex at a fixed offset from the right edge. It has three implementations that are displayed stacked vertically in the following order:
-
MTKView
. Which is supposed to be the easy view. Apple's Hello Triangle example uses this, and suffers terrible resize juddering. Code is inMetalView.swift
. -
NSView
. This is a benchmark implementation that draws the triangle with Core Graphics, it works perfectly. Code is inCocoaView.swift
. -
CAMetalLayer
. Implemented according to the top answer from this Stack Overflow question. Using just the Stack Overflow answer works better thanMTKView
but still glitches occasionally. It's also necessary to usepresentsWithTransaction
and follow the recommendations in the docs for that option regardingwaitUntilScheduled
, that delivers completely glitch-free results. Code is inMetalLayerView.swift
.
GIF
Triangles are stacked in the order listed above, watch the top vertex of the top triangle: