floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.55k stars 469 forks source link

macOS: workaround wobbly MTKView resizing #963

Closed Seb-degraff closed 6 months ago

Seb-degraff commented 6 months ago

Workaround for the MTKView resizing glitch by "anchoring" the layer to the window corner opposite to the currently manipulated corner (or edge). This prevents the content stretching back and forth during resizing. This is basically this: https://github.com/floooh/sokol/issues/700

As pointed out in the glitchess resizing blogpost, layerContentsPlacement = .topLeft helps because it prevents stretching the content of the layer while waiting for the correctly sized frame to be rendered. However it works only if the rendered elements are top left aligned. If there are elements that are rendered in the bottom right of the frame and the user resizes from the top left of the window, these elements will appear to shake as they get moved when resizing, then "aligned back" when the frame is rendered.

My solution is to detect which corner is being resized and set layerContentsPlacement accordingly in windowWillStartLiveResize.

Before:

https://github.com/floooh/sokol/assets/6556843/2172ed47-7ef7-476e-ac92-a5c74077e04d

Notice the blue squares stretching while resizing. Pausing the video near the end makes it even more obvious.

After:

https://github.com/floooh/sokol/assets/6556843/7047c9ba-244d-4421-ac43-7ada9a393b53

Notice the blue squares stability independent of which corner is being dragged. Pausing the video makes the latency apparent in the form of grey padding on the resized sides. Barely noticeable at 120 fps.

This workaround can be removed if/when migrating to CAMetalLayer: https://github.com/floooh/sokol/issues/727

Untested: dragging a window from a high dpi to a non high dpi monitor (I can test this in a few days if needed).

floooh commented 6 months ago

Wow, interesting and unexpectedly simple fix! I'll give it a whirl tomorrow and see how it looks on my side!

floooh commented 6 months ago

Gave it a whirl and it looks great!

I tested on the configs I have here with the samples imgui-sapp and imgui-highdpi-sapp:

...going to merge now...

floooh commented 6 months ago

...and merged. Many thanks for picking up the issue and creating this perfect little PR :)

...will also add a changelog update now.

Seb-degraff commented 6 months ago

Thank you for testing and merging so fast! And for the kind words!

By the way I really appreciate your work on the sokol libs, it's not an understatement to say they changed my programmer life! So I'm glad to be able to contribute back :)

For good measure I also tested this setup:

https://github.com/floooh/sokol/assets/6556843/1b1f574a-6f2a-459a-95d9-01f28409c846