我正在尝试在我的C#程序中使用新的vulkan API。因为我们只能访问头文件,所以我复制了代码,并在不安全的类中对其进行了修改以包装它:
BIOS
所有PFN_xxx最初都是标题中的函数指针,我直观地将它们重写为委托。所以,目前,它正在发挥作用。但是,在代码的后期,我有从dll导入的以下函数,这会产生错误:
unsafe public static class Vk {
...
public delegate void* PFN_vkAllocationFunction(...);
public delegate void* PFN_vkReallocationFunction(...);
public delegate void PFN_vkFreeFunction(...);
public delegate void PFN_vkInternalAllocationNotification(...);
public delegate void PFN_vkInternalFreeNotification(...);
public struct AllocationCallbacks {
void* pUserData;
PFN_vkAllocationFunction pfnAllocation;
PFN_vkReallocationFunction pfnReallocation;
PFN_vkFreeFunction pfnFree;
PFN_vkInternalAllocationNotification pfnInternalAllocation;
PFN_vkInternalFreeNotification pfnInternalFree;
}
...
}
错误是 CS0208 ,由以下消息描述:“无法获取地址,获取大小,或声明指向托管类型的指针('Vk.AllocationCallbacks “)“ 通过谷歌搜索,我了解AllocationCallbacks是管理的,因为其中的代表。那么我该如何解决这个问题呢?我在网上找不到令人满意的答案,我试图在我的结构中封送代理作为非托管函数指针(像这样),但它似乎不起作用,编译器保留错误。
[DllImport("Dll\vulkan-1.dll", EntryPoint = "VkCreateInstance")]
public extern static Result CreateInstance(
InstanceCreateInfo* pCreateInfo,
AllocationCallbacks* pAllocator, <- error here
Instance* pInstance);
这是另一个问题:我有一个结构,其中定义了固定大小的struct数组。原始声明是:
public struct AllocationCallbacks {
void* pUserData;
[MarshalAs(UnmanagedType.FunctionPtr)]
PFN_vkAllocationFunction pfnAllocation;
[MarshalAs(UnmanagedType.FunctionPtr)]
PFN_vkReallocationFunction pfnReallocation;
[MarshalAs(UnmanagedType.FunctionPtr)]
PFN_vkFreeFunction pfnFree;
[MarshalAs(UnmanagedType.FunctionPtr)]
PFN_vkInternalAllocationNotification pfnInternalAllocation;
[MarshalAs(UnmanagedType.FunctionPtr)]
PFN_vkInternalFreeNotification pfnInternalFree;
}
我只是这样翻译:
typedef struct VkPhysicalDeviceMemoryProperties {
uint32_t memoryTypeCount;
VkMemoryType memoryTypes[VK_MAX_MEMORY_TYPES];
uint32_t memoryHeapCount;
VkMemoryHeap memoryHeaps[VK_MAX_MEMORY_HEAPS];
} VkPhysicalDeviceMemoryProperties;
但是,我有同样的问题,着名的CS0208错误,因为以下函数导入:
[StructLayout(LayoutKind.Sequential)]
public struct PhysicalDeviceMemoryProperties {
uint memoryTypeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_MEMORY_TYPES)]
MemoryType[] memoryTypes;
uint memoryHeapCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_MEMORY_HEAPS)]
MemoryHeap[] memoryHeaps;
}
而这一次,错误是因为固定大小的数组。有什么想法吗?
感谢您的帮助!