Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: MoltenVK/Vulkan backend for macOS #7482

Merged
merged 2 commits into from
Oct 14, 2023
Merged

Conversation

Akemi
Copy link
Member

@Akemi Akemi commented Feb 23, 2020

this is in a very early state. i still need to majorly clean this code up and kill all the code duplications. my goal is to have one code base to use in all the macOS backends (might be a bit tricky with libmpv/cocoa-cb). nothing really has been tested so far. it renders the video and the basic windowing stuff works.

TODO:

  • cleanup code
  • unify the backends
  • properly detect moltenvk(?)
  • proper naming
  • performance tests
  • feature tests
  • use a CAMetalLayer directly instead of the view
  • Documentation
  • Bundle creation changes doesn't work properly atm, needs to be fixed separately
  • 10bit check needs to be implemented in vulkan context

though feel free to report any kind of problems you encounter.

simple and dirty way to install the dependencies:

#!/bin/bash

export DEPS_ROOT_LOCATON=$(grealpath .)
export VULKAN_SDK_VERSION="1.2.131.2"
export VULKAN_SDK_BASENAME="vulkansdk-macos-$VULKAN_SDK_VERSION"
export VULKAN_SDK="$DEPS_ROOT_LOCATON/$VULKAN_SDK_BASENAME/macOS"
export VULKAN_SDK_PKG_CONFIG_PATH="$VULKAN_SDK/lib/pkgconfig"
export PATH="/usr/local/opt/python/libexec/bin:$VULKAN_SDK/bin:$PATH"

cat > "$VULKAN_SDK_PKG_CONFIG_PATH/vulkan.pc" << EOL
libdir=$VULKAN_SDK/lib
includedir=$VULKAN_SDK/include

Name: vulkan
Description: Vulkan
Version: $VULKAN_SDK_VERSION
Libs: -L\${libdir} -lMoltenVK
Libs.private: -lm
Cflags: -I\${includedir}
EOL
  • symlinked the vulkan.pc file /usr/local/lib/pkgconfig
    ln -s vulkansdk-macos-1.2.131.2/macOS/lib/pkgconfig/vulkan.pc /usr/local/lib/pkgconfig/vulkan.pc
  • install libplacebo

fixes #5571

@Akemi
Copy link
Member Author

Akemi commented Feb 25, 2020

i am not recommending anything. even said it's a dirty way. i didn't look into building/linking properly yet.

.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK,
.pNext = NULL,
.flags = 0,
.pView = p->vo_macos.video,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

segfaulted, until I have changed it to p->vo_macos.video.layer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's kinda interesting since the docs say something else https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkMacOSSurfaceCreateInfoMVK.html

on the other hand the moltenVK docs say something else and the layer is recommended KhronosGroup/MoltenVK@e546b83

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VK_MVK_macos_surface is called obsolete here KhronosGroup/Vulkan-Tools#220, so maybe it's better to just use VK_EXT_metal_surface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. i was planning to use the Metal Layer directly at some point anyway (i looked into metal surface ext beforehand), to reduce some redundancy and make it similar to cocoa-cb with its layer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, i don't think i have enough time for that.

@Akemi
Copy link
Member Author

Akemi commented Jul 17, 2020

a little update. i finished unifying the two backends to get rid of a majority of all the code duplications. the cocoa-cb generalisation is completely done and properly cleaned up, and could possibly be merged independently. though there are no real benefits besides me not having to backport possible fixes from master. the molten backend still has to be cleaned up properly.

@Akemi Akemi force-pushed the mac_vulkan branch 5 times, most recently from 2c3a49c to 7a3e19f Compare July 18, 2020 15:11
@Akemi Akemi force-pushed the mac_vulkan branch 5 times, most recently from 5243559 to a9e376e Compare July 26, 2020 09:53
@Akemi
Copy link
Member Author

Akemi commented Jul 26, 2020

some findings:

  • with interpolation i can reliable reproduce rendering freezes, this can be mitigated by --vulkan-disable-events
  • initialisation takes quite a lot of time, sometimes more than a second, this can be mitigated by --gpu-shader-cache-dir
  • setting an icc profile on startup doesn't work properly and needs to be set after the first draw (still needs a proper fix)
  • vsync jitter is still a bit erratical and needs more testing
  • live reisze is not as smooth as on cocoa-cb, rendering surface is not fixed in size and updates constantly
  • drawing in background doesn't work and blocks, like with every layer, needs a mechanism to actively skip
  • drawing size update still needs testing

for myself building libplacebo so i don't forget it:

meson configure --clearcache $DIR
meson --reconfigure $DIR -Dvulkan-registry=/Users/Akemi/Repositories/vulkansdk-macos-1.2.141.2/macOS/share/vulkan/registry/vk.xml

@Akemi
Copy link
Member Author

Akemi commented Aug 1, 2020

some more findings:

stats with the follwoing:
VK_PRESENT_MODE_IMMEDIATE_KHR: https://0x0.st/ixIc.png
VK_PRESENT_MODE_FIFO_KHR: https://0x0.st/ixIA.png

@Akemi
Copy link
Member Author

Akemi commented Aug 1, 2020

includes for the molten stuff needs to be manually installed?

ln -s /Users/Akemi/Repositories/vulkansdk-macos-1.2.141.2/MoltenVK/include/MoltenVK /usr/local/include/MoltenVK
ln -s /Users/Akemi/Repositories/vulkansdk-macos-1.2.141.2/MoltenVK/include/vulkan-portability /usr/local/include/vulkan-portability

https://github.com/haasn/libplacebo/blob/5e517936c15583e3148f46d76135d63537617f9f/src/vulkan/swapchain.c#L164-L172 check why it picks an 8bit format even so 10/16bit formats are available.

@Akemi Akemi force-pushed the mac_vulkan branch 3 times, most recently from 20af3e4 to c6bb856 Compare August 2, 2020 12:38
@Akemi
Copy link
Member Author

Akemi commented Aug 7, 2020

some more info:

  • VK_PRESENT_MODE_IMMEDIATE_KHR causes major tearing and can't be used, VK_PRESENT_MODE_FIFO_KHR needs to be used but this causes bad vsync

some more info from recent testing with moltenvk 1.2.148.0 i updated to:

  • performance with VK_PRESENT_MODE_FIFO_KHR got a lot better, still needs proper testing
  • after updating libplacebo the bad performance was back, caused by commit haasn/libplacebo@055cdc0 can't reproduce this anymore, maybe a hiccup

@Akemi
Copy link
Member Author

Akemi commented Aug 8, 2020

did some performance tests today. i couldn't find any sample or settings that would indicate any kind of performance difference, neither better nor worse, that weren't bottlenecked by decoding speed.

only thing i noticed is that vsyncing on moltenvk is a tad more unstable so far.

@github-actions
Copy link

Download the artifacts for this pull request:

Windows

@m154k1
Copy link
Contributor

m154k1 commented Sep 30, 2023

Nice, thanks! I've did a quick test, looks very promising :)

@github-actions
Copy link

github-actions bot commented Oct 1, 2023

Download the artifacts for this pull request:

Windows

@Akemi
Copy link
Member Author

Akemi commented Oct 1, 2023

i fixed/adjusted the precise timer. it's there for testing purpose mostly. when i started with this +2 years ago, moltenvk had some problems where the precise timing worked better than directly using the displaylink callback. it should be tested further, hence the new option to switch between 3 different modes.

in the long run the precise timer should be removed and the proper presentation feedback should be used. this won't be done in the PR most likely.

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Download the artifacts for this pull request:

Windows

@m154k1
Copy link
Contributor

m154k1 commented Oct 2, 2023

Great. All 3 modes are working good. I'm not sure how to properly test this but playback, seeking, --video-sync=audio/display-resample, etc. are working as expected. Tested on built-in display (60Hz) and external display (75Hz) with random content.

The only difference I can find is vsync jitter.

  • --macos-frame-timer=callback/system has 0.001-0.040 jitter. Usually it is ~0.020.
  • --macos-frame-timer=precise has very low and stable 0.001 jitter 99% of the time.

But anyway, I can't tell any difference without stats menu, so I'm fine with whatever mode.

@Akemi
Copy link
Member Author

Akemi commented Oct 2, 2023

if it doesn't make any difference anymore, that's good.

i found one issue with the precise timer. toggling fullscreen several times throws off the vsync in windowed mode on my end. everything fine in fullscreen.

@m154k1
Copy link
Contributor

m154k1 commented Oct 2, 2023

i found one issue with the precise timer. toggling fullscreen several times throws off the vsync in windowed mode on my end. everything fine in fullscreen.

Hmm, works fine for me...

@Akemi
Copy link
Member Author

Akemi commented Oct 7, 2023

i am done with this now and i am comfortable with it being merged.

  • mpv --vulkan-device=help seems broken now
  • the bundle creation should be fixed in a separate PR since it's generally broken in some cases
  • render timing should be changed to use the presentation feedback in separate PR

also i am not quite sure about the commit message prefix.

meson.build Show resolved Hide resolved
meson.build Outdated Show resolved Hide resolved
add support for vulkan through metal and a translation layer like
MoltenVK. also add the possibility to use different render timing modes
for testing.

i still consider this experimental atm.
@github-actions
Copy link

github-actions bot commented Oct 7, 2023

Download the artifacts for this pull request:

Windows

@Akemi Akemi merged commit 78d4374 into mpv-player:master Oct 14, 2023
@Akemi Akemi deleted the mac_vulkan branch October 14, 2023 16:40
@Akemi
Copy link
Member Author

Akemi commented Oct 14, 2023

have fun and happy testing.

@guyman624
Copy link

would this work with libmpv and ios as well? or would more work need to be done first?

@Akemi
Copy link
Member Author

Akemi commented Oct 30, 2023

libmpv only has opengl support, if you run your own render loop. wid embedding ist not supported. so it won't work.

also see my previous comment #7482 (comment)

@rcombs
Copy link
Contributor

rcombs commented Oct 30, 2023

wid embedding does work with libmpv, and adding it here likely wouldn't be a huge undertaking. Not sure if there's something else that'd be preferred in the long run.

@Akemi
Copy link
Member Author

Akemi commented Oct 30, 2023

nothing else was claimed. it does not work currently with the code related to this PR. like you said yourself. the feature is not implemented.

@aehlke
Copy link

aehlke commented Oct 30, 2023

would this work with libmpv and ios as well? or would more work need to be done first?

someone's ported it to ios via flurry which you can embed as a (heavy?) wrapped component inside a non-flurry ios project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

leverage Vulkan as default on MacOS and iOS now that MoltenVK is open-sourced