Possible problem with C-Interop?

I've been writing a wrapper library for Vulkan targeting MacOS. It was going fine until I hit this obstacle. There's a call to SDL_Vulkan_CreateSurface which is supposed to return a pointer to a surface VkSurfaceKHR. However, the returned pointer is not a memory address, but rather the number 1. At first I thought this could be a bug with SDL, but then I noticed another call to another Vulkan API is returning 2. I am out of options now and I'm hoping it's something I can fix on my end. This is the output

1== CREATE VULKAN INSTANCE
Enabling extensions:
VK_KHR_surface
VK_MVK_macos_surface
===

2== CREATE PHYSICAL DEVICE
Created GPU (Physical device): 0x00007fe23b81e380

3== CREATE SURFACE (Metal->MoltenVK)
Created Surface: 0x0000000000000001

4== CREATE DEVICE
Chosen queue Family is 0
Created Device: 0x00007fe23c017a10

5== CREATE COMMAND POOL
Created Command Pool: 0x0000000000000002

6== CREATE COMMAND BUFFER
Created Command Buffer: 0x00007fe23a4d2bd0

7== CREATE SWAPCHAIN
Segmentation fault: 11

The responsible function is this:

    func createVulkanSurface() throws -> Surface {
        var surface = VkSurfaceKHR(bitPattern: 0)

        if SDL_Vulkan_CreateSurface(window, self.instance!.pointer, &surface) != SDL_TRUE {
            throw lastSDLError()
        }

        return Surface(instance: self.instance!, surface: surface!)
    }

The code is here: https://github.com/alexanderuv/vulkanSwift

Specific code above is here: https://github.com/alexanderuv/vulkanSwift/blob/master/Sources/SwiftSDL2/Window.swift#L198

Any help will be appreciated!

Just in case, without trying out the code (I don’t have Swift 5). Did you check if all the unwrapping Optionals are proper?

@Lantua Yes I'm using the same pattern everywhere, and also I tried different approaches - same result though.
I'm thinking it could be a problem with Swift itself :frowning:

I think that the behavior might be correct, SDL_Vulkan_CreateSurface returns a handle, not a raw pointer, so this "pointer" might be the handle from Vulkan. I would suggest running the implementation in C and seeing the results.

@compnerd Yes, somebody from stackoverflow pointed out the fact that this is a Non-dispatchable handle and therefore that value must be correct. Then the seg-fault must be coming from somewhere else.

I will look for it. Will post if I have any news.

Thanks!

Turns out it was 2 problems:
1-> The handle is not a pointer, so the value was fine.
2-> The reason for the failure was an invalid pointer to a function, due to not registering the extension "VK_KHR_swapchain" when creating the device

Also when passing the string I had more problems due to the way Swift manages pointers and Strings. Turns out converting a String array to a C String pointer/array is not trivial. I got it resolved by using this approach:

https://oleb.net/blog/2016/10/swift-array-of-c-strings/

Terms of Service

Privacy Policy

Cookie Policy