KhronosGroup / MoltenVK

MoltenVK is a Vulkan Portability implementation. It layers a subset of the high-performance, industry-standard Vulkan graphics and compute API over Apple's Metal graphics framework, enabling Vulkan applications to run on macOS, iOS and tvOS.
Apache License 2.0
4.85k stars 429 forks source link

Can't run on visionOS simulator #1984

Open ericbyoui opened 1 year ago

ericbyoui commented 1 year ago

Doing fetchDependencies and make with visionossim is producing a MoltenVK.xcframework with only xros-arm64 and does not include the simulator one, almost as if it's doing the same as selecting visionos.

If you create a blank visionOS app in xcode and add the MoltenVK.xcframework in the framework section, you are able to build for (Any visionOS device (arm64)) but you can't build for the simulator.

It says: ~/Documents/MoltenVK/Package/Release/MoltenVK/MoltenVK.xcframework:1:1: While building for visionOS Simulator, no library for this platform was found in '~/Documents/MoltenVK/Package/Release/MoltenVK/MoltenVK.xcframework'.

System Info:

m1 mac
Ventura 13.4.1
Xcode Version 15.0 beta 3
cmake version 3.27.20230720-g388c980
ericbyoui commented 1 year ago

Also, when I try to use it in my project, I get this error:

building for 'xrOS-simulator', but linking in object file (/Users/user/lib/libMoltenVK.a[2](libMoltenVK.a-arm64-master.o)) built for 'xrOS'

I was able to get around this error by adding -ld64 to OTHER_LDFLAGS in the xcode build settings.

billhollings commented 1 year ago

@ericbyoui Thanks for all the debugging effort. I'll have a look soon. But if you have it sorted out already, feel free to submit a PR here.

I'm not clear on why your -ld64 flag would help. Do you understand why what fix works?

ericbyoui commented 1 year ago

@billhollings I found that -ld64 trick from this post: https://developer.apple.com/forums/thread/734321

Which lets me build and run now, but it crashes when I reach vkCreateInstance with this error:

-[MTLSimDevice minimumTextureBufferAlignmentForPixelFormat:], line 2,154: error 'MTLPixelFormatInvalid is not supported on this device.'
-[MTLSimDevice minimumTextureBufferAlignmentForPixelFormat:]:2154: failed assertion `MTLPixelFormatInvalid is not supported on this device.'

Looks like a similar issue that someone else ran into when iOS simulator was not working: https://github.com/KhronosGroup/MoltenVK/issues/726#issuecomment-614624369

I wonder if the answer is in the pr you wrote a while back here: https://github.com/KhronosGroup/MoltenVK/pull/935

I tried messing around with the MVKPixelFormats.mm file and adding visionos, it was a bit tricky and I was not sure exactly what to do since some code is obsolete now for visionos, but it did not help with something rough.

I attached a sample project to reproduce the issue. You just have to do fetchdependencies and make for visionossim in MVK_visionos_test/MoltenVKcopy

Then add the framework I attached in the xcode project or copy the info.plist and folder structure from my framework to the one you generated. (The framework I attached has no changes, it's just on current master but it has the folder structure for visionossim): d9b32e0c7515ea88fb88bfb1d374ec95d4a25038

MoltenVK.xcframework.zip MVK_visionos_test.zip

ericbyoui commented 1 year ago

@ifiddynine Any ideas? Where you able to run on visionOS simulator?

ifiddynine commented 1 year ago

Sorry for the delay! Yup we have it running since the last PR I submitted (thanks for merging that by the way @billhollings)

Haven't synced latest as I've been working on switching to direct Metal, but my first guess is that it's related to this: https://github.com/KhronosGroup/MoltenVK/blob/main/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h#L1085

Try adding something along the lines of:

#elif MVK_VISIONOS
#define MTLFeatureSet_iOS_GPUFamily1_v1         MTLGPUFamilyApple1
#define MTLFeatureSet_iOS_GPUFamily1_v2         MTLGPUFamilyApple1
#define MTLFeatureSet_iOS_GPUFamily1_v3         MTLGPUFamilyApple1
#define MTLFeatureSet_iOS_GPUFamily1_v4         MTLGPUFamilyApple1
#define MTLFeatureSet_iOS_GPUFamily1_v5         MTLGPUFamilyApple1

#define MTLFeatureSet_iOS_GPUFamily2_v1         MTLGPUFamilyApple2
#define MTLFeatureSet_iOS_GPUFamily2_v2         MTLGPUFamilyApple2
#define MTLFeatureSet_iOS_GPUFamily2_v3         MTLGPUFamilyApple2
#define MTLFeatureSet_iOS_GPUFamily2_v4         MTLGPUFamilyApple2
#define MTLFeatureSet_iOS_GPUFamily2_v5         MTLGPUFamilyApple2

#define MTLFeatureSet_iOS_GPUFamily3_v1         MTLGPUFamilyApple3
#define MTLFeatureSet_iOS_GPUFamily3_v2         MTLGPUFamilyApple3
#define MTLFeatureSet_iOS_GPUFamily3_v3         MTLGPUFamilyApple3
#define MTLFeatureSet_iOS_GPUFamily3_v4         MTLGPUFamilyApple3

#define MTLFeatureSet_iOS_GPUFamily4_v1         MTLGPUFamilyApple4
#define MTLFeatureSet_iOS_GPUFamily4_v2         MTLGPUFamilyApple4

#define MTLFeatureSet_iOS_GPUFamily5_v1         MTLGPUFamilyApple5

Must be that the format you're using isn't detected, so if feature set detection is going wrong that could easily be it.

Edit: Likely also need changes in these places: https://github.com/KhronosGroup/MoltenVK/blob/main/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm#L55 https://github.com/KhronosGroup/MoltenVK/blob/main/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm#L1505 https://github.com/KhronosGroup/MoltenVK/blob/main/MoltenVK/MoltenVK/GPUObjects/MVKFramebuffer.mm#L66C2-L66C2

visionOS is very similar to Mac Catalyst in that it doesn't support feature sets, so need to replicate the same code there.

BTW Keep in mind you won't be able to do true mixed reality. It probably wouldn't need that much work to support in MoltenVK, but we opted to do it in pure Metal instead. iirc it would be pretty trivial to do in the client app as long as you modify MoltenVK to add a method of getting the underlying MTLCommandBuffer (or encoders can't remember)

ericbyoui commented 1 year ago

Hi @ifiddynine, I just tried what you posted, but I'm getting the same error. I also tried it on this specific commit 4540175a7dd187ba1d52836d584f0bd57ff05b39 but no luck.

luozhonghai commented 1 year ago

Hi @ifiddynine, I just tried what you posted, but I'm getting the same error. I also tried it on this specific commit 4540175 but no luck.

please check your xcode version and its visionxr sdk, platform related marcros are different (Xcode 15 beta 8 TARGET_IOS 1 Xcode 15.1 beta has TARGET_IOS 0, wtf), which leads to different moltenvk compile result.

AdamGoodApp commented 1 month ago

Any update on solving this issue?

HyperCed commented 1 month ago

I made a fork which enables shader compilation on VisionOS simulator : https://github.com/HyperCed/MoltenVK/

ethan021021 commented 2 weeks ago

I made a fork which enables shader compilation on VisionOS simulator : https://github.com/HyperCed/MoltenVK/

Thanks a bunch for this! Was really confused when trying to compile shaders on why my MSL version was 0.0!