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.81k stars 426 forks source link

Application crashes when setting up swapchain on Vulkan SDK version of MoltenVK #141

Closed ghost closed 6 years ago

ghost commented 6 years ago

When attempting to set up a swapchain on an OS X 10.11.6 system, I get the crash below.

I note that this same code works fine with MoltenVK 0.18, the version I was using for testing.

My swapchain is requesting VK_PRESENT_MODE_IMMEDIATE_KHR.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CAMetalLayer setDisplaySyncEnabled:]: unrecognized selector sent to instance 0x7fa44051dbd0'
terminating with uncaught exception of type NSException
abort() called

Application Specific Backtrace 1:
0   CoreFoundation                      0x00007fff9107e452 __exceptionPreprocess + 178
1   libobjc.A.dylib                     0x00007fff9201af7e objc_exception_throw + 48
2   CoreFoundation                      0x00007fff910e818d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00007fff90fee4c1 ___forwarding___ + 1009
4   CoreFoundation                      0x00007fff90fee048 _CF_forwarding_prep_0 + 120
5   libMoltenVK.dylib                   0x000000010bc7e5aa _ZN12MVKSwapchainC2EP9MVKDevicePK24VkSwapchainCreateInfoKHR + 202
6   libMoltenVK.dylib                   0x000000010bc6926b _ZN9MVKDevice15createSwapchainEPK24VkSwapchainCreateInfoKHRPK21VkAllocationCallbacks + 43
7   libMoltenVK.dylib                   0x000000010bc4bdd2 vkCreateSwapchainKHR + 18
8   VulkanEngine                        0x000000010baf1821 _ZN12VulkanEngine15createSwapchainEv + 241
9   VulkanEngine                        0x000000010baf083e _ZN12VulkanEngine29createStaticRendererResourcesEv + 78
10  VulkanEngine                        0x000000010bae9db1 main + 2465
11  libdyld.dylib                       0x00007fff913e35ad start + 1
ghost commented 6 years ago

I should also note that the application renders fine with VK_PRESENT_MODE_FIFO_KHR used instead.

Looking at some other code examples dealing with CAMetalLayer (SDL), it seems likely that setDisplaySyncEnabled is only available on OS X 10.13.

billhollings commented 6 years ago

@marchingcubes

vkGetPhysicalDeviceSurfacePresentModesKHR() will not include VK_PRESENT_MODE_IMMEDIATE_KHR unless it is supported on the device, and you should not create a swap chain with any present mode not included in that list.

I've fixed the crash in PR #142...and MoltenVK now at least avoids the crash if VK_PRESENT_MODE_IMMEDIATE_KHR is used incorrectly.

The crash did not appear on previous versions of MoltenVK because VK_PRESENT_MODE_IMMEDIATE_KHR was not supported at all...and was simply ignored. Now it is supported if it is available (macOS 10.13 only).

but0n commented 6 years ago

@billhollings Hi, I have same problems when setting up swapchain. My code is here. I tried to modify the presentMode of swapchain create info, but it is still crashing no matter what presentMode I selected, even VK_PRESENT_MODE_FIFO_KHR.

Here is the console messages:

SDL Extension Count : 2

    Extension 0: VK_KHR_surface

    Extension 1: VK_KHR_surface

[mvk-info] MoltenVK version 1.0.12. Vulkan version 1.0.77.
[mvk-info] GPU device:
        model: Intel(R) Iris(TM) Plus Graphics 640
        type: Integrated
        vendorID: 0x8086
        deviceID: 0x5926
        pipelineCacheUUID: 00000000-0000-0000-0000-27130000271C
    supports the following Metal Feature Sets:
        macOS GPU Family 1 v3
        macOS GPU Family 1 v2
        macOS GPU Family 1 v1

Physical devices Count : 1

    Driver Version: 10012
    Device Name:    Intel(R) Iris(TM) Plus Graphics 640
    Device Type:    1
    API Version:    1.0.77
GPU[0] Count of Queues: 8
Supported operationg on this queue:
         Graphics @ 0
         Compute @ 0
         Transfer @ 0
[mvk-info] Created VkDevice to run on GPU Intel(R) Iris(TM) Plus Graphics 640

GPU supported? Yes

 - support extension amount:  5
     Name: VK_MVK_moltenvk
     Name: VK_KHR_swapchain
     Name: VK_KHR_surface
     Name: VK_AMD_negative_viewport_height
     Name: VK_MVK_macos_surface
Resolution:
    Width - 640
    Height - 480
Max Resolution:
    Width - 640
    Height - 480
Physical Device Surface Formats count: 3

     Format 0:
         * format: 2c
         * colorSpace: 0
     Format 1:
         * format: 32
         * colorSpace: 0
     Format 2:
         * format: 61
         * colorSpace: 0

Physical Device Surface Present Modes count: 2

     Present Modes 0:
         * mode: 2
     Present Modes 1:
         * mode: 0
2018-07-22 20:47:11.513 helloVulkan[15338:838908] -[NSTaggedPointerString setDevice:]: unrecognized selector sent to instance 0x5
2018-07-22 20:47:11.514 helloVulkan[15338:838908] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString setDevice:]: unrecognized selector sent to instance 0x5'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff4fbb9a17 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff7b9bea18 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff4fc414fc -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff4fb48992 ___forwarding___ + 1490
    4   CoreFoundation                      0x00007fff4fb48338 _CF_forwarding_prep_0 + 120
    5   libMoltenVK.dylib                   0x000000010c229500 _ZN12MVKSwapchainC2EP9MVKDevicePK24VkSwapchainCreateInfoKHR + 112
    6   libMoltenVK.dylib                   0x000000010c21327b _ZN9MVKDevice15createSwapchainEPK24VkSwapchainCreateInfoKHRPK21VkAllocationCallbacks + 43
    7   libMoltenVK.dylib                   0x000000010c1f6e82 vkCreateSwapchainKHR + 18
    8   libvulkan.1.dylib                   0x0000000106b15bc5 vkCreateSwapchainKHR + 69
    9   helloVulkan                         0x0000000106ae893c getDevices + 2220
    10  helloVulkan                         0x0000000106ae89b6 main + 54
    11  libdyld.dylib                       0x00007fff7c99a3ed start + 1
    12  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1]    15338 abort      ./helloVulkan

Is there something wrong with my configuration?

Thanks.

billhollings commented 6 years ago

@but0n

It looks like the surface you are submitting in VkSwapchainCreateInfoKHR ::surface is not valid...and specifically...does not contain a valid CAMetalLayer.

In your code, it looks like this surface is created by SDL_Vulkan_CreateSurface()...which is not a MoltenVK function...and I assume is part of SDL.

Are you able to determine what that SDL_Vulkan_CreateSurface() function is doing?

mrtrizer commented 5 years ago

@billhollings I'm struggling with similar issue in my experiments. When I link MoltenVK via Vulkan loader as ICU driver, it falls within swapchain initialization. When I link MoltenVK as a static library, swapchain initialization works, but debug layers unavaliable, so I can't carry on without them.

I use prebuilt vulkansdk-macos-1.1.85.0 from https://vulkan.lunarg.com/sdk/home I've just noticed that a new version is avaliable and tried with 1.1.92.0, but no luck. Maybe I've missed something in window initializaton (I've tried to init window with SDL2 first, but then decided to try to reproduce initialization by myself, so you can see what exactly happening in window initialization)

Main source with vulkan initialization: https://github.com/mrtrizer/VulkanTriangleMacOS/blob/master/src/main.cpp Surface init ObjC: https://github.com/mrtrizer/VulkanTriangleMacOS/blob/master/src/macOSInterface.m CMakeLists: https://github.com/mrtrizer/VulkanTriangleMacOS/blob/master/generated/cmake/CMakeLists.txt

Trace:

2018-11-25 13:42:43.261415+0300 TestApp[40092:3235772] MessageTracer: Falling back to default whitelist
2018-11-25 13:42:43.278224+0300 TestApp[40092:3235772] -[NSTaggedPointerString setDevice:]: unrecognized selector sent to instance 0x7fff3e110005
2018-11-25 13:42:43.281313+0300 TestApp[40092:3235772] [General] An uncaught exception was raised
2018-11-25 13:42:43.281350+0300 TestApp[40092:3235772] [General] -[NSTaggedPointerString setDevice:]: unrecognized selector sent to instance 0x7fff3e110005
2018-11-25 13:42:43.281443+0300 TestApp[40092:3235772] [General] (
    0   CoreFoundation                      0x00007fff3ff6f2db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff6710cc76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff40007db4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff3fee5820 ___forwarding___ + 1456
    4   CoreFoundation                      0x00007fff3fee51e8 _CF_forwarding_prep_0 + 120
    5   libMoltenVK.dylib                   0x000000010284708f _ZN12MVKSwapchainC2EP9MVKDevicePK24VkSwapchainCreateInfoKHR + 111
    6   libMoltenVK.dylib                   0x00000001028304cb _ZN9MVKDevice15createSwapchainEPK24VkSwapchainCr
eateInfoKHRPK21VkAllocationCallbacks + 43
    7   libMoltenVK.dylib                   0x000000010280ff4b vkCreateSwapchainKHR + 27
    8   TestApp                             0x0000000100009686 _Z15createSwapchainP18VkPhysicalDevice_TP10VkDevice_TP14VkSurfaceKHR_TRK17SwapchainSettings + 358
    9   TestApp                             0x000000010000daf8 main + 4104
    10  libdyld.dylib                       0x00007fff67d26015 start + 1
)
2018-11-25 13:42:43.281921+0300 TestApp[40092:3235772] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString setDevice:]: unrecognized selector sent to instance 0x7fff3e110005'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff3ff6f2db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff6710cc76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff40007db4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff3fee5820 ___forwarding___ + 1456
    4   CoreFoundation                      0x00007fff3fee51e8 _CF_forwarding_prep_0 + 120
    5   libMoltenVK.dylib                   0x000000010284708f _ZN12MVKSwapchainC2EP9MVKDevicePK24VkSwapchainCreateInfoKHR + 111
    6   libMoltenVK.dylib                   0x00000001028304cb _ZN9MVKDevice15createSwapchainEPK24VkSwapchainCreateInfoKHRPK21VkAllocationCallbacks + 43
    7   libMoltenVK
.dylib                   0x000000010280ff4b vkCreateSwapchainKHR + 27
    8   TestApp                             0x0000000100009686 _Z15createSwapchainP18VkPhysicalDevice_TP10VkDevice_TP14VkSurfaceKHR_TRK17SwapchainSettings + 358
    9   TestApp                             0x000000010000daf8 main + 4104
    10  libdyld.dylib                       0x00007fff67d26015 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Avaliable layers and extensions:

Validation layers:
    VK_LAYER_GOOGLE_unique_objects
    VK_LAYER_GOOGLE_threading
    VK_LAYER_LUNARG_standard_validation
    VK_LAYER_LUNARG_core_validation
    VK_LAYER_LUNARG_parameter_validation
    VK_LAYER_LUNARG_object_tracker
Required extensions for instance:
    VK_KHR_surface
    VK_MVK_macos_surface
available extensions:
    VK_KHR_get_physical_device_properties2
    VK_KHR_surface
    VK_MVK_macos_surface
    VK_EXT_debug_report
    VK_EXT_debug_utils
Graphics family index: 0
Present family index: 0
mrtrizer commented 5 years ago

I've finally succeed. I've rolled back to 1.1.77.0 and debug layouts started working. It's appeared that I didn't enable VK_KHR_swapchain in VkDeviceCreateInfo. After enabling it I quickly gone forward. I used your cube example to initalize NSWindow with layout backed NSView right way and finally seen the triangle. I didn't think this gonna take so much time :) (if anybody intereseted in pure initialization from code, refer this example: https://github.com/mrtrizer/VulkanTriangleMacOS/blob/master/src/macOSInterface.m) So, the main problem I see now is that Vulkan didn't show me output from debug layers. But seems the problem is not on side of MoltenVK. Maybe tutorial I followed is outdate or something like this. Anyway, thanks for Molten! 👍

billhollings commented 5 years ago

@mrtrizer I'm glad you got it sorted out!

And nice to hear you are finding MoltenVK to be valuable!