-1

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

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

我不明白我的问题出在哪里,因为它似乎是一个有效的解决方案:StackOverflow : AccessViolationException when call vkEnumeratePhysicalDevices via pInvoke from c#

我尝试通过初始化我的IntPtr hinstance

Marshal.AllocHGlobal(Marshal.SizeOf<Vk.Instance>());  

我还尝试使用Marshal.StructureToPtr(...); 将我的 instance_create_info “转换”为另一个 IntPtr;我试图通过ref 关键字传递instance_create_infoinstance 。显然,没有任何效果。

任何想法 ?


编辑 :

本机函数使用如下:

//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 ) );
4

2 回答 2

1

看来您正在尝试做类似的事情,这个家伙为 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();
        }
    }

希望它有帮助:)

于 2016-05-06T09:11:02.677 回答
-1

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

于 2016-04-16T14:33:30.210 回答