jpbruyere / vk.net

Vulkan binding for .net
MIT License
32 stars 3 forks source link

vkEnumerateInstanceLayerProperties always gives AccessViolationException #10

Closed TechLiam closed 4 years ago

TechLiam commented 4 years ago

So I will start by saying I'm not sure what I'm about to say is right but please stick with me.

This is the code I'm using to call the vkEnumerateInstanceLayerProperties

if (vkEnumerateInstanceLayerProperties(out var count, IntPtr.Zero) != VkResult.Success) return;
var sizeStruct = Marshal.SizeOf<VkLayerProperties>();
var ptrSupLayers = Marshal.AllocHGlobal(sizeStruct * (int) count);
if (vkEnumerateInstanceLayerProperties(out count, ptrSupLayers) != VkResult.Success) return;
var result = new string[count];
var tmp = ptrSupLayers;
for (var i = 0; i < count; i++) {
    result[i] = Marshal.PtrToStringAnsi(tmp);
    tmp += sizeStruct;
}
Marshal.FreeHGlobal(ptrSupLayers);

This is effectivally your code from VKE for vkEnumerateInstanceExtensionProperties but for Layers.

Looking at the generated code

[CalliRewrite]
public static unsafe VkResult vkEnumerateInstanceLayerProperties(out uint pPropertyCount, IntPtr pProperties)
{
    fixed (uint* numPtr = &pPropertyCount)
    {
        IntPtr num = pProperties;
        // ISSUE: cast to a function pointer type
        // ISSUE: function pointer call
        return __calli((__FnPtr<VkResult (uint*, IntPtr)>) Vk.vkEnumerateInstanceLayerProperties_ptr)((IntPtr) numPtr, (uint*) num);
    }
}

To my unaware of how such things work but learning mind this looks like things are the wrong cast in that

return __calli((__FnPtr<VkResult (uint*, IntPtr)>) Vk.vkEnumerateInstanceLayerProperties_ptr)((IntPtr) numPtr, (uint*) num);

should be

return __calli((__FnPtr<VkResult (uint*, IntPtr)>) Vk.vkEnumerateInstanceLayerProperties_ptr)((uint*) numPtr, (IntPtr) num);

But I'm happy to accept if I'm wrong please let me know

jpbruyere commented 4 years ago

Could you please point the faulting line, this code works as expected on my linux, is it the first call to vkEnumerateInstanceLayerProperties?

jpbruyere commented 4 years ago

For your remark concerning the casting (IntPtr) instead of (uint) you may be right, there's maybe a useless extra conversion but uint to/from IntPtr is implicit, it should not cause exception (I don't think that's the source of your error). I'll put this check on my todo list for next version.

TechLiam commented 4 years ago

Ah sorry about the specifics missing yes it is the first call to vkEnumerateInstanceLayerProperties that gives the exception.

I think the course of it might be that pProperties is been put into num which is an IntPtr then changed into a uint* (yes this is a pointer but only to a uint space) so when it trying to write something to the pProperties address it's going out of bounds maybe I'm probably wrong about that.

More information on my system: Window 10 .Net Core Console app

When would a next release be likely to happen no rush just interested in how active you were on the development of this :) I'm only playing around with Vulkan for fun so no rush from me

jpbruyere commented 4 years ago

The problem is that the function pointer for vkEnumerateInstanceLayerProperties is loaded after device or instance creation. There are some preloaded functions by the static ctor of 'Vk' but 'vkEnumerateInstanceLayerProperties' was not preloaded (https://github.com/jpbruyere/vk.net/blob/master/vk.generator/Program.cs#L181), so I've just had it in a '0.2.2-beta' release that should be available on nuget now or in a few minuts.

TechLiam commented 4 years ago

Thanks, @jpbruyere that has solved my issue grate work. I would be happy to help out this project in some way if you would like me to do anything else I will report any other issues I come up against while I play around with Vulkan using your wrapper

jpbruyere commented 4 years ago

Thank you, I mostly program on linux, so I'm happy to have some return from windows users. If you want to help, feel free to make propositions and push requests, it is also always welcome to improve documentation or make tutorials (even small ones, install steps,...), each comments or remarks will be welcome.