C# DllImport:调用 vkCreateInstance 时出现 AccessViolationException

C# DllImport : AccessViolationException when calling a vkCreateInstance

我正在尝试在 C# 中创建 Vulkan 包装器,但在调用函数时遇到了一些问题。我重写了 vulkan.h header 如下:

public static class Vk {
    [StructLayout(LayoutKind.Sequential)] public class Instance { }

    public enum Result {
        ...
    }
    public enum StructureType {
        ...
    }

    [StructLayout(LayoutKind.Sequential)] public class ApplicationInfo {
        public StructureType sType;
        public IntPtr pNext;
        public string pApplicationName;
        public uint applicationVersion;
        public string pEngineName;
        public uint engineVersion;
        public uint apiVersion;
    }

    [StructLayout(LayoutKind.Sequential)] public class InstanceCreateInfo {
        public StructureType sType;
        public IntPtr pNext;
        public uint flags_VkInstanceCreateFlags;
        public ApplicationInfo pApplicationInfo;
        public uint enabledLayerCount;
        public string[] ppEnabledLayerNames;
        public uint enabledExtensionCount;
        public string[] ppEnabledExtensionNames;
    }

    [DllImport("vulkan-1.dll", EntryPoint = "vkCreateInstance")]
    public extern static Result CreateInstance(
        InstanceCreateInfo pCreateInfo,
        IntPtr AllocationCallbacks_pAllocator,
        out IntPtr pInstance_Instance);
}

这个函数在C中的原始声明是

VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(
    const VkInstanceCreateInfo*                 pCreateInfo,
    const VkAllocationCallbacks*                pAllocator,
    VkInstance*                                 pInstance);

现在当我调用我的函数时,我是这样做的:

Vk.InstanceCreateInfo instance_create_info = new Vk.InstanceCreateInfo();
...

IntPtr hinstance;
Vk.Result result = Vk.CreateInstance(instance_create_info, IntPtr.Zero, out hinstance);   <-- error AccessViolationException

我不明白我的问题在哪里,因为它似乎是一个有效的解决方案:

我尝试用

初始化我的 IntPtr hinstance
Marshal.AllocHGlobal(Marshal.SizeOf<Vk.Instance>());  

我也尝试 "convert" 我的 instance_create_info 到另一个 IntPtr Marshal.StructureToPtr(...); 我试图通过instance_create_infoinstance 通过 ref 关键字。显然,没有任何效果。

有什么想法吗?


编辑:

原生函数使用如下:

//Definition
typedef struct VkApplicationInfo {
    VkStructureType    sType;
    const void*        pNext;
    const char*        pApplicationName;
    uint32_t           applicationVersion;
    const char*        pEngineName;
    uint32_t           engineVersion;
    uint32_t           apiVersion;
} VkApplicationInfo;`
typedef struct VkInstanceCreateInfo {
    VkStructureType             sType;
    const void*                 pNext;
    VkInstanceCreateFlags       flags;
    const VkApplicationInfo*    pApplicationInfo;
    uint32_t                    enabledLayerCount;
    const char* const*          ppEnabledLayerNames;
    uint32_t                    enabledExtensionCount;
    const char* const*          ppEnabledExtensionNames;
} VkInstanceCreateInfo;

#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
VK_DEFINE_HANDLE(VkInstance)

//Code
VkApplicationInfo application_info{};
    application_info.sType                  = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    application_info.apiVersion             = VK_API_VERSION;
    application_info.applicationVersion     = VK_MAKE_VERSION( 1, 0, 0 );
    application_info.pApplicationName       = "";
    application_info.engineVersion          = VK_MAKE_VERSION( 1, 0, 0 );
    application_info.pEngineName            = "";

VkInstanceCreateInfo instance_create_info{};
    instance_create_info.sType                      = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instance_create_info.pApplicationInfo           = &application_info;
    instance_create_info.enabledLayerCount          = 0
    instance_create_info.ppEnabledLayerNames        = nullptr
    instance_create_info.enabledExtensionCount      = 0
    instance_create_info.ppEnabledExtensionNames    = nullptr

VkInstance _instance = nullptr;
assert( !vkCreateInstance( &instance_create_info, nullptr, &_instance ) );

删除第三个参数的 out 关键字或将其声明为相应的结构。

看来您正在尝试做类似的事情 this guy 为 c# 制作了一个包装器。对你有用。转到 Source/SharpVulkan/Generated/Functions。

    public static unsafe Instance CreateInstance(ref InstanceCreateInfo createInfo, AllocationCallbacks* allocator = null)
    {
        Instance instance;
        fixed (InstanceCreateInfo* __createInfo__ = &createInfo)
        {
            vkCreateInstance(__createInfo__, allocator, &instance).CheckError();
        }
        return instance;
    }

    [DllImport("vulkan-1.dll", CallingConvention = CallingConvention.StdCall)]
    internal static extern unsafe Result vkCreateInstance(InstanceCreateInfo* createInfo, AllocationCallbacks* allocator, Instance* instance);

    internal static unsafe void EnumerateInstanceExtensionProperties(byte* layerName, ref uint propertyCount, ExtensionProperties* properties)
    {
        fixed (uint* __propertyCount__ = &propertyCount)
        {
            vkEnumerateInstanceExtensionProperties(layerName, __propertyCount__, properties).CheckError();
        }
    }

希望对您有所帮助:)