vulkan-go / vulkan

Vulkan API bindings for Go programming language
MIT License
742 stars 55 forks source link

How can i get a vk.Surface ? #61

Closed fijosilo closed 2 years ago

fijosilo commented 2 years ago

Hello I am trying to create a vk.Surface and i know of two ways to create it.

Using platform specific code like the function vkCreateWin32SurfaceKHR from the Vulkan API, however this function does not seam to be defined in these bindings.

Using the function CreateWindowSurface from GLFW with is platform agnostic and the preferred way to do it when using GLFW, the problem is i don't know how to use this function and the function does not seam to be very well documented. Most functions i have seen so far work by me creating the data type and passing a pointer of that data type to a function but the function CreateWindowSurface from GLFW does not work this way, well the one from these bindings at least does not seam to work this way because when i search for it on the internet i keep finding things like "glfwCreateWindowSurface(instance, window, nullptr, &surface)" but the one from this bindings is "func (window *Window) CreateWindowSurface(instance interface{}, allocCallbacks unsafe.Pointer) (surface uintptr, err error)" and dosen't even return a vk.Surface. It gets the surface grabs a unsafe pointer to it and then gets it's pointer as a uintptr like so "return uintptr(unsafe.Pointer(&vulkanSurface)), nil".

I have tried to drill thru these pointers to get a vk.Surface like so "(vk.Surface)(unsafe.Pointer(surface))" but when i then try to pass this surface to the function GetPhysicalDeviceSurfaceSupport the validation layers complain this is not a valid handle "The Vulkan spec states: surface must be a valid VkSurfaceKHR handle".

So my question is how do i create vk.Surface and can the CreateWindowSurface from GLFW be better documented in relation to this issue since it works in such a different way from the rest of the functions of the Vulkan API?

fijosilo commented 2 years ago

I figured it out i need to convert the uintptr to an unsafe.Pointer unsafe.Pointer(surface) then type cast this pointer to a vk.Surface pointer (*vk.Surface)(unsafe.Pointer(surface)), because you can't convert an address to a vk.Surface, then you can grab what the pointer points at *(*vk.Surface)(unsafe.Pointer(surface)) and that will be a vk.Surface.

This being said this was really a problem with me not understanding pointers and type casting properly, and it doesn't have anything to do with vulkan-go specifically, so feel free to delete this issue.

xlab commented 2 years ago

No probs, let it just be there. Yeah, unsafe pointer casting is what I originally tried to avoid in the lib. There must be a higher level abstraction for that!