screen-13 icon indicating copy to clipboard operation
screen-13 copied to clipboard

Allow dynamic linking of molten-vk on MacOS.

Open karhu opened this issue 7 months ago • 7 comments

I wanted to use Vulkan validation layers on MacOS. The changes below are what's necessary to dynamically link Vulkan from an installed Vulkan SDK.

I've moved these changes behind a new feature-flag, to keep the static linking behavior as default on MacOS.

Please take a look :)

karhu avatar May 22 '25 12:05 karhu

@karhu Would you be able to provide/link a bit of docs on how to use this feature (dynamically linking Vulkan, using Vulkan validation layers on MacOS)? If so, I would be happy to test it.

DGriffin91 avatar May 22 '25 17:05 DGriffin91

@DGriffin91 Thanks for taking the time to look into this. I'm far from an expert on Vulkan tbh, but I'm happy to provide some context based on my understanding:

The current version of screen-13 has different behavior on MacOS, compared to other platforms (Win, Linux, ...?). While on most platforms it relies on dynamic linking, on MacOS it does not, and instead relies on ash-molten to statically link against Vulkan (to be specific it links against MoltenVK, a Vulkan compatibility layer on top of Metal, specific for MacOS).

Vulkan validation layers work by injecting verification logic into your application when the dynamically linked Vulkan symbols are resolved at runtime. This allows enabling/disabling them without recompiling the target binary.

The undesirable consequences of this are:

  • ash-molten statically links against a hardcoded version of MoltenVK, as of today that is 1.2.8, which at about a year old is a bit outdated, with 1.3.0 being the latest release.
  • Vulkan validation layers cannot be injected.
  • Longer compilation times, running an uncached build on MacOS takes quite a bit of time for the ash-molten dependency.
  • Differences in debugging capabilities between platforms.

On the other hand, the desirable consequences of this are:

  • No need to separately install the Vulkan SDK on your machine.
  • "cargo run ..." just works / compiles.

Check the ash-molten page, as they are pretty open about that in their project description.

karhu avatar May 22 '25 19:05 karhu

As for testing instructions:

  • Get the VulkanSDK for MacOS from the download page linked in this article, and while at it give at least the first part of the article a read :)
  • After installing the SDK, I had a Vulkan Configurator icon in my top-left menu bar. Opening that application allows to enable /disable Vulkan validation features. image
  • You might (not sure) have to run a source ~/VulkanSDK/1.4.309.0/setup-env.sh in the terminal session you're using to run your rust build and run commands.
  • Lastly, pick your screen-13 based project of choice and update the dependency to link to my fork, and enabling the new feature flag, something along the lines of screen-13 = { path = "../third-party/screen-13", features = ["macos-dynamic-molten-vk"]} assuming you have it locally checked out.
  • Run the app with different validation settings, and observe how verbose the console output becomes.

karhu avatar May 22 '25 20:05 karhu

Thank you for those instructions - I've never taken the time to try out dynamic linking Vulkan on Mac but I definitely want the debugging layers there. Mac has always been a "best-effort" platform for what I've been doing and I would like this get merged.

I'll test on the Mac's I have access to:

  • [ ] MacBook Pro 2016 Intel HD 530 & AMD Radeon Pro 450 (Monterey 12.7.6)
  • [ ] MacBook Pro 2023 M3 Max (Sequoia 15.4)
  • [x] Mac mini 2023 M2 (Sonoma 14.5)

attackgoat avatar May 22 '25 20:05 attackgoat

Glad to hear! For reference, I've been testing a similar setup as your second one: MacBook Pro 2023 M3 Max (Sequoia 15.5), Vulkan SDK 1.4.309.0

Btw, I'm curious about your preference as to which linking behavior should be the default, and which opt-in via feature flag. I implemented it the way it is right now in order to not break any existing behavior. Defaulting to "static linking" definitely has the advantage that it is more likely to "just work". On the other hand, defaulting to dynamic linking would make the library more consistent across platforms.

karhu avatar May 25 '25 14:05 karhu

Sorry for the delay, I've just now tried this and it works as described.

I would like to swap the feature flag for linked and have the same logic as Ash: default to loaded and allow linked. Really there is no reason we can't allow Ash to be statically linked on Windows/Linux.

I'll add some details to the documentation before release, too:

Vulkan SDK optional except on macOS unless using the linked feature

... but probably more wordy. The way I put that does seem super confusing.

attackgoat avatar May 30 '25 13:05 attackgoat

@karhu I'll make the above changes, if that's alright with you?

attackgoat avatar Jun 12 '25 16:06 attackgoat